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"
23 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
26 put_rpccred(delegation
->cred
);
30 static void nfs_free_delegation_callback(struct rcu_head
*head
)
32 struct nfs_delegation
*delegation
= container_of(head
, struct nfs_delegation
, rcu
);
34 nfs_free_delegation(delegation
);
37 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
39 struct inode
*inode
= state
->inode
;
43 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
44 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
46 if ((struct nfs_open_context
*)fl
->fl_file
->private_data
!= ctx
)
48 status
= nfs4_lock_delegation_recall(state
, fl
);
53 printk(KERN_ERR
"%s: unhandled error %d.\n",
54 __FUNCTION__
, status
);
55 case -NFS4ERR_EXPIRED
:
56 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
57 case -NFS4ERR_STALE_CLIENTID
:
58 nfs4_schedule_state_recovery(NFS_SERVER(inode
)->nfs_client
);
67 static void nfs_delegation_claim_opens(struct inode
*inode
, const nfs4_stateid
*stateid
)
69 struct nfs_inode
*nfsi
= NFS_I(inode
);
70 struct nfs_open_context
*ctx
;
71 struct nfs4_state
*state
;
75 spin_lock(&inode
->i_lock
);
76 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
80 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
82 if (memcmp(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
)) != 0)
84 get_nfs_open_context(ctx
);
85 spin_unlock(&inode
->i_lock
);
86 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
88 err
= nfs_delegation_claim_locks(ctx
, state
);
89 put_nfs_open_context(ctx
);
94 spin_unlock(&inode
->i_lock
);
98 * Set up a delegation on an inode
100 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
102 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
104 if (delegation
== NULL
)
106 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
107 sizeof(delegation
->stateid
.data
));
108 delegation
->type
= res
->delegation_type
;
109 delegation
->maxsize
= res
->maxsize
;
111 delegation
->cred
= get_rpccred(cred
);
112 delegation
->flags
&= ~NFS_DELEGATION_NEED_RECLAIM
;
113 NFS_I(inode
)->delegation_state
= delegation
->type
;
118 * Set up a delegation on an inode
120 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
122 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
123 struct nfs_inode
*nfsi
= NFS_I(inode
);
124 struct nfs_delegation
*delegation
;
127 /* Ensure we first revalidate the attributes and page cache! */
128 if ((nfsi
->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_ATTR
)))
129 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
131 delegation
= kmalloc(sizeof(*delegation
), GFP_KERNEL
);
132 if (delegation
== NULL
)
134 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
135 sizeof(delegation
->stateid
.data
));
136 delegation
->type
= res
->delegation_type
;
137 delegation
->maxsize
= res
->maxsize
;
138 delegation
->change_attr
= nfsi
->change_attr
;
139 delegation
->cred
= get_rpccred(cred
);
140 delegation
->inode
= inode
;
142 spin_lock(&clp
->cl_lock
);
143 if (rcu_dereference(nfsi
->delegation
) == NULL
) {
144 list_add_rcu(&delegation
->super_list
, &clp
->cl_delegations
);
145 nfsi
->delegation_state
= delegation
->type
;
146 rcu_assign_pointer(nfsi
->delegation
, delegation
);
149 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
150 sizeof(delegation
->stateid
)) != 0 ||
151 delegation
->type
!= nfsi
->delegation
->type
) {
152 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
153 __FUNCTION__
, NIPQUAD(clp
->cl_addr
.sin_addr
));
157 spin_unlock(&clp
->cl_lock
);
162 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
166 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
);
167 call_rcu(&delegation
->rcu
, nfs_free_delegation_callback
);
171 /* Sync all data to disk upon delegation return */
172 static void nfs_msync_inode(struct inode
*inode
)
174 filemap_fdatawrite(inode
->i_mapping
);
176 filemap_fdatawait(inode
->i_mapping
);
180 * Basic procedure for returning a delegation to the server
182 static int __nfs_inode_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
184 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
185 struct nfs_inode
*nfsi
= NFS_I(inode
);
187 nfs_msync_inode(inode
);
188 down_read(&clp
->cl_sem
);
189 /* Guard against new delegated open calls */
190 down_write(&nfsi
->rwsem
);
191 nfs_delegation_claim_opens(inode
, &delegation
->stateid
);
192 up_write(&nfsi
->rwsem
);
193 up_read(&clp
->cl_sem
);
194 nfs_msync_inode(inode
);
196 return nfs_do_return_delegation(inode
, delegation
);
199 static struct nfs_delegation
*nfs_detach_delegation_locked(struct nfs_inode
*nfsi
, const nfs4_stateid
*stateid
)
201 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
203 if (delegation
== NULL
)
205 if (stateid
!= NULL
&& memcmp(delegation
->stateid
.data
, stateid
->data
,
206 sizeof(delegation
->stateid
.data
)) != 0)
208 list_del_rcu(&delegation
->super_list
);
209 nfsi
->delegation_state
= 0;
210 rcu_assign_pointer(nfsi
->delegation
, NULL
);
216 int nfs_inode_return_delegation(struct inode
*inode
)
218 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
219 struct nfs_inode
*nfsi
= NFS_I(inode
);
220 struct nfs_delegation
*delegation
;
223 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
224 spin_lock(&clp
->cl_lock
);
225 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
226 spin_unlock(&clp
->cl_lock
);
227 if (delegation
!= NULL
)
228 err
= __nfs_inode_return_delegation(inode
, delegation
);
234 * Return all delegations associated to a super block
236 void nfs_return_all_delegations(struct super_block
*sb
)
238 struct nfs_client
*clp
= NFS_SB(sb
)->nfs_client
;
239 struct nfs_delegation
*delegation
;
246 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
247 if (delegation
->inode
->i_sb
!= sb
)
249 inode
= igrab(delegation
->inode
);
252 spin_lock(&clp
->cl_lock
);
253 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
254 spin_unlock(&clp
->cl_lock
);
256 if (delegation
!= NULL
)
257 __nfs_inode_return_delegation(inode
, delegation
);
264 static int nfs_do_expire_all_delegations(void *ptr
)
266 struct nfs_client
*clp
= ptr
;
267 struct nfs_delegation
*delegation
;
270 allow_signal(SIGKILL
);
272 if (test_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
) != 0)
274 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0)
277 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
278 inode
= igrab(delegation
->inode
);
281 spin_lock(&clp
->cl_lock
);
282 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
283 spin_unlock(&clp
->cl_lock
);
286 __nfs_inode_return_delegation(inode
, delegation
);
293 module_put_and_exit(0);
296 void nfs_expire_all_delegations(struct nfs_client
*clp
)
298 struct task_struct
*task
;
300 __module_get(THIS_MODULE
);
301 atomic_inc(&clp
->cl_count
);
302 task
= kthread_run(nfs_do_expire_all_delegations
, clp
,
303 "%u.%u.%u.%u-delegreturn",
304 NIPQUAD(clp
->cl_addr
.sin_addr
));
308 module_put(THIS_MODULE
);
312 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
314 void nfs_handle_cb_pathdown(struct nfs_client
*clp
)
316 struct nfs_delegation
*delegation
;
323 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
324 inode
= igrab(delegation
->inode
);
327 spin_lock(&clp
->cl_lock
);
328 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
329 spin_unlock(&clp
->cl_lock
);
331 if (delegation
!= NULL
)
332 __nfs_inode_return_delegation(inode
, delegation
);
339 struct recall_threadargs
{
341 struct nfs_client
*clp
;
342 const nfs4_stateid
*stateid
;
344 struct completion started
;
348 static int recall_thread(void *data
)
350 struct recall_threadargs
*args
= (struct recall_threadargs
*)data
;
351 struct inode
*inode
= igrab(args
->inode
);
352 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
353 struct nfs_inode
*nfsi
= NFS_I(inode
);
354 struct nfs_delegation
*delegation
;
356 daemonize("nfsv4-delegreturn");
358 nfs_msync_inode(inode
);
359 down_read(&clp
->cl_sem
);
360 down_write(&nfsi
->rwsem
);
361 spin_lock(&clp
->cl_lock
);
362 delegation
= nfs_detach_delegation_locked(nfsi
, args
->stateid
);
363 if (delegation
!= NULL
)
366 args
->result
= -ENOENT
;
367 spin_unlock(&clp
->cl_lock
);
368 complete(&args
->started
);
369 nfs_delegation_claim_opens(inode
, args
->stateid
);
370 up_write(&nfsi
->rwsem
);
371 up_read(&clp
->cl_sem
);
372 nfs_msync_inode(inode
);
374 if (delegation
!= NULL
)
375 nfs_do_return_delegation(inode
, delegation
);
377 module_put_and_exit(0);
381 * Asynchronous delegation recall!
383 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
385 struct recall_threadargs data
= {
391 init_completion(&data
.started
);
392 __module_get(THIS_MODULE
);
393 status
= kernel_thread(recall_thread
, &data
, CLONE_KERNEL
);
396 wait_for_completion(&data
.started
);
399 module_put(THIS_MODULE
);
404 * Retrieve the inode associated with a delegation
406 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
, const struct nfs_fh
*fhandle
)
408 struct nfs_delegation
*delegation
;
409 struct inode
*res
= NULL
;
411 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
412 if (nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
413 res
= igrab(delegation
->inode
);
422 * Mark all delegations as needing to be reclaimed
424 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
426 struct nfs_delegation
*delegation
;
428 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
)
429 delegation
->flags
|= NFS_DELEGATION_NEED_RECLAIM
;
434 * Reap all unclaimed delegations after reboot recovery is done
436 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
438 struct nfs_delegation
*delegation
;
441 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
442 if ((delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
) == 0)
444 spin_lock(&clp
->cl_lock
);
445 delegation
= nfs_detach_delegation_locked(NFS_I(delegation
->inode
), NULL
);
446 spin_unlock(&clp
->cl_lock
);
448 if (delegation
!= NULL
)
449 call_rcu(&delegation
->rcu
, nfs_free_delegation_callback
);
455 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
457 struct nfs_inode
*nfsi
= NFS_I(inode
);
458 struct nfs_delegation
*delegation
;
462 delegation
= rcu_dereference(nfsi
->delegation
);
463 if (delegation
!= NULL
) {
464 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));