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/smp_lock.h>
14 #include <linux/spinlock.h>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
21 #include "delegation.h"
24 static void nfs_do_free_delegation(struct nfs_delegation
*delegation
)
29 static void nfs_free_delegation_callback(struct rcu_head
*head
)
31 struct nfs_delegation
*delegation
= container_of(head
, struct nfs_delegation
, rcu
);
33 nfs_do_free_delegation(delegation
);
36 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
38 struct rpc_cred
*cred
;
40 cred
= rcu_dereference(delegation
->cred
);
41 rcu_assign_pointer(delegation
->cred
, NULL
);
42 call_rcu(&delegation
->rcu
, nfs_free_delegation_callback
);
47 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
49 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
52 int nfs_have_delegation(struct inode
*inode
, fmode_t flags
)
54 struct nfs_delegation
*delegation
;
57 flags
&= FMODE_READ
|FMODE_WRITE
;
59 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
60 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
) {
61 nfs_mark_delegation_referenced(delegation
);
68 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
70 struct inode
*inode
= state
->inode
;
74 if (inode
->i_flock
== NULL
)
77 /* Protect inode->i_flock using the BKL */
79 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
80 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
82 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
85 status
= nfs4_lock_delegation_recall(state
, fl
);
95 static void nfs_delegation_claim_opens(struct inode
*inode
, const nfs4_stateid
*stateid
)
97 struct nfs_inode
*nfsi
= NFS_I(inode
);
98 struct nfs_open_context
*ctx
;
99 struct nfs4_state
*state
;
103 spin_lock(&inode
->i_lock
);
104 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
108 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
110 if (memcmp(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
)) != 0)
112 get_nfs_open_context(ctx
);
113 spin_unlock(&inode
->i_lock
);
114 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
116 err
= nfs_delegation_claim_locks(ctx
, state
);
117 put_nfs_open_context(ctx
);
122 spin_unlock(&inode
->i_lock
);
126 * Set up a delegation on an inode
128 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
130 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
131 struct rpc_cred
*oldcred
;
133 if (delegation
== NULL
)
135 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
136 sizeof(delegation
->stateid
.data
));
137 delegation
->type
= res
->delegation_type
;
138 delegation
->maxsize
= res
->maxsize
;
139 oldcred
= delegation
->cred
;
140 delegation
->cred
= get_rpccred(cred
);
141 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
142 NFS_I(inode
)->delegation_state
= delegation
->type
;
144 put_rpccred(oldcred
);
147 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
151 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
, issync
);
152 nfs_free_delegation(delegation
);
156 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
158 struct inode
*inode
= NULL
;
160 spin_lock(&delegation
->lock
);
161 if (delegation
->inode
!= NULL
)
162 inode
= igrab(delegation
->inode
);
163 spin_unlock(&delegation
->lock
);
167 static struct nfs_delegation
*nfs_detach_delegation_locked(struct nfs_inode
*nfsi
, const nfs4_stateid
*stateid
)
169 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
171 if (delegation
== NULL
)
173 spin_lock(&delegation
->lock
);
174 if (stateid
!= NULL
&& memcmp(delegation
->stateid
.data
, stateid
->data
,
175 sizeof(delegation
->stateid
.data
)) != 0)
177 list_del_rcu(&delegation
->super_list
);
178 delegation
->inode
= NULL
;
179 nfsi
->delegation_state
= 0;
180 rcu_assign_pointer(nfsi
->delegation
, NULL
);
181 spin_unlock(&delegation
->lock
);
184 spin_unlock(&delegation
->lock
);
190 * Set up a delegation on an inode
192 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
194 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
195 struct nfs_inode
*nfsi
= NFS_I(inode
);
196 struct nfs_delegation
*delegation
;
197 struct nfs_delegation
*freeme
= NULL
;
200 delegation
= kmalloc(sizeof(*delegation
), GFP_KERNEL
);
201 if (delegation
== NULL
)
203 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
204 sizeof(delegation
->stateid
.data
));
205 delegation
->type
= res
->delegation_type
;
206 delegation
->maxsize
= res
->maxsize
;
207 delegation
->change_attr
= nfsi
->change_attr
;
208 delegation
->cred
= get_rpccred(cred
);
209 delegation
->inode
= inode
;
210 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
211 spin_lock_init(&delegation
->lock
);
213 spin_lock(&clp
->cl_lock
);
214 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
215 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
216 sizeof(delegation
->stateid
)) == 0 &&
217 delegation
->type
== nfsi
->delegation
->type
) {
221 * Deal with broken servers that hand out two
222 * delegations for the same file.
224 dfprintk(FILE, "%s: server %s handed out "
225 "a duplicate delegation!\n",
226 __func__
, clp
->cl_hostname
);
227 if (delegation
->type
<= nfsi
->delegation
->type
) {
232 freeme
= nfs_detach_delegation_locked(nfsi
, NULL
);
234 list_add_rcu(&delegation
->super_list
, &clp
->cl_delegations
);
235 nfsi
->delegation_state
= delegation
->type
;
236 rcu_assign_pointer(nfsi
->delegation
, delegation
);
239 /* Ensure we revalidate the attributes and page cache! */
240 spin_lock(&inode
->i_lock
);
241 nfsi
->cache_validity
|= NFS_INO_REVAL_FORCED
;
242 spin_unlock(&inode
->i_lock
);
245 spin_unlock(&clp
->cl_lock
);
246 if (delegation
!= NULL
)
247 nfs_free_delegation(delegation
);
249 nfs_do_return_delegation(inode
, freeme
, 0);
253 /* Sync all data to disk upon delegation return */
254 static void nfs_msync_inode(struct inode
*inode
)
256 filemap_fdatawrite(inode
->i_mapping
);
258 filemap_fdatawait(inode
->i_mapping
);
262 * Basic procedure for returning a delegation to the server
264 static int __nfs_inode_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
266 struct nfs_inode
*nfsi
= NFS_I(inode
);
268 nfs_msync_inode(inode
);
270 * Guard against new delegated open/lock/unlock calls and against
273 down_write(&nfsi
->rwsem
);
274 nfs_delegation_claim_opens(inode
, &delegation
->stateid
);
275 up_write(&nfsi
->rwsem
);
276 nfs_msync_inode(inode
);
278 return nfs_do_return_delegation(inode
, delegation
, 1);
282 * Return all delegations that have been marked for return
284 void nfs_client_return_marked_delegations(struct nfs_client
*clp
)
286 struct nfs_delegation
*delegation
;
291 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
292 if (!test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
294 inode
= nfs_delegation_grab_inode(delegation
);
297 spin_lock(&clp
->cl_lock
);
298 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
299 spin_unlock(&clp
->cl_lock
);
301 if (delegation
!= NULL
)
302 __nfs_inode_return_delegation(inode
, delegation
);
310 * This function returns the delegation without reclaiming opens
311 * or protecting against delegation reclaims.
312 * It is therefore really only safe to be called from
315 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
317 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
318 struct nfs_inode
*nfsi
= NFS_I(inode
);
319 struct nfs_delegation
*delegation
;
321 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
322 spin_lock(&clp
->cl_lock
);
323 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
324 spin_unlock(&clp
->cl_lock
);
325 if (delegation
!= NULL
)
326 nfs_do_return_delegation(inode
, delegation
, 0);
330 int nfs_inode_return_delegation(struct inode
*inode
)
332 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
333 struct nfs_inode
*nfsi
= NFS_I(inode
);
334 struct nfs_delegation
*delegation
;
337 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
338 spin_lock(&clp
->cl_lock
);
339 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
340 spin_unlock(&clp
->cl_lock
);
341 if (delegation
!= NULL
)
342 err
= __nfs_inode_return_delegation(inode
, delegation
);
347 static void nfs_mark_return_delegation(struct nfs_client
*clp
, struct nfs_delegation
*delegation
)
349 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
350 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
354 * Return all delegations associated to a super block
356 void nfs_super_return_all_delegations(struct super_block
*sb
)
358 struct nfs_client
*clp
= NFS_SB(sb
)->nfs_client
;
359 struct nfs_delegation
*delegation
;
364 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
365 spin_lock(&delegation
->lock
);
366 if (delegation
->inode
!= NULL
&& delegation
->inode
->i_sb
== sb
)
367 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
368 spin_unlock(&delegation
->lock
);
371 nfs_client_return_marked_delegations(clp
);
374 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
376 struct nfs_delegation
*delegation
;
379 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
380 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
381 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
386 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
388 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
389 nfs4_schedule_state_manager(clp
);
392 void nfs_expire_all_delegations(struct nfs_client
*clp
)
394 nfs_client_mark_return_all_delegations(clp
);
395 nfs_delegation_run_state_manager(clp
);
399 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
401 void nfs_handle_cb_pathdown(struct nfs_client
*clp
)
405 nfs_client_mark_return_all_delegations(clp
);
408 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client
*clp
)
410 struct nfs_delegation
*delegation
;
413 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
414 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
416 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
417 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
422 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
424 nfs_client_mark_return_unreferenced_delegations(clp
);
425 nfs_delegation_run_state_manager(clp
);
429 * Asynchronous delegation recall!
431 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
433 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
434 struct nfs_delegation
*delegation
;
437 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
438 if (delegation
== NULL
|| memcmp(delegation
->stateid
.data
, stateid
->data
,
439 sizeof(delegation
->stateid
.data
)) != 0) {
443 nfs_mark_return_delegation(clp
, delegation
);
445 nfs_delegation_run_state_manager(clp
);
450 * Retrieve the inode associated with a delegation
452 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
, const struct nfs_fh
*fhandle
)
454 struct nfs_delegation
*delegation
;
455 struct inode
*res
= NULL
;
457 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
458 spin_lock(&delegation
->lock
);
459 if (delegation
->inode
!= NULL
&&
460 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
461 res
= igrab(delegation
->inode
);
463 spin_unlock(&delegation
->lock
);
472 * Mark all delegations as needing to be reclaimed
474 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
476 struct nfs_delegation
*delegation
;
478 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
)
479 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
484 * Reap all unclaimed delegations after reboot recovery is done
486 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
488 struct nfs_delegation
*delegation
;
492 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
493 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0)
495 inode
= nfs_delegation_grab_inode(delegation
);
498 spin_lock(&clp
->cl_lock
);
499 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
500 spin_unlock(&clp
->cl_lock
);
502 if (delegation
!= NULL
)
503 nfs_free_delegation(delegation
);
510 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
512 struct nfs_inode
*nfsi
= NFS_I(inode
);
513 struct nfs_delegation
*delegation
;
517 delegation
= rcu_dereference(nfsi
->delegation
);
518 if (delegation
!= NULL
) {
519 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));