| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/fs/nfs/delegation.c | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2004 Trond Myklebust | 
 | 5 |  * | 
 | 6 |  * NFS file delegation management | 
 | 7 |  * | 
 | 8 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/completion.h> | 
| Trond Myklebust | 58d9714a | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 10 | #include <linux/kthread.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> | 
 | 12 | #include <linux/sched.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/spinlock.h> | 
 | 15 |  | 
 | 16 | #include <linux/nfs4.h> | 
 | 17 | #include <linux/nfs_fs.h> | 
 | 18 | #include <linux/nfs_xdr.h> | 
 | 19 |  | 
| Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 20 | #include "nfs4_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "delegation.h" | 
| David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 22 | #include "internal.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 |  | 
| Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 24 | static void nfs_do_free_delegation(struct nfs_delegation *delegation) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | 	kfree(delegation); | 
 | 27 | } | 
 | 28 |  | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 29 | static void nfs_free_delegation_callback(struct rcu_head *head) | 
 | 30 | { | 
 | 31 | 	struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu); | 
 | 32 |  | 
| Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 33 | 	nfs_do_free_delegation(delegation); | 
 | 34 | } | 
 | 35 |  | 
 | 36 | static void nfs_free_delegation(struct nfs_delegation *delegation) | 
 | 37 | { | 
| Trond Myklebust | e00b8a2 | 2011-01-27 14:55:39 -0500 | [diff] [blame] | 38 | 	if (delegation->cred) { | 
 | 39 | 		put_rpccred(delegation->cred); | 
 | 40 | 		delegation->cred = NULL; | 
 | 41 | 	} | 
| Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 42 | 	call_rcu(&delegation->rcu, nfs_free_delegation_callback); | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 43 | } | 
 | 44 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 45 | /** | 
 | 46 |  * nfs_mark_delegation_referenced - set delegation's REFERENCED flag | 
 | 47 |  * @delegation: delegation to process | 
 | 48 |  * | 
 | 49 |  */ | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 50 | void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) | 
 | 51 | { | 
 | 52 | 	set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); | 
 | 53 | } | 
 | 54 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 55 | /** | 
 | 56 |  * nfs_have_delegation - check if inode has a delegation | 
 | 57 |  * @inode: inode to check | 
 | 58 |  * @flags: delegation types to check for | 
 | 59 |  * | 
 | 60 |  * Returns one if inode has the indicated delegation, otherwise zero. | 
 | 61 |  */ | 
| Trond Myklebust | bd7bf9d | 2008-12-23 15:21:53 -0500 | [diff] [blame] | 62 | int nfs_have_delegation(struct inode *inode, fmode_t flags) | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 63 | { | 
 | 64 | 	struct nfs_delegation *delegation; | 
 | 65 | 	int ret = 0; | 
 | 66 |  | 
 | 67 | 	flags &= FMODE_READ|FMODE_WRITE; | 
 | 68 | 	rcu_read_lock(); | 
 | 69 | 	delegation = rcu_dereference(NFS_I(inode)->delegation); | 
 | 70 | 	if (delegation != NULL && (delegation->type & flags) == flags) { | 
 | 71 | 		nfs_mark_delegation_referenced(delegation); | 
 | 72 | 		ret = 1; | 
 | 73 | 	} | 
 | 74 | 	rcu_read_unlock(); | 
 | 75 | 	return ret; | 
 | 76 | } | 
 | 77 |  | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 78 | static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state) | 
 | 79 | { | 
 | 80 | 	struct inode *inode = state->inode; | 
 | 81 | 	struct file_lock *fl; | 
| Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 82 | 	int status = 0; | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 83 |  | 
| Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 84 | 	if (inode->i_flock == NULL) | 
 | 85 | 		goto out; | 
 | 86 |  | 
| Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 87 | 	/* Protect inode->i_flock using the file locks lock */ | 
 | 88 | 	lock_flocks(); | 
| Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 89 | 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 90 | 		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) | 
 | 91 | 			continue; | 
| Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 92 | 		if (nfs_file_open_context(fl->fl_file) != ctx) | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 93 | 			continue; | 
| Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 94 | 		unlock_flocks(); | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 95 | 		status = nfs4_lock_delegation_recall(state, fl); | 
| Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 96 | 		if (status < 0) | 
| Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 97 | 			goto out; | 
| Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 98 | 		lock_flocks(); | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 99 | 	} | 
| Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 100 | 	unlock_flocks(); | 
| Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 101 | out: | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 102 | 	return status; | 
 | 103 | } | 
 | 104 |  | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 105 | static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { | 
 | 107 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 108 | 	struct nfs_open_context *ctx; | 
 | 109 | 	struct nfs4_state *state; | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 110 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
 | 112 | again: | 
 | 113 | 	spin_lock(&inode->i_lock); | 
 | 114 | 	list_for_each_entry(ctx, &nfsi->open_files, list) { | 
 | 115 | 		state = ctx->state; | 
 | 116 | 		if (state == NULL) | 
 | 117 | 			continue; | 
 | 118 | 		if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) | 
 | 119 | 			continue; | 
| Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 120 | 		if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) | 
 | 121 | 			continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 		get_nfs_open_context(ctx); | 
 | 123 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 124 | 		err = nfs4_open_delegation_recall(ctx, state, stateid); | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 125 | 		if (err >= 0) | 
 | 126 | 			err = nfs_delegation_claim_locks(ctx, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 		put_nfs_open_context(ctx); | 
| Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 128 | 		if (err != 0) | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 129 | 			return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | 		goto again; | 
 | 131 | 	} | 
 | 132 | 	spin_unlock(&inode->i_lock); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 133 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } | 
 | 135 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 136 | /** | 
 | 137 |  * nfs_inode_reclaim_delegation - process a delegation reclaim request | 
 | 138 |  * @inode: inode to process | 
 | 139 |  * @cred: credential to use for request | 
 | 140 |  * @res: new delegation state from server | 
 | 141 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 |  */ | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 143 | void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, | 
 | 144 | 				  struct nfs_openres *res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { | 
| Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 146 | 	struct nfs_delegation *delegation; | 
 | 147 | 	struct rpc_cred *oldcred = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 |  | 
| Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 149 | 	rcu_read_lock(); | 
 | 150 | 	delegation = rcu_dereference(NFS_I(inode)->delegation); | 
 | 151 | 	if (delegation != NULL) { | 
 | 152 | 		spin_lock(&delegation->lock); | 
 | 153 | 		if (delegation->inode != NULL) { | 
 | 154 | 			memcpy(delegation->stateid.data, res->delegation.data, | 
 | 155 | 			       sizeof(delegation->stateid.data)); | 
 | 156 | 			delegation->type = res->delegation_type; | 
 | 157 | 			delegation->maxsize = res->maxsize; | 
 | 158 | 			oldcred = delegation->cred; | 
 | 159 | 			delegation->cred = get_rpccred(cred); | 
 | 160 | 			clear_bit(NFS_DELEGATION_NEED_RECLAIM, | 
 | 161 | 				  &delegation->flags); | 
 | 162 | 			NFS_I(inode)->delegation_state = delegation->type; | 
 | 163 | 			spin_unlock(&delegation->lock); | 
 | 164 | 			put_rpccred(oldcred); | 
 | 165 | 			rcu_read_unlock(); | 
 | 166 | 		} else { | 
 | 167 | 			/* We appear to have raced with a delegation return. */ | 
 | 168 | 			spin_unlock(&delegation->lock); | 
 | 169 | 			rcu_read_unlock(); | 
 | 170 | 			nfs_inode_set_delegation(inode, cred, res); | 
 | 171 | 		} | 
 | 172 | 	} else { | 
 | 173 | 		rcu_read_unlock(); | 
 | 174 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } | 
 | 176 |  | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 177 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) | 
 | 178 | { | 
 | 179 | 	int res = 0; | 
 | 180 |  | 
 | 181 | 	res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync); | 
 | 182 | 	nfs_free_delegation(delegation); | 
 | 183 | 	return res; | 
 | 184 | } | 
 | 185 |  | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 186 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) | 
 | 187 | { | 
 | 188 | 	struct inode *inode = NULL; | 
 | 189 |  | 
 | 190 | 	spin_lock(&delegation->lock); | 
 | 191 | 	if (delegation->inode != NULL) | 
 | 192 | 		inode = igrab(delegation->inode); | 
 | 193 | 	spin_unlock(&delegation->lock); | 
 | 194 | 	return inode; | 
 | 195 | } | 
 | 196 |  | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 197 | static struct nfs_delegation * | 
 | 198 | nfs_detach_delegation_locked(struct nfs_inode *nfsi, | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 199 | 			     struct nfs_server *server) | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 200 | { | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 201 | 	struct nfs_delegation *delegation = | 
 | 202 | 		rcu_dereference_protected(nfsi->delegation, | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 203 | 				lockdep_is_held(&server->nfs_client->cl_lock)); | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 204 |  | 
 | 205 | 	if (delegation == NULL) | 
 | 206 | 		goto nomatch; | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 207 |  | 
| Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 208 | 	spin_lock(&delegation->lock); | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 209 | 	list_del_rcu(&delegation->super_list); | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 210 | 	delegation->inode = NULL; | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 211 | 	nfsi->delegation_state = 0; | 
 | 212 | 	rcu_assign_pointer(nfsi->delegation, NULL); | 
| Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 213 | 	spin_unlock(&delegation->lock); | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 214 | 	return delegation; | 
 | 215 | nomatch: | 
 | 216 | 	return NULL; | 
 | 217 | } | 
 | 218 |  | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 219 | static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 220 | 						    struct nfs_server *server) | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 221 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 222 | 	struct nfs_client *clp = server->nfs_client; | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 223 | 	struct nfs_delegation *delegation; | 
 | 224 |  | 
 | 225 | 	spin_lock(&clp->cl_lock); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 226 | 	delegation = nfs_detach_delegation_locked(nfsi, server); | 
| Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 227 | 	spin_unlock(&clp->cl_lock); | 
 | 228 | 	return delegation; | 
 | 229 | } | 
 | 230 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 231 | /** | 
 | 232 |  * nfs_inode_set_delegation - set up a delegation on an inode | 
 | 233 |  * @inode: inode to which delegation applies | 
 | 234 |  * @cred: cred to use for subsequent delegation processing | 
 | 235 |  * @res: new delegation state from server | 
 | 236 |  * | 
 | 237 |  * Returns zero on success, or a negative errno value. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 |  */ | 
 | 239 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) | 
 | 240 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 241 | 	struct nfs_server *server = NFS_SERVER(inode); | 
 | 242 | 	struct nfs_client *clp = server->nfs_client; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 244 | 	struct nfs_delegation *delegation, *old_delegation; | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 245 | 	struct nfs_delegation *freeme = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | 	int status = 0; | 
 | 247 |  | 
| Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 248 | 	delegation = kmalloc(sizeof(*delegation), GFP_NOFS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | 	if (delegation == NULL) | 
 | 250 | 		return -ENOMEM; | 
 | 251 | 	memcpy(delegation->stateid.data, res->delegation.data, | 
 | 252 | 			sizeof(delegation->stateid.data)); | 
 | 253 | 	delegation->type = res->delegation_type; | 
 | 254 | 	delegation->maxsize = res->maxsize; | 
| Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 255 | 	delegation->change_attr = nfsi->change_attr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | 	delegation->cred = get_rpccred(cred); | 
 | 257 | 	delegation->inode = inode; | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 258 | 	delegation->flags = 1<<NFS_DELEGATION_REFERENCED; | 
| Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 259 | 	spin_lock_init(&delegation->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 |  | 
 | 261 | 	spin_lock(&clp->cl_lock); | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 262 | 	old_delegation = rcu_dereference_protected(nfsi->delegation, | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 263 | 					lockdep_is_held(&clp->cl_lock)); | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 264 | 	if (old_delegation != NULL) { | 
 | 265 | 		if (memcmp(&delegation->stateid, &old_delegation->stateid, | 
 | 266 | 					sizeof(old_delegation->stateid)) == 0 && | 
 | 267 | 				delegation->type == old_delegation->type) { | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 268 | 			goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | 		} | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 270 | 		/* | 
 | 271 | 		 * Deal with broken servers that hand out two | 
 | 272 | 		 * delegations for the same file. | 
 | 273 | 		 */ | 
 | 274 | 		dfprintk(FILE, "%s: server %s handed out " | 
 | 275 | 				"a duplicate delegation!\n", | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 276 | 				__func__, clp->cl_hostname); | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 277 | 		if (delegation->type <= old_delegation->type) { | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 278 | 			freeme = delegation; | 
 | 279 | 			delegation = NULL; | 
 | 280 | 			goto out; | 
 | 281 | 		} | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 282 | 		freeme = nfs_detach_delegation_locked(nfsi, server); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 	} | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 284 | 	list_add_rcu(&delegation->super_list, &server->delegations); | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 285 | 	nfsi->delegation_state = delegation->type; | 
 | 286 | 	rcu_assign_pointer(nfsi->delegation, delegation); | 
 | 287 | 	delegation = NULL; | 
| Trond Myklebust | 412c77c | 2007-07-03 16:10:55 -0400 | [diff] [blame] | 288 |  | 
 | 289 | 	/* Ensure we revalidate the attributes and page cache! */ | 
 | 290 | 	spin_lock(&inode->i_lock); | 
 | 291 | 	nfsi->cache_validity |= NFS_INO_REVAL_FORCED; | 
 | 292 | 	spin_unlock(&inode->i_lock); | 
 | 293 |  | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 294 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 	spin_unlock(&clp->cl_lock); | 
| Trond Myklebust | 603c83d | 2007-10-18 19:59:20 -0400 | [diff] [blame] | 296 | 	if (delegation != NULL) | 
 | 297 | 		nfs_free_delegation(delegation); | 
| Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 298 | 	if (freeme != NULL) | 
 | 299 | 		nfs_do_return_delegation(inode, freeme, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | 	return status; | 
 | 301 | } | 
 | 302 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* | 
 | 304 |  * Basic procedure for returning a delegation to the server | 
 | 305 |  */ | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 306 | static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 309 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 |  | 
| Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 311 | 	/* | 
 | 312 | 	 * Guard against new delegated open/lock/unlock calls and against | 
 | 313 | 	 * state recovery | 
 | 314 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | 	down_write(&nfsi->rwsem); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 316 | 	err = nfs_delegation_claim_opens(inode, &delegation->stateid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | 	up_write(&nfsi->rwsem); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 318 | 	if (err) | 
 | 319 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 |  | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 321 | 	err = nfs_do_return_delegation(inode, delegation, issync); | 
 | 322 | out: | 
 | 323 | 	return err; | 
| Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 324 | } | 
 | 325 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 326 | /** | 
 | 327 |  * nfs_client_return_marked_delegations - return previously marked delegations | 
 | 328 |  * @clp: nfs_client to process | 
 | 329 |  * | 
 | 330 |  * Returns zero on success, or a negative errno value. | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 331 |  */ | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 332 | int nfs_client_return_marked_delegations(struct nfs_client *clp) | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 333 | { | 
 | 334 | 	struct nfs_delegation *delegation; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 335 | 	struct nfs_server *server; | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 336 | 	struct inode *inode; | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 337 | 	int err = 0; | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 338 |  | 
 | 339 | restart: | 
 | 340 | 	rcu_read_lock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 341 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | 
 | 342 | 		list_for_each_entry_rcu(delegation, &server->delegations, | 
 | 343 | 								super_list) { | 
 | 344 | 			if (!test_and_clear_bit(NFS_DELEGATION_RETURN, | 
 | 345 | 							&delegation->flags)) | 
 | 346 | 				continue; | 
 | 347 | 			inode = nfs_delegation_grab_inode(delegation); | 
 | 348 | 			if (inode == NULL) | 
 | 349 | 				continue; | 
 | 350 | 			delegation = nfs_detach_delegation(NFS_I(inode), | 
 | 351 | 								server); | 
 | 352 | 			rcu_read_unlock(); | 
 | 353 |  | 
 | 354 | 			if (delegation != NULL) { | 
 | 355 | 				filemap_flush(inode->i_mapping); | 
 | 356 | 				err = __nfs_inode_return_delegation(inode, | 
 | 357 | 								delegation, 0); | 
 | 358 | 			} | 
 | 359 | 			iput(inode); | 
 | 360 | 			if (!err) | 
 | 361 | 				goto restart; | 
 | 362 | 			set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); | 
 | 363 | 			return err; | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 364 | 		} | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 365 | 	} | 
 | 366 | 	rcu_read_unlock(); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 367 | 	return 0; | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 368 | } | 
 | 369 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 370 | /** | 
 | 371 |  * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens | 
 | 372 |  * @inode: inode to process | 
 | 373 |  * | 
 | 374 |  * Does not protect against delegation reclaims, therefore really only safe | 
 | 375 |  * to be called from nfs4_clear_inode(). | 
| Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 376 |  */ | 
 | 377 | void nfs_inode_return_delegation_noreclaim(struct inode *inode) | 
 | 378 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 379 | 	struct nfs_server *server = NFS_SERVER(inode); | 
| Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 380 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 381 | 	struct nfs_delegation *delegation; | 
 | 382 |  | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 383 | 	if (rcu_access_pointer(nfsi->delegation) != NULL) { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 384 | 		delegation = nfs_detach_delegation(nfsi, server); | 
| Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 385 | 		if (delegation != NULL) | 
 | 386 | 			nfs_do_return_delegation(inode, delegation, 0); | 
 | 387 | 	} | 
 | 388 | } | 
 | 389 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 390 | /** | 
 | 391 |  * nfs_inode_return_delegation - synchronously return a delegation | 
 | 392 |  * @inode: inode to process | 
 | 393 |  * | 
 | 394 |  * Returns zero on success, or a negative errno value. | 
 | 395 |  */ | 
| Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 396 | int nfs_inode_return_delegation(struct inode *inode) | 
 | 397 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 398 | 	struct nfs_server *server = NFS_SERVER(inode); | 
| Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 399 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 400 | 	struct nfs_delegation *delegation; | 
 | 401 | 	int err = 0; | 
 | 402 |  | 
| David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 403 | 	if (rcu_access_pointer(nfsi->delegation) != NULL) { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 404 | 		delegation = nfs_detach_delegation(nfsi, server); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 405 | 		if (delegation != NULL) { | 
| Trond Myklebust | 1b924e5 | 2010-07-31 14:29:06 -0400 | [diff] [blame] | 406 | 			nfs_wb_all(inode); | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 407 | 			err = __nfs_inode_return_delegation(inode, delegation, 1); | 
 | 408 | 		} | 
| Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 409 | 	} | 
 | 410 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } | 
 | 412 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 413 | static void nfs_mark_return_delegation(struct nfs_delegation *delegation) | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 414 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 415 | 	struct nfs_client *clp = NFS_SERVER(delegation->inode)->nfs_client; | 
 | 416 |  | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 417 | 	set_bit(NFS_DELEGATION_RETURN, &delegation->flags); | 
 | 418 | 	set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); | 
 | 419 | } | 
 | 420 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 421 | /** | 
 | 422 |  * nfs_super_return_all_delegations - return delegations for one superblock | 
 | 423 |  * @sb: sb to process | 
 | 424 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 |  */ | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 426 | void nfs_super_return_all_delegations(struct super_block *sb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 428 | 	struct nfs_server *server = NFS_SB(sb); | 
 | 429 | 	struct nfs_client *clp = server->nfs_client; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | 	struct nfs_delegation *delegation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 |  | 
 | 432 | 	if (clp == NULL) | 
 | 433 | 		return; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 434 |  | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 435 | 	rcu_read_lock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 436 | 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 437 | 		spin_lock(&delegation->lock); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 438 | 		set_bit(NFS_DELEGATION_RETURN, &delegation->flags); | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 439 | 		spin_unlock(&delegation->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | 	} | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 441 | 	rcu_read_unlock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 442 |  | 
| Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 443 | 	if (nfs_client_return_marked_delegations(clp) != 0) | 
 | 444 | 		nfs4_schedule_state_manager(clp); | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 445 | } | 
 | 446 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 447 | static void nfs_mark_return_all_delegation_types(struct nfs_server *server, | 
 | 448 | 						 fmode_t flags) | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 449 | { | 
 | 450 | 	struct nfs_delegation *delegation; | 
 | 451 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 452 | 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | 
| Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 453 | 		if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) | 
 | 454 | 			continue; | 
 | 455 | 		if (delegation->type & flags) | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 456 | 			nfs_mark_return_delegation(delegation); | 
| Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 457 | 	} | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 458 | } | 
 | 459 |  | 
 | 460 | static void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, | 
 | 461 | 							fmode_t flags) | 
 | 462 | { | 
 | 463 | 	struct nfs_server *server; | 
 | 464 |  | 
 | 465 | 	rcu_read_lock(); | 
 | 466 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | 
 | 467 | 		nfs_mark_return_all_delegation_types(server, flags); | 
| Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 468 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } | 
 | 470 |  | 
| Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 471 | static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) | 
 | 472 | { | 
 | 473 | 	nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); | 
 | 474 | } | 
 | 475 |  | 
| Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 476 | static void nfs_delegation_run_state_manager(struct nfs_client *clp) | 
| Trond Myklebust | 58d9714a | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 477 | { | 
| Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 478 | 	if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) | 
 | 479 | 		nfs4_schedule_state_manager(clp); | 
| Trond Myklebust | 58d9714a | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 480 | } | 
 | 481 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 482 | /** | 
 | 483 |  * nfs_expire_all_delegation_types | 
 | 484 |  * @clp: client to process | 
 | 485 |  * @flags: delegation types to expire | 
 | 486 |  * | 
 | 487 |  */ | 
| Alexandros Batsakis | 31f0960 | 2009-12-05 13:27:02 -0500 | [diff] [blame] | 488 | void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags) | 
| Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 489 | { | 
 | 490 | 	nfs_client_mark_return_all_delegation_types(clp, flags); | 
 | 491 | 	nfs_delegation_run_state_manager(clp); | 
 | 492 | } | 
 | 493 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 494 | /** | 
 | 495 |  * nfs_expire_all_delegations | 
 | 496 |  * @clp: client to process | 
 | 497 |  * | 
 | 498 |  */ | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 499 | void nfs_expire_all_delegations(struct nfs_client *clp) | 
| Trond Myklebust | 58d9714a | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 500 | { | 
| Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 501 | 	nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); | 
| Trond Myklebust | 58d9714a | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 502 | } | 
 | 503 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 504 | /** | 
 | 505 |  * nfs_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN | 
 | 506 |  * @clp: client to process | 
 | 507 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 |  */ | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 509 | void nfs_handle_cb_pathdown(struct nfs_client *clp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 	if (clp == NULL) | 
 | 512 | 		return; | 
| Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 513 | 	nfs_client_mark_return_all_delegations(clp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } | 
 | 515 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 516 | static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 517 | { | 
 | 518 | 	struct nfs_delegation *delegation; | 
 | 519 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 520 | 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 521 | 		if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) | 
 | 522 | 			continue; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 523 | 		nfs_mark_return_delegation(delegation); | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 524 | 	} | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 525 | } | 
 | 526 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 527 | /** | 
 | 528 |  * nfs_expire_unreferenced_delegations - Eliminate unused delegations | 
 | 529 |  * @clp: nfs_client to process | 
 | 530 |  * | 
 | 531 |  */ | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 532 | void nfs_expire_unreferenced_delegations(struct nfs_client *clp) | 
 | 533 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 534 | 	struct nfs_server *server; | 
 | 535 |  | 
 | 536 | 	rcu_read_lock(); | 
 | 537 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | 
 | 538 | 		nfs_mark_return_unreferenced_delegations(server); | 
 | 539 | 	rcu_read_unlock(); | 
 | 540 |  | 
| Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 541 | 	nfs_delegation_run_state_manager(clp); | 
 | 542 | } | 
 | 543 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 544 | /** | 
 | 545 |  * nfs_async_inode_return_delegation - asynchronously return a delegation | 
 | 546 |  * @inode: inode to process | 
 | 547 |  * @stateid: state ID information from CB_RECALL arguments | 
 | 548 |  * | 
 | 549 |  * Returns zero on success, or a negative errno value. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 |  */ | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 551 | int nfs_async_inode_return_delegation(struct inode *inode, | 
 | 552 | 				      const nfs4_stateid *stateid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 554 | 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | 
 | 555 | 	struct nfs_delegation *delegation; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 |  | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 557 | 	rcu_read_lock(); | 
 | 558 | 	delegation = rcu_dereference(NFS_I(inode)->delegation); | 
| Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 559 |  | 
| Trond Myklebust | e047a10 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 560 | 	if (!clp->cl_mvops->validate_stateid(delegation, stateid)) { | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 561 | 		rcu_read_unlock(); | 
 | 562 | 		return -ENOENT; | 
 | 563 | 	} | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 564 | 	nfs_mark_return_delegation(delegation); | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 565 | 	rcu_read_unlock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 566 |  | 
| Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 567 | 	nfs_delegation_run_state_manager(clp); | 
 | 568 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } | 
 | 570 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 571 | static struct inode * | 
 | 572 | nfs_delegation_find_inode_server(struct nfs_server *server, | 
 | 573 | 				 const struct nfs_fh *fhandle) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { | 
 | 575 | 	struct nfs_delegation *delegation; | 
 | 576 | 	struct inode *res = NULL; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 577 |  | 
 | 578 | 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 579 | 		spin_lock(&delegation->lock); | 
 | 580 | 		if (delegation->inode != NULL && | 
 | 581 | 		    nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | 			res = igrab(delegation->inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | 		} | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 584 | 		spin_unlock(&delegation->lock); | 
 | 585 | 		if (res != NULL) | 
 | 586 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | 	} | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 588 | 	return res; | 
 | 589 | } | 
 | 590 |  | 
 | 591 | /** | 
 | 592 |  * nfs_delegation_find_inode - retrieve the inode associated with a delegation | 
 | 593 |  * @clp: client state handle | 
 | 594 |  * @fhandle: filehandle from a delegation recall | 
 | 595 |  * | 
 | 596 |  * Returns pointer to inode matching "fhandle," or NULL if a matching inode | 
 | 597 |  * cannot be found. | 
 | 598 |  */ | 
 | 599 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, | 
 | 600 | 					const struct nfs_fh *fhandle) | 
 | 601 | { | 
 | 602 | 	struct nfs_server *server; | 
 | 603 | 	struct inode *res = NULL; | 
 | 604 |  | 
 | 605 | 	rcu_read_lock(); | 
 | 606 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | 
 | 607 | 		res = nfs_delegation_find_inode_server(server, fhandle); | 
 | 608 | 		if (res != NULL) | 
 | 609 | 			break; | 
 | 610 | 	} | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 611 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | 	return res; | 
 | 613 | } | 
 | 614 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 615 | static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) | 
 | 616 | { | 
 | 617 | 	struct nfs_delegation *delegation; | 
 | 618 |  | 
 | 619 | 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) | 
 | 620 | 		set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); | 
 | 621 | } | 
 | 622 |  | 
 | 623 | /** | 
 | 624 |  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed | 
 | 625 |  * @clp: nfs_client to process | 
 | 626 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 |  */ | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 628 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 630 | 	struct nfs_server *server; | 
 | 631 |  | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 632 | 	rcu_read_lock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 633 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | 
 | 634 | 		nfs_delegation_mark_reclaim_server(server); | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 635 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } | 
 | 637 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 638 | /** | 
 | 639 |  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done | 
 | 640 |  * @clp: nfs_client to process | 
 | 641 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 |  */ | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 643 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 645 | 	struct nfs_delegation *delegation; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 646 | 	struct nfs_server *server; | 
| Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 647 | 	struct inode *inode; | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 648 |  | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 649 | restart: | 
 | 650 | 	rcu_read_lock(); | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 651 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | 
 | 652 | 		list_for_each_entry_rcu(delegation, &server->delegations, | 
 | 653 | 								super_list) { | 
 | 654 | 			if (test_bit(NFS_DELEGATION_NEED_RECLAIM, | 
 | 655 | 						&delegation->flags) == 0) | 
 | 656 | 				continue; | 
 | 657 | 			inode = nfs_delegation_grab_inode(delegation); | 
 | 658 | 			if (inode == NULL) | 
 | 659 | 				continue; | 
 | 660 | 			delegation = nfs_detach_delegation(NFS_I(inode), | 
 | 661 | 								server); | 
 | 662 | 			rcu_read_unlock(); | 
 | 663 |  | 
 | 664 | 			if (delegation != NULL) | 
 | 665 | 				nfs_free_delegation(delegation); | 
 | 666 | 			iput(inode); | 
 | 667 | 			goto restart; | 
 | 668 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | 	} | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 670 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 672 |  | 
| Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 673 | /** | 
 | 674 |  * nfs_delegations_present - check for existence of delegations | 
 | 675 |  * @clp: client state handle | 
 | 676 |  * | 
 | 677 |  * Returns one if there are any nfs_delegation structures attached | 
 | 678 |  * to this nfs_client. | 
 | 679 |  */ | 
 | 680 | int nfs_delegations_present(struct nfs_client *clp) | 
 | 681 | { | 
 | 682 | 	struct nfs_server *server; | 
 | 683 | 	int ret = 0; | 
 | 684 |  | 
 | 685 | 	rcu_read_lock(); | 
 | 686 | 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | 
 | 687 | 		if (!list_empty(&server->delegations)) { | 
 | 688 | 			ret = 1; | 
 | 689 | 			break; | 
 | 690 | 		} | 
 | 691 | 	rcu_read_unlock(); | 
 | 692 | 	return ret; | 
 | 693 | } | 
 | 694 |  | 
 | 695 | /** | 
 | 696 |  * nfs4_copy_delegation_stateid - Copy inode's state ID information | 
 | 697 |  * @dst: stateid data structure to fill in | 
 | 698 |  * @inode: inode to check | 
 | 699 |  * | 
 | 700 |  * Returns one and fills in "dst->data" * if inode had a delegation, | 
 | 701 |  * otherwise zero is returned. | 
 | 702 |  */ | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 703 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) | 
 | 704 | { | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 705 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 706 | 	struct nfs_delegation *delegation; | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 707 | 	int ret = 0; | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 708 |  | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 709 | 	rcu_read_lock(); | 
 | 710 | 	delegation = rcu_dereference(nfsi->delegation); | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 711 | 	if (delegation != NULL) { | 
 | 712 | 		memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 713 | 		ret = 1; | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 714 | 	} | 
| Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 715 | 	rcu_read_unlock(); | 
 | 716 | 	return ret; | 
| Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 717 | } |