blob: bbbc6bf5cb2e42a8b1440c31eafa2e0f2c0c5a77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
Trond Myklebust58d9714a2006-01-03 09:55:24 +010010#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
15
16#include <linux/nfs4.h>
17#include <linux/nfs_fs.h>
18#include <linux/nfs_xdr.h>
19
Trond Myklebust4ce79712005-06-22 17:16:21 +000020#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Trond Myklebust905f8d12007-08-06 12:18:34 -040024static void nfs_do_free_delegation(struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 kfree(delegation);
27}
28
Trond Myklebust8383e462007-07-06 15:12:04 -040029static void nfs_free_delegation_callback(struct rcu_head *head)
30{
31 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
32
Trond Myklebust905f8d12007-08-06 12:18:34 -040033 nfs_do_free_delegation(delegation);
34}
35
36static void nfs_free_delegation(struct nfs_delegation *delegation)
37{
Trond Myklebuste00b8a22011-01-27 14:55:39 -050038 if (delegation->cred) {
39 put_rpccred(delegation->cred);
40 delegation->cred = NULL;
41 }
Trond Myklebust905f8d12007-08-06 12:18:34 -040042 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
Trond Myklebust8383e462007-07-06 15:12:04 -040043}
44
Chuck Leverd3978bb2010-12-24 01:33:04 +000045/**
46 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
47 * @delegation: delegation to process
48 *
49 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050050void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
51{
52 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
53}
54
Chuck Leverd3978bb2010-12-24 01:33:04 +000055/**
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 Myklebustbd7bf9d2008-12-23 15:21:53 -050062int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050063{
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 Myklebust888e6942005-11-04 15:38:11 -050078static 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 Myklebustd5122202009-06-17 13:22:58 -070082 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050083
Trond Myklebust3f09df72009-06-17 13:23:00 -070084 if (inode->i_flock == NULL)
85 goto out;
86
Arnd Bergmannb89f4322010-09-18 15:09:31 +020087 /* Protect inode->i_flock using the file locks lock */
88 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080089 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050090 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
91 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040092 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050093 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +020094 unlock_flocks();
Trond Myklebust888e6942005-11-04 15:38:11 -050095 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070096 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070097 goto out;
Arnd Bergmannb89f4322010-09-18 15:09:31 +020098 lock_flocks();
Trond Myklebust888e6942005-11-04 15:38:11 -050099 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200100 unlock_flocks();
Trond Myklebust3f09df72009-06-17 13:23:00 -0700101out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500102 return status;
103}
104
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500105static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct nfs_inode *nfsi = NFS_I(inode);
108 struct nfs_open_context *ctx;
109 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -0500110 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112again:
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 Myklebust90163022007-07-05 14:55:18 -0400120 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
121 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 get_nfs_open_context(ctx);
123 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400124 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500125 if (err >= 0)
126 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500128 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto again;
131 }
132 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Chuck Leverd3978bb2010-12-24 01:33:04 +0000136/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000143void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
144 struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400146 struct nfs_delegation *delegation;
147 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Trond Myklebust8f649c32010-05-01 12:36:18 -0400149 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 Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Trond Myklebust57bfa892008-01-25 16:38:18 -0500177static 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 Myklebust86e89482008-12-23 15:21:39 -0500186static 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 Leverdda4b222010-12-24 01:32:54 +0000197static struct nfs_delegation *
198nfs_detach_delegation_locked(struct nfs_inode *nfsi,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000199 struct nfs_server *server)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500200{
David Howells17d2c0a2010-05-01 12:37:18 -0400201 struct nfs_delegation *delegation =
202 rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000203 lockdep_is_held(&server->nfs_client->cl_lock));
Trond Myklebust57bfa892008-01-25 16:38:18 -0500204
205 if (delegation == NULL)
206 goto nomatch;
Chuck Leverdda4b222010-12-24 01:32:54 +0000207
Trond Myklebust34310432008-12-23 15:21:38 -0500208 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500209 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500210 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500211 nfsi->delegation_state = 0;
212 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500213 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500214 return delegation;
215nomatch:
216 return NULL;
217}
218
Chuck Leverdda4b222010-12-24 01:32:54 +0000219static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000220 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000221{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000222 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000223 struct nfs_delegation *delegation;
224
225 spin_lock(&clp->cl_lock);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000226 delegation = nfs_detach_delegation_locked(nfsi, server);
Chuck Leverdda4b222010-12-24 01:32:54 +0000227 spin_unlock(&clp->cl_lock);
228 return delegation;
229}
230
Chuck Leverd3978bb2010-12-24 01:33:04 +0000231/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
240{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000241 struct nfs_server *server = NFS_SERVER(inode);
242 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400244 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500245 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int status = 0;
247
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400248 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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 Myklebustbeb2a5e2006-01-03 09:55:37 +0100255 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 delegation->cred = get_rpccred(cred);
257 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500258 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500259 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400262 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000263 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400264 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 Myklebust57bfa892008-01-25 16:38:18 -0500268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500270 /*
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 Harrison3110ff82008-05-02 13:42:44 -0700276 __func__, clp->cl_hostname);
David Howells17d2c0a2010-05-01 12:37:18 -0400277 if (delegation->type <= old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500278 freeme = delegation;
279 delegation = NULL;
280 goto out;
281 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000282 freeme = nfs_detach_delegation_locked(nfsi, server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000284 list_add_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500285 nfsi->delegation_state = delegation->type;
286 rcu_assign_pointer(nfsi->delegation, delegation);
287 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400288
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 Myklebust57bfa892008-01-25 16:38:18 -0500294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400296 if (delegation != NULL)
297 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500298 if (freeme != NULL)
299 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return status;
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * Basic procedure for returning a delegation to the server
305 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500306static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Trond Myklebust3f09df72009-06-17 13:23:00 -0700311 /*
312 * Guard against new delegated open/lock/unlock calls and against
313 * state recovery
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 down_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500316 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 up_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500318 if (err)
319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500321 err = nfs_do_return_delegation(inode, delegation, issync);
322out:
323 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400324}
325
Chuck Leverd3978bb2010-12-24 01:33:04 +0000326/**
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 Myklebust515d8612008-12-23 15:21:46 -0500331 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500332int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500333{
334 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000335 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500336 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500337 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500338
339restart:
340 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000341 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 Myklebustd18cc1f2009-12-03 08:10:17 -0500364 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500365 }
366 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500367 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500368}
369
Chuck Leverd3978bb2010-12-24 01:33:04 +0000370/**
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 Myklebuste6f81072008-01-24 18:14:34 -0500376 */
377void nfs_inode_return_delegation_noreclaim(struct inode *inode)
378{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000379 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500380 struct nfs_inode *nfsi = NFS_I(inode);
381 struct nfs_delegation *delegation;
382
David Howells17d2c0a2010-05-01 12:37:18 -0400383 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Chuck Leverd3978bb2010-12-24 01:33:04 +0000384 delegation = nfs_detach_delegation(nfsi, server);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500385 if (delegation != NULL)
386 nfs_do_return_delegation(inode, delegation, 0);
387 }
388}
389
Chuck Leverd3978bb2010-12-24 01:33:04 +0000390/**
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 Myklebust90163022007-07-05 14:55:18 -0400396int nfs_inode_return_delegation(struct inode *inode)
397{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000398 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust90163022007-07-05 14:55:18 -0400399 struct nfs_inode *nfsi = NFS_I(inode);
400 struct nfs_delegation *delegation;
401 int err = 0;
402
David Howells17d2c0a2010-05-01 12:37:18 -0400403 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Chuck Leverd3978bb2010-12-24 01:33:04 +0000404 delegation = nfs_detach_delegation(nfsi, server);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500405 if (delegation != NULL) {
Trond Myklebust1b924e52010-07-31 14:29:06 -0400406 nfs_wb_all(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500407 err = __nfs_inode_return_delegation(inode, delegation, 1);
408 }
Trond Myklebust90163022007-07-05 14:55:18 -0400409 }
410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Chuck Leverd3978bb2010-12-24 01:33:04 +0000413static void nfs_mark_return_delegation(struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500414{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000415 struct nfs_client *clp = NFS_SERVER(delegation->inode)->nfs_client;
416
Trond Myklebust6411bd42008-12-23 15:21:51 -0500417 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
418 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
419}
420
Chuck Leverd3978bb2010-12-24 01:33:04 +0000421/**
422 * nfs_super_return_all_delegations - return delegations for one superblock
423 * @sb: sb to process
424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500426void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000428 struct nfs_server *server = NFS_SB(sb);
429 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (clp == NULL)
433 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000434
Trond Myklebust8383e462007-07-06 15:12:04 -0400435 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000436 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500437 spin_lock(&delegation->lock);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000438 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500439 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400441 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000442
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500443 if (nfs_client_return_marked_delegations(clp) != 0)
444 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500445}
446
Chuck Leverd3978bb2010-12-24 01:33:04 +0000447static void nfs_mark_return_all_delegation_types(struct nfs_server *server,
448 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500449{
450 struct nfs_delegation *delegation;
451
Chuck Leverd3978bb2010-12-24 01:33:04 +0000452 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500453 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
454 continue;
455 if (delegation->type & flags)
Chuck Leverd3978bb2010-12-24 01:33:04 +0000456 nfs_mark_return_delegation(delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500457 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000458}
459
460static 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 Myklebust515d8612008-12-23 15:21:46 -0500468 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500471static 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 Myklebustb0d3ded2008-12-23 15:21:50 -0500476static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100477{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500478 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
479 nfs4_schedule_state_manager(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100480}
481
Chuck Leverd3978bb2010-12-24 01:33:04 +0000482/**
483 * nfs_expire_all_delegation_types
484 * @clp: client to process
485 * @flags: delegation types to expire
486 *
487 */
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500488void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500489{
490 nfs_client_mark_return_all_delegation_types(clp, flags);
491 nfs_delegation_run_state_manager(clp);
492}
493
Chuck Leverd3978bb2010-12-24 01:33:04 +0000494/**
495 * nfs_expire_all_delegations
496 * @clp: client to process
497 *
498 */
David Howellsadfa6f92006-08-22 20:06:08 -0400499void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100500{
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500501 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100502}
503
Chuck Leverd3978bb2010-12-24 01:33:04 +0000504/**
505 * nfs_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
506 * @clp: client to process
507 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
David Howellsadfa6f92006-08-22 20:06:08 -0400509void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (clp == NULL)
512 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500513 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Chuck Leverd3978bb2010-12-24 01:33:04 +0000516static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500517{
518 struct nfs_delegation *delegation;
519
Chuck Leverd3978bb2010-12-24 01:33:04 +0000520 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500521 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
522 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000523 nfs_mark_return_delegation(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500524 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500525}
526
Chuck Leverd3978bb2010-12-24 01:33:04 +0000527/**
528 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
529 * @clp: nfs_client to process
530 *
531 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500532void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
533{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000534 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 Myklebustb7391f42008-12-23 15:21:52 -0500541 nfs_delegation_run_state_manager(clp);
542}
543
Chuck Leverd3978bb2010-12-24 01:33:04 +0000544/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000551int nfs_async_inode_return_delegation(struct inode *inode,
552 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500554 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
555 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Trond Myklebust6411bd42008-12-23 15:21:51 -0500557 rcu_read_lock();
558 delegation = rcu_dereference(NFS_I(inode)->delegation);
Alexandros Batsakis25976412009-12-05 13:48:55 -0500559
Trond Myklebuste047a102010-06-16 09:52:27 -0400560 if (!clp->cl_mvops->validate_stateid(delegation, stateid)) {
Trond Myklebust6411bd42008-12-23 15:21:51 -0500561 rcu_read_unlock();
562 return -ENOENT;
563 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000564 nfs_mark_return_delegation(delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500565 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000566
Trond Myklebust6411bd42008-12-23 15:21:51 -0500567 nfs_delegation_run_state_manager(clp);
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Chuck Leverd3978bb2010-12-24 01:33:04 +0000571static struct inode *
572nfs_delegation_find_inode_server(struct nfs_server *server,
573 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct nfs_delegation *delegation;
576 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000577
578 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500579 spin_lock(&delegation->lock);
580 if (delegation->inode != NULL &&
581 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500584 spin_unlock(&delegation->lock);
585 if (res != NULL)
586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000588 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 */
599struct 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 Myklebust8383e462007-07-06 15:12:04 -0400611 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return res;
613}
614
Chuck Leverd3978bb2010-12-24 01:33:04 +0000615static 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 Torvalds1da177e2005-04-16 15:20:36 -0700627 */
David Howellsadfa6f92006-08-22 20:06:08 -0400628void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000630 struct nfs_server *server;
631
Trond Myklebust8383e462007-07-06 15:12:04 -0400632 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000633 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
634 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Chuck Leverd3978bb2010-12-24 01:33:04 +0000638/**
639 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
640 * @clp: nfs_client to process
641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
David Howellsadfa6f92006-08-22 20:06:08 -0400643void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Trond Myklebust8383e462007-07-06 15:12:04 -0400645 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000646 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500647 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000648
Trond Myklebust8383e462007-07-06 15:12:04 -0400649restart:
650 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000651 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 Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400670 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500672
Chuck Leverd3978bb2010-12-24 01:33:04 +0000673/**
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 */
680int 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 Myklebust3e4f6292006-03-20 13:44:46 -0500703int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
704{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500705 struct nfs_inode *nfsi = NFS_I(inode);
706 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400707 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500708
Trond Myklebust8383e462007-07-06 15:12:04 -0400709 rcu_read_lock();
710 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500711 if (delegation != NULL) {
712 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400713 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500714 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400715 rcu_read_unlock();
716 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500717}