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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | static void nfs_free_delegation(struct nfs_delegation *delegation) |
| 24 | { |
| 25 | if (delegation->cred) |
| 26 | put_rpccred(delegation->cred); |
| 27 | kfree(delegation); |
| 28 | } |
| 29 | |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 30 | static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 31 | { |
| 32 | struct inode *inode = state->inode; |
| 33 | struct file_lock *fl; |
| 34 | int status; |
| 35 | |
| 36 | for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) { |
| 37 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
| 38 | continue; |
| 39 | if ((struct nfs_open_context *)fl->fl_file->private_data != ctx) |
| 40 | continue; |
| 41 | status = nfs4_lock_delegation_recall(state, fl); |
| 42 | if (status >= 0) |
| 43 | continue; |
| 44 | switch (status) { |
| 45 | default: |
| 46 | printk(KERN_ERR "%s: unhandled error %d.\n", |
| 47 | __FUNCTION__, status); |
| 48 | case -NFS4ERR_EXPIRED: |
| 49 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
| 50 | case -NFS4ERR_STALE_CLIENTID: |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 51 | nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 52 | goto out_err; |
| 53 | } |
| 54 | } |
| 55 | return 0; |
| 56 | out_err: |
| 57 | return status; |
| 58 | } |
| 59 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 60 | 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] | 61 | { |
| 62 | struct nfs_inode *nfsi = NFS_I(inode); |
| 63 | struct nfs_open_context *ctx; |
| 64 | struct nfs4_state *state; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 65 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | again: |
| 68 | spin_lock(&inode->i_lock); |
| 69 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 70 | state = ctx->state; |
| 71 | if (state == NULL) |
| 72 | continue; |
| 73 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 74 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 75 | if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) |
| 76 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | get_nfs_open_context(ctx); |
| 78 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame^] | 79 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 80 | if (err >= 0) |
| 81 | err = nfs_delegation_claim_locks(ctx, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | put_nfs_open_context(ctx); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 83 | if (err != 0) |
| 84 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | goto again; |
| 86 | } |
| 87 | spin_unlock(&inode->i_lock); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Set up a delegation on an inode |
| 92 | */ |
| 93 | void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 94 | { |
| 95 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
| 96 | |
| 97 | if (delegation == NULL) |
| 98 | return; |
| 99 | memcpy(delegation->stateid.data, res->delegation.data, |
| 100 | sizeof(delegation->stateid.data)); |
| 101 | delegation->type = res->delegation_type; |
| 102 | delegation->maxsize = res->maxsize; |
| 103 | put_rpccred(cred); |
| 104 | delegation->cred = get_rpccred(cred); |
| 105 | delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM; |
| 106 | NFS_I(inode)->delegation_state = delegation->type; |
| 107 | smp_wmb(); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Set up a delegation on an inode |
| 112 | */ |
| 113 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 114 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 115 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct nfs_inode *nfsi = NFS_I(inode); |
| 117 | struct nfs_delegation *delegation; |
| 118 | int status = 0; |
| 119 | |
Trond Myklebust | b3c52da | 2005-10-17 06:02:00 -0400 | [diff] [blame] | 120 | /* Ensure we first revalidate the attributes and page cache! */ |
| 121 | if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR))) |
| 122 | __nfs_revalidate_inode(NFS_SERVER(inode), inode); |
| 123 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 124 | delegation = kmalloc(sizeof(*delegation), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (delegation == NULL) |
| 126 | return -ENOMEM; |
| 127 | memcpy(delegation->stateid.data, res->delegation.data, |
| 128 | sizeof(delegation->stateid.data)); |
| 129 | delegation->type = res->delegation_type; |
| 130 | delegation->maxsize = res->maxsize; |
Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 131 | delegation->change_attr = nfsi->change_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | delegation->cred = get_rpccred(cred); |
| 133 | delegation->inode = inode; |
| 134 | |
| 135 | spin_lock(&clp->cl_lock); |
| 136 | if (nfsi->delegation == NULL) { |
| 137 | list_add(&delegation->super_list, &clp->cl_delegations); |
| 138 | nfsi->delegation = delegation; |
| 139 | nfsi->delegation_state = delegation->type; |
| 140 | delegation = NULL; |
| 141 | } else { |
| 142 | if (memcmp(&delegation->stateid, &nfsi->delegation->stateid, |
| 143 | sizeof(delegation->stateid)) != 0 || |
| 144 | delegation->type != nfsi->delegation->type) { |
| 145 | printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n", |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 146 | __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | status = -EIO; |
| 148 | } |
| 149 | } |
| 150 | spin_unlock(&clp->cl_lock); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 151 | kfree(delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return status; |
| 153 | } |
| 154 | |
| 155 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation) |
| 156 | { |
| 157 | int res = 0; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid); |
| 160 | nfs_free_delegation(delegation); |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | /* Sync all data to disk upon delegation return */ |
| 165 | static void nfs_msync_inode(struct inode *inode) |
| 166 | { |
| 167 | filemap_fdatawrite(inode->i_mapping); |
| 168 | nfs_wb_all(inode); |
| 169 | filemap_fdatawait(inode->i_mapping); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Basic procedure for returning a delegation to the server |
| 174 | */ |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 175 | 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] | 176 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 177 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | struct nfs_inode *nfsi = NFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | nfs_msync_inode(inode); |
| 181 | down_read(&clp->cl_sem); |
| 182 | /* Guard against new delegated open calls */ |
| 183 | down_write(&nfsi->rwsem); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 184 | nfs_delegation_claim_opens(inode, &delegation->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | up_write(&nfsi->rwsem); |
| 186 | up_read(&clp->cl_sem); |
| 187 | nfs_msync_inode(inode); |
| 188 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 189 | return nfs_do_return_delegation(inode, delegation); |
| 190 | } |
| 191 | |
| 192 | static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid) |
| 193 | { |
| 194 | struct nfs_delegation *delegation = nfsi->delegation; |
| 195 | |
| 196 | if (delegation == NULL) |
| 197 | goto nomatch; |
| 198 | if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data, |
| 199 | sizeof(delegation->stateid.data)) != 0) |
| 200 | goto nomatch; |
| 201 | list_del_init(&delegation->super_list); |
| 202 | nfsi->delegation = NULL; |
| 203 | nfsi->delegation_state = 0; |
| 204 | return delegation; |
| 205 | nomatch: |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | int nfs_inode_return_delegation(struct inode *inode) |
| 210 | { |
| 211 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 212 | struct nfs_inode *nfsi = NFS_I(inode); |
| 213 | struct nfs_delegation *delegation; |
| 214 | int err = 0; |
| 215 | |
| 216 | if (nfsi->delegation_state != 0) { |
| 217 | spin_lock(&clp->cl_lock); |
| 218 | delegation = nfs_detach_delegation_locked(nfsi, NULL); |
| 219 | spin_unlock(&clp->cl_lock); |
| 220 | if (delegation != NULL) |
| 221 | err = __nfs_inode_return_delegation(inode, delegation); |
| 222 | } |
| 223 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Return all delegations associated to a super block |
| 228 | */ |
| 229 | void nfs_return_all_delegations(struct super_block *sb) |
| 230 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 231 | struct nfs_client *clp = NFS_SB(sb)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | struct nfs_delegation *delegation; |
| 233 | struct inode *inode; |
| 234 | |
| 235 | if (clp == NULL) |
| 236 | return; |
| 237 | restart: |
| 238 | spin_lock(&clp->cl_lock); |
| 239 | list_for_each_entry(delegation, &clp->cl_delegations, super_list) { |
| 240 | if (delegation->inode->i_sb != sb) |
| 241 | continue; |
| 242 | inode = igrab(delegation->inode); |
| 243 | if (inode == NULL) |
| 244 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 245 | nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 247 | __nfs_inode_return_delegation(inode, delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | iput(inode); |
| 249 | goto restart; |
| 250 | } |
| 251 | spin_unlock(&clp->cl_lock); |
| 252 | } |
| 253 | |
Trond Myklebust | 10afec90 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 254 | static int nfs_do_expire_all_delegations(void *ptr) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 255 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 256 | struct nfs_client *clp = ptr; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 257 | struct nfs_delegation *delegation; |
| 258 | struct inode *inode; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 259 | |
| 260 | allow_signal(SIGKILL); |
| 261 | restart: |
| 262 | spin_lock(&clp->cl_lock); |
| 263 | if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0) |
| 264 | goto out; |
| 265 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) |
| 266 | goto out; |
| 267 | list_for_each_entry(delegation, &clp->cl_delegations, super_list) { |
| 268 | inode = igrab(delegation->inode); |
| 269 | if (inode == NULL) |
| 270 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 271 | nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 272 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 273 | __nfs_inode_return_delegation(inode, delegation); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 274 | iput(inode); |
Trond Myklebust | 26c78e1 | 2006-01-03 09:55:58 +0100 | [diff] [blame] | 275 | goto restart; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 276 | } |
| 277 | out: |
| 278 | spin_unlock(&clp->cl_lock); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 279 | nfs_put_client(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 280 | module_put_and_exit(0); |
| 281 | } |
| 282 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 283 | void nfs_expire_all_delegations(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 284 | { |
| 285 | struct task_struct *task; |
| 286 | |
| 287 | __module_get(THIS_MODULE); |
| 288 | atomic_inc(&clp->cl_count); |
| 289 | task = kthread_run(nfs_do_expire_all_delegations, clp, |
| 290 | "%u.%u.%u.%u-delegreturn", |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 291 | NIPQUAD(clp->cl_addr.sin_addr)); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 292 | if (!IS_ERR(task)) |
| 293 | return; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 294 | nfs_put_client(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 295 | module_put(THIS_MODULE); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. |
| 300 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 301 | void nfs_handle_cb_pathdown(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | struct nfs_delegation *delegation; |
| 304 | struct inode *inode; |
| 305 | |
| 306 | if (clp == NULL) |
| 307 | return; |
| 308 | restart: |
| 309 | spin_lock(&clp->cl_lock); |
| 310 | list_for_each_entry(delegation, &clp->cl_delegations, super_list) { |
| 311 | inode = igrab(delegation->inode); |
| 312 | if (inode == NULL) |
| 313 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 314 | nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 316 | __nfs_inode_return_delegation(inode, delegation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | iput(inode); |
| 318 | goto restart; |
| 319 | } |
| 320 | spin_unlock(&clp->cl_lock); |
| 321 | } |
| 322 | |
| 323 | struct recall_threadargs { |
| 324 | struct inode *inode; |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 325 | struct nfs_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | const nfs4_stateid *stateid; |
| 327 | |
| 328 | struct completion started; |
| 329 | int result; |
| 330 | }; |
| 331 | |
| 332 | static int recall_thread(void *data) |
| 333 | { |
| 334 | struct recall_threadargs *args = (struct recall_threadargs *)data; |
| 335 | struct inode *inode = igrab(args->inode); |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 336 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | struct nfs_inode *nfsi = NFS_I(inode); |
| 338 | struct nfs_delegation *delegation; |
| 339 | |
| 340 | daemonize("nfsv4-delegreturn"); |
| 341 | |
| 342 | nfs_msync_inode(inode); |
| 343 | down_read(&clp->cl_sem); |
| 344 | down_write(&nfsi->rwsem); |
| 345 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 346 | delegation = nfs_detach_delegation_locked(nfsi, args->stateid); |
| 347 | if (delegation != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | args->result = 0; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 349 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | args->result = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | spin_unlock(&clp->cl_lock); |
| 352 | complete(&args->started); |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 353 | nfs_delegation_claim_opens(inode, args->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | up_write(&nfsi->rwsem); |
| 355 | up_read(&clp->cl_sem); |
| 356 | nfs_msync_inode(inode); |
| 357 | |
| 358 | if (delegation != NULL) |
| 359 | nfs_do_return_delegation(inode, delegation); |
| 360 | iput(inode); |
| 361 | module_put_and_exit(0); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Asynchronous delegation recall! |
| 366 | */ |
| 367 | int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid) |
| 368 | { |
| 369 | struct recall_threadargs data = { |
| 370 | .inode = inode, |
| 371 | .stateid = stateid, |
| 372 | }; |
| 373 | int status; |
| 374 | |
| 375 | init_completion(&data.started); |
| 376 | __module_get(THIS_MODULE); |
| 377 | status = kernel_thread(recall_thread, &data, CLONE_KERNEL); |
| 378 | if (status < 0) |
| 379 | goto out_module_put; |
| 380 | wait_for_completion(&data.started); |
| 381 | return data.result; |
| 382 | out_module_put: |
| 383 | module_put(THIS_MODULE); |
| 384 | return status; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Retrieve the inode associated with a delegation |
| 389 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | struct nfs_delegation *delegation; |
| 393 | struct inode *res = NULL; |
| 394 | spin_lock(&clp->cl_lock); |
| 395 | list_for_each_entry(delegation, &clp->cl_delegations, super_list) { |
| 396 | if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
| 397 | res = igrab(delegation->inode); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | spin_unlock(&clp->cl_lock); |
| 402 | return res; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Mark all delegations as needing to be reclaimed |
| 407 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 408 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
| 410 | struct nfs_delegation *delegation; |
| 411 | spin_lock(&clp->cl_lock); |
| 412 | list_for_each_entry(delegation, &clp->cl_delegations, super_list) |
| 413 | delegation->flags |= NFS_DELEGATION_NEED_RECLAIM; |
| 414 | spin_unlock(&clp->cl_lock); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Reap all unclaimed delegations after reboot recovery is done |
| 419 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 420 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
| 422 | struct nfs_delegation *delegation, *n; |
| 423 | LIST_HEAD(head); |
| 424 | spin_lock(&clp->cl_lock); |
| 425 | list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) { |
| 426 | if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) |
| 427 | continue; |
| 428 | list_move(&delegation->super_list, &head); |
| 429 | NFS_I(delegation->inode)->delegation = NULL; |
| 430 | NFS_I(delegation->inode)->delegation_state = 0; |
| 431 | } |
| 432 | spin_unlock(&clp->cl_lock); |
| 433 | while(!list_empty(&head)) { |
| 434 | delegation = list_entry(head.next, struct nfs_delegation, super_list); |
| 435 | list_del(&delegation->super_list); |
| 436 | nfs_free_delegation(delegation); |
| 437 | } |
| 438 | } |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 439 | |
| 440 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) |
| 441 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 442 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 443 | struct nfs_inode *nfsi = NFS_I(inode); |
| 444 | struct nfs_delegation *delegation; |
| 445 | int res = 0; |
| 446 | |
| 447 | if (nfsi->delegation_state == 0) |
| 448 | return 0; |
| 449 | spin_lock(&clp->cl_lock); |
| 450 | delegation = nfsi->delegation; |
| 451 | if (delegation != NULL) { |
| 452 | memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); |
| 453 | res = 1; |
| 454 | } |
| 455 | spin_unlock(&clp->cl_lock); |
| 456 | return res; |
| 457 | } |