blob: 581a5d01b005ccf2882e840536efc15b7964101f [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 Fieldsefc4bb42010-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
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400221static inline void hash_stid(struct nfs4_stid *stid)
222{
223 stateid_t *s = &stid->sc_stateid;
224 unsigned int hashval;
225
226 hashval = stateid_hashval(s->si_stateownerid, s->si_fileid);
227 list_add(&stid->sc_hash, &stateid_hashtbl[hashval]);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400231alloc_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 -0700232{
233 struct nfs4_delegation *dp;
234 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400237 /*
238 * Major work on the lease subsystem (for example, to support
239 * calbacks on stat) will be required before we can support
240 * write delegations properly.
241 */
242 if (type != NFS4_OPEN_DELEGATE_READ)
243 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700244 if (fp->fi_had_conflict)
245 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700246 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700247 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700248 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
249 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700251 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700252 INIT_LIST_HEAD(&dp->dl_perfile);
253 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 INIT_LIST_HEAD(&dp->dl_recall_lru);
255 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700256 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 dp->dl_type = type;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400259 dp->dl_stid.sc_stateid.si_boot = boot_time;
260 dp->dl_stid.sc_stateid.si_stateownerid = current_delegid++;
261 dp->dl_stid.sc_stateid.si_fileid = 0;
262 dp->dl_stid.sc_stateid.si_generation = 1;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500263 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dp->dl_time = 0;
265 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500266 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return dp;
268}
269
270void
271nfs4_put_delegation(struct nfs4_delegation *dp)
272{
273 if (atomic_dec_and_test(&dp->dl_count)) {
274 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700275 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700276 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700277 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279}
280
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500281static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500283 if (atomic_dec_and_test(&fp->fi_delegees)) {
284 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
285 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400286 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500287 fp->fi_deleg_file = NULL;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/* Called under the state lock. */
292static void
293unhash_delegation(struct nfs4_delegation *dp)
294{
NeilBrownea1da632005-06-23 22:04:17 -0700295 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500297 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 list_del_init(&dp->dl_recall_lru);
299 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500300 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 nfs4_put_delegation(dp);
302}
303
304/*
305 * SETCLIENTID state
306 */
307
Benny Halevy36acb662010-05-12 00:13:04 +0300308/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300309static DEFINE_SPINLOCK(client_lock);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* Hash tables for nfs4_clientid state */
312#define CLIENT_HASH_BITS 4
313#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
314#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
315
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400316static unsigned int clientid_hashval(u32 id)
317{
318 return id & CLIENT_HASH_MASK;
319}
320
321static unsigned int clientstr_hashval(const char *name)
322{
323 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
327 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
328 * used in reboot/reset lease grace period processing
329 *
330 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
331 * setclientid_confirmed info.
332 *
333 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
334 * setclientid info.
335 *
336 * client_lru holds client queue ordered by nfs4_client.cl_time
337 * for lease renewal.
338 *
339 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
340 * for last close replay.
341 */
342static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
343static int reclaim_str_hashtbl_size = 0;
344static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
345static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
346static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
347static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
348static struct list_head client_lru;
349static struct list_head close_lru;
350
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400351/*
352 * We store the NONE, READ, WRITE, and BOTH bits separately in the
353 * st_{access,deny}_bmap field of the stateid, in order to track not
354 * only what share bits are currently in force, but also what
355 * combinations of share bits previous opens have used. This allows us
356 * to enforce the recommendation of rfc 3530 14.2.19 that the server
357 * return an error if the client attempt to downgrade to a combination
358 * of share bits not explicable by closing some of its previous opens.
359 *
360 * XXX: This enforcement is actually incomplete, since we don't keep
361 * track of access/deny bit combinations; so, e.g., we allow:
362 *
363 * OPEN allow read, deny write
364 * OPEN allow both, deny none
365 * DOWNGRADE allow read, deny none
366 *
367 * which we should reject.
368 */
369static void
370set_access(unsigned int *access, unsigned long bmap) {
371 int i;
372
373 *access = 0;
374 for (i = 1; i < 4; i++) {
375 if (test_bit(i, &bmap))
376 *access |= i;
377 }
378}
379
380static void
381set_deny(unsigned int *deny, unsigned long bmap) {
382 int i;
383
384 *deny = 0;
385 for (i = 0; i < 4; i++) {
386 if (test_bit(i, &bmap))
387 *deny |= i ;
388 }
389}
390
391static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400392test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400393 unsigned int access, deny;
394
395 set_access(&access, stp->st_access_bmap);
396 set_deny(&deny, stp->st_deny_bmap);
397 if ((access & open->op_share_deny) || (deny & open->op_share_access))
398 return 0;
399 return 1;
400}
401
402static int nfs4_access_to_omode(u32 access)
403{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400404 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400405 case NFS4_SHARE_ACCESS_READ:
406 return O_RDONLY;
407 case NFS4_SHARE_ACCESS_WRITE:
408 return O_WRONLY;
409 case NFS4_SHARE_ACCESS_BOTH:
410 return O_RDWR;
411 }
412 BUG();
413}
414
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400415static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500416{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400417 list_del(&stp->st_stid.sc_hash);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500418 list_del(&stp->st_perfile);
419 list_del(&stp->st_perstateowner);
420}
421
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400422static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500423{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400424 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500425
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800426 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400427 for (i = 1; i < 4; i++) {
428 if (test_bit(i, &stp->st_access_bmap))
429 nfs4_file_put_access(stp->st_file,
430 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400431 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400432 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800433 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400434 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400435 stp->st_file = NULL;
436}
437
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400438static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400439{
440 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500441 kmem_cache_free(stateid_slab, stp);
442}
443
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400444static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500445{
446 struct file *file;
447
448 unhash_generic_stateid(stp);
449 file = find_any_file(stp->st_file);
450 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400451 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500452 free_generic_stateid(stp);
453}
454
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400455static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500456{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400457 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500458
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400459 list_del(&lo->lo_owner.so_idhash);
460 list_del(&lo->lo_owner.so_strhash);
461 list_del(&lo->lo_perstateid);
462 while (!list_empty(&lo->lo_owner.so_stateids)) {
463 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400464 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500465 release_lock_stateid(stp);
466 }
467}
468
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400469static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500470{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400471 unhash_lockowner(lo);
472 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500473}
474
475static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400476release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500477{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400478 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500479
480 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400481 lo = list_entry(open_stp->st_lockowners.next,
482 struct nfs4_lockowner, lo_perstateid);
483 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500484 }
485}
486
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400487static void release_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500488{
489 unhash_generic_stateid(stp);
490 release_stateid_lockowners(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500491 free_generic_stateid(stp);
492}
493
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400494static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500495{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400496 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500497
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400498 list_del(&oo->oo_owner.so_idhash);
499 list_del(&oo->oo_owner.so_strhash);
500 list_del(&oo->oo_perclient);
501 while (!list_empty(&oo->oo_owner.so_stateids)) {
502 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400503 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500504 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500505 }
506}
507
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400508static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500509{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400510 unhash_openowner(oo);
511 list_del(&oo->oo_close_lru);
512 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500513}
514
Marc Eshel5282fd72009-04-03 08:27:52 +0300515#define SESSION_HASH_SIZE 512
516static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
517
518static inline int
519hash_sessionid(struct nfs4_sessionid *sessionid)
520{
521 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
522
523 return sid->sequence % SESSION_HASH_SIZE;
524}
525
526static inline void
527dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
528{
529 u32 *ptr = (u32 *)(&sessionid->data[0]);
530 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
531}
532
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300533static void
534gen_sessionid(struct nfsd4_session *ses)
535{
536 struct nfs4_client *clp = ses->se_client;
537 struct nfsd4_sessionid *sid;
538
539 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
540 sid->clientid = clp->cl_clientid;
541 sid->sequence = current_sessionid++;
542 sid->reserved = 0;
543}
544
545/*
Andy Adamsona6496372009-08-28 08:45:01 -0400546 * The protocol defines ca_maxresponssize_cached to include the size of
547 * the rpc header, but all we need to cache is the data starting after
548 * the end of the initial SEQUENCE operation--the rest we regenerate
549 * each time. Therefore we can advertise a ca_maxresponssize_cached
550 * value that is the number of bytes in our cache plus a few additional
551 * bytes. In order to stay on the safe side, and not promise more than
552 * we can cache, those additional bytes must be the minimum possible: 24
553 * bytes of rpc header (xid through accept state, with AUTH_NULL
554 * verifier), 12 for the compound header (with zero-length tag), and 44
555 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300556 */
Andy Adamsona6496372009-08-28 08:45:01 -0400557#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
558
Andy Adamson557ce262009-08-28 08:45:04 -0400559static void
560free_session_slots(struct nfsd4_session *ses)
561{
562 int i;
563
564 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
565 kfree(ses->se_slots[i]);
566}
567
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400568/*
569 * We don't actually need to cache the rpc and session headers, so we
570 * can allocate a little less for each slot:
571 */
572static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
573{
574 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
575}
576
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400577static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300578{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400579 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
580 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300581
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400582 return size;
583}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300584
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400585/*
586 * XXX: If we run out of reserved DRC memory we could (up to a point)
587 * re-negotiate active sessions and reduce their slot usage to make
588 * rooom for new connections. For now we just fail the create session.
589 */
590static int nfsd4_get_drc_mem(int slotsize, u32 num)
591{
592 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300593
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400594 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400595
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400596 spin_lock(&nfsd_drc_lock);
597 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
598 nfsd_drc_max_mem - nfsd_drc_mem_used);
599 num = min_t(int, num, avail / slotsize);
600 nfsd_drc_mem_used += num * slotsize;
601 spin_unlock(&nfsd_drc_lock);
602
603 return num;
604}
605
606static void nfsd4_put_drc_mem(int slotsize, int num)
607{
608 spin_lock(&nfsd_drc_lock);
609 nfsd_drc_mem_used -= slotsize * num;
610 spin_unlock(&nfsd_drc_lock);
611}
612
613static struct nfsd4_session *alloc_session(int slotsize, int numslots)
614{
615 struct nfsd4_session *new;
616 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300617
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400618 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400619 + sizeof(struct nfsd4_session) > PAGE_SIZE);
620 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400621
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400622 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300623 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400624 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400625 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400626 for (i = 0; i < numslots; i++) {
627 mem = sizeof(struct nfsd4_slot) + slotsize;
628 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
629 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400630 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400631 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400632 return new;
633out_free:
634 while (i--)
635 kfree(new->se_slots[i]);
636 kfree(new);
637 return NULL;
638}
639
640static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
641{
642 u32 maxrpc = nfsd_serv->sv_max_mesg;
643
644 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800645 new->maxresp_cached = min_t(u32, req->maxresp_cached,
646 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400647 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
648 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
649 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
650}
651
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400652static void free_conn(struct nfsd4_conn *c)
653{
654 svc_xprt_put(c->cn_xprt);
655 kfree(c);
656}
657
658static void nfsd4_conn_lost(struct svc_xpt_user *u)
659{
660 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
661 struct nfs4_client *clp = c->cn_session->se_client;
662
663 spin_lock(&clp->cl_lock);
664 if (!list_empty(&c->cn_persession)) {
665 list_del(&c->cn_persession);
666 free_conn(c);
667 }
668 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500669 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400670}
671
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400672static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400673{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400674 struct nfsd4_conn *conn;
675
676 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
677 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400678 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400679 svc_xprt_get(rqstp->rq_xprt);
680 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400681 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400682 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
683 return conn;
684}
685
J. Bruce Fields328ead22010-09-29 16:11:06 -0400686static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
687{
688 conn->cn_session = ses;
689 list_add(&conn->cn_persession, &ses->se_conns);
690}
691
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400692static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
693{
694 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400695
696 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400697 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400698 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400699}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400700
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400701static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400702{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400703 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400704 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400705}
706
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400707static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400708{
709 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400710 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400711
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400712 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400713 if (!conn)
714 return nfserr_jukebox;
715 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400716 ret = nfsd4_register_conn(conn);
717 if (ret)
718 /* oops; xprt is already down: */
719 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400720 return nfs_ok;
721}
722
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400723static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
724{
725 u32 dir = NFS4_CDFC4_FORE;
726
727 if (ses->se_flags & SESSION4_BACK_CHAN)
728 dir |= NFS4_CDFC4_BACK;
729
730 return nfsd4_new_conn(rqstp, ses, dir);
731}
732
733/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400734static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400735{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400736 struct nfs4_client *clp = s->se_client;
737 struct nfsd4_conn *c;
738
739 spin_lock(&clp->cl_lock);
740 while (!list_empty(&s->se_conns)) {
741 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
742 list_del_init(&c->cn_persession);
743 spin_unlock(&clp->cl_lock);
744
745 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
746 free_conn(c);
747
748 spin_lock(&clp->cl_lock);
749 }
750 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400751}
752
753void free_session(struct kref *kref)
754{
755 struct nfsd4_session *ses;
756 int mem;
757
758 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400759 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400760 spin_lock(&nfsd_drc_lock);
761 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
762 nfsd_drc_mem_used -= mem;
763 spin_unlock(&nfsd_drc_lock);
764 free_session_slots(ses);
765 kfree(ses);
766}
767
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400768static 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 -0400769{
770 struct nfsd4_session *new;
771 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
772 int numslots, slotsize;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400773 int status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400774 int idx;
775
776 /*
777 * Note decreasing slot size below client's request may
778 * make it difficult for client to function correctly, whereas
779 * decreasing the number of slots will (just?) affect
780 * performance. When short on memory we therefore prefer to
781 * decrease number of slots instead of their size.
782 */
783 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
784 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800785 if (numslots < 1)
786 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400787
788 new = alloc_session(slotsize, numslots);
789 if (!new) {
790 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400791 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400792 }
793 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400794
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300795 new->se_client = clp;
796 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300797
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400798 INIT_LIST_HEAD(&new->se_conns);
799
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400800 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300801 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400802 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300803 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400804 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300805 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300806 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400807 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300808 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400809 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300810 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300811
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400812 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400813 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400814 if (status) {
815 free_session(&new->se_ref);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400816 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400817 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400818 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400819 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400820 /*
821 * This is a little silly; with sessions there's no real
822 * use for the callback address. Use the peer address
823 * as a reasonable default for now, but consider fixing
824 * the rpc client not to require an address in the
825 * future:
826 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400827 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
828 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400829 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400830 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400831 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300832}
833
Benny Halevy9089f1b2010-05-12 00:12:26 +0300834/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300835static struct nfsd4_session *
836find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
837{
838 struct nfsd4_session *elem;
839 int idx;
840
841 dump_sessionid(__func__, sessionid);
842 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300843 /* Search in the appropriate list */
844 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300845 if (!memcmp(elem->se_sessionid.data, sessionid->data,
846 NFS4_MAX_SESSIONID_LEN)) {
847 return elem;
848 }
849 }
850
851 dprintk("%s: session not found\n", __func__);
852 return NULL;
853}
854
Benny Halevy9089f1b2010-05-12 00:12:26 +0300855/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300856static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300857unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300858{
859 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400860 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300861 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400862 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300863}
864
Benny Halevy36acb662010-05-12 00:13:04 +0300865/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300867renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Benny Halevy07cd4902010-05-12 00:13:41 +0300869 if (is_client_expired(clp)) {
870 dprintk("%s: client (clientid %08x/%08x) already expired\n",
871 __func__,
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 return;
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * Move client to the end to the LRU list.
879 */
880 dprintk("renewing client (clientid %08x/%08x)\n",
881 clp->cl_clientid.cl_boot,
882 clp->cl_clientid.cl_id);
883 list_move_tail(&clp->cl_lru, &client_lru);
884 clp->cl_time = get_seconds();
885}
886
Benny Halevy36acb662010-05-12 00:13:04 +0300887static inline void
888renew_client(struct nfs4_client *clp)
889{
890 spin_lock(&client_lock);
891 renew_client_locked(clp);
892 spin_unlock(&client_lock);
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
896static int
897STALE_CLIENTID(clientid_t *clid)
898{
899 if (clid->cl_boot == boot_time)
900 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300901 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
902 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 1;
904}
905
906/*
907 * XXX Should we use a slab cache ?
908 * This type of memory management is somewhat inefficient, but we use it
909 * anyway since SETCLIENTID is not a common operation.
910 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500911static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 struct nfs4_client *clp;
914
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500915 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
916 if (clp == NULL)
917 return NULL;
918 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
919 if (clp->cl_name.data == NULL) {
920 kfree(clp);
921 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500923 memcpy(clp->cl_name.data, name.data, name.len);
924 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return clp;
926}
927
928static inline void
929free_client(struct nfs4_client *clp)
930{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400931 while (!list_empty(&clp->cl_sessions)) {
932 struct nfsd4_session *ses;
933 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
934 se_perclnt);
935 list_del(&ses->se_perclnt);
936 nfsd4_put_session(ses);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (clp->cl_cred.cr_group_info)
939 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500940 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 kfree(clp->cl_name.data);
942 kfree(clp);
943}
944
Benny Halevyd7682982010-05-12 00:13:54 +0300945void
946release_session_client(struct nfsd4_session *session)
947{
948 struct nfs4_client *clp = session->se_client;
949
950 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
951 return;
952 if (is_client_expired(clp)) {
953 free_client(clp);
954 session->se_client = NULL;
955 } else
956 renew_client_locked(clp);
957 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300958}
959
Benny Halevy84d38ac2010-05-12 00:13:16 +0300960/* must be called under the client_lock */
961static inline void
962unhash_client_locked(struct nfs4_client *clp)
963{
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400964 struct nfsd4_session *ses;
965
Benny Halevy07cd4902010-05-12 00:13:41 +0300966 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300967 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400968 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -0400969 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
970 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400971 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static void
975expire_client(struct nfs4_client *clp)
976{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400977 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct list_head reaplist;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 INIT_LIST_HEAD(&reaplist);
982 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700983 while (!list_empty(&clp->cl_delegations)) {
984 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -0700985 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 list_move(&dp->dl_recall_lru, &reaplist);
987 }
988 spin_unlock(&recall_lock);
989 while (!list_empty(&reaplist)) {
990 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
991 list_del_init(&dp->dl_recall_lru);
992 unhash_delegation(dp);
993 }
NeilBrownea1da632005-06-23 22:04:17 -0700994 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400995 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
996 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400998 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500999 if (clp->cl_cb_conn.cb_xprt)
1000 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001001 list_del(&clp->cl_idhash);
1002 list_del(&clp->cl_strhash);
1003 spin_lock(&client_lock);
1004 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001005 if (atomic_read(&clp->cl_refcount) == 0)
1006 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001007 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001010static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1011{
1012 memcpy(target->cl_verifier.data, source->data,
1013 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001016static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1017{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1019 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1020}
1021
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001022static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 target->cr_uid = source->cr_uid;
1025 target->cr_gid = source->cr_gid;
1026 target->cr_group_info = source->cr_group_info;
1027 get_group_info(target->cr_group_info);
1028}
1029
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001030static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001031{
NeilBrowna55370a2005-06-23 22:03:52 -07001032 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001036same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1037{
1038 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001042same_clid(clientid_t *cl1, clientid_t *cl2)
1043{
1044 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/* XXX what about NGROUP */
1048static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001049same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1050{
1051 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001054static void gen_clid(struct nfs4_client *clp)
1055{
1056 static u32 current_clientid = 1;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 clp->cl_clientid.cl_boot = boot_time;
1059 clp->cl_clientid.cl_id = current_clientid++;
1060}
1061
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001062static void gen_confirm(struct nfs4_client *clp)
1063{
1064 static u32 i;
1065 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001068 *p++ = get_seconds();
1069 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
J. Bruce Fields4581d142011-09-06 14:56:09 -04001072static int
1073same_stateid(stateid_t *id_one, stateid_t *id_two)
1074{
1075 if (id_one->si_stateownerid != id_two->si_stateownerid)
1076 return 0;
1077 return id_one->si_fileid == id_two->si_fileid;
1078}
1079
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001080static struct nfs4_ol_stateid *find_stateid(stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001081{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001082 struct nfs4_stid *s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001083 unsigned int hashval;
1084
1085 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001086 list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash)
1087 if (same_stateid(&s->sc_stateid, t))
1088 return openlockstateid(s);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001089 return NULL;
1090}
1091
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001092static struct nfs4_ol_stateid *find_stateid_by_type(stateid_t *t, char typemask)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001093{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001094 struct nfs4_ol_stateid *s;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001095
1096 s = find_stateid(t);
1097 if (!s)
1098 return NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001099 if (typemask & s->st_stid.sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001100 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001101 return NULL;
1102}
1103
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001104static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1105 struct svc_rqst *rqstp, nfs4_verifier *verf)
1106{
1107 struct nfs4_client *clp;
1108 struct sockaddr *sa = svc_addr(rqstp);
1109 char *princ;
1110
1111 clp = alloc_client(name);
1112 if (clp == NULL)
1113 return NULL;
1114
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001115 INIT_LIST_HEAD(&clp->cl_sessions);
1116
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001117 princ = svc_gss_principal(rqstp);
1118 if (princ) {
1119 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1120 if (clp->cl_principal == NULL) {
1121 free_client(clp);
1122 return NULL;
1123 }
1124 }
1125
1126 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001127 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001128 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001129 INIT_LIST_HEAD(&clp->cl_idhash);
1130 INIT_LIST_HEAD(&clp->cl_strhash);
1131 INIT_LIST_HEAD(&clp->cl_openowners);
1132 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001133 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba252011-01-10 16:44:41 -05001134 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001135 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001136 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001137 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001138 clear_bit(0, &clp->cl_cb_slot_busy);
1139 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1140 copy_verf(clp, verf);
1141 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1142 clp->cl_flavor = rqstp->rq_flavor;
1143 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1144 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001145 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001146 return clp;
1147}
1148
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001149static int check_name(struct xdr_netobj name)
1150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (name.len == 0)
1152 return 0;
1153 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04001154 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156 }
1157 return 1;
1158}
1159
NeilBrownfd39ca92005-06-23 22:04:03 -07001160static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1162{
1163 unsigned int idhashval;
1164
1165 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1166 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1167 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001168 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
NeilBrownfd39ca92005-06-23 22:04:03 -07001171static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172move_to_confirmed(struct nfs4_client *clp)
1173{
1174 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1175 unsigned int strhashval;
1176
1177 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001178 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001179 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001180 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 renew_client(clp);
1182}
1183
1184static struct nfs4_client *
1185find_confirmed_client(clientid_t *clid)
1186{
1187 struct nfs4_client *clp;
1188 unsigned int idhashval = clientid_hashval(clid->cl_id);
1189
1190 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001191 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return clp;
1193 }
1194 return NULL;
1195}
1196
1197static struct nfs4_client *
1198find_unconfirmed_client(clientid_t *clid)
1199{
1200 struct nfs4_client *clp;
1201 unsigned int idhashval = clientid_hashval(clid->cl_id);
1202
1203 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001204 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return clp;
1206 }
1207 return NULL;
1208}
1209
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001210static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001211{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001212 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001213}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001214
NeilBrown28ce6052005-06-23 22:03:56 -07001215static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001216find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001217{
1218 struct nfs4_client *clp;
1219
1220 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001221 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001222 return clp;
1223 }
1224 return NULL;
1225}
1226
1227static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001228find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001229{
1230 struct nfs4_client *clp;
1231
1232 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001233 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001234 return clp;
1235 }
1236 return NULL;
1237}
1238
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001239static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1240{
1241 switch (family) {
1242 case AF_INET:
1243 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1244 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1245 return;
1246 case AF_INET6:
1247 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1248 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1249 return;
1250 }
1251}
1252
NeilBrownfd39ca92005-06-23 22:04:03 -07001253static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001254gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001256 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001257 struct sockaddr *sa = svc_addr(rqstp);
1258 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001259 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jeff Layton7077ecb2009-08-14 12:57:58 -04001261 /* Currently, we only support tcp and tcp6 for the callback channel */
1262 if (se->se_callback_netid_len == 3 &&
1263 !memcmp(se->se_callback_netid_val, "tcp", 3))
1264 expected_family = AF_INET;
1265 else if (se->se_callback_netid_len == 4 &&
1266 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1267 expected_family = AF_INET6;
1268 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto out_err;
1270
J. Bruce Fields07263f12010-05-31 19:09:40 -04001271 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001272 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001273 (struct sockaddr *)&conn->cb_addr,
1274 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001275
J. Bruce Fields07263f12010-05-31 19:09:40 -04001276 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001278
J. Bruce Fields07263f12010-05-31 19:09:40 -04001279 if (conn->cb_addr.ss_family == AF_INET6)
1280 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001281
J. Bruce Fields07263f12010-05-31 19:09:40 -04001282 conn->cb_prog = se->se_callback_prog;
1283 conn->cb_ident = se->se_callback_ident;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001284 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return;
1286out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001287 conn->cb_addr.ss_family = AF_UNSPEC;
1288 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001289 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 "will not receive delegations\n",
1291 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return;
1294}
1295
Andy Adamson074fe892009-04-03 08:28:15 +03001296/*
Andy Adamson557ce262009-08-28 08:45:04 -04001297 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001298 */
1299void
1300nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1301{
Andy Adamson557ce262009-08-28 08:45:04 -04001302 struct nfsd4_slot *slot = resp->cstate.slot;
1303 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001304
Andy Adamson557ce262009-08-28 08:45:04 -04001305 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001306
Andy Adamson557ce262009-08-28 08:45:04 -04001307 slot->sl_opcnt = resp->opcnt;
1308 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001309
1310 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001311 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001312 return;
1313 }
Andy Adamson557ce262009-08-28 08:45:04 -04001314 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1315 base = (char *)resp->cstate.datap -
1316 (char *)resp->xbuf->head[0].iov_base;
1317 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1318 slot->sl_datalen))
1319 WARN("%s: sessions DRC could not cache compound\n", __func__);
1320 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001321}
1322
1323/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001324 * Encode the replay sequence operation from the slot values.
1325 * If cachethis is FALSE encode the uncached rep error on the next
1326 * operation which sets resp->p and increments resp->opcnt for
1327 * nfs4svc_encode_compoundres.
1328 *
Andy Adamson074fe892009-04-03 08:28:15 +03001329 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001330static __be32
1331nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1332 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001333{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001334 struct nfsd4_op *op;
1335 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001336
Andy Adamsonabfabf82009-07-23 19:02:18 -04001337 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001338 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001339
1340 /* Encode the replayed sequence operation */
1341 op = &args->ops[resp->opcnt - 1];
1342 nfsd4_encode_operation(resp, op);
1343
1344 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001345 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001346 op = &args->ops[resp->opcnt++];
1347 op->status = nfserr_retry_uncached_rep;
1348 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001349 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001350 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001351}
1352
1353/*
Andy Adamson557ce262009-08-28 08:45:04 -04001354 * The sequence operation is not cached because we can use the slot and
1355 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001356 */
1357__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001358nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1359 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001360{
Andy Adamson557ce262009-08-28 08:45:04 -04001361 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001362 __be32 status;
1363
Andy Adamson557ce262009-08-28 08:45:04 -04001364 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001365
Andy Adamsonabfabf82009-07-23 19:02:18 -04001366 /* Either returns 0 or nfserr_retry_uncached */
1367 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1368 if (status == nfserr_retry_uncached_rep)
1369 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001370
Andy Adamson557ce262009-08-28 08:45:04 -04001371 /* The sequence operation has been encoded, cstate->datap set. */
1372 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001373
Andy Adamson557ce262009-08-28 08:45:04 -04001374 resp->opcnt = slot->sl_opcnt;
1375 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1376 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001377
1378 return status;
1379}
1380
Andy Adamson0733d212009-04-03 08:28:01 +03001381/*
1382 * Set the exchange_id flags returned by the server.
1383 */
1384static void
1385nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1386{
1387 /* pNFS is not supported */
1388 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1389
1390 /* Referrals are supported, Migration is not. */
1391 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1392
1393 /* set the wire flags to return to client. */
1394 clid->flags = new->cl_exchange_flags;
1395}
1396
Al Virob37ad282006-10-19 23:28:59 -07001397__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001398nfsd4_exchange_id(struct svc_rqst *rqstp,
1399 struct nfsd4_compound_state *cstate,
1400 struct nfsd4_exchange_id *exid)
1401{
Andy Adamson0733d212009-04-03 08:28:01 +03001402 struct nfs4_client *unconf, *conf, *new;
1403 int status;
1404 unsigned int strhashval;
1405 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001406 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001407 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001408 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001409
Jeff Layton363168b2009-08-14 12:57:56 -04001410 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001411 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001412 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001413 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001414 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001415
1416 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1417 return nfserr_inval;
1418
1419 /* Currently only support SP4_NONE */
1420 switch (exid->spa_how) {
1421 case SP4_NONE:
1422 break;
1423 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001424 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001425 default:
1426 BUG(); /* checked by xdr code */
1427 case SP4_MACH_CRED:
1428 return nfserr_serverfault; /* no excuse :-/ */
1429 }
1430
1431 status = nfs4_make_rec_clidname(dname, &exid->clname);
1432
1433 if (status)
1434 goto error;
1435
1436 strhashval = clientstr_hashval(dname);
1437
1438 nfs4_lock_state();
1439 status = nfs_ok;
1440
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001441 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001442 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001443 if (!clp_used_exchangeid(conf)) {
1444 status = nfserr_clid_inuse; /* XXX: ? */
1445 goto out;
1446 }
Andy Adamson0733d212009-04-03 08:28:01 +03001447 if (!same_verf(&verf, &conf->cl_verifier)) {
1448 /* 18.35.4 case 8 */
1449 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1450 status = nfserr_not_same;
1451 goto out;
1452 }
1453 /* Client reboot: destroy old state */
1454 expire_client(conf);
1455 goto out_new;
1456 }
1457 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1458 /* 18.35.4 case 9 */
1459 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1460 status = nfserr_perm;
1461 goto out;
1462 }
1463 expire_client(conf);
1464 goto out_new;
1465 }
Andy Adamson0733d212009-04-03 08:28:01 +03001466 /*
1467 * Set bit when the owner id and verifier map to an already
1468 * confirmed client id (18.35.3).
1469 */
1470 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1471
1472 /*
1473 * Falling into 18.35.4 case 2, possible router replay.
1474 * Leave confirmed record intact and return same result.
1475 */
1476 copy_verf(conf, &verf);
1477 new = conf;
1478 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001479 }
1480
1481 /* 18.35.4 case 7 */
1482 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1483 status = nfserr_noent;
1484 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001485 }
1486
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001487 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001488 if (unconf) {
1489 /*
1490 * Possible retry or client restart. Per 18.35.4 case 4,
1491 * a new unconfirmed record should be generated regardless
1492 * of whether any properties have changed.
1493 */
1494 expire_client(unconf);
1495 }
1496
1497out_new:
1498 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001499 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001500 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001501 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001502 goto out;
1503 }
1504
Andy Adamson0733d212009-04-03 08:28:01 +03001505 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001506 add_to_unconfirmed(new, strhashval);
1507out_copy:
1508 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1509 exid->clientid.cl_id = new->cl_clientid.cl_id;
1510
Andy Adamson38eb76a2009-04-03 08:28:32 +03001511 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001512 nfsd4_set_ex_flags(new, exid);
1513
1514 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001515 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001516 status = nfs_ok;
1517
1518out:
1519 nfs4_unlock_state();
1520error:
1521 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1522 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001523}
1524
Benny Halevyb85d4c02009-04-03 08:28:08 +03001525static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001526check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001527{
Andy Adamson88e588d2009-07-23 19:02:15 -04001528 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1529 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001530
1531 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001532 if (slot_inuse) {
1533 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001534 return nfserr_jukebox;
1535 else
1536 return nfserr_seq_misordered;
1537 }
1538 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001539 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001540 return nfs_ok;
1541 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001542 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001543 return nfserr_replay_cache;
1544 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001545 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001546 return nfs_ok;
1547 /* Misordered replay or misordered new request */
1548 return nfserr_seq_misordered;
1549}
1550
Andy Adamson49557cc2009-07-23 19:02:16 -04001551/*
1552 * Cache the create session result into the create session single DRC
1553 * slot cache by saving the xdr structure. sl_seqid has been set.
1554 * Do this for solo or embedded create session operations.
1555 */
1556static void
1557nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1558 struct nfsd4_clid_slot *slot, int nfserr)
1559{
1560 slot->sl_status = nfserr;
1561 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1562}
1563
1564static __be32
1565nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1566 struct nfsd4_clid_slot *slot)
1567{
1568 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1569 return slot->sl_status;
1570}
1571
Mi Jinlong1b74c252011-07-14 14:50:17 +08001572#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1573 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* version, opcount, opcode */ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, cache */ \
1578 4 ) * sizeof(__be32))
1579
1580#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1581 2 + /* verifier: AUTH_NULL, length 0 */\
1582 1 + /* status */ \
1583 1 + /* MIN tag is length with zero, only length */ \
1584 3 + /* opcount, opcode, opstatus*/ \
1585 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1586 /* seqid, slotID, slotID, slotID, status */ \
1587 5 ) * sizeof(__be32))
1588
1589static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1590{
1591 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1592 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1593}
1594
Andy Adamson069b6ad2009-04-03 08:27:58 +03001595__be32
1596nfsd4_create_session(struct svc_rqst *rqstp,
1597 struct nfsd4_compound_state *cstate,
1598 struct nfsd4_create_session *cr_ses)
1599{
Jeff Layton363168b2009-08-14 12:57:56 -04001600 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001601 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001602 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001603 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001604 bool confirm_me = false;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001605 int status = 0;
1606
Mi Jinlonga62573d2011-03-23 17:57:07 +08001607 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1608 return nfserr_inval;
1609
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001610 nfs4_lock_state();
1611 unconf = find_unconfirmed_client(&cr_ses->clientid);
1612 conf = find_confirmed_client(&cr_ses->clientid);
1613
1614 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001615 cs_slot = &conf->cl_cs_slot;
1616 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001617 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001618 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001619 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001620 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001621 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001622 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001623 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001624 status = nfserr_seq_misordered;
1625 dprintk("Sequence misordered!\n");
1626 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001627 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001628 goto out;
1629 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001630 } else if (unconf) {
1631 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001632 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001633 status = nfserr_clid_inuse;
1634 goto out;
1635 }
1636
Andy Adamson49557cc2009-07-23 19:02:16 -04001637 cs_slot = &unconf->cl_cs_slot;
1638 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001639 if (status) {
1640 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001641 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001642 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001643 }
1644
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001645 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001646 conf = unconf;
1647 } else {
1648 status = nfserr_stale_clientid;
1649 goto out;
1650 }
1651
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001652 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001653 * XXX: we should probably set this at creation time, and check
1654 * for consistent minorversion use throughout:
1655 */
1656 conf->cl_minorversion = 1;
1657 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001658 * We do not support RDMA or persistent sessions
1659 */
1660 cr_ses->flags &= ~SESSION4_PERSIST;
1661 cr_ses->flags &= ~SESSION4_RDMA;
1662
Mi Jinlong1b74c252011-07-14 14:50:17 +08001663 status = nfserr_toosmall;
1664 if (check_forechannel_attrs(cr_ses->fore_channel))
1665 goto out;
1666
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001667 status = nfserr_jukebox;
1668 new = alloc_init_session(rqstp, conf, cr_ses);
1669 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001670 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001671 status = nfs_ok;
1672 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001673 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001674 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1675 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001676 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001677 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001678
Andy Adamson49557cc2009-07-23 19:02:16 -04001679 /* cache solo and embedded create sessions under the state lock */
1680 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001681 if (confirm_me)
1682 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001683out:
1684 nfs4_unlock_state();
1685 dprintk("%s returns %d\n", __func__, ntohl(status));
1686 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001687}
1688
J. Bruce Fields57716352010-04-21 12:27:19 -04001689static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1690{
1691 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1692 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1693
1694 return argp->opcnt == resp->opcnt;
1695}
1696
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001697static __be32 nfsd4_map_bcts_dir(u32 *dir)
1698{
1699 switch (*dir) {
1700 case NFS4_CDFC4_FORE:
1701 case NFS4_CDFC4_BACK:
1702 return nfs_ok;
1703 case NFS4_CDFC4_FORE_OR_BOTH:
1704 case NFS4_CDFC4_BACK_OR_BOTH:
1705 *dir = NFS4_CDFC4_BOTH;
1706 return nfs_ok;
1707 };
1708 return nfserr_inval;
1709}
1710
1711__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1712 struct nfsd4_compound_state *cstate,
1713 struct nfsd4_bind_conn_to_session *bcts)
1714{
1715 __be32 status;
1716
1717 if (!nfsd4_last_compound_op(rqstp))
1718 return nfserr_not_only_op;
1719 spin_lock(&client_lock);
1720 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1721 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1722 * client_lock iself: */
1723 if (cstate->session) {
1724 nfsd4_get_session(cstate->session);
1725 atomic_inc(&cstate->session->se_client->cl_refcount);
1726 }
1727 spin_unlock(&client_lock);
1728 if (!cstate->session)
1729 return nfserr_badsession;
1730
1731 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001732 if (!status)
1733 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1734 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001735}
1736
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001737static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1738{
1739 if (!session)
1740 return 0;
1741 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1742}
1743
Andy Adamson069b6ad2009-04-03 08:27:58 +03001744__be32
1745nfsd4_destroy_session(struct svc_rqst *r,
1746 struct nfsd4_compound_state *cstate,
1747 struct nfsd4_destroy_session *sessionid)
1748{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001749 struct nfsd4_session *ses;
1750 u32 status = nfserr_badsession;
1751
1752 /* Notes:
1753 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1754 * - Should we return nfserr_back_chan_busy if waiting for
1755 * callbacks on to-be-destroyed session?
1756 * - Do we need to clear any callback info from previous session?
1757 */
1758
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001759 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001760 if (!nfsd4_last_compound_op(r))
1761 return nfserr_not_only_op;
1762 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001763 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001764 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001765 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1766 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001767 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001768 goto out;
1769 }
1770
1771 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001772 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001773
Benny Halevyab707e152010-05-12 00:14:06 +03001774 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001775 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001776 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001777
1778 nfsd4_del_conns(ses);
1779
Benny Halevye10e0cf2009-04-03 08:28:38 +03001780 nfsd4_put_session(ses);
1781 status = nfs_ok;
1782out:
1783 dprintk("%s returns %d\n", __func__, ntohl(status));
1784 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001785}
1786
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001787static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001788{
1789 struct nfsd4_conn *c;
1790
1791 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001792 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001793 return c;
1794 }
1795 }
1796 return NULL;
1797}
1798
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001799static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001800{
1801 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001802 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001803 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001804
1805 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001806 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001807 if (c) {
1808 spin_unlock(&clp->cl_lock);
1809 free_conn(new);
1810 return;
1811 }
1812 __nfsd4_hash_conn(new, ses);
1813 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001814 ret = nfsd4_register_conn(new);
1815 if (ret)
1816 /* oops; xprt is already down: */
1817 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001818 return;
1819}
1820
Mi Jinlong868b89c2011-04-27 09:09:58 +08001821static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1822{
1823 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1824
1825 return args->opcnt > session->se_fchannel.maxops;
1826}
1827
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001828static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1829 struct nfsd4_session *session)
1830{
1831 struct xdr_buf *xb = &rqstp->rq_arg;
1832
1833 return xb->len > session->se_fchannel.maxreq_sz;
1834}
1835
Andy Adamson069b6ad2009-04-03 08:27:58 +03001836__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001837nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001838 struct nfsd4_compound_state *cstate,
1839 struct nfsd4_sequence *seq)
1840{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001841 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001842 struct nfsd4_session *session;
1843 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001844 struct nfsd4_conn *conn;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001845 int status;
1846
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001847 if (resp->opcnt != 1)
1848 return nfserr_sequence_pos;
1849
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001850 /*
1851 * Will be either used or freed by nfsd4_sequence_check_conn
1852 * below.
1853 */
1854 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1855 if (!conn)
1856 return nfserr_jukebox;
1857
Benny Halevy9089f1b2010-05-12 00:12:26 +03001858 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001859 status = nfserr_badsession;
1860 session = find_in_sessionid_hashtbl(&seq->sessionid);
1861 if (!session)
1862 goto out;
1863
Mi Jinlong868b89c2011-04-27 09:09:58 +08001864 status = nfserr_too_many_ops;
1865 if (nfsd4_session_too_many_ops(rqstp, session))
1866 goto out;
1867
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001868 status = nfserr_req_too_big;
1869 if (nfsd4_request_too_big(rqstp, session))
1870 goto out;
1871
Benny Halevyb85d4c02009-04-03 08:28:08 +03001872 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001873 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001874 goto out;
1875
Andy Adamson557ce262009-08-28 08:45:04 -04001876 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001877 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1878
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001879 /* We do not negotiate the number of slots yet, so set the
1880 * maxslots to the session maxreqs which is used to encode
1881 * sr_highest_slotid and the sr_target_slot id to maxslots */
1882 seq->maxslots = session->se_fchannel.maxreqs;
1883
Andy Adamson88e588d2009-07-23 19:02:15 -04001884 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001885 if (status == nfserr_replay_cache) {
1886 cstate->slot = slot;
1887 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001888 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001889 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001890 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001891 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001892 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001893 }
1894 if (status)
1895 goto out;
1896
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001897 nfsd4_sequence_check_conn(conn, session);
1898 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001899
Benny Halevyb85d4c02009-04-03 08:28:08 +03001900 /* Success! bump slot seqid */
1901 slot->sl_inuse = true;
1902 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001903 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001904
1905 cstate->slot = slot;
1906 cstate->session = session;
1907
Benny Halevyb85d4c02009-04-03 08:28:08 +03001908out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001909 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001910 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001911 struct nfs4_client *clp = session->se_client;
1912
Benny Halevy36acb662010-05-12 00:13:04 +03001913 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001914 atomic_inc(&clp->cl_refcount);
1915 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1916 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001917 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001918 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03001919 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001920 dprintk("%s: return %d\n", __func__, ntohl(status));
1921 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001922}
1923
1924__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001925nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1926{
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001927 int status = 0;
1928
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001929 if (rc->rca_one_fs) {
1930 if (!cstate->current_fh.fh_dentry)
1931 return nfserr_nofilehandle;
1932 /*
1933 * We don't take advantage of the rca_one_fs case.
1934 * That's OK, it's optional, we can safely ignore it.
1935 */
1936 return nfs_ok;
1937 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001938
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001939 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001940 status = nfserr_complete_already;
1941 if (cstate->session->se_client->cl_firststate)
1942 goto out;
1943
1944 status = nfserr_stale_clientid;
1945 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001946 /*
1947 * The following error isn't really legal.
1948 * But we only get here if the client just explicitly
1949 * destroyed the client. Surely it no longer cares what
1950 * error it gets back on an operation for the dead
1951 * client.
1952 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001953 goto out;
1954
1955 status = nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001956 nfsd4_create_clid_dir(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001957out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001958 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08001959 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001960}
1961
1962__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001963nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1964 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct xdr_netobj clname = {
1967 .len = setclid->se_namelen,
1968 .data = setclid->se_name,
1969 };
1970 nfs4_verifier clverifier = setclid->se_verf;
1971 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001972 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001973 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001974 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001977 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
NeilBrowna55370a2005-06-23 22:03:52 -07001979 status = nfs4_make_rec_clidname(dname, &clname);
1980 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001981 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /*
1984 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1985 * We get here on a DRC miss.
1986 */
1987
NeilBrowna55370a2005-06-23 22:03:52 -07001988 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001991 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07001992 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001993 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001995 if (clp_used_exchangeid(conf))
1996 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001997 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04001998 char addr_str[INET6_ADDRSTRLEN];
1999 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2000 sizeof(addr_str));
2001 dprintk("NFSD: setclientid: string in use by client "
2002 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 goto out;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002006 /*
2007 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2008 * has a description of SETCLIENTID request processing consisting
2009 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2010 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002011 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002012 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002014 /*
2015 * RFC 3530 14.2.33 CASE 4:
2016 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
2018 if (unconf)
2019 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002020 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002021 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002024 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002026 * RFC 3530 14.2.33 CASE 1:
2027 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002029 if (unconf) {
2030 /* Note this is removing unconfirmed {*x***},
2031 * which is stronger than RFC recommended {vxc**}.
2032 * This has the advantage that there is at most
2033 * one {*x***} in either list at any time.
2034 */
2035 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002037 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002038 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 } else if (!unconf) {
2042 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002043 * RFC 3530 14.2.33 CASE 2:
2044 * probable client reboot; state will be removed if
2045 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002047 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002048 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002051 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002052 /*
2053 * RFC 3530 14.2.33 CASE 3:
2054 * probable client reboot; state will be removed if
2055 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
2057 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002058 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002059 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002063 /*
2064 * XXX: we should probably set this at creation time, and check
2065 * for consistent minorversion use throughout:
2066 */
2067 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002068 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002069 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2071 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2072 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2073 status = nfs_ok;
2074out:
2075 nfs4_unlock_state();
2076 return status;
2077}
2078
2079
2080/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002081 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2082 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2083 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
Al Virob37ad282006-10-19 23:28:59 -07002085__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002086nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2087 struct nfsd4_compound_state *cstate,
2088 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Jeff Layton363168b2009-08-14 12:57:56 -04002090 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07002091 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2093 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002094 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 if (STALE_CLIENTID(clid))
2097 return nfserr_stale_clientid;
2098 /*
2099 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2100 * We get here on a DRC miss.
2101 */
2102
2103 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002104
2105 conf = find_confirmed_client(clid);
2106 unconf = find_unconfirmed_client(clid);
2107
2108 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04002109 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002110 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04002111 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07002112 goto out;
2113
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002114 /*
2115 * section 14.2.34 of RFC 3530 has a description of
2116 * SETCLIENTID_CONFIRM request processing consisting
2117 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2118 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002119 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002120 /*
2121 * RFC 3530 14.2.34 CASE 1:
2122 * callback update
2123 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002124 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 status = nfserr_clid_inuse;
2126 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002127 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2128 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002129 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002133 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002134 /*
2135 * RFC 3530 14.2.34 CASE 2:
2136 * probable retransmitted request; play it safe and
2137 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002138 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002139 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002141 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002143 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002144 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002145 /*
2146 * RFC 3530 14.2.34 CASE 3:
2147 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002148 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002149 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 status = nfserr_clid_inuse;
2151 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002152 unsigned int hash =
2153 clientstr_hashval(unconf->cl_recdir);
2154 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002155 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002156 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07002157 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002158 expire_client(conf);
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002161 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002162 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002163 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002165 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2166 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002167 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002168 /*
2169 * RFC 3530 14.2.34 CASE 4:
2170 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07002173 } else {
NeilBrown08e89872005-06-23 22:04:11 -07002174 /* check that we have hit one of the cases...*/
2175 status = nfserr_clid_inuse;
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 nfs4_unlock_state();
2179 return status;
2180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/* OPEN Share state helper functions */
2183static inline struct nfs4_file *
2184alloc_init_file(struct inode *ino)
2185{
2186 struct nfs4_file *fp;
2187 unsigned int hashval = file_hashval(ino);
2188
NeilBrowne60d4392005-06-23 22:03:01 -07002189 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2190 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002191 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07002193 INIT_LIST_HEAD(&fp->fi_stateids);
2194 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 fp->fi_inode = igrab(ino);
2196 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07002197 fp->fi_had_conflict = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002198 fp->fi_lease = NULL;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002199 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2200 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Pavel Emelyanov47cee542010-05-17 20:00:37 +04002201 spin_lock(&recall_lock);
2202 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2203 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return fp;
2205 }
2206 return NULL;
2207}
2208
2209static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002210nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002211{
NeilBrowne60d4392005-06-23 22:03:01 -07002212 if (*slab == NULL)
2213 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002214 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002215 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002216}
2217
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002218void
NeilBrowne60d4392005-06-23 22:03:01 -07002219nfsd4_free_slabs(void)
2220{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002221 nfsd4_free_slab(&openowner_slab);
2222 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002223 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002224 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002225 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228static int
2229nfsd4_init_slabs(void)
2230{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002231 openowner_slab = kmem_cache_create("nfsd4_openowners",
2232 sizeof(struct nfs4_openowner), 0, 0, NULL);
2233 if (openowner_slab == NULL)
2234 goto out_nomem;
2235 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2236 sizeof(struct nfs4_openowner), 0, 0, NULL);
2237 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002238 goto out_nomem;
2239 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002240 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002241 if (file_slab == NULL)
2242 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002243 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002244 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002245 if (stateid_slab == NULL)
2246 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002247 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002248 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002249 if (deleg_slab == NULL)
2250 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002252out_nomem:
2253 nfsd4_free_slabs();
2254 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002258void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002260 kfree(oo->oo_owner.so_owner.data);
2261 kmem_cache_free(openowner_slab, oo);
2262}
2263
2264void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2265{
2266 kfree(lo->lo_owner.so_owner.data);
2267 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002270static void init_nfs4_replay(struct nfs4_replay *rp)
2271{
2272 rp->rp_status = nfserr_serverfault;
2273 rp->rp_buflen = 0;
2274 rp->rp_buf = rp->rp_ibuf;
2275}
2276
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002277static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 struct nfs4_stateowner *sop;
2280
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002281 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002282 if (!sop)
2283 return NULL;
2284
2285 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2286 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002287 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002290 sop->so_owner.len = owner->len;
2291
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002292 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002293 sop->so_id = current_ownerid++;
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002294 sop->so_client = clp;
2295 init_nfs4_replay(&sop->so_replay);
2296 return sop;
2297}
2298
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002299static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002300{
2301 unsigned int idhashval;
2302
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002303 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2304 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2305 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2306 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002309static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002311 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002313 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2314 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002316 oo->oo_owner.so_is_open_owner = 1;
2317 oo->oo_owner.so_seqid = open->op_seqid;
2318 oo->oo_confirmed = 0;
2319 oo->oo_time = 0;
2320 INIT_LIST_HEAD(&oo->oo_close_lru);
2321 hash_openowner(oo, clp, strhashval);
2322 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325static inline void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002326init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002327 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
NeilBrownea1da632005-06-23 22:04:17 -07002329 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002330 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002331 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002332 stp->st_stid.sc_type = NFS4_OPEN_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002333 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002334 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002336 stp->st_stid.sc_stateid.si_boot = boot_time;
2337 stp->st_stid.sc_stateid.si_stateownerid = oo->oo_owner.so_id;
2338 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04002339 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002340 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04002341 hash_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 stp->st_access_bmap = 0;
2343 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03002344 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2345 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002347 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002351move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002353 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002355 list_move_tail(&oo->oo_close_lru, &close_lru);
2356 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002360same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2361 clientid_t *clid)
2362{
2363 return (sop->so_owner.len == owner->len) &&
2364 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2365 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002368static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2370{
2371 struct nfs4_stateowner *so = NULL;
2372
J. Bruce Fields506f2752011-08-11 18:50:18 -04002373 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002374 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002375 return container_of(so, struct nfs4_openowner, oo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377 return NULL;
2378}
2379
2380/* search file_hashtbl[] for file */
2381static struct nfs4_file *
2382find_file(struct inode *ino)
2383{
2384 unsigned int hashval = file_hashval(ino);
2385 struct nfs4_file *fp;
2386
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002387 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002389 if (fp->fi_inode == ino) {
2390 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002391 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002395 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return NULL;
2397}
2398
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002399static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002400{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002401 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002402 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002403 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2404 return 0;
2405 x &= ~NFS4_SHARE_ACCESS_MASK;
2406 if (minorversion && x) {
2407 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2408 return 0;
2409 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2410 return 0;
2411 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2412 }
2413 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002414 return 0;
2415 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002416}
2417
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002418static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002419{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05002420 /* Note: unlike access bits, deny bits may be zero. */
2421 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 * Called to check deny when READ with all zero stateid or
2426 * WRITE with all zero or all one stateid
2427 */
Al Virob37ad282006-10-19 23:28:59 -07002428static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2430{
2431 struct inode *ino = current_fh->fh_dentry->d_inode;
2432 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002433 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002434 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 dprintk("NFSD: nfs4_share_conflict\n");
2437
2438 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002439 if (!fp)
2440 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002441 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002443 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2444 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2445 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
NeilBrown13cd2182005-06-23 22:03:10 -07002448 ret = nfs_ok;
2449out:
2450 put_nfs4_file(fp);
2451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002454static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* We're assuming the state code never drops its reference
2457 * without first removing the lease. Since we're in this lease
2458 * callback (and since the lease code is serialized by the kernel
2459 * lock) we know the server hasn't removed the lease yet, we know
2460 * it's safe to take a reference: */
2461 atomic_inc(&dp->dl_count);
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Arnd Bergmann460781b2010-11-17 16:26:56 +01002465 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 dp->dl_time = get_seconds();
2467
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002468 nfsd4_cb_recall(dp);
2469}
2470
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002471/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002472static void nfsd_break_deleg_cb(struct file_lock *fl)
2473{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002474 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2475 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002476
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002477 BUG_ON(!fp);
2478 /* We assume break_lease is only called once per lease: */
2479 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002480 /*
2481 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002482 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002483 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002484 */
2485 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002487 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002488 fp->fi_had_conflict = true;
2489 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2490 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002491 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494static
2495int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2496{
2497 if (arg & F_UNLCK)
2498 return lease_modify(onlist, arg);
2499 else
2500 return -EAGAIN;
2501}
2502
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002503static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002504 .lm_break = nfsd_break_deleg_cb,
2505 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506};
2507
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002508static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2509{
2510 if (nfsd4_has_session(cstate))
2511 return nfs_ok;
2512 if (seqid == so->so_seqid - 1)
2513 return nfserr_replay_me;
2514 if (seqid == so->so_seqid)
2515 return nfs_ok;
2516 return nfserr_bad_seqid;
2517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Al Virob37ad282006-10-19 23:28:59 -07002519__be32
Andy Adamson66689582009-04-03 08:28:45 +03002520nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2521 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 clientid_t *clientid = &open->op_clientid;
2524 struct nfs4_client *clp = NULL;
2525 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002526 struct nfs4_openowner *oo = NULL;
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002527 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002530 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 if (STALE_CLIENTID(&open->op_clientid))
2533 return nfserr_stale_clientid;
2534
J. Bruce Fields506f2752011-08-11 18:50:18 -04002535 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002536 oo = find_openstateowner_str(strhashval, open);
2537 open->op_openowner = oo;
2538 if (!oo) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002539 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 clp = find_confirmed_client(clientid);
2541 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002542 return nfserr_expired;
2543 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002545 if (!oo->oo_confirmed) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002546 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002547 clp = oo->oo_owner.so_client;
2548 release_openowner(oo);
2549 open->op_openowner = NULL;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002550 goto renew;
2551 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002552 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002553 if (status)
2554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555renew:
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002556 if (open->op_openowner == NULL) {
2557 oo = alloc_init_open_stateowner(strhashval, clp, open);
2558 if (oo == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002559 return nfserr_jukebox;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002560 open->op_openowner = oo;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002561 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002562 list_del_init(&oo->oo_close_lru);
2563 renew_client(oo->oo_owner.so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002564 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Al Virob37ad282006-10-19 23:28:59 -07002567static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002568nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2569{
2570 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2571 return nfserr_openmode;
2572 else
2573 return nfs_ok;
2574}
2575
NeilBrown52f4fb42005-06-23 22:02:49 -07002576static struct nfs4_delegation *
2577find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2578{
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002579 struct nfs4_delegation *dp;
NeilBrown52f4fb42005-06-23 22:02:49 -07002580
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002581 spin_lock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002582 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002583 if (dp->dl_stid.sc_stateid.si_stateownerid == stid->si_stateownerid) {
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002584 spin_unlock(&recall_lock);
2585 return dp;
2586 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002587 spin_unlock(&recall_lock);
J. Bruce Fields32b007b2011-03-06 19:11:03 -05002588 return NULL;
NeilBrown52f4fb42005-06-23 22:02:49 -07002589}
2590
Daniel Mackc47d8322011-05-16 16:38:14 +02002591static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002592{
2593 share_access &= ~NFS4_SHARE_WANT_MASK;
2594
2595 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2596}
2597
Al Virob37ad282006-10-19 23:28:59 -07002598static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002599nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2600 struct nfs4_delegation **dp)
2601{
2602 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002603 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002604
2605 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2606 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002607 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002608 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002609 status = nfs4_check_delegmode(*dp, flags);
2610 if (status)
2611 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002612out:
2613 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2614 return nfs_ok;
2615 if (status)
2616 return status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002617 open->op_openowner->oo_confirmed = 1;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002618 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002619}
2620
Al Virob37ad282006-10-19 23:28:59 -07002621static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002622nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002624 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002625 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
NeilBrown8beefa22005-06-23 22:03:08 -07002627 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 /* ignore lock owners */
2629 if (local->st_stateowner->so_is_open_owner == 0)
2630 continue;
2631 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002632 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *stpp = local;
2634 /* check for conflicting share reservations */
2635 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002636 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002638 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002641static inline struct nfs4_ol_stateid *
NeilBrown5ac049a2005-06-23 22:03:03 -07002642nfs4_alloc_stateid(void)
2643{
2644 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2645}
2646
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002647static inline int nfs4_access_to_access(u32 nfs4_access)
2648{
2649 int flags = 0;
2650
2651 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2652 flags |= NFSD_MAY_READ;
2653 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2654 flags |= NFSD_MAY_WRITE;
2655 return flags;
2656}
2657
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002658static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2659 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002660{
2661 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002662 int oflag = nfs4_access_to_omode(open->op_share_access);
2663 int access = nfs4_access_to_access(open->op_share_access);
2664
2665 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2666 * allowing it to break the lease and return EAGAIN leaves the
2667 * client unable to make progress in returning the delegation */
2668 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2669 access |= NFSD_MAY_NOT_BREAK_LEASE;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002670
2671 if (!fp->fi_fds[oflag]) {
2672 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2673 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002674 if (status)
2675 return status;
2676 }
2677 nfs4_file_get_access(fp, oflag);
2678
2679 return nfs_ok;
2680}
2681
Al Virob37ad282006-10-19 23:28:59 -07002682static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002683nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002684 struct nfs4_file *fp, struct svc_fh *cur_fh,
2685 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002687 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002688 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
NeilBrown5ac049a2005-06-23 22:03:03 -07002690 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (stp == NULL)
J. Bruce Fields3e772462011-08-10 19:07:33 -04002692 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002694 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002695 if (status) {
2696 kmem_cache_free(stateid_slab, stp);
2697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *stpp = stp;
2700 return 0;
2701}
2702
Al Virob37ad282006-10-19 23:28:59 -07002703static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2705 struct nfsd4_open *open)
2706{
2707 struct iattr iattr = {
2708 .ia_valid = ATTR_SIZE,
2709 .ia_size = 0,
2710 };
2711 if (!open->op_truncate)
2712 return 0;
2713 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002714 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2716}
2717
Al Virob37ad282006-10-19 23:28:59 -07002718static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002719nfs4_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 -07002720{
J. Bruce Fields7d947842010-08-20 18:09:31 -04002721 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2722 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002723 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
J. Bruce Fields7d947842010-08-20 18:09:31 -04002725 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002726 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002727 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002728 if (status)
2729 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 status = nfsd4_truncate(rqstp, cur_fh, open);
2732 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002733 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002734 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002735 nfs4_file_put_access(fp, oflag);
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return status;
2738 }
2739 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002740 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002741 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 return nfs_ok;
2744}
2745
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static void
NeilBrown37515172005-07-07 17:59:16 -07002748nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002750 open->op_openowner->oo_confirmed = 1;
2751 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002754/* Should we give out recallable state?: */
2755static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2756{
2757 if (clp->cl_cb_state == NFSD4_CB_UP)
2758 return true;
2759 /*
2760 * In the sessions case, since we don't have to establish a
2761 * separate connection for callbacks, we assume it's OK
2762 * until we hear otherwise:
2763 */
2764 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2765}
2766
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002767static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2768{
2769 struct file_lock *fl;
2770
2771 fl = locks_alloc_lock();
2772 if (!fl)
2773 return NULL;
2774 locks_init_lock(fl);
2775 fl->fl_lmops = &nfsd_lease_mng_ops;
2776 fl->fl_flags = FL_LEASE;
2777 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2778 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002779 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002780 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002781 return fl;
2782}
2783
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002784static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2785{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002786 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002787 struct file_lock *fl;
2788 int status;
2789
2790 fl = nfs4_alloc_init_lease(dp, flag);
2791 if (!fl)
2792 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002793 fl->fl_file = find_readable_file(fp);
2794 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2795 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002796 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002797 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002798 locks_free_lock(fl);
2799 return -ENOMEM;
2800 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002801 fp->fi_lease = fl;
2802 fp->fi_deleg_file = fl->fl_file;
2803 get_file(fp->fi_deleg_file);
2804 atomic_set(&fp->fi_delegees, 1);
2805 list_add(&dp->dl_perfile, &fp->fi_delegations);
2806 return 0;
2807}
2808
2809static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2810{
2811 struct nfs4_file *fp = dp->dl_file;
2812
2813 if (!fp->fi_lease)
2814 return nfs4_setlease(dp, flag);
2815 spin_lock(&recall_lock);
2816 if (fp->fi_had_conflict) {
2817 spin_unlock(&recall_lock);
2818 return -EAGAIN;
2819 }
2820 atomic_inc(&fp->fi_delegees);
2821 list_add(&dp->dl_perfile, &fp->fi_delegations);
2822 spin_unlock(&recall_lock);
2823 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002824 return 0;
2825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
2828 * Attempt to hand out a delegation.
2829 */
2830static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002831nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002834 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002835 int cb_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 int status, flag = 0;
2837
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002838 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002840 open->op_recall = 0;
2841 switch (open->op_claim_type) {
2842 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002843 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002844 open->op_recall = 1;
2845 flag = open->op_delegate_type;
2846 if (flag == NFS4_OPEN_DELEGATE_NONE)
2847 goto out;
2848 break;
2849 case NFS4_OPEN_CLAIM_NULL:
2850 /* Let's not give out any delegations till everyone's
2851 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002852 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002853 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002854 if (!cb_up || !oo->oo_confirmed)
NeilBrown7b190fe2005-06-23 22:03:23 -07002855 goto out;
2856 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2857 flag = NFS4_OPEN_DELEGATE_WRITE;
2858 else
2859 flag = NFS4_OPEN_DELEGATE_READ;
2860 break;
2861 default:
2862 goto out;
2863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002865 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002866 if (dp == NULL)
2867 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002868 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002869 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002870 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002872 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002874 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002875 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002877 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2878 && flag == NFS4_OPEN_DELEGATE_NONE
2879 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002880 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 open->op_delegate_type = flag;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002882 return;
2883out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002884 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002885out_no_deleg:
2886 flag = NFS4_OPEN_DELEGATE_NONE;
2887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
2890/*
2891 * called with nfs4_lock_state() held.
2892 */
Al Virob37ad282006-10-19 23:28:59 -07002893__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2895{
Andy Adamson66689582009-04-03 08:28:45 +03002896 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 struct nfs4_file *fp = NULL;
2898 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002899 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002900 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002901 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002904 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002905 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 goto out;
2907 /*
2908 * Lookup file; if found, lookup stateid and check open request,
2909 * and check for delegations in the process of being recalled.
2910 * If not found, create the nfs4_file struct
2911 */
2912 fp = find_file(ino);
2913 if (fp) {
2914 if ((status = nfs4_check_open(fp, open, &stp)))
2915 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002916 status = nfs4_check_deleg(fp, open, &dp);
2917 if (status)
2918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002920 status = nfserr_bad_stateid;
2921 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2922 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04002923 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 fp = alloc_init_file(ino);
2925 if (fp == NULL)
2926 goto out;
2927 }
2928
2929 /*
2930 * OPEN the file, or upgrade an existing OPEN.
2931 * If truncate fails, the OPEN fails.
2932 */
2933 if (stp) {
2934 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002935 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (status)
2937 goto out;
2938 } else {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002939 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07002940 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto out;
J. Bruce Fields881ea2b2011-09-06 17:20:34 -04002942 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 status = nfsd4_truncate(rqstp, current_fh, open);
2944 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002945 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 goto out;
2947 }
2948 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002949 update_stateid(&stp->st_stid.sc_stateid);
2950 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002952 if (nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002953 open->op_openowner->oo_confirmed = 1;
Andy Adamson66689582009-04-03 08:28:45 +03002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /*
2956 * Attempt to hand out a delegation. No error return, because the
2957 * OPEN succeeds even if we fail.
2958 */
2959 nfs4_open_delegation(current_fh, open, stp);
2960
2961 status = nfs_ok;
2962
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002963 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002964 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965out:
NeilBrown13cd2182005-06-23 22:03:10 -07002966 if (fp)
2967 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002968 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2969 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 /*
2971 * To finish the open response, we just need to set the rflags.
2972 */
2973 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002974 if (!open->op_openowner->oo_confirmed &&
Andy Adamson66689582009-04-03 08:28:45 +03002975 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2977
2978 return status;
2979}
2980
Al Virob37ad282006-10-19 23:28:59 -07002981__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002982nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2983 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
2985 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002986 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 nfs4_lock_state();
2989 dprintk("process_renew(%08x/%08x): starting\n",
2990 clid->cl_boot, clid->cl_id);
2991 status = nfserr_stale_clientid;
2992 if (STALE_CLIENTID(clid))
2993 goto out;
2994 clp = find_confirmed_client(clid);
2995 status = nfserr_expired;
2996 if (clp == NULL) {
2997 /* We assume the client took too long to RENEW. */
2998 dprintk("nfsd4_renew: clientid not found!\n");
2999 goto out;
3000 }
3001 renew_client(clp);
3002 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003003 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003004 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 goto out;
3006 status = nfs_ok;
3007out:
3008 nfs4_unlock_state();
3009 return status;
3010}
3011
Daniel Mackc47d8322011-05-16 16:38:14 +02003012static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003013};
3014
NeilBrowna76b4312005-06-23 22:04:01 -07003015static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003016nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003017{
3018 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07003019 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003020 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003021 /*
3022 * Now that every NFSv4 client has had the chance to recover and
3023 * to see the (possibly new, possibly shorter) lease time, we
3024 * can safely set the next grace time to the current lease time:
3025 */
3026 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003027}
3028
NeilBrownfd39ca92005-06-23 22:04:03 -07003029static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030nfs4_laundromat(void)
3031{
3032 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003033 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct nfs4_delegation *dp;
3035 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003036 time_t cutoff = get_seconds() - nfsd4_lease;
3037 time_t t, clientid_val = nfsd4_lease;
3038 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 nfs4_lock_state();
3041
3042 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003043 if (locks_in_grace())
3044 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003045 INIT_LIST_HEAD(&reaplist);
3046 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 list_for_each_safe(pos, next, &client_lru) {
3048 clp = list_entry(pos, struct nfs4_client, cl_lru);
3049 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3050 t = clp->cl_time - cutoff;
3051 if (clientid_val > t)
3052 clientid_val = t;
3053 break;
3054 }
Benny Halevyd7682982010-05-12 00:13:54 +03003055 if (atomic_read(&clp->cl_refcount)) {
3056 dprintk("NFSD: client in use (clientid %08x)\n",
3057 clp->cl_clientid.cl_id);
3058 continue;
3059 }
3060 unhash_client_locked(clp);
3061 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003062 }
3063 spin_unlock(&client_lock);
3064 list_for_each_safe(pos, next, &reaplist) {
3065 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 dprintk("NFSD: purging unused client (clientid %08x)\n",
3067 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003068 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 expire_client(clp);
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 spin_lock(&recall_lock);
3072 list_for_each_safe(pos, next, &del_recall_lru) {
3073 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3074 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3075 u = dp->dl_time - cutoff;
3076 if (test_val > u)
3077 test_val = u;
3078 break;
3079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 list_move(&dp->dl_recall_lru, &reaplist);
3081 }
3082 spin_unlock(&recall_lock);
3083 list_for_each_safe(pos, next, &reaplist) {
3084 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3085 list_del_init(&dp->dl_recall_lru);
3086 unhash_delegation(dp);
3087 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003088 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003090 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3091 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3092 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (test_val > u)
3094 test_val = u;
3095 break;
3096 }
3097 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003098 oo->oo_owner.so_id);
3099 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3102 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3103 nfs4_unlock_state();
3104 return clientid_val;
3105}
3106
Harvey Harrisona254b242008-02-20 12:49:00 -08003107static struct workqueue_struct *laundry_wq;
3108static void laundromat_main(struct work_struct *);
3109static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3110
3111static void
David Howellsc4028952006-11-22 14:57:56 +00003112laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 time_t t;
3115
3116 t = nfs4_laundromat();
3117 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003118 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003121static struct nfs4_openowner * search_close_lru(u32 st_id)
NeilBrownf8816512005-07-07 17:59:25 -07003122{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003123 struct nfs4_openowner *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003125 list_for_each_entry(local, &close_lru, oo_close_lru) {
3126 if (local->oo_owner.so_id == st_id)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003127 return local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129 return NULL;
3130}
3131
3132static inline int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003133nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003135 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
3138static int
3139STALE_STATEID(stateid_t *stateid)
3140{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003141 if (stateid->si_boot == boot_time)
3142 return 0;
3143 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003144 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003145 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
3148static inline int
3149access_permit_read(unsigned long access_bmap)
3150{
3151 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3152 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3153 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3154}
3155
3156static inline int
3157access_permit_write(unsigned long access_bmap)
3158{
3159 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3160 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3161}
3162
3163static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003164__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Al Virob37ad282006-10-19 23:28:59 -07003166 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
J. Bruce Fields02921912010-07-29 15:16:59 -04003168 /* For lock stateid's, we test the parent open, not the lock: */
3169 if (stp->st_openstp)
3170 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3172 goto out;
3173 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3174 goto out;
3175 status = nfs_ok;
3176out:
3177 return status;
3178}
3179
Al Virob37ad282006-10-19 23:28:59 -07003180static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3182{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003183 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003185 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003186 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 * conflicting state; so we must wait out the grace period. */
3188 return nfserr_grace;
3189 } else if (flags & WR_STATE)
3190 return nfs4_share_conflict(current_fh,
3191 NFS4_SHARE_DENY_WRITE);
3192 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3193 return nfs4_share_conflict(current_fh,
3194 NFS4_SHARE_DENY_READ);
3195}
3196
3197/*
3198 * Allow READ/WRITE during grace period on recovered state only for files
3199 * that are not able to provide mandatory locking.
3200 */
3201static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003202grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003204 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
J. Bruce Fields81b82962011-08-23 11:03:29 -04003207/* Returns true iff a is later than b: */
3208static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3209{
3210 return (s32)a->si_generation - (s32)b->si_generation > 0;
3211}
3212
J. Bruce Fields28dde242011-08-22 10:07:12 -04003213static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003214{
Andy Adamson66689582009-04-03 08:28:45 +03003215 /*
3216 * When sessions are used the stateid generation number is ignored
3217 * when it is zero.
3218 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003219 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003220 return nfs_ok;
3221
3222 if (in->si_generation == ref->si_generation)
3223 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003224
J. Bruce Fields0836f582008-01-26 19:08:12 -05003225 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003226 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003227 return nfserr_bad_stateid;
3228 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003229 * However, we could see a stateid from the past, even from a
3230 * non-buggy client. For example, if the client sends a lock
3231 * while some IO is outstanding, the lock may bump si_generation
3232 * while the IO is still in flight. The client could avoid that
3233 * situation by waiting for responses on all the IO requests,
3234 * but better performance may result in retrying IO that
3235 * receives an old_stateid error if requests are rarely
3236 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003237 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003238 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003239}
3240
J. Bruce Fields3e633072009-02-21 13:17:19 -08003241static int is_delegation_stateid(stateid_t *stateid)
3242{
3243 return stateid->si_fileid == 0;
3244}
3245
J. Bruce Fields28dde242011-08-22 10:07:12 -04003246__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
Bryan Schumaker17456802011-07-13 10:50:48 -04003247{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003248 struct nfs4_ol_stateid *stp = NULL;
Bryan Schumaker17456802011-07-13 10:50:48 -04003249 __be32 status = nfserr_stale_stateid;
3250
3251 if (STALE_STATEID(stateid))
3252 goto out;
3253
3254 status = nfserr_expired;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003255 stp = find_stateid(stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04003256 if (!stp)
3257 goto out;
3258 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003259 if (stp->st_stateowner->so_is_open_owner
3260 && !openowner(stp->st_stateowner)->oo_confirmed)
Bryan Schumaker17456802011-07-13 10:50:48 -04003261 goto out;
3262
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003263 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, has_session);
Bryan Schumaker17456802011-07-13 10:50:48 -04003264 if (status)
3265 goto out;
3266
3267 status = nfs_ok;
3268out:
3269 return status;
3270}
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272/*
3273* Checks for stateid operations
3274*/
Al Virob37ad282006-10-19 23:28:59 -07003275__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003276nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3277 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003279 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003281 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003283 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (filpp)
3286 *filpp = NULL;
3287
J. Bruce Fields18f82732009-02-21 15:23:01 -08003288 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return nfserr_grace;
3290
3291 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3292 return check_special_stateids(current_fh, stateid, flags);
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 status = nfserr_stale_stateid;
3295 if (STALE_STATEID(stateid))
3296 goto out;
3297
J. Bruce Fields33515142010-10-02 18:42:39 -04003298 /*
3299 * We assume that any stateid that has the current boot time,
3300 * but that we can't find, is expired:
3301 */
3302 status = nfserr_expired;
J. Bruce Fields3e633072009-02-21 13:17:19 -08003303 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003304 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003305 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003307 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003308 if (status)
3309 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003310 status = nfs4_check_delegmode(dp, flags);
3311 if (status)
3312 goto out;
3313 renew_client(dp->dl_client);
Dan Carpenter43b01782010-10-27 23:19:04 +02003314 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003315 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003316 BUG_ON(!*filpp);
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 } else { /* open or lock stateid */
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003319 stp = find_stateid(stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003320 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003322 status = nfserr_bad_stateid;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08003323 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003325 if (stp->st_stateowner->so_is_open_owner
3326 && !openowner(stp->st_stateowner)->oo_confirmed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003328 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid,
J. Bruce Fields28dde242011-08-22 10:07:12 -04003329 nfsd4_has_session(cstate));
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08003330 if (status)
3331 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003332 status = nfs4_check_openmode(stp, flags);
3333 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 goto out;
3335 renew_client(stp->st_stateowner->so_client);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003336 if (filpp) {
3337 if (flags & RD_STATE)
3338 *filpp = find_readable_file(stp->st_file);
3339 else
3340 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 status = nfs_ok;
3344out:
3345 return status;
3346}
3347
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003348static __be32
3349nfsd4_free_delegation_stateid(stateid_t *stateid)
3350{
3351 struct nfs4_delegation *dp = search_for_delegation(stateid);
3352 if (dp)
3353 return nfserr_locks_held;
3354 return nfserr_bad_stateid;
3355}
3356
3357static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003358nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003359{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003360 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003361 return nfserr_locks_held;
3362 release_lock_stateid(stp);
3363 return nfs_ok;
3364}
3365
3366/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003367 * Test if the stateid is valid
3368 */
3369__be32
3370nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3371 struct nfsd4_test_stateid *test_stateid)
3372{
3373 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3374 return nfs_ok;
3375}
3376
3377/*
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003378 * Free a state id
3379 */
3380__be32
3381nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3382 struct nfsd4_free_stateid *free_stateid)
3383{
3384 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003385 struct nfs4_ol_stateid *stp;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003386 __be32 ret;
3387
3388 nfs4_lock_state();
3389 if (is_delegation_stateid(stateid)) {
3390 ret = nfsd4_free_delegation_stateid(stateid);
3391 goto out;
3392 }
3393
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04003394 stp = find_stateid(stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003395 if (!stp) {
3396 ret = nfserr_bad_stateid;
3397 goto out;
3398 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003399 ret = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, 1);
J. Bruce Fields81b82962011-08-23 11:03:29 -04003400 if (ret)
3401 goto out;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003402
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003403 if (stp->st_stid.sc_type == NFS4_OPEN_STID) {
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003404 ret = nfserr_locks_held;
3405 goto out;
3406 } else {
3407 ret = nfsd4_free_lock_stateid(stp);
3408 goto out;
3409 }
3410
3411out:
3412 nfs4_unlock_state();
3413 return ret;
3414}
3415
NeilBrown4c4cd222005-07-07 17:59:27 -07003416static inline int
3417setlkflg (int type)
3418{
3419 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3420 RD_STATE : WR_STATE;
3421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003423static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3424{
3425 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3426 return nfserr_bad_stateid;
3427 if (STALE_STATEID(stateid))
3428 return nfserr_stale_stateid;
3429 return nfs_ok;
3430}
3431
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003432static __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 -04003433{
3434 struct svc_fh *current_fh = &cstate->current_fh;
3435 struct nfs4_stateowner *sop = stp->st_stateowner;
3436 __be32 status;
3437
3438 if (nfs4_check_fh(current_fh, stp))
3439 return nfserr_bad_stateid;
3440 status = nfsd4_check_seqid(cstate, sop, seqid);
3441 if (status)
3442 return status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003443 return check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446/*
3447 * Checks for sequence id mutating operations.
3448 */
Al Virob37ad282006-10-19 23:28:59 -07003449static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003450nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003451 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003452 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003454 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003456 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3457 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 *stpp = NULL;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003460 status = nfs4_nospecial_stateid_checks(stateid);
3461 if (status)
3462 return status;
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003463 *stpp = find_stateid_by_type(stateid, typemask);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003464 if (*stpp == NULL)
3465 return nfserr_expired;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003466 cstate->replay_owner = (*stpp)->st_stateowner;
3467 renew_client((*stpp)->st_stateowner->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003469 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3470}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003471
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003472static __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 -04003473{
3474 __be32 status;
3475 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003477 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003478 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003479 if (status)
3480 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003481 oo = openowner((*stpp)->st_stateowner);
3482 if (!oo->oo_confirmed)
NeilBrown3a4f98b2005-07-07 17:59:26 -07003483 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003484 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Al Virob37ad282006-10-19 23:28:59 -07003487__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003488nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003489 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Al Virob37ad282006-10-19 23:28:59 -07003491 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003492 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003493 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003496 (int)cstate->current_fh.fh_dentry->d_name.len,
3497 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003499 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003500 if (status)
3501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 nfs4_lock_state();
3504
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003505 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003506 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003507 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003508 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003509 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003510 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003511 status = nfserr_bad_stateid;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003512 if (oo->oo_confirmed)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003513 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003514 oo->oo_confirmed = 1;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003515 update_stateid(&stp->st_stid.sc_stateid);
3516 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003517 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003518 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003519
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003520 nfsd4_create_clid_dir(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003521 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003523 if (!cstate->replay_owner)
3524 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 return status;
3526}
3527
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003528static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
3530 int i;
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 for (i = 1; i < 4; i++) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003533 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3534 nfs4_file_put_access(stp->st_file, i);
3535 __clear_bit(i, &stp->st_access_bmap);
3536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
3538}
3539
3540static void
3541reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3542{
3543 int i;
3544 for (i = 0; i < 4; i++) {
3545 if ((i & deny) != i)
3546 __clear_bit(i, bmap);
3547 }
3548}
3549
Al Virob37ad282006-10-19 23:28:59 -07003550__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003551nfsd4_open_downgrade(struct svc_rqst *rqstp,
3552 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003553 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Al Virob37ad282006-10-19 23:28:59 -07003555 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003556 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003559 (int)cstate->current_fh.fh_dentry->d_name.len,
3560 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003562 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003563 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return nfserr_inval;
3565
3566 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003567 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3568 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003569 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 status = nfserr_inval;
3572 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3573 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3574 stp->st_access_bmap, od->od_share_access);
3575 goto out;
3576 }
3577 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3578 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3579 stp->st_deny_bmap, od->od_share_deny);
3580 goto out;
3581 }
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003582 nfs4_file_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3585
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003586 update_stateid(&stp->st_stid.sc_stateid);
3587 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 status = nfs_ok;
3589out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003590 if (!cstate->replay_owner)
3591 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return status;
3593}
3594
3595/*
3596 * nfs4_unlock_state() called after encode
3597 */
Al Virob37ad282006-10-19 23:28:59 -07003598__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003599nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003600 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Al Virob37ad282006-10-19 23:28:59 -07003602 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003603 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003604 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003607 (int)cstate->current_fh.fh_dentry->d_name.len,
3608 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 nfs4_lock_state();
3611 /* check close_lru for replay */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003612 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3613 &close->cl_stateid, &stp);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003614 if (stp == NULL && status == nfserr_expired) {
3615 /*
3616 * Also, we should make sure this isn't just the result of
3617 * a replayed close:
3618 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003619 oo = search_close_lru(close->cl_stateid.si_stateownerid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003620 /* It's not stale; let's assume it's expired: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003621 if (oo == NULL)
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003622 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003623 cstate->replay_owner = &oo->oo_owner;
3624 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
J. Bruce Fieldsf4dee242011-09-02 16:36:49 -04003625 if (status)
3626 goto out;
3627 status = nfserr_bad_seqid;
3628 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003629 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003631 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003633 update_stateid(&stp->st_stid.sc_stateid);
3634 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003636 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05003637 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003638
3639 /* place unused nfs4_stateowners on so_close_lru list to be
3640 * released by the laundromat service after the lease period
3641 * to enable us to handle CLOSE replay
3642 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003643 if (list_empty(&oo->oo_owner.so_stateids))
3644 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003646 if (!cstate->replay_owner)
3647 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return status;
3649}
3650
Al Virob37ad282006-10-19 23:28:59 -07003651__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003652nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3653 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003655 struct nfs4_delegation *dp;
3656 stateid_t *stateid = &dr->dr_stateid;
3657 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003658 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003660 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003661 return status;
3662 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003665 status = nfserr_bad_stateid;
3666 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3667 goto out;
3668 status = nfserr_stale_stateid;
3669 if (STALE_STATEID(stateid))
3670 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08003671 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003672 if (!is_delegation_stateid(stateid))
3673 goto out;
J. Bruce Fields33515142010-10-02 18:42:39 -04003674 status = nfserr_expired;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003675 dp = find_delegation_stateid(inode, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003676 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003677 goto out;
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003678 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003679 if (status)
3680 goto out;
3681 renew_client(dp->dl_client);
3682
3683 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003685 nfs4_unlock_state();
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return status;
3688}
3689
3690
3691/*
3692 * Lock owner state (byte-range locks)
3693 */
3694#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3695#define LOCK_HASH_BITS 8
3696#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3697#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3698
Benny Halevy87df4de2008-12-15 19:42:03 +02003699static inline u64
3700end_offset(u64 start, u64 len)
3701{
3702 u64 end;
3703
3704 end = start + len;
3705 return end >= start ? end: NFS4_MAX_UINT64;
3706}
3707
3708/* last octet in a range */
3709static inline u64
3710last_byte_offset(u64 start, u64 len)
3711{
3712 u64 end;
3713
3714 BUG_ON(!len);
3715 end = start + len;
3716 return end > start ? end - 1: NFS4_MAX_UINT64;
3717}
3718
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04003719static unsigned int lockownerid_hashval(u32 id)
3720{
3721 return id & LOCK_HASH_MASK;
3722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724static inline unsigned int
3725lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3726 struct xdr_netobj *ownername)
3727{
3728 return (file_hashval(inode) + cl_id
3729 + opaque_hashval(ownername->data, ownername->len))
3730 & LOCK_HASH_MASK;
3731}
3732
3733static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3734static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003736static struct nfs4_delegation *
3737search_for_delegation(stateid_t *stid)
3738{
3739 struct nfs4_file *fp;
3740 struct nfs4_delegation *dp;
3741 struct list_head *pos;
3742 int i;
3743
3744 for (i = 0; i < FILE_HASH_SIZE; i++) {
3745 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3746 list_for_each(pos, &fp->fi_delegations) {
3747 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003748 if (same_stateid(&dp->dl_stid.sc_stateid, stid))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003749 return dp;
3750 }
3751 }
3752 }
3753 return NULL;
3754}
3755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756static struct nfs4_delegation *
3757find_delegation_stateid(struct inode *ino, stateid_t *stid)
3758{
NeilBrown13cd2182005-06-23 22:03:10 -07003759 struct nfs4_file *fp;
3760 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003762 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3763 STATEID_VAL(stid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003766 if (!fp)
3767 return NULL;
3768 dl = find_delegation_file(fp, stid);
3769 put_nfs4_file(fp);
3770 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
3773/*
3774 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3775 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3776 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3777 * locking, this prevents us from being completely protocol-compliant. The
3778 * real solution to this problem is to start using unsigned file offsets in
3779 * the VFS, but this is a very deep change!
3780 */
3781static inline void
3782nfs4_transform_lock_offset(struct file_lock *lock)
3783{
3784 if (lock->fl_start < 0)
3785 lock->fl_start = OFFSET_MAX;
3786 if (lock->fl_end < 0)
3787 lock->fl_end = OFFSET_MAX;
3788}
3789
NeilBrownd5b90262006-04-10 22:55:22 -07003790/* Hack!: For now, we're defining this just so we can use a pointer to it
3791 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003792static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003793};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795static inline void
3796nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3797{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003798 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
NeilBrownd5b90262006-04-10 22:55:22 -07003800 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003801 lo = (struct nfs4_lockowner *) fl->fl_owner;
3802 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3803 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003804 if (!deny->ld_owner.data)
3805 /* We just don't care that much */
3806 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003807 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3808 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003809 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003810nevermind:
3811 deny->ld_owner.len = 0;
3812 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003813 deny->ld_clientid.cl_boot = 0;
3814 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
3816 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003817 deny->ld_length = NFS4_MAX_UINT64;
3818 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3820 deny->ld_type = NFS4_READ_LT;
3821 if (fl->fl_type != F_RDLCK)
3822 deny->ld_type = NFS4_WRITE_LT;
3823}
3824
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003825static struct nfs4_lockowner *
3826find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 struct xdr_netobj *owner)
3828{
3829 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3830 struct nfs4_stateowner *op;
3831
3832 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003833 if (same_owner_str(op, owner, clid))
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003834 return lockowner(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
3836 return NULL;
3837}
3838
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003839static 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 -04003840{
3841 unsigned int idhashval;
3842
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003843 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3844 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3845 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3846 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003847}
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849/*
3850 * Alloc a lock owner structure.
3851 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003852 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 *
3854 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 */
3856
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003857static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003858alloc_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 -04003859 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003861 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3862 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003864 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3865 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003866 /* It is the openowner seqid that will be incremented in encode in the
3867 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003868 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3869 hash_lockowner(lo, strhashval, clp, open_stp);
3870 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
3872
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003873static struct nfs4_ol_stateid *
3874alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003876 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
NeilBrown5ac049a2005-06-23 22:03:03 -07003878 stp = nfs4_alloc_stateid();
3879 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 goto out;
NeilBrown8beefa22005-06-23 22:03:08 -07003881 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003882 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3883 stp->st_stateowner = &lo->lo_owner;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003884 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown13cd2182005-06-23 22:03:10 -07003885 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 stp->st_file = fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003887 stp->st_stid.sc_stateid.si_boot = boot_time;
3888 stp->st_stid.sc_stateid.si_stateownerid = lo->lo_owner.so_id;
3889 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
J. Bruce Fields73997dc2011-08-31 15:47:21 -04003890 /* note will be incremented before first return to client: */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003891 stp->st_stid.sc_stateid.si_generation = 0;
J. Bruce Fields36d44c62011-09-08 12:16:03 -04003892 hash_stid(&stp->st_stid);
J. Bruce Fields0997b172011-03-02 18:01:35 -05003893 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003895 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897out:
3898 return stp;
3899}
3900
NeilBrownfd39ca92005-06-23 22:04:03 -07003901static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902check_lock_length(u64 offset, u64 length)
3903{
Benny Halevy87df4de2008-12-15 19:42:03 +02003904 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 LOFF_OVERFLOW(offset, length)));
3906}
3907
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003908static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05003909{
3910 struct nfs4_file *fp = lock_stp->st_file;
3911 int oflag = nfs4_access_to_omode(access);
3912
3913 if (test_bit(access, &lock_stp->st_access_bmap))
3914 return;
3915 nfs4_file_get_access(fp, oflag);
3916 __set_bit(access, &lock_stp->st_access_bmap);
3917}
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919/*
3920 * LOCK operation
3921 */
Al Virob37ad282006-10-19 23:28:59 -07003922__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003923nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003924 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926 struct nfs4_openowner *open_sop = NULL;
3927 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003928 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04003929 struct nfs4_file *fp;
3930 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003932 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003933 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 unsigned int strhashval;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003935 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07003936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3939 (long long) lock->lk_offset,
3940 (long long) lock->lk_length);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (check_lock_length(lock->lk_offset, lock->lk_length))
3943 return nfserr_inval;
3944
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003945 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003946 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003947 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3948 return status;
3949 }
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 nfs4_lock_state();
3952
3953 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003954 /*
3955 * Client indicates that this is a new lockowner.
3956 * Use open owner and open stateid to create lock owner and
3957 * lock stateid.
3958 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003959 struct nfs4_ol_stateid *open_stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003962 if (!nfsd4_has_session(cstate) &&
3963 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003967 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 lock->lk_new_open_seqid,
3969 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003970 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07003971 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003973 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003974 status = nfserr_bad_stateid;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003975 if (!nfsd4_has_session(cstate) &&
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003976 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04003977 &lock->v.new.clientid))
3978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /* create lockowner and lock stateid */
3980 fp = open_stp->st_file;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003981 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3982 open_sop->oo_owner.so_client->cl_clientid.cl_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003984 /* XXX: Do we need to check for duplicate stateowners on
3985 * the same file, or should they just be allowed (and
3986 * create new stateids)? */
J. Bruce Fields3e772462011-08-10 19:07:33 -04003987 status = nfserr_jukebox;
Neil Brownb59e3c02005-09-13 01:25:38 -07003988 lock_sop = alloc_init_lock_stateowner(strhashval,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003989 open_sop->oo_owner.so_client, open_stp, lock);
Neil Brownb59e3c02005-09-13 01:25:38 -07003990 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003992 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003993 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 } else {
3996 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003997 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003998 lock->lk_old_lock_seqid,
3999 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004000 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (status)
4002 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004003 lock_sop = lockowner(lock_stp->st_stateowner);
J. Bruce Fields7d947842010-08-20 18:09:31 -04004004 fp = lock_stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 }
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004006 /* lock_sop and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004008 lkflg = setlkflg(lock->lk_type);
4009 status = nfs4_check_openmode(lock_stp, lkflg);
4010 if (status)
4011 goto out;
4012
NeilBrown0dd395d2005-07-07 17:59:15 -07004013 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004014 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004015 goto out;
4016 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004017 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004018 goto out;
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 locks_init_lock(&file_lock);
4021 switch (lock->lk_type) {
4022 case NFS4_READ_LT:
4023 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004024 filp = find_readable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 case NFS4_WRITE_LT:
4030 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004031 filp = find_writeable_file(lock_stp->st_file);
4032 if (filp)
4033 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 default:
4037 status = nfserr_inval;
4038 goto out;
4039 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004040 if (!filp) {
4041 status = nfserr_openmode;
4042 goto out;
4043 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004044 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 file_lock.fl_pid = current->tgid;
4046 file_lock.fl_file = filp;
4047 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004048 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
4050 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004051 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 nfs4_transform_lock_offset(&file_lock);
4053
4054 /*
4055 * Try to lock the file in the VFS.
4056 * Note: locks.c uses the BKL to protect the inode's lock list.
4057 */
4058
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004059 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004060 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004062 update_stateid(&lock_stp->st_stid.sc_stateid);
4063 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004065 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004066 break;
4067 case (EAGAIN): /* conflock holds conflicting lock */
4068 status = nfserr_denied;
4069 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4070 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 case (EDEADLK):
4073 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004074 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004075 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004076 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004077 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08004081 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004082 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004083 if (!cstate->replay_owner)
4084 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return status;
4086}
4087
4088/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004089 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4090 * so we do a temporary open here just to get an open file to pass to
4091 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4092 * inode operation.)
4093 */
4094static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4095{
4096 struct file *file;
4097 int err;
4098
4099 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4100 if (err)
4101 return err;
4102 err = vfs_test_lock(file, lock);
4103 nfsd_close(file);
4104 return err;
4105}
4106
4107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 * LOCKT operation
4109 */
Al Virob37ad282006-10-19 23:28:59 -07004110__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004111nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4112 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
4114 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004116 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004117 int error;
Al Virob37ad282006-10-19 23:28:59 -07004118 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004120 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return nfserr_grace;
4122
4123 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4124 return nfserr_inval;
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 nfs4_lock_state();
4127
4128 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004129 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004132 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004135 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 locks_init_lock(&file_lock);
4137 switch (lockt->lt_type) {
4138 case NFS4_READ_LT:
4139 case NFS4_READW_LT:
4140 file_lock.fl_type = F_RDLCK;
4141 break;
4142 case NFS4_WRITE_LT:
4143 case NFS4_WRITEW_LT:
4144 file_lock.fl_type = F_WRLCK;
4145 break;
4146 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004147 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 status = nfserr_inval;
4149 goto out;
4150 }
4151
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004152 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4153 if (lo)
4154 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 file_lock.fl_pid = current->tgid;
4156 file_lock.fl_flags = FL_POSIX;
4157
4158 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004159 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 nfs4_transform_lock_offset(&file_lock);
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004164 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004165 if (error) {
4166 status = nfserrno(error);
4167 goto out;
4168 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004169 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004171 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173out:
4174 nfs4_unlock_state();
4175 return status;
4176}
4177
Al Virob37ad282006-10-19 23:28:59 -07004178__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004179nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004180 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004182 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 struct file *filp = NULL;
4184 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004185 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004186 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4189 (long long) locku->lu_offset,
4190 (long long) locku->lu_length);
4191
4192 if (check_lock_length(locku->lu_offset, locku->lu_length))
4193 return nfserr_inval;
4194
4195 nfs4_lock_state();
4196
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004197 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004198 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004199 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004201 filp = find_any_file(stp->st_file);
4202 if (!filp) {
4203 status = nfserr_lock_range;
4204 goto out;
4205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 BUG_ON(!filp);
4207 locks_init_lock(&file_lock);
4208 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004209 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 file_lock.fl_pid = current->tgid;
4211 file_lock.fl_file = filp;
4212 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004213 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 file_lock.fl_start = locku->lu_offset;
4215
Benny Halevy87df4de2008-12-15 19:42:03 +02004216 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 nfs4_transform_lock_offset(&file_lock);
4218
4219 /*
4220 * Try to unlock the file in the VFS.
4221 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004222 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004223 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004224 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 goto out_nfserr;
4226 }
4227 /*
4228 * OK, unlock succeeded; the only thing left to do is update the stateid.
4229 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004230 update_stateid(&stp->st_stid.sc_stateid);
4231 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 nfs4_unlock_state();
4235 return status;
4236
4237out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004238 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 goto out;
4240}
4241
4242/*
4243 * returns
4244 * 1: locks held by lockowner
4245 * 0: no locks held by lockowner
4246 */
4247static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004248check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004251 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 int status = 0;
4253
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004254 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004256 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 status = 1;
4258 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004262 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return status;
4264}
4265
Al Virob37ad282006-10-19 23:28:59 -07004266__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004267nfsd4_release_lockowner(struct svc_rqst *rqstp,
4268 struct nfsd4_compound_state *cstate,
4269 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004272 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004273 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004274 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004276 struct list_head matches;
4277 int i;
Al Virob37ad282006-10-19 23:28:59 -07004278 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4281 clid->cl_boot, clid->cl_id);
4282
4283 /* XXX check for lease expiration */
4284
4285 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004286 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 nfs4_lock_state();
4290
NeilBrown3e9e3db2005-06-23 22:04:20 -07004291 status = nfserr_locks_held;
4292 /* XXX: we're doing a linear search through all the lockowners.
4293 * Yipes! For now we'll just hope clients aren't really using
4294 * release_lockowner much, but eventually we have to fix these
4295 * data structures. */
4296 INIT_LIST_HEAD(&matches);
4297 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4298 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04004299 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004300 continue;
4301 list_for_each_entry(stp, &sop->so_stateids,
4302 st_perstateowner) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004303 lo = lockowner(sop);
4304 if (check_for_locks(stp->st_file, lo))
NeilBrown3e9e3db2005-06-23 22:04:20 -07004305 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004306 list_add(&lo->lo_list, &matches);
NeilBrown3e9e3db2005-06-23 22:04:20 -07004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004309 }
4310 /* Clients probably won't expect us to return with some (but not all)
4311 * of the lockowner state released; so don't release any until all
4312 * have been checked. */
4313 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004314 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004315 lo = list_entry(matches.next, struct nfs4_lockowner,
4316 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004317 /* unhash_stateowner deletes so_perclient only
4318 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004319 list_del(&lo->lo_list);
4320 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322out:
4323 nfs4_unlock_state();
4324 return status;
4325}
4326
4327static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004328alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
NeilBrowna55370a2005-06-23 22:03:52 -07004330 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
NeilBrownc7b9a452005-06-23 22:04:30 -07004333int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004334nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004335{
4336 unsigned int strhashval = clientstr_hashval(name);
4337 struct nfs4_client *clp;
4338
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004339 clp = find_confirmed_client_by_str(name, strhashval);
NeilBrownc7b9a452005-06-23 22:04:30 -07004340 return clp ? 1 : 0;
4341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/*
4344 * failure => all reset bets are off, nfserr_no_grace...
4345 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004346int
4347nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
4349 unsigned int strhashval;
4350 struct nfs4_client_reclaim *crp = NULL;
4351
NeilBrowna55370a2005-06-23 22:03:52 -07004352 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4353 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 if (!crp)
4355 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004356 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 INIT_LIST_HEAD(&crp->cr_strhash);
4358 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004359 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 reclaim_str_hashtbl_size++;
4361 return 1;
4362}
4363
4364static void
4365nfs4_release_reclaim(void)
4366{
4367 struct nfs4_client_reclaim *crp = NULL;
4368 int i;
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4371 while (!list_empty(&reclaim_str_hashtbl[i])) {
4372 crp = list_entry(reclaim_str_hashtbl[i].next,
4373 struct nfs4_client_reclaim, cr_strhash);
4374 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 kfree(crp);
4376 reclaim_str_hashtbl_size--;
4377 }
4378 }
4379 BUG_ON(reclaim_str_hashtbl_size);
4380}
4381
4382/*
4383 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07004384static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385nfs4_find_reclaim_client(clientid_t *clid)
4386{
4387 unsigned int strhashval;
4388 struct nfs4_client *clp;
4389 struct nfs4_client_reclaim *crp = NULL;
4390
4391
4392 /* find clientid in conf_id_hashtbl */
4393 clp = find_confirmed_client(clid);
4394 if (clp == NULL)
4395 return NULL;
4396
NeilBrowna55370a2005-06-23 22:03:52 -07004397 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4398 clp->cl_name.len, clp->cl_name.data,
4399 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004402 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004404 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return crp;
4406 }
4407 }
4408 return NULL;
4409}
4410
4411/*
4412* Called from OPEN. Look for clientid in reclaim list.
4413*/
Al Virob37ad282006-10-19 23:28:59 -07004414__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415nfs4_check_open_reclaim(clientid_t *clid)
4416{
NeilBrowndfc83562005-06-23 22:03:16 -07004417 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
NeilBrownac4d8ff22005-06-23 22:03:30 -07004420/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004422int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004423nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004425 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004427 status = nfsd4_init_slabs();
4428 if (status)
4429 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4431 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4432 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4433 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4434 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004435 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004437 for (i = 0; i < SESSION_HASH_SIZE; i++)
4438 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 for (i = 0; i < FILE_HASH_SIZE; i++) {
4440 INIT_LIST_HEAD(&file_hashtbl[i]);
4441 }
J. Bruce Fields506f2752011-08-11 18:50:18 -04004442 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4443 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4444 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
J. Bruce Fieldsb79abad2011-08-22 18:01:43 -04004446 for (i = 0; i < STATEID_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 INIT_LIST_HEAD(&stateid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4449 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4450 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 INIT_LIST_HEAD(&close_lru);
4454 INIT_LIST_HEAD(&client_lru);
4455 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004456 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004457 return 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004458}
4459
NeilBrown190e4fb2005-06-23 22:04:25 -07004460static void
4461nfsd4_load_reboot_recovery_data(void)
4462{
4463 int status;
4464
NeilBrown0964a3d2005-06-23 22:04:32 -07004465 nfs4_lock_state();
J. Bruce Fields48483bf2011-08-26 20:40:28 -04004466 nfsd4_init_recdir();
NeilBrown190e4fb2005-06-23 22:04:25 -07004467 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004468 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004469 if (status)
4470 printk("NFSD: Failure reading reboot recovery data\n");
4471}
4472
Meelap Shahc2f1a552007-07-17 04:04:39 -07004473/*
4474 * Since the lifetime of a delegation isn't limited to that of an open, a
4475 * client may quite reasonably hang on to a delegation as long as it has
4476 * the inode cached. This becomes an obvious problem the first time a
4477 * client's inode cache approaches the size of the server's total memory.
4478 *
4479 * For now we avoid this problem by imposing a hard limit on the number
4480 * of delegations, which varies according to the server's memory size.
4481 */
4482static void
4483set_max_delegations(void)
4484{
4485 /*
4486 * Allow at most 4 delegations per megabyte of RAM. Quick
4487 * estimates suggest that in the worst case (where every delegation
4488 * is for a different inode), a delegation could take about 1.5K,
4489 * giving a worst case usage of about 6% of memory.
4490 */
4491 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4492}
4493
NeilBrownac4d8ff22005-06-23 22:03:30 -07004494/* initialization to perform when the nfsd service is started: */
4495
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004496static int
NeilBrownac4d8ff22005-06-23 22:03:30 -07004497__nfs4_state_start(void)
4498{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004499 int ret;
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004502 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004503 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004504 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004505 ret = set_callback_cred();
4506 if (ret)
4507 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004508 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004509 if (laundry_wq == NULL)
4510 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004511 ret = nfsd4_create_callback_queue();
4512 if (ret)
4513 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004514 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004515 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004516 return 0;
4517out_free_laundry:
4518 destroy_workqueue(laundry_wq);
4519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004522int
NeilBrown76a35502005-06-23 22:03:26 -07004523nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
NeilBrown190e4fb2005-06-23 22:04:25 -07004525 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004526 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529static void
4530__nfs4_state_shutdown(void)
4531{
4532 int i;
4533 struct nfs4_client *clp = NULL;
4534 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 struct list_head *pos, *next, reaplist;
4536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4538 while (!list_empty(&conf_id_hashtbl[i])) {
4539 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4540 expire_client(clp);
4541 }
4542 while (!list_empty(&unconf_str_hashtbl[i])) {
4543 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4544 expire_client(clp);
4545 }
4546 }
4547 INIT_LIST_HEAD(&reaplist);
4548 spin_lock(&recall_lock);
4549 list_for_each_safe(pos, next, &del_recall_lru) {
4550 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4551 list_move(&dp->dl_recall_lru, &reaplist);
4552 }
4553 spin_unlock(&recall_lock);
4554 list_for_each_safe(pos, next, &reaplist) {
4555 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4556 list_del_init(&dp->dl_recall_lru);
4557 unhash_delegation(dp);
4558 }
4559
NeilBrown190e4fb2005-06-23 22:04:25 -07004560 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
4563void
4564nfs4_state_shutdown(void)
4565{
Tejun Heoafe2c512010-12-14 16:21:17 +01004566 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004567 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004568 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 nfs4_lock_state();
4570 nfs4_release_reclaim();
4571 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004573 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}