blob: fdd03f6ae044795bb74329e73f4983b38f2b73ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050041#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040042#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050044#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define NFSDDBG_FACILITY NFSDDBG_PROC
47
48/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050049time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050050time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070051static time_t boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static u32 current_ownerid = 1;
53static u32 current_fileid = 1;
54static u32 current_delegid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070055static stateid_t zerostateid; /* bits all 0 */
56static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030057static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070058
59#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040063static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
J. Bruce Fields8b671b82009-02-22 14:51:34 -080065/* Locking: */
66
67/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080068static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
J. Bruce Fields8b671b82009-02-22 14:51:34 -080070/*
71 * Currently used for the del_recall_lru and file hash table. In an
72 * effort to decrease the scope of the client_mutex, this spinlock may
73 * eventually cover more:
74 */
75static DEFINE_SPINLOCK(recall_lock);
76
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040077static struct kmem_cache *openowner_slab = NULL;
78static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *file_slab = NULL;
80static struct kmem_cache *stateid_slab = NULL;
81static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083void
84nfs4_lock_state(void)
85{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080086 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89void
90nfs4_unlock_state(void)
91{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080092 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static inline u32
96opaque_hashval(const void *ptr, int nbytes)
97{
98 unsigned char *cptr = (unsigned char *) ptr;
99
100 u32 x = 0;
101 while (nbytes--) {
102 x *= 37;
103 x += *cptr++;
104 }
105 return x;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct list_head del_recall_lru;
109
NeilBrown13cd2182005-06-23 22:03:10 -0700110static inline void
111put_nfs4_file(struct nfs4_file *fi)
112{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800113 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
114 list_del(&fi->fi_hash);
115 spin_unlock(&recall_lock);
116 iput(fi->fi_inode);
117 kmem_cache_free(file_slab, fi);
118 }
NeilBrown13cd2182005-06-23 22:03:10 -0700119}
120
121static inline void
122get_nfs4_file(struct nfs4_file *fi)
123{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800124 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700125}
126
NeilBrownef0f3392006-04-10 22:55:41 -0700127static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700128unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700129
130/*
131 * Open owner state (share locks)
132 */
133
J. Bruce Fields506f2752011-08-11 18:50:18 -0400134/* hash tables for open owners */
135#define OPEN_OWNER_HASH_BITS 8
136#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
137#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700138
J. Bruce Fields506f2752011-08-11 18:50:18 -0400139static unsigned int open_ownerid_hashval(const u32 id)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400140{
J. Bruce Fields506f2752011-08-11 18:50:18 -0400141 return id & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400142}
143
J. Bruce Fields506f2752011-08-11 18:50:18 -0400144static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400145{
146 unsigned int ret;
147
148 ret = opaque_hashval(ownername->data, ownername->len);
149 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400150 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400151}
NeilBrownef0f3392006-04-10 22:55:41 -0700152
J. Bruce Fields506f2752011-08-11 18:50:18 -0400153static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
154static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700155
156/* hash table for nfs4_file */
157#define FILE_HASH_BITS 8
158#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800159
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400160/* hash table for (open)nfs4_ol_stateid */
NeilBrownef0f3392006-04-10 22:55:41 -0700161#define STATEID_HASH_BITS 10
162#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
163#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
164
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400165static unsigned int file_hashval(struct inode *ino)
166{
167 /* XXX: why are we hashing on inode pointer, anyway? */
168 return hash_ptr(ino, FILE_HASH_BITS);
169}
170
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400171static unsigned int stateid_hashval(stateid_t *s)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400172{
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400173 return opaque_hashval(&s->si_opaque, sizeof(stateid_opaque_t)) & STATEID_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400174}
NeilBrownef0f3392006-04-10 22:55:41 -0700175
176static struct list_head file_hashtbl[FILE_HASH_SIZE];
177static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
178
J. Bruce Fields998db522010-08-07 09:21:41 -0400179static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400180{
181 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
182 atomic_inc(&fp->fi_access[oflag]);
183}
184
J. Bruce Fields998db522010-08-07 09:21:41 -0400185static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
186{
187 if (oflag == O_RDWR) {
188 __nfs4_file_get_access(fp, O_RDONLY);
189 __nfs4_file_get_access(fp, O_WRONLY);
190 } else
191 __nfs4_file_get_access(fp, oflag);
192}
193
194static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400195{
196 if (fp->fi_fds[oflag]) {
197 fput(fp->fi_fds[oflag]);
198 fp->fi_fds[oflag] = NULL;
199 }
200}
201
J. Bruce Fields998db522010-08-07 09:21:41 -0400202static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400203{
204 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
205 nfs4_file_put_fd(fp, O_RDWR);
206 nfs4_file_put_fd(fp, oflag);
207 }
208}
209
J. Bruce Fields998db522010-08-07 09:21:41 -0400210static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
211{
212 if (oflag == O_RDWR) {
213 __nfs4_file_put_access(fp, O_RDONLY);
214 __nfs4_file_put_access(fp, O_WRONLY);
215 } else
216 __nfs4_file_put_access(fp, oflag);
217}
218
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400219static inline void hash_stid(struct nfs4_stid *stid)
220{
221 stateid_t *s = &stid->sc_stateid;
222 unsigned int hashval;
223
J. Bruce Fieldsee626a72011-09-11 13:48:41 -0400224 hashval = stateid_hashval(s);
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400225 list_add(&stid->sc_hash, &stateid_hashtbl[hashval]);
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400229alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct nfs4_delegation *dp;
232 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400235 /*
236 * Major work on the lease subsystem (for example, to support
237 * calbacks on stat) will be required before we can support
238 * write delegations properly.
239 */
240 if (type != NFS4_OPEN_DELEGATE_READ)
241 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700242 if (fp->fi_had_conflict)
243 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700244 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700245 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700246 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
247 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700249 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700250 INIT_LIST_HEAD(&dp->dl_perfile);
251 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 INIT_LIST_HEAD(&dp->dl_recall_lru);
253 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700254 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dp->dl_type = type;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400257 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400258 dp->dl_stid.sc_stateid.si_boot = boot_time;
259 dp->dl_stid.sc_stateid.si_stateownerid = current_delegid++;
260 dp->dl_stid.sc_stateid.si_fileid = 0;
261 dp->dl_stid.sc_stateid.si_generation = 1;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400262 hash_stid(&dp->dl_stid);
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500263 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dp->dl_time = 0;
265 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500266 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return dp;
268}
269
270void
271nfs4_put_delegation(struct nfs4_delegation *dp)
272{
273 if (atomic_dec_and_test(&dp->dl_count)) {
274 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700275 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700276 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700277 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279}
280
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500281static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500283 if (atomic_dec_and_test(&fp->fi_delegees)) {
284 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
285 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400286 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500287 fp->fi_deleg_file = NULL;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/* Called under the state lock. */
292static void
293unhash_delegation(struct nfs4_delegation *dp)
294{
J. Bruce Fieldsf459e452011-09-09 09:06:12 -0400295 list_del_init(&dp->dl_stid.sc_hash);
NeilBrownea1da632005-06-23 22:04:17 -0700296 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500298 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 list_del_init(&dp->dl_recall_lru);
300 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500301 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 nfs4_put_delegation(dp);
303}
304
305/*
306 * SETCLIENTID state
307 */
308
Benny Halevy36acb662010-05-12 00:13:04 +0300309/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300310static DEFINE_SPINLOCK(client_lock);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* Hash tables for nfs4_clientid state */
313#define CLIENT_HASH_BITS 4
314#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
315#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
316
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400317static unsigned int clientid_hashval(u32 id)
318{
319 return id & CLIENT_HASH_MASK;
320}
321
322static unsigned int clientstr_hashval(const char *name)
323{
324 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
329 * used in reboot/reset lease grace period processing
330 *
331 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
332 * setclientid_confirmed info.
333 *
334 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
335 * setclientid info.
336 *
337 * client_lru holds client queue ordered by nfs4_client.cl_time
338 * for lease renewal.
339 *
340 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
341 * for last close replay.
342 */
343static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
344static int reclaim_str_hashtbl_size = 0;
345static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
346static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
347static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
348static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
349static struct list_head client_lru;
350static struct list_head close_lru;
351
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400352/*
353 * We store the NONE, READ, WRITE, and BOTH bits separately in the
354 * st_{access,deny}_bmap field of the stateid, in order to track not
355 * only what share bits are currently in force, but also what
356 * combinations of share bits previous opens have used. This allows us
357 * to enforce the recommendation of rfc 3530 14.2.19 that the server
358 * return an error if the client attempt to downgrade to a combination
359 * of share bits not explicable by closing some of its previous opens.
360 *
361 * XXX: This enforcement is actually incomplete, since we don't keep
362 * track of access/deny bit combinations; so, e.g., we allow:
363 *
364 * OPEN allow read, deny write
365 * OPEN allow both, deny none
366 * DOWNGRADE allow read, deny none
367 *
368 * which we should reject.
369 */
370static void
371set_access(unsigned int *access, unsigned long bmap) {
372 int i;
373
374 *access = 0;
375 for (i = 1; i < 4; i++) {
376 if (test_bit(i, &bmap))
377 *access |= i;
378 }
379}
380
381static void
382set_deny(unsigned int *deny, unsigned long bmap) {
383 int i;
384
385 *deny = 0;
386 for (i = 0; i < 4; i++) {
387 if (test_bit(i, &bmap))
388 *deny |= i ;
389 }
390}
391
392static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400393test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400394 unsigned int access, deny;
395
396 set_access(&access, stp->st_access_bmap);
397 set_deny(&deny, stp->st_deny_bmap);
398 if ((access & open->op_share_deny) || (deny & open->op_share_access))
399 return 0;
400 return 1;
401}
402
403static int nfs4_access_to_omode(u32 access)
404{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400405 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400406 case NFS4_SHARE_ACCESS_READ:
407 return O_RDONLY;
408 case NFS4_SHARE_ACCESS_WRITE:
409 return O_WRONLY;
410 case NFS4_SHARE_ACCESS_BOTH:
411 return O_RDWR;
412 }
413 BUG();
414}
415
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400416static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500417{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400418 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500419 list_del(&stp->st_perfile);
420 list_del(&stp->st_perstateowner);
421}
422
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400423static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500424{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400425 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500426
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800427 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400428 for (i = 1; i < 4; i++) {
429 if (test_bit(i, &stp->st_access_bmap))
430 nfs4_file_put_access(stp->st_file,
431 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400432 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400433 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800434 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400435 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400436 stp->st_file = NULL;
437}
438
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400439static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400440{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500441 kmem_cache_free(stateid_slab, stp);
442}
443
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400444static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500445{
446 struct file *file;
447
448 unhash_generic_stateid(stp);
449 file = find_any_file(stp->st_file);
450 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400451 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400452 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500453 free_generic_stateid(stp);
454}
455
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400456static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500457{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400458 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500459
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400460 list_del(&lo->lo_owner.so_idhash);
461 list_del(&lo->lo_owner.so_strhash);
462 list_del(&lo->lo_perstateid);
463 while (!list_empty(&lo->lo_owner.so_stateids)) {
464 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400465 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500466 release_lock_stateid(stp);
467 }
468}
469
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400470static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500471{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400472 unhash_lockowner(lo);
473 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500474}
475
476static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400477release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500478{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400479 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500480
481 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400482 lo = list_entry(open_stp->st_lockowners.next,
483 struct nfs4_lockowner, lo_perstateid);
484 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500485 }
486}
487
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400488static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500489{
490 unhash_generic_stateid(stp);
491 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400492 close_generic_stateid(stp);
493}
494
495static void release_open_stateid(struct nfs4_ol_stateid *stp)
496{
497 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500498 free_generic_stateid(stp);
499}
500
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400501static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500502{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400503 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500504
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400505 list_del(&oo->oo_owner.so_idhash);
506 list_del(&oo->oo_owner.so_strhash);
507 list_del(&oo->oo_perclient);
508 while (!list_empty(&oo->oo_owner.so_stateids)) {
509 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400510 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500511 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500512 }
513}
514
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400515static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500516{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400517 unhash_openowner(oo);
518 list_del(&oo->oo_close_lru);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400519 if (oo->oo_last_closed_stid)
520 free_generic_stateid(oo->oo_last_closed_stid);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400521 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500522}
523
Marc Eshel5282fd72009-04-03 08:27:52 +0300524#define SESSION_HASH_SIZE 512
525static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
526
527static inline int
528hash_sessionid(struct nfs4_sessionid *sessionid)
529{
530 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
531
532 return sid->sequence % SESSION_HASH_SIZE;
533}
534
535static inline void
536dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
537{
538 u32 *ptr = (u32 *)(&sessionid->data[0]);
539 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
540}
541
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300542static void
543gen_sessionid(struct nfsd4_session *ses)
544{
545 struct nfs4_client *clp = ses->se_client;
546 struct nfsd4_sessionid *sid;
547
548 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
549 sid->clientid = clp->cl_clientid;
550 sid->sequence = current_sessionid++;
551 sid->reserved = 0;
552}
553
554/*
Andy Adamsona6496372009-08-28 08:45:01 -0400555 * The protocol defines ca_maxresponssize_cached to include the size of
556 * the rpc header, but all we need to cache is the data starting after
557 * the end of the initial SEQUENCE operation--the rest we regenerate
558 * each time. Therefore we can advertise a ca_maxresponssize_cached
559 * value that is the number of bytes in our cache plus a few additional
560 * bytes. In order to stay on the safe side, and not promise more than
561 * we can cache, those additional bytes must be the minimum possible: 24
562 * bytes of rpc header (xid through accept state, with AUTH_NULL
563 * verifier), 12 for the compound header (with zero-length tag), and 44
564 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300565 */
Andy Adamsona6496372009-08-28 08:45:01 -0400566#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
567
Andy Adamson557ce262009-08-28 08:45:04 -0400568static void
569free_session_slots(struct nfsd4_session *ses)
570{
571 int i;
572
573 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
574 kfree(ses->se_slots[i]);
575}
576
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400577/*
578 * We don't actually need to cache the rpc and session headers, so we
579 * can allocate a little less for each slot:
580 */
581static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
582{
583 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
584}
585
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400586static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300587{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400588 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
589 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300590
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400591 return size;
592}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300593
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400594/*
595 * XXX: If we run out of reserved DRC memory we could (up to a point)
596 * re-negotiate active sessions and reduce their slot usage to make
597 * rooom for new connections. For now we just fail the create session.
598 */
599static int nfsd4_get_drc_mem(int slotsize, u32 num)
600{
601 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300602
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400603 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400604
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400605 spin_lock(&nfsd_drc_lock);
606 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
607 nfsd_drc_max_mem - nfsd_drc_mem_used);
608 num = min_t(int, num, avail / slotsize);
609 nfsd_drc_mem_used += num * slotsize;
610 spin_unlock(&nfsd_drc_lock);
611
612 return num;
613}
614
615static void nfsd4_put_drc_mem(int slotsize, int num)
616{
617 spin_lock(&nfsd_drc_lock);
618 nfsd_drc_mem_used -= slotsize * num;
619 spin_unlock(&nfsd_drc_lock);
620}
621
622static struct nfsd4_session *alloc_session(int slotsize, int numslots)
623{
624 struct nfsd4_session *new;
625 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300626
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400627 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400628 + sizeof(struct nfsd4_session) > PAGE_SIZE);
629 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400630
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400631 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300632 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400633 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400634 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400635 for (i = 0; i < numslots; i++) {
636 mem = sizeof(struct nfsd4_slot) + slotsize;
637 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
638 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400639 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400640 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400641 return new;
642out_free:
643 while (i--)
644 kfree(new->se_slots[i]);
645 kfree(new);
646 return NULL;
647}
648
649static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
650{
651 u32 maxrpc = nfsd_serv->sv_max_mesg;
652
653 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800654 new->maxresp_cached = min_t(u32, req->maxresp_cached,
655 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400656 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
657 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
658 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
659}
660
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400661static void free_conn(struct nfsd4_conn *c)
662{
663 svc_xprt_put(c->cn_xprt);
664 kfree(c);
665}
666
667static void nfsd4_conn_lost(struct svc_xpt_user *u)
668{
669 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
670 struct nfs4_client *clp = c->cn_session->se_client;
671
672 spin_lock(&clp->cl_lock);
673 if (!list_empty(&c->cn_persession)) {
674 list_del(&c->cn_persession);
675 free_conn(c);
676 }
677 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500678 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400679}
680
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400681static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400682{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400683 struct nfsd4_conn *conn;
684
685 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
686 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400687 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400688 svc_xprt_get(rqstp->rq_xprt);
689 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400690 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400691 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
692 return conn;
693}
694
J. Bruce Fields328ead22010-09-29 16:11:06 -0400695static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
696{
697 conn->cn_session = ses;
698 list_add(&conn->cn_persession, &ses->se_conns);
699}
700
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400701static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
702{
703 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400704
705 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400706 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400707 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400708}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400709
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400710static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400711{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400712 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400713 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400714}
715
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400716static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400717{
718 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400719 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400720
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400721 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400722 if (!conn)
723 return nfserr_jukebox;
724 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400725 ret = nfsd4_register_conn(conn);
726 if (ret)
727 /* oops; xprt is already down: */
728 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400729 return nfs_ok;
730}
731
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400732static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
733{
734 u32 dir = NFS4_CDFC4_FORE;
735
736 if (ses->se_flags & SESSION4_BACK_CHAN)
737 dir |= NFS4_CDFC4_BACK;
738
739 return nfsd4_new_conn(rqstp, ses, dir);
740}
741
742/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400743static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400744{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400745 struct nfs4_client *clp = s->se_client;
746 struct nfsd4_conn *c;
747
748 spin_lock(&clp->cl_lock);
749 while (!list_empty(&s->se_conns)) {
750 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
751 list_del_init(&c->cn_persession);
752 spin_unlock(&clp->cl_lock);
753
754 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
755 free_conn(c);
756
757 spin_lock(&clp->cl_lock);
758 }
759 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400760}
761
762void free_session(struct kref *kref)
763{
764 struct nfsd4_session *ses;
765 int mem;
766
767 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400768 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400769 spin_lock(&nfsd_drc_lock);
770 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
771 nfsd_drc_mem_used -= mem;
772 spin_unlock(&nfsd_drc_lock);
773 free_session_slots(ses);
774 kfree(ses);
775}
776
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400777static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400778{
779 struct nfsd4_session *new;
780 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
781 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400782 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400783 int idx;
784
785 /*
786 * Note decreasing slot size below client's request may
787 * make it difficult for client to function correctly, whereas
788 * decreasing the number of slots will (just?) affect
789 * performance. When short on memory we therefore prefer to
790 * decrease number of slots instead of their size.
791 */
792 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
793 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800794 if (numslots < 1)
795 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400796
797 new = alloc_session(slotsize, numslots);
798 if (!new) {
799 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400800 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400801 }
802 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400803
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300804 new->se_client = clp;
805 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300806
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400807 INIT_LIST_HEAD(&new->se_conns);
808
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400809 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300810 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400811 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300812 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400813 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300814 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300815 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400816 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300817 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400818 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300819 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300820
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400821 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400822 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400823 if (status) {
824 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400825 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400826 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400827 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400828 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400829 /*
830 * This is a little silly; with sessions there's no real
831 * use for the callback address. Use the peer address
832 * as a reasonable default for now, but consider fixing
833 * the rpc client not to require an address in the
834 * future:
835 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400836 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
837 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400838 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400839 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400840 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300841}
842
Benny Halevy9089f1b2010-05-12 00:12:26 +0300843/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300844static struct nfsd4_session *
845find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
846{
847 struct nfsd4_session *elem;
848 int idx;
849
850 dump_sessionid(__func__, sessionid);
851 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300852 /* Search in the appropriate list */
853 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300854 if (!memcmp(elem->se_sessionid.data, sessionid->data,
855 NFS4_MAX_SESSIONID_LEN)) {
856 return elem;
857 }
858 }
859
860 dprintk("%s: session not found\n", __func__);
861 return NULL;
862}
863
Benny Halevy9089f1b2010-05-12 00:12:26 +0300864/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300865static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300866unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300867{
868 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400869 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300870 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400871 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300872}
873
Benny Halevy36acb662010-05-12 00:13:04 +0300874/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300876renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Benny Halevy07cd4902010-05-12 00:13:41 +0300878 if (is_client_expired(clp)) {
879 dprintk("%s: client (clientid %08x/%08x) already expired\n",
880 __func__,
881 clp->cl_clientid.cl_boot,
882 clp->cl_clientid.cl_id);
883 return;
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Move client to the end to the LRU list.
888 */
889 dprintk("renewing client (clientid %08x/%08x)\n",
890 clp->cl_clientid.cl_boot,
891 clp->cl_clientid.cl_id);
892 list_move_tail(&clp->cl_lru, &client_lru);
893 clp->cl_time = get_seconds();
894}
895
Benny Halevy36acb662010-05-12 00:13:04 +0300896static inline void
897renew_client(struct nfs4_client *clp)
898{
899 spin_lock(&client_lock);
900 renew_client_locked(clp);
901 spin_unlock(&client_lock);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
905static int
906STALE_CLIENTID(clientid_t *clid)
907{
908 if (clid->cl_boot == boot_time)
909 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300910 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
911 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 1;
913}
914
915/*
916 * XXX Should we use a slab cache ?
917 * This type of memory management is somewhat inefficient, but we use it
918 * anyway since SETCLIENTID is not a common operation.
919 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500920static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct nfs4_client *clp;
923
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500924 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
925 if (clp == NULL)
926 return NULL;
927 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
928 if (clp->cl_name.data == NULL) {
929 kfree(clp);
930 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500932 memcpy(clp->cl_name.data, name.data, name.len);
933 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return clp;
935}
936
937static inline void
938free_client(struct nfs4_client *clp)
939{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400940 while (!list_empty(&clp->cl_sessions)) {
941 struct nfsd4_session *ses;
942 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
943 se_perclnt);
944 list_del(&ses->se_perclnt);
945 nfsd4_put_session(ses);
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (clp->cl_cred.cr_group_info)
948 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500949 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 kfree(clp->cl_name.data);
951 kfree(clp);
952}
953
Benny Halevyd7682982010-05-12 00:13:54 +0300954void
955release_session_client(struct nfsd4_session *session)
956{
957 struct nfs4_client *clp = session->se_client;
958
959 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
960 return;
961 if (is_client_expired(clp)) {
962 free_client(clp);
963 session->se_client = NULL;
964 } else
965 renew_client_locked(clp);
966 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300967}
968
Benny Halevy84d38ac2010-05-12 00:13:16 +0300969/* must be called under the client_lock */
970static inline void
971unhash_client_locked(struct nfs4_client *clp)
972{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400973 struct nfsd4_session *ses;
974
Benny Halevy07cd4902010-05-12 00:13:41 +0300975 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300976 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400977 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400978 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
979 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400980 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983static void
984expire_client(struct nfs4_client *clp)
985{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct list_head reaplist;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 INIT_LIST_HEAD(&reaplist);
991 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700992 while (!list_empty(&clp->cl_delegations)) {
993 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700994 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 list_move(&dp->dl_recall_lru, &reaplist);
996 }
997 spin_unlock(&recall_lock);
998 while (!list_empty(&reaplist)) {
999 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1000 list_del_init(&dp->dl_recall_lru);
1001 unhash_delegation(dp);
1002 }
NeilBrownea1da632005-06-23 22:04:17 -07001003 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001004 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1005 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001007 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001008 if (clp->cl_cb_conn.cb_xprt)
1009 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001010 list_del(&clp->cl_idhash);
1011 list_del(&clp->cl_strhash);
1012 spin_lock(&client_lock);
1013 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001014 if (atomic_read(&clp->cl_refcount) == 0)
1015 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001016 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001019static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1020{
1021 memcpy(target->cl_verifier.data, source->data,
1022 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001025static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1028 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1029}
1030
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001031static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 target->cr_uid = source->cr_uid;
1034 target->cr_gid = source->cr_gid;
1035 target->cr_group_info = source->cr_group_info;
1036 get_group_info(target->cr_group_info);
1037}
1038
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001039static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001040{
NeilBrowna55370a2005-06-23 22:03:52 -07001041 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001045same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1046{
1047 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001051same_clid(clientid_t *cl1, clientid_t *cl2)
1052{
1053 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/* XXX what about NGROUP */
1057static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001058same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1059{
1060 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001063static void gen_clid(struct nfs4_client *clp)
1064{
1065 static u32 current_clientid = 1;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 clp->cl_clientid.cl_boot = boot_time;
1068 clp->cl_clientid.cl_id = current_clientid++;
1069}
1070
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001071static void gen_confirm(struct nfs4_client *clp)
1072{
1073 static u32 i;
1074 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001077 *p++ = get_seconds();
1078 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
J. Bruce Fields4581d142011-09-06 14:56:09 -04001081static int
1082same_stateid(stateid_t *id_one, stateid_t *id_two)
1083{
1084 if (id_one->si_stateownerid != id_two->si_stateownerid)
1085 return 0;
1086 return id_one->si_fileid == id_two->si_fileid;
1087}
1088
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001089static struct nfs4_stid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001090{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001091 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001092 unsigned int hashval;
1093
J. Bruce Fieldsee626a72011-09-11 13:48:41 -04001094 hashval = stateid_hashval(t);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001095 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1096 if (same_stateid(&s->sc_stateid, t))
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001097 return s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001098 return NULL;
1099}
1100
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001101static struct nfs4_stid *find_stateid_by_type(stateid_t *t, char typemask)
1102{
1103 struct nfs4_stid *s;
1104
1105 s = find_stateid(t);
1106 if (!s)
1107 return NULL;
1108 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001109 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001110 return NULL;
1111}
1112
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001113static struct nfs4_ol_stateid *find_ol_stateid_by_type(stateid_t *t, char typemask)
1114{
1115 struct nfs4_stid *s;
1116
1117 s = find_stateid_by_type(t, typemask);
1118 if (!s)
1119 return NULL;
1120 return openlockstateid(s);
1121}
1122
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001123static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1124 struct svc_rqst *rqstp, nfs4_verifier *verf)
1125{
1126 struct nfs4_client *clp;
1127 struct sockaddr *sa = svc_addr(rqstp);
1128 char *princ;
1129
1130 clp = alloc_client(name);
1131 if (clp == NULL)
1132 return NULL;
1133
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001134 INIT_LIST_HEAD(&clp->cl_sessions);
1135
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001136 princ = svc_gss_principal(rqstp);
1137 if (princ) {
1138 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1139 if (clp->cl_principal == NULL) {
1140 free_client(clp);
1141 return NULL;
1142 }
1143 }
1144
1145 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001146 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001147 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001148 INIT_LIST_HEAD(&clp->cl_idhash);
1149 INIT_LIST_HEAD(&clp->cl_strhash);
1150 INIT_LIST_HEAD(&clp->cl_openowners);
1151 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001152 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001153 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001154 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001155 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001156 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001157 clear_bit(0, &clp->cl_cb_slot_busy);
1158 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1159 copy_verf(clp, verf);
1160 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1161 clp->cl_flavor = rqstp->rq_flavor;
1162 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1163 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001164 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001165 return clp;
1166}
1167
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001168static int check_name(struct xdr_netobj name)
1169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (name.len == 0)
1171 return 0;
1172 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001173 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175 }
1176 return 1;
1177}
1178
NeilBrownfd39ca92005-06-23 22:04:03 -07001179static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1181{
1182 unsigned int idhashval;
1183
1184 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1185 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1186 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001187 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
NeilBrownfd39ca92005-06-23 22:04:03 -07001190static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191move_to_confirmed(struct nfs4_client *clp)
1192{
1193 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1194 unsigned int strhashval;
1195
1196 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001197 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001198 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001199 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 renew_client(clp);
1201}
1202
1203static struct nfs4_client *
1204find_confirmed_client(clientid_t *clid)
1205{
1206 struct nfs4_client *clp;
1207 unsigned int idhashval = clientid_hashval(clid->cl_id);
1208
1209 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001210 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return clp;
1212 }
1213 return NULL;
1214}
1215
1216static struct nfs4_client *
1217find_unconfirmed_client(clientid_t *clid)
1218{
1219 struct nfs4_client *clp;
1220 unsigned int idhashval = clientid_hashval(clid->cl_id);
1221
1222 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001223 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return clp;
1225 }
1226 return NULL;
1227}
1228
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001229static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001230{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001231 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001232}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001233
NeilBrown28ce6052005-06-23 22:03:56 -07001234static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001235find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001236{
1237 struct nfs4_client *clp;
1238
1239 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001240 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001241 return clp;
1242 }
1243 return NULL;
1244}
1245
1246static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001247find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001248{
1249 struct nfs4_client *clp;
1250
1251 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001252 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001253 return clp;
1254 }
1255 return NULL;
1256}
1257
NeilBrownfd39ca92005-06-23 22:04:03 -07001258static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001259gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001261 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001262 struct sockaddr *sa = svc_addr(rqstp);
1263 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001264 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jeff Layton7077ecb2009-08-14 12:57:58 -04001266 /* Currently, we only support tcp and tcp6 for the callback channel */
1267 if (se->se_callback_netid_len == 3 &&
1268 !memcmp(se->se_callback_netid_val, "tcp", 3))
1269 expected_family = AF_INET;
1270 else if (se->se_callback_netid_len == 4 &&
1271 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1272 expected_family = AF_INET6;
1273 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto out_err;
1275
J. Bruce Fields07263f12010-05-31 19:09:40 -04001276 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001277 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001278 (struct sockaddr *)&conn->cb_addr,
1279 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001280
J. Bruce Fields07263f12010-05-31 19:09:40 -04001281 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001283
J. Bruce Fields07263f12010-05-31 19:09:40 -04001284 if (conn->cb_addr.ss_family == AF_INET6)
1285 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001286
J. Bruce Fields07263f12010-05-31 19:09:40 -04001287 conn->cb_prog = se->se_callback_prog;
1288 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001289 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return;
1291out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001292 conn->cb_addr.ss_family = AF_UNSPEC;
1293 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001294 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 "will not receive delegations\n",
1296 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return;
1299}
1300
Andy Adamson074fe892009-04-03 08:28:15 +03001301/*
Andy Adamson557ce262009-08-28 08:45:04 -04001302 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001303 */
1304void
1305nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1306{
Andy Adamson557ce262009-08-28 08:45:04 -04001307 struct nfsd4_slot *slot = resp->cstate.slot;
1308 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001309
Andy Adamson557ce262009-08-28 08:45:04 -04001310 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001311
Andy Adamson557ce262009-08-28 08:45:04 -04001312 slot->sl_opcnt = resp->opcnt;
1313 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001314
1315 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001316 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001317 return;
1318 }
Andy Adamson557ce262009-08-28 08:45:04 -04001319 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1320 base = (char *)resp->cstate.datap -
1321 (char *)resp->xbuf->head[0].iov_base;
1322 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1323 slot->sl_datalen))
1324 WARN("%s: sessions DRC could not cache compound\n", __func__);
1325 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001326}
1327
1328/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001329 * Encode the replay sequence operation from the slot values.
1330 * If cachethis is FALSE encode the uncached rep error on the next
1331 * operation which sets resp->p and increments resp->opcnt for
1332 * nfs4svc_encode_compoundres.
1333 *
Andy Adamson074fe892009-04-03 08:28:15 +03001334 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001335static __be32
1336nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1337 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001338{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001339 struct nfsd4_op *op;
1340 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001341
Andy Adamsonabfabf82009-07-23 19:02:18 -04001342 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001343 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001344
1345 /* Encode the replayed sequence operation */
1346 op = &args->ops[resp->opcnt - 1];
1347 nfsd4_encode_operation(resp, op);
1348
1349 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001350 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001351 op = &args->ops[resp->opcnt++];
1352 op->status = nfserr_retry_uncached_rep;
1353 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001354 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001355 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001356}
1357
1358/*
Andy Adamson557ce262009-08-28 08:45:04 -04001359 * The sequence operation is not cached because we can use the slot and
1360 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001361 */
1362__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001363nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1364 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001365{
Andy Adamson557ce262009-08-28 08:45:04 -04001366 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001367 __be32 status;
1368
Andy Adamson557ce262009-08-28 08:45:04 -04001369 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001370
Andy Adamsonabfabf82009-07-23 19:02:18 -04001371 /* Either returns 0 or nfserr_retry_uncached */
1372 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1373 if (status == nfserr_retry_uncached_rep)
1374 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001375
Andy Adamson557ce262009-08-28 08:45:04 -04001376 /* The sequence operation has been encoded, cstate->datap set. */
1377 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001378
Andy Adamson557ce262009-08-28 08:45:04 -04001379 resp->opcnt = slot->sl_opcnt;
1380 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1381 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001382
1383 return status;
1384}
1385
Andy Adamson0733d212009-04-03 08:28:01 +03001386/*
1387 * Set the exchange_id flags returned by the server.
1388 */
1389static void
1390nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1391{
1392 /* pNFS is not supported */
1393 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1394
1395 /* Referrals are supported, Migration is not. */
1396 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1397
1398 /* set the wire flags to return to client. */
1399 clid->flags = new->cl_exchange_flags;
1400}
1401
Al Virob37ad282006-10-19 23:28:59 -07001402__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001403nfsd4_exchange_id(struct svc_rqst *rqstp,
1404 struct nfsd4_compound_state *cstate,
1405 struct nfsd4_exchange_id *exid)
1406{
Andy Adamson0733d212009-04-03 08:28:01 +03001407 struct nfs4_client *unconf, *conf, *new;
1408 int status;
1409 unsigned int strhashval;
1410 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001411 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001412 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001413 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001414
Jeff Layton363168b2009-08-14 12:57:56 -04001415 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001416 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001417 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001418 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001419 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001420
1421 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1422 return nfserr_inval;
1423
1424 /* Currently only support SP4_NONE */
1425 switch (exid->spa_how) {
1426 case SP4_NONE:
1427 break;
1428 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001429 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001430 default:
1431 BUG(); /* checked by xdr code */
1432 case SP4_MACH_CRED:
1433 return nfserr_serverfault; /* no excuse :-/ */
1434 }
1435
1436 status = nfs4_make_rec_clidname(dname, &exid->clname);
1437
1438 if (status)
1439 goto error;
1440
1441 strhashval = clientstr_hashval(dname);
1442
1443 nfs4_lock_state();
1444 status = nfs_ok;
1445
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001446 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001447 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001448 if (!clp_used_exchangeid(conf)) {
1449 status = nfserr_clid_inuse; /* XXX: ? */
1450 goto out;
1451 }
Andy Adamson0733d212009-04-03 08:28:01 +03001452 if (!same_verf(&verf, &conf->cl_verifier)) {
1453 /* 18.35.4 case 8 */
1454 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1455 status = nfserr_not_same;
1456 goto out;
1457 }
1458 /* Client reboot: destroy old state */
1459 expire_client(conf);
1460 goto out_new;
1461 }
1462 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1463 /* 18.35.4 case 9 */
1464 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1465 status = nfserr_perm;
1466 goto out;
1467 }
1468 expire_client(conf);
1469 goto out_new;
1470 }
Andy Adamson0733d212009-04-03 08:28:01 +03001471 /*
1472 * Set bit when the owner id and verifier map to an already
1473 * confirmed client id (18.35.3).
1474 */
1475 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1476
1477 /*
1478 * Falling into 18.35.4 case 2, possible router replay.
1479 * Leave confirmed record intact and return same result.
1480 */
1481 copy_verf(conf, &verf);
1482 new = conf;
1483 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001484 }
1485
1486 /* 18.35.4 case 7 */
1487 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1488 status = nfserr_noent;
1489 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001490 }
1491
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001492 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001493 if (unconf) {
1494 /*
1495 * Possible retry or client restart. Per 18.35.4 case 4,
1496 * a new unconfirmed record should be generated regardless
1497 * of whether any properties have changed.
1498 */
1499 expire_client(unconf);
1500 }
1501
1502out_new:
1503 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001504 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001505 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001506 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001507 goto out;
1508 }
1509
Andy Adamson0733d212009-04-03 08:28:01 +03001510 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001511 add_to_unconfirmed(new, strhashval);
1512out_copy:
1513 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1514 exid->clientid.cl_id = new->cl_clientid.cl_id;
1515
Andy Adamson38eb76a2009-04-03 08:28:32 +03001516 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001517 nfsd4_set_ex_flags(new, exid);
1518
1519 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001520 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001521 status = nfs_ok;
1522
1523out:
1524 nfs4_unlock_state();
1525error:
1526 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1527 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001528}
1529
Benny Halevyb85d4c02009-04-03 08:28:08 +03001530static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001531check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001532{
Andy Adamson88e588d2009-07-23 19:02:15 -04001533 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1534 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001535
1536 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001537 if (slot_inuse) {
1538 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001539 return nfserr_jukebox;
1540 else
1541 return nfserr_seq_misordered;
1542 }
1543 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001544 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001545 return nfs_ok;
1546 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001547 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001548 return nfserr_replay_cache;
1549 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001550 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001551 return nfs_ok;
1552 /* Misordered replay or misordered new request */
1553 return nfserr_seq_misordered;
1554}
1555
Andy Adamson49557cc2009-07-23 19:02:16 -04001556/*
1557 * Cache the create session result into the create session single DRC
1558 * slot cache by saving the xdr structure. sl_seqid has been set.
1559 * Do this for solo or embedded create session operations.
1560 */
1561static void
1562nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1563 struct nfsd4_clid_slot *slot, int nfserr)
1564{
1565 slot->sl_status = nfserr;
1566 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1567}
1568
1569static __be32
1570nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1571 struct nfsd4_clid_slot *slot)
1572{
1573 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1574 return slot->sl_status;
1575}
1576
Mi Jinlong1b74c252011-07-14 14:50:17 +08001577#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1578 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1579 1 + /* MIN tag is length with zero, only length */ \
1580 3 + /* version, opcount, opcode */ \
1581 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1582 /* seqid, slotID, slotID, cache */ \
1583 4 ) * sizeof(__be32))
1584
1585#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1586 2 + /* verifier: AUTH_NULL, length 0 */\
1587 1 + /* status */ \
1588 1 + /* MIN tag is length with zero, only length */ \
1589 3 + /* opcount, opcode, opstatus*/ \
1590 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1591 /* seqid, slotID, slotID, slotID, status */ \
1592 5 ) * sizeof(__be32))
1593
1594static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1595{
1596 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1597 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1598}
1599
Andy Adamson069b6ad2009-04-03 08:27:58 +03001600__be32
1601nfsd4_create_session(struct svc_rqst *rqstp,
1602 struct nfsd4_compound_state *cstate,
1603 struct nfsd4_create_session *cr_ses)
1604{
Jeff Layton363168b2009-08-14 12:57:56 -04001605 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001606 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001607 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001608 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001609 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001610 int status = 0;
1611
Mi Jinlonga62573d2011-03-23 17:57:07 +08001612 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1613 return nfserr_inval;
1614
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001615 nfs4_lock_state();
1616 unconf = find_unconfirmed_client(&cr_ses->clientid);
1617 conf = find_confirmed_client(&cr_ses->clientid);
1618
1619 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001620 cs_slot = &conf->cl_cs_slot;
1621 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001622 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001623 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001624 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001625 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001626 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001627 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001628 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001629 status = nfserr_seq_misordered;
1630 dprintk("Sequence misordered!\n");
1631 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001632 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001633 goto out;
1634 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635 } else if (unconf) {
1636 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001637 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001638 status = nfserr_clid_inuse;
1639 goto out;
1640 }
1641
Andy Adamson49557cc2009-07-23 19:02:16 -04001642 cs_slot = &unconf->cl_cs_slot;
1643 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001644 if (status) {
1645 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001646 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001647 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001648 }
1649
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001650 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001651 conf = unconf;
1652 } else {
1653 status = nfserr_stale_clientid;
1654 goto out;
1655 }
1656
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001657 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001658 * XXX: we should probably set this at creation time, and check
1659 * for consistent minorversion use throughout:
1660 */
1661 conf->cl_minorversion = 1;
1662 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001663 * We do not support RDMA or persistent sessions
1664 */
1665 cr_ses->flags &= ~SESSION4_PERSIST;
1666 cr_ses->flags &= ~SESSION4_RDMA;
1667
Mi Jinlong1b74c252011-07-14 14:50:17 +08001668 status = nfserr_toosmall;
1669 if (check_forechannel_attrs(cr_ses->fore_channel))
1670 goto out;
1671
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001672 status = nfserr_jukebox;
1673 new = alloc_init_session(rqstp, conf, cr_ses);
1674 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001675 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001676 status = nfs_ok;
1677 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001678 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001679 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1680 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001681 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001682 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001683
Andy Adamson49557cc2009-07-23 19:02:16 -04001684 /* cache solo and embedded create sessions under the state lock */
1685 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001686 if (confirm_me)
1687 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001688out:
1689 nfs4_unlock_state();
1690 dprintk("%s returns %d\n", __func__, ntohl(status));
1691 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001692}
1693
J. Bruce Fields57716352010-04-21 12:27:19 -04001694static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1695{
1696 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1697 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1698
1699 return argp->opcnt == resp->opcnt;
1700}
1701
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001702static __be32 nfsd4_map_bcts_dir(u32 *dir)
1703{
1704 switch (*dir) {
1705 case NFS4_CDFC4_FORE:
1706 case NFS4_CDFC4_BACK:
1707 return nfs_ok;
1708 case NFS4_CDFC4_FORE_OR_BOTH:
1709 case NFS4_CDFC4_BACK_OR_BOTH:
1710 *dir = NFS4_CDFC4_BOTH;
1711 return nfs_ok;
1712 };
1713 return nfserr_inval;
1714}
1715
1716__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1717 struct nfsd4_compound_state *cstate,
1718 struct nfsd4_bind_conn_to_session *bcts)
1719{
1720 __be32 status;
1721
1722 if (!nfsd4_last_compound_op(rqstp))
1723 return nfserr_not_only_op;
1724 spin_lock(&client_lock);
1725 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1726 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1727 * client_lock iself: */
1728 if (cstate->session) {
1729 nfsd4_get_session(cstate->session);
1730 atomic_inc(&cstate->session->se_client->cl_refcount);
1731 }
1732 spin_unlock(&client_lock);
1733 if (!cstate->session)
1734 return nfserr_badsession;
1735
1736 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001737 if (!status)
1738 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1739 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001740}
1741
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001742static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1743{
1744 if (!session)
1745 return 0;
1746 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1747}
1748
Andy Adamson069b6ad2009-04-03 08:27:58 +03001749__be32
1750nfsd4_destroy_session(struct svc_rqst *r,
1751 struct nfsd4_compound_state *cstate,
1752 struct nfsd4_destroy_session *sessionid)
1753{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001754 struct nfsd4_session *ses;
1755 u32 status = nfserr_badsession;
1756
1757 /* Notes:
1758 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1759 * - Should we return nfserr_back_chan_busy if waiting for
1760 * callbacks on to-be-destroyed session?
1761 * - Do we need to clear any callback info from previous session?
1762 */
1763
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001764 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001765 if (!nfsd4_last_compound_op(r))
1766 return nfserr_not_only_op;
1767 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001768 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001769 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001770 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1771 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001772 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001773 goto out;
1774 }
1775
1776 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001777 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001778
Benny Halevyab707e152010-05-12 00:14:06 +03001779 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001780 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001781 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001782
1783 nfsd4_del_conns(ses);
1784
Benny Halevye10e0cf2009-04-03 08:28:38 +03001785 nfsd4_put_session(ses);
1786 status = nfs_ok;
1787out:
1788 dprintk("%s returns %d\n", __func__, ntohl(status));
1789 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001790}
1791
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001792static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001793{
1794 struct nfsd4_conn *c;
1795
1796 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001797 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001798 return c;
1799 }
1800 }
1801 return NULL;
1802}
1803
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001804static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001805{
1806 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001807 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001808 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001809
1810 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001811 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001812 if (c) {
1813 spin_unlock(&clp->cl_lock);
1814 free_conn(new);
1815 return;
1816 }
1817 __nfsd4_hash_conn(new, ses);
1818 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001819 ret = nfsd4_register_conn(new);
1820 if (ret)
1821 /* oops; xprt is already down: */
1822 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001823 return;
1824}
1825
Mi Jinlong868b89c2011-04-27 09:09:58 +08001826static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1827{
1828 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1829
1830 return args->opcnt > session->se_fchannel.maxops;
1831}
1832
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001833static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1834 struct nfsd4_session *session)
1835{
1836 struct xdr_buf *xb = &rqstp->rq_arg;
1837
1838 return xb->len > session->se_fchannel.maxreq_sz;
1839}
1840
Andy Adamson069b6ad2009-04-03 08:27:58 +03001841__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001842nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001843 struct nfsd4_compound_state *cstate,
1844 struct nfsd4_sequence *seq)
1845{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001846 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001847 struct nfsd4_session *session;
1848 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001849 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001850 int status;
1851
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001852 if (resp->opcnt != 1)
1853 return nfserr_sequence_pos;
1854
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001855 /*
1856 * Will be either used or freed by nfsd4_sequence_check_conn
1857 * below.
1858 */
1859 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1860 if (!conn)
1861 return nfserr_jukebox;
1862
Benny Halevy9089f1b2010-05-12 00:12:26 +03001863 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001864 status = nfserr_badsession;
1865 session = find_in_sessionid_hashtbl(&seq->sessionid);
1866 if (!session)
1867 goto out;
1868
Mi Jinlong868b89c2011-04-27 09:09:58 +08001869 status = nfserr_too_many_ops;
1870 if (nfsd4_session_too_many_ops(rqstp, session))
1871 goto out;
1872
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001873 status = nfserr_req_too_big;
1874 if (nfsd4_request_too_big(rqstp, session))
1875 goto out;
1876
Benny Halevyb85d4c02009-04-03 08:28:08 +03001877 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001878 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001879 goto out;
1880
Andy Adamson557ce262009-08-28 08:45:04 -04001881 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001882 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1883
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001884 /* We do not negotiate the number of slots yet, so set the
1885 * maxslots to the session maxreqs which is used to encode
1886 * sr_highest_slotid and the sr_target_slot id to maxslots */
1887 seq->maxslots = session->se_fchannel.maxreqs;
1888
Andy Adamson88e588d2009-07-23 19:02:15 -04001889 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001890 if (status == nfserr_replay_cache) {
1891 cstate->slot = slot;
1892 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001893 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001894 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001895 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001896 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001897 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001898 }
1899 if (status)
1900 goto out;
1901
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001902 nfsd4_sequence_check_conn(conn, session);
1903 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001904
Benny Halevyb85d4c02009-04-03 08:28:08 +03001905 /* Success! bump slot seqid */
1906 slot->sl_inuse = true;
1907 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001908 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001909
1910 cstate->slot = slot;
1911 cstate->session = session;
1912
Benny Halevyb85d4c02009-04-03 08:28:08 +03001913out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001914 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001915 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001916 struct nfs4_client *clp = session->se_client;
1917
Benny Halevy36acb662010-05-12 00:13:04 +03001918 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001919 atomic_inc(&clp->cl_refcount);
1920 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1921 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001922 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001923 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001924 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001925 dprintk("%s: return %d\n", __func__, ntohl(status));
1926 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001927}
1928
1929__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001930nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1931{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001932 int status = 0;
1933
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001934 if (rc->rca_one_fs) {
1935 if (!cstate->current_fh.fh_dentry)
1936 return nfserr_nofilehandle;
1937 /*
1938 * We don't take advantage of the rca_one_fs case.
1939 * That's OK, it's optional, we can safely ignore it.
1940 */
1941 return nfs_ok;
1942 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001943
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001944 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001945 status = nfserr_complete_already;
1946 if (cstate->session->se_client->cl_firststate)
1947 goto out;
1948
1949 status = nfserr_stale_clientid;
1950 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001951 /*
1952 * The following error isn't really legal.
1953 * But we only get here if the client just explicitly
1954 * destroyed the client. Surely it no longer cares what
1955 * error it gets back on an operation for the dead
1956 * client.
1957 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001958 goto out;
1959
1960 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001961 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001962out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001963 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001964 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001965}
1966
1967__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001968nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1969 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 struct xdr_netobj clname = {
1972 .len = setclid->se_namelen,
1973 .data = setclid->se_name,
1974 };
1975 nfs4_verifier clverifier = setclid->se_verf;
1976 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001977 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001978 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001979 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001982 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
NeilBrowna55370a2005-06-23 22:03:52 -07001984 status = nfs4_make_rec_clidname(dname, &clname);
1985 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001986 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1990 * We get here on a DRC miss.
1991 */
1992
NeilBrowna55370a2005-06-23 22:03:52 -07001993 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001996 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07001997 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001998 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002000 if (clp_used_exchangeid(conf))
2001 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002002 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002003 char addr_str[INET6_ADDRSTRLEN];
2004 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2005 sizeof(addr_str));
2006 dprintk("NFSD: setclientid: string in use by client "
2007 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto out;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002011 /*
2012 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2013 * has a description of SETCLIENTID request processing consisting
2014 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2015 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002016 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002017 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002019 /*
2020 * RFC 3530 14.2.33 CASE 4:
2021 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
2023 if (unconf)
2024 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002025 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002026 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002029 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002031 * RFC 3530 14.2.33 CASE 1:
2032 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002034 if (unconf) {
2035 /* Note this is removing unconfirmed {*x***},
2036 * which is stronger than RFC recommended {vxc**}.
2037 * This has the advantage that there is at most
2038 * one {*x***} in either list at any time.
2039 */
2040 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002042 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002043 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else if (!unconf) {
2047 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002048 * RFC 3530 14.2.33 CASE 2:
2049 * probable client reboot; state will be removed if
2050 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002052 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002053 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002056 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002057 /*
2058 * RFC 3530 14.2.33 CASE 3:
2059 * probable client reboot; state will be removed if
2060 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
2062 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002063 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002064 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002068 /*
2069 * XXX: we should probably set this at creation time, and check
2070 * for consistent minorversion use throughout:
2071 */
2072 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002073 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002074 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2076 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2077 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2078 status = nfs_ok;
2079out:
2080 nfs4_unlock_state();
2081 return status;
2082}
2083
2084
2085/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002086 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2087 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2088 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Al Virob37ad282006-10-19 23:28:59 -07002090__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002091nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2092 struct nfsd4_compound_state *cstate,
2093 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Jeff Layton363168b2009-08-14 12:57:56 -04002095 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002096 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2098 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002099 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 if (STALE_CLIENTID(clid))
2102 return nfserr_stale_clientid;
2103 /*
2104 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2105 * We get here on a DRC miss.
2106 */
2107
2108 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002109
2110 conf = find_confirmed_client(clid);
2111 unconf = find_unconfirmed_client(clid);
2112
2113 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002114 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002115 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002116 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002117 goto out;
2118
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002119 /*
2120 * section 14.2.34 of RFC 3530 has a description of
2121 * SETCLIENTID_CONFIRM request processing consisting
2122 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2123 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002124 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002125 /*
2126 * RFC 3530 14.2.34 CASE 1:
2127 * callback update
2128 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002129 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 status = nfserr_clid_inuse;
2131 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002132 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2133 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002134 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002138 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002139 /*
2140 * RFC 3530 14.2.34 CASE 2:
2141 * probable retransmitted request; play it safe and
2142 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002143 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002144 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002146 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002148 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002149 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002150 /*
2151 * RFC 3530 14.2.34 CASE 3:
2152 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002153 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002154 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 status = nfserr_clid_inuse;
2156 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002157 unsigned int hash =
2158 clientstr_hashval(unconf->cl_recdir);
2159 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002160 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002161 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002162 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002163 expire_client(conf);
2164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002166 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002167 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002168 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002170 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2171 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002172 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002173 /*
2174 * RFC 3530 14.2.34 CASE 4:
2175 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002178 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002179 /* check that we have hit one of the cases...*/
2180 status = nfserr_clid_inuse;
2181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 nfs4_unlock_state();
2184 return status;
2185}
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/* OPEN Share state helper functions */
2188static inline struct nfs4_file *
2189alloc_init_file(struct inode *ino)
2190{
2191 struct nfs4_file *fp;
2192 unsigned int hashval = file_hashval(ino);
2193
NeilBrowne60d4392005-06-23 22:03:01 -07002194 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2195 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002196 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002198 INIT_LIST_HEAD(&fp->fi_stateids);
2199 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 fp->fi_inode = igrab(ino);
2201 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002202 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002203 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002204 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2205 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002206 spin_lock(&recall_lock);
2207 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2208 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return fp;
2210 }
2211 return NULL;
2212}
2213
2214static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002215nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002216{
NeilBrowne60d4392005-06-23 22:03:01 -07002217 if (*slab == NULL)
2218 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002219 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002220 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002221}
2222
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002223void
NeilBrowne60d4392005-06-23 22:03:01 -07002224nfsd4_free_slabs(void)
2225{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002226 nfsd4_free_slab(&openowner_slab);
2227 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002228 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002229 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002230 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233static int
2234nfsd4_init_slabs(void)
2235{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002236 openowner_slab = kmem_cache_create("nfsd4_openowners",
2237 sizeof(struct nfs4_openowner), 0, 0, NULL);
2238 if (openowner_slab == NULL)
2239 goto out_nomem;
2240 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2241 sizeof(struct nfs4_openowner), 0, 0, NULL);
2242 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002243 goto out_nomem;
2244 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002245 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002246 if (file_slab == NULL)
2247 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002248 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002249 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002250 if (stateid_slab == NULL)
2251 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002252 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002253 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002254 if (deleg_slab == NULL)
2255 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002257out_nomem:
2258 nfsd4_free_slabs();
2259 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2260 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002263void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002265 kfree(oo->oo_owner.so_owner.data);
2266 kmem_cache_free(openowner_slab, oo);
2267}
2268
2269void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2270{
2271 kfree(lo->lo_owner.so_owner.data);
2272 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002275static void init_nfs4_replay(struct nfs4_replay *rp)
2276{
2277 rp->rp_status = nfserr_serverfault;
2278 rp->rp_buflen = 0;
2279 rp->rp_buf = rp->rp_ibuf;
2280}
2281
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002282static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 struct nfs4_stateowner *sop;
2285
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002286 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002287 if (!sop)
2288 return NULL;
2289
2290 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2291 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002292 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002295 sop->so_owner.len = owner->len;
2296
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002297 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002298 sop->so_id = current_ownerid++;
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002299 sop->so_client = clp;
2300 init_nfs4_replay(&sop->so_replay);
2301 return sop;
2302}
2303
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002304static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002305{
2306 unsigned int idhashval;
2307
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002308 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2309 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2310 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2311 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002314static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002316 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002318 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2319 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002321 oo->oo_owner.so_is_open_owner = 1;
2322 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002323 oo->oo_flags = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002324 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002325 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002326 INIT_LIST_HEAD(&oo->oo_close_lru);
2327 hash_openowner(oo, clp, strhashval);
2328 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002332init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002333 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
NeilBrownea1da632005-06-23 22:04:17 -07002335 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002336 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002337 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002338 stp->st_stid.sc_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002339 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002340 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002342 stp->st_stid.sc_stateid.si_boot = boot_time;
2343 stp->st_stid.sc_stateid.si_stateownerid = oo->oo_owner.so_id;
2344 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002345 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002346 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04002347 hash_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 stp->st_access_bmap = 0;
2349 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002350 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2351 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002353 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002357move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002359 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002361 list_move_tail(&oo->oo_close_lru, &close_lru);
2362 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002366same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2367 clientid_t *clid)
2368{
2369 return (sop->so_owner.len == owner->len) &&
2370 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2371 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002374static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2376{
2377 struct nfs4_stateowner *so = NULL;
2378
J. Bruce Fields506f2752011-08-11 18:50:18 -04002379 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002380 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002381 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 return NULL;
2384}
2385
2386/* search file_hashtbl[] for file */
2387static struct nfs4_file *
2388find_file(struct inode *ino)
2389{
2390 unsigned int hashval = file_hashval(ino);
2391 struct nfs4_file *fp;
2392
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002393 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002395 if (fp->fi_inode == ino) {
2396 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002397 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002401 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return NULL;
2403}
2404
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002405static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002406{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002407 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002408 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002409 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2410 return 0;
2411 x &= ~NFS4_SHARE_ACCESS_MASK;
2412 if (minorversion && x) {
2413 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2414 return 0;
2415 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2416 return 0;
2417 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2418 }
2419 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002420 return 0;
2421 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002422}
2423
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002424static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002425{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002426 /* Note: unlike access bits, deny bits may be zero. */
2427 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 * Called to check deny when READ with all zero stateid or
2432 * WRITE with all zero or all one stateid
2433 */
Al Virob37ad282006-10-19 23:28:59 -07002434static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2436{
2437 struct inode *ino = current_fh->fh_dentry->d_inode;
2438 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002439 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002440 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 dprintk("NFSD: nfs4_share_conflict\n");
2443
2444 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002445 if (!fp)
2446 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002447 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002449 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2450 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2451 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
NeilBrown13cd2182005-06-23 22:03:10 -07002454 ret = nfs_ok;
2455out:
2456 put_nfs4_file(fp);
2457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002460static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* We're assuming the state code never drops its reference
2463 * without first removing the lease. Since we're in this lease
2464 * callback (and since the lease code is serialized by the kernel
2465 * lock) we know the server hasn't removed the lease yet, we know
2466 * it's safe to take a reference: */
2467 atomic_inc(&dp->dl_count);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Arnd Bergmann460781b2010-11-17 16:26:56 +01002471 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 dp->dl_time = get_seconds();
2473
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002474 nfsd4_cb_recall(dp);
2475}
2476
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002477/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002478static void nfsd_break_deleg_cb(struct file_lock *fl)
2479{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002480 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2481 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002482
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002483 BUG_ON(!fp);
2484 /* We assume break_lease is only called once per lease: */
2485 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002486 /*
2487 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002488 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002489 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002490 */
2491 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002493 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002494 fp->fi_had_conflict = true;
2495 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2496 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002497 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500static
2501int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2502{
2503 if (arg & F_UNLCK)
2504 return lease_modify(onlist, arg);
2505 else
2506 return -EAGAIN;
2507}
2508
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002509static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002510 .lm_break = nfsd_break_deleg_cb,
2511 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
2513
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002514static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2515{
2516 if (nfsd4_has_session(cstate))
2517 return nfs_ok;
2518 if (seqid == so->so_seqid - 1)
2519 return nfserr_replay_me;
2520 if (seqid == so->so_seqid)
2521 return nfs_ok;
2522 return nfserr_bad_seqid;
2523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Al Virob37ad282006-10-19 23:28:59 -07002525__be32
Andy Adamson66689582009-04-03 08:28:45 +03002526nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2527 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 clientid_t *clientid = &open->op_clientid;
2530 struct nfs4_client *clp = NULL;
2531 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002532 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002533 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002536 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if (STALE_CLIENTID(&open->op_clientid))
2539 return nfserr_stale_clientid;
2540
J. Bruce Fields506f2752011-08-11 18:50:18 -04002541 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002542 oo = find_openstateowner_str(strhashval, open);
2543 open->op_openowner = oo;
2544 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002545 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 clp = find_confirmed_client(clientid);
2547 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002548 return nfserr_expired;
2549 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002551 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002552 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002553 clp = oo->oo_owner.so_client;
2554 release_openowner(oo);
2555 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002556 goto renew;
2557 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002558 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002559 if (status)
2560 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002562 if (open->op_openowner == NULL) {
2563 oo = alloc_init_open_stateowner(strhashval, clp, open);
2564 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002565 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002566 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002567 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002568 list_del_init(&oo->oo_close_lru);
2569 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002570 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Al Virob37ad282006-10-19 23:28:59 -07002573static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002574nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2575{
2576 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2577 return nfserr_openmode;
2578 else
2579 return nfs_ok;
2580}
2581
Daniel Mackc47d8322011-05-16 16:38:14 +02002582static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002583{
2584 share_access &= ~NFS4_SHARE_WANT_MASK;
2585
2586 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2587}
2588
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002589static struct nfs4_delegation *find_deleg_stateid(stateid_t *s)
2590{
2591 struct nfs4_stid *ret;
2592
2593 ret = find_stateid_by_type(s, NFS4_DELEG_STID);
2594 if (!ret)
2595 return NULL;
2596 return delegstateid(ret);
2597}
2598
Al Virob37ad282006-10-19 23:28:59 -07002599static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002600nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2601 struct nfs4_delegation **dp)
2602{
2603 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002604 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002605
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002606 *dp = find_deleg_stateid(&open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002607 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002608 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002609 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002610 status = nfs4_check_delegmode(*dp, flags);
2611 if (status)
2612 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002613out:
2614 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2615 return nfs_ok;
2616 if (status)
2617 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002618 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002619 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002620}
2621
Al Virob37ad282006-10-19 23:28:59 -07002622static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002623nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002625 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002626 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
NeilBrown8beefa22005-06-23 22:03:08 -07002628 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* ignore lock owners */
2630 if (local->st_stateowner->so_is_open_owner == 0)
2631 continue;
2632 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002633 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 *stpp = local;
2635 /* check for conflicting share reservations */
2636 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002637 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002639 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002642static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002643nfs4_alloc_stateid(void)
2644{
2645 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2646}
2647
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002648static inline int nfs4_access_to_access(u32 nfs4_access)
2649{
2650 int flags = 0;
2651
2652 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2653 flags |= NFSD_MAY_READ;
2654 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2655 flags |= NFSD_MAY_WRITE;
2656 return flags;
2657}
2658
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002659static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2660 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002661{
2662 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002663 int oflag = nfs4_access_to_omode(open->op_share_access);
2664 int access = nfs4_access_to_access(open->op_share_access);
2665
2666 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2667 * allowing it to break the lease and return EAGAIN leaves the
2668 * client unable to make progress in returning the delegation */
2669 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2670 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002671
2672 if (!fp->fi_fds[oflag]) {
2673 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2674 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002675 if (status)
2676 return status;
2677 }
2678 nfs4_file_get_access(fp, oflag);
2679
2680 return nfs_ok;
2681}
2682
Al Virob37ad282006-10-19 23:28:59 -07002683static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002684nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002685 struct nfs4_file *fp, struct svc_fh *cur_fh,
2686 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002688 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002689 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
NeilBrown5ac049a2005-06-23 22:03:03 -07002691 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002693 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002695 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002696 if (status) {
2697 kmem_cache_free(stateid_slab, stp);
2698 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 *stpp = stp;
2701 return 0;
2702}
2703
Al Virob37ad282006-10-19 23:28:59 -07002704static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2706 struct nfsd4_open *open)
2707{
2708 struct iattr iattr = {
2709 .ia_valid = ATTR_SIZE,
2710 .ia_size = 0,
2711 };
2712 if (!open->op_truncate)
2713 return 0;
2714 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002715 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2717}
2718
Al Virob37ad282006-10-19 23:28:59 -07002719static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002720nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002722 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2723 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002724 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
J. Bruce Fields7d947842010-08-20 18:09:31 -04002726 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002727 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002728 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002729 if (status)
2730 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 status = nfsd4_truncate(rqstp, cur_fh, open);
2733 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002734 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002735 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002736 nfs4_file_put_access(fp, oflag);
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return status;
2739 }
2740 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002741 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002742 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 return nfs_ok;
2745}
2746
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748static void
NeilBrown37515172005-07-07 17:59:16 -07002749nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002751 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002752 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002755/* Should we give out recallable state?: */
2756static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2757{
2758 if (clp->cl_cb_state == NFSD4_CB_UP)
2759 return true;
2760 /*
2761 * In the sessions case, since we don't have to establish a
2762 * separate connection for callbacks, we assume it's OK
2763 * until we hear otherwise:
2764 */
2765 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2766}
2767
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002768static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2769{
2770 struct file_lock *fl;
2771
2772 fl = locks_alloc_lock();
2773 if (!fl)
2774 return NULL;
2775 locks_init_lock(fl);
2776 fl->fl_lmops = &nfsd_lease_mng_ops;
2777 fl->fl_flags = FL_LEASE;
2778 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2779 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002780 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002781 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002782 return fl;
2783}
2784
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002785static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2786{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002787 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002788 struct file_lock *fl;
2789 int status;
2790
2791 fl = nfs4_alloc_init_lease(dp, flag);
2792 if (!fl)
2793 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002794 fl->fl_file = find_readable_file(fp);
2795 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2796 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002797 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002798 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002799 locks_free_lock(fl);
2800 return -ENOMEM;
2801 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002802 fp->fi_lease = fl;
2803 fp->fi_deleg_file = fl->fl_file;
2804 get_file(fp->fi_deleg_file);
2805 atomic_set(&fp->fi_delegees, 1);
2806 list_add(&dp->dl_perfile, &fp->fi_delegations);
2807 return 0;
2808}
2809
2810static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2811{
2812 struct nfs4_file *fp = dp->dl_file;
2813
2814 if (!fp->fi_lease)
2815 return nfs4_setlease(dp, flag);
2816 spin_lock(&recall_lock);
2817 if (fp->fi_had_conflict) {
2818 spin_unlock(&recall_lock);
2819 return -EAGAIN;
2820 }
2821 atomic_inc(&fp->fi_delegees);
2822 list_add(&dp->dl_perfile, &fp->fi_delegations);
2823 spin_unlock(&recall_lock);
2824 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002825 return 0;
2826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/*
2829 * Attempt to hand out a delegation.
2830 */
2831static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002832nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002835 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002836 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int status, flag = 0;
2838
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002839 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002841 open->op_recall = 0;
2842 switch (open->op_claim_type) {
2843 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002844 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002845 open->op_recall = 1;
2846 flag = open->op_delegate_type;
2847 if (flag == NFS4_OPEN_DELEGATE_NONE)
2848 goto out;
2849 break;
2850 case NFS4_OPEN_CLAIM_NULL:
2851 /* Let's not give out any delegations till everyone's
2852 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002853 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002854 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002855 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002856 goto out;
2857 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2858 flag = NFS4_OPEN_DELEGATE_WRITE;
2859 else
2860 flag = NFS4_OPEN_DELEGATE_READ;
2861 break;
2862 default:
2863 goto out;
2864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002866 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002867 if (dp == NULL)
2868 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002869 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002870 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002871 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002873 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002875 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002876 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002878 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2879 && flag == NFS4_OPEN_DELEGATE_NONE
2880 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002881 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002883 return;
2884out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002885 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002886out_no_deleg:
2887 flag = NFS4_OPEN_DELEGATE_NONE;
2888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
2891/*
2892 * called with nfs4_lock_state() held.
2893 */
Al Virob37ad282006-10-19 23:28:59 -07002894__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2896{
Andy Adamson66689582009-04-03 08:28:45 +03002897 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 struct nfs4_file *fp = NULL;
2899 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002900 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002901 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002902 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002905 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002906 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
2908 /*
2909 * Lookup file; if found, lookup stateid and check open request,
2910 * and check for delegations in the process of being recalled.
2911 * If not found, create the nfs4_file struct
2912 */
2913 fp = find_file(ino);
2914 if (fp) {
2915 if ((status = nfs4_check_open(fp, open, &stp)))
2916 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002917 status = nfs4_check_deleg(fp, open, &dp);
2918 if (status)
2919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002921 status = nfserr_bad_stateid;
2922 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2923 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002924 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 fp = alloc_init_file(ino);
2926 if (fp == NULL)
2927 goto out;
2928 }
2929
2930 /*
2931 * OPEN the file, or upgrade an existing OPEN.
2932 * If truncate fails, the OPEN fails.
2933 */
2934 if (stp) {
2935 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002936 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (status)
2938 goto out;
2939 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002940 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002941 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002943 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 status = nfsd4_truncate(rqstp, current_fh, open);
2945 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002946 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto out;
2948 }
2949 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002950 update_stateid(&stp->st_stid.sc_stateid);
2951 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002953 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002954 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /*
2957 * Attempt to hand out a delegation. No error return, because the
2958 * OPEN succeeds even if we fail.
2959 */
2960 nfs4_open_delegation(current_fh, open, stp);
2961
2962 status = nfs_ok;
2963
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002964 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002965 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966out:
NeilBrown13cd2182005-06-23 22:03:10 -07002967 if (fp)
2968 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002969 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2970 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 /*
2972 * To finish the open response, we just need to set the rflags.
2973 */
2974 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002975 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03002976 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2978
2979 return status;
2980}
2981
Al Virob37ad282006-10-19 23:28:59 -07002982__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002983nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2984 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002987 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 nfs4_lock_state();
2990 dprintk("process_renew(%08x/%08x): starting\n",
2991 clid->cl_boot, clid->cl_id);
2992 status = nfserr_stale_clientid;
2993 if (STALE_CLIENTID(clid))
2994 goto out;
2995 clp = find_confirmed_client(clid);
2996 status = nfserr_expired;
2997 if (clp == NULL) {
2998 /* We assume the client took too long to RENEW. */
2999 dprintk("nfsd4_renew: clientid not found!\n");
3000 goto out;
3001 }
3002 renew_client(clp);
3003 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003004 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003005 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto out;
3007 status = nfs_ok;
3008out:
3009 nfs4_unlock_state();
3010 return status;
3011}
3012
Daniel Mackc47d8322011-05-16 16:38:14 +02003013static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003014};
3015
NeilBrowna76b4312005-06-23 22:04:01 -07003016static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003017nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003018{
3019 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003020 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003021 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003022 /*
3023 * Now that every NFSv4 client has had the chance to recover and
3024 * to see the (possibly new, possibly shorter) lease time, we
3025 * can safely set the next grace time to the current lease time:
3026 */
3027 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003028}
3029
NeilBrownfd39ca92005-06-23 22:04:03 -07003030static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031nfs4_laundromat(void)
3032{
3033 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003034 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct nfs4_delegation *dp;
3036 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003037 time_t cutoff = get_seconds() - nfsd4_lease;
3038 time_t t, clientid_val = nfsd4_lease;
3039 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 nfs4_lock_state();
3042
3043 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003044 if (locks_in_grace())
3045 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003046 INIT_LIST_HEAD(&reaplist);
3047 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 list_for_each_safe(pos, next, &client_lru) {
3049 clp = list_entry(pos, struct nfs4_client, cl_lru);
3050 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3051 t = clp->cl_time - cutoff;
3052 if (clientid_val > t)
3053 clientid_val = t;
3054 break;
3055 }
Benny Halevyd7682982010-05-12 00:13:54 +03003056 if (atomic_read(&clp->cl_refcount)) {
3057 dprintk("NFSD: client in use (clientid %08x)\n",
3058 clp->cl_clientid.cl_id);
3059 continue;
3060 }
3061 unhash_client_locked(clp);
3062 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003063 }
3064 spin_unlock(&client_lock);
3065 list_for_each_safe(pos, next, &reaplist) {
3066 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 dprintk("NFSD: purging unused client (clientid %08x)\n",
3068 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003069 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 expire_client(clp);
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 spin_lock(&recall_lock);
3073 list_for_each_safe(pos, next, &del_recall_lru) {
3074 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3075 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3076 u = dp->dl_time - cutoff;
3077 if (test_val > u)
3078 test_val = u;
3079 break;
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 list_move(&dp->dl_recall_lru, &reaplist);
3082 }
3083 spin_unlock(&recall_lock);
3084 list_for_each_safe(pos, next, &reaplist) {
3085 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3086 list_del_init(&dp->dl_recall_lru);
3087 unhash_delegation(dp);
3088 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003089 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003091 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3092 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3093 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (test_val > u)
3095 test_val = u;
3096 break;
3097 }
3098 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003099 oo->oo_owner.so_id);
3100 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3103 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3104 nfs4_unlock_state();
3105 return clientid_val;
3106}
3107
Harvey Harrisona254b242008-02-20 12:49:00 -08003108static struct workqueue_struct *laundry_wq;
3109static void laundromat_main(struct work_struct *);
3110static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3111
3112static void
David Howellsc4028952006-11-22 14:57:56 +00003113laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
3115 time_t t;
3116
3117 t = nfs4_laundromat();
3118 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003119 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003122static struct nfs4_openowner * search_close_lru(stateid_t *s)
NeilBrownf8816512005-07-07 17:59:25 -07003123{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003124 struct nfs4_openowner *local;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003125 struct nfs4_ol_stateid *os;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003127 list_for_each_entry(local, &close_lru, oo_close_lru) {
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003128 os = local->oo_last_closed_stid;
3129 if (same_stateid(&os->st_stid.sc_stateid, s))
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003130 return local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132 return NULL;
3133}
3134
3135static inline int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003136nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003138 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
3141static int
3142STALE_STATEID(stateid_t *stateid)
3143{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003144 if (stateid->si_boot == boot_time)
3145 return 0;
3146 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003147 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003148 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151static inline int
3152access_permit_read(unsigned long access_bmap)
3153{
3154 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3155 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3156 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3157}
3158
3159static inline int
3160access_permit_write(unsigned long access_bmap)
3161{
3162 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3163 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3164}
3165
3166static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003167__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Al Virob37ad282006-10-19 23:28:59 -07003169 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
J. Bruce Fields02921912010-07-29 15:16:59 -04003171 /* For lock stateid's, we test the parent open, not the lock: */
3172 if (stp->st_openstp)
3173 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3175 goto out;
3176 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3177 goto out;
3178 status = nfs_ok;
3179out:
3180 return status;
3181}
3182
Al Virob37ad282006-10-19 23:28:59 -07003183static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3185{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003186 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003188 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003189 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 * conflicting state; so we must wait out the grace period. */
3191 return nfserr_grace;
3192 } else if (flags & WR_STATE)
3193 return nfs4_share_conflict(current_fh,
3194 NFS4_SHARE_DENY_WRITE);
3195 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3196 return nfs4_share_conflict(current_fh,
3197 NFS4_SHARE_DENY_READ);
3198}
3199
3200/*
3201 * Allow READ/WRITE during grace period on recovered state only for files
3202 * that are not able to provide mandatory locking.
3203 */
3204static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003205grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003207 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
J. Bruce Fields81b82962011-08-23 11:03:29 -04003210/* Returns true iff a is later than b: */
3211static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3212{
3213 return (s32)a->si_generation - (s32)b->si_generation > 0;
3214}
3215
J. Bruce Fields28dde242011-08-22 10:07:12 -04003216static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003217{
Andy Adamson66689582009-04-03 08:28:45 +03003218 /*
3219 * When sessions are used the stateid generation number is ignored
3220 * when it is zero.
3221 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003222 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003223 return nfs_ok;
3224
3225 if (in->si_generation == ref->si_generation)
3226 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003227
J. Bruce Fields0836f582008-01-26 19:08:12 -05003228 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003229 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003230 return nfserr_bad_stateid;
3231 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003232 * However, we could see a stateid from the past, even from a
3233 * non-buggy client. For example, if the client sends a lock
3234 * while some IO is outstanding, the lock may bump si_generation
3235 * while the IO is still in flight. The client could avoid that
3236 * situation by waiting for responses on all the IO requests,
3237 * but better performance may result in retrying IO that
3238 * receives an old_stateid error if requests are rarely
3239 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003240 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003241 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003242}
3243
J. Bruce Fields28dde242011-08-22 10:07:12 -04003244__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003245{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003246 struct nfs4_stid *s;
3247 struct nfs4_ol_stateid *ols;
3248 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003249
3250 if (STALE_STATEID(stateid))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003251 return nfserr_stale_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04003252
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003253 s = find_stateid(stateid);
3254 if (!s)
3255 return nfserr_stale_stateid;
3256 status = check_stateid_generation(stateid, &s->sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003257 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003258 return status;
3259 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3260 return nfs_ok;
3261 ols = openlockstateid(s);
3262 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003263 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003264 return nfserr_bad_stateid;
3265 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003266}
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268/*
3269* Checks for stateid operations
3270*/
Al Virob37ad282006-10-19 23:28:59 -07003271__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003272nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3273 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003275 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003276 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003278 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003280 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (filpp)
3283 *filpp = NULL;
3284
J. Bruce Fields18f82732009-02-21 15:23:01 -08003285 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return nfserr_grace;
3287
3288 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3289 return check_special_stateids(current_fh, stateid, flags);
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 status = nfserr_stale_stateid;
3292 if (STALE_STATEID(stateid))
3293 goto out;
3294
J. Bruce Fields33515142010-10-02 18:42:39 -04003295 /*
3296 * We assume that any stateid that has the current boot time,
3297 * but that we can't find, is expired:
3298 */
3299 status = nfserr_expired;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003300 s = find_stateid(stateid);
3301 if (!s)
3302 goto out;
3303 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3304 if (status)
3305 goto out;
3306 if (s->sc_type == NFS4_DELEG_STID) {
3307 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003308 status = nfs4_check_delegmode(dp, flags);
3309 if (status)
3310 goto out;
3311 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003312 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003313 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003314 BUG_ON(!*filpp);
3315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 } else { /* open or lock stateid */
J. Bruce Fields69064a22011-09-09 11:54:57 -04003317 stp = openlockstateid(s);
J. Bruce Fields33515142010-10-02 18:42:39 -04003318 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003319 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003321 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003322 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003324 status = nfs4_check_openmode(stp, flags);
3325 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 goto out;
3327 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003328 if (filpp) {
3329 if (flags & RD_STATE)
3330 *filpp = find_readable_file(stp->st_file);
3331 else
3332 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335 status = nfs_ok;
3336out:
3337 return status;
3338}
3339
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003340static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003341nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003342{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003343 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003344 return nfserr_locks_held;
3345 release_lock_stateid(stp);
3346 return nfs_ok;
3347}
3348
3349/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003350 * Test if the stateid is valid
3351 */
3352__be32
3353nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3354 struct nfsd4_test_stateid *test_stateid)
3355{
3356 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3357 return nfs_ok;
3358}
3359
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003360__be32
3361nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3362 struct nfsd4_free_stateid *free_stateid)
3363{
3364 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003365 struct nfs4_stid *s;
3366 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003367
3368 nfs4_lock_state();
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003369 s = find_stateid(stateid);
3370 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003371 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003372 switch (s->sc_type) {
3373 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003374 ret = nfserr_locks_held;
3375 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003376 case NFS4_OPEN_STID:
3377 case NFS4_LOCK_STID:
3378 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3379 if (ret)
3380 goto out;
3381 if (s->sc_type == NFS4_LOCK_STID)
3382 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3383 else
3384 ret = nfserr_locks_held;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003385 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003386out:
3387 nfs4_unlock_state();
3388 return ret;
3389}
3390
NeilBrown4c4cd222005-07-07 17:59:27 -07003391static inline int
3392setlkflg (int type)
3393{
3394 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3395 RD_STATE : WR_STATE;
3396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003398static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3399{
3400 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3401 return nfserr_bad_stateid;
3402 if (STALE_STATEID(stateid))
3403 return nfserr_stale_stateid;
3404 return nfs_ok;
3405}
3406
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003407static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003408{
3409 struct svc_fh *current_fh = &cstate->current_fh;
3410 struct nfs4_stateowner *sop = stp->st_stateowner;
3411 __be32 status;
3412
3413 if (nfs4_check_fh(current_fh, stp))
3414 return nfserr_bad_stateid;
3415 status = nfsd4_check_seqid(cstate, sop, seqid);
3416 if (status)
3417 return status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003418 return check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003419}
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421/*
3422 * Checks for sequence id mutating operations.
3423 */
Al Virob37ad282006-10-19 23:28:59 -07003424static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003425nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003426 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003427 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003429 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003431 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3432 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003435 status = nfs4_nospecial_stateid_checks(stateid);
3436 if (status)
3437 return status;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003438 *stpp = find_ol_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003439 if (*stpp == NULL)
3440 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003441 cstate->replay_owner = (*stpp)->st_stateowner;
3442 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003444 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3445}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003446
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003447static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003448{
3449 __be32 status;
3450 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003452 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003453 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003454 if (status)
3455 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003456 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003457 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003458 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003459 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
Al Virob37ad282006-10-19 23:28:59 -07003462__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003463nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003464 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Al Virob37ad282006-10-19 23:28:59 -07003466 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003467 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003468 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003471 (int)cstate->current_fh.fh_dentry->d_name.len,
3472 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003474 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003475 if (status)
3476 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 nfs4_lock_state();
3479
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003480 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003481 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003482 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003483 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003484 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003485 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003486 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003487 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003488 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003489 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003490 update_stateid(&stp->st_stid.sc_stateid);
3491 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003492 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003493 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003494
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003495 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003496 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003498 if (!cstate->replay_owner)
3499 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return status;
3501}
3502
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003503static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003508 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3509 nfs4_file_put_access(stp->st_file, i);
3510 __clear_bit(i, &stp->st_access_bmap);
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 }
3513}
3514
3515static void
3516reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3517{
3518 int i;
3519 for (i = 0; i < 4; i++) {
3520 if ((i & deny) != i)
3521 __clear_bit(i, bmap);
3522 }
3523}
3524
Al Virob37ad282006-10-19 23:28:59 -07003525__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003526nfsd4_open_downgrade(struct svc_rqst *rqstp,
3527 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003528 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Al Virob37ad282006-10-19 23:28:59 -07003530 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003531 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003534 (int)cstate->current_fh.fh_dentry->d_name.len,
3535 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003537 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003538 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 return nfserr_inval;
3540
3541 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003542 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3543 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003544 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 status = nfserr_inval;
3547 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3548 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3549 stp->st_access_bmap, od->od_share_access);
3550 goto out;
3551 }
3552 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3553 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3554 stp->st_deny_bmap, od->od_share_deny);
3555 goto out;
3556 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003557 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3560
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003561 update_stateid(&stp->st_stid.sc_stateid);
3562 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 status = nfs_ok;
3564out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003565 if (!cstate->replay_owner)
3566 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return status;
3568}
3569
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003570void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3571{
3572 struct nfs4_openowner *oo;
3573 struct nfs4_ol_stateid *s;
3574
3575 if (!so->so_is_open_owner)
3576 return;
3577 oo = openowner(so);
3578 s = oo->oo_last_closed_stid;
3579 if (!s)
3580 return;
3581 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3582 /* Release the last_closed_stid on the next seqid bump: */
3583 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3584 return;
3585 }
3586 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3587 free_generic_stateid(oo->oo_last_closed_stid);
3588 oo->oo_last_closed_stid = NULL;
3589}
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591/*
3592 * nfs4_unlock_state() called after encode
3593 */
Al Virob37ad282006-10-19 23:28:59 -07003594__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003595nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003596 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Al Virob37ad282006-10-19 23:28:59 -07003598 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003599 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003600 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003603 (int)cstate->current_fh.fh_dentry->d_name.len,
3604 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 nfs4_lock_state();
3607 /* check close_lru for replay */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003608 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3609 &close->cl_stateid, &stp);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003610 if (stp == NULL && status == nfserr_expired) {
3611 /*
3612 * Also, we should make sure this isn't just the result of
3613 * a replayed close:
3614 */
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003615 oo = search_close_lru(&close->cl_stateid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003616 /* It's not stale; let's assume it's expired: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003617 if (oo == NULL)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003618 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003619 cstate->replay_owner = &oo->oo_owner;
3620 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003621 if (status)
3622 goto out;
3623 status = nfserr_bad_seqid;
3624 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003625 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003627 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003629 update_stateid(&stp->st_stid.sc_stateid);
3630 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003632 /* unhash_open_stateid() calls nfsd_close() if needed */
3633 oo->oo_last_closed_stid = stp;
3634 unhash_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003635
3636 /* place unused nfs4_stateowners on so_close_lru list to be
3637 * released by the laundromat service after the lease period
3638 * to enable us to handle CLOSE replay
3639 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003640 if (list_empty(&oo->oo_owner.so_stateids))
3641 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003643 if (!cstate->replay_owner)
3644 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return status;
3646}
3647
Al Virob37ad282006-10-19 23:28:59 -07003648__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003649nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3650 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003652 struct nfs4_delegation *dp;
3653 stateid_t *stateid = &dr->dr_stateid;
3654 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003655 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003657 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003658 return status;
3659 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003662 status = nfserr_bad_stateid;
3663 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3664 goto out;
3665 status = nfserr_stale_stateid;
3666 if (STALE_STATEID(stateid))
3667 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003668 status = nfserr_expired;
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003669 dp = find_deleg_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003670 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003671 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003672 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003673 if (status)
3674 goto out;
3675 renew_client(dp->dl_client);
3676
3677 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003679 nfs4_unlock_state();
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return status;
3682}
3683
3684
3685/*
3686 * Lock owner state (byte-range locks)
3687 */
3688#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3689#define LOCK_HASH_BITS 8
3690#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3691#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3692
Benny Halevy87df4de2008-12-15 19:42:03 +02003693static inline u64
3694end_offset(u64 start, u64 len)
3695{
3696 u64 end;
3697
3698 end = start + len;
3699 return end >= start ? end: NFS4_MAX_UINT64;
3700}
3701
3702/* last octet in a range */
3703static inline u64
3704last_byte_offset(u64 start, u64 len)
3705{
3706 u64 end;
3707
3708 BUG_ON(!len);
3709 end = start + len;
3710 return end > start ? end - 1: NFS4_MAX_UINT64;
3711}
3712
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04003713static unsigned int lockownerid_hashval(u32 id)
3714{
3715 return id & LOCK_HASH_MASK;
3716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
3718static inline unsigned int
3719lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3720 struct xdr_netobj *ownername)
3721{
3722 return (file_hashval(inode) + cl_id
3723 + opaque_hashval(ownername->data, ownername->len))
3724 & LOCK_HASH_MASK;
3725}
3726
3727static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3728static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730/*
3731 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3732 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3733 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3734 * locking, this prevents us from being completely protocol-compliant. The
3735 * real solution to this problem is to start using unsigned file offsets in
3736 * the VFS, but this is a very deep change!
3737 */
3738static inline void
3739nfs4_transform_lock_offset(struct file_lock *lock)
3740{
3741 if (lock->fl_start < 0)
3742 lock->fl_start = OFFSET_MAX;
3743 if (lock->fl_end < 0)
3744 lock->fl_end = OFFSET_MAX;
3745}
3746
NeilBrownd5b90262006-04-10 22:55:22 -07003747/* Hack!: For now, we're defining this just so we can use a pointer to it
3748 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003749static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003750};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752static inline void
3753nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3754{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003755 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
NeilBrownd5b90262006-04-10 22:55:22 -07003757 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003758 lo = (struct nfs4_lockowner *) fl->fl_owner;
3759 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3760 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003761 if (!deny->ld_owner.data)
3762 /* We just don't care that much */
3763 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003764 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3765 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003766 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003767nevermind:
3768 deny->ld_owner.len = 0;
3769 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003770 deny->ld_clientid.cl_boot = 0;
3771 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
3773 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003774 deny->ld_length = NFS4_MAX_UINT64;
3775 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3777 deny->ld_type = NFS4_READ_LT;
3778 if (fl->fl_type != F_RDLCK)
3779 deny->ld_type = NFS4_WRITE_LT;
3780}
3781
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003782static struct nfs4_lockowner *
3783find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 struct xdr_netobj *owner)
3785{
3786 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3787 struct nfs4_stateowner *op;
3788
3789 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003790 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003791 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793 return NULL;
3794}
3795
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003796static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003797{
3798 unsigned int idhashval;
3799
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003800 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3801 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3802 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3803 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806/*
3807 * Alloc a lock owner structure.
3808 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003809 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 *
3811 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 */
3813
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003814static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003815alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003816 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003818 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3819 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003821 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3822 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003823 /* It is the openowner seqid that will be incremented in encode in the
3824 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003825 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3826 hash_lockowner(lo, strhashval, clp, open_stp);
3827 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003830static struct nfs4_ol_stateid *
3831alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003833 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
NeilBrown5ac049a2005-06-23 22:03:03 -07003835 stp = nfs4_alloc_stateid();
3836 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 goto out;
NeilBrown8beefa22005-06-23 22:03:08 -07003838 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003839 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3840 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003841 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003842 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003844 stp->st_stid.sc_stateid.si_boot = boot_time;
3845 stp->st_stid.sc_stateid.si_stateownerid = lo->lo_owner.so_id;
3846 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003847 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003848 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04003849 hash_stid(&stp->st_stid);
J. Bruce Fields0997b172011-03-02 18:01:35 -05003850 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003852 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854out:
3855 return stp;
3856}
3857
NeilBrownfd39ca92005-06-23 22:04:03 -07003858static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859check_lock_length(u64 offset, u64 length)
3860{
Benny Halevy87df4de2008-12-15 19:42:03 +02003861 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 LOFF_OVERFLOW(offset, length)));
3863}
3864
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003865static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003866{
3867 struct nfs4_file *fp = lock_stp->st_file;
3868 int oflag = nfs4_access_to_omode(access);
3869
3870 if (test_bit(access, &lock_stp->st_access_bmap))
3871 return;
3872 nfs4_file_get_access(fp, oflag);
3873 __set_bit(access, &lock_stp->st_access_bmap);
3874}
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876/*
3877 * LOCK operation
3878 */
Al Virob37ad282006-10-19 23:28:59 -07003879__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003880nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003881 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003883 struct nfs4_openowner *open_sop = NULL;
3884 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003885 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003886 struct nfs4_file *fp;
3887 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003889 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003890 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003892 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003893 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
3895 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3896 (long long) lock->lk_offset,
3897 (long long) lock->lk_length);
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (check_lock_length(lock->lk_offset, lock->lk_length))
3900 return nfserr_inval;
3901
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003902 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003903 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003904 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3905 return status;
3906 }
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 nfs4_lock_state();
3909
3910 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003911 /*
3912 * Client indicates that this is a new lockowner.
3913 * Use open owner and open stateid to create lock owner and
3914 * lock stateid.
3915 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003916 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003919 if (!nfsd4_has_session(cstate) &&
3920 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003924 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 lock->lk_new_open_seqid,
3926 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003927 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003928 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003930 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003931 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003932 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003933 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003934 &lock->v.new.clientid))
3935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 /* create lockowner and lock stateid */
3937 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003938 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3939 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003941 /* XXX: Do we need to check for duplicate stateowners on
3942 * the same file, or should they just be allowed (and
3943 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003944 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003945 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003946 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003947 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003949 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003950 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 } else {
3953 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003954 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003955 lock->lk_old_lock_seqid,
3956 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003957 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (status)
3959 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003960 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04003961 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003963 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003965 lkflg = setlkflg(lock->lk_type);
3966 status = nfs4_check_openmode(lock_stp, lkflg);
3967 if (status)
3968 goto out;
3969
NeilBrown0dd395d2005-07-07 17:59:15 -07003970 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003971 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003972 goto out;
3973 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003974 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003975 goto out;
3976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 locks_init_lock(&file_lock);
3978 switch (lock->lk_type) {
3979 case NFS4_READ_LT:
3980 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003981 filp = find_readable_file(lock_stp->st_file);
3982 if (filp)
3983 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 case NFS4_WRITE_LT:
3987 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05003988 filp = find_writeable_file(lock_stp->st_file);
3989 if (filp)
3990 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05003992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 default:
3994 status = nfserr_inval;
3995 goto out;
3996 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003997 if (!filp) {
3998 status = nfserr_openmode;
3999 goto out;
4000 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004001 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 file_lock.fl_pid = current->tgid;
4003 file_lock.fl_file = filp;
4004 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004005 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004008 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 nfs4_transform_lock_offset(&file_lock);
4010
4011 /*
4012 * Try to lock the file in the VFS.
4013 * Note: locks.c uses the BKL to protect the inode's lock list.
4014 */
4015
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004016 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004017 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004019 update_stateid(&lock_stp->st_stid.sc_stateid);
4020 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004022 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004023 break;
4024 case (EAGAIN): /* conflock holds conflicting lock */
4025 status = nfserr_denied;
4026 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4027 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 case (EDEADLK):
4030 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004031 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004032 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004033 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004034 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004038 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004039 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004040 if (!cstate->replay_owner)
4041 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return status;
4043}
4044
4045/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004046 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4047 * so we do a temporary open here just to get an open file to pass to
4048 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4049 * inode operation.)
4050 */
4051static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4052{
4053 struct file *file;
4054 int err;
4055
4056 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4057 if (err)
4058 return err;
4059 err = vfs_test_lock(file, lock);
4060 nfsd_close(file);
4061 return err;
4062}
4063
4064/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 * LOCKT operation
4066 */
Al Virob37ad282006-10-19 23:28:59 -07004067__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004068nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4069 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004073 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004074 int error;
Al Virob37ad282006-10-19 23:28:59 -07004075 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004077 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return nfserr_grace;
4079
4080 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4081 return nfserr_inval;
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 nfs4_lock_state();
4084
4085 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004086 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004089 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004092 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 locks_init_lock(&file_lock);
4094 switch (lockt->lt_type) {
4095 case NFS4_READ_LT:
4096 case NFS4_READW_LT:
4097 file_lock.fl_type = F_RDLCK;
4098 break;
4099 case NFS4_WRITE_LT:
4100 case NFS4_WRITEW_LT:
4101 file_lock.fl_type = F_WRLCK;
4102 break;
4103 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004104 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 status = nfserr_inval;
4106 goto out;
4107 }
4108
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4110 if (lo)
4111 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 file_lock.fl_pid = current->tgid;
4113 file_lock.fl_flags = FL_POSIX;
4114
4115 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004116 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 nfs4_transform_lock_offset(&file_lock);
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004121 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004122 if (error) {
4123 status = nfserrno(error);
4124 goto out;
4125 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004126 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004128 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 }
4130out:
4131 nfs4_unlock_state();
4132 return status;
4133}
4134
Al Virob37ad282006-10-19 23:28:59 -07004135__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004136nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004137 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004139 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 struct file *filp = NULL;
4141 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004142 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4146 (long long) locku->lu_offset,
4147 (long long) locku->lu_length);
4148
4149 if (check_lock_length(locku->lu_offset, locku->lu_length))
4150 return nfserr_inval;
4151
4152 nfs4_lock_state();
4153
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004154 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004155 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004156 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004158 filp = find_any_file(stp->st_file);
4159 if (!filp) {
4160 status = nfserr_lock_range;
4161 goto out;
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 BUG_ON(!filp);
4164 locks_init_lock(&file_lock);
4165 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004166 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 file_lock.fl_pid = current->tgid;
4168 file_lock.fl_file = filp;
4169 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004170 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 file_lock.fl_start = locku->lu_offset;
4172
Benny Halevy87df4de2008-12-15 19:42:03 +02004173 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 nfs4_transform_lock_offset(&file_lock);
4175
4176 /*
4177 * Try to unlock the file in the VFS.
4178 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004179 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004180 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004181 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 goto out_nfserr;
4183 }
4184 /*
4185 * OK, unlock succeeded; the only thing left to do is update the stateid.
4186 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004187 update_stateid(&stp->st_stid.sc_stateid);
4188 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 nfs4_unlock_state();
4192 return status;
4193
4194out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004195 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 goto out;
4197}
4198
4199/*
4200 * returns
4201 * 1: locks held by lockowner
4202 * 0: no locks held by lockowner
4203 */
4204static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004205check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
4207 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004208 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 int status = 0;
4210
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004211 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004213 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 status = 1;
4215 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
4218out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004219 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
Al Virob37ad282006-10-19 23:28:59 -07004223__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004224nfsd4_release_lockowner(struct svc_rqst *rqstp,
4225 struct nfsd4_compound_state *cstate,
4226 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
4228 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004229 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004230 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004231 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004233 struct list_head matches;
4234 int i;
Al Virob37ad282006-10-19 23:28:59 -07004235 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4238 clid->cl_boot, clid->cl_id);
4239
4240 /* XXX check for lease expiration */
4241
4242 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004243 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 nfs4_lock_state();
4247
NeilBrown3e9e3db2005-06-23 22:04:20 -07004248 status = nfserr_locks_held;
4249 /* XXX: we're doing a linear search through all the lockowners.
4250 * Yipes! For now we'll just hope clients aren't really using
4251 * release_lockowner much, but eventually we have to fix these
4252 * data structures. */
4253 INIT_LIST_HEAD(&matches);
4254 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4255 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004256 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004257 continue;
4258 list_for_each_entry(stp, &sop->so_stateids,
4259 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004260 lo = lockowner(sop);
4261 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004262 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004263 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004266 }
4267 /* Clients probably won't expect us to return with some (but not all)
4268 * of the lockowner state released; so don't release any until all
4269 * have been checked. */
4270 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004271 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004272 lo = list_entry(matches.next, struct nfs4_lockowner,
4273 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004274 /* unhash_stateowner deletes so_perclient only
4275 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004276 list_del(&lo->lo_list);
4277 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279out:
4280 nfs4_unlock_state();
4281 return status;
4282}
4283
4284static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004285alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
NeilBrowna55370a2005-06-23 22:03:52 -07004287 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
NeilBrownc7b9a452005-06-23 22:04:30 -07004290int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004291nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004292{
4293 unsigned int strhashval = clientstr_hashval(name);
4294 struct nfs4_client *clp;
4295
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004296 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004297 return clp ? 1 : 0;
4298}
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300/*
4301 * failure => all reset bets are off, nfserr_no_grace...
4302 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004303int
4304nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 unsigned int strhashval;
4307 struct nfs4_client_reclaim *crp = NULL;
4308
NeilBrowna55370a2005-06-23 22:03:52 -07004309 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4310 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (!crp)
4312 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004313 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 INIT_LIST_HEAD(&crp->cr_strhash);
4315 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004316 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 reclaim_str_hashtbl_size++;
4318 return 1;
4319}
4320
4321static void
4322nfs4_release_reclaim(void)
4323{
4324 struct nfs4_client_reclaim *crp = NULL;
4325 int i;
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4328 while (!list_empty(&reclaim_str_hashtbl[i])) {
4329 crp = list_entry(reclaim_str_hashtbl[i].next,
4330 struct nfs4_client_reclaim, cr_strhash);
4331 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 kfree(crp);
4333 reclaim_str_hashtbl_size--;
4334 }
4335 }
4336 BUG_ON(reclaim_str_hashtbl_size);
4337}
4338
4339/*
4340 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004341static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342nfs4_find_reclaim_client(clientid_t *clid)
4343{
4344 unsigned int strhashval;
4345 struct nfs4_client *clp;
4346 struct nfs4_client_reclaim *crp = NULL;
4347
4348
4349 /* find clientid in conf_id_hashtbl */
4350 clp = find_confirmed_client(clid);
4351 if (clp == NULL)
4352 return NULL;
4353
NeilBrowna55370a2005-06-23 22:03:52 -07004354 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4355 clp->cl_name.len, clp->cl_name.data,
4356 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004359 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004361 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return crp;
4363 }
4364 }
4365 return NULL;
4366}
4367
4368/*
4369* Called from OPEN. Look for clientid in reclaim list.
4370*/
Al Virob37ad282006-10-19 23:28:59 -07004371__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372nfs4_check_open_reclaim(clientid_t *clid)
4373{
NeilBrowndfc83562005-06-23 22:03:16 -07004374 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375}
4376
NeilBrownac4d8ff22005-06-23 22:03:30 -07004377/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004379int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004380nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004382 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004384 status = nfsd4_init_slabs();
4385 if (status)
4386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4388 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4389 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4390 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4391 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004392 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004394 for (i = 0; i < SESSION_HASH_SIZE; i++)
4395 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 for (i = 0; i < FILE_HASH_SIZE; i++) {
4397 INIT_LIST_HEAD(&file_hashtbl[i]);
4398 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004399 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4400 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4401 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004403 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4406 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4407 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 INIT_LIST_HEAD(&close_lru);
4411 INIT_LIST_HEAD(&client_lru);
4412 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004413 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004414 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004415}
4416
NeilBrown190e4fb2005-06-23 22:04:25 -07004417static void
4418nfsd4_load_reboot_recovery_data(void)
4419{
4420 int status;
4421
NeilBrown0964a3d2005-06-23 22:04:32 -07004422 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004423 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004424 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004425 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004426 if (status)
4427 printk("NFSD: Failure reading reboot recovery data\n");
4428}
4429
Meelap Shahc2f1a552007-07-17 04:04:39 -07004430/*
4431 * Since the lifetime of a delegation isn't limited to that of an open, a
4432 * client may quite reasonably hang on to a delegation as long as it has
4433 * the inode cached. This becomes an obvious problem the first time a
4434 * client's inode cache approaches the size of the server's total memory.
4435 *
4436 * For now we avoid this problem by imposing a hard limit on the number
4437 * of delegations, which varies according to the server's memory size.
4438 */
4439static void
4440set_max_delegations(void)
4441{
4442 /*
4443 * Allow at most 4 delegations per megabyte of RAM. Quick
4444 * estimates suggest that in the worst case (where every delegation
4445 * is for a different inode), a delegation could take about 1.5K,
4446 * giving a worst case usage of about 6% of memory.
4447 */
4448 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4449}
4450
NeilBrownac4d8ff22005-06-23 22:03:30 -07004451/* initialization to perform when the nfsd service is started: */
4452
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004453static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004454__nfs4_state_start(void)
4455{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004456 int ret;
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004459 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004460 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004461 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004462 ret = set_callback_cred();
4463 if (ret)
4464 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004465 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004466 if (laundry_wq == NULL)
4467 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004468 ret = nfsd4_create_callback_queue();
4469 if (ret)
4470 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004471 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004472 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004473 return 0;
4474out_free_laundry:
4475 destroy_workqueue(laundry_wq);
4476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004479int
NeilBrown76a35502005-06-23 22:03:26 -07004480nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
NeilBrown190e4fb2005-06-23 22:04:25 -07004482 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004483 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486static void
4487__nfs4_state_shutdown(void)
4488{
4489 int i;
4490 struct nfs4_client *clp = NULL;
4491 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct list_head *pos, *next, reaplist;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4495 while (!list_empty(&conf_id_hashtbl[i])) {
4496 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4497 expire_client(clp);
4498 }
4499 while (!list_empty(&unconf_str_hashtbl[i])) {
4500 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4501 expire_client(clp);
4502 }
4503 }
4504 INIT_LIST_HEAD(&reaplist);
4505 spin_lock(&recall_lock);
4506 list_for_each_safe(pos, next, &del_recall_lru) {
4507 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4508 list_move(&dp->dl_recall_lru, &reaplist);
4509 }
4510 spin_unlock(&recall_lock);
4511 list_for_each_safe(pos, next, &reaplist) {
4512 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4513 list_del_init(&dp->dl_recall_lru);
4514 unhash_delegation(dp);
4515 }
4516
NeilBrown190e4fb2005-06-23 22:04:25 -07004517 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518}
4519
4520void
4521nfs4_state_shutdown(void)
4522{
Tejun Heoafe2c512010-12-14 16:21:17 +01004523 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004524 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004525 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 nfs4_lock_state();
4527 nfs4_release_reclaim();
4528 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004530 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531}