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 * nfs4_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_KERNEL_ACCOUNT);
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;
493 * the delegation, then pre-emptively ask for a full attribute
507 list_add_tail_rcu(&delegation->super_list, &server->delegations);
508 rcu_assign_pointer(nfsi->delegation, delegation);
509 delegation = NULL;
516 if (delegation != NULL)
517 __nfs_free_delegation(delegation);
526 * Basic procedure for returning a delegation to the server
528 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
534 if (delegation == NULL)
543 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
545 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
546 delegation->type);
556 nfs_abort_delegation_return(delegation, clp, err);
560 err = nfs_do_return_delegation(inode, delegation, issync);
563 nfs_put_delegation(delegation);
567 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
571 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
573 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
576 spin_lock(&delegation->lock);
577 inode = delegation->inode;
580 spin_unlock(&delegation->lock);
583 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
584 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
585 test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
586 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
595 struct nfs_delegation *delegation;
606 * list delegation in the server from the delegations
608 * prev is an RCU-protected pointer to a delegation which
613 delegation = NULL;
616 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
617 if (!delegation || delegation != place_holder_deleg)
618 delegation = list_entry_rcu(server->delegations.next,
620 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
623 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
625 if (!nfs_delegation_need_return(delegation)) {
626 if (nfs4_is_valid_delegation(delegation, 0))
627 prev = delegation;
640 inode = nfs_delegation_grab_inode(delegation);
646 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
651 err = nfs_end_delegation_return(inode, delegation, 0);
720 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
723 * Does not protect against delegation reclaims, therefore really only safe
725 * the delegation structure.
729 struct nfs_delegation *delegation;
731 delegation = nfs_inode_detach_delegation(inode);
732 if (delegation != NULL) {
733 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
734 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
735 nfs_do_return_delegation(inode, delegation, 1);
736 nfs_free_delegation(delegation);
741 * nfs4_inode_return_delegation - synchronously return a delegation
745 * assumption that if we need to return the delegation, then
753 struct nfs_delegation *delegation;
755 delegation = nfs_start_delegation_return(nfsi);
756 if (delegation != NULL) {
761 return nfs_end_delegation_return(inode, delegation, 1);
767 * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
771 * inode delegation needs to be returned immediately.
775 struct nfs_delegation *delegation;
781 delegation = nfs4_get_valid_delegation(inode);
782 if (!delegation)
784 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
786 spin_lock(&delegation->lock);
787 if (delegation->inode &&
789 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
790 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
792 ret = nfs_get_delegation(delegation);
794 spin_unlock(&delegation->lock);
807 * Make the inode writeable by returning the delegation if necessary
813 struct nfs_delegation *delegation;
816 delegation = nfs4_get_valid_delegation(inode);
817 if (delegation == NULL ||
819 (delegation->type & FMODE_WRITE))) {
828 struct nfs_delegation *delegation)
830 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
836 struct nfs_delegation *delegation;
839 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
840 nfs_mark_return_delegation(server, delegation);
899 struct nfs_delegation *delegation;
901 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
902 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
904 if (delegation->type & flags)
905 nfs_mark_return_if_closed_delegation(server, delegation);
923 struct nfs_delegation *delegation;
928 delegation = rcu_dereference(NFS_I(inode)->delegation);
929 if (delegation == NULL)
932 nfs4_stateid_copy(&tmp, &delegation->stateid);
935 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
937 spin_lock(&delegation->lock);
939 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
940 spin_unlock(&delegation->lock);
943 delegation->stateid.seqid = stateid->seqid;
945 spin_unlock(&delegation->lock);
947 nfs_mark_delegation_revoked(delegation);
965 struct nfs_delegation *delegation;
971 delegation = rcu_dereference(NFS_I(inode)->delegation);
972 if (!delegation)
975 spin_lock(&delegation->lock);
976 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
979 /* If delegation->stateid is newer, dont mark as returned */
980 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
982 if (delegation->stateid.seqid != stateid->seqid)
983 delegation->stateid.seqid = stateid->seqid;
986 nfs_mark_delegation_revoked(delegation);
989 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
991 spin_unlock(&delegation->lock);
1001 * @flags: delegation types to expire
1012 struct nfs_delegation *delegation;
1014 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1015 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
1017 nfs_mark_return_if_closed_delegation(server, delegation);
1039 * nfs_async_inode_return_delegation - asynchronously return a delegation
1050 struct nfs_delegation *delegation;
1053 delegation = nfs4_get_valid_delegation(inode);
1054 if (delegation == NULL)
1057 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1059 nfs_mark_return_delegation(server, delegation);
1076 struct nfs_delegation *delegation;
1080 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1081 spin_lock(&delegation->lock);
1082 if (delegation->inode != NULL &&
1083 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1084 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1087 res = igrab(delegation->inode);
1089 spin_unlock(&delegation->lock);
1099 spin_unlock(&delegation->lock);
1105 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1107 * @fhandle: filehandle from a delegation recall
1132 struct nfs_delegation *delegation;
1134 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1136 * If the delegation may have been admin revoked, then we
1139 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1141 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1163 struct nfs_delegation *delegation;
1168 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1170 &delegation->flags) ||
1172 &delegation->flags) ||
1174 &delegation->flags) == 0)
1176 inode = nfs_delegation_grab_inode(delegation);
1179 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1181 if (delegation != NULL) {
1182 if (nfs_detach_delegation(NFS_I(inode), delegation,
1184 nfs_free_delegation(delegation);
1186 nfs_put_delegation(delegation);
1215 struct nfs_delegation *delegation)
1217 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1219 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1220 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1227 struct nfs_delegation *delegation;
1230 delegation = rcu_dereference(NFS_I(inode)->delegation);
1231 if (delegation)
1232 nfs_mark_test_expired_delegation(server, delegation);
1239 struct nfs_delegation *delegation;
1241 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1242 nfs_mark_test_expired_delegation(server, delegation);
1293 struct nfs_delegation *delegation;
1300 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1302 &delegation->flags) ||
1304 &delegation->flags) ||
1306 &delegation->flags) == 0)
1308 inode = nfs_delegation_grab_inode(delegation);
1311 spin_lock(&delegation->lock);
1312 cred = get_cred_rcu(delegation->cred);
1313 nfs4_stateid_copy(&stateid, &delegation->stateid);
1314 spin_unlock(&delegation->lock);
1315 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1351 struct nfs_delegation *delegation;
1355 delegation = rcu_dereference(NFS_I(inode)->delegation);
1356 if (delegation &&
1357 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1358 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1359 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1390 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1394 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1395 * that matches our delegation stateid. Otherwise "false" is returned.
1399 struct nfs_delegation *delegation;
1405 delegation = rcu_dereference(NFS_I(inode)->delegation);
1406 if (delegation != NULL &&
1407 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1408 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1409 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1410 dst->seqid = delegation->stateid.seqid;
1421 * @flags: delegation type requirement
1425 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1432 struct nfs_delegation *delegation;
1437 delegation = rcu_dereference(nfsi->delegation);
1438 if (!delegation)
1440 spin_lock(&delegation->lock);
1441 ret = nfs4_is_valid_delegation(delegation, flags);
1443 nfs4_stateid_copy(dst, &delegation->stateid);
1444 nfs_mark_delegation_referenced(delegation);
1446 *cred = get_cred(delegation->cred);
1448 spin_unlock(&delegation->lock);
1459 * against the delegation 'space_limit' field to see if
1465 struct nfs_delegation *delegation;
1469 delegation = rcu_dereference(nfsi->delegation);
1470 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1472 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)