Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/delegation.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFS file delegation management |
| 7 | * |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/completion.h> |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 10 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | |
| 15 | #include <linux/nfs4.h> |
| 16 | #include <linux/nfs_fs.h> |
| 17 | #include <linux/nfs_xdr.h> |
| 18 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 19 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 21 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 23 | static void nfs_do_free_delegation(struct nfs_delegation *delegation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | kfree(delegation); |
| 26 | } |
| 27 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 28 | static void nfs_free_delegation_callback(struct rcu_head *head) |
| 29 | { |
| 30 | struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu); |
| 31 | |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 32 | nfs_do_free_delegation(delegation); |
| 33 | } |
| 34 | |
| 35 | static void nfs_free_delegation(struct nfs_delegation *delegation) |
| 36 | { |
| 37 | struct rpc_cred *cred; |
| 38 | |
| 39 | cred = rcu_dereference(delegation->cred); |
| 40 | rcu_assign_pointer(delegation->cred, NULL); |
| 41 | call_rcu(&delegation->rcu, nfs_free_delegation_callback); |
| 42 | if (cred) |
| 43 | put_rpccred(cred); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 46 | static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 47 | { |
| 48 | struct inode *inode = state->inode; |
| 49 | struct file_lock *fl; |
| 50 | int status; |
| 51 | |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 52 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 53 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
| 54 | continue; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 55 | if (nfs_file_open_context(fl->fl_file) != ctx) |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 56 | continue; |
| 57 | status = nfs4_lock_delegation_recall(state, fl); |
| 58 | if (status >= 0) |
| 59 | continue; |
| 60 | switch (status) { |
| 61 | default: |
| 62 | printk(KERN_ERR "%s: unhandled error %d.\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 63 | __func__, status); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 64 | case -NFS4ERR_EXPIRED: |
| 65 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
| 66 | case -NFS4ERR_STALE_CLIENTID: |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 67 | nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 68 | goto out_err; |
| 69 | } |
| 70 | } |
| 71 | return 0; |
| 72 | out_err: |
| 73 | return status; |
| 74 | } |
| 75 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 76 | static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct nfs_inode *nfsi = NFS_I(inode); |
| 79 | struct nfs_open_context *ctx; |
| 80 | struct nfs4_state *state; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 81 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | again: |
| 84 | spin_lock(&inode->i_lock); |
| 85 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 86 | state = ctx->state; |
| 87 | if (state == NULL) |
| 88 | continue; |
| 89 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 90 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 91 | if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) |
| 92 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | get_nfs_open_context(ctx); |
| 94 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 95 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 96 | if (err >= 0) |
| 97 | err = nfs_delegation_claim_locks(ctx, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | put_nfs_open_context(ctx); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 99 | if (err != 0) |
| 100 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | goto again; |
| 102 | } |
| 103 | spin_unlock(&inode->i_lock); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Set up a delegation on an inode |
| 108 | */ |
| 109 | void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 110 | { |
| 111 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 112 | struct rpc_cred *oldcred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | if (delegation == NULL) |
| 115 | return; |
| 116 | memcpy(delegation->stateid.data, res->delegation.data, |
| 117 | sizeof(delegation->stateid.data)); |
| 118 | delegation->type = res->delegation_type; |
| 119 | delegation->maxsize = res->maxsize; |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 120 | oldcred = delegation->cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | delegation->cred = get_rpccred(cred); |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame^] | 122 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | NFS_I(inode)->delegation_state = delegation->type; |
| 124 | smp_wmb(); |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 125 | put_rpccred(oldcred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 128 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
| 129 | { |
| 130 | int res = 0; |
| 131 | |
| 132 | res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync); |
| 133 | nfs_free_delegation(delegation); |
| 134 | return res; |
| 135 | } |
| 136 | |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 137 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) |
| 138 | { |
| 139 | struct inode *inode = NULL; |
| 140 | |
| 141 | spin_lock(&delegation->lock); |
| 142 | if (delegation->inode != NULL) |
| 143 | inode = igrab(delegation->inode); |
| 144 | spin_unlock(&delegation->lock); |
| 145 | return inode; |
| 146 | } |
| 147 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 148 | static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid) |
| 149 | { |
| 150 | struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); |
| 151 | |
| 152 | if (delegation == NULL) |
| 153 | goto nomatch; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 154 | spin_lock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 155 | if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data, |
| 156 | sizeof(delegation->stateid.data)) != 0) |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 157 | goto nomatch_unlock; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 158 | list_del_rcu(&delegation->super_list); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 159 | delegation->inode = NULL; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 160 | nfsi->delegation_state = 0; |
| 161 | rcu_assign_pointer(nfsi->delegation, NULL); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 162 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 163 | return delegation; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 164 | nomatch_unlock: |
| 165 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 166 | nomatch: |
| 167 | return NULL; |
| 168 | } |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Set up a delegation on an inode |
| 172 | */ |
| 173 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 174 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 175 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | struct nfs_inode *nfsi = NFS_I(inode); |
| 177 | struct nfs_delegation *delegation; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 178 | struct nfs_delegation *freeme = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | int status = 0; |
| 180 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 181 | delegation = kmalloc(sizeof(*delegation), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (delegation == NULL) |
| 183 | return -ENOMEM; |
| 184 | memcpy(delegation->stateid.data, res->delegation.data, |
| 185 | sizeof(delegation->stateid.data)); |
| 186 | delegation->type = res->delegation_type; |
| 187 | delegation->maxsize = res->maxsize; |
Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 188 | delegation->change_attr = nfsi->change_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | delegation->cred = get_rpccred(cred); |
| 190 | delegation->inode = inode; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 191 | spin_lock_init(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 194 | if (rcu_dereference(nfsi->delegation) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (memcmp(&delegation->stateid, &nfsi->delegation->stateid, |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 196 | sizeof(delegation->stateid)) == 0 && |
| 197 | delegation->type == nfsi->delegation->type) { |
| 198 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 200 | /* |
| 201 | * Deal with broken servers that hand out two |
| 202 | * delegations for the same file. |
| 203 | */ |
| 204 | dfprintk(FILE, "%s: server %s handed out " |
| 205 | "a duplicate delegation!\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 206 | __func__, clp->cl_hostname); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 207 | if (delegation->type <= nfsi->delegation->type) { |
| 208 | freeme = delegation; |
| 209 | delegation = NULL; |
| 210 | goto out; |
| 211 | } |
| 212 | freeme = nfs_detach_delegation_locked(nfsi, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 214 | list_add_rcu(&delegation->super_list, &clp->cl_delegations); |
| 215 | nfsi->delegation_state = delegation->type; |
| 216 | rcu_assign_pointer(nfsi->delegation, delegation); |
| 217 | delegation = NULL; |
Trond Myklebust | 412c77c | 2007-07-03 16:10:55 -0400 | [diff] [blame] | 218 | |
| 219 | /* Ensure we revalidate the attributes and page cache! */ |
| 220 | spin_lock(&inode->i_lock); |
| 221 | nfsi->cache_validity |= NFS_INO_REVAL_FORCED; |
| 222 | spin_unlock(&inode->i_lock); |
| 223 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 224 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 603c83d | 2007-10-18 19:59:20 -0400 | [diff] [blame] | 226 | if (delegation != NULL) |
| 227 | nfs_free_delegation(delegation); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 228 | if (freeme != NULL) |
| 229 | nfs_do_return_delegation(inode, freeme, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return status; |
| 231 | } |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* Sync all data to disk upon delegation return */ |
| 234 | static void nfs_msync_inode(struct inode *inode) |
| 235 | { |
| 236 | filemap_fdatawrite(inode->i_mapping); |
| 237 | nfs_wb_all(inode); |
| 238 | filemap_fdatawait(inode->i_mapping); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Basic procedure for returning a delegation to the server |
| 243 | */ |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 244 | static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 246 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | struct nfs_inode *nfsi = NFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | nfs_msync_inode(inode); |
| 250 | down_read(&clp->cl_sem); |
| 251 | /* Guard against new delegated open calls */ |
| 252 | down_write(&nfsi->rwsem); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 253 | nfs_delegation_claim_opens(inode, &delegation->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | up_write(&nfsi->rwsem); |
| 255 | up_read(&clp->cl_sem); |
| 256 | nfs_msync_inode(inode); |
| 257 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 258 | return nfs_do_return_delegation(inode, delegation, 1); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 261 | /* |
| 262 | * This function returns the delegation without reclaiming opens |
| 263 | * or protecting against delegation reclaims. |
| 264 | * It is therefore really only safe to be called from |
| 265 | * nfs4_clear_inode() |
| 266 | */ |
| 267 | void nfs_inode_return_delegation_noreclaim(struct inode *inode) |
| 268 | { |
| 269 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 270 | struct nfs_inode *nfsi = NFS_I(inode); |
| 271 | struct nfs_delegation *delegation; |
| 272 | |
| 273 | if (rcu_dereference(nfsi->delegation) != NULL) { |
| 274 | spin_lock(&clp->cl_lock); |
| 275 | delegation = nfs_detach_delegation_locked(nfsi, NULL); |
| 276 | spin_unlock(&clp->cl_lock); |
| 277 | if (delegation != NULL) |
| 278 | nfs_do_return_delegation(inode, delegation, 0); |
| 279 | } |
| 280 | } |
| 281 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 282 | int nfs_inode_return_delegation(struct inode *inode) |
| 283 | { |
| 284 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 285 | struct nfs_inode *nfsi = NFS_I(inode); |
| 286 | struct nfs_delegation *delegation; |
| 287 | int err = 0; |
| 288 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 289 | if (rcu_dereference(nfsi->delegation) != NULL) { |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 290 | spin_lock(&clp->cl_lock); |
| 291 | delegation = nfs_detach_delegation_locked(nfsi, NULL); |
| 292 | spin_unlock(&clp->cl_lock); |
| 293 | if (delegation != NULL) |
| 294 | err = __nfs_inode_return_delegation(inode, delegation); |
| 295 | } |
| 296 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Return all delegations associated to a super block |
| 301 | */ |
| 302 | void nfs_return_all_delegations(struct super_block *sb) |
| 303 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 304 | struct nfs_client *clp = NFS_SB(sb)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct nfs_delegation *delegation; |
| 306 | struct inode *inode; |
| 307 | |
| 308 | if (clp == NULL) |
| 309 | return; |
| 310 | restart: |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 311 | rcu_read_lock(); |
| 312 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 313 | inode = NULL; |
| 314 | spin_lock(&delegation->lock); |
| 315 | if (delegation->inode != NULL && delegation->inode->i_sb == sb) |
| 316 | inode = igrab(delegation->inode); |
| 317 | spin_unlock(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (inode == NULL) |
| 319 | continue; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 320 | spin_lock(&clp->cl_lock); |
| 321 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 323 | rcu_read_unlock(); |
| 324 | if (delegation != NULL) |
| 325 | __nfs_inode_return_delegation(inode, delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | iput(inode); |
| 327 | goto restart; |
| 328 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 329 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Trond Myklebust | 10afec90 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 332 | static int nfs_do_expire_all_delegations(void *ptr) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 333 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 334 | struct nfs_client *clp = ptr; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 335 | struct nfs_delegation *delegation; |
| 336 | struct inode *inode; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 337 | |
| 338 | allow_signal(SIGKILL); |
| 339 | restart: |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 340 | if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0) |
| 341 | goto out; |
| 342 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) |
| 343 | goto out; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 344 | rcu_read_lock(); |
| 345 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 346 | inode = nfs_delegation_grab_inode(delegation); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 347 | if (inode == NULL) |
| 348 | continue; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 349 | spin_lock(&clp->cl_lock); |
| 350 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 351 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 352 | rcu_read_unlock(); |
| 353 | if (delegation) |
| 354 | __nfs_inode_return_delegation(inode, delegation); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 355 | iput(inode); |
Trond Myklebust | 26c78e1 | 2006-01-03 09:55:58 +0100 | [diff] [blame] | 356 | goto restart; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 357 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 358 | rcu_read_unlock(); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 359 | out: |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 360 | nfs_put_client(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 361 | module_put_and_exit(0); |
| 362 | } |
| 363 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 364 | void nfs_expire_all_delegations(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 365 | { |
| 366 | struct task_struct *task; |
| 367 | |
| 368 | __module_get(THIS_MODULE); |
| 369 | atomic_inc(&clp->cl_count); |
| 370 | task = kthread_run(nfs_do_expire_all_delegations, clp, |
Chuck Lever | 5d8515c | 2007-12-10 14:57:16 -0500 | [diff] [blame] | 371 | "%s-delegreturn", |
| 372 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 373 | RPC_DISPLAY_ADDR)); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 374 | if (!IS_ERR(task)) |
| 375 | return; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 376 | nfs_put_client(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 377 | module_put(THIS_MODULE); |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. |
| 382 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 383 | void nfs_handle_cb_pathdown(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
| 385 | struct nfs_delegation *delegation; |
| 386 | struct inode *inode; |
| 387 | |
| 388 | if (clp == NULL) |
| 389 | return; |
| 390 | restart: |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 391 | rcu_read_lock(); |
| 392 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 393 | inode = nfs_delegation_grab_inode(delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (inode == NULL) |
| 395 | continue; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 396 | spin_lock(&clp->cl_lock); |
| 397 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 399 | rcu_read_unlock(); |
| 400 | if (delegation != NULL) |
| 401 | __nfs_inode_return_delegation(inode, delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | iput(inode); |
| 403 | goto restart; |
| 404 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 405 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | struct recall_threadargs { |
| 409 | struct inode *inode; |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 410 | struct nfs_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | const nfs4_stateid *stateid; |
| 412 | |
| 413 | struct completion started; |
| 414 | int result; |
| 415 | }; |
| 416 | |
| 417 | static int recall_thread(void *data) |
| 418 | { |
| 419 | struct recall_threadargs *args = (struct recall_threadargs *)data; |
| 420 | struct inode *inode = igrab(args->inode); |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 421 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | struct nfs_inode *nfsi = NFS_I(inode); |
| 423 | struct nfs_delegation *delegation; |
| 424 | |
| 425 | daemonize("nfsv4-delegreturn"); |
| 426 | |
| 427 | nfs_msync_inode(inode); |
| 428 | down_read(&clp->cl_sem); |
| 429 | down_write(&nfsi->rwsem); |
| 430 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 431 | delegation = nfs_detach_delegation_locked(nfsi, args->stateid); |
| 432 | if (delegation != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | args->result = 0; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 434 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | args->result = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | spin_unlock(&clp->cl_lock); |
| 437 | complete(&args->started); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 438 | nfs_delegation_claim_opens(inode, args->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | up_write(&nfsi->rwsem); |
| 440 | up_read(&clp->cl_sem); |
| 441 | nfs_msync_inode(inode); |
| 442 | |
| 443 | if (delegation != NULL) |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 444 | nfs_do_return_delegation(inode, delegation, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | iput(inode); |
| 446 | module_put_and_exit(0); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Asynchronous delegation recall! |
| 451 | */ |
| 452 | int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid) |
| 453 | { |
| 454 | struct recall_threadargs data = { |
| 455 | .inode = inode, |
| 456 | .stateid = stateid, |
| 457 | }; |
| 458 | int status; |
| 459 | |
| 460 | init_completion(&data.started); |
| 461 | __module_get(THIS_MODULE); |
| 462 | status = kernel_thread(recall_thread, &data, CLONE_KERNEL); |
| 463 | if (status < 0) |
| 464 | goto out_module_put; |
| 465 | wait_for_completion(&data.started); |
| 466 | return data.result; |
| 467 | out_module_put: |
| 468 | module_put(THIS_MODULE); |
| 469 | return status; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Retrieve the inode associated with a delegation |
| 474 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 475 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | struct nfs_delegation *delegation; |
| 478 | struct inode *res = NULL; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 479 | rcu_read_lock(); |
| 480 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 481 | spin_lock(&delegation->lock); |
| 482 | if (delegation->inode != NULL && |
| 483 | nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | res = igrab(delegation->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 486 | spin_unlock(&delegation->lock); |
| 487 | if (res != NULL) |
| 488 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 490 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | return res; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Mark all delegations as needing to be reclaimed |
| 496 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 497 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
| 499 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 500 | rcu_read_lock(); |
| 501 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame^] | 502 | set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 503 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Reap all unclaimed delegations after reboot recovery is done |
| 508 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 509 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 511 | struct nfs_delegation *delegation; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 512 | struct inode *inode; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 513 | restart: |
| 514 | rcu_read_lock(); |
| 515 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame^] | 516 | if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | continue; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 518 | inode = nfs_delegation_grab_inode(delegation); |
| 519 | if (inode == NULL) |
| 520 | continue; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 521 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 522 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 523 | spin_unlock(&clp->cl_lock); |
| 524 | rcu_read_unlock(); |
| 525 | if (delegation != NULL) |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 526 | nfs_free_delegation(delegation); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 527 | iput(inode); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 528 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 530 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 532 | |
| 533 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) |
| 534 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 535 | struct nfs_inode *nfsi = NFS_I(inode); |
| 536 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 537 | int ret = 0; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 538 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 539 | rcu_read_lock(); |
| 540 | delegation = rcu_dereference(nfsi->delegation); |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 541 | if (delegation != NULL) { |
| 542 | memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 543 | ret = 1; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 544 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 545 | rcu_read_unlock(); |
| 546 | return ret; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 547 | } |