2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
20 #include "delegation.h"
22 static struct nfs_delegation
*nfs_alloc_delegation(void)
24 return (struct nfs_delegation
*)kmalloc(sizeof(struct nfs_delegation
), GFP_KERNEL
);
27 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
30 put_rpccred(delegation
->cred
);
34 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
36 struct inode
*inode
= state
->inode
;
40 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
41 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
43 if ((struct nfs_open_context
*)fl
->fl_file
->private_data
!= ctx
)
45 status
= nfs4_lock_delegation_recall(state
, fl
);
50 printk(KERN_ERR
"%s: unhandled error %d.\n",
51 __FUNCTION__
, status
);
52 case -NFS4ERR_EXPIRED
:
53 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
54 case -NFS4ERR_STALE_CLIENTID
:
55 nfs4_schedule_state_recovery(NFS_SERVER(inode
)->nfs4_state
);
64 static void nfs_delegation_claim_opens(struct inode
*inode
)
66 struct nfs_inode
*nfsi
= NFS_I(inode
);
67 struct nfs_open_context
*ctx
;
68 struct nfs4_state
*state
;
72 spin_lock(&inode
->i_lock
);
73 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
77 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
79 get_nfs_open_context(ctx
);
80 spin_unlock(&inode
->i_lock
);
81 err
= nfs4_open_delegation_recall(ctx
->dentry
, state
);
83 err
= nfs_delegation_claim_locks(ctx
, state
);
84 put_nfs_open_context(ctx
);
89 spin_unlock(&inode
->i_lock
);
93 * Set up a delegation on an inode
95 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
97 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
99 if (delegation
== NULL
)
101 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
102 sizeof(delegation
->stateid
.data
));
103 delegation
->type
= res
->delegation_type
;
104 delegation
->maxsize
= res
->maxsize
;
106 delegation
->cred
= get_rpccred(cred
);
107 delegation
->flags
&= ~NFS_DELEGATION_NEED_RECLAIM
;
108 NFS_I(inode
)->delegation_state
= delegation
->type
;
113 * Set up a delegation on an inode
115 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
117 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
118 struct nfs_inode
*nfsi
= NFS_I(inode
);
119 struct nfs_delegation
*delegation
;
122 /* Ensure we first revalidate the attributes and page cache! */
123 if ((nfsi
->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_ATTR
)))
124 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
126 delegation
= nfs_alloc_delegation();
127 if (delegation
== NULL
)
129 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
130 sizeof(delegation
->stateid
.data
));
131 delegation
->type
= res
->delegation_type
;
132 delegation
->maxsize
= res
->maxsize
;
133 delegation
->change_attr
= nfsi
->change_attr
;
134 delegation
->cred
= get_rpccred(cred
);
135 delegation
->inode
= inode
;
137 spin_lock(&clp
->cl_lock
);
138 if (nfsi
->delegation
== NULL
) {
139 list_add(&delegation
->super_list
, &clp
->cl_delegations
);
140 nfsi
->delegation
= delegation
;
141 nfsi
->delegation_state
= delegation
->type
;
144 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
145 sizeof(delegation
->stateid
)) != 0 ||
146 delegation
->type
!= nfsi
->delegation
->type
) {
147 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
148 __FUNCTION__
, NIPQUAD(clp
->cl_addr
));
152 spin_unlock(&clp
->cl_lock
);
157 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
161 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
);
162 nfs_free_delegation(delegation
);
166 /* Sync all data to disk upon delegation return */
167 static void nfs_msync_inode(struct inode
*inode
)
169 filemap_fdatawrite(inode
->i_mapping
);
171 filemap_fdatawait(inode
->i_mapping
);
175 * Basic procedure for returning a delegation to the server
177 int __nfs_inode_return_delegation(struct inode
*inode
)
179 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
180 struct nfs_inode
*nfsi
= NFS_I(inode
);
181 struct nfs_delegation
*delegation
;
184 nfs_msync_inode(inode
);
185 down_read(&clp
->cl_sem
);
186 /* Guard against new delegated open calls */
187 down_write(&nfsi
->rwsem
);
188 spin_lock(&clp
->cl_lock
);
189 delegation
= nfsi
->delegation
;
190 if (delegation
!= NULL
) {
191 list_del_init(&delegation
->super_list
);
192 nfsi
->delegation
= NULL
;
193 nfsi
->delegation_state
= 0;
195 spin_unlock(&clp
->cl_lock
);
196 nfs_delegation_claim_opens(inode
);
197 up_write(&nfsi
->rwsem
);
198 up_read(&clp
->cl_sem
);
199 nfs_msync_inode(inode
);
201 if (delegation
!= NULL
)
202 res
= nfs_do_return_delegation(inode
, delegation
);
207 * Return all delegations associated to a super block
209 void nfs_return_all_delegations(struct super_block
*sb
)
211 struct nfs4_client
*clp
= NFS_SB(sb
)->nfs4_state
;
212 struct nfs_delegation
*delegation
;
218 spin_lock(&clp
->cl_lock
);
219 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
220 if (delegation
->inode
->i_sb
!= sb
)
222 inode
= igrab(delegation
->inode
);
225 spin_unlock(&clp
->cl_lock
);
226 nfs_inode_return_delegation(inode
);
230 spin_unlock(&clp
->cl_lock
);
233 int nfs_do_expire_all_delegations(void *ptr
)
235 struct nfs4_client
*clp
= ptr
;
236 struct nfs_delegation
*delegation
;
239 allow_signal(SIGKILL
);
241 spin_lock(&clp
->cl_lock
);
242 if (test_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
) != 0)
244 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0)
246 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
247 inode
= igrab(delegation
->inode
);
250 spin_unlock(&clp
->cl_lock
);
251 nfs_inode_return_delegation(inode
);
256 spin_unlock(&clp
->cl_lock
);
257 nfs4_put_client(clp
);
258 module_put_and_exit(0);
261 void nfs_expire_all_delegations(struct nfs4_client
*clp
)
263 struct task_struct
*task
;
265 __module_get(THIS_MODULE
);
266 atomic_inc(&clp
->cl_count
);
267 task
= kthread_run(nfs_do_expire_all_delegations
, clp
,
268 "%u.%u.%u.%u-delegreturn",
269 NIPQUAD(clp
->cl_addr
));
272 nfs4_put_client(clp
);
273 module_put(THIS_MODULE
);
277 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
279 void nfs_handle_cb_pathdown(struct nfs4_client
*clp
)
281 struct nfs_delegation
*delegation
;
287 spin_lock(&clp
->cl_lock
);
288 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
289 inode
= igrab(delegation
->inode
);
292 spin_unlock(&clp
->cl_lock
);
293 nfs_inode_return_delegation(inode
);
297 spin_unlock(&clp
->cl_lock
);
300 struct recall_threadargs
{
302 struct nfs4_client
*clp
;
303 const nfs4_stateid
*stateid
;
305 struct completion started
;
309 static int recall_thread(void *data
)
311 struct recall_threadargs
*args
= (struct recall_threadargs
*)data
;
312 struct inode
*inode
= igrab(args
->inode
);
313 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
314 struct nfs_inode
*nfsi
= NFS_I(inode
);
315 struct nfs_delegation
*delegation
;
317 daemonize("nfsv4-delegreturn");
319 nfs_msync_inode(inode
);
320 down_read(&clp
->cl_sem
);
321 down_write(&nfsi
->rwsem
);
322 spin_lock(&clp
->cl_lock
);
323 delegation
= nfsi
->delegation
;
324 if (delegation
!= NULL
&& memcmp(delegation
->stateid
.data
,
326 sizeof(delegation
->stateid
.data
)) == 0) {
327 list_del_init(&delegation
->super_list
);
328 nfsi
->delegation
= NULL
;
329 nfsi
->delegation_state
= 0;
333 args
->result
= -ENOENT
;
335 spin_unlock(&clp
->cl_lock
);
336 complete(&args
->started
);
337 nfs_delegation_claim_opens(inode
);
338 up_write(&nfsi
->rwsem
);
339 up_read(&clp
->cl_sem
);
340 nfs_msync_inode(inode
);
342 if (delegation
!= NULL
)
343 nfs_do_return_delegation(inode
, delegation
);
345 module_put_and_exit(0);
349 * Asynchronous delegation recall!
351 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
353 struct recall_threadargs data
= {
359 init_completion(&data
.started
);
360 __module_get(THIS_MODULE
);
361 status
= kernel_thread(recall_thread
, &data
, CLONE_KERNEL
);
364 wait_for_completion(&data
.started
);
367 module_put(THIS_MODULE
);
372 * Retrieve the inode associated with a delegation
374 struct inode
*nfs_delegation_find_inode(struct nfs4_client
*clp
, const struct nfs_fh
*fhandle
)
376 struct nfs_delegation
*delegation
;
377 struct inode
*res
= NULL
;
378 spin_lock(&clp
->cl_lock
);
379 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
380 if (nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
381 res
= igrab(delegation
->inode
);
385 spin_unlock(&clp
->cl_lock
);
390 * Mark all delegations as needing to be reclaimed
392 void nfs_delegation_mark_reclaim(struct nfs4_client
*clp
)
394 struct nfs_delegation
*delegation
;
395 spin_lock(&clp
->cl_lock
);
396 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
)
397 delegation
->flags
|= NFS_DELEGATION_NEED_RECLAIM
;
398 spin_unlock(&clp
->cl_lock
);
402 * Reap all unclaimed delegations after reboot recovery is done
404 void nfs_delegation_reap_unclaimed(struct nfs4_client
*clp
)
406 struct nfs_delegation
*delegation
, *n
;
408 spin_lock(&clp
->cl_lock
);
409 list_for_each_entry_safe(delegation
, n
, &clp
->cl_delegations
, super_list
) {
410 if ((delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
) == 0)
412 list_move(&delegation
->super_list
, &head
);
413 NFS_I(delegation
->inode
)->delegation
= NULL
;
414 NFS_I(delegation
->inode
)->delegation_state
= 0;
416 spin_unlock(&clp
->cl_lock
);
417 while(!list_empty(&head
)) {
418 delegation
= list_entry(head
.next
, struct nfs_delegation
, super_list
);
419 list_del(&delegation
->super_list
);
420 nfs_free_delegation(delegation
);
424 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
426 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
427 struct nfs_inode
*nfsi
= NFS_I(inode
);
428 struct nfs_delegation
*delegation
;
431 if (nfsi
->delegation_state
== 0)
433 spin_lock(&clp
->cl_lock
);
434 delegation
= nfsi
->delegation
;
435 if (delegation
!= NULL
) {
436 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));
439 spin_unlock(&clp
->cl_lock
);