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 23905f8d16STrond Myklebust static void nfs_do_free_delegation(struct nfs_delegation *delegation) 241da177e4SLinus Torvalds { 251da177e4SLinus Torvalds kfree(delegation); 261da177e4SLinus Torvalds } 271da177e4SLinus Torvalds 288383e460STrond Myklebust static void nfs_free_delegation_callback(struct rcu_head *head) 298383e460STrond Myklebust { 308383e460STrond Myklebust struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu); 318383e460STrond Myklebust 32905f8d16STrond Myklebust nfs_do_free_delegation(delegation); 33905f8d16STrond Myklebust } 34905f8d16STrond Myklebust 35905f8d16STrond Myklebust static void nfs_free_delegation(struct nfs_delegation *delegation) 36905f8d16STrond Myklebust { 37905f8d16STrond Myklebust struct rpc_cred *cred; 38905f8d16STrond Myklebust 39905f8d16STrond Myklebust cred = rcu_dereference(delegation->cred); 40905f8d16STrond Myklebust rcu_assign_pointer(delegation->cred, NULL); 41905f8d16STrond Myklebust call_rcu(&delegation->rcu, nfs_free_delegation_callback); 42905f8d16STrond Myklebust if (cred) 43905f8d16STrond Myklebust put_rpccred(cred); 448383e460STrond Myklebust } 458383e460STrond Myklebust 46888e694cSTrond Myklebust static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state) 47888e694cSTrond Myklebust { 48888e694cSTrond Myklebust struct inode *inode = state->inode; 49888e694cSTrond Myklebust struct file_lock *fl; 50888e694cSTrond Myklebust int status; 51888e694cSTrond Myklebust 52888e694cSTrond Myklebust for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) { 53888e694cSTrond Myklebust if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) 54888e694cSTrond Myklebust continue; 55cd3758e3STrond Myklebust if (nfs_file_open_context(fl->fl_file) != ctx) 56888e694cSTrond Myklebust continue; 57888e694cSTrond Myklebust status = nfs4_lock_delegation_recall(state, fl); 58888e694cSTrond Myklebust if (status >= 0) 59888e694cSTrond Myklebust continue; 60888e694cSTrond Myklebust switch (status) { 61888e694cSTrond Myklebust default: 62888e694cSTrond Myklebust printk(KERN_ERR "%s: unhandled error %d.\n", 63888e694cSTrond Myklebust __FUNCTION__, status); 64888e694cSTrond Myklebust case -NFS4ERR_EXPIRED: 65888e694cSTrond Myklebust /* kill_proc(fl->fl_pid, SIGLOST, 1); */ 66888e694cSTrond Myklebust case -NFS4ERR_STALE_CLIENTID: 677539bbabSDavid Howells nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client); 68888e694cSTrond Myklebust goto out_err; 69888e694cSTrond Myklebust } 70888e694cSTrond Myklebust } 71888e694cSTrond Myklebust return 0; 72888e694cSTrond Myklebust out_err: 73888e694cSTrond Myklebust return status; 74888e694cSTrond Myklebust } 75888e694cSTrond Myklebust 7690163027STrond Myklebust static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 791da177e4SLinus Torvalds struct nfs_open_context *ctx; 801da177e4SLinus Torvalds struct nfs4_state *state; 81888e694cSTrond Myklebust int err; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds again: 841da177e4SLinus Torvalds spin_lock(&inode->i_lock); 851da177e4SLinus Torvalds list_for_each_entry(ctx, &nfsi->open_files, list) { 861da177e4SLinus Torvalds state = ctx->state; 871da177e4SLinus Torvalds if (state == NULL) 881da177e4SLinus Torvalds continue; 891da177e4SLinus Torvalds if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) 901da177e4SLinus Torvalds continue; 9190163027STrond Myklebust if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) 9290163027STrond Myklebust continue; 931da177e4SLinus Torvalds get_nfs_open_context(ctx); 941da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 9513437e12STrond Myklebust err = nfs4_open_delegation_recall(ctx, state, stateid); 96888e694cSTrond Myklebust if (err >= 0) 97888e694cSTrond Myklebust err = nfs_delegation_claim_locks(ctx, state); 981da177e4SLinus Torvalds put_nfs_open_context(ctx); 99888e694cSTrond Myklebust if (err != 0) 100888e694cSTrond Myklebust return; 1011da177e4SLinus Torvalds goto again; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * Set up a delegation on an inode 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds struct nfs_delegation *delegation = NFS_I(inode)->delegation; 11205c88babSTrond Myklebust struct rpc_cred *oldcred; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds if (delegation == NULL) 1151da177e4SLinus Torvalds return; 1161da177e4SLinus Torvalds memcpy(delegation->stateid.data, res->delegation.data, 1171da177e4SLinus Torvalds sizeof(delegation->stateid.data)); 1181da177e4SLinus Torvalds delegation->type = res->delegation_type; 1191da177e4SLinus Torvalds delegation->maxsize = res->maxsize; 12005c88babSTrond Myklebust oldcred = delegation->cred; 1211da177e4SLinus Torvalds delegation->cred = get_rpccred(cred); 1221da177e4SLinus Torvalds delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM; 1231da177e4SLinus Torvalds NFS_I(inode)->delegation_state = delegation->type; 1241da177e4SLinus Torvalds smp_wmb(); 12505c88babSTrond Myklebust put_rpccred(oldcred); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * Set up a delegation on an inode 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) 1321da177e4SLinus Torvalds { 1337539bbabSDavid Howells struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 1341da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 1351da177e4SLinus Torvalds struct nfs_delegation *delegation; 1361da177e4SLinus Torvalds int status = 0; 1371da177e4SLinus Torvalds 138f52720caSPanagiotis Issaris delegation = kmalloc(sizeof(*delegation), GFP_KERNEL); 1391da177e4SLinus Torvalds if (delegation == NULL) 1401da177e4SLinus Torvalds return -ENOMEM; 1411da177e4SLinus Torvalds memcpy(delegation->stateid.data, res->delegation.data, 1421da177e4SLinus Torvalds sizeof(delegation->stateid.data)); 1431da177e4SLinus Torvalds delegation->type = res->delegation_type; 1441da177e4SLinus Torvalds delegation->maxsize = res->maxsize; 145beb2a5ecSTrond Myklebust delegation->change_attr = nfsi->change_attr; 1461da177e4SLinus Torvalds delegation->cred = get_rpccred(cred); 1471da177e4SLinus Torvalds delegation->inode = inode; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds spin_lock(&clp->cl_lock); 1508383e460STrond Myklebust if (rcu_dereference(nfsi->delegation) == NULL) { 1518383e460STrond Myklebust list_add_rcu(&delegation->super_list, &clp->cl_delegations); 1521da177e4SLinus Torvalds nfsi->delegation_state = delegation->type; 1538383e460STrond Myklebust rcu_assign_pointer(nfsi->delegation, delegation); 1541da177e4SLinus Torvalds delegation = NULL; 1551da177e4SLinus Torvalds } else { 1561da177e4SLinus Torvalds if (memcmp(&delegation->stateid, &nfsi->delegation->stateid, 1571da177e4SLinus Torvalds sizeof(delegation->stateid)) != 0 || 1581da177e4SLinus Torvalds delegation->type != nfsi->delegation->type) { 159*5d8515caSChuck Lever printk(KERN_WARNING "%s: server %s handed out " 160*5d8515caSChuck Lever "a duplicate delegation!\n", 161*5d8515caSChuck Lever __FUNCTION__, clp->cl_hostname); 1621da177e4SLinus Torvalds status = -EIO; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds } 165412c77ceSTrond Myklebust 166412c77ceSTrond Myklebust /* Ensure we revalidate the attributes and page cache! */ 167412c77ceSTrond Myklebust spin_lock(&inode->i_lock); 168412c77ceSTrond Myklebust nfsi->cache_validity |= NFS_INO_REVAL_FORCED; 169412c77ceSTrond Myklebust spin_unlock(&inode->i_lock); 170412c77ceSTrond Myklebust 1711da177e4SLinus Torvalds spin_unlock(&clp->cl_lock); 172603c83daSTrond Myklebust if (delegation != NULL) 173603c83daSTrond Myklebust nfs_free_delegation(delegation); 1741da177e4SLinus Torvalds return status; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds int res = 0; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid); 182905f8d16STrond Myklebust nfs_free_delegation(delegation); 1831da177e4SLinus Torvalds return res; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* Sync all data to disk upon delegation return */ 1871da177e4SLinus Torvalds static void nfs_msync_inode(struct inode *inode) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds filemap_fdatawrite(inode->i_mapping); 1901da177e4SLinus Torvalds nfs_wb_all(inode); 1911da177e4SLinus Torvalds filemap_fdatawait(inode->i_mapping); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* 1951da177e4SLinus Torvalds * Basic procedure for returning a delegation to the server 1961da177e4SLinus Torvalds */ 19790163027STrond Myklebust static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation) 1981da177e4SLinus Torvalds { 1997539bbabSDavid Howells struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 2001da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds nfs_msync_inode(inode); 2031da177e4SLinus Torvalds down_read(&clp->cl_sem); 2041da177e4SLinus Torvalds /* Guard against new delegated open calls */ 2051da177e4SLinus Torvalds down_write(&nfsi->rwsem); 20690163027STrond Myklebust nfs_delegation_claim_opens(inode, &delegation->stateid); 2071da177e4SLinus Torvalds up_write(&nfsi->rwsem); 2081da177e4SLinus Torvalds up_read(&clp->cl_sem); 2091da177e4SLinus Torvalds nfs_msync_inode(inode); 2101da177e4SLinus Torvalds 21190163027STrond Myklebust return nfs_do_return_delegation(inode, delegation); 21290163027STrond Myklebust } 21390163027STrond Myklebust 21490163027STrond Myklebust static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid) 21590163027STrond Myklebust { 2168383e460STrond Myklebust struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); 21790163027STrond Myklebust 21890163027STrond Myklebust if (delegation == NULL) 21990163027STrond Myklebust goto nomatch; 22090163027STrond Myklebust if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data, 22190163027STrond Myklebust sizeof(delegation->stateid.data)) != 0) 22290163027STrond Myklebust goto nomatch; 2238383e460STrond Myklebust list_del_rcu(&delegation->super_list); 22490163027STrond Myklebust nfsi->delegation_state = 0; 2258383e460STrond Myklebust rcu_assign_pointer(nfsi->delegation, NULL); 22690163027STrond Myklebust return delegation; 22790163027STrond Myklebust nomatch: 22890163027STrond Myklebust return NULL; 22990163027STrond Myklebust } 23090163027STrond Myklebust 23190163027STrond Myklebust int nfs_inode_return_delegation(struct inode *inode) 23290163027STrond Myklebust { 23390163027STrond Myklebust struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 23490163027STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 23590163027STrond Myklebust struct nfs_delegation *delegation; 23690163027STrond Myklebust int err = 0; 23790163027STrond Myklebust 2388383e460STrond Myklebust if (rcu_dereference(nfsi->delegation) != NULL) { 23990163027STrond Myklebust spin_lock(&clp->cl_lock); 24090163027STrond Myklebust delegation = nfs_detach_delegation_locked(nfsi, NULL); 24190163027STrond Myklebust spin_unlock(&clp->cl_lock); 2421da177e4SLinus Torvalds if (delegation != NULL) 24390163027STrond Myklebust err = __nfs_inode_return_delegation(inode, delegation); 24490163027STrond Myklebust } 24590163027STrond Myklebust return err; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* 2491da177e4SLinus Torvalds * Return all delegations associated to a super block 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds void nfs_return_all_delegations(struct super_block *sb) 2521da177e4SLinus Torvalds { 2537539bbabSDavid Howells struct nfs_client *clp = NFS_SB(sb)->nfs_client; 2541da177e4SLinus Torvalds struct nfs_delegation *delegation; 2551da177e4SLinus Torvalds struct inode *inode; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (clp == NULL) 2581da177e4SLinus Torvalds return; 2591da177e4SLinus Torvalds restart: 2608383e460STrond Myklebust rcu_read_lock(); 2618383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { 2621da177e4SLinus Torvalds if (delegation->inode->i_sb != sb) 2631da177e4SLinus Torvalds continue; 2641da177e4SLinus Torvalds inode = igrab(delegation->inode); 2651da177e4SLinus Torvalds if (inode == NULL) 2661da177e4SLinus Torvalds continue; 2678383e460STrond Myklebust spin_lock(&clp->cl_lock); 2688383e460STrond Myklebust delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); 2691da177e4SLinus Torvalds spin_unlock(&clp->cl_lock); 2708383e460STrond Myklebust rcu_read_unlock(); 2718383e460STrond Myklebust if (delegation != NULL) 27290163027STrond Myklebust __nfs_inode_return_delegation(inode, delegation); 2731da177e4SLinus Torvalds iput(inode); 2741da177e4SLinus Torvalds goto restart; 2751da177e4SLinus Torvalds } 2768383e460STrond Myklebust rcu_read_unlock(); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 27910afec90STrond Myklebust static int nfs_do_expire_all_delegations(void *ptr) 28058d9714aSTrond Myklebust { 281adfa6f98SDavid Howells struct nfs_client *clp = ptr; 28258d9714aSTrond Myklebust struct nfs_delegation *delegation; 28358d9714aSTrond Myklebust struct inode *inode; 28458d9714aSTrond Myklebust 28558d9714aSTrond Myklebust allow_signal(SIGKILL); 28658d9714aSTrond Myklebust restart: 28758d9714aSTrond Myklebust if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0) 28858d9714aSTrond Myklebust goto out; 28958d9714aSTrond Myklebust if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) 29058d9714aSTrond Myklebust goto out; 2918383e460STrond Myklebust rcu_read_lock(); 2928383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { 29358d9714aSTrond Myklebust inode = igrab(delegation->inode); 29458d9714aSTrond Myklebust if (inode == NULL) 29558d9714aSTrond Myklebust continue; 2968383e460STrond Myklebust spin_lock(&clp->cl_lock); 2978383e460STrond Myklebust delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); 29858d9714aSTrond Myklebust spin_unlock(&clp->cl_lock); 2998383e460STrond Myklebust rcu_read_unlock(); 3008383e460STrond Myklebust if (delegation) 30190163027STrond Myklebust __nfs_inode_return_delegation(inode, delegation); 30258d9714aSTrond Myklebust iput(inode); 30358d9714aSTrond Myklebust goto restart; 30458d9714aSTrond Myklebust } 3058383e460STrond Myklebust rcu_read_unlock(); 30658d9714aSTrond Myklebust out: 30724c8dbbbSDavid Howells nfs_put_client(clp); 30858d9714aSTrond Myklebust module_put_and_exit(0); 30958d9714aSTrond Myklebust } 31058d9714aSTrond Myklebust 311adfa6f98SDavid Howells void nfs_expire_all_delegations(struct nfs_client *clp) 31258d9714aSTrond Myklebust { 31358d9714aSTrond Myklebust struct task_struct *task; 31458d9714aSTrond Myklebust 31558d9714aSTrond Myklebust __module_get(THIS_MODULE); 31658d9714aSTrond Myklebust atomic_inc(&clp->cl_count); 31758d9714aSTrond Myklebust task = kthread_run(nfs_do_expire_all_delegations, clp, 318*5d8515caSChuck Lever "%s-delegreturn", 319*5d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, 320*5d8515caSChuck Lever RPC_DISPLAY_ADDR)); 32158d9714aSTrond Myklebust if (!IS_ERR(task)) 32258d9714aSTrond Myklebust return; 32324c8dbbbSDavid Howells nfs_put_client(clp); 32458d9714aSTrond Myklebust module_put(THIS_MODULE); 32558d9714aSTrond Myklebust } 32658d9714aSTrond Myklebust 3271da177e4SLinus Torvalds /* 3281da177e4SLinus Torvalds * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. 3291da177e4SLinus Torvalds */ 330adfa6f98SDavid Howells void nfs_handle_cb_pathdown(struct nfs_client *clp) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct nfs_delegation *delegation; 3331da177e4SLinus Torvalds struct inode *inode; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds if (clp == NULL) 3361da177e4SLinus Torvalds return; 3371da177e4SLinus Torvalds restart: 3388383e460STrond Myklebust rcu_read_lock(); 3398383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { 3401da177e4SLinus Torvalds inode = igrab(delegation->inode); 3411da177e4SLinus Torvalds if (inode == NULL) 3421da177e4SLinus Torvalds continue; 3438383e460STrond Myklebust spin_lock(&clp->cl_lock); 3448383e460STrond Myklebust delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); 3451da177e4SLinus Torvalds spin_unlock(&clp->cl_lock); 3468383e460STrond Myklebust rcu_read_unlock(); 3478383e460STrond Myklebust if (delegation != NULL) 34890163027STrond Myklebust __nfs_inode_return_delegation(inode, delegation); 3491da177e4SLinus Torvalds iput(inode); 3501da177e4SLinus Torvalds goto restart; 3511da177e4SLinus Torvalds } 3528383e460STrond Myklebust rcu_read_unlock(); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds struct recall_threadargs { 3561da177e4SLinus Torvalds struct inode *inode; 357adfa6f98SDavid Howells struct nfs_client *clp; 3581da177e4SLinus Torvalds const nfs4_stateid *stateid; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds struct completion started; 3611da177e4SLinus Torvalds int result; 3621da177e4SLinus Torvalds }; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds static int recall_thread(void *data) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds struct recall_threadargs *args = (struct recall_threadargs *)data; 3671da177e4SLinus Torvalds struct inode *inode = igrab(args->inode); 3687539bbabSDavid Howells struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 3691da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3701da177e4SLinus Torvalds struct nfs_delegation *delegation; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds daemonize("nfsv4-delegreturn"); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds nfs_msync_inode(inode); 3751da177e4SLinus Torvalds down_read(&clp->cl_sem); 3761da177e4SLinus Torvalds down_write(&nfsi->rwsem); 3771da177e4SLinus Torvalds spin_lock(&clp->cl_lock); 37890163027STrond Myklebust delegation = nfs_detach_delegation_locked(nfsi, args->stateid); 37990163027STrond Myklebust if (delegation != NULL) 3801da177e4SLinus Torvalds args->result = 0; 38190163027STrond Myklebust else 3821da177e4SLinus Torvalds args->result = -ENOENT; 3831da177e4SLinus Torvalds spin_unlock(&clp->cl_lock); 3841da177e4SLinus Torvalds complete(&args->started); 38590163027STrond Myklebust nfs_delegation_claim_opens(inode, args->stateid); 3861da177e4SLinus Torvalds up_write(&nfsi->rwsem); 3871da177e4SLinus Torvalds up_read(&clp->cl_sem); 3881da177e4SLinus Torvalds nfs_msync_inode(inode); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds if (delegation != NULL) 3911da177e4SLinus Torvalds nfs_do_return_delegation(inode, delegation); 3921da177e4SLinus Torvalds iput(inode); 3931da177e4SLinus Torvalds module_put_and_exit(0); 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 3971da177e4SLinus Torvalds * Asynchronous delegation recall! 3981da177e4SLinus Torvalds */ 3991da177e4SLinus Torvalds int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds struct recall_threadargs data = { 4021da177e4SLinus Torvalds .inode = inode, 4031da177e4SLinus Torvalds .stateid = stateid, 4041da177e4SLinus Torvalds }; 4051da177e4SLinus Torvalds int status; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds init_completion(&data.started); 4081da177e4SLinus Torvalds __module_get(THIS_MODULE); 4091da177e4SLinus Torvalds status = kernel_thread(recall_thread, &data, CLONE_KERNEL); 4101da177e4SLinus Torvalds if (status < 0) 4111da177e4SLinus Torvalds goto out_module_put; 4121da177e4SLinus Torvalds wait_for_completion(&data.started); 4131da177e4SLinus Torvalds return data.result; 4141da177e4SLinus Torvalds out_module_put: 4151da177e4SLinus Torvalds module_put(THIS_MODULE); 4161da177e4SLinus Torvalds return status; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* 4201da177e4SLinus Torvalds * Retrieve the inode associated with a delegation 4211da177e4SLinus Torvalds */ 422adfa6f98SDavid Howells struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds struct nfs_delegation *delegation; 4251da177e4SLinus Torvalds struct inode *res = NULL; 4268383e460STrond Myklebust rcu_read_lock(); 4278383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { 4281da177e4SLinus Torvalds if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { 4291da177e4SLinus Torvalds res = igrab(delegation->inode); 4301da177e4SLinus Torvalds break; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 4338383e460STrond Myklebust rcu_read_unlock(); 4341da177e4SLinus Torvalds return res; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * Mark all delegations as needing to be reclaimed 4391da177e4SLinus Torvalds */ 440adfa6f98SDavid Howells void nfs_delegation_mark_reclaim(struct nfs_client *clp) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds struct nfs_delegation *delegation; 4438383e460STrond Myklebust rcu_read_lock(); 4448383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) 4451da177e4SLinus Torvalds delegation->flags |= NFS_DELEGATION_NEED_RECLAIM; 4468383e460STrond Myklebust rcu_read_unlock(); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * Reap all unclaimed delegations after reboot recovery is done 4511da177e4SLinus Torvalds */ 452adfa6f98SDavid Howells void nfs_delegation_reap_unclaimed(struct nfs_client *clp) 4531da177e4SLinus Torvalds { 4548383e460STrond Myklebust struct nfs_delegation *delegation; 4558383e460STrond Myklebust restart: 4568383e460STrond Myklebust rcu_read_lock(); 4578383e460STrond Myklebust list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { 4581da177e4SLinus Torvalds if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) 4591da177e4SLinus Torvalds continue; 4608383e460STrond Myklebust spin_lock(&clp->cl_lock); 4618383e460STrond Myklebust delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL); 4621da177e4SLinus Torvalds spin_unlock(&clp->cl_lock); 4638383e460STrond Myklebust rcu_read_unlock(); 4648383e460STrond Myklebust if (delegation != NULL) 465905f8d16STrond Myklebust nfs_free_delegation(delegation); 4668383e460STrond Myklebust goto restart; 4671da177e4SLinus Torvalds } 4688383e460STrond Myklebust rcu_read_unlock(); 4691da177e4SLinus Torvalds } 4703e4f6290STrond Myklebust 4713e4f6290STrond Myklebust int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) 4723e4f6290STrond Myklebust { 4733e4f6290STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 4743e4f6290STrond Myklebust struct nfs_delegation *delegation; 4758383e460STrond Myklebust int ret = 0; 4763e4f6290STrond Myklebust 4778383e460STrond Myklebust rcu_read_lock(); 4788383e460STrond Myklebust delegation = rcu_dereference(nfsi->delegation); 4793e4f6290STrond Myklebust if (delegation != NULL) { 4803e4f6290STrond Myklebust memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); 4818383e460STrond Myklebust ret = 1; 4823e4f6290STrond Myklebust } 4838383e460STrond Myklebust rcu_read_unlock(); 4848383e460STrond Myklebust return ret; 4853e4f6290STrond Myklebust } 486