xref: /openbmc/linux/fs/nfs/delegation.c (revision 24c8dbbb)
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 struct nfs_delegation *nfs_alloc_delegation(void)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static void nfs_free_delegation(struct nfs_delegation *delegation)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	if (delegation->cred)
311da177e4SLinus Torvalds 		put_rpccred(delegation->cred);
321da177e4SLinus Torvalds 	kfree(delegation);
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
35888e694cSTrond Myklebust static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
36888e694cSTrond Myklebust {
37888e694cSTrond Myklebust 	struct inode *inode = state->inode;
38888e694cSTrond Myklebust 	struct file_lock *fl;
39888e694cSTrond Myklebust 	int status;
40888e694cSTrond Myklebust 
41888e694cSTrond Myklebust 	for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
42888e694cSTrond Myklebust 		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
43888e694cSTrond Myklebust 			continue;
44888e694cSTrond Myklebust 		if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
45888e694cSTrond Myklebust 			continue;
46888e694cSTrond Myklebust 		status = nfs4_lock_delegation_recall(state, fl);
47888e694cSTrond Myklebust 		if (status >= 0)
48888e694cSTrond Myklebust 			continue;
49888e694cSTrond Myklebust 		switch (status) {
50888e694cSTrond Myklebust 			default:
51888e694cSTrond Myklebust 				printk(KERN_ERR "%s: unhandled error %d.\n",
52888e694cSTrond Myklebust 						__FUNCTION__, status);
53888e694cSTrond Myklebust 			case -NFS4ERR_EXPIRED:
54888e694cSTrond Myklebust 				/* kill_proc(fl->fl_pid, SIGLOST, 1); */
55888e694cSTrond Myklebust 			case -NFS4ERR_STALE_CLIENTID:
567539bbabSDavid Howells 				nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
57888e694cSTrond Myklebust 				goto out_err;
58888e694cSTrond Myklebust 		}
59888e694cSTrond Myklebust 	}
60888e694cSTrond Myklebust 	return 0;
61888e694cSTrond Myklebust out_err:
62888e694cSTrond Myklebust 	return status;
63888e694cSTrond Myklebust }
64888e694cSTrond Myklebust 
651da177e4SLinus Torvalds static void nfs_delegation_claim_opens(struct inode *inode)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
681da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
691da177e4SLinus Torvalds 	struct nfs4_state *state;
70888e694cSTrond Myklebust 	int err;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds again:
731da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
741da177e4SLinus Torvalds 	list_for_each_entry(ctx, &nfsi->open_files, list) {
751da177e4SLinus Torvalds 		state = ctx->state;
761da177e4SLinus Torvalds 		if (state == NULL)
771da177e4SLinus Torvalds 			continue;
781da177e4SLinus Torvalds 		if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
791da177e4SLinus Torvalds 			continue;
801da177e4SLinus Torvalds 		get_nfs_open_context(ctx);
811da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
82888e694cSTrond Myklebust 		err = nfs4_open_delegation_recall(ctx->dentry, state);
83888e694cSTrond Myklebust 		if (err >= 0)
84888e694cSTrond Myklebust 			err = nfs_delegation_claim_locks(ctx, state);
851da177e4SLinus Torvalds 		put_nfs_open_context(ctx);
86888e694cSTrond Myklebust 		if (err != 0)
87888e694cSTrond Myklebust 			return;
881da177e4SLinus Torvalds 		goto again;
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * Set up a delegation on an inode
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct nfs_delegation *delegation = NFS_I(inode)->delegation;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if (delegation == NULL)
1011da177e4SLinus Torvalds 		return;
1021da177e4SLinus Torvalds 	memcpy(delegation->stateid.data, res->delegation.data,
1031da177e4SLinus Torvalds 			sizeof(delegation->stateid.data));
1041da177e4SLinus Torvalds 	delegation->type = res->delegation_type;
1051da177e4SLinus Torvalds 	delegation->maxsize = res->maxsize;
1061da177e4SLinus Torvalds 	put_rpccred(cred);
1071da177e4SLinus Torvalds 	delegation->cred = get_rpccred(cred);
1081da177e4SLinus Torvalds 	delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
1091da177e4SLinus Torvalds 	NFS_I(inode)->delegation_state = delegation->type;
1101da177e4SLinus Torvalds 	smp_wmb();
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /*
1141da177e4SLinus Torvalds  * Set up a delegation on an inode
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
1171da177e4SLinus Torvalds {
1187539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1191da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
1201da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
1211da177e4SLinus Torvalds 	int status = 0;
1221da177e4SLinus Torvalds 
123b3c52da3STrond Myklebust 	/* Ensure we first revalidate the attributes and page cache! */
124b3c52da3STrond Myklebust 	if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
125b3c52da3STrond Myklebust 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
126b3c52da3STrond Myklebust 
1271da177e4SLinus Torvalds 	delegation = nfs_alloc_delegation();
1281da177e4SLinus Torvalds 	if (delegation == NULL)
1291da177e4SLinus Torvalds 		return -ENOMEM;
1301da177e4SLinus Torvalds 	memcpy(delegation->stateid.data, res->delegation.data,
1311da177e4SLinus Torvalds 			sizeof(delegation->stateid.data));
1321da177e4SLinus Torvalds 	delegation->type = res->delegation_type;
1331da177e4SLinus Torvalds 	delegation->maxsize = res->maxsize;
134beb2a5ecSTrond Myklebust 	delegation->change_attr = nfsi->change_attr;
1351da177e4SLinus Torvalds 	delegation->cred = get_rpccred(cred);
1361da177e4SLinus Torvalds 	delegation->inode = inode;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
1391da177e4SLinus Torvalds 	if (nfsi->delegation == NULL) {
1401da177e4SLinus Torvalds 		list_add(&delegation->super_list, &clp->cl_delegations);
1411da177e4SLinus Torvalds 		nfsi->delegation = delegation;
1421da177e4SLinus Torvalds 		nfsi->delegation_state = delegation->type;
1431da177e4SLinus Torvalds 		delegation = NULL;
1441da177e4SLinus Torvalds 	} else {
1451da177e4SLinus Torvalds 		if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
1461da177e4SLinus Torvalds 					sizeof(delegation->stateid)) != 0 ||
1471da177e4SLinus Torvalds 				delegation->type != nfsi->delegation->type) {
1481da177e4SLinus Torvalds 			printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
14924c8dbbbSDavid Howells 					__FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
1501da177e4SLinus Torvalds 			status = -EIO;
1511da177e4SLinus Torvalds 		}
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
1541da177e4SLinus Torvalds 	kfree(delegation);
1551da177e4SLinus Torvalds 	return status;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	int res = 0;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
1631da177e4SLinus Torvalds 	nfs_free_delegation(delegation);
1641da177e4SLinus Torvalds 	return res;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* Sync all data to disk upon delegation return */
1681da177e4SLinus Torvalds static void nfs_msync_inode(struct inode *inode)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	filemap_fdatawrite(inode->i_mapping);
1711da177e4SLinus Torvalds 	nfs_wb_all(inode);
1721da177e4SLinus Torvalds 	filemap_fdatawait(inode->i_mapping);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  * Basic procedure for returning a delegation to the server
1771da177e4SLinus Torvalds  */
178cae7a073STrond Myklebust int __nfs_inode_return_delegation(struct inode *inode)
1791da177e4SLinus Torvalds {
1807539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1811da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
1821da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
1831da177e4SLinus Torvalds 	int res = 0;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	nfs_msync_inode(inode);
1861da177e4SLinus Torvalds 	down_read(&clp->cl_sem);
1871da177e4SLinus Torvalds 	/* Guard against new delegated open calls */
1881da177e4SLinus Torvalds 	down_write(&nfsi->rwsem);
1891da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
1901da177e4SLinus Torvalds 	delegation = nfsi->delegation;
1911da177e4SLinus Torvalds 	if (delegation != NULL) {
1921da177e4SLinus Torvalds 		list_del_init(&delegation->super_list);
1931da177e4SLinus Torvalds 		nfsi->delegation = NULL;
1941da177e4SLinus Torvalds 		nfsi->delegation_state = 0;
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
1971da177e4SLinus Torvalds 	nfs_delegation_claim_opens(inode);
1981da177e4SLinus Torvalds 	up_write(&nfsi->rwsem);
1991da177e4SLinus Torvalds 	up_read(&clp->cl_sem);
2001da177e4SLinus Torvalds 	nfs_msync_inode(inode);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (delegation != NULL)
2031da177e4SLinus Torvalds 		res = nfs_do_return_delegation(inode, delegation);
2041da177e4SLinus Torvalds 	return res;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  * Return all delegations associated to a super block
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds void nfs_return_all_delegations(struct super_block *sb)
2111da177e4SLinus Torvalds {
2127539bbabSDavid Howells 	struct nfs_client *clp = NFS_SB(sb)->nfs_client;
2131da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
2141da177e4SLinus Torvalds 	struct inode *inode;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if (clp == NULL)
2171da177e4SLinus Torvalds 		return;
2181da177e4SLinus Torvalds restart:
2191da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
2201da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
2211da177e4SLinus Torvalds 		if (delegation->inode->i_sb != sb)
2221da177e4SLinus Torvalds 			continue;
2231da177e4SLinus Torvalds 		inode = igrab(delegation->inode);
2241da177e4SLinus Torvalds 		if (inode == NULL)
2251da177e4SLinus Torvalds 			continue;
2261da177e4SLinus Torvalds 		spin_unlock(&clp->cl_lock);
2271da177e4SLinus Torvalds 		nfs_inode_return_delegation(inode);
2281da177e4SLinus Torvalds 		iput(inode);
2291da177e4SLinus Torvalds 		goto restart;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
23458d9714aSTrond Myklebust int nfs_do_expire_all_delegations(void *ptr)
23558d9714aSTrond Myklebust {
236adfa6f98SDavid Howells 	struct nfs_client *clp = ptr;
23758d9714aSTrond Myklebust 	struct nfs_delegation *delegation;
23858d9714aSTrond Myklebust 	struct inode *inode;
23958d9714aSTrond Myklebust 
24058d9714aSTrond Myklebust 	allow_signal(SIGKILL);
24158d9714aSTrond Myklebust restart:
24258d9714aSTrond Myklebust 	spin_lock(&clp->cl_lock);
24358d9714aSTrond Myklebust 	if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
24458d9714aSTrond Myklebust 		goto out;
24558d9714aSTrond Myklebust 	if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
24658d9714aSTrond Myklebust 		goto out;
24758d9714aSTrond Myklebust 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
24858d9714aSTrond Myklebust 		inode = igrab(delegation->inode);
24958d9714aSTrond Myklebust 		if (inode == NULL)
25058d9714aSTrond Myklebust 			continue;
25158d9714aSTrond Myklebust 		spin_unlock(&clp->cl_lock);
25226c78e15STrond Myklebust 		nfs_inode_return_delegation(inode);
25358d9714aSTrond Myklebust 		iput(inode);
25458d9714aSTrond Myklebust 		goto restart;
25558d9714aSTrond Myklebust 	}
25658d9714aSTrond Myklebust out:
25758d9714aSTrond Myklebust 	spin_unlock(&clp->cl_lock);
25824c8dbbbSDavid Howells 	nfs_put_client(clp);
25958d9714aSTrond Myklebust 	module_put_and_exit(0);
26058d9714aSTrond Myklebust }
26158d9714aSTrond Myklebust 
262adfa6f98SDavid Howells void nfs_expire_all_delegations(struct nfs_client *clp)
26358d9714aSTrond Myklebust {
26458d9714aSTrond Myklebust 	struct task_struct *task;
26558d9714aSTrond Myklebust 
26658d9714aSTrond Myklebust 	__module_get(THIS_MODULE);
26758d9714aSTrond Myklebust 	atomic_inc(&clp->cl_count);
26858d9714aSTrond Myklebust 	task = kthread_run(nfs_do_expire_all_delegations, clp,
26958d9714aSTrond Myklebust 			"%u.%u.%u.%u-delegreturn",
27024c8dbbbSDavid Howells 			NIPQUAD(clp->cl_addr.sin_addr));
27158d9714aSTrond Myklebust 	if (!IS_ERR(task))
27258d9714aSTrond Myklebust 		return;
27324c8dbbbSDavid Howells 	nfs_put_client(clp);
27458d9714aSTrond Myklebust 	module_put(THIS_MODULE);
27558d9714aSTrond Myklebust }
27658d9714aSTrond Myklebust 
2771da177e4SLinus Torvalds /*
2781da177e4SLinus Torvalds  * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
2791da177e4SLinus Torvalds  */
280adfa6f98SDavid Howells void nfs_handle_cb_pathdown(struct nfs_client *clp)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
2831da177e4SLinus Torvalds 	struct inode *inode;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	if (clp == NULL)
2861da177e4SLinus Torvalds 		return;
2871da177e4SLinus Torvalds restart:
2881da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
2891da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
2901da177e4SLinus Torvalds 		inode = igrab(delegation->inode);
2911da177e4SLinus Torvalds 		if (inode == NULL)
2921da177e4SLinus Torvalds 			continue;
2931da177e4SLinus Torvalds 		spin_unlock(&clp->cl_lock);
2941da177e4SLinus Torvalds 		nfs_inode_return_delegation(inode);
2951da177e4SLinus Torvalds 		iput(inode);
2961da177e4SLinus Torvalds 		goto restart;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds struct recall_threadargs {
3021da177e4SLinus Torvalds 	struct inode *inode;
303adfa6f98SDavid Howells 	struct nfs_client *clp;
3041da177e4SLinus Torvalds 	const nfs4_stateid *stateid;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	struct completion started;
3071da177e4SLinus Torvalds 	int result;
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static int recall_thread(void *data)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	struct recall_threadargs *args = (struct recall_threadargs *)data;
3131da177e4SLinus Torvalds 	struct inode *inode = igrab(args->inode);
3147539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
3151da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3161da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	daemonize("nfsv4-delegreturn");
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	nfs_msync_inode(inode);
3211da177e4SLinus Torvalds 	down_read(&clp->cl_sem);
3221da177e4SLinus Torvalds 	down_write(&nfsi->rwsem);
3231da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
3241da177e4SLinus Torvalds 	delegation = nfsi->delegation;
3251da177e4SLinus Torvalds 	if (delegation != NULL && memcmp(delegation->stateid.data,
3261da177e4SLinus Torvalds 				args->stateid->data,
3271da177e4SLinus Torvalds 				sizeof(delegation->stateid.data)) == 0) {
3281da177e4SLinus Torvalds 		list_del_init(&delegation->super_list);
3291da177e4SLinus Torvalds 		nfsi->delegation = NULL;
3301da177e4SLinus Torvalds 		nfsi->delegation_state = 0;
3311da177e4SLinus Torvalds 		args->result = 0;
3321da177e4SLinus Torvalds 	} else {
3331da177e4SLinus Torvalds 		delegation = NULL;
3341da177e4SLinus Torvalds 		args->result = -ENOENT;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
3371da177e4SLinus Torvalds 	complete(&args->started);
3381da177e4SLinus Torvalds 	nfs_delegation_claim_opens(inode);
3391da177e4SLinus Torvalds 	up_write(&nfsi->rwsem);
3401da177e4SLinus Torvalds 	up_read(&clp->cl_sem);
3411da177e4SLinus Torvalds 	nfs_msync_inode(inode);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (delegation != NULL)
3441da177e4SLinus Torvalds 		nfs_do_return_delegation(inode, delegation);
3451da177e4SLinus Torvalds 	iput(inode);
3461da177e4SLinus Torvalds 	module_put_and_exit(0);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /*
3501da177e4SLinus Torvalds  * Asynchronous delegation recall!
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct recall_threadargs data = {
3551da177e4SLinus Torvalds 		.inode = inode,
3561da177e4SLinus Torvalds 		.stateid = stateid,
3571da177e4SLinus Torvalds 	};
3581da177e4SLinus Torvalds 	int status;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	init_completion(&data.started);
3611da177e4SLinus Torvalds 	__module_get(THIS_MODULE);
3621da177e4SLinus Torvalds 	status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
3631da177e4SLinus Torvalds 	if (status < 0)
3641da177e4SLinus Torvalds 		goto out_module_put;
3651da177e4SLinus Torvalds 	wait_for_completion(&data.started);
3661da177e4SLinus Torvalds 	return data.result;
3671da177e4SLinus Torvalds out_module_put:
3681da177e4SLinus Torvalds 	module_put(THIS_MODULE);
3691da177e4SLinus Torvalds 	return status;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  * Retrieve the inode associated with a delegation
3741da177e4SLinus Torvalds  */
375adfa6f98SDavid Howells struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3781da177e4SLinus Torvalds 	struct inode *res = NULL;
3791da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
3801da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
3811da177e4SLinus Torvalds 		if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
3821da177e4SLinus Torvalds 			res = igrab(delegation->inode);
3831da177e4SLinus Torvalds 			break;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
3871da177e4SLinus Torvalds 	return res;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds  * Mark all delegations as needing to be reclaimed
3921da177e4SLinus Torvalds  */
393adfa6f98SDavid Howells void nfs_delegation_mark_reclaim(struct nfs_client *clp)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	struct nfs_delegation *delegation;
3961da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
3971da177e4SLinus Torvalds 	list_for_each_entry(delegation, &clp->cl_delegations, super_list)
3981da177e4SLinus Torvalds 		delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
3991da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  * Reap all unclaimed delegations after reboot recovery is done
4041da177e4SLinus Torvalds  */
405adfa6f98SDavid Howells void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct nfs_delegation *delegation, *n;
4081da177e4SLinus Torvalds 	LIST_HEAD(head);
4091da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
4101da177e4SLinus Torvalds 	list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
4111da177e4SLinus Torvalds 		if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
4121da177e4SLinus Torvalds 			continue;
4131da177e4SLinus Torvalds 		list_move(&delegation->super_list, &head);
4141da177e4SLinus Torvalds 		NFS_I(delegation->inode)->delegation = NULL;
4151da177e4SLinus Torvalds 		NFS_I(delegation->inode)->delegation_state = 0;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
4181da177e4SLinus Torvalds 	while(!list_empty(&head)) {
4191da177e4SLinus Torvalds 		delegation = list_entry(head.next, struct nfs_delegation, super_list);
4201da177e4SLinus Torvalds 		list_del(&delegation->super_list);
4211da177e4SLinus Torvalds 		nfs_free_delegation(delegation);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds }
4243e4f6290STrond Myklebust 
4253e4f6290STrond Myklebust int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
4263e4f6290STrond Myklebust {
4277539bbabSDavid Howells 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
4283e4f6290STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
4293e4f6290STrond Myklebust 	struct nfs_delegation *delegation;
4303e4f6290STrond Myklebust 	int res = 0;
4313e4f6290STrond Myklebust 
4323e4f6290STrond Myklebust 	if (nfsi->delegation_state == 0)
4333e4f6290STrond Myklebust 		return 0;
4343e4f6290STrond Myklebust 	spin_lock(&clp->cl_lock);
4353e4f6290STrond Myklebust 	delegation = nfsi->delegation;
4363e4f6290STrond Myklebust 	if (delegation != NULL) {
4373e4f6290STrond Myklebust 		memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
4383e4f6290STrond Myklebust 		res = 1;
4393e4f6290STrond Myklebust 	}
4403e4f6290STrond Myklebust 	spin_unlock(&clp->cl_lock);
4413e4f6290STrond Myklebust 	return res;
4423e4f6290STrond Myklebust }
443