Lines Matching defs:delegation
3 * linux/fs/nfs/delegation.c
7 * NFS file delegation management
24 #include "delegation.h"
33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
35 put_cred(delegation->cred);
36 delegation->cred = NULL;
37 kfree_rcu(delegation, rcu);
40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
45 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46 nfs_clear_verifier_delegated(delegation->inode);
50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
52 refcount_inc(&delegation->refcount);
53 return delegation;
56 static void nfs_put_delegation(struct nfs_delegation *delegation)
58 if (refcount_dec_and_test(&delegation->refcount))
59 __nfs_free_delegation(delegation);
62 static void nfs_free_delegation(struct nfs_delegation *delegation)
64 nfs_mark_delegation_revoked(delegation);
65 nfs_put_delegation(delegation);
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
79 struct nfs_delegation *delegation)
81 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
86 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
89 if (delegation != NULL && (delegation->type & flags) == flags &&
90 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
91 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
98 struct nfs_delegation *delegation;
100 delegation = rcu_dereference(NFS_I(inode)->delegation);
101 if (nfs4_is_valid_delegation(delegation, 0))
102 return delegation;
109 struct nfs_delegation *delegation;
114 delegation = rcu_dereference(NFS_I(inode)->delegation);
115 if (nfs4_is_valid_delegation(delegation, flags)) {
117 nfs_mark_delegation_referenced(delegation);
124 * nfs_have_delegation - check if inode has a delegation, mark it
127 * @flags: delegation types to check for
129 * Returns one if inode has the indicated delegation, otherwise zero.
137 * nfs4_check_delegation - check if inode has a delegation, do not mark
222 * nfs_inode_reclaim_delegation - process a delegation reclaim request
225 * @type: delegation type
226 * @stateid: delegation stateid
227 * @pagemod_limit: write delegation "space_limit"
234 struct nfs_delegation *delegation;
238 delegation = rcu_dereference(NFS_I(inode)->delegation);
239 if (delegation != NULL) {
240 spin_lock(&delegation->lock);
241 nfs4_stateid_copy(&delegation->stateid, stateid);
242 delegation->type = type;
243 delegation->pagemod_limit = pagemod_limit;
244 oldcred = delegation->cred;
245 delegation->cred = get_cred(cred);
246 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
248 &delegation->flags))
250 spin_unlock(&delegation->lock);
261 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
266 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
267 spin_lock(&delegation->lock);
268 cred = get_cred(delegation->cred);
269 spin_unlock(&delegation->lock);
271 &delegation->stateid,
278 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
282 spin_lock(&delegation->lock);
283 if (delegation->inode != NULL)
284 inode = igrab(delegation->inode);
286 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
287 spin_unlock(&delegation->lock);
295 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
297 if (delegation == NULL)
299 spin_lock(&delegation->lock);
300 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
301 clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
303 ret = nfs_get_delegation(delegation);
305 spin_unlock(&delegation->lock);
315 struct nfs_delegation *delegation;
318 delegation = nfs_start_delegation_return_locked(nfsi);
320 return delegation;
323 static void nfs_abort_delegation_return(struct nfs_delegation *delegation,
327 spin_lock(&delegation->lock);
328 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
330 set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
333 spin_unlock(&delegation->lock);
338 struct nfs_delegation *delegation,
342 rcu_dereference_protected(nfsi->delegation,
345 if (deleg_cur == NULL || delegation != deleg_cur)
348 spin_lock(&delegation->lock);
349 if (!delegation->inode) {
350 spin_unlock(&delegation->lock);
353 list_del_rcu(&delegation->super_list);
354 delegation->inode = NULL;
355 rcu_assign_pointer(nfsi->delegation, NULL);
356 spin_unlock(&delegation->lock);
357 return delegation;
361 struct nfs_delegation *delegation,
367 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
369 return delegation;
377 struct nfs_delegation *delegation;
380 delegation = rcu_dereference(nfsi->delegation);
381 if (delegation != NULL)
382 delegation = nfs_detach_delegation(nfsi, delegation, server);
384 return delegation;
388 nfs_update_delegation_cred(struct nfs_delegation *delegation,
393 if (cred_fscmp(delegation->cred, cred) != 0) {
394 old = xchg(&delegation->cred, get_cred(cred));
400 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
403 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
404 delegation->stateid.seqid = update->stateid.seqid;
406 delegation->type = update->type;
407 delegation->pagemod_limit = update->pagemod_limit;
408 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
409 delegation->change_attr = update->change_attr;
410 nfs_update_delegation_cred(delegation, update->cred);
412 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
419 * nfs_inode_set_delegation - set up a delegation on an inode
420 * @inode: inode to which delegation applies
421 * @cred: cred to use for subsequent delegation processing
422 * @type: delegation type
423 * @stateid: delegation stateid
424 * @pagemod_limit: write delegation "space_limit"
436 struct nfs_delegation *delegation, *old_delegation;
440 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
441 if (delegation == NULL)
443 nfs4_stateid_copy(&delegation->stateid, stateid);
444 refcount_set(&delegation->refcount, 1);
445 delegation->type = type;
446 delegation->pagemod_limit = pagemod_limit;
447 delegation->change_attr = inode_peek_iversion_raw(inode);
448 delegation->cred = get_cred(cred);
449 delegation->inode = inode;
450 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
451 spin_lock_init(&delegation->lock);
454 old_delegation = rcu_dereference_protected(nfsi->delegation,
458 /* Is this an update of the existing delegation? */
460 &delegation->stateid)) {
463 delegation);
471 * Allow for upgrades to a WRITE delegation, but
475 "a duplicate delegation!\n",
477 if (delegation->type == old_delegation->type ||
478 !(delegation->type & FMODE_WRITE)) {
479 freeme = delegation;
480 delegation = NULL;
491 list_add_tail_rcu(&delegation->super_list, &server->delegations);
492 rcu_assign_pointer(nfsi->delegation, delegation);
493 delegation = NULL;
505 if (delegation != NULL)
506 __nfs_free_delegation(delegation);
515 * Basic procedure for returning a delegation to the server
517 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
522 if (delegation == NULL)
525 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
527 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
528 delegation->type);
538 nfs_abort_delegation_return(delegation, clp, err);
542 err = nfs_do_return_delegation(inode, delegation, issync);
545 nfs_put_delegation(delegation);
549 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
553 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
555 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
558 spin_lock(&delegation->lock);
559 inode = delegation->inode;
562 spin_unlock(&delegation->lock);
565 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
566 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
567 test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
568 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
577 struct nfs_delegation *delegation;
588 * list delegation in the server from the delegations
590 * prev is an RCU-protected pointer to a delegation which
595 delegation = NULL;
598 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
599 if (!delegation || delegation != place_holder_deleg)
600 delegation = list_entry_rcu(server->delegations.next,
602 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
605 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
607 if (!nfs_delegation_need_return(delegation)) {
608 if (nfs4_is_valid_delegation(delegation, 0))
609 prev = delegation;
622 inode = nfs_delegation_grab_inode(delegation);
628 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
633 err = nfs_end_delegation_return(inode, delegation, 0);
702 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
705 * Does not protect against delegation reclaims, therefore really only safe
707 * the delegation structure.
711 struct nfs_delegation *delegation;
713 delegation = nfs_inode_detach_delegation(inode);
714 if (delegation != NULL) {
715 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
716 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
717 nfs_do_return_delegation(inode, delegation, 1);
718 nfs_free_delegation(delegation);
723 * nfs_inode_return_delegation - synchronously return a delegation
727 * assumption that if we need to return the delegation, then
735 struct nfs_delegation *delegation;
739 delegation = nfs_start_delegation_return(nfsi);
740 if (delegation != NULL)
741 err = nfs_end_delegation_return(inode, delegation, 1);
746 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
750 * inode delegation needs to be returned immediately.
754 struct nfs_delegation *delegation;
760 delegation = nfs4_get_valid_delegation(inode);
761 if (!delegation)
763 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
765 spin_lock(&delegation->lock);
766 if (delegation->inode &&
768 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
769 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
771 ret = nfs_get_delegation(delegation);
773 spin_unlock(&delegation->lock);
786 * Make the inode writeable by returning the delegation if necessary
792 struct nfs_delegation *delegation;
795 delegation = nfs4_get_valid_delegation(inode);
796 if (delegation == NULL ||
798 (delegation->type & FMODE_WRITE))) {
807 struct nfs_delegation *delegation)
809 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
815 struct nfs_delegation *delegation;
818 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
819 nfs_mark_return_delegation(server, delegation);
878 struct nfs_delegation *delegation;
880 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
881 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
883 if (delegation->type & flags)
884 nfs_mark_return_if_closed_delegation(server, delegation);
902 struct nfs_delegation *delegation;
907 delegation = rcu_dereference(NFS_I(inode)->delegation);
908 if (delegation == NULL)
911 nfs4_stateid_copy(&tmp, &delegation->stateid);
914 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
916 spin_lock(&delegation->lock);
918 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
919 spin_unlock(&delegation->lock);
922 delegation->stateid.seqid = stateid->seqid;
924 spin_unlock(&delegation->lock);
926 nfs_mark_delegation_revoked(delegation);
944 struct nfs_delegation *delegation;
950 delegation = rcu_dereference(NFS_I(inode)->delegation);
951 if (!delegation)
954 spin_lock(&delegation->lock);
955 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
958 /* If delegation->stateid is newer, dont mark as returned */
959 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
961 if (delegation->stateid.seqid != stateid->seqid)
962 delegation->stateid.seqid = stateid->seqid;
965 nfs_mark_delegation_revoked(delegation);
968 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
970 spin_unlock(&delegation->lock);
980 * @flags: delegation types to expire
991 struct nfs_delegation *delegation;
993 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
994 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
996 nfs_mark_return_if_closed_delegation(server, delegation);
1018 * nfs_async_inode_return_delegation - asynchronously return a delegation
1029 struct nfs_delegation *delegation;
1032 delegation = nfs4_get_valid_delegation(inode);
1033 if (delegation == NULL)
1036 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1038 nfs_mark_return_delegation(server, delegation);
1052 struct nfs_delegation *delegation;
1056 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1057 spin_lock(&delegation->lock);
1058 if (delegation->inode != NULL &&
1059 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1060 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1063 res = igrab(delegation->inode);
1065 spin_unlock(&delegation->lock);
1075 spin_unlock(&delegation->lock);
1081 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1083 * @fhandle: filehandle from a delegation recall
1108 struct nfs_delegation *delegation;
1110 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1112 * If the delegation may have been admin revoked, then we
1115 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1117 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1139 struct nfs_delegation *delegation;
1144 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1146 &delegation->flags) ||
1148 &delegation->flags) ||
1150 &delegation->flags) == 0)
1152 inode = nfs_delegation_grab_inode(delegation);
1155 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1157 if (delegation != NULL) {
1158 if (nfs_detach_delegation(NFS_I(inode), delegation,
1160 nfs_free_delegation(delegation);
1162 nfs_put_delegation(delegation);
1191 struct nfs_delegation *delegation)
1193 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1195 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1196 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1203 struct nfs_delegation *delegation;
1206 delegation = rcu_dereference(NFS_I(inode)->delegation);
1207 if (delegation)
1208 nfs_mark_test_expired_delegation(server, delegation);
1215 struct nfs_delegation *delegation;
1217 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1218 nfs_mark_test_expired_delegation(server, delegation);
1269 struct nfs_delegation *delegation;
1276 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1278 &delegation->flags) ||
1280 &delegation->flags) ||
1282 &delegation->flags) == 0)
1284 inode = nfs_delegation_grab_inode(delegation);
1287 spin_lock(&delegation->lock);
1288 cred = get_cred_rcu(delegation->cred);
1289 nfs4_stateid_copy(&stateid, &delegation->stateid);
1290 spin_unlock(&delegation->lock);
1291 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1327 struct nfs_delegation *delegation;
1331 delegation = rcu_dereference(NFS_I(inode)->delegation);
1332 if (delegation &&
1333 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1334 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1335 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1366 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1370 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1371 * that matches our delegation stateid. Otherwise "false" is returned.
1375 struct nfs_delegation *delegation;
1381 delegation = rcu_dereference(NFS_I(inode)->delegation);
1382 if (delegation != NULL &&
1383 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1384 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1385 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1386 dst->seqid = delegation->stateid.seqid;
1397 * @flags: delegation type requirement
1401 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1408 struct nfs_delegation *delegation;
1413 delegation = rcu_dereference(nfsi->delegation);
1414 if (!delegation)
1416 spin_lock(&delegation->lock);
1417 ret = nfs4_is_valid_delegation(delegation, flags);
1419 nfs4_stateid_copy(dst, &delegation->stateid);
1420 nfs_mark_delegation_referenced(delegation);
1422 *cred = get_cred(delegation->cred);
1424 spin_unlock(&delegation->lock);
1435 * against the delegation 'space_limit' field to see if
1441 struct nfs_delegation *delegation;
1445 delegation = rcu_dereference(nfsi->delegation);
1446 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1448 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)