xref: /openbmc/linux/fs/nfs/delegation.c (revision 13437e12)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/delegation.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2004 Trond Myklebust
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * NFS file delegation management
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/completion.h>
1058d9714aSTrond Myklebust #include <linux/kthread.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
131da177e4SLinus Torvalds #include <linux/spinlock.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/nfs4.h>
161da177e4SLinus Torvalds #include <linux/nfs_fs.h>
171da177e4SLinus Torvalds #include <linux/nfs_xdr.h>
181da177e4SLinus Torvalds 
194ce79717STrond Myklebust #include "nfs4_fs.h"
201da177e4SLinus Torvalds #include "delegation.h"
2124c8dbbbSDavid Howells #include "internal.h"
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds static void nfs_free_delegation(struct nfs_delegation *delegation)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	if (delegation->cred)
261da177e4SLinus Torvalds 		put_rpccred(delegation->cred);
271da177e4SLinus Torvalds 	kfree(delegation);
281da177e4SLinus Torvalds }
291da177e4SLinus Torvalds 
30888e694cSTrond Myklebust static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
31888e694cSTrond Myklebust {
32888e694cSTrond Myklebust 	struct inode *inode = state->inode;
33888e694cSTrond Myklebust 	struct file_lock *fl;
34888e694cSTrond Myklebust 	int status;
35888e694cSTrond Myklebust 
36888e694cSTrond Myklebust 	for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
37888e694cSTrond Myklebust 		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
38888e694cSTrond Myklebust 			continue;
39888e694cSTrond Myklebust 		if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
40888e694cSTrond Myklebust 			continue;
41888e694cSTrond Myklebust 		status = nfs4_lock_delegation_recall(state, fl);
42888e694cSTrond Myklebust 		if (status >= 0)
43888e694cSTrond Myklebust 			continue;
44888e694cSTrond Myklebust 		switch (status) {
45888e694cSTrond Myklebust 			default:
46888e694cSTrond Myklebust 				printk(KERN_ERR "%s: unhandled error %d.\n",
47888e694cSTrond Myklebust 						__FUNCTION__, status);
48888e694cSTrond Myklebust 			case -NFS4ERR_EXPIRED:
49888e694cSTrond Myklebust 				/* kill_proc(fl->fl_pid, SIGLOST, 1); */
50888e694cSTrond Myklebust 			case -NFS4ERR_STALE_CLIENTID:
517539bbabSDavid Howells 				nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
52888e694cSTrond Myklebust 				goto out_err;
53888e694cSTrond Myklebust 		}
54888e694cSTrond Myklebust 	}
55888e694cSTrond Myklebust 	return 0;
56888e694cSTrond Myklebust out_err:
57888e694cSTrond Myklebust 	return status;
58888e694cSTrond Myklebust }
59888e694cSTrond Myklebust 
6090163027STrond Myklebust static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
631da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
641da177e4SLinus Torvalds 	struct nfs4_state *state;
65888e694cSTrond Myklebust 	int err;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds again:
681da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
691da177e4SLinus Torvalds 	list_for_each_entry(ctx, &nfsi->open_files, list) {
701da177e4SLinus Torvalds 		state = ctx->state;
711da177e4SLinus Torvalds 		if (state == NULL)
721da177e4SLinus Torvalds 			continue;
731da177e4SLinus Torvalds 		if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
741da177e4SLinus Torvalds 			continue;
7590163027STrond Myklebust 		if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
7690163027STrond Myklebust 			continue;
771da177e4SLinus Torvalds 		get_nfs_open_context(ctx);
781da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
7913437e12STrond Myklebust 		err = nfs4_open_delegation_recall(ctx, state, stateid);
80888e694cSTrond Myklebust 		if (err >= 0)
81888e694cSTrond Myklebust 			err = nfs_delegation_claim_locks(ctx, state);
821da177e4SLinus Torvalds 		put_nfs_open_context(ctx);
83888e694cSTrond Myklebust 		if (err != 0)
84888e694cSTrond Myklebust 			return;
851da177e4SLinus Torvalds 		goto again;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * Set up a delegation on an inode
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct nfs_delegation *delegation = NFS_I(inode)->delegation;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (delegation == NULL)
981da177e4SLinus Torvalds 		return;
991da177e4SLinus Torvalds 	memcpy(delegation->stateid.data, res->delegation.data,
1001da177e4SLinus Torvalds 			sizeof(delegation->stateid.data));
1011da177e4SLinus Torvalds 	delegation->type = res->delegation_type;
1021da177e4SLinus Torvalds 	delegation->maxsize = res->maxsize;
1031da177e4SLinus Torvalds 	put_rpccred(cred);
1041da177e4SLinus Torvalds 	delegation->cred = get_rpccred(cred);
1051da177e4SLinus Torvalds 	delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
1061da177e4SLinus Torvalds 	NFS_I(inode)->delegation_state = delegation->type;
1071da177e4SLinus Torvalds 	smp_wmb();
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * Set up a delegation on an inode
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
1141da177e4SLinus Torvalds {
1157539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1161da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
1171da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
1181da177e4SLinus Torvalds 	int status = 0;
1191da177e4SLinus Torvalds 
120b3c52da3STrond Myklebust 	/* Ensure we first revalidate the attributes and page cache! */
121b3c52da3STrond Myklebust 	if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
122b3c52da3STrond Myklebust 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
123b3c52da3STrond Myklebust 
124f52720caSPanagiotis Issaris 	delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
1251da177e4SLinus Torvalds 	if (delegation == NULL)
1261da177e4SLinus Torvalds 		return -ENOMEM;
1271da177e4SLinus Torvalds 	memcpy(delegation->stateid.data, res->delegation.data,
1281da177e4SLinus Torvalds 			sizeof(delegation->stateid.data));
1291da177e4SLinus Torvalds 	delegation->type = res->delegation_type;
1301da177e4SLinus Torvalds 	delegation->maxsize = res->maxsize;
131beb2a5ecSTrond Myklebust 	delegation->change_attr = nfsi->change_attr;
1321da177e4SLinus Torvalds 	delegation->cred = get_rpccred(cred);
1331da177e4SLinus Torvalds 	delegation->inode = inode;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
1361da177e4SLinus Torvalds 	if (nfsi->delegation == NULL) {
1371da177e4SLinus Torvalds 		list_add(&delegation->super_list, &clp->cl_delegations);
1381da177e4SLinus Torvalds 		nfsi->delegation = delegation;
1391da177e4SLinus Torvalds 		nfsi->delegation_state = delegation->type;
1401da177e4SLinus Torvalds 		delegation = NULL;
1411da177e4SLinus Torvalds 	} else {
1421da177e4SLinus Torvalds 		if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
1431da177e4SLinus Torvalds 					sizeof(delegation->stateid)) != 0 ||
1441da177e4SLinus Torvalds 				delegation->type != nfsi->delegation->type) {
1451da177e4SLinus Torvalds 			printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
14624c8dbbbSDavid Howells 					__FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
1471da177e4SLinus Torvalds 			status = -EIO;
1481da177e4SLinus Torvalds 		}
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
1511da177e4SLinus Torvalds 	kfree(delegation);
1521da177e4SLinus Torvalds 	return status;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	int res = 0;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
1601da177e4SLinus Torvalds 	nfs_free_delegation(delegation);
1611da177e4SLinus Torvalds 	return res;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /* Sync all data to disk upon delegation return */
1651da177e4SLinus Torvalds static void nfs_msync_inode(struct inode *inode)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	filemap_fdatawrite(inode->i_mapping);
1681da177e4SLinus Torvalds 	nfs_wb_all(inode);
1691da177e4SLinus Torvalds 	filemap_fdatawait(inode->i_mapping);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * Basic procedure for returning a delegation to the server
1741da177e4SLinus Torvalds  */
17590163027STrond Myklebust static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
1761da177e4SLinus Torvalds {
1777539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1781da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	nfs_msync_inode(inode);
1811da177e4SLinus Torvalds 	down_read(&clp->cl_sem);
1821da177e4SLinus Torvalds 	/* Guard against new delegated open calls */
1831da177e4SLinus Torvalds 	down_write(&nfsi->rwsem);
18490163027STrond Myklebust 	nfs_delegation_claim_opens(inode, &delegation->stateid);
1851da177e4SLinus Torvalds 	up_write(&nfsi->rwsem);
1861da177e4SLinus Torvalds 	up_read(&clp->cl_sem);
1871da177e4SLinus Torvalds 	nfs_msync_inode(inode);
1881da177e4SLinus Torvalds 
18990163027STrond Myklebust 	return nfs_do_return_delegation(inode, delegation);
19090163027STrond Myklebust }
19190163027STrond Myklebust 
19290163027STrond Myklebust static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
19390163027STrond Myklebust {
19490163027STrond Myklebust 	struct nfs_delegation *delegation = nfsi->delegation;
19590163027STrond Myklebust 
19690163027STrond Myklebust 	if (delegation == NULL)
19790163027STrond Myklebust 		goto nomatch;
19890163027STrond Myklebust 	if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
19990163027STrond Myklebust 				sizeof(delegation->stateid.data)) != 0)
20090163027STrond Myklebust 		goto nomatch;
20190163027STrond Myklebust 	list_del_init(&delegation->super_list);
20290163027STrond Myklebust 	nfsi->delegation = NULL;
20390163027STrond Myklebust 	nfsi->delegation_state = 0;
20490163027STrond Myklebust 	return delegation;
20590163027STrond Myklebust nomatch:
20690163027STrond Myklebust 	return NULL;
20790163027STrond Myklebust }
20890163027STrond Myklebust 
20990163027STrond Myklebust int nfs_inode_return_delegation(struct inode *inode)
21090163027STrond Myklebust {
21190163027STrond Myklebust 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
21290163027STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
21390163027STrond Myklebust 	struct nfs_delegation *delegation;
21490163027STrond Myklebust 	int err = 0;
21590163027STrond Myklebust 
21690163027STrond Myklebust 	if (nfsi->delegation_state != 0) {
21790163027STrond Myklebust 		spin_lock(&clp->cl_lock);
21890163027STrond Myklebust 		delegation = nfs_detach_delegation_locked(nfsi, NULL);
21990163027STrond Myklebust 		spin_unlock(&clp->cl_lock);
2201da177e4SLinus Torvalds 		if (delegation != NULL)
22190163027STrond Myklebust 			err = __nfs_inode_return_delegation(inode, delegation);
22290163027STrond Myklebust 	}
22390163027STrond Myklebust 	return err;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /*
2271da177e4SLinus Torvalds  * Return all delegations associated to a super block
2281da177e4SLinus Torvalds  */
2291da177e4SLinus Torvalds void nfs_return_all_delegations(struct super_block *sb)
2301da177e4SLinus Torvalds {
2317539bbabSDavid Howells 	struct nfs_client *clp = NFS_SB(sb)->nfs_client;
2321da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
2331da177e4SLinus Torvalds 	struct inode *inode;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (clp == NULL)
2361da177e4SLinus Torvalds 		return;
2371da177e4SLinus Torvalds restart:
2381da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
2391da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
2401da177e4SLinus Torvalds 		if (delegation->inode->i_sb != sb)
2411da177e4SLinus Torvalds 			continue;
2421da177e4SLinus Torvalds 		inode = igrab(delegation->inode);
2431da177e4SLinus Torvalds 		if (inode == NULL)
2441da177e4SLinus Torvalds 			continue;
24590163027STrond Myklebust 		nfs_detach_delegation_locked(NFS_I(inode), NULL);
2461da177e4SLinus Torvalds 		spin_unlock(&clp->cl_lock);
24790163027STrond Myklebust 		__nfs_inode_return_delegation(inode, delegation);
2481da177e4SLinus Torvalds 		iput(inode);
2491da177e4SLinus Torvalds 		goto restart;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
25410afec90STrond Myklebust static int nfs_do_expire_all_delegations(void *ptr)
25558d9714aSTrond Myklebust {
256adfa6f98SDavid Howells 	struct nfs_client *clp = ptr;
25758d9714aSTrond Myklebust 	struct nfs_delegation *delegation;
25858d9714aSTrond Myklebust 	struct inode *inode;
25958d9714aSTrond Myklebust 
26058d9714aSTrond Myklebust 	allow_signal(SIGKILL);
26158d9714aSTrond Myklebust restart:
26258d9714aSTrond Myklebust 	spin_lock(&clp->cl_lock);
26358d9714aSTrond Myklebust 	if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
26458d9714aSTrond Myklebust 		goto out;
26558d9714aSTrond Myklebust 	if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
26658d9714aSTrond Myklebust 		goto out;
26758d9714aSTrond Myklebust 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
26858d9714aSTrond Myklebust 		inode = igrab(delegation->inode);
26958d9714aSTrond Myklebust 		if (inode == NULL)
27058d9714aSTrond Myklebust 			continue;
27190163027STrond Myklebust 		nfs_detach_delegation_locked(NFS_I(inode), NULL);
27258d9714aSTrond Myklebust 		spin_unlock(&clp->cl_lock);
27390163027STrond Myklebust 		__nfs_inode_return_delegation(inode, delegation);
27458d9714aSTrond Myklebust 		iput(inode);
27558d9714aSTrond Myklebust 		goto restart;
27658d9714aSTrond Myklebust 	}
27758d9714aSTrond Myklebust out:
27858d9714aSTrond Myklebust 	spin_unlock(&clp->cl_lock);
27924c8dbbbSDavid Howells 	nfs_put_client(clp);
28058d9714aSTrond Myklebust 	module_put_and_exit(0);
28158d9714aSTrond Myklebust }
28258d9714aSTrond Myklebust 
283adfa6f98SDavid Howells void nfs_expire_all_delegations(struct nfs_client *clp)
28458d9714aSTrond Myklebust {
28558d9714aSTrond Myklebust 	struct task_struct *task;
28658d9714aSTrond Myklebust 
28758d9714aSTrond Myklebust 	__module_get(THIS_MODULE);
28858d9714aSTrond Myklebust 	atomic_inc(&clp->cl_count);
28958d9714aSTrond Myklebust 	task = kthread_run(nfs_do_expire_all_delegations, clp,
29058d9714aSTrond Myklebust 			"%u.%u.%u.%u-delegreturn",
29124c8dbbbSDavid Howells 			NIPQUAD(clp->cl_addr.sin_addr));
29258d9714aSTrond Myklebust 	if (!IS_ERR(task))
29358d9714aSTrond Myklebust 		return;
29424c8dbbbSDavid Howells 	nfs_put_client(clp);
29558d9714aSTrond Myklebust 	module_put(THIS_MODULE);
29658d9714aSTrond Myklebust }
29758d9714aSTrond Myklebust 
2981da177e4SLinus Torvalds /*
2991da177e4SLinus Torvalds  * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
3001da177e4SLinus Torvalds  */
301adfa6f98SDavid Howells void nfs_handle_cb_pathdown(struct nfs_client *clp)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3041da177e4SLinus Torvalds 	struct inode *inode;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (clp == NULL)
3071da177e4SLinus Torvalds 		return;
3081da177e4SLinus Torvalds restart:
3091da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
3101da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
3111da177e4SLinus Torvalds 		inode = igrab(delegation->inode);
3121da177e4SLinus Torvalds 		if (inode == NULL)
3131da177e4SLinus Torvalds 			continue;
31490163027STrond Myklebust 		nfs_detach_delegation_locked(NFS_I(inode), NULL);
3151da177e4SLinus Torvalds 		spin_unlock(&clp->cl_lock);
31690163027STrond Myklebust 		__nfs_inode_return_delegation(inode, delegation);
3171da177e4SLinus Torvalds 		iput(inode);
3181da177e4SLinus Torvalds 		goto restart;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds struct recall_threadargs {
3241da177e4SLinus Torvalds 	struct inode *inode;
325adfa6f98SDavid Howells 	struct nfs_client *clp;
3261da177e4SLinus Torvalds 	const nfs4_stateid *stateid;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	struct completion started;
3291da177e4SLinus Torvalds 	int result;
3301da177e4SLinus Torvalds };
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static int recall_thread(void *data)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct recall_threadargs *args = (struct recall_threadargs *)data;
3351da177e4SLinus Torvalds 	struct inode *inode = igrab(args->inode);
3367539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
3371da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3381da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	daemonize("nfsv4-delegreturn");
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	nfs_msync_inode(inode);
3431da177e4SLinus Torvalds 	down_read(&clp->cl_sem);
3441da177e4SLinus Torvalds 	down_write(&nfsi->rwsem);
3451da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
34690163027STrond Myklebust 	delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
34790163027STrond Myklebust 	if (delegation != NULL)
3481da177e4SLinus Torvalds 		args->result = 0;
34990163027STrond Myklebust 	else
3501da177e4SLinus Torvalds 		args->result = -ENOENT;
3511da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
3521da177e4SLinus Torvalds 	complete(&args->started);
35390163027STrond Myklebust 	nfs_delegation_claim_opens(inode, args->stateid);
3541da177e4SLinus Torvalds 	up_write(&nfsi->rwsem);
3551da177e4SLinus Torvalds 	up_read(&clp->cl_sem);
3561da177e4SLinus Torvalds 	nfs_msync_inode(inode);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (delegation != NULL)
3591da177e4SLinus Torvalds 		nfs_do_return_delegation(inode, delegation);
3601da177e4SLinus Torvalds 	iput(inode);
3611da177e4SLinus Torvalds 	module_put_and_exit(0);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /*
3651da177e4SLinus Torvalds  * Asynchronous delegation recall!
3661da177e4SLinus Torvalds  */
3671da177e4SLinus Torvalds int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct recall_threadargs data = {
3701da177e4SLinus Torvalds 		.inode = inode,
3711da177e4SLinus Torvalds 		.stateid = stateid,
3721da177e4SLinus Torvalds 	};
3731da177e4SLinus Torvalds 	int status;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	init_completion(&data.started);
3761da177e4SLinus Torvalds 	__module_get(THIS_MODULE);
3771da177e4SLinus Torvalds 	status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
3781da177e4SLinus Torvalds 	if (status < 0)
3791da177e4SLinus Torvalds 		goto out_module_put;
3801da177e4SLinus Torvalds 	wait_for_completion(&data.started);
3811da177e4SLinus Torvalds 	return data.result;
3821da177e4SLinus Torvalds out_module_put:
3831da177e4SLinus Torvalds 	module_put(THIS_MODULE);
3841da177e4SLinus Torvalds 	return status;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * Retrieve the inode associated with a delegation
3891da177e4SLinus Torvalds  */
390adfa6f98SDavid Howells struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3931da177e4SLinus Torvalds 	struct inode *res = NULL;
3941da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
3951da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
3961da177e4SLinus Torvalds 		if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
3971da177e4SLinus Torvalds 			res = igrab(delegation->inode);
3981da177e4SLinus Torvalds 			break;
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
4021da177e4SLinus Torvalds 	return res;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4061da177e4SLinus Torvalds  * Mark all delegations as needing to be reclaimed
4071da177e4SLinus Torvalds  */
408adfa6f98SDavid Howells void nfs_delegation_mark_reclaim(struct nfs_client *clp)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
4111da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
4121da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list)
4131da177e4SLinus Torvalds 		delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
4141da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds /*
4181da177e4SLinus Torvalds  * Reap all unclaimed delegations after reboot recovery is done
4191da177e4SLinus Torvalds  */
420adfa6f98SDavid Howells void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct nfs_delegation *delegation, *n;
4231da177e4SLinus Torvalds 	LIST_HEAD(head);
4241da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
4251da177e4SLinus Torvalds 	list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
4261da177e4SLinus Torvalds 		if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
4271da177e4SLinus Torvalds 			continue;
4281da177e4SLinus Torvalds 		list_move(&delegation->super_list, &head);
4291da177e4SLinus Torvalds 		NFS_I(delegation->inode)->delegation = NULL;
4301da177e4SLinus Torvalds 		NFS_I(delegation->inode)->delegation_state = 0;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
4331da177e4SLinus Torvalds 	while(!list_empty(&head)) {
4341da177e4SLinus Torvalds 		delegation = list_entry(head.next, struct nfs_delegation, super_list);
4351da177e4SLinus Torvalds 		list_del(&delegation->super_list);
4361da177e4SLinus Torvalds 		nfs_free_delegation(delegation);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds }
4393e4f6290STrond Myklebust 
4403e4f6290STrond Myklebust int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
4413e4f6290STrond Myklebust {
4427539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
4433e4f6290STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
4443e4f6290STrond Myklebust 	struct nfs_delegation *delegation;
4453e4f6290STrond Myklebust 	int res = 0;
4463e4f6290STrond Myklebust 
4473e4f6290STrond Myklebust 	if (nfsi->delegation_state == 0)
4483e4f6290STrond Myklebust 		return 0;
4493e4f6290STrond Myklebust 	spin_lock(&clp->cl_lock);
4503e4f6290STrond Myklebust 	delegation = nfsi->delegation;
4513e4f6290STrond Myklebust 	if (delegation != NULL) {
4523e4f6290STrond Myklebust 		memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
4533e4f6290STrond Myklebust 		res = 1;
4543e4f6290STrond Myklebust 	}
4553e4f6290STrond Myklebust 	spin_unlock(&clp->cl_lock);
4563e4f6290STrond Myklebust 	return res;
4573e4f6290STrond Myklebust }
458