blob: d0bb5a5613a9dcb429c1bf4853df8c14962f4b00 [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 */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -040063static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040065static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
J. Bruce Fields8b671b82009-02-22 14:51:34 -080067/* Locking: */
68
69/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080070static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
J. Bruce Fields8b671b82009-02-22 14:51:34 -080072/*
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
76 */
77static DEFINE_SPINLOCK(recall_lock);
78
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040079static struct kmem_cache *openowner_slab = NULL;
80static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *file_slab = NULL;
82static struct kmem_cache *stateid_slab = NULL;
83static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085void
86nfs4_lock_state(void)
87{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080088 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91void
92nfs4_unlock_state(void)
93{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080094 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static inline u32
98opaque_hashval(const void *ptr, int nbytes)
99{
100 unsigned char *cptr = (unsigned char *) ptr;
101
102 u32 x = 0;
103 while (nbytes--) {
104 x *= 37;
105 x += *cptr++;
106 }
107 return x;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static struct list_head del_recall_lru;
111
NeilBrown13cd2182005-06-23 22:03:10 -0700112static inline void
113put_nfs4_file(struct nfs4_file *fi)
114{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800115 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
116 list_del(&fi->fi_hash);
117 spin_unlock(&recall_lock);
118 iput(fi->fi_inode);
119 kmem_cache_free(file_slab, fi);
120 }
NeilBrown13cd2182005-06-23 22:03:10 -0700121}
122
123static inline void
124get_nfs4_file(struct nfs4_file *fi)
125{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800126 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700127}
128
NeilBrownef0f3392006-04-10 22:55:41 -0700129static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700130unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700131
132/*
133 * Open owner state (share locks)
134 */
135
J. Bruce Fields506f2752011-08-11 18:50:18 -0400136/* hash tables for open owners */
137#define OPEN_OWNER_HASH_BITS 8
138#define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139#define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700140
J. Bruce Fields506f2752011-08-11 18:50:18 -0400141static unsigned int open_ownerid_hashval(const u32 id)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400142{
J. Bruce Fields506f2752011-08-11 18:50:18 -0400143 return id & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400144}
145
J. Bruce Fields506f2752011-08-11 18:50:18 -0400146static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400147{
148 unsigned int ret;
149
150 ret = opaque_hashval(ownername->data, ownername->len);
151 ret += clientid;
J. Bruce Fields506f2752011-08-11 18:50:18 -0400152 return ret & OPEN_OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400153}
NeilBrownef0f3392006-04-10 22:55:41 -0700154
J. Bruce Fields506f2752011-08-11 18:50:18 -0400155static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
156static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700157
158/* hash table for nfs4_file */
159#define FILE_HASH_BITS 8
160#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800161
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400162/* hash table for (open)nfs4_ol_stateid */
NeilBrownef0f3392006-04-10 22:55:41 -0700163#define STATEID_HASH_BITS 10
164#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
166
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400167static unsigned int file_hashval(struct inode *ino)
168{
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino, FILE_HASH_BITS);
171}
172
173static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
174{
175 return (owner_id + file_id) & STATEID_HASH_MASK;
176}
NeilBrownef0f3392006-04-10 22:55:41 -0700177
178static struct list_head file_hashtbl[FILE_HASH_SIZE];
179static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
180
J. Bruce Fields998db522010-08-07 09:21:41 -0400181static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400182{
183 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
184 atomic_inc(&fp->fi_access[oflag]);
185}
186
J. Bruce Fields998db522010-08-07 09:21:41 -0400187static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
188{
189 if (oflag == O_RDWR) {
190 __nfs4_file_get_access(fp, O_RDONLY);
191 __nfs4_file_get_access(fp, O_WRONLY);
192 } else
193 __nfs4_file_get_access(fp, oflag);
194}
195
196static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400197{
198 if (fp->fi_fds[oflag]) {
199 fput(fp->fi_fds[oflag]);
200 fp->fi_fds[oflag] = NULL;
201 }
202}
203
J. Bruce Fields998db522010-08-07 09:21:41 -0400204static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400205{
206 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
207 nfs4_file_put_fd(fp, O_RDWR);
208 nfs4_file_put_fd(fp, oflag);
209 }
210}
211
J. Bruce Fields998db522010-08-07 09:21:41 -0400212static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
213{
214 if (oflag == O_RDWR) {
215 __nfs4_file_put_access(fp, O_RDONLY);
216 __nfs4_file_put_access(fp, O_WRONLY);
217 } else
218 __nfs4_file_put_access(fp, oflag);
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400222alloc_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 -0700223{
224 struct nfs4_delegation *dp;
225 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400228 /*
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
232 */
233 if (type != NFS4_OPEN_DELEGATE_READ)
234 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700235 if (fp->fi_had_conflict)
236 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700237 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700238 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700239 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
240 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700242 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700243 INIT_LIST_HEAD(&dp->dl_perfile);
244 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 INIT_LIST_HEAD(&dp->dl_recall_lru);
246 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700247 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dp->dl_type = type;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -0400250 dp->dl_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dp->dl_stateid.si_stateownerid = current_delegid++;
252 dp->dl_stateid.si_fileid = 0;
J. Bruce Fields73997dc2011-08-31 15:47:21 -0400253 dp->dl_stateid.si_generation = 1;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500254 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dp->dl_time = 0;
256 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500257 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return dp;
259}
260
261void
262nfs4_put_delegation(struct nfs4_delegation *dp)
263{
264 if (atomic_dec_and_test(&dp->dl_count)) {
265 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700266 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700267 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700268 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270}
271
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500272static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500274 if (atomic_dec_and_test(&fp->fi_delegees)) {
275 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
276 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400277 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500278 fp->fi_deleg_file = NULL;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/* Called under the state lock. */
283static void
284unhash_delegation(struct nfs4_delegation *dp)
285{
NeilBrownea1da632005-06-23 22:04:17 -0700286 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500288 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 list_del_init(&dp->dl_recall_lru);
290 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500291 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 nfs4_put_delegation(dp);
293}
294
295/*
296 * SETCLIENTID state
297 */
298
Benny Halevy36acb662010-05-12 00:13:04 +0300299/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300300static DEFINE_SPINLOCK(client_lock);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* Hash tables for nfs4_clientid state */
303#define CLIENT_HASH_BITS 4
304#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
306
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400307static unsigned int clientid_hashval(u32 id)
308{
309 return id & CLIENT_HASH_MASK;
310}
311
312static unsigned int clientstr_hashval(const char *name)
313{
314 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
320 *
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
323 *
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
325 * setclientid info.
326 *
327 * client_lru holds client queue ordered by nfs4_client.cl_time
328 * for lease renewal.
329 *
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
332 */
333static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
334static int reclaim_str_hashtbl_size = 0;
335static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
336static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
337static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
338static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
339static struct list_head client_lru;
340static struct list_head close_lru;
341
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400342/*
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
350 *
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
353 *
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
357 *
358 * which we should reject.
359 */
360static void
361set_access(unsigned int *access, unsigned long bmap) {
362 int i;
363
364 *access = 0;
365 for (i = 1; i < 4; i++) {
366 if (test_bit(i, &bmap))
367 *access |= i;
368 }
369}
370
371static void
372set_deny(unsigned int *deny, unsigned long bmap) {
373 int i;
374
375 *deny = 0;
376 for (i = 0; i < 4; i++) {
377 if (test_bit(i, &bmap))
378 *deny |= i ;
379 }
380}
381
382static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400383test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400384 unsigned int access, deny;
385
386 set_access(&access, stp->st_access_bmap);
387 set_deny(&deny, stp->st_deny_bmap);
388 if ((access & open->op_share_deny) || (deny & open->op_share_access))
389 return 0;
390 return 1;
391}
392
393static int nfs4_access_to_omode(u32 access)
394{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400395 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400396 case NFS4_SHARE_ACCESS_READ:
397 return O_RDONLY;
398 case NFS4_SHARE_ACCESS_WRITE:
399 return O_WRONLY;
400 case NFS4_SHARE_ACCESS_BOTH:
401 return O_RDWR;
402 }
403 BUG();
404}
405
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400406static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500407{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400408 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
411}
412
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400413static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500414{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400415 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500416
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800417 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400418 for (i = 1; i < 4; i++) {
419 if (test_bit(i, &stp->st_access_bmap))
420 nfs4_file_put_access(stp->st_file,
421 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400422 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400423 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800424 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400425 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400426 stp->st_file = NULL;
427}
428
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400429static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400430{
431 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500432 kmem_cache_free(stateid_slab, stp);
433}
434
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400435static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500436{
437 struct file *file;
438
439 unhash_generic_stateid(stp);
440 file = find_any_file(stp->st_file);
441 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400442 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500443 free_generic_stateid(stp);
444}
445
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400446static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500447{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400448 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500449
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400450 list_del(&lo->lo_owner.so_idhash);
451 list_del(&lo->lo_owner.so_strhash);
452 list_del(&lo->lo_perstateid);
453 while (!list_empty(&lo->lo_owner.so_stateids)) {
454 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400455 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500456 release_lock_stateid(stp);
457 }
458}
459
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400460static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500461{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400462 unhash_lockowner(lo);
463 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500464}
465
466static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400467release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500468{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400469 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500470
471 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400472 lo = list_entry(open_stp->st_lockowners.next,
473 struct nfs4_lockowner, lo_perstateid);
474 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500475 }
476}
477
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400478static void release_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500479{
480 unhash_generic_stateid(stp);
481 release_stateid_lockowners(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500482 free_generic_stateid(stp);
483}
484
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400485static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500486{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400487 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500488
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400489 list_del(&oo->oo_owner.so_idhash);
490 list_del(&oo->oo_owner.so_strhash);
491 list_del(&oo->oo_perclient);
492 while (!list_empty(&oo->oo_owner.so_stateids)) {
493 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400494 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500495 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500496 }
497}
498
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400499static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500500{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400501 unhash_openowner(oo);
502 list_del(&oo->oo_close_lru);
503 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500504}
505
Marc Eshel5282fd72009-04-03 08:27:52 +0300506#define SESSION_HASH_SIZE 512
507static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
508
509static inline int
510hash_sessionid(struct nfs4_sessionid *sessionid)
511{
512 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
513
514 return sid->sequence % SESSION_HASH_SIZE;
515}
516
517static inline void
518dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
519{
520 u32 *ptr = (u32 *)(&sessionid->data[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
522}
523
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300524static void
525gen_sessionid(struct nfsd4_session *ses)
526{
527 struct nfs4_client *clp = ses->se_client;
528 struct nfsd4_sessionid *sid;
529
530 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
531 sid->clientid = clp->cl_clientid;
532 sid->sequence = current_sessionid++;
533 sid->reserved = 0;
534}
535
536/*
Andy Adamsona6496372009-08-28 08:45:01 -0400537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300547 */
Andy Adamsona6496372009-08-28 08:45:01 -0400548#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
549
Andy Adamson557ce262009-08-28 08:45:04 -0400550static void
551free_session_slots(struct nfsd4_session *ses)
552{
553 int i;
554
555 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
556 kfree(ses->se_slots[i]);
557}
558
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400559/*
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
562 */
563static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
564{
565 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
566}
567
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400568static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300569{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400570 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
571 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300572
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400573 return size;
574}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300575
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400576/*
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
580 */
581static int nfsd4_get_drc_mem(int slotsize, u32 num)
582{
583 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300584
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400585 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400586
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400587 spin_lock(&nfsd_drc_lock);
588 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
589 nfsd_drc_max_mem - nfsd_drc_mem_used);
590 num = min_t(int, num, avail / slotsize);
591 nfsd_drc_mem_used += num * slotsize;
592 spin_unlock(&nfsd_drc_lock);
593
594 return num;
595}
596
597static void nfsd4_put_drc_mem(int slotsize, int num)
598{
599 spin_lock(&nfsd_drc_lock);
600 nfsd_drc_mem_used -= slotsize * num;
601 spin_unlock(&nfsd_drc_lock);
602}
603
604static struct nfsd4_session *alloc_session(int slotsize, int numslots)
605{
606 struct nfsd4_session *new;
607 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300608
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
611 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400612
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300614 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400615 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400616 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400617 for (i = 0; i < numslots; i++) {
618 mem = sizeof(struct nfsd4_slot) + slotsize;
619 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
620 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400621 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400622 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400623 return new;
624out_free:
625 while (i--)
626 kfree(new->se_slots[i]);
627 kfree(new);
628 return NULL;
629}
630
631static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
632{
633 u32 maxrpc = nfsd_serv->sv_max_mesg;
634
635 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800636 new->maxresp_cached = min_t(u32, req->maxresp_cached,
637 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400638 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
639 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
640 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
641}
642
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400643static void free_conn(struct nfsd4_conn *c)
644{
645 svc_xprt_put(c->cn_xprt);
646 kfree(c);
647}
648
649static void nfsd4_conn_lost(struct svc_xpt_user *u)
650{
651 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
652 struct nfs4_client *clp = c->cn_session->se_client;
653
654 spin_lock(&clp->cl_lock);
655 if (!list_empty(&c->cn_persession)) {
656 list_del(&c->cn_persession);
657 free_conn(c);
658 }
659 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500660 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400661}
662
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400663static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400664{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400665 struct nfsd4_conn *conn;
666
667 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
668 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400669 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400670 svc_xprt_get(rqstp->rq_xprt);
671 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400672 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400673 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
674 return conn;
675}
676
J. Bruce Fields328ead22010-09-29 16:11:06 -0400677static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
678{
679 conn->cn_session = ses;
680 list_add(&conn->cn_persession, &ses->se_conns);
681}
682
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400683static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
684{
685 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400686
687 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400688 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400689 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400690}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400691
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400692static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400693{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400694 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400695 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400696}
697
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400698static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400699{
700 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400701 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400702
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400703 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400704 if (!conn)
705 return nfserr_jukebox;
706 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400707 ret = nfsd4_register_conn(conn);
708 if (ret)
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400711 return nfs_ok;
712}
713
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400714static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
715{
716 u32 dir = NFS4_CDFC4_FORE;
717
718 if (ses->se_flags & SESSION4_BACK_CHAN)
719 dir |= NFS4_CDFC4_BACK;
720
721 return nfsd4_new_conn(rqstp, ses, dir);
722}
723
724/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400725static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400726{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400727 struct nfs4_client *clp = s->se_client;
728 struct nfsd4_conn *c;
729
730 spin_lock(&clp->cl_lock);
731 while (!list_empty(&s->se_conns)) {
732 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
733 list_del_init(&c->cn_persession);
734 spin_unlock(&clp->cl_lock);
735
736 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
737 free_conn(c);
738
739 spin_lock(&clp->cl_lock);
740 }
741 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400742}
743
744void free_session(struct kref *kref)
745{
746 struct nfsd4_session *ses;
747 int mem;
748
749 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400750 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400751 spin_lock(&nfsd_drc_lock);
752 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
753 nfsd_drc_mem_used -= mem;
754 spin_unlock(&nfsd_drc_lock);
755 free_session_slots(ses);
756 kfree(ses);
757}
758
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400759static 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 -0400760{
761 struct nfsd4_session *new;
762 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
763 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400764 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400765 int idx;
766
767 /*
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
773 */
774 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
775 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800776 if (numslots < 1)
777 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400778
779 new = alloc_session(slotsize, numslots);
780 if (!new) {
781 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400782 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400783 }
784 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400785
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300786 new->se_client = clp;
787 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300788
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400789 INIT_LIST_HEAD(&new->se_conns);
790
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400791 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300792 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400793 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300794 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400795 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300796 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300797 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400798 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300799 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400800 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300801 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300802
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400803 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400804 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400805 if (status) {
806 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400807 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400808 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400809 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400810 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400811 /*
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
816 * future:
817 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400818 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
819 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400820 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400821 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400822 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300823}
824
Benny Halevy9089f1b2010-05-12 00:12:26 +0300825/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300826static struct nfsd4_session *
827find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
828{
829 struct nfsd4_session *elem;
830 int idx;
831
832 dump_sessionid(__func__, sessionid);
833 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300834 /* Search in the appropriate list */
835 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300836 if (!memcmp(elem->se_sessionid.data, sessionid->data,
837 NFS4_MAX_SESSIONID_LEN)) {
838 return elem;
839 }
840 }
841
842 dprintk("%s: session not found\n", __func__);
843 return NULL;
844}
845
Benny Halevy9089f1b2010-05-12 00:12:26 +0300846/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300847static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300848unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300849{
850 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400851 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300852 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400853 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300854}
855
Benny Halevy36acb662010-05-12 00:13:04 +0300856/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300858renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Benny Halevy07cd4902010-05-12 00:13:41 +0300860 if (is_client_expired(clp)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
862 __func__,
863 clp->cl_clientid.cl_boot,
864 clp->cl_clientid.cl_id);
865 return;
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Move client to the end to the LRU list.
870 */
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 list_move_tail(&clp->cl_lru, &client_lru);
875 clp->cl_time = get_seconds();
876}
877
Benny Halevy36acb662010-05-12 00:13:04 +0300878static inline void
879renew_client(struct nfs4_client *clp)
880{
881 spin_lock(&client_lock);
882 renew_client_locked(clp);
883 spin_unlock(&client_lock);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
887static int
888STALE_CLIENTID(clientid_t *clid)
889{
890 if (clid->cl_boot == boot_time)
891 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 1;
895}
896
897/*
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
901 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500902static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct nfs4_client *clp;
905
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500906 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
907 if (clp == NULL)
908 return NULL;
909 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
910 if (clp->cl_name.data == NULL) {
911 kfree(clp);
912 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500914 memcpy(clp->cl_name.data, name.data, name.len);
915 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return clp;
917}
918
919static inline void
920free_client(struct nfs4_client *clp)
921{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400922 while (!list_empty(&clp->cl_sessions)) {
923 struct nfsd4_session *ses;
924 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
925 se_perclnt);
926 list_del(&ses->se_perclnt);
927 nfsd4_put_session(ses);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (clp->cl_cred.cr_group_info)
930 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500931 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 kfree(clp->cl_name.data);
933 kfree(clp);
934}
935
Benny Halevyd7682982010-05-12 00:13:54 +0300936void
937release_session_client(struct nfsd4_session *session)
938{
939 struct nfs4_client *clp = session->se_client;
940
941 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
942 return;
943 if (is_client_expired(clp)) {
944 free_client(clp);
945 session->se_client = NULL;
946 } else
947 renew_client_locked(clp);
948 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300949}
950
Benny Halevy84d38ac2010-05-12 00:13:16 +0300951/* must be called under the client_lock */
952static inline void
953unhash_client_locked(struct nfs4_client *clp)
954{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400955 struct nfsd4_session *ses;
956
Benny Halevy07cd4902010-05-12 00:13:41 +0300957 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300958 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400959 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400960 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
961 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400962 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void
966expire_client(struct nfs4_client *clp)
967{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400968 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct list_head reaplist;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 INIT_LIST_HEAD(&reaplist);
973 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700974 while (!list_empty(&clp->cl_delegations)) {
975 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700976 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 list_move(&dp->dl_recall_lru, &reaplist);
978 }
979 spin_unlock(&recall_lock);
980 while (!list_empty(&reaplist)) {
981 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
982 list_del_init(&dp->dl_recall_lru);
983 unhash_delegation(dp);
984 }
NeilBrownea1da632005-06-23 22:04:17 -0700985 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
987 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400989 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500990 if (clp->cl_cb_conn.cb_xprt)
991 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300992 list_del(&clp->cl_idhash);
993 list_del(&clp->cl_strhash);
994 spin_lock(&client_lock);
995 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +0300996 if (atomic_read(&clp->cl_refcount) == 0)
997 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300998 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001001static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1002{
1003 memcpy(target->cl_verifier.data, source->data,
1004 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001007static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1010 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1011}
1012
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001013static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 target->cr_uid = source->cr_uid;
1016 target->cr_gid = source->cr_gid;
1017 target->cr_group_info = source->cr_group_info;
1018 get_group_info(target->cr_group_info);
1019}
1020
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001021static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001022{
NeilBrowna55370a2005-06-23 22:03:52 -07001023 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001027same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1028{
1029 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001033same_clid(clientid_t *cl1, clientid_t *cl2)
1034{
1035 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/* XXX what about NGROUP */
1039static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001040same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1041{
1042 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001045static void gen_clid(struct nfs4_client *clp)
1046{
1047 static u32 current_clientid = 1;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 clp->cl_clientid.cl_boot = boot_time;
1050 clp->cl_clientid.cl_id = current_clientid++;
1051}
1052
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001053static void gen_confirm(struct nfs4_client *clp)
1054{
1055 static u32 i;
1056 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001059 *p++ = get_seconds();
1060 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
J. Bruce Fields4581d142011-09-06 14:56:09 -04001063static int
1064same_stateid(stateid_t *id_one, stateid_t *id_two)
1065{
1066 if (id_one->si_stateownerid != id_two->si_stateownerid)
1067 return 0;
1068 return id_one->si_fileid == id_two->si_fileid;
1069}
1070
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001071static struct nfs4_ol_stateid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001072{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001073 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001074 unsigned int hashval;
1075
1076 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001077 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1078 if (same_stateid(&s->sc_stateid, t))
1079 return openlockstateid(s);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001080 return NULL;
1081}
1082
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001083static struct nfs4_ol_stateid *find_stateid_by_type(stateid_t *t, char typemask)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001084{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001085 struct nfs4_ol_stateid *s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001086
1087 s = find_stateid(t);
1088 if (!s)
1089 return NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001090 if (typemask & s->st_stid.sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001091 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001092 return NULL;
1093}
1094
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001095static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1096 struct svc_rqst *rqstp, nfs4_verifier *verf)
1097{
1098 struct nfs4_client *clp;
1099 struct sockaddr *sa = svc_addr(rqstp);
1100 char *princ;
1101
1102 clp = alloc_client(name);
1103 if (clp == NULL)
1104 return NULL;
1105
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001106 INIT_LIST_HEAD(&clp->cl_sessions);
1107
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001108 princ = svc_gss_principal(rqstp);
1109 if (princ) {
1110 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1111 if (clp->cl_principal == NULL) {
1112 free_client(clp);
1113 return NULL;
1114 }
1115 }
1116
1117 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001118 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001119 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001120 INIT_LIST_HEAD(&clp->cl_idhash);
1121 INIT_LIST_HEAD(&clp->cl_strhash);
1122 INIT_LIST_HEAD(&clp->cl_openowners);
1123 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001124 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001125 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001126 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001127 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001128 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001129 clear_bit(0, &clp->cl_cb_slot_busy);
1130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1131 copy_verf(clp, verf);
1132 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1133 clp->cl_flavor = rqstp->rq_flavor;
1134 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1135 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001136 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001137 return clp;
1138}
1139
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001140static int check_name(struct xdr_netobj name)
1141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (name.len == 0)
1143 return 0;
1144 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001145 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147 }
1148 return 1;
1149}
1150
NeilBrownfd39ca92005-06-23 22:04:03 -07001151static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1153{
1154 unsigned int idhashval;
1155
1156 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1157 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1158 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001159 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
NeilBrownfd39ca92005-06-23 22:04:03 -07001162static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163move_to_confirmed(struct nfs4_client *clp)
1164{
1165 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1166 unsigned int strhashval;
1167
1168 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001169 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001170 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001171 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 renew_client(clp);
1173}
1174
1175static struct nfs4_client *
1176find_confirmed_client(clientid_t *clid)
1177{
1178 struct nfs4_client *clp;
1179 unsigned int idhashval = clientid_hashval(clid->cl_id);
1180
1181 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001182 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return clp;
1184 }
1185 return NULL;
1186}
1187
1188static struct nfs4_client *
1189find_unconfirmed_client(clientid_t *clid)
1190{
1191 struct nfs4_client *clp;
1192 unsigned int idhashval = clientid_hashval(clid->cl_id);
1193
1194 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001195 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return clp;
1197 }
1198 return NULL;
1199}
1200
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001201static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001202{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001203 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001204}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001205
NeilBrown28ce6052005-06-23 22:03:56 -07001206static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001207find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001208{
1209 struct nfs4_client *clp;
1210
1211 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001212 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001213 return clp;
1214 }
1215 return NULL;
1216}
1217
1218static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001219find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001220{
1221 struct nfs4_client *clp;
1222
1223 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001224 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001225 return clp;
1226 }
1227 return NULL;
1228}
1229
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001230static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1231{
1232 switch (family) {
1233 case AF_INET:
1234 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1236 return;
1237 case AF_INET6:
1238 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1239 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1240 return;
1241 }
1242}
1243
NeilBrownfd39ca92005-06-23 22:04:03 -07001244static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001245gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001247 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001248 struct sockaddr *sa = svc_addr(rqstp);
1249 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001250 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jeff Layton7077ecb2009-08-14 12:57:58 -04001252 /* Currently, we only support tcp and tcp6 for the callback channel */
1253 if (se->se_callback_netid_len == 3 &&
1254 !memcmp(se->se_callback_netid_val, "tcp", 3))
1255 expected_family = AF_INET;
1256 else if (se->se_callback_netid_len == 4 &&
1257 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1258 expected_family = AF_INET6;
1259 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto out_err;
1261
J. Bruce Fields07263f12010-05-31 19:09:40 -04001262 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001263 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001264 (struct sockaddr *)&conn->cb_addr,
1265 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001266
J. Bruce Fields07263f12010-05-31 19:09:40 -04001267 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001269
J. Bruce Fields07263f12010-05-31 19:09:40 -04001270 if (conn->cb_addr.ss_family == AF_INET6)
1271 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001272
J. Bruce Fields07263f12010-05-31 19:09:40 -04001273 conn->cb_prog = se->se_callback_prog;
1274 conn->cb_ident = se->se_callback_ident;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001275 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return;
1277out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001278 conn->cb_addr.ss_family = AF_UNSPEC;
1279 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001280 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 "will not receive delegations\n",
1282 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285}
1286
Andy Adamson074fe892009-04-03 08:28:15 +03001287/*
Andy Adamson557ce262009-08-28 08:45:04 -04001288 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001289 */
1290void
1291nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1292{
Andy Adamson557ce262009-08-28 08:45:04 -04001293 struct nfsd4_slot *slot = resp->cstate.slot;
1294 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001295
Andy Adamson557ce262009-08-28 08:45:04 -04001296 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001297
Andy Adamson557ce262009-08-28 08:45:04 -04001298 slot->sl_opcnt = resp->opcnt;
1299 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001300
1301 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001302 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001303 return;
1304 }
Andy Adamson557ce262009-08-28 08:45:04 -04001305 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1306 base = (char *)resp->cstate.datap -
1307 (char *)resp->xbuf->head[0].iov_base;
1308 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1309 slot->sl_datalen))
1310 WARN("%s: sessions DRC could not cache compound\n", __func__);
1311 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001312}
1313
1314/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001315 * Encode the replay sequence operation from the slot values.
1316 * If cachethis is FALSE encode the uncached rep error on the next
1317 * operation which sets resp->p and increments resp->opcnt for
1318 * nfs4svc_encode_compoundres.
1319 *
Andy Adamson074fe892009-04-03 08:28:15 +03001320 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001321static __be32
1322nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1323 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001324{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001325 struct nfsd4_op *op;
1326 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001327
Andy Adamsonabfabf82009-07-23 19:02:18 -04001328 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001329 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001330
1331 /* Encode the replayed sequence operation */
1332 op = &args->ops[resp->opcnt - 1];
1333 nfsd4_encode_operation(resp, op);
1334
1335 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001336 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001337 op = &args->ops[resp->opcnt++];
1338 op->status = nfserr_retry_uncached_rep;
1339 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001340 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001341 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001342}
1343
1344/*
Andy Adamson557ce262009-08-28 08:45:04 -04001345 * The sequence operation is not cached because we can use the slot and
1346 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001347 */
1348__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001349nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1350 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001351{
Andy Adamson557ce262009-08-28 08:45:04 -04001352 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001353 __be32 status;
1354
Andy Adamson557ce262009-08-28 08:45:04 -04001355 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001356
Andy Adamsonabfabf82009-07-23 19:02:18 -04001357 /* Either returns 0 or nfserr_retry_uncached */
1358 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1359 if (status == nfserr_retry_uncached_rep)
1360 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001361
Andy Adamson557ce262009-08-28 08:45:04 -04001362 /* The sequence operation has been encoded, cstate->datap set. */
1363 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001364
Andy Adamson557ce262009-08-28 08:45:04 -04001365 resp->opcnt = slot->sl_opcnt;
1366 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1367 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001368
1369 return status;
1370}
1371
Andy Adamson0733d212009-04-03 08:28:01 +03001372/*
1373 * Set the exchange_id flags returned by the server.
1374 */
1375static void
1376nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1377{
1378 /* pNFS is not supported */
1379 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1380
1381 /* Referrals are supported, Migration is not. */
1382 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1383
1384 /* set the wire flags to return to client. */
1385 clid->flags = new->cl_exchange_flags;
1386}
1387
Al Virob37ad282006-10-19 23:28:59 -07001388__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001389nfsd4_exchange_id(struct svc_rqst *rqstp,
1390 struct nfsd4_compound_state *cstate,
1391 struct nfsd4_exchange_id *exid)
1392{
Andy Adamson0733d212009-04-03 08:28:01 +03001393 struct nfs4_client *unconf, *conf, *new;
1394 int status;
1395 unsigned int strhashval;
1396 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001397 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001398 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001399 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001400
Jeff Layton363168b2009-08-14 12:57:56 -04001401 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001402 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001403 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001404 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001405 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001406
1407 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1408 return nfserr_inval;
1409
1410 /* Currently only support SP4_NONE */
1411 switch (exid->spa_how) {
1412 case SP4_NONE:
1413 break;
1414 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001415 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001416 default:
1417 BUG(); /* checked by xdr code */
1418 case SP4_MACH_CRED:
1419 return nfserr_serverfault; /* no excuse :-/ */
1420 }
1421
1422 status = nfs4_make_rec_clidname(dname, &exid->clname);
1423
1424 if (status)
1425 goto error;
1426
1427 strhashval = clientstr_hashval(dname);
1428
1429 nfs4_lock_state();
1430 status = nfs_ok;
1431
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001432 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001433 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001434 if (!clp_used_exchangeid(conf)) {
1435 status = nfserr_clid_inuse; /* XXX: ? */
1436 goto out;
1437 }
Andy Adamson0733d212009-04-03 08:28:01 +03001438 if (!same_verf(&verf, &conf->cl_verifier)) {
1439 /* 18.35.4 case 8 */
1440 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1441 status = nfserr_not_same;
1442 goto out;
1443 }
1444 /* Client reboot: destroy old state */
1445 expire_client(conf);
1446 goto out_new;
1447 }
1448 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1449 /* 18.35.4 case 9 */
1450 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1451 status = nfserr_perm;
1452 goto out;
1453 }
1454 expire_client(conf);
1455 goto out_new;
1456 }
Andy Adamson0733d212009-04-03 08:28:01 +03001457 /*
1458 * Set bit when the owner id and verifier map to an already
1459 * confirmed client id (18.35.3).
1460 */
1461 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1462
1463 /*
1464 * Falling into 18.35.4 case 2, possible router replay.
1465 * Leave confirmed record intact and return same result.
1466 */
1467 copy_verf(conf, &verf);
1468 new = conf;
1469 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001470 }
1471
1472 /* 18.35.4 case 7 */
1473 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1474 status = nfserr_noent;
1475 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001476 }
1477
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001478 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001479 if (unconf) {
1480 /*
1481 * Possible retry or client restart. Per 18.35.4 case 4,
1482 * a new unconfirmed record should be generated regardless
1483 * of whether any properties have changed.
1484 */
1485 expire_client(unconf);
1486 }
1487
1488out_new:
1489 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001490 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001491 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001492 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001493 goto out;
1494 }
1495
Andy Adamson0733d212009-04-03 08:28:01 +03001496 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001497 add_to_unconfirmed(new, strhashval);
1498out_copy:
1499 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1500 exid->clientid.cl_id = new->cl_clientid.cl_id;
1501
Andy Adamson38eb76a2009-04-03 08:28:32 +03001502 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001503 nfsd4_set_ex_flags(new, exid);
1504
1505 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001506 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001507 status = nfs_ok;
1508
1509out:
1510 nfs4_unlock_state();
1511error:
1512 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1513 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001514}
1515
Benny Halevyb85d4c02009-04-03 08:28:08 +03001516static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001517check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001518{
Andy Adamson88e588d2009-07-23 19:02:15 -04001519 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1520 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001521
1522 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001523 if (slot_inuse) {
1524 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001525 return nfserr_jukebox;
1526 else
1527 return nfserr_seq_misordered;
1528 }
1529 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001530 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001531 return nfs_ok;
1532 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001533 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001534 return nfserr_replay_cache;
1535 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001536 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001537 return nfs_ok;
1538 /* Misordered replay or misordered new request */
1539 return nfserr_seq_misordered;
1540}
1541
Andy Adamson49557cc2009-07-23 19:02:16 -04001542/*
1543 * Cache the create session result into the create session single DRC
1544 * slot cache by saving the xdr structure. sl_seqid has been set.
1545 * Do this for solo or embedded create session operations.
1546 */
1547static void
1548nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1549 struct nfsd4_clid_slot *slot, int nfserr)
1550{
1551 slot->sl_status = nfserr;
1552 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1553}
1554
1555static __be32
1556nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1557 struct nfsd4_clid_slot *slot)
1558{
1559 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1560 return slot->sl_status;
1561}
1562
Mi Jinlong1b74c252011-07-14 14:50:17 +08001563#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1564 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1565 1 + /* MIN tag is length with zero, only length */ \
1566 3 + /* version, opcount, opcode */ \
1567 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1568 /* seqid, slotID, slotID, cache */ \
1569 4 ) * sizeof(__be32))
1570
1571#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1572 2 + /* verifier: AUTH_NULL, length 0 */\
1573 1 + /* status */ \
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* opcount, opcode, opstatus*/ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, slotID, status */ \
1578 5 ) * sizeof(__be32))
1579
1580static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1581{
1582 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1583 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1584}
1585
Andy Adamson069b6ad2009-04-03 08:27:58 +03001586__be32
1587nfsd4_create_session(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_create_session *cr_ses)
1590{
Jeff Layton363168b2009-08-14 12:57:56 -04001591 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001592 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001593 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001594 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001595 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001596 int status = 0;
1597
Mi Jinlonga62573d2011-03-23 17:57:07 +08001598 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1599 return nfserr_inval;
1600
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001601 nfs4_lock_state();
1602 unconf = find_unconfirmed_client(&cr_ses->clientid);
1603 conf = find_confirmed_client(&cr_ses->clientid);
1604
1605 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001606 cs_slot = &conf->cl_cs_slot;
1607 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001608 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001609 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001610 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001611 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001612 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001613 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001614 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001615 status = nfserr_seq_misordered;
1616 dprintk("Sequence misordered!\n");
1617 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001618 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001619 goto out;
1620 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001621 } else if (unconf) {
1622 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001623 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001624 status = nfserr_clid_inuse;
1625 goto out;
1626 }
1627
Andy Adamson49557cc2009-07-23 19:02:16 -04001628 cs_slot = &unconf->cl_cs_slot;
1629 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001630 if (status) {
1631 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001632 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001633 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001634 }
1635
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001636 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001637 conf = unconf;
1638 } else {
1639 status = nfserr_stale_clientid;
1640 goto out;
1641 }
1642
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001643 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001644 * XXX: we should probably set this at creation time, and check
1645 * for consistent minorversion use throughout:
1646 */
1647 conf->cl_minorversion = 1;
1648 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001649 * We do not support RDMA or persistent sessions
1650 */
1651 cr_ses->flags &= ~SESSION4_PERSIST;
1652 cr_ses->flags &= ~SESSION4_RDMA;
1653
Mi Jinlong1b74c252011-07-14 14:50:17 +08001654 status = nfserr_toosmall;
1655 if (check_forechannel_attrs(cr_ses->fore_channel))
1656 goto out;
1657
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001658 status = nfserr_jukebox;
1659 new = alloc_init_session(rqstp, conf, cr_ses);
1660 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001661 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001662 status = nfs_ok;
1663 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001664 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001665 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1666 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001667 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001668 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001669
Andy Adamson49557cc2009-07-23 19:02:16 -04001670 /* cache solo and embedded create sessions under the state lock */
1671 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001672 if (confirm_me)
1673 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001674out:
1675 nfs4_unlock_state();
1676 dprintk("%s returns %d\n", __func__, ntohl(status));
1677 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001678}
1679
J. Bruce Fields57716352010-04-21 12:27:19 -04001680static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1681{
1682 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1683 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1684
1685 return argp->opcnt == resp->opcnt;
1686}
1687
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001688static __be32 nfsd4_map_bcts_dir(u32 *dir)
1689{
1690 switch (*dir) {
1691 case NFS4_CDFC4_FORE:
1692 case NFS4_CDFC4_BACK:
1693 return nfs_ok;
1694 case NFS4_CDFC4_FORE_OR_BOTH:
1695 case NFS4_CDFC4_BACK_OR_BOTH:
1696 *dir = NFS4_CDFC4_BOTH;
1697 return nfs_ok;
1698 };
1699 return nfserr_inval;
1700}
1701
1702__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1703 struct nfsd4_compound_state *cstate,
1704 struct nfsd4_bind_conn_to_session *bcts)
1705{
1706 __be32 status;
1707
1708 if (!nfsd4_last_compound_op(rqstp))
1709 return nfserr_not_only_op;
1710 spin_lock(&client_lock);
1711 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1712 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1713 * client_lock iself: */
1714 if (cstate->session) {
1715 nfsd4_get_session(cstate->session);
1716 atomic_inc(&cstate->session->se_client->cl_refcount);
1717 }
1718 spin_unlock(&client_lock);
1719 if (!cstate->session)
1720 return nfserr_badsession;
1721
1722 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001723 if (!status)
1724 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1725 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001726}
1727
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001728static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1729{
1730 if (!session)
1731 return 0;
1732 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1733}
1734
Andy Adamson069b6ad2009-04-03 08:27:58 +03001735__be32
1736nfsd4_destroy_session(struct svc_rqst *r,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_destroy_session *sessionid)
1739{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001740 struct nfsd4_session *ses;
1741 u32 status = nfserr_badsession;
1742
1743 /* Notes:
1744 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1745 * - Should we return nfserr_back_chan_busy if waiting for
1746 * callbacks on to-be-destroyed session?
1747 * - Do we need to clear any callback info from previous session?
1748 */
1749
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001750 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001751 if (!nfsd4_last_compound_op(r))
1752 return nfserr_not_only_op;
1753 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001754 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001755 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001756 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1757 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001758 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001759 goto out;
1760 }
1761
1762 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001763 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001764
Benny Halevyab707e152010-05-12 00:14:06 +03001765 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001766 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001767 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001768
1769 nfsd4_del_conns(ses);
1770
Benny Halevye10e0cf2009-04-03 08:28:38 +03001771 nfsd4_put_session(ses);
1772 status = nfs_ok;
1773out:
1774 dprintk("%s returns %d\n", __func__, ntohl(status));
1775 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001776}
1777
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001778static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001779{
1780 struct nfsd4_conn *c;
1781
1782 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001783 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001784 return c;
1785 }
1786 }
1787 return NULL;
1788}
1789
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001790static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001791{
1792 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001793 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001794 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001795
1796 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001797 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001798 if (c) {
1799 spin_unlock(&clp->cl_lock);
1800 free_conn(new);
1801 return;
1802 }
1803 __nfsd4_hash_conn(new, ses);
1804 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001805 ret = nfsd4_register_conn(new);
1806 if (ret)
1807 /* oops; xprt is already down: */
1808 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001809 return;
1810}
1811
Mi Jinlong868b89c2011-04-27 09:09:58 +08001812static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1813{
1814 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1815
1816 return args->opcnt > session->se_fchannel.maxops;
1817}
1818
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001819static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1820 struct nfsd4_session *session)
1821{
1822 struct xdr_buf *xb = &rqstp->rq_arg;
1823
1824 return xb->len > session->se_fchannel.maxreq_sz;
1825}
1826
Andy Adamson069b6ad2009-04-03 08:27:58 +03001827__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001828nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001829 struct nfsd4_compound_state *cstate,
1830 struct nfsd4_sequence *seq)
1831{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001832 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001833 struct nfsd4_session *session;
1834 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001835 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001836 int status;
1837
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001838 if (resp->opcnt != 1)
1839 return nfserr_sequence_pos;
1840
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001841 /*
1842 * Will be either used or freed by nfsd4_sequence_check_conn
1843 * below.
1844 */
1845 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1846 if (!conn)
1847 return nfserr_jukebox;
1848
Benny Halevy9089f1b2010-05-12 00:12:26 +03001849 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001850 status = nfserr_badsession;
1851 session = find_in_sessionid_hashtbl(&seq->sessionid);
1852 if (!session)
1853 goto out;
1854
Mi Jinlong868b89c2011-04-27 09:09:58 +08001855 status = nfserr_too_many_ops;
1856 if (nfsd4_session_too_many_ops(rqstp, session))
1857 goto out;
1858
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001859 status = nfserr_req_too_big;
1860 if (nfsd4_request_too_big(rqstp, session))
1861 goto out;
1862
Benny Halevyb85d4c02009-04-03 08:28:08 +03001863 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001864 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001865 goto out;
1866
Andy Adamson557ce262009-08-28 08:45:04 -04001867 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001868 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1869
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001870 /* We do not negotiate the number of slots yet, so set the
1871 * maxslots to the session maxreqs which is used to encode
1872 * sr_highest_slotid and the sr_target_slot id to maxslots */
1873 seq->maxslots = session->se_fchannel.maxreqs;
1874
Andy Adamson88e588d2009-07-23 19:02:15 -04001875 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001876 if (status == nfserr_replay_cache) {
1877 cstate->slot = slot;
1878 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001879 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001880 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001881 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001882 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001883 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001884 }
1885 if (status)
1886 goto out;
1887
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001888 nfsd4_sequence_check_conn(conn, session);
1889 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001890
Benny Halevyb85d4c02009-04-03 08:28:08 +03001891 /* Success! bump slot seqid */
1892 slot->sl_inuse = true;
1893 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001894 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001895
1896 cstate->slot = slot;
1897 cstate->session = session;
1898
Benny Halevyb85d4c02009-04-03 08:28:08 +03001899out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001900 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001901 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001902 struct nfs4_client *clp = session->se_client;
1903
Benny Halevy36acb662010-05-12 00:13:04 +03001904 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001905 atomic_inc(&clp->cl_refcount);
1906 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1907 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001908 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001909 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001910 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001911 dprintk("%s: return %d\n", __func__, ntohl(status));
1912 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001913}
1914
1915__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001916nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1917{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001918 int status = 0;
1919
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001920 if (rc->rca_one_fs) {
1921 if (!cstate->current_fh.fh_dentry)
1922 return nfserr_nofilehandle;
1923 /*
1924 * We don't take advantage of the rca_one_fs case.
1925 * That's OK, it's optional, we can safely ignore it.
1926 */
1927 return nfs_ok;
1928 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001929
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001930 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001931 status = nfserr_complete_already;
1932 if (cstate->session->se_client->cl_firststate)
1933 goto out;
1934
1935 status = nfserr_stale_clientid;
1936 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001937 /*
1938 * The following error isn't really legal.
1939 * But we only get here if the client just explicitly
1940 * destroyed the client. Surely it no longer cares what
1941 * error it gets back on an operation for the dead
1942 * client.
1943 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001944 goto out;
1945
1946 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001947 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001948out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001949 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001950 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001951}
1952
1953__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001954nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1955 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct xdr_netobj clname = {
1958 .len = setclid->se_namelen,
1959 .data = setclid->se_name,
1960 };
1961 nfs4_verifier clverifier = setclid->se_verf;
1962 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001963 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001964 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001965 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001968 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
NeilBrowna55370a2005-06-23 22:03:52 -07001970 status = nfs4_make_rec_clidname(dname, &clname);
1971 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001972 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1976 * We get here on a DRC miss.
1977 */
1978
NeilBrowna55370a2005-06-23 22:03:52 -07001979 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001982 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07001983 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001984 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001986 if (clp_used_exchangeid(conf))
1987 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001988 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04001989 char addr_str[INET6_ADDRSTRLEN];
1990 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1991 sizeof(addr_str));
1992 dprintk("NFSD: setclientid: string in use by client "
1993 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001997 /*
1998 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1999 * has a description of SETCLIENTID request processing consisting
2000 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2001 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002002 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002003 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002005 /*
2006 * RFC 3530 14.2.33 CASE 4:
2007 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
2009 if (unconf)
2010 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002011 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002012 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002015 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002017 * RFC 3530 14.2.33 CASE 1:
2018 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002020 if (unconf) {
2021 /* Note this is removing unconfirmed {*x***},
2022 * which is stronger than RFC recommended {vxc**}.
2023 * This has the advantage that there is at most
2024 * one {*x***} in either list at any time.
2025 */
2026 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002028 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002029 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else if (!unconf) {
2033 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002034 * RFC 3530 14.2.33 CASE 2:
2035 * probable client reboot; state will be removed if
2036 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002038 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002039 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002042 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002043 /*
2044 * RFC 3530 14.2.33 CASE 3:
2045 * probable client reboot; state will be removed if
2046 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 */
2048 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002049 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002050 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002054 /*
2055 * XXX: we should probably set this at creation time, and check
2056 * for consistent minorversion use throughout:
2057 */
2058 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002059 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002060 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2062 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2063 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2064 status = nfs_ok;
2065out:
2066 nfs4_unlock_state();
2067 return status;
2068}
2069
2070
2071/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002072 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2073 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2074 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Al Virob37ad282006-10-19 23:28:59 -07002076__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002077nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2078 struct nfsd4_compound_state *cstate,
2079 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Jeff Layton363168b2009-08-14 12:57:56 -04002081 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002082 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2084 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002085 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (STALE_CLIENTID(clid))
2088 return nfserr_stale_clientid;
2089 /*
2090 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2091 * We get here on a DRC miss.
2092 */
2093
2094 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002095
2096 conf = find_confirmed_client(clid);
2097 unconf = find_unconfirmed_client(clid);
2098
2099 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002100 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002101 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002102 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002103 goto out;
2104
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002105 /*
2106 * section 14.2.34 of RFC 3530 has a description of
2107 * SETCLIENTID_CONFIRM request processing consisting
2108 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2109 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002110 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002111 /*
2112 * RFC 3530 14.2.34 CASE 1:
2113 * callback update
2114 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002115 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 status = nfserr_clid_inuse;
2117 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002118 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2119 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002120 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002124 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002125 /*
2126 * RFC 3530 14.2.34 CASE 2:
2127 * probable retransmitted request; play it safe and
2128 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002129 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002132 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002134 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002135 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002136 /*
2137 * RFC 3530 14.2.34 CASE 3:
2138 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002139 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002140 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 status = nfserr_clid_inuse;
2142 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002143 unsigned int hash =
2144 clientstr_hashval(unconf->cl_recdir);
2145 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002146 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002147 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002148 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002149 expire_client(conf);
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002152 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002153 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002154 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002156 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2157 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002158 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002159 /*
2160 * RFC 3530 14.2.34 CASE 4:
2161 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002164 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002165 /* check that we have hit one of the cases...*/
2166 status = nfserr_clid_inuse;
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 nfs4_unlock_state();
2170 return status;
2171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173/* OPEN Share state helper functions */
2174static inline struct nfs4_file *
2175alloc_init_file(struct inode *ino)
2176{
2177 struct nfs4_file *fp;
2178 unsigned int hashval = file_hashval(ino);
2179
NeilBrowne60d4392005-06-23 22:03:01 -07002180 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2181 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002182 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002184 INIT_LIST_HEAD(&fp->fi_stateids);
2185 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 fp->fi_inode = igrab(ino);
2187 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002188 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002189 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002192 spin_lock(&recall_lock);
2193 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2194 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return fp;
2196 }
2197 return NULL;
2198}
2199
2200static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002201nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002202{
NeilBrowne60d4392005-06-23 22:03:01 -07002203 if (*slab == NULL)
2204 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002205 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002206 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002207}
2208
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002209void
NeilBrowne60d4392005-06-23 22:03:01 -07002210nfsd4_free_slabs(void)
2211{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002212 nfsd4_free_slab(&openowner_slab);
2213 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002214 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002215 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002216 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219static int
2220nfsd4_init_slabs(void)
2221{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002222 openowner_slab = kmem_cache_create("nfsd4_openowners",
2223 sizeof(struct nfs4_openowner), 0, 0, NULL);
2224 if (openowner_slab == NULL)
2225 goto out_nomem;
2226 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2227 sizeof(struct nfs4_openowner), 0, 0, NULL);
2228 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002229 goto out_nomem;
2230 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002231 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002232 if (file_slab == NULL)
2233 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002234 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002235 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002236 if (stateid_slab == NULL)
2237 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002238 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002239 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002240 if (deleg_slab == NULL)
2241 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002243out_nomem:
2244 nfsd4_free_slabs();
2245 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002249void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002251 kfree(oo->oo_owner.so_owner.data);
2252 kmem_cache_free(openowner_slab, oo);
2253}
2254
2255void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2256{
2257 kfree(lo->lo_owner.so_owner.data);
2258 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002261static void init_nfs4_replay(struct nfs4_replay *rp)
2262{
2263 rp->rp_status = nfserr_serverfault;
2264 rp->rp_buflen = 0;
2265 rp->rp_buf = rp->rp_ibuf;
2266}
2267
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002268static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct nfs4_stateowner *sop;
2271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002272 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002273 if (!sop)
2274 return NULL;
2275
2276 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2277 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002278 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002279 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002281 sop->so_owner.len = owner->len;
2282
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002283 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002284 sop->so_id = current_ownerid++;
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002285 sop->so_client = clp;
2286 init_nfs4_replay(&sop->so_replay);
2287 return sop;
2288}
2289
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002290static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002291{
2292 unsigned int idhashval;
2293
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002294 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2295 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2296 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2297 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002300static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002302 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002304 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2305 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002307 oo->oo_owner.so_is_open_owner = 1;
2308 oo->oo_owner.so_seqid = open->op_seqid;
2309 oo->oo_confirmed = 0;
2310 oo->oo_time = 0;
2311 INIT_LIST_HEAD(&oo->oo_close_lru);
2312 hash_openowner(oo, clp, strhashval);
2313 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002317init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002318 struct nfs4_openowner *oo = open->op_openowner;
2319 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
NeilBrownea1da632005-06-23 22:04:17 -07002321 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002322 list_add(&stp->st_stid.sc_hash, &stateid_hashtbl[hashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002323 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002324 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002325 stp->st_stid.sc_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002326 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002327 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002329 stp->st_stid.sc_stateid.si_boot = boot_time;
2330 stp->st_stid.sc_stateid.si_stateownerid = oo->oo_owner.so_id;
2331 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002332 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002333 stp->st_stid.sc_stateid.si_generation = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 stp->st_access_bmap = 0;
2335 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002336 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2337 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002339 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
2342static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002343move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002345 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002347 list_move_tail(&oo->oo_close_lru, &close_lru);
2348 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002352same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2353 clientid_t *clid)
2354{
2355 return (sop->so_owner.len == owner->len) &&
2356 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2357 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002360static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2362{
2363 struct nfs4_stateowner *so = NULL;
2364
J. Bruce Fields506f2752011-08-11 18:50:18 -04002365 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002366 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002367 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 return NULL;
2370}
2371
2372/* search file_hashtbl[] for file */
2373static struct nfs4_file *
2374find_file(struct inode *ino)
2375{
2376 unsigned int hashval = file_hashval(ino);
2377 struct nfs4_file *fp;
2378
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002379 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002381 if (fp->fi_inode == ino) {
2382 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002383 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002387 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return NULL;
2389}
2390
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002391static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002392{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002393 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002394 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002395 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2396 return 0;
2397 x &= ~NFS4_SHARE_ACCESS_MASK;
2398 if (minorversion && x) {
2399 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2400 return 0;
2401 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2402 return 0;
2403 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2404 }
2405 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002406 return 0;
2407 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002408}
2409
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002410static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002411{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002412 /* Note: unlike access bits, deny bits may be zero. */
2413 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002416/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 * Called to check deny when READ with all zero stateid or
2418 * WRITE with all zero or all one stateid
2419 */
Al Virob37ad282006-10-19 23:28:59 -07002420static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2422{
2423 struct inode *ino = current_fh->fh_dentry->d_inode;
2424 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002425 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002426 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 dprintk("NFSD: nfs4_share_conflict\n");
2429
2430 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002431 if (!fp)
2432 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002433 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002435 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2436 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2437 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
NeilBrown13cd2182005-06-23 22:03:10 -07002440 ret = nfs_ok;
2441out:
2442 put_nfs4_file(fp);
2443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002446static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* We're assuming the state code never drops its reference
2449 * without first removing the lease. Since we're in this lease
2450 * callback (and since the lease code is serialized by the kernel
2451 * lock) we know the server hasn't removed the lease yet, we know
2452 * it's safe to take a reference: */
2453 atomic_inc(&dp->dl_count);
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Arnd Bergmann460781b2010-11-17 16:26:56 +01002457 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 dp->dl_time = get_seconds();
2459
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002460 nfsd4_cb_recall(dp);
2461}
2462
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002463/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002464static void nfsd_break_deleg_cb(struct file_lock *fl)
2465{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002466 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2467 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002468
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002469 BUG_ON(!fp);
2470 /* We assume break_lease is only called once per lease: */
2471 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002472 /*
2473 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002474 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002475 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002476 */
2477 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002479 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002480 fp->fi_had_conflict = true;
2481 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2482 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002483 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486static
2487int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2488{
2489 if (arg & F_UNLCK)
2490 return lease_modify(onlist, arg);
2491 else
2492 return -EAGAIN;
2493}
2494
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002495static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002496 .lm_break = nfsd_break_deleg_cb,
2497 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
2499
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002500static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2501{
2502 if (nfsd4_has_session(cstate))
2503 return nfs_ok;
2504 if (seqid == so->so_seqid - 1)
2505 return nfserr_replay_me;
2506 if (seqid == so->so_seqid)
2507 return nfs_ok;
2508 return nfserr_bad_seqid;
2509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Al Virob37ad282006-10-19 23:28:59 -07002511__be32
Andy Adamson66689582009-04-03 08:28:45 +03002512nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2513 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 clientid_t *clientid = &open->op_clientid;
2516 struct nfs4_client *clp = NULL;
2517 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002518 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002519 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002522 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 if (STALE_CLIENTID(&open->op_clientid))
2525 return nfserr_stale_clientid;
2526
J. Bruce Fields506f2752011-08-11 18:50:18 -04002527 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002528 oo = find_openstateowner_str(strhashval, open);
2529 open->op_openowner = oo;
2530 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002531 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 clp = find_confirmed_client(clientid);
2533 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002534 return nfserr_expired;
2535 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002537 if (!oo->oo_confirmed) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002538 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002539 clp = oo->oo_owner.so_client;
2540 release_openowner(oo);
2541 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002542 goto renew;
2543 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002544 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002545 if (status)
2546 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002548 if (open->op_openowner == NULL) {
2549 oo = alloc_init_open_stateowner(strhashval, clp, open);
2550 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002551 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002552 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002553 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002554 list_del_init(&oo->oo_close_lru);
2555 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002556 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Al Virob37ad282006-10-19 23:28:59 -07002559static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002560nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2561{
2562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2563 return nfserr_openmode;
2564 else
2565 return nfs_ok;
2566}
2567
NeilBrown52f4fb42005-06-23 22:02:49 -07002568static struct nfs4_delegation *
2569find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2570{
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002571 struct nfs4_delegation *dp;
NeilBrown52f4fb42005-06-23 22:02:49 -07002572
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002573 spin_lock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002574 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2575 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2576 spin_unlock(&recall_lock);
2577 return dp;
2578 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002579 spin_unlock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002580 return NULL;
NeilBrown52f4fb42005-06-23 22:02:49 -07002581}
2582
Daniel Mackc47d8322011-05-16 16:38:14 +02002583static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002584{
2585 share_access &= ~NFS4_SHARE_WANT_MASK;
2586
2587 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2588}
2589
Al Virob37ad282006-10-19 23:28:59 -07002590static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002591nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2592 struct nfs4_delegation **dp)
2593{
2594 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002595 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002596
2597 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2598 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002599 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002600 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002601 status = nfs4_check_delegmode(*dp, flags);
2602 if (status)
2603 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002604out:
2605 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2606 return nfs_ok;
2607 if (status)
2608 return status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002609 open->op_openowner->oo_confirmed = 1;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002610 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002611}
2612
Al Virob37ad282006-10-19 23:28:59 -07002613static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002614nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002616 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002617 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
NeilBrown8beefa22005-06-23 22:03:08 -07002619 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 /* ignore lock owners */
2621 if (local->st_stateowner->so_is_open_owner == 0)
2622 continue;
2623 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002624 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 *stpp = local;
2626 /* check for conflicting share reservations */
2627 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002628 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002630 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002633static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002634nfs4_alloc_stateid(void)
2635{
2636 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2637}
2638
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002639static inline int nfs4_access_to_access(u32 nfs4_access)
2640{
2641 int flags = 0;
2642
2643 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2644 flags |= NFSD_MAY_READ;
2645 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2646 flags |= NFSD_MAY_WRITE;
2647 return flags;
2648}
2649
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002650static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2651 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002652{
2653 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002654 int oflag = nfs4_access_to_omode(open->op_share_access);
2655 int access = nfs4_access_to_access(open->op_share_access);
2656
2657 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2658 * allowing it to break the lease and return EAGAIN leaves the
2659 * client unable to make progress in returning the delegation */
2660 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2661 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002662
2663 if (!fp->fi_fds[oflag]) {
2664 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2665 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002666 if (status)
2667 return status;
2668 }
2669 nfs4_file_get_access(fp, oflag);
2670
2671 return nfs_ok;
2672}
2673
Al Virob37ad282006-10-19 23:28:59 -07002674static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002675nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002676 struct nfs4_file *fp, struct svc_fh *cur_fh,
2677 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002679 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002680 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
NeilBrown5ac049a2005-06-23 22:03:03 -07002682 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002684 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002686 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002687 if (status) {
2688 kmem_cache_free(stateid_slab, stp);
2689 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 *stpp = stp;
2692 return 0;
2693}
2694
Al Virob37ad282006-10-19 23:28:59 -07002695static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2697 struct nfsd4_open *open)
2698{
2699 struct iattr iattr = {
2700 .ia_valid = ATTR_SIZE,
2701 .ia_size = 0,
2702 };
2703 if (!open->op_truncate)
2704 return 0;
2705 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002706 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2708}
2709
Al Virob37ad282006-10-19 23:28:59 -07002710static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002711nfs4_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 -07002712{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002713 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2714 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002715 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
J. Bruce Fields7d947842010-08-20 18:09:31 -04002717 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002718 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002719 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002720 if (status)
2721 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 status = nfsd4_truncate(rqstp, cur_fh, open);
2724 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002725 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002726 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002727 nfs4_file_put_access(fp, oflag);
2728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return status;
2730 }
2731 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002732 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002733 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 return nfs_ok;
2736}
2737
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739static void
NeilBrown37515172005-07-07 17:59:16 -07002740nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002742 open->op_openowner->oo_confirmed = 1;
2743 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002746/* Should we give out recallable state?: */
2747static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2748{
2749 if (clp->cl_cb_state == NFSD4_CB_UP)
2750 return true;
2751 /*
2752 * In the sessions case, since we don't have to establish a
2753 * separate connection for callbacks, we assume it's OK
2754 * until we hear otherwise:
2755 */
2756 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2757}
2758
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002759static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2760{
2761 struct file_lock *fl;
2762
2763 fl = locks_alloc_lock();
2764 if (!fl)
2765 return NULL;
2766 locks_init_lock(fl);
2767 fl->fl_lmops = &nfsd_lease_mng_ops;
2768 fl->fl_flags = FL_LEASE;
2769 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2770 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002771 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002772 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002773 return fl;
2774}
2775
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002776static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2777{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002778 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002779 struct file_lock *fl;
2780 int status;
2781
2782 fl = nfs4_alloc_init_lease(dp, flag);
2783 if (!fl)
2784 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002785 fl->fl_file = find_readable_file(fp);
2786 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2787 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002788 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002789 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002790 locks_free_lock(fl);
2791 return -ENOMEM;
2792 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002793 fp->fi_lease = fl;
2794 fp->fi_deleg_file = fl->fl_file;
2795 get_file(fp->fi_deleg_file);
2796 atomic_set(&fp->fi_delegees, 1);
2797 list_add(&dp->dl_perfile, &fp->fi_delegations);
2798 return 0;
2799}
2800
2801static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2802{
2803 struct nfs4_file *fp = dp->dl_file;
2804
2805 if (!fp->fi_lease)
2806 return nfs4_setlease(dp, flag);
2807 spin_lock(&recall_lock);
2808 if (fp->fi_had_conflict) {
2809 spin_unlock(&recall_lock);
2810 return -EAGAIN;
2811 }
2812 atomic_inc(&fp->fi_delegees);
2813 list_add(&dp->dl_perfile, &fp->fi_delegations);
2814 spin_unlock(&recall_lock);
2815 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002816 return 0;
2817}
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819/*
2820 * Attempt to hand out a delegation.
2821 */
2822static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002823nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002826 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002827 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 int status, flag = 0;
2829
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002830 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002832 open->op_recall = 0;
2833 switch (open->op_claim_type) {
2834 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002835 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002836 open->op_recall = 1;
2837 flag = open->op_delegate_type;
2838 if (flag == NFS4_OPEN_DELEGATE_NONE)
2839 goto out;
2840 break;
2841 case NFS4_OPEN_CLAIM_NULL:
2842 /* Let's not give out any delegations till everyone's
2843 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002844 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002845 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002846 if (!cb_up || !oo->oo_confirmed)
NeilBrown7b190fe2005-06-23 22:03:23 -07002847 goto out;
2848 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2849 flag = NFS4_OPEN_DELEGATE_WRITE;
2850 else
2851 flag = NFS4_OPEN_DELEGATE_READ;
2852 break;
2853 default:
2854 goto out;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002857 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002858 if (dp == NULL)
2859 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002860 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002861 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002862 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2865
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002866 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2867 STATEID_VAL(&dp->dl_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002869 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2870 && flag == NFS4_OPEN_DELEGATE_NONE
2871 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002872 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002874 return;
2875out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002876 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002877out_no_deleg:
2878 flag = NFS4_OPEN_DELEGATE_NONE;
2879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
2882/*
2883 * called with nfs4_lock_state() held.
2884 */
Al Virob37ad282006-10-19 23:28:59 -07002885__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2887{
Andy Adamson66689582009-04-03 08:28:45 +03002888 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct nfs4_file *fp = NULL;
2890 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002891 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002892 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002893 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002896 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002897 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 goto out;
2899 /*
2900 * Lookup file; if found, lookup stateid and check open request,
2901 * and check for delegations in the process of being recalled.
2902 * If not found, create the nfs4_file struct
2903 */
2904 fp = find_file(ino);
2905 if (fp) {
2906 if ((status = nfs4_check_open(fp, open, &stp)))
2907 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002908 status = nfs4_check_deleg(fp, open, &dp);
2909 if (status)
2910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002912 status = nfserr_bad_stateid;
2913 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2914 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002915 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 fp = alloc_init_file(ino);
2917 if (fp == NULL)
2918 goto out;
2919 }
2920
2921 /*
2922 * OPEN the file, or upgrade an existing OPEN.
2923 * If truncate fails, the OPEN fails.
2924 */
2925 if (stp) {
2926 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002927 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (status)
2929 goto out;
2930 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002931 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002932 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002934 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 status = nfsd4_truncate(rqstp, current_fh, open);
2936 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002937 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 goto out;
2939 }
2940 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002941 update_stateid(&stp->st_stid.sc_stateid);
2942 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002944 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945 open->op_openowner->oo_confirmed = 1;
Andy Adamson66689582009-04-03 08:28:45 +03002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*
2948 * Attempt to hand out a delegation. No error return, because the
2949 * OPEN succeeds even if we fail.
2950 */
2951 nfs4_open_delegation(current_fh, open, stp);
2952
2953 status = nfs_ok;
2954
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002955 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002956 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957out:
NeilBrown13cd2182005-06-23 22:03:10 -07002958 if (fp)
2959 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002960 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2961 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 /*
2963 * To finish the open response, we just need to set the rflags.
2964 */
2965 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002966 if (!open->op_openowner->oo_confirmed &&
Andy Adamson66689582009-04-03 08:28:45 +03002967 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2969
2970 return status;
2971}
2972
Al Virob37ad282006-10-19 23:28:59 -07002973__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002974nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2975 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002978 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 nfs4_lock_state();
2981 dprintk("process_renew(%08x/%08x): starting\n",
2982 clid->cl_boot, clid->cl_id);
2983 status = nfserr_stale_clientid;
2984 if (STALE_CLIENTID(clid))
2985 goto out;
2986 clp = find_confirmed_client(clid);
2987 status = nfserr_expired;
2988 if (clp == NULL) {
2989 /* We assume the client took too long to RENEW. */
2990 dprintk("nfsd4_renew: clientid not found!\n");
2991 goto out;
2992 }
2993 renew_client(clp);
2994 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07002995 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04002996 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 goto out;
2998 status = nfs_ok;
2999out:
3000 nfs4_unlock_state();
3001 return status;
3002}
3003
Daniel Mackc47d8322011-05-16 16:38:14 +02003004static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003005};
3006
NeilBrowna76b4312005-06-23 22:04:01 -07003007static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003008nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003009{
3010 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003011 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003012 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003013 /*
3014 * Now that every NFSv4 client has had the chance to recover and
3015 * to see the (possibly new, possibly shorter) lease time, we
3016 * can safely set the next grace time to the current lease time:
3017 */
3018 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003019}
3020
NeilBrownfd39ca92005-06-23 22:04:03 -07003021static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022nfs4_laundromat(void)
3023{
3024 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003025 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct nfs4_delegation *dp;
3027 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003028 time_t cutoff = get_seconds() - nfsd4_lease;
3029 time_t t, clientid_val = nfsd4_lease;
3030 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 nfs4_lock_state();
3033
3034 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003035 if (locks_in_grace())
3036 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003037 INIT_LIST_HEAD(&reaplist);
3038 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 list_for_each_safe(pos, next, &client_lru) {
3040 clp = list_entry(pos, struct nfs4_client, cl_lru);
3041 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3042 t = clp->cl_time - cutoff;
3043 if (clientid_val > t)
3044 clientid_val = t;
3045 break;
3046 }
Benny Halevyd7682982010-05-12 00:13:54 +03003047 if (atomic_read(&clp->cl_refcount)) {
3048 dprintk("NFSD: client in use (clientid %08x)\n",
3049 clp->cl_clientid.cl_id);
3050 continue;
3051 }
3052 unhash_client_locked(clp);
3053 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003054 }
3055 spin_unlock(&client_lock);
3056 list_for_each_safe(pos, next, &reaplist) {
3057 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 dprintk("NFSD: purging unused client (clientid %08x)\n",
3059 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003060 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 expire_client(clp);
3062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 spin_lock(&recall_lock);
3064 list_for_each_safe(pos, next, &del_recall_lru) {
3065 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3066 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3067 u = dp->dl_time - cutoff;
3068 if (test_val > u)
3069 test_val = u;
3070 break;
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 list_move(&dp->dl_recall_lru, &reaplist);
3073 }
3074 spin_unlock(&recall_lock);
3075 list_for_each_safe(pos, next, &reaplist) {
3076 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3077 list_del_init(&dp->dl_recall_lru);
3078 unhash_delegation(dp);
3079 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003080 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003082 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3083 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3084 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (test_val > u)
3086 test_val = u;
3087 break;
3088 }
3089 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003090 oo->oo_owner.so_id);
3091 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3094 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3095 nfs4_unlock_state();
3096 return clientid_val;
3097}
3098
Harvey Harrisona254b242008-02-20 12:49:00 -08003099static struct workqueue_struct *laundry_wq;
3100static void laundromat_main(struct work_struct *);
3101static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3102
3103static void
David Howellsc4028952006-11-22 14:57:56 +00003104laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 time_t t;
3107
3108 t = nfs4_laundromat();
3109 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003110 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003113static struct nfs4_openowner * search_close_lru(u32 st_id)
NeilBrownf8816512005-07-07 17:59:25 -07003114{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003115 struct nfs4_openowner *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003117 list_for_each_entry(local, &close_lru, oo_close_lru) {
3118 if (local->oo_owner.so_id == st_id)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003119 return local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 return NULL;
3122}
3123
3124static inline int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003125nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003127 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
3130static int
3131STALE_STATEID(stateid_t *stateid)
3132{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003133 if (stateid->si_boot == boot_time)
3134 return 0;
3135 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003136 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
3140static inline int
3141access_permit_read(unsigned long access_bmap)
3142{
3143 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3144 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3145 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3146}
3147
3148static inline int
3149access_permit_write(unsigned long access_bmap)
3150{
3151 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3152 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3153}
3154
3155static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003156__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Al Virob37ad282006-10-19 23:28:59 -07003158 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
J. Bruce Fields02921912010-07-29 15:16:59 -04003160 /* For lock stateid's, we test the parent open, not the lock: */
3161 if (stp->st_openstp)
3162 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3164 goto out;
3165 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3166 goto out;
3167 status = nfs_ok;
3168out:
3169 return status;
3170}
3171
Al Virob37ad282006-10-19 23:28:59 -07003172static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3174{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003175 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003177 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003178 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 * conflicting state; so we must wait out the grace period. */
3180 return nfserr_grace;
3181 } else if (flags & WR_STATE)
3182 return nfs4_share_conflict(current_fh,
3183 NFS4_SHARE_DENY_WRITE);
3184 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3185 return nfs4_share_conflict(current_fh,
3186 NFS4_SHARE_DENY_READ);
3187}
3188
3189/*
3190 * Allow READ/WRITE during grace period on recovered state only for files
3191 * that are not able to provide mandatory locking.
3192 */
3193static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003194grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003196 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
J. Bruce Fields81b82962011-08-23 11:03:29 -04003199/* Returns true iff a is later than b: */
3200static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3201{
3202 return (s32)a->si_generation - (s32)b->si_generation > 0;
3203}
3204
J. Bruce Fields28dde242011-08-22 10:07:12 -04003205static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003206{
Andy Adamson66689582009-04-03 08:28:45 +03003207 /*
3208 * When sessions are used the stateid generation number is ignored
3209 * when it is zero.
3210 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003211 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003212 return nfs_ok;
3213
3214 if (in->si_generation == ref->si_generation)
3215 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003216
J. Bruce Fields0836f582008-01-26 19:08:12 -05003217 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003218 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003219 return nfserr_bad_stateid;
3220 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003221 * However, we could see a stateid from the past, even from a
3222 * non-buggy client. For example, if the client sends a lock
3223 * while some IO is outstanding, the lock may bump si_generation
3224 * while the IO is still in flight. The client could avoid that
3225 * situation by waiting for responses on all the IO requests,
3226 * but better performance may result in retrying IO that
3227 * receives an old_stateid error if requests are rarely
3228 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003229 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003230 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003231}
3232
J. Bruce Fields3e633072009-02-21 13:17:19 -08003233static int is_delegation_stateid(stateid_t *stateid)
3234{
3235 return stateid->si_fileid == 0;
3236}
3237
J. Bruce Fields28dde242011-08-22 10:07:12 -04003238__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003239{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003240 struct nfs4_ol_stateid *stp = NULL;
Bryan Schumaker17456802011-07-13 10:50:48 -04003241 __be32 status = nfserr_stale_stateid;
3242
3243 if (STALE_STATEID(stateid))
3244 goto out;
3245
3246 status = nfserr_expired;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003247 stp = find_stateid(stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04003248 if (!stp)
3249 goto out;
3250 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003251 if (stp->st_stateowner->so_is_open_owner
3252 && !openowner(stp->st_stateowner)->oo_confirmed)
Bryan Schumaker17456802011-07-13 10:50:48 -04003253 goto out;
3254
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003255 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003256 if (status)
3257 goto out;
3258
3259 status = nfs_ok;
3260out:
3261 return status;
3262}
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264/*
3265* Checks for stateid operations
3266*/
Al Virob37ad282006-10-19 23:28:59 -07003267__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003268nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3269 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003271 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003273 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003275 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (filpp)
3278 *filpp = NULL;
3279
J. Bruce Fields18f82732009-02-21 15:23:01 -08003280 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return nfserr_grace;
3282
3283 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3284 return check_special_stateids(current_fh, stateid, flags);
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 status = nfserr_stale_stateid;
3287 if (STALE_STATEID(stateid))
3288 goto out;
3289
J. Bruce Fields33515142010-10-02 18:42:39 -04003290 /*
3291 * We assume that any stateid that has the current boot time,
3292 * but that we can't find, is expired:
3293 */
3294 status = nfserr_expired;
J. Bruce Fields3e633072009-02-21 13:17:19 -08003295 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003296 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003297 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 goto out;
J. Bruce Fields28dde242011-08-22 10:07:12 -04003299 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003300 if (status)
3301 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003302 status = nfs4_check_delegmode(dp, flags);
3303 if (status)
3304 goto out;
3305 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003306 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003307 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003308 BUG_ON(!*filpp);
3309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 } else { /* open or lock stateid */
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003311 stp = find_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003312 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003314 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003315 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003317 if (stp->st_stateowner->so_is_open_owner
3318 && !openowner(stp->st_stateowner)->oo_confirmed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003320 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid,
J. Bruce Fields28dde242011-08-22 10:07:12 -04003321 nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003322 if (status)
3323 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
3341nfsd4_free_delegation_stateid(stateid_t *stateid)
3342{
3343 struct nfs4_delegation *dp = search_for_delegation(stateid);
3344 if (dp)
3345 return nfserr_locks_held;
3346 return nfserr_bad_stateid;
3347}
3348
3349static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003350nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003351{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003352 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003353 return nfserr_locks_held;
3354 release_lock_stateid(stp);
3355 return nfs_ok;
3356}
3357
3358/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003359 * Test if the stateid is valid
3360 */
3361__be32
3362nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3363 struct nfsd4_test_stateid *test_stateid)
3364{
3365 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3366 return nfs_ok;
3367}
3368
3369/*
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003370 * Free a state id
3371 */
3372__be32
3373nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3374 struct nfsd4_free_stateid *free_stateid)
3375{
3376 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003377 struct nfs4_ol_stateid *stp;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003378 __be32 ret;
3379
3380 nfs4_lock_state();
3381 if (is_delegation_stateid(stateid)) {
3382 ret = nfsd4_free_delegation_stateid(stateid);
3383 goto out;
3384 }
3385
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003386 stp = find_stateid(stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003387 if (!stp) {
3388 ret = nfserr_bad_stateid;
3389 goto out;
3390 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003391 ret = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, 1);
J. Bruce Fields81b82962011-08-23 11:03:29 -04003392 if (ret)
3393 goto out;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003394
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003395 if (stp->st_stid.sc_type == NFS4_OPEN_STID) {
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003396 ret = nfserr_locks_held;
3397 goto out;
3398 } else {
3399 ret = nfsd4_free_lock_stateid(stp);
3400 goto out;
3401 }
3402
3403out:
3404 nfs4_unlock_state();
3405 return ret;
3406}
3407
NeilBrown4c4cd222005-07-07 17:59:27 -07003408static inline int
3409setlkflg (int type)
3410{
3411 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3412 RD_STATE : WR_STATE;
3413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003415static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3416{
3417 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3418 return nfserr_bad_stateid;
3419 if (STALE_STATEID(stateid))
3420 return nfserr_stale_stateid;
3421 return nfs_ok;
3422}
3423
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003424static __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 -04003425{
3426 struct svc_fh *current_fh = &cstate->current_fh;
3427 struct nfs4_stateowner *sop = stp->st_stateowner;
3428 __be32 status;
3429
3430 if (nfs4_check_fh(current_fh, stp))
3431 return nfserr_bad_stateid;
3432 status = nfsd4_check_seqid(cstate, sop, seqid);
3433 if (status)
3434 return status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003435 return check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003436}
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438/*
3439 * Checks for sequence id mutating operations.
3440 */
Al Virob37ad282006-10-19 23:28:59 -07003441static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003442nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003443 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003444 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003446 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003448 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3449 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003452 status = nfs4_nospecial_stateid_checks(stateid);
3453 if (status)
3454 return status;
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003455 *stpp = find_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003456 if (*stpp == NULL)
3457 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003458 cstate->replay_owner = (*stpp)->st_stateowner;
3459 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003461 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3462}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003463
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003464static __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 -04003465{
3466 __be32 status;
3467 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003469 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003470 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003471 if (status)
3472 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003473 oo = openowner((*stpp)->st_stateowner);
3474 if (!oo->oo_confirmed)
NeilBrown3a4f98b2005-07-07 17:59:26 -07003475 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003476 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Al Virob37ad282006-10-19 23:28:59 -07003479__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003480nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003481 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Al Virob37ad282006-10-19 23:28:59 -07003483 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003484 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003485 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003488 (int)cstate->current_fh.fh_dentry->d_name.len,
3489 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003491 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003492 if (status)
3493 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 nfs4_lock_state();
3496
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003497 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003498 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003499 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003500 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003501 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003502 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003503 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003504 if (oo->oo_confirmed)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003505 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003506 oo->oo_confirmed = 1;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003507 update_stateid(&stp->st_stid.sc_stateid);
3508 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003509 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003510 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003511
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003512 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003513 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003515 if (!cstate->replay_owner)
3516 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return status;
3518}
3519
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003520static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003525 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3526 nfs4_file_put_access(stp->st_file, i);
3527 __clear_bit(i, &stp->st_access_bmap);
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530}
3531
3532static void
3533reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3534{
3535 int i;
3536 for (i = 0; i < 4; i++) {
3537 if ((i & deny) != i)
3538 __clear_bit(i, bmap);
3539 }
3540}
3541
Al Virob37ad282006-10-19 23:28:59 -07003542__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003543nfsd4_open_downgrade(struct svc_rqst *rqstp,
3544 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003545 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
Al Virob37ad282006-10-19 23:28:59 -07003547 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003548 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003551 (int)cstate->current_fh.fh_dentry->d_name.len,
3552 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003554 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003555 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return nfserr_inval;
3557
3558 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003559 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3560 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003561 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 status = nfserr_inval;
3564 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3565 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3566 stp->st_access_bmap, od->od_share_access);
3567 goto out;
3568 }
3569 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3570 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3571 stp->st_deny_bmap, od->od_share_deny);
3572 goto out;
3573 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003574 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3577
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003578 update_stateid(&stp->st_stid.sc_stateid);
3579 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 status = nfs_ok;
3581out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003582 if (!cstate->replay_owner)
3583 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return status;
3585}
3586
3587/*
3588 * nfs4_unlock_state() called after encode
3589 */
Al Virob37ad282006-10-19 23:28:59 -07003590__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003591nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003592 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Al Virob37ad282006-10-19 23:28:59 -07003594 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003595 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003596 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003599 (int)cstate->current_fh.fh_dentry->d_name.len,
3600 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 nfs4_lock_state();
3603 /* check close_lru for replay */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003604 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3605 &close->cl_stateid, &stp);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003606 if (stp == NULL && status == nfserr_expired) {
3607 /*
3608 * Also, we should make sure this isn't just the result of
3609 * a replayed close:
3610 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003611 oo = search_close_lru(close->cl_stateid.si_stateownerid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003612 /* It's not stale; let's assume it's expired: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003613 if (oo == NULL)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003614 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003615 cstate->replay_owner = &oo->oo_owner;
3616 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003617 if (status)
3618 goto out;
3619 status = nfserr_bad_seqid;
3620 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003621 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003623 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003625 update_stateid(&stp->st_stid.sc_stateid);
3626 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003628 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05003629 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003630
3631 /* place unused nfs4_stateowners on so_close_lru list to be
3632 * released by the laundromat service after the lease period
3633 * to enable us to handle CLOSE replay
3634 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003635 if (list_empty(&oo->oo_owner.so_stateids))
3636 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003638 if (!cstate->replay_owner)
3639 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return status;
3641}
3642
Al Virob37ad282006-10-19 23:28:59 -07003643__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003644nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3645 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003647 struct nfs4_delegation *dp;
3648 stateid_t *stateid = &dr->dr_stateid;
3649 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003650 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003652 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003653 return status;
3654 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003657 status = nfserr_bad_stateid;
3658 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3659 goto out;
3660 status = nfserr_stale_stateid;
3661 if (STALE_STATEID(stateid))
3662 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08003663 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003664 if (!is_delegation_stateid(stateid))
3665 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003666 status = nfserr_expired;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003667 dp = find_delegation_stateid(inode, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003668 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003669 goto out;
J. Bruce Fields28dde242011-08-22 10:07:12 -04003670 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003671 if (status)
3672 goto out;
3673 renew_client(dp->dl_client);
3674
3675 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003677 nfs4_unlock_state();
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return status;
3680}
3681
3682
3683/*
3684 * Lock owner state (byte-range locks)
3685 */
3686#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3687#define LOCK_HASH_BITS 8
3688#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3689#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3690
Benny Halevy87df4de2008-12-15 19:42:03 +02003691static inline u64
3692end_offset(u64 start, u64 len)
3693{
3694 u64 end;
3695
3696 end = start + len;
3697 return end >= start ? end: NFS4_MAX_UINT64;
3698}
3699
3700/* last octet in a range */
3701static inline u64
3702last_byte_offset(u64 start, u64 len)
3703{
3704 u64 end;
3705
3706 BUG_ON(!len);
3707 end = start + len;
3708 return end > start ? end - 1: NFS4_MAX_UINT64;
3709}
3710
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04003711static unsigned int lockownerid_hashval(u32 id)
3712{
3713 return id & LOCK_HASH_MASK;
3714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716static inline unsigned int
3717lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3718 struct xdr_netobj *ownername)
3719{
3720 return (file_hashval(inode) + cl_id
3721 + opaque_hashval(ownername->data, ownername->len))
3722 & LOCK_HASH_MASK;
3723}
3724
3725static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3726static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003728static struct nfs4_delegation *
3729search_for_delegation(stateid_t *stid)
3730{
3731 struct nfs4_file *fp;
3732 struct nfs4_delegation *dp;
3733 struct list_head *pos;
3734 int i;
3735
3736 for (i = 0; i < FILE_HASH_SIZE; i++) {
3737 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3738 list_for_each(pos, &fp->fi_delegations) {
3739 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3740 if (same_stateid(&dp->dl_stateid, stid))
3741 return dp;
3742 }
3743 }
3744 }
3745 return NULL;
3746}
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748static struct nfs4_delegation *
3749find_delegation_stateid(struct inode *ino, stateid_t *stid)
3750{
NeilBrown13cd2182005-06-23 22:03:10 -07003751 struct nfs4_file *fp;
3752 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003754 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3755 STATEID_VAL(stid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003758 if (!fp)
3759 return NULL;
3760 dl = find_delegation_file(fp, stid);
3761 put_nfs4_file(fp);
3762 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
3765/*
3766 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3767 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3768 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3769 * locking, this prevents us from being completely protocol-compliant. The
3770 * real solution to this problem is to start using unsigned file offsets in
3771 * the VFS, but this is a very deep change!
3772 */
3773static inline void
3774nfs4_transform_lock_offset(struct file_lock *lock)
3775{
3776 if (lock->fl_start < 0)
3777 lock->fl_start = OFFSET_MAX;
3778 if (lock->fl_end < 0)
3779 lock->fl_end = OFFSET_MAX;
3780}
3781
NeilBrownd5b90262006-04-10 22:55:22 -07003782/* Hack!: For now, we're defining this just so we can use a pointer to it
3783 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003784static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003785};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787static inline void
3788nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3789{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003790 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
NeilBrownd5b90262006-04-10 22:55:22 -07003792 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003793 lo = (struct nfs4_lockowner *) fl->fl_owner;
3794 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3795 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003796 if (!deny->ld_owner.data)
3797 /* We just don't care that much */
3798 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003799 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3800 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003801 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003802nevermind:
3803 deny->ld_owner.len = 0;
3804 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003805 deny->ld_clientid.cl_boot = 0;
3806 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003809 deny->ld_length = NFS4_MAX_UINT64;
3810 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3812 deny->ld_type = NFS4_READ_LT;
3813 if (fl->fl_type != F_RDLCK)
3814 deny->ld_type = NFS4_WRITE_LT;
3815}
3816
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003817static struct nfs4_lockowner *
3818find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 struct xdr_netobj *owner)
3820{
3821 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3822 struct nfs4_stateowner *op;
3823
3824 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003825 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003826 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828 return NULL;
3829}
3830
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003831static 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 -04003832{
3833 unsigned int idhashval;
3834
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003835 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3836 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3837 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3838 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003839}
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841/*
3842 * Alloc a lock owner structure.
3843 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003844 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 *
3846 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 */
3848
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003849static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003850alloc_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 -04003851 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003853 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3854 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003856 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3857 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003858 /* It is the openowner seqid that will be incremented in encode in the
3859 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003860 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3861 hash_lockowner(lo, strhashval, clp, open_stp);
3862 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003865static struct nfs4_ol_stateid *
3866alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003868 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003869 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
NeilBrown5ac049a2005-06-23 22:03:03 -07003871 stp = nfs4_alloc_stateid();
3872 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003874 list_add(&stp->st_stid.sc_hash, &stateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07003875 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003876 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3877 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003878 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003879 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003881 stp->st_stid.sc_stateid.si_boot = boot_time;
3882 stp->st_stid.sc_stateid.si_stateownerid = lo->lo_owner.so_id;
3883 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003884 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003885 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields0997b172011-03-02 18:01:35 -05003886 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003888 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890out:
3891 return stp;
3892}
3893
NeilBrownfd39ca92005-06-23 22:04:03 -07003894static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895check_lock_length(u64 offset, u64 length)
3896{
Benny Halevy87df4de2008-12-15 19:42:03 +02003897 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 LOFF_OVERFLOW(offset, length)));
3899}
3900
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003901static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003902{
3903 struct nfs4_file *fp = lock_stp->st_file;
3904 int oflag = nfs4_access_to_omode(access);
3905
3906 if (test_bit(access, &lock_stp->st_access_bmap))
3907 return;
3908 nfs4_file_get_access(fp, oflag);
3909 __set_bit(access, &lock_stp->st_access_bmap);
3910}
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912/*
3913 * LOCK operation
3914 */
Al Virob37ad282006-10-19 23:28:59 -07003915__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003916nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003917 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003919 struct nfs4_openowner *open_sop = NULL;
3920 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003921 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003922 struct nfs4_file *fp;
3923 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003925 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003926 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003928 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3932 (long long) lock->lk_offset,
3933 (long long) lock->lk_length);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (check_lock_length(lock->lk_offset, lock->lk_length))
3936 return nfserr_inval;
3937
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003938 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003939 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003940 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3941 return status;
3942 }
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 nfs4_lock_state();
3945
3946 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003947 /*
3948 * Client indicates that this is a new lockowner.
3949 * Use open owner and open stateid to create lock owner and
3950 * lock stateid.
3951 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003952 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003955 if (!nfsd4_has_session(cstate) &&
3956 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003960 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 lock->lk_new_open_seqid,
3962 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003963 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003964 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003966 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003967 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003968 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003969 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003970 &lock->v.new.clientid))
3971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 /* create lockowner and lock stateid */
3973 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003974 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3975 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003977 /* XXX: Do we need to check for duplicate stateowners on
3978 * the same file, or should they just be allowed (and
3979 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003980 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003981 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003982 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003983 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003985 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003986 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 } else {
3989 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003990 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003991 lock->lk_old_lock_seqid,
3992 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003993 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (status)
3995 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003996 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04003997 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003999 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004001 lkflg = setlkflg(lock->lk_type);
4002 status = nfs4_check_openmode(lock_stp, lkflg);
4003 if (status)
4004 goto out;
4005
NeilBrown0dd395d2005-07-07 17:59:15 -07004006 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004007 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004008 goto out;
4009 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004010 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004011 goto out;
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 locks_init_lock(&file_lock);
4014 switch (lock->lk_type) {
4015 case NFS4_READ_LT:
4016 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004017 filp = find_readable_file(lock_stp->st_file);
4018 if (filp)
4019 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 case NFS4_WRITE_LT:
4023 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004024 filp = find_writeable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 default:
4030 status = nfserr_inval;
4031 goto out;
4032 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004033 if (!filp) {
4034 status = nfserr_openmode;
4035 goto out;
4036 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004037 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 file_lock.fl_pid = current->tgid;
4039 file_lock.fl_file = filp;
4040 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004041 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004044 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 nfs4_transform_lock_offset(&file_lock);
4046
4047 /*
4048 * Try to lock the file in the VFS.
4049 * Note: locks.c uses the BKL to protect the inode's lock list.
4050 */
4051
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004052 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004053 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004055 update_stateid(&lock_stp->st_stid.sc_stateid);
4056 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004058 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004059 break;
4060 case (EAGAIN): /* conflock holds conflicting lock */
4061 status = nfserr_denied;
4062 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4063 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 case (EDEADLK):
4066 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004067 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004068 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004069 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004070 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004074 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004075 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004076 if (!cstate->replay_owner)
4077 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return status;
4079}
4080
4081/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004082 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4083 * so we do a temporary open here just to get an open file to pass to
4084 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4085 * inode operation.)
4086 */
4087static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4088{
4089 struct file *file;
4090 int err;
4091
4092 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4093 if (err)
4094 return err;
4095 err = vfs_test_lock(file, lock);
4096 nfsd_close(file);
4097 return err;
4098}
4099
4100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * LOCKT operation
4102 */
Al Virob37ad282006-10-19 23:28:59 -07004103__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004104nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4105 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004110 int error;
Al Virob37ad282006-10-19 23:28:59 -07004111 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004113 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return nfserr_grace;
4115
4116 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4117 return nfserr_inval;
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 nfs4_lock_state();
4120
4121 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004122 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004125 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004128 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 locks_init_lock(&file_lock);
4130 switch (lockt->lt_type) {
4131 case NFS4_READ_LT:
4132 case NFS4_READW_LT:
4133 file_lock.fl_type = F_RDLCK;
4134 break;
4135 case NFS4_WRITE_LT:
4136 case NFS4_WRITEW_LT:
4137 file_lock.fl_type = F_WRLCK;
4138 break;
4139 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004140 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 status = nfserr_inval;
4142 goto out;
4143 }
4144
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004145 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4146 if (lo)
4147 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 file_lock.fl_pid = current->tgid;
4149 file_lock.fl_flags = FL_POSIX;
4150
4151 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004152 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 nfs4_transform_lock_offset(&file_lock);
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004157 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004158 if (error) {
4159 status = nfserrno(error);
4160 goto out;
4161 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004162 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004164 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
4166out:
4167 nfs4_unlock_state();
4168 return status;
4169}
4170
Al Virob37ad282006-10-19 23:28:59 -07004171__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004172nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004173 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004175 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 struct file *filp = NULL;
4177 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004178 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004179 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4182 (long long) locku->lu_offset,
4183 (long long) locku->lu_length);
4184
4185 if (check_lock_length(locku->lu_offset, locku->lu_length))
4186 return nfserr_inval;
4187
4188 nfs4_lock_state();
4189
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004190 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004191 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004192 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004194 filp = find_any_file(stp->st_file);
4195 if (!filp) {
4196 status = nfserr_lock_range;
4197 goto out;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 BUG_ON(!filp);
4200 locks_init_lock(&file_lock);
4201 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004202 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 file_lock.fl_pid = current->tgid;
4204 file_lock.fl_file = filp;
4205 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004206 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 file_lock.fl_start = locku->lu_offset;
4208
Benny Halevy87df4de2008-12-15 19:42:03 +02004209 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 nfs4_transform_lock_offset(&file_lock);
4211
4212 /*
4213 * Try to unlock the file in the VFS.
4214 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004215 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004216 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004217 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 goto out_nfserr;
4219 }
4220 /*
4221 * OK, unlock succeeded; the only thing left to do is update the stateid.
4222 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004223 update_stateid(&stp->st_stid.sc_stateid);
4224 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 nfs4_unlock_state();
4228 return status;
4229
4230out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004231 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 goto out;
4233}
4234
4235/*
4236 * returns
4237 * 1: locks held by lockowner
4238 * 0: no locks held by lockowner
4239 */
4240static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004241check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004244 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int status = 0;
4246
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004247 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004249 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 status = 1;
4251 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004255 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return status;
4257}
4258
Al Virob37ad282006-10-19 23:28:59 -07004259__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004260nfsd4_release_lockowner(struct svc_rqst *rqstp,
4261 struct nfsd4_compound_state *cstate,
4262 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004265 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004266 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004267 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004269 struct list_head matches;
4270 int i;
Al Virob37ad282006-10-19 23:28:59 -07004271 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4274 clid->cl_boot, clid->cl_id);
4275
4276 /* XXX check for lease expiration */
4277
4278 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004279 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 nfs4_lock_state();
4283
NeilBrown3e9e3db2005-06-23 22:04:20 -07004284 status = nfserr_locks_held;
4285 /* XXX: we're doing a linear search through all the lockowners.
4286 * Yipes! For now we'll just hope clients aren't really using
4287 * release_lockowner much, but eventually we have to fix these
4288 * data structures. */
4289 INIT_LIST_HEAD(&matches);
4290 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4291 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004292 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004293 continue;
4294 list_for_each_entry(stp, &sop->so_stateids,
4295 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004296 lo = lockowner(sop);
4297 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004298 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004299 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004302 }
4303 /* Clients probably won't expect us to return with some (but not all)
4304 * of the lockowner state released; so don't release any until all
4305 * have been checked. */
4306 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004307 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 lo = list_entry(matches.next, struct nfs4_lockowner,
4309 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004310 /* unhash_stateowner deletes so_perclient only
4311 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004312 list_del(&lo->lo_list);
4313 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
4315out:
4316 nfs4_unlock_state();
4317 return status;
4318}
4319
4320static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004321alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
NeilBrowna55370a2005-06-23 22:03:52 -07004323 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
NeilBrownc7b9a452005-06-23 22:04:30 -07004326int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004327nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004328{
4329 unsigned int strhashval = clientstr_hashval(name);
4330 struct nfs4_client *clp;
4331
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004332 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004333 return clp ? 1 : 0;
4334}
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336/*
4337 * failure => all reset bets are off, nfserr_no_grace...
4338 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004339int
4340nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
4342 unsigned int strhashval;
4343 struct nfs4_client_reclaim *crp = NULL;
4344
NeilBrowna55370a2005-06-23 22:03:52 -07004345 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4346 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (!crp)
4348 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004349 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 INIT_LIST_HEAD(&crp->cr_strhash);
4351 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004352 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 reclaim_str_hashtbl_size++;
4354 return 1;
4355}
4356
4357static void
4358nfs4_release_reclaim(void)
4359{
4360 struct nfs4_client_reclaim *crp = NULL;
4361 int i;
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4364 while (!list_empty(&reclaim_str_hashtbl[i])) {
4365 crp = list_entry(reclaim_str_hashtbl[i].next,
4366 struct nfs4_client_reclaim, cr_strhash);
4367 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 kfree(crp);
4369 reclaim_str_hashtbl_size--;
4370 }
4371 }
4372 BUG_ON(reclaim_str_hashtbl_size);
4373}
4374
4375/*
4376 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004377static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378nfs4_find_reclaim_client(clientid_t *clid)
4379{
4380 unsigned int strhashval;
4381 struct nfs4_client *clp;
4382 struct nfs4_client_reclaim *crp = NULL;
4383
4384
4385 /* find clientid in conf_id_hashtbl */
4386 clp = find_confirmed_client(clid);
4387 if (clp == NULL)
4388 return NULL;
4389
NeilBrowna55370a2005-06-23 22:03:52 -07004390 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4391 clp->cl_name.len, clp->cl_name.data,
4392 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004395 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004397 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return crp;
4399 }
4400 }
4401 return NULL;
4402}
4403
4404/*
4405* Called from OPEN. Look for clientid in reclaim list.
4406*/
Al Virob37ad282006-10-19 23:28:59 -07004407__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408nfs4_check_open_reclaim(clientid_t *clid)
4409{
NeilBrowndfc83562005-06-23 22:03:16 -07004410 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
NeilBrownac4d8ff22005-06-23 22:03:30 -07004413/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004415int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004416nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004418 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004420 status = nfsd4_init_slabs();
4421 if (status)
4422 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4424 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4425 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4426 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4427 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004428 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004430 for (i = 0; i < SESSION_HASH_SIZE; i++)
4431 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 for (i = 0; i < FILE_HASH_SIZE; i++) {
4433 INIT_LIST_HEAD(&file_hashtbl[i]);
4434 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004435 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4436 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4437 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004439 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4442 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4443 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 INIT_LIST_HEAD(&close_lru);
4447 INIT_LIST_HEAD(&client_lru);
4448 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004449 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004450 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004451}
4452
NeilBrown190e4fb2005-06-23 22:04:25 -07004453static void
4454nfsd4_load_reboot_recovery_data(void)
4455{
4456 int status;
4457
NeilBrown0964a3d2005-06-23 22:04:32 -07004458 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004459 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004460 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004461 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004462 if (status)
4463 printk("NFSD: Failure reading reboot recovery data\n");
4464}
4465
Meelap Shahc2f1a552007-07-17 04:04:39 -07004466/*
4467 * Since the lifetime of a delegation isn't limited to that of an open, a
4468 * client may quite reasonably hang on to a delegation as long as it has
4469 * the inode cached. This becomes an obvious problem the first time a
4470 * client's inode cache approaches the size of the server's total memory.
4471 *
4472 * For now we avoid this problem by imposing a hard limit on the number
4473 * of delegations, which varies according to the server's memory size.
4474 */
4475static void
4476set_max_delegations(void)
4477{
4478 /*
4479 * Allow at most 4 delegations per megabyte of RAM. Quick
4480 * estimates suggest that in the worst case (where every delegation
4481 * is for a different inode), a delegation could take about 1.5K,
4482 * giving a worst case usage of about 6% of memory.
4483 */
4484 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4485}
4486
NeilBrownac4d8ff22005-06-23 22:03:30 -07004487/* initialization to perform when the nfsd service is started: */
4488
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004489static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004490__nfs4_state_start(void)
4491{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004492 int ret;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004495 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004496 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004497 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004498 ret = set_callback_cred();
4499 if (ret)
4500 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004501 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004502 if (laundry_wq == NULL)
4503 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004504 ret = nfsd4_create_callback_queue();
4505 if (ret)
4506 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004507 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004508 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004509 return 0;
4510out_free_laundry:
4511 destroy_workqueue(laundry_wq);
4512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004515int
NeilBrown76a35502005-06-23 22:03:26 -07004516nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
NeilBrown190e4fb2005-06-23 22:04:25 -07004518 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004519 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static void
4523__nfs4_state_shutdown(void)
4524{
4525 int i;
4526 struct nfs4_client *clp = NULL;
4527 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 struct list_head *pos, *next, reaplist;
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4531 while (!list_empty(&conf_id_hashtbl[i])) {
4532 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4533 expire_client(clp);
4534 }
4535 while (!list_empty(&unconf_str_hashtbl[i])) {
4536 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4537 expire_client(clp);
4538 }
4539 }
4540 INIT_LIST_HEAD(&reaplist);
4541 spin_lock(&recall_lock);
4542 list_for_each_safe(pos, next, &del_recall_lru) {
4543 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4544 list_move(&dp->dl_recall_lru, &reaplist);
4545 }
4546 spin_unlock(&recall_lock);
4547 list_for_each_safe(pos, next, &reaplist) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 list_del_init(&dp->dl_recall_lru);
4550 unhash_delegation(dp);
4551 }
4552
NeilBrown190e4fb2005-06-23 22:04:25 -07004553 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554}
4555
4556void
4557nfs4_state_shutdown(void)
4558{
Tejun Heoafe2c512010-12-14 16:21:17 +01004559 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004560 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004561 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 nfs4_lock_state();
4563 nfs4_release_reclaim();
4564 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004566 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}