2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/config.h>
10 #include <linux/completion.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
->cred
= get_rpccred(cred
);
134 delegation
->inode
= inode
;
136 spin_lock(&clp
->cl_lock
);
137 if (nfsi
->delegation
== NULL
) {
138 list_add(&delegation
->super_list
, &clp
->cl_delegations
);
139 nfsi
->delegation
= delegation
;
140 nfsi
->delegation_state
= delegation
->type
;
143 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
144 sizeof(delegation
->stateid
)) != 0 ||
145 delegation
->type
!= nfsi
->delegation
->type
) {
146 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
147 __FUNCTION__
, NIPQUAD(clp
->cl_addr
));
151 spin_unlock(&clp
->cl_lock
);
156 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
160 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
162 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
);
163 nfs_free_delegation(delegation
);
167 /* Sync all data to disk upon delegation return */
168 static void nfs_msync_inode(struct inode
*inode
)
170 filemap_fdatawrite(inode
->i_mapping
);
172 filemap_fdatawait(inode
->i_mapping
);
176 * Basic procedure for returning a delegation to the server
178 int __nfs_inode_return_delegation(struct inode
*inode
)
180 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
181 struct nfs_inode
*nfsi
= NFS_I(inode
);
182 struct nfs_delegation
*delegation
;
185 nfs_msync_inode(inode
);
186 down_read(&clp
->cl_sem
);
187 /* Guard against new delegated open calls */
188 down_write(&nfsi
->rwsem
);
189 spin_lock(&clp
->cl_lock
);
190 delegation
= nfsi
->delegation
;
191 if (delegation
!= NULL
) {
192 list_del_init(&delegation
->super_list
);
193 nfsi
->delegation
= NULL
;
194 nfsi
->delegation_state
= 0;
196 spin_unlock(&clp
->cl_lock
);
197 nfs_delegation_claim_opens(inode
);
198 up_write(&nfsi
->rwsem
);
199 up_read(&clp
->cl_sem
);
200 nfs_msync_inode(inode
);
202 if (delegation
!= NULL
)
203 res
= nfs_do_return_delegation(inode
, delegation
);
208 * Return all delegations associated to a super block
210 void nfs_return_all_delegations(struct super_block
*sb
)
212 struct nfs4_client
*clp
= NFS_SB(sb
)->nfs4_state
;
213 struct nfs_delegation
*delegation
;
219 spin_lock(&clp
->cl_lock
);
220 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
221 if (delegation
->inode
->i_sb
!= sb
)
223 inode
= igrab(delegation
->inode
);
226 spin_unlock(&clp
->cl_lock
);
227 nfs_inode_return_delegation(inode
);
231 spin_unlock(&clp
->cl_lock
);
235 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
237 void nfs_handle_cb_pathdown(struct nfs4_client
*clp
)
239 struct nfs_delegation
*delegation
;
245 spin_lock(&clp
->cl_lock
);
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
);
255 spin_unlock(&clp
->cl_lock
);
258 struct recall_threadargs
{
260 struct nfs4_client
*clp
;
261 const nfs4_stateid
*stateid
;
263 struct completion started
;
267 static int recall_thread(void *data
)
269 struct recall_threadargs
*args
= (struct recall_threadargs
*)data
;
270 struct inode
*inode
= igrab(args
->inode
);
271 struct nfs4_client
*clp
= NFS_SERVER(inode
)->nfs4_state
;
272 struct nfs_inode
*nfsi
= NFS_I(inode
);
273 struct nfs_delegation
*delegation
;
275 daemonize("nfsv4-delegreturn");
277 nfs_msync_inode(inode
);
278 down_read(&clp
->cl_sem
);
279 down_write(&nfsi
->rwsem
);
280 spin_lock(&clp
->cl_lock
);
281 delegation
= nfsi
->delegation
;
282 if (delegation
!= NULL
&& memcmp(delegation
->stateid
.data
,
284 sizeof(delegation
->stateid
.data
)) == 0) {
285 list_del_init(&delegation
->super_list
);
286 nfsi
->delegation
= NULL
;
287 nfsi
->delegation_state
= 0;
291 args
->result
= -ENOENT
;
293 spin_unlock(&clp
->cl_lock
);
294 complete(&args
->started
);
295 nfs_delegation_claim_opens(inode
);
296 up_write(&nfsi
->rwsem
);
297 up_read(&clp
->cl_sem
);
298 nfs_msync_inode(inode
);
300 if (delegation
!= NULL
)
301 nfs_do_return_delegation(inode
, delegation
);
303 module_put_and_exit(0);
307 * Asynchronous delegation recall!
309 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
311 struct recall_threadargs data
= {
317 init_completion(&data
.started
);
318 __module_get(THIS_MODULE
);
319 status
= kernel_thread(recall_thread
, &data
, CLONE_KERNEL
);
322 wait_for_completion(&data
.started
);
325 module_put(THIS_MODULE
);
330 * Retrieve the inode associated with a delegation
332 struct inode
*nfs_delegation_find_inode(struct nfs4_client
*clp
, const struct nfs_fh
*fhandle
)
334 struct nfs_delegation
*delegation
;
335 struct inode
*res
= NULL
;
336 spin_lock(&clp
->cl_lock
);
337 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
) {
338 if (nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
339 res
= igrab(delegation
->inode
);
343 spin_unlock(&clp
->cl_lock
);
348 * Mark all delegations as needing to be reclaimed
350 void nfs_delegation_mark_reclaim(struct nfs4_client
*clp
)
352 struct nfs_delegation
*delegation
;
353 spin_lock(&clp
->cl_lock
);
354 list_for_each_entry(delegation
, &clp
->cl_delegations
, super_list
)
355 delegation
->flags
|= NFS_DELEGATION_NEED_RECLAIM
;
356 spin_unlock(&clp
->cl_lock
);
360 * Reap all unclaimed delegations after reboot recovery is done
362 void nfs_delegation_reap_unclaimed(struct nfs4_client
*clp
)
364 struct nfs_delegation
*delegation
, *n
;
366 spin_lock(&clp
->cl_lock
);
367 list_for_each_entry_safe(delegation
, n
, &clp
->cl_delegations
, super_list
) {
368 if ((delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
) == 0)
370 list_move(&delegation
->super_list
, &head
);
371 NFS_I(delegation
->inode
)->delegation
= NULL
;
372 NFS_I(delegation
->inode
)->delegation_state
= 0;
374 spin_unlock(&clp
->cl_lock
);
375 while(!list_empty(&head
)) {
376 delegation
= list_entry(head
.next
, struct nfs_delegation
, super_list
);
377 list_del(&delegation
->super_list
);
378 nfs_free_delegation(delegation
);