blob: 9cc3b786d56c2a0eff5a0848f2aea1eb340a4bbb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/smp_lock.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>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050040#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040041#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020042#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050043#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define NFSDDBG_FACILITY NFSDDBG_PROC
46
47/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050048time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050049time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070050static time_t boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static u32 current_ownerid = 1;
52static u32 current_fileid = 1;
53static u32 current_delegid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070054static stateid_t zerostateid; /* bits all 0 */
55static stateid_t onestateid; /* bits all 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +030056static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070057
58#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
59#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* forward declarations */
NeilBrownfd39ca92005-06-23 22:04:03 -070062static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
NeilBrown0964a3d2005-06-23 22:04:32 -070064static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
65static void nfs4_set_recdir(char *recdir);
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
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *stateowner_slab = NULL;
80static struct kmem_cache *file_slab = NULL;
81static struct kmem_cache *stateid_slab = NULL;
82static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084void
85nfs4_lock_state(void)
86{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080087 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void
91nfs4_unlock_state(void)
92{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080093 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static inline u32
97opaque_hashval(const void *ptr, int nbytes)
98{
99 unsigned char *cptr = (unsigned char *) ptr;
100
101 u32 x = 0;
102 while (nbytes--) {
103 x *= 37;
104 x += *cptr++;
105 }
106 return x;
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct list_head del_recall_lru;
110
NeilBrown13cd2182005-06-23 22:03:10 -0700111static inline void
112put_nfs4_file(struct nfs4_file *fi)
113{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800114 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
115 list_del(&fi->fi_hash);
116 spin_unlock(&recall_lock);
117 iput(fi->fi_inode);
118 kmem_cache_free(file_slab, fi);
119 }
NeilBrown13cd2182005-06-23 22:03:10 -0700120}
121
122static inline void
123get_nfs4_file(struct nfs4_file *fi)
124{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800125 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700126}
127
NeilBrownef0f3392006-04-10 22:55:41 -0700128static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700129unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700130
131/*
132 * Open owner state (share locks)
133 */
134
135/* hash tables for nfs4_stateowner */
136#define OWNER_HASH_BITS 8
137#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
138#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
139
140#define ownerid_hashval(id) \
141 ((id) & OWNER_HASH_MASK)
142#define ownerstr_hashval(clientid, ownername) \
143 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
144
145static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
146static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
147
148/* hash table for nfs4_file */
149#define FILE_HASH_BITS 8
150#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
151#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
152/* hash table for (open)nfs4_stateid */
153#define STATEID_HASH_BITS 10
154#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
156
157#define file_hashval(x) \
158 hash_ptr(x, FILE_HASH_BITS)
159#define stateid_hashval(owner_id, file_id) \
160 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
161
162static struct list_head file_hashtbl[FILE_HASH_SIZE];
163static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static struct nfs4_delegation *
166alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
167{
168 struct nfs4_delegation *dp;
169 struct nfs4_file *fp = stp->st_file;
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400170 struct nfs4_cb_conn *cb = &stp->st_stateowner->so_client->cl_cb_conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 dprintk("NFSD alloc_init_deleg\n");
Meelap Shah47f99402007-07-17 04:04:40 -0700173 if (fp->fi_had_conflict)
174 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700175 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700176 return NULL;
NeilBrown5b2d21c2005-06-23 22:03:04 -0700177 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
178 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return dp;
NeilBrownef0f3392006-04-10 22:55:41 -0700180 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700181 INIT_LIST_HEAD(&dp->dl_perfile);
182 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 INIT_LIST_HEAD(&dp->dl_recall_lru);
184 dp->dl_client = clp;
NeilBrown13cd2182005-06-23 22:03:10 -0700185 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dp->dl_file = fp;
187 dp->dl_flock = NULL;
188 get_file(stp->st_vfs_file);
189 dp->dl_vfs_file = stp->st_vfs_file;
190 dp->dl_type = type;
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400191 dp->dl_ident = cb->cb_ident;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -0400192 dp->dl_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dp->dl_stateid.si_stateownerid = current_delegid++;
194 dp->dl_stateid.si_fileid = 0;
195 dp->dl_stateid.si_generation = 0;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500196 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 dp->dl_time = 0;
198 atomic_set(&dp->dl_count, 1);
NeilBrownea1da632005-06-23 22:04:17 -0700199 list_add(&dp->dl_perfile, &fp->fi_delegations);
200 list_add(&dp->dl_perclnt, &clp->cl_delegations);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500201 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return dp;
203}
204
205void
206nfs4_put_delegation(struct nfs4_delegation *dp)
207{
208 if (atomic_dec_and_test(&dp->dl_count)) {
209 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700210 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700211 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700212 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214}
215
216/* Remove the associated file_lock first, then remove the delegation.
217 * lease_modify() is called to remove the FS_LEASE file_lock from
218 * the i_flock list, eventually calling nfsd's lock_manager
219 * fl_release_callback.
220 */
221static void
222nfs4_close_delegation(struct nfs4_delegation *dp)
223{
224 struct file *filp = dp->dl_vfs_file;
225
226 dprintk("NFSD: close_delegation dp %p\n",dp);
227 dp->dl_vfs_file = NULL;
228 /* The following nfsd_close may not actually close the file,
229 * but we want to remove the lease in any case. */
NeilBrownc9071322005-04-16 15:26:38 -0700230 if (dp->dl_flock)
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -0400231 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/* Called under the state lock. */
236static void
237unhash_delegation(struct nfs4_delegation *dp)
238{
NeilBrownea1da632005-06-23 22:04:17 -0700239 list_del_init(&dp->dl_perfile);
240 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 spin_lock(&recall_lock);
242 list_del_init(&dp->dl_recall_lru);
243 spin_unlock(&recall_lock);
244 nfs4_close_delegation(dp);
245 nfs4_put_delegation(dp);
246}
247
248/*
249 * SETCLIENTID state
250 */
251
Benny Halevy36acb662010-05-12 00:13:04 +0300252/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300253static DEFINE_SPINLOCK(client_lock);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* Hash tables for nfs4_clientid state */
256#define CLIENT_HASH_BITS 4
257#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
258#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
259
260#define clientid_hashval(id) \
261 ((id) & CLIENT_HASH_MASK)
NeilBrowna55370a2005-06-23 22:03:52 -0700262#define clientstr_hashval(name) \
263 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
266 * used in reboot/reset lease grace period processing
267 *
268 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
269 * setclientid_confirmed info.
270 *
271 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
272 * setclientid info.
273 *
274 * client_lru holds client queue ordered by nfs4_client.cl_time
275 * for lease renewal.
276 *
277 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
278 * for last close replay.
279 */
280static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
281static int reclaim_str_hashtbl_size = 0;
282static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
283static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
284static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
285static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
286static struct list_head client_lru;
287static struct list_head close_lru;
288
J. Bruce Fields22839632009-01-11 14:27:17 -0500289static void unhash_generic_stateid(struct nfs4_stateid *stp)
290{
291 list_del(&stp->st_hash);
292 list_del(&stp->st_perfile);
293 list_del(&stp->st_perstateowner);
294}
295
296static void free_generic_stateid(struct nfs4_stateid *stp)
297{
298 put_nfs4_file(stp->st_file);
299 kmem_cache_free(stateid_slab, stp);
300}
301
302static void release_lock_stateid(struct nfs4_stateid *stp)
303{
304 unhash_generic_stateid(stp);
305 locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
306 free_generic_stateid(stp);
307}
308
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500309static void unhash_lockowner(struct nfs4_stateowner *sop)
310{
311 struct nfs4_stateid *stp;
312
313 list_del(&sop->so_idhash);
314 list_del(&sop->so_strhash);
315 list_del(&sop->so_perstateid);
316 while (!list_empty(&sop->so_stateids)) {
317 stp = list_first_entry(&sop->so_stateids,
318 struct nfs4_stateid, st_perstateowner);
319 release_lock_stateid(stp);
320 }
321}
322
323static void release_lockowner(struct nfs4_stateowner *sop)
324{
325 unhash_lockowner(sop);
326 nfs4_put_stateowner(sop);
327}
328
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500329static void
330release_stateid_lockowners(struct nfs4_stateid *open_stp)
331{
332 struct nfs4_stateowner *lock_sop;
333
334 while (!list_empty(&open_stp->st_lockowners)) {
335 lock_sop = list_entry(open_stp->st_lockowners.next,
336 struct nfs4_stateowner, so_perstateid);
337 /* list_del(&open_stp->st_lockowners); */
338 BUG_ON(lock_sop->so_is_open_owner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500339 release_lockowner(lock_sop);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500340 }
341}
342
J. Bruce Fields22839632009-01-11 14:27:17 -0500343static void release_open_stateid(struct nfs4_stateid *stp)
344{
345 unhash_generic_stateid(stp);
346 release_stateid_lockowners(stp);
347 nfsd_close(stp->st_vfs_file);
348 free_generic_stateid(stp);
349}
350
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500351static void unhash_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500352{
353 struct nfs4_stateid *stp;
354
355 list_del(&sop->so_idhash);
356 list_del(&sop->so_strhash);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500357 list_del(&sop->so_perclient);
358 list_del(&sop->so_perstateid); /* XXX: necessary? */
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500359 while (!list_empty(&sop->so_stateids)) {
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500360 stp = list_first_entry(&sop->so_stateids,
361 struct nfs4_stateid, st_perstateowner);
362 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500363 }
364}
365
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500366static void release_openowner(struct nfs4_stateowner *sop)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500367{
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500368 unhash_openowner(sop);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500369 list_del(&sop->so_close_lru);
370 nfs4_put_stateowner(sop);
371}
372
Marc Eshel5282fd72009-04-03 08:27:52 +0300373#define SESSION_HASH_SIZE 512
374static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
375
376static inline int
377hash_sessionid(struct nfs4_sessionid *sessionid)
378{
379 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
380
381 return sid->sequence % SESSION_HASH_SIZE;
382}
383
384static inline void
385dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
386{
387 u32 *ptr = (u32 *)(&sessionid->data[0]);
388 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
389}
390
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300391static void
392gen_sessionid(struct nfsd4_session *ses)
393{
394 struct nfs4_client *clp = ses->se_client;
395 struct nfsd4_sessionid *sid;
396
397 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
398 sid->clientid = clp->cl_clientid;
399 sid->sequence = current_sessionid++;
400 sid->reserved = 0;
401}
402
403/*
Andy Adamsona6496372009-08-28 08:45:01 -0400404 * The protocol defines ca_maxresponssize_cached to include the size of
405 * the rpc header, but all we need to cache is the data starting after
406 * the end of the initial SEQUENCE operation--the rest we regenerate
407 * each time. Therefore we can advertise a ca_maxresponssize_cached
408 * value that is the number of bytes in our cache plus a few additional
409 * bytes. In order to stay on the safe side, and not promise more than
410 * we can cache, those additional bytes must be the minimum possible: 24
411 * bytes of rpc header (xid through accept state, with AUTH_NULL
412 * verifier), 12 for the compound header (with zero-length tag), and 44
413 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300414 */
Andy Adamsona6496372009-08-28 08:45:01 -0400415#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
416
417/*
418 * Give the client the number of ca_maxresponsesize_cached slots it
419 * requests, of size bounded by NFSD_SLOT_CACHE_SIZE,
420 * NFSD_MAX_MEM_PER_SESSION, and nfsd_drc_max_mem. Do not allow more
421 * than NFSD_MAX_SLOTS_PER_SESSION.
422 *
423 * If we run out of reserved DRC memory we should (up to a point)
424 * re-negotiate active sessions and reduce their slot usage to make
425 * rooom for new connections. For now we just fail the create session.
426 */
427static int set_forechannel_drc_size(struct nfsd4_channel_attrs *fchan)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300428{
Andy Adamsona6496372009-08-28 08:45:01 -0400429 int mem, size = fchan->maxresp_cached;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300430
Andy Adamson5d77ddf2009-06-16 04:19:38 +0300431 if (fchan->maxreqs < 1)
432 return nfserr_inval;
Andy Adamson5d77ddf2009-06-16 04:19:38 +0300433
Andy Adamsona6496372009-08-28 08:45:01 -0400434 if (size < NFSD_MIN_HDR_SEQ_SZ)
435 size = NFSD_MIN_HDR_SEQ_SZ;
436 size -= NFSD_MIN_HDR_SEQ_SZ;
437 if (size > NFSD_SLOT_CACHE_SIZE)
438 size = NFSD_SLOT_CACHE_SIZE;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300439
Andy Adamsona6496372009-08-28 08:45:01 -0400440 /* bound the maxreqs by NFSD_MAX_MEM_PER_SESSION */
441 mem = fchan->maxreqs * size;
442 if (mem > NFSD_MAX_MEM_PER_SESSION) {
443 fchan->maxreqs = NFSD_MAX_MEM_PER_SESSION / size;
444 if (fchan->maxreqs > NFSD_MAX_SLOTS_PER_SESSION)
445 fchan->maxreqs = NFSD_MAX_SLOTS_PER_SESSION;
446 mem = fchan->maxreqs * size;
447 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300448
Andy Adamson4bd9b0f2009-06-24 15:37:45 -0400449 spin_lock(&nfsd_drc_lock);
Andy Adamsona6496372009-08-28 08:45:01 -0400450 /* bound the total session drc memory ussage */
451 if (mem + nfsd_drc_mem_used > nfsd_drc_max_mem) {
452 fchan->maxreqs = (nfsd_drc_max_mem - nfsd_drc_mem_used) / size;
453 mem = fchan->maxreqs * size;
454 }
Andy Adamson0c193052009-07-27 19:09:19 -0400455 nfsd_drc_mem_used += mem;
Andy Adamson4bd9b0f2009-06-24 15:37:45 -0400456 spin_unlock(&nfsd_drc_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300457
Andy Adamsonb101ebb2009-07-27 18:40:09 -0400458 if (fchan->maxreqs == 0)
J. Bruce Fields47310302010-06-22 16:17:12 -0400459 return nfserr_jukebox;
Andy Adamsona6496372009-08-28 08:45:01 -0400460
461 fchan->maxresp_cached = size + NFSD_MIN_HDR_SEQ_SZ;
Andy Adamsonb101ebb2009-07-27 18:40:09 -0400462 return 0;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300463}
464
465/*
466 * fchan holds the client values on input, and the server values on output
Andy Adamsonddc04fd2009-09-23 21:32:21 -0400467 * sv_max_mesg is the maximum payload plus one page for overhead.
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300468 */
469static int init_forechannel_attrs(struct svc_rqst *rqstp,
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300470 struct nfsd4_channel_attrs *session_fchan,
471 struct nfsd4_channel_attrs *fchan)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300472{
473 int status = 0;
Andy Adamsonddc04fd2009-09-23 21:32:21 -0400474 __u32 maxcount = nfsd_serv->sv_max_mesg;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300475
476 /* headerpadsz set to zero in encode routine */
477
478 /* Use the client's max request and max response size if possible */
479 if (fchan->maxreq_sz > maxcount)
480 fchan->maxreq_sz = maxcount;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300481 session_fchan->maxreq_sz = fchan->maxreq_sz;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300482
483 if (fchan->maxresp_sz > maxcount)
484 fchan->maxresp_sz = maxcount;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300485 session_fchan->maxresp_sz = fchan->maxresp_sz;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300486
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300487 /* Use the client's maxops if possible */
488 if (fchan->maxops > NFSD_MAX_OPS_PER_COMPOUND)
489 fchan->maxops = NFSD_MAX_OPS_PER_COMPOUND;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300490 session_fchan->maxops = fchan->maxops;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300491
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300492 /* FIXME: Error means no more DRC pages so the server should
493 * recover pages from existing sessions. For now fail session
494 * creation.
495 */
Andy Adamsona6496372009-08-28 08:45:01 -0400496 status = set_forechannel_drc_size(fchan);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300497
Andy Adamsona6496372009-08-28 08:45:01 -0400498 session_fchan->maxresp_cached = fchan->maxresp_cached;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300499 session_fchan->maxreqs = fchan->maxreqs;
Andy Adamsona6496372009-08-28 08:45:01 -0400500
501 dprintk("%s status %d\n", __func__, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300502 return status;
503}
504
Andy Adamson557ce262009-08-28 08:45:04 -0400505static void
506free_session_slots(struct nfsd4_session *ses)
507{
508 int i;
509
510 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
511 kfree(ses->se_slots[i]);
512}
513
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400514/*
515 * We don't actually need to cache the rpc and session headers, so we
516 * can allocate a little less for each slot:
517 */
518static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
519{
520 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
521}
522
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300523static int
524alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp,
525 struct nfsd4_create_session *cses)
526{
527 struct nfsd4_session *new, tmp;
Andy Adamson557ce262009-08-28 08:45:04 -0400528 struct nfsd4_slot *sp;
529 int idx, slotsize, cachesize, i;
530 int status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300531
532 memset(&tmp, 0, sizeof(tmp));
533
534 /* FIXME: For now, we just accept the client back channel attributes. */
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +0300535 tmp.se_bchannel = cses->back_channel;
536 status = init_forechannel_attrs(rqstp, &tmp.se_fchannel,
537 &cses->fore_channel);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300538 if (status)
539 goto out;
540
Andy Adamson557ce262009-08-28 08:45:04 -0400541 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot)
542 + sizeof(struct nfsd4_session) > PAGE_SIZE);
543
J. Bruce Fields47310302010-06-22 16:17:12 -0400544 status = nfserr_jukebox;
Andy Adamson557ce262009-08-28 08:45:04 -0400545 /* allocate struct nfsd4_session and slot table pointers in one piece */
546 slotsize = tmp.se_fchannel.maxreqs * sizeof(struct nfsd4_slot *);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300547 new = kzalloc(sizeof(*new) + slotsize, GFP_KERNEL);
548 if (!new)
549 goto out;
550
551 memcpy(new, &tmp, sizeof(*new));
552
Andy Adamson557ce262009-08-28 08:45:04 -0400553 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400554 cachesize = slot_bytes(&new->se_fchannel);
Andy Adamson557ce262009-08-28 08:45:04 -0400555 for (i = 0; i < new->se_fchannel.maxreqs; i++) {
556 sp = kzalloc(sizeof(*sp) + cachesize, GFP_KERNEL);
557 if (!sp)
558 goto out_free;
559 new->se_slots[i] = sp;
560 }
561
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300562 new->se_client = clp;
563 gen_sessionid(new);
564 idx = hash_sessionid(&new->se_sessionid);
565 memcpy(clp->cl_sessionid.data, new->se_sessionid.data,
566 NFS4_MAX_SESSIONID_LEN);
567
568 new->se_flags = cses->flags;
569 kref_init(&new->se_ref);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300570 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300571 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
572 list_add(&new->se_perclnt, &clp->cl_sessions);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300573 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300574
575 status = nfs_ok;
576out:
577 return status;
Andy Adamson557ce262009-08-28 08:45:04 -0400578out_free:
579 free_session_slots(new);
580 kfree(new);
581 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300582}
583
Benny Halevy9089f1b2010-05-12 00:12:26 +0300584/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300585static struct nfsd4_session *
586find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
587{
588 struct nfsd4_session *elem;
589 int idx;
590
591 dump_sessionid(__func__, sessionid);
592 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300593 /* Search in the appropriate list */
594 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300595 if (!memcmp(elem->se_sessionid.data, sessionid->data,
596 NFS4_MAX_SESSIONID_LEN)) {
597 return elem;
598 }
599 }
600
601 dprintk("%s: session not found\n", __func__);
602 return NULL;
603}
604
Benny Halevy9089f1b2010-05-12 00:12:26 +0300605/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300606static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300607unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300608{
609 list_del(&ses->se_hash);
610 list_del(&ses->se_perclnt);
Marc Eshel5282fd72009-04-03 08:27:52 +0300611}
612
Andy Adamson7116ed62009-04-03 08:27:43 +0300613void
614free_session(struct kref *kref)
615{
616 struct nfsd4_session *ses;
J. Bruce Fieldsdd829c42009-10-21 17:54:13 -0400617 int mem;
Andy Adamson7116ed62009-04-03 08:27:43 +0300618
619 ses = container_of(kref, struct nfsd4_session, se_ref);
Andy Adamsonbe98d1b2009-07-27 18:49:05 -0400620 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400621 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
J. Bruce Fieldsdd829c42009-10-21 17:54:13 -0400622 nfsd_drc_mem_used -= mem;
Andy Adamsonbe98d1b2009-07-27 18:49:05 -0400623 spin_unlock(&nfsd_drc_lock);
Andy Adamson557ce262009-08-28 08:45:04 -0400624 free_session_slots(ses);
Andy Adamson7116ed62009-04-03 08:27:43 +0300625 kfree(ses);
626}
627
Benny Halevy36acb662010-05-12 00:13:04 +0300628/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300630renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Benny Halevy07cd4902010-05-12 00:13:41 +0300632 if (is_client_expired(clp)) {
633 dprintk("%s: client (clientid %08x/%08x) already expired\n",
634 __func__,
635 clp->cl_clientid.cl_boot,
636 clp->cl_clientid.cl_id);
637 return;
638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * Move client to the end to the LRU list.
642 */
643 dprintk("renewing client (clientid %08x/%08x)\n",
644 clp->cl_clientid.cl_boot,
645 clp->cl_clientid.cl_id);
646 list_move_tail(&clp->cl_lru, &client_lru);
647 clp->cl_time = get_seconds();
648}
649
Benny Halevy36acb662010-05-12 00:13:04 +0300650static inline void
651renew_client(struct nfs4_client *clp)
652{
653 spin_lock(&client_lock);
654 renew_client_locked(clp);
655 spin_unlock(&client_lock);
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
659static int
660STALE_CLIENTID(clientid_t *clid)
661{
662 if (clid->cl_boot == boot_time)
663 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +0300664 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
665 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 1;
667}
668
669/*
670 * XXX Should we use a slab cache ?
671 * This type of memory management is somewhat inefficient, but we use it
672 * anyway since SETCLIENTID is not a common operation.
673 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500674static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct nfs4_client *clp;
677
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500678 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
679 if (clp == NULL)
680 return NULL;
681 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
682 if (clp->cl_name.data == NULL) {
683 kfree(clp);
684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500686 memcpy(clp->cl_name.data, name.data, name.len);
687 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return clp;
689}
690
691static inline void
692free_client(struct nfs4_client *clp)
693{
694 if (clp->cl_cred.cr_group_info)
695 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -0500696 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(clp->cl_name.data);
698 kfree(clp);
699}
700
Benny Halevyd7682982010-05-12 00:13:54 +0300701void
702release_session_client(struct nfsd4_session *session)
703{
704 struct nfs4_client *clp = session->se_client;
705
706 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
707 return;
708 if (is_client_expired(clp)) {
709 free_client(clp);
710 session->se_client = NULL;
711 } else
712 renew_client_locked(clp);
713 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +0300714}
715
Benny Halevy84d38ac2010-05-12 00:13:16 +0300716/* must be called under the client_lock */
717static inline void
718unhash_client_locked(struct nfs4_client *clp)
719{
Benny Halevy07cd4902010-05-12 00:13:41 +0300720 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300721 list_del(&clp->cl_lru);
722 while (!list_empty(&clp->cl_sessions)) {
723 struct nfsd4_session *ses;
724 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
725 se_perclnt);
726 unhash_session(ses);
727 nfsd4_put_session(ses);
728 }
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static void
732expire_client(struct nfs4_client *clp)
733{
734 struct nfs4_stateowner *sop;
735 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct list_head reaplist;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 INIT_LIST_HEAD(&reaplist);
739 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -0700740 while (!list_empty(&clp->cl_delegations)) {
741 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
743 dp->dl_flock);
NeilBrownea1da632005-06-23 22:04:17 -0700744 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 list_move(&dp->dl_recall_lru, &reaplist);
746 }
747 spin_unlock(&recall_lock);
748 while (!list_empty(&reaplist)) {
749 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
750 list_del_init(&dp->dl_recall_lru);
751 unhash_delegation(dp);
752 }
NeilBrownea1da632005-06-23 22:04:17 -0700753 while (!list_empty(&clp->cl_openowners)) {
754 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500755 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500757 nfsd4_set_callback_client(clp, NULL);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500758 if (clp->cl_cb_conn.cb_xprt)
759 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300760 list_del(&clp->cl_idhash);
761 list_del(&clp->cl_strhash);
762 spin_lock(&client_lock);
763 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +0300764 if (atomic_read(&clp->cl_refcount) == 0)
765 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +0300766 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500769static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
770{
771 memcpy(target->cl_verifier.data, source->data,
772 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500775static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
776{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
778 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
779}
780
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500781static void copy_cred(struct svc_cred *target, struct svc_cred *source)
782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 target->cr_uid = source->cr_uid;
784 target->cr_gid = source->cr_gid;
785 target->cr_group_info = source->cr_group_info;
786 get_group_info(target->cr_group_info);
787}
788
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500789static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400790{
NeilBrowna55370a2005-06-23 22:03:52 -0700791 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400795same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
796{
797 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400801same_clid(clientid_t *cl1, clientid_t *cl2)
802{
803 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806/* XXX what about NGROUP */
807static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400808same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
809{
810 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
J. Bruce Fields5ec7b462007-11-21 21:58:56 -0500813static void gen_clid(struct nfs4_client *clp)
814{
815 static u32 current_clientid = 1;
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 clp->cl_clientid.cl_boot = boot_time;
818 clp->cl_clientid.cl_id = current_clientid++;
819}
820
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500821static void gen_confirm(struct nfs4_client *clp)
822{
823 static u32 i;
824 u32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 p = (u32 *)clp->cl_confirm.data;
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -0500827 *p++ = get_seconds();
828 *p++ = i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Ricardo Labiagab09333c2009-09-10 12:27:34 +0300831static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
832 struct svc_rqst *rqstp, nfs4_verifier *verf)
833{
834 struct nfs4_client *clp;
835 struct sockaddr *sa = svc_addr(rqstp);
836 char *princ;
837
838 clp = alloc_client(name);
839 if (clp == NULL)
840 return NULL;
841
842 princ = svc_gss_principal(rqstp);
843 if (princ) {
844 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
845 if (clp->cl_principal == NULL) {
846 free_client(clp);
847 return NULL;
848 }
849 }
850
851 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +0300852 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500853 atomic_set(&clp->cl_cb_set, 0);
Ricardo Labiagab09333c2009-09-10 12:27:34 +0300854 INIT_LIST_HEAD(&clp->cl_idhash);
855 INIT_LIST_HEAD(&clp->cl_strhash);
856 INIT_LIST_HEAD(&clp->cl_openowners);
857 INIT_LIST_HEAD(&clp->cl_delegations);
858 INIT_LIST_HEAD(&clp->cl_sessions);
859 INIT_LIST_HEAD(&clp->cl_lru);
Benny Halevy07cd4902010-05-12 00:13:41 +0300860 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +0300861 clear_bit(0, &clp->cl_cb_slot_busy);
862 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
863 copy_verf(clp, verf);
864 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
865 clp->cl_flavor = rqstp->rq_flavor;
866 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
867 gen_confirm(clp);
868
869 return clp;
870}
871
J. Bruce Fields35bba9a2007-11-21 22:07:08 -0500872static int check_name(struct xdr_netobj name)
873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (name.len == 0)
875 return 0;
876 if (name.len > NFS4_OPAQUE_LIMIT) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400877 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879 }
880 return 1;
881}
882
NeilBrownfd39ca92005-06-23 22:04:03 -0700883static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
885{
886 unsigned int idhashval;
887
888 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
889 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
890 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +0300891 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
NeilBrownfd39ca92005-06-23 22:04:03 -0700894static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895move_to_confirmed(struct nfs4_client *clp)
896{
897 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
898 unsigned int strhashval;
899
900 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700901 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -0700902 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +0300903 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 renew_client(clp);
905}
906
907static struct nfs4_client *
908find_confirmed_client(clientid_t *clid)
909{
910 struct nfs4_client *clp;
911 unsigned int idhashval = clientid_hashval(clid->cl_id);
912
913 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400914 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return clp;
916 }
917 return NULL;
918}
919
920static struct nfs4_client *
921find_unconfirmed_client(clientid_t *clid)
922{
923 struct nfs4_client *clp;
924 unsigned int idhashval = clientid_hashval(clid->cl_id);
925
926 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -0400927 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return clp;
929 }
930 return NULL;
931}
932
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300933/*
934 * Return 1 iff clp's clientid establishment method matches the use_exchange_id
935 * parameter. Matching is based on the fact the at least one of the
936 * EXCHGID4_FLAG_USE_{NON_PNFS,PNFS_MDS,PNFS_DS} flags must be set for v4.1
937 *
938 * FIXME: we need to unify the clientid namespaces for nfsv4.x
939 * and correctly deal with client upgrade/downgrade in EXCHANGE_ID
940 * and SET_CLIENTID{,_CONFIRM}
941 */
942static inline int
943match_clientid_establishment(struct nfs4_client *clp, bool use_exchange_id)
944{
945 bool has_exchange_flags = (clp->cl_exchange_flags != 0);
946 return use_exchange_id == has_exchange_flags;
947}
948
NeilBrown28ce6052005-06-23 22:03:56 -0700949static struct nfs4_client *
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300950find_confirmed_client_by_str(const char *dname, unsigned int hashval,
951 bool use_exchange_id)
NeilBrown28ce6052005-06-23 22:03:56 -0700952{
953 struct nfs4_client *clp;
954
955 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300956 if (same_name(clp->cl_recdir, dname) &&
957 match_clientid_establishment(clp, use_exchange_id))
NeilBrown28ce6052005-06-23 22:03:56 -0700958 return clp;
959 }
960 return NULL;
961}
962
963static struct nfs4_client *
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300964find_unconfirmed_client_by_str(const char *dname, unsigned int hashval,
965 bool use_exchange_id)
NeilBrown28ce6052005-06-23 22:03:56 -0700966{
967 struct nfs4_client *clp;
968
969 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
Andy Adamsona1bcecd2009-04-03 08:28:05 +0300970 if (same_name(clp->cl_recdir, dname) &&
971 match_clientid_establishment(clp, use_exchange_id))
NeilBrown28ce6052005-06-23 22:03:56 -0700972 return clp;
973 }
974 return NULL;
975}
976
NeilBrownfd39ca92005-06-23 22:04:03 -0700977static void
Jeff Laytonfbf46652009-08-14 12:57:59 -0400978gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, u32 scopeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400980 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
Jeff Layton7077ecb2009-08-14 12:57:58 -0400981 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jeff Layton7077ecb2009-08-14 12:57:58 -0400983 /* Currently, we only support tcp and tcp6 for the callback channel */
984 if (se->se_callback_netid_len == 3 &&
985 !memcmp(se->se_callback_netid_val, "tcp", 3))
986 expected_family = AF_INET;
987 else if (se->se_callback_netid_len == 4 &&
988 !memcmp(se->se_callback_netid_val, "tcp6", 4))
989 expected_family = AF_INET6;
990 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto out_err;
992
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -0400993 cb->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
994 se->se_callback_addr_len,
995 (struct sockaddr *) &cb->cb_addr,
996 sizeof(cb->cb_addr));
997
Jeff Layton7077ecb2009-08-14 12:57:58 -0400998 if (!cb->cb_addrlen || cb->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001000
Jeff Laytonfbf46652009-08-14 12:57:59 -04001001 if (cb->cb_addr.ss_family == AF_INET6)
1002 ((struct sockaddr_in6 *) &cb->cb_addr)->sin6_scope_id = scopeid;
1003
Andy Adamsonab52ae62009-06-16 04:20:53 +03001004 cb->cb_minorversion = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 cb->cb_prog = se->se_callback_prog;
1006 cb->cb_ident = se->se_callback_ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
1008out_err:
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001009 cb->cb_addr.ss_family = AF_UNSPEC;
1010 cb->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001011 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 "will not receive delegations\n",
1013 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return;
1016}
1017
Andy Adamson074fe892009-04-03 08:28:15 +03001018/*
Andy Adamson557ce262009-08-28 08:45:04 -04001019 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001020 */
1021void
1022nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1023{
Andy Adamson557ce262009-08-28 08:45:04 -04001024 struct nfsd4_slot *slot = resp->cstate.slot;
1025 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001026
Andy Adamson557ce262009-08-28 08:45:04 -04001027 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001028
Andy Adamson557ce262009-08-28 08:45:04 -04001029 slot->sl_opcnt = resp->opcnt;
1030 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001031
1032 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001033 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001034 return;
1035 }
Andy Adamson557ce262009-08-28 08:45:04 -04001036 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1037 base = (char *)resp->cstate.datap -
1038 (char *)resp->xbuf->head[0].iov_base;
1039 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1040 slot->sl_datalen))
1041 WARN("%s: sessions DRC could not cache compound\n", __func__);
1042 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001043}
1044
1045/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001046 * Encode the replay sequence operation from the slot values.
1047 * If cachethis is FALSE encode the uncached rep error on the next
1048 * operation which sets resp->p and increments resp->opcnt for
1049 * nfs4svc_encode_compoundres.
1050 *
Andy Adamson074fe892009-04-03 08:28:15 +03001051 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001052static __be32
1053nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1054 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001055{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001056 struct nfsd4_op *op;
1057 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001058
Andy Adamsonabfabf82009-07-23 19:02:18 -04001059 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
Andy Adamson557ce262009-08-28 08:45:04 -04001060 resp->opcnt, resp->cstate.slot->sl_cachethis);
Andy Adamsonabfabf82009-07-23 19:02:18 -04001061
1062 /* Encode the replayed sequence operation */
1063 op = &args->ops[resp->opcnt - 1];
1064 nfsd4_encode_operation(resp, op);
1065
1066 /* Return nfserr_retry_uncached_rep in next operation. */
Andy Adamson557ce262009-08-28 08:45:04 -04001067 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001068 op = &args->ops[resp->opcnt++];
1069 op->status = nfserr_retry_uncached_rep;
1070 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001071 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001072 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001073}
1074
1075/*
Andy Adamson557ce262009-08-28 08:45:04 -04001076 * The sequence operation is not cached because we can use the slot and
1077 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001078 */
1079__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001080nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1081 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001082{
Andy Adamson557ce262009-08-28 08:45:04 -04001083 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001084 __be32 status;
1085
Andy Adamson557ce262009-08-28 08:45:04 -04001086 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001087
Andy Adamsonabfabf82009-07-23 19:02:18 -04001088 /* Either returns 0 or nfserr_retry_uncached */
1089 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1090 if (status == nfserr_retry_uncached_rep)
1091 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001092
Andy Adamson557ce262009-08-28 08:45:04 -04001093 /* The sequence operation has been encoded, cstate->datap set. */
1094 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001095
Andy Adamson557ce262009-08-28 08:45:04 -04001096 resp->opcnt = slot->sl_opcnt;
1097 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1098 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001099
1100 return status;
1101}
1102
Andy Adamson0733d212009-04-03 08:28:01 +03001103/*
1104 * Set the exchange_id flags returned by the server.
1105 */
1106static void
1107nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1108{
1109 /* pNFS is not supported */
1110 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1111
1112 /* Referrals are supported, Migration is not. */
1113 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1114
1115 /* set the wire flags to return to client. */
1116 clid->flags = new->cl_exchange_flags;
1117}
1118
Al Virob37ad282006-10-19 23:28:59 -07001119__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001120nfsd4_exchange_id(struct svc_rqst *rqstp,
1121 struct nfsd4_compound_state *cstate,
1122 struct nfsd4_exchange_id *exid)
1123{
Andy Adamson0733d212009-04-03 08:28:01 +03001124 struct nfs4_client *unconf, *conf, *new;
1125 int status;
1126 unsigned int strhashval;
1127 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001128 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001129 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001130 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001131
Jeff Layton363168b2009-08-14 12:57:56 -04001132 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001133 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001134 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001135 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001136 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001137
1138 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1139 return nfserr_inval;
1140
1141 /* Currently only support SP4_NONE */
1142 switch (exid->spa_how) {
1143 case SP4_NONE:
1144 break;
1145 case SP4_SSV:
1146 return nfserr_encr_alg_unsupp;
1147 default:
1148 BUG(); /* checked by xdr code */
1149 case SP4_MACH_CRED:
1150 return nfserr_serverfault; /* no excuse :-/ */
1151 }
1152
1153 status = nfs4_make_rec_clidname(dname, &exid->clname);
1154
1155 if (status)
1156 goto error;
1157
1158 strhashval = clientstr_hashval(dname);
1159
1160 nfs4_lock_state();
1161 status = nfs_ok;
1162
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001163 conf = find_confirmed_client_by_str(dname, strhashval, true);
Andy Adamson0733d212009-04-03 08:28:01 +03001164 if (conf) {
1165 if (!same_verf(&verf, &conf->cl_verifier)) {
1166 /* 18.35.4 case 8 */
1167 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1168 status = nfserr_not_same;
1169 goto out;
1170 }
1171 /* Client reboot: destroy old state */
1172 expire_client(conf);
1173 goto out_new;
1174 }
1175 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1176 /* 18.35.4 case 9 */
1177 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1178 status = nfserr_perm;
1179 goto out;
1180 }
1181 expire_client(conf);
1182 goto out_new;
1183 }
Andy Adamson0733d212009-04-03 08:28:01 +03001184 /*
1185 * Set bit when the owner id and verifier map to an already
1186 * confirmed client id (18.35.3).
1187 */
1188 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1189
1190 /*
1191 * Falling into 18.35.4 case 2, possible router replay.
1192 * Leave confirmed record intact and return same result.
1193 */
1194 copy_verf(conf, &verf);
1195 new = conf;
1196 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001197 }
1198
1199 /* 18.35.4 case 7 */
1200 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1201 status = nfserr_noent;
1202 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001203 }
1204
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001205 unconf = find_unconfirmed_client_by_str(dname, strhashval, true);
Andy Adamson0733d212009-04-03 08:28:01 +03001206 if (unconf) {
1207 /*
1208 * Possible retry or client restart. Per 18.35.4 case 4,
1209 * a new unconfirmed record should be generated regardless
1210 * of whether any properties have changed.
1211 */
1212 expire_client(unconf);
1213 }
1214
1215out_new:
1216 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001217 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001218 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001219 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001220 goto out;
1221 }
1222
Andy Adamson0733d212009-04-03 08:28:01 +03001223 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001224 add_to_unconfirmed(new, strhashval);
1225out_copy:
1226 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1227 exid->clientid.cl_id = new->cl_clientid.cl_id;
1228
Andy Adamson38eb76a2009-04-03 08:28:32 +03001229 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001230 nfsd4_set_ex_flags(new, exid);
1231
1232 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001233 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001234 status = nfs_ok;
1235
1236out:
1237 nfs4_unlock_state();
1238error:
1239 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1240 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001241}
1242
Benny Halevyb85d4c02009-04-03 08:28:08 +03001243static int
Andy Adamson88e588d2009-07-23 19:02:15 -04001244check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001245{
Andy Adamson88e588d2009-07-23 19:02:15 -04001246 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1247 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001248
1249 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001250 if (slot_inuse) {
1251 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001252 return nfserr_jukebox;
1253 else
1254 return nfserr_seq_misordered;
1255 }
1256 /* Normal */
Andy Adamson88e588d2009-07-23 19:02:15 -04001257 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001258 return nfs_ok;
1259 /* Replay */
Andy Adamson88e588d2009-07-23 19:02:15 -04001260 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001261 return nfserr_replay_cache;
1262 /* Wraparound */
Andy Adamson88e588d2009-07-23 19:02:15 -04001263 if (seqid == 1 && (slot_seqid + 1) == 0)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001264 return nfs_ok;
1265 /* Misordered replay or misordered new request */
1266 return nfserr_seq_misordered;
1267}
1268
Andy Adamson49557cc2009-07-23 19:02:16 -04001269/*
1270 * Cache the create session result into the create session single DRC
1271 * slot cache by saving the xdr structure. sl_seqid has been set.
1272 * Do this for solo or embedded create session operations.
1273 */
1274static void
1275nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1276 struct nfsd4_clid_slot *slot, int nfserr)
1277{
1278 slot->sl_status = nfserr;
1279 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1280}
1281
1282static __be32
1283nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1284 struct nfsd4_clid_slot *slot)
1285{
1286 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1287 return slot->sl_status;
1288}
1289
Andy Adamson069b6ad2009-04-03 08:27:58 +03001290__be32
1291nfsd4_create_session(struct svc_rqst *rqstp,
1292 struct nfsd4_compound_state *cstate,
1293 struct nfsd4_create_session *cr_ses)
1294{
Jeff Layton363168b2009-08-14 12:57:56 -04001295 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001296 struct nfs4_client *conf, *unconf;
Andy Adamson49557cc2009-07-23 19:02:16 -04001297 struct nfsd4_clid_slot *cs_slot = NULL;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001298 int status = 0;
1299
1300 nfs4_lock_state();
1301 unconf = find_unconfirmed_client(&cr_ses->clientid);
1302 conf = find_confirmed_client(&cr_ses->clientid);
1303
1304 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001305 cs_slot = &conf->cl_cs_slot;
1306 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001307 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001308 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001309 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001310 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001311 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001312 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001313 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001314 status = nfserr_seq_misordered;
1315 dprintk("Sequence misordered!\n");
1316 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001317 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001318 goto out;
1319 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001320 cs_slot->sl_seqid++;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001321 } else if (unconf) {
1322 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001323 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001324 status = nfserr_clid_inuse;
1325 goto out;
1326 }
1327
Andy Adamson49557cc2009-07-23 19:02:16 -04001328 cs_slot = &unconf->cl_cs_slot;
1329 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001330 if (status) {
1331 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001332 status = nfserr_seq_misordered;
Andy Adamson49557cc2009-07-23 19:02:16 -04001333 goto out_cache;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001334 }
1335
Andy Adamson49557cc2009-07-23 19:02:16 -04001336 cs_slot->sl_seqid++; /* from 0 to 1 */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001337 move_to_confirmed(unconf);
1338
Andy Adamson38524ab2009-09-10 12:25:59 +03001339 if (cr_ses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001340 unconf->cl_cb_conn.cb_xprt = rqstp->rq_xprt;
1341 svc_xprt_get(rqstp->rq_xprt);
Andy Adamson38524ab2009-09-10 12:25:59 +03001342 rpc_copy_addr(
1343 (struct sockaddr *)&unconf->cl_cb_conn.cb_addr,
1344 sa);
1345 unconf->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1346 unconf->cl_cb_conn.cb_minorversion =
1347 cstate->minorversion;
1348 unconf->cl_cb_conn.cb_prog = cr_ses->callback_prog;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +03001349 unconf->cl_cb_seq_nr = 1;
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -05001350 nfsd4_probe_callback(unconf, &unconf->cl_cb_conn);
Andy Adamson38524ab2009-09-10 12:25:59 +03001351 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001352 conf = unconf;
1353 } else {
1354 status = nfserr_stale_clientid;
1355 goto out;
1356 }
1357
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001358 /*
1359 * We do not support RDMA or persistent sessions
1360 */
1361 cr_ses->flags &= ~SESSION4_PERSIST;
1362 cr_ses->flags &= ~SESSION4_RDMA;
1363
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001364 status = alloc_init_session(rqstp, conf, cr_ses);
1365 if (status)
1366 goto out;
1367
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001368 memcpy(cr_ses->sessionid.data, conf->cl_sessionid.data,
1369 NFS4_MAX_SESSIONID_LEN);
Andy Adamson49557cc2009-07-23 19:02:16 -04001370 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001371
Andy Adamson49557cc2009-07-23 19:02:16 -04001372out_cache:
1373 /* cache solo and embedded create sessions under the state lock */
1374 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001375out:
1376 nfs4_unlock_state();
1377 dprintk("%s returns %d\n", __func__, ntohl(status));
1378 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001379}
1380
J. Bruce Fields57716352010-04-21 12:27:19 -04001381static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1382{
1383 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1384 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1385
1386 return argp->opcnt == resp->opcnt;
1387}
1388
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001389static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1390{
1391 if (!session)
1392 return 0;
1393 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1394}
1395
Andy Adamson069b6ad2009-04-03 08:27:58 +03001396__be32
1397nfsd4_destroy_session(struct svc_rqst *r,
1398 struct nfsd4_compound_state *cstate,
1399 struct nfsd4_destroy_session *sessionid)
1400{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001401 struct nfsd4_session *ses;
1402 u32 status = nfserr_badsession;
1403
1404 /* Notes:
1405 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1406 * - Should we return nfserr_back_chan_busy if waiting for
1407 * callbacks on to-be-destroyed session?
1408 * - Do we need to clear any callback info from previous session?
1409 */
1410
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001411 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001412 if (!nfsd4_last_compound_op(r))
1413 return nfserr_not_only_op;
1414 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001415 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001416 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001417 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1418 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001419 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001420 goto out;
1421 }
1422
1423 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001424 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001425
Benny Halevyab707e12010-05-12 00:14:06 +03001426 nfs4_lock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03001427 /* wait for callbacks */
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001428 nfsd4_set_callback_client(ses->se_client, NULL);
Benny Halevyab707e12010-05-12 00:14:06 +03001429 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03001430 nfsd4_put_session(ses);
1431 status = nfs_ok;
1432out:
1433 dprintk("%s returns %d\n", __func__, ntohl(status));
1434 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001435}
1436
1437__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001438nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001439 struct nfsd4_compound_state *cstate,
1440 struct nfsd4_sequence *seq)
1441{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001442 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001443 struct nfsd4_session *session;
1444 struct nfsd4_slot *slot;
1445 int status;
1446
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001447 if (resp->opcnt != 1)
1448 return nfserr_sequence_pos;
1449
Benny Halevy9089f1b2010-05-12 00:12:26 +03001450 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001451 status = nfserr_badsession;
1452 session = find_in_sessionid_hashtbl(&seq->sessionid);
1453 if (!session)
1454 goto out;
1455
1456 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001457 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001458 goto out;
1459
Andy Adamson557ce262009-08-28 08:45:04 -04001460 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001461 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1462
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001463 /* We do not negotiate the number of slots yet, so set the
1464 * maxslots to the session maxreqs which is used to encode
1465 * sr_highest_slotid and the sr_target_slot id to maxslots */
1466 seq->maxslots = session->se_fchannel.maxreqs;
1467
Andy Adamson88e588d2009-07-23 19:02:15 -04001468 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001469 if (status == nfserr_replay_cache) {
1470 cstate->slot = slot;
1471 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001472 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001473 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001474 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001475 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001476 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001477 }
1478 if (status)
1479 goto out;
1480
1481 /* Success! bump slot seqid */
1482 slot->sl_inuse = true;
1483 slot->sl_seqid = seq->seqid;
Andy Adamson557ce262009-08-28 08:45:04 -04001484 slot->sl_cachethis = seq->cachethis;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001485
1486 cstate->slot = slot;
1487 cstate->session = session;
1488
Benny Halevyb85d4c02009-04-03 08:28:08 +03001489out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001490 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001491 if (cstate->session) {
Benny Halevy36acb662010-05-12 00:13:04 +03001492 nfsd4_get_session(cstate->session);
Benny Halevyd7682982010-05-12 00:13:54 +03001493 atomic_inc(&session->se_client->cl_refcount);
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001494 }
Benny Halevy36acb662010-05-12 00:13:04 +03001495 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001496 dprintk("%s: return %d\n", __func__, ntohl(status));
1497 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001498}
1499
1500__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001501nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1502{
1503 if (rc->rca_one_fs) {
1504 if (!cstate->current_fh.fh_dentry)
1505 return nfserr_nofilehandle;
1506 /*
1507 * We don't take advantage of the rca_one_fs case.
1508 * That's OK, it's optional, we can safely ignore it.
1509 */
1510 return nfs_ok;
1511 }
1512 nfs4_lock_state();
1513 if (is_client_expired(cstate->session->se_client)) {
1514 nfs4_unlock_state();
1515 /*
1516 * The following error isn't really legal.
1517 * But we only get here if the client just explicitly
1518 * destroyed the client. Surely it no longer cares what
1519 * error it gets back on an operation for the dead
1520 * client.
1521 */
1522 return nfserr_stale_clientid;
1523 }
1524 nfsd4_create_clid_dir(cstate->session->se_client);
1525 nfs4_unlock_state();
1526 return nfs_ok;
1527}
1528
1529__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001530nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1531 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Jeff Layton363168b2009-08-14 12:57:56 -04001533 struct sockaddr *sa = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct xdr_netobj clname = {
1535 .len = setclid->se_namelen,
1536 .data = setclid->se_name,
1537 };
1538 nfs4_verifier clverifier = setclid->se_verf;
1539 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07001540 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07001541 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07001542 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (!check_name(clname))
Neil Brown73aea4e2005-09-13 01:25:39 -07001545 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrowna55370a2005-06-23 22:03:52 -07001547 status = nfs4_make_rec_clidname(dname, &clname);
1548 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07001549 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1553 * We get here on a DRC miss.
1554 */
1555
NeilBrowna55370a2005-06-23 22:03:52 -07001556 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 nfs4_lock_state();
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001559 conf = find_confirmed_client_by_str(dname, strhashval, false);
NeilBrown28ce6052005-06-23 22:03:56 -07001560 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001561 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 status = nfserr_clid_inuse;
J. Bruce Fields026722c2009-03-18 15:06:26 -04001563 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04001564 char addr_str[INET6_ADDRSTRLEN];
1565 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1566 sizeof(addr_str));
1567 dprintk("NFSD: setclientid: string in use by client "
1568 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 goto out;
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001572 /*
1573 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1574 * has a description of SETCLIENTID request processing consisting
1575 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1576 */
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001577 unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 status = nfserr_resource;
1579 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001580 /*
1581 * RFC 3530 14.2.33 CASE 4:
1582 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
1584 if (unconf)
1585 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001586 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001587 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001590 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001592 * RFC 3530 14.2.33 CASE 1:
1593 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07001595 if (unconf) {
1596 /* Note this is removing unconfirmed {*x***},
1597 * which is stronger than RFC recommended {vxc**}.
1598 * This has the advantage that there is at most
1599 * one {*x***} in either list at any time.
1600 */
1601 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001603 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001604 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 } else if (!unconf) {
1608 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001609 * RFC 3530 14.2.33 CASE 2:
1610 * probable client reboot; state will be removed if
1611 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001613 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001614 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05001617 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001618 /*
1619 * RFC 3530 14.2.33 CASE 3:
1620 * probable client reboot; state will be removed if
1621 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
1623 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001624 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07001625 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Jeff Laytonfbf46652009-08-14 12:57:59 -04001629 gen_callback(new, setclid, rpc_get_scope_id(sa));
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04001630 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1632 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1633 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1634 status = nfs_ok;
1635out:
1636 nfs4_unlock_state();
1637 return status;
1638}
1639
1640
1641/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001642 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1643 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1644 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
Al Virob37ad282006-10-19 23:28:59 -07001646__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001647nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1648 struct nfsd4_compound_state *cstate,
1649 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Jeff Layton363168b2009-08-14 12:57:56 -04001651 struct sockaddr *sa = svc_addr(rqstp);
NeilBrown21ab45a2005-06-23 22:04:14 -07001652 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1654 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07001655 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (STALE_CLIENTID(clid))
1658 return nfserr_stale_clientid;
1659 /*
1660 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1661 * We get here on a DRC miss.
1662 */
1663
1664 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07001665
1666 conf = find_confirmed_client(clid);
1667 unconf = find_unconfirmed_client(clid);
1668
1669 status = nfserr_clid_inuse;
Jeff Layton363168b2009-08-14 12:57:56 -04001670 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07001671 goto out;
Jeff Layton363168b2009-08-14 12:57:56 -04001672 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
NeilBrown21ab45a2005-06-23 22:04:14 -07001673 goto out;
1674
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001675 /*
1676 * section 14.2.34 of RFC 3530 has a description of
1677 * SETCLIENTID_CONFIRM request processing consisting
1678 * of 4 bullet points, labeled as CASE1 - CASE4 below.
1679 */
J. Bruce Fields366e0c12007-11-20 15:54:10 -05001680 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001681 /*
1682 * RFC 3530 14.2.34 CASE 1:
1683 * callback update
1684 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001685 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 status = nfserr_clid_inuse;
1687 else {
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001688 atomic_set(&conf->cl_cb_set, 0);
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -05001689 nfsd4_probe_callback(conf, &unconf->cl_cb_conn);
NeilBrown1a69c172005-06-23 22:04:08 -07001690 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05001694 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001695 /*
1696 * RFC 3530 14.2.34 CASE 2:
1697 * probable retransmitted request; play it safe and
1698 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07001699 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001700 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07001702 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07001704 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001705 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001706 /*
1707 * RFC 3530 14.2.34 CASE 3:
1708 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07001709 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001710 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 status = nfserr_clid_inuse;
1712 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07001713 unsigned int hash =
1714 clientstr_hashval(unconf->cl_recdir);
1715 conf = find_confirmed_client_by_str(unconf->cl_recdir,
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001716 hash, false);
NeilBrown1a69c172005-06-23 22:04:08 -07001717 if (conf) {
NeilBrownc7b9a452005-06-23 22:04:30 -07001718 nfsd4_remove_clid_dir(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07001719 expire_client(conf);
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07001722 conf = unconf;
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -05001723 nfsd4_probe_callback(conf, &conf->cl_cb_conn);
NeilBrown1a69c172005-06-23 22:04:08 -07001724 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001726 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1727 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07001728 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05001729 /*
1730 * RFC 3530 14.2.34 CASE 4:
1731 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07001732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 status = nfserr_stale_clientid;
NeilBrown7c79f732005-06-23 22:04:13 -07001734 } else {
NeilBrown08e89872005-06-23 22:04:11 -07001735 /* check that we have hit one of the cases...*/
1736 status = nfserr_clid_inuse;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 nfs4_unlock_state();
1740 return status;
1741}
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743/* OPEN Share state helper functions */
1744static inline struct nfs4_file *
1745alloc_init_file(struct inode *ino)
1746{
1747 struct nfs4_file *fp;
1748 unsigned int hashval = file_hashval(ino);
1749
NeilBrowne60d4392005-06-23 22:03:01 -07001750 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1751 if (fp) {
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001752 atomic_set(&fp->fi_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07001754 INIT_LIST_HEAD(&fp->fi_stateids);
1755 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 fp->fi_inode = igrab(ino);
1757 fp->fi_id = current_fileid++;
Meelap Shah47f99402007-07-17 04:04:40 -07001758 fp->fi_had_conflict = false;
Pavel Emelyanov47cee542010-05-17 20:00:37 +04001759 spin_lock(&recall_lock);
1760 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1761 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return fp;
1763 }
1764 return NULL;
1765}
1766
1767static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001768nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07001769{
NeilBrowne60d4392005-06-23 22:03:01 -07001770 if (*slab == NULL)
1771 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001772 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001773 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07001774}
1775
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04001776void
NeilBrowne60d4392005-06-23 22:03:01 -07001777nfsd4_free_slabs(void)
1778{
1779 nfsd4_free_slab(&stateowner_slab);
1780 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001781 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001782 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785static int
1786nfsd4_init_slabs(void)
1787{
1788 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
Paul Mundt20c2df82007-07-20 10:11:58 +09001789 sizeof(struct nfs4_stateowner), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001790 if (stateowner_slab == NULL)
1791 goto out_nomem;
1792 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09001793 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001794 if (file_slab == NULL)
1795 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001796 stateid_slab = kmem_cache_create("nfsd4_stateids",
Paul Mundt20c2df82007-07-20 10:11:58 +09001797 sizeof(struct nfs4_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07001798 if (stateid_slab == NULL)
1799 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001800 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09001801 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001802 if (deleg_slab == NULL)
1803 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001805out_nomem:
1806 nfsd4_free_slabs();
1807 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1808 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811void
1812nfs4_free_stateowner(struct kref *kref)
1813{
1814 struct nfs4_stateowner *sop =
1815 container_of(kref, struct nfs4_stateowner, so_ref);
1816 kfree(sop->so_owner.data);
1817 kmem_cache_free(stateowner_slab, sop);
1818}
1819
1820static inline struct nfs4_stateowner *
1821alloc_stateowner(struct xdr_netobj *owner)
1822{
1823 struct nfs4_stateowner *sop;
1824
1825 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1826 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1827 memcpy(sop->so_owner.data, owner->data, owner->len);
1828 sop->so_owner.len = owner->len;
1829 kref_init(&sop->so_ref);
1830 return sop;
1831 }
1832 kmem_cache_free(stateowner_slab, sop);
1833 }
1834 return NULL;
1835}
1836
1837static struct nfs4_stateowner *
1838alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1839 struct nfs4_stateowner *sop;
1840 struct nfs4_replay *rp;
1841 unsigned int idhashval;
1842
1843 if (!(sop = alloc_stateowner(&open->op_owner)))
1844 return NULL;
1845 idhashval = ownerid_hashval(current_ownerid);
1846 INIT_LIST_HEAD(&sop->so_idhash);
1847 INIT_LIST_HEAD(&sop->so_strhash);
1848 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07001849 INIT_LIST_HEAD(&sop->so_stateids);
1850 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 INIT_LIST_HEAD(&sop->so_close_lru);
1852 sop->so_time = 0;
1853 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1854 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001855 list_add(&sop->so_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 sop->so_is_open_owner = 1;
1857 sop->so_id = current_ownerid++;
1858 sop->so_client = clp;
1859 sop->so_seqid = open->op_seqid;
1860 sop->so_confirmed = 0;
1861 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08001862 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 rp->rp_buflen = 0;
1864 rp->rp_buf = rp->rp_ibuf;
1865 return sop;
1866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868static inline void
1869init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1870 struct nfs4_stateowner *sop = open->op_stateowner;
1871 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1872
1873 INIT_LIST_HEAD(&stp->st_hash);
NeilBrownea1da632005-06-23 22:04:17 -07001874 INIT_LIST_HEAD(&stp->st_perstateowner);
1875 INIT_LIST_HEAD(&stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 INIT_LIST_HEAD(&stp->st_perfile);
1877 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
NeilBrownea1da632005-06-23 22:04:17 -07001878 list_add(&stp->st_perstateowner, &sop->so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07001879 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001881 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04001883 stp->st_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 stp->st_stateid.si_stateownerid = sop->so_id;
1885 stp->st_stateid.si_fileid = fp->fi_id;
1886 stp->st_stateid.si_generation = 0;
1887 stp->st_access_bmap = 0;
1888 stp->st_deny_bmap = 0;
Andy Adamson84459a12009-04-03 08:28:56 +03001889 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
1890 &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07001892 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896move_to_close_lru(struct nfs4_stateowner *sop)
1897{
1898 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1899
NeilBrown358dd552006-04-10 22:55:42 -07001900 list_move_tail(&sop->so_close_lru, &close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 sop->so_time = get_seconds();
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001905same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1906 clientid_t *clid)
1907{
1908 return (sop->so_owner.len == owner->len) &&
1909 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1910 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913static struct nfs4_stateowner *
1914find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1915{
1916 struct nfs4_stateowner *so = NULL;
1917
1918 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001919 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return so;
1921 }
1922 return NULL;
1923}
1924
1925/* search file_hashtbl[] for file */
1926static struct nfs4_file *
1927find_file(struct inode *ino)
1928{
1929 unsigned int hashval = file_hashval(ino);
1930 struct nfs4_file *fp;
1931
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001932 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001934 if (fp->fi_inode == ino) {
1935 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001936 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08001940 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return NULL;
1942}
1943
Andy Adamsond87a8ad2009-04-03 08:28:53 +03001944static inline int access_valid(u32 x, u32 minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001945{
Andy Adamsond87a8ad2009-04-03 08:28:53 +03001946 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001947 return 0;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03001948 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
1949 return 0;
1950 x &= ~NFS4_SHARE_ACCESS_MASK;
1951 if (minorversion && x) {
1952 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
1953 return 0;
1954 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
1955 return 0;
1956 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
1957 }
1958 if (x)
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001959 return 0;
1960 return 1;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001961}
1962
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001963static inline int deny_valid(u32 x)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001964{
J. Bruce Fields8838dc42008-01-14 13:12:19 -05001965 /* Note: unlike access bits, deny bits may be zero. */
1966 return x <= NFS4_SHARE_DENY_BOTH;
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07001967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04001969/*
1970 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1971 * st_{access,deny}_bmap field of the stateid, in order to track not
1972 * only what share bits are currently in force, but also what
1973 * combinations of share bits previous opens have used. This allows us
1974 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1975 * return an error if the client attempt to downgrade to a combination
1976 * of share bits not explicable by closing some of its previous opens.
1977 *
1978 * XXX: This enforcement is actually incomplete, since we don't keep
1979 * track of access/deny bit combinations; so, e.g., we allow:
1980 *
1981 * OPEN allow read, deny write
1982 * OPEN allow both, deny none
1983 * DOWNGRADE allow read, deny none
1984 *
1985 * which we should reject.
1986 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001987static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988set_access(unsigned int *access, unsigned long bmap) {
1989 int i;
1990
1991 *access = 0;
1992 for (i = 1; i < 4; i++) {
1993 if (test_bit(i, &bmap))
1994 *access |= i;
1995 }
1996}
1997
NeilBrownfd39ca92005-06-23 22:04:03 -07001998static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999set_deny(unsigned int *deny, unsigned long bmap) {
2000 int i;
2001
2002 *deny = 0;
2003 for (i = 0; i < 4; i++) {
2004 if (test_bit(i, &bmap))
2005 *deny |= i ;
2006 }
2007}
2008
2009static int
2010test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
2011 unsigned int access, deny;
2012
2013 set_access(&access, stp->st_access_bmap);
2014 set_deny(&deny, stp->st_deny_bmap);
2015 if ((access & open->op_share_deny) || (deny & open->op_share_access))
2016 return 0;
2017 return 1;
2018}
2019
2020/*
2021 * Called to check deny when READ with all zero stateid or
2022 * WRITE with all zero or all one stateid
2023 */
Al Virob37ad282006-10-19 23:28:59 -07002024static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2026{
2027 struct inode *ino = current_fh->fh_dentry->d_inode;
2028 struct nfs4_file *fp;
2029 struct nfs4_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002030 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 dprintk("NFSD: nfs4_share_conflict\n");
2033
2034 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002035 if (!fp)
2036 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002037 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002039 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2040 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2041 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
NeilBrown13cd2182005-06-23 22:03:10 -07002044 ret = nfs_ok;
2045out:
2046 put_nfs4_file(fp);
2047 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050static inline void
2051nfs4_file_downgrade(struct file *filp, unsigned int share_access)
2052{
2053 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
Dave Hansenaceaf782008-02-15 14:37:31 -08002054 drop_file_write_access(filp);
Wu Fengguang42e49602010-03-05 13:42:01 -08002055 spin_lock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
Wu Fengguang42e49602010-03-05 13:42:01 -08002057 spin_unlock(&filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
2061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * Spawn a thread to perform a recall on the delegation represented
2063 * by the lease (file_lock)
2064 *
2065 * Called from break_lease() with lock_kernel() held.
2066 * Note: we assume break_lease will only call this *once* for any given
2067 * lease.
2068 */
2069static
2070void nfsd_break_deleg_cb(struct file_lock *fl)
2071{
J. Bruce Fields63e48632009-05-01 22:36:55 -04002072 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
2075 if (!dp)
2076 return;
2077
2078 /* We're assuming the state code never drops its reference
2079 * without first removing the lease. Since we're in this lease
2080 * callback (and since the lease code is serialized by the kernel
2081 * lock) we know the server hasn't removed the lease yet, we know
2082 * it's safe to take a reference: */
2083 atomic_inc(&dp->dl_count);
2084
2085 spin_lock(&recall_lock);
2086 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2087 spin_unlock(&recall_lock);
2088
2089 /* only place dl_time is set. protected by lock_kernel*/
2090 dp->dl_time = get_seconds();
2091
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002092 /*
2093 * We don't want the locks code to timeout the lease for us;
2094 * we'll remove it ourself if the delegation isn't returned
2095 * in time.
2096 */
2097 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
J. Bruce Fields63e48632009-05-01 22:36:55 -04002099 dp->dl_file->fi_had_conflict = true;
2100 nfsd4_cb_recall(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
2103/*
2104 * The file_lock is being reapd.
2105 *
2106 * Called by locks_free_lock() with lock_kernel() held.
2107 */
2108static
2109void nfsd_release_deleg_cb(struct file_lock *fl)
2110{
2111 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
2112
2113 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
2114
2115 if (!(fl->fl_flags & FL_LEASE) || !dp)
2116 return;
2117 dp->dl_flock = NULL;
2118}
2119
2120/*
2121 * Set the delegation file_lock back pointer.
2122 *
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04002123 * Called from setlease() with lock_kernel() held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 */
2125static
2126void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
2127{
2128 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
2129
2130 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
2131 if (!dp)
2132 return;
2133 dp->dl_flock = new;
2134}
2135
2136/*
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04002137 * Called from setlease() with lock_kernel() held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 */
2139static
2140int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
2141{
2142 struct nfs4_delegation *onlistd =
2143 (struct nfs4_delegation *)onlist->fl_owner;
2144 struct nfs4_delegation *tryd =
2145 (struct nfs4_delegation *)try->fl_owner;
2146
2147 if (onlist->fl_lmops != try->fl_lmops)
2148 return 0;
2149
2150 return onlistd->dl_client == tryd->dl_client;
2151}
2152
2153
2154static
2155int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2156{
2157 if (arg & F_UNLCK)
2158 return lease_modify(onlist, arg);
2159 else
2160 return -EAGAIN;
2161}
2162
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002163static const struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 .fl_break = nfsd_break_deleg_cb,
2165 .fl_release_private = nfsd_release_deleg_cb,
2166 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
2167 .fl_mylease = nfsd_same_client_deleg_cb,
2168 .fl_change = nfsd_change_deleg_cb,
2169};
2170
2171
Al Virob37ad282006-10-19 23:28:59 -07002172__be32
Andy Adamson66689582009-04-03 08:28:45 +03002173nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2174 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 clientid_t *clientid = &open->op_clientid;
2177 struct nfs4_client *clp = NULL;
2178 unsigned int strhashval;
2179 struct nfs4_stateowner *sop = NULL;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!check_name(open->op_owner))
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002182 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 if (STALE_CLIENTID(&open->op_clientid))
2185 return nfserr_stale_clientid;
2186
2187 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2188 sop = find_openstateowner_str(strhashval, open);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002189 open->op_stateowner = sop;
2190 if (!sop) {
2191 /* Make sure the client's lease hasn't expired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 clp = find_confirmed_client(clientid);
2193 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002194 return nfserr_expired;
2195 goto renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
Andy Adamson66689582009-04-03 08:28:45 +03002197 /* When sessions are used, skip open sequenceid processing */
2198 if (nfsd4_has_session(cstate))
2199 goto renew;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002200 if (!sop->so_confirmed) {
2201 /* Replace unconfirmed owners without checking for replay. */
2202 clp = sop->so_client;
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05002203 release_openowner(sop);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002204 open->op_stateowner = NULL;
2205 goto renew;
2206 }
2207 if (open->op_seqid == sop->so_seqid - 1) {
2208 if (sop->so_replay.rp_buflen)
Al Viroa90b0612006-10-19 23:29:03 -07002209 return nfserr_replay_me;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002210 /* The original OPEN failed so spectacularly
2211 * that we don't even have replay data saved!
2212 * Therefore, we have no choice but to continue
2213 * processing this OPEN; presumably, we'll
2214 * fail again for the same reason.
2215 */
2216 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2217 goto renew;
2218 }
2219 if (open->op_seqid != sop->so_seqid)
2220 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221renew:
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002222 if (open->op_stateowner == NULL) {
2223 sop = alloc_init_open_stateowner(strhashval, clp, open);
2224 if (sop == NULL)
2225 return nfserr_resource;
2226 open->op_stateowner = sop;
2227 }
2228 list_del_init(&sop->so_close_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 renew_client(sop->so_client);
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002230 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Al Virob37ad282006-10-19 23:28:59 -07002233static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002234nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2235{
2236 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2237 return nfserr_openmode;
2238 else
2239 return nfs_ok;
2240}
2241
NeilBrown52f4fb42005-06-23 22:02:49 -07002242static struct nfs4_delegation *
2243find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2244{
2245 struct nfs4_delegation *dp;
2246
NeilBrownea1da632005-06-23 22:04:17 -07002247 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07002248 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
2249 return dp;
2250 }
2251 return NULL;
2252}
2253
J. Bruce Fields24a01112010-05-18 20:01:35 -04002254int share_access_to_flags(u32 share_access)
2255{
2256 share_access &= ~NFS4_SHARE_WANT_MASK;
2257
2258 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2259}
2260
Al Virob37ad282006-10-19 23:28:59 -07002261static __be32
NeilBrown567d9822005-06-23 22:02:53 -07002262nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2263 struct nfs4_delegation **dp)
2264{
2265 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002266 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002267
2268 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2269 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002270 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002271 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002272 status = nfs4_check_delegmode(*dp, flags);
2273 if (status)
2274 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002275out:
2276 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2277 return nfs_ok;
2278 if (status)
2279 return status;
2280 open->op_stateowner->so_confirmed = 1;
2281 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002282}
2283
Al Virob37ad282006-10-19 23:28:59 -07002284static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2286{
2287 struct nfs4_stateid *local;
Al Virob37ad282006-10-19 23:28:59 -07002288 __be32 status = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct nfs4_stateowner *sop = open->op_stateowner;
2290
NeilBrown8beefa22005-06-23 22:03:08 -07002291 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /* ignore lock owners */
2293 if (local->st_stateowner->so_is_open_owner == 0)
2294 continue;
2295 /* remember if we have seen this open owner */
2296 if (local->st_stateowner == sop)
2297 *stpp = local;
2298 /* check for conflicting share reservations */
2299 if (!test_share(local, open))
2300 goto out;
2301 }
2302 status = 0;
2303out:
2304 return status;
2305}
2306
NeilBrown5ac049a2005-06-23 22:03:03 -07002307static inline struct nfs4_stateid *
2308nfs4_alloc_stateid(void)
2309{
2310 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2311}
2312
Al Virob37ad282006-10-19 23:28:59 -07002313static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07002315 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct svc_fh *cur_fh, int flags)
2317{
2318 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
NeilBrown5ac049a2005-06-23 22:03:03 -07002320 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (stp == NULL)
2322 return nfserr_resource;
2323
NeilBrown567d9822005-06-23 22:02:53 -07002324 if (dp) {
2325 get_file(dp->dl_vfs_file);
2326 stp->st_vfs_file = dp->dl_vfs_file;
2327 } else {
Al Virob37ad282006-10-19 23:28:59 -07002328 __be32 status;
NeilBrown567d9822005-06-23 22:02:53 -07002329 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
2330 &stp->st_vfs_file);
2331 if (status) {
2332 if (status == nfserr_dropit)
2333 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07002334 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07002335 return status;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 *stpp = stp;
2339 return 0;
2340}
2341
Al Virob37ad282006-10-19 23:28:59 -07002342static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2344 struct nfsd4_open *open)
2345{
2346 struct iattr iattr = {
2347 .ia_valid = ATTR_SIZE,
2348 .ia_size = 0,
2349 };
2350 if (!open->op_truncate)
2351 return 0;
2352 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002353 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2355}
2356
Al Virob37ad282006-10-19 23:28:59 -07002357static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2359{
2360 struct file *filp = stp->st_vfs_file;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002361 struct inode *inode = filp->f_path.dentry->d_inode;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002362 unsigned int share_access, new_writer;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002363 u32 op_share_access;
Al Virob37ad282006-10-19 23:28:59 -07002364 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 set_access(&share_access, stp->st_access_bmap);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002367 new_writer = (~share_access) & open->op_share_access
2368 & NFS4_SHARE_ACCESS_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002370 if (new_writer) {
Al Virob37ad282006-10-19 23:28:59 -07002371 int err = get_write_access(inode);
2372 if (err)
2373 return nfserrno(err);
Benny Halevye518f052008-07-04 15:38:41 +03002374 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
2375 if (err)
2376 return nfserrno(err);
2377 file_take_write(filp);
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 status = nfsd4_truncate(rqstp, cur_fh, open);
2380 if (status) {
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002381 if (new_writer)
2382 put_write_access(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return status;
2384 }
2385 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002386 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2387 filp->f_mode |= op_share_access;
2388 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002389 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 return nfs_ok;
2392}
2393
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395static void
NeilBrown37515172005-07-07 17:59:16 -07002396nfs4_set_claim_prev(struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
NeilBrown37515172005-07-07 17:59:16 -07002398 open->op_stateowner->so_confirmed = 1;
2399 open->op_stateowner->so_client->cl_firststate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
2402/*
2403 * Attempt to hand out a delegation.
2404 */
2405static void
2406nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2407{
2408 struct nfs4_delegation *dp;
2409 struct nfs4_stateowner *sop = stp->st_stateowner;
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002410 int cb_up = atomic_read(&sop->so_client->cl_cb_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 struct file_lock fl, *flp = &fl;
2412 int status, flag = 0;
2413
2414 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002415 open->op_recall = 0;
2416 switch (open->op_claim_type) {
2417 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002418 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002419 open->op_recall = 1;
2420 flag = open->op_delegate_type;
2421 if (flag == NFS4_OPEN_DELEGATE_NONE)
2422 goto out;
2423 break;
2424 case NFS4_OPEN_CLAIM_NULL:
2425 /* Let's not give out any delegations till everyone's
2426 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002427 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002428 goto out;
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002429 if (!cb_up || !sop->so_confirmed)
NeilBrown7b190fe2005-06-23 22:03:23 -07002430 goto out;
2431 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2432 flag = NFS4_OPEN_DELEGATE_WRITE;
2433 else
2434 flag = NFS4_OPEN_DELEGATE_READ;
2435 break;
2436 default:
2437 goto out;
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2441 if (dp == NULL) {
2442 flag = NFS4_OPEN_DELEGATE_NONE;
2443 goto out;
2444 }
2445 locks_init_lock(&fl);
2446 fl.fl_lmops = &nfsd_lease_mng_ops;
2447 fl.fl_flags = FL_LEASE;
Felix Blyakher9167f502008-02-26 10:54:36 -08002448 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 fl.fl_end = OFFSET_MAX;
2450 fl.fl_owner = (fl_owner_t)dp;
2451 fl.fl_file = stp->st_vfs_file;
2452 fl.fl_pid = current->tgid;
2453
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04002454 /* vfs_setlease checks to see if delegation should be handed out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * the lock_manager callbacks fl_mylease and fl_change are used
2456 */
Felix Blyakher9167f502008-02-26 10:54:36 -08002457 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07002459 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 flag = NFS4_OPEN_DELEGATE_NONE;
2461 goto out;
2462 }
2463
2464 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2465
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002466 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2467 STATEID_VAL(&dp->dl_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468out:
NeilBrown7b190fe2005-06-23 22:03:23 -07002469 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2470 && flag == NFS4_OPEN_DELEGATE_NONE
2471 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002472 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 open->op_delegate_type = flag;
2474}
2475
2476/*
2477 * called with nfs4_lock_state() held.
2478 */
Al Virob37ad282006-10-19 23:28:59 -07002479__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2481{
Andy Adamson66689582009-04-03 08:28:45 +03002482 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 struct nfs4_file *fp = NULL;
2484 struct inode *ino = current_fh->fh_dentry->d_inode;
2485 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07002486 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002487 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 status = nfserr_inval;
Andy Adamsond87a8ad2009-04-03 08:28:53 +03002490 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07002491 || !deny_valid(open->op_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto out;
2493 /*
2494 * Lookup file; if found, lookup stateid and check open request,
2495 * and check for delegations in the process of being recalled.
2496 * If not found, create the nfs4_file struct
2497 */
2498 fp = find_file(ino);
2499 if (fp) {
2500 if ((status = nfs4_check_open(fp, open, &stp)))
2501 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002502 status = nfs4_check_deleg(fp, open, &dp);
2503 if (status)
2504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07002506 status = nfserr_bad_stateid;
2507 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 status = nfserr_resource;
2510 fp = alloc_init_file(ino);
2511 if (fp == NULL)
2512 goto out;
2513 }
2514
2515 /*
2516 * OPEN the file, or upgrade an existing OPEN.
2517 * If truncate fails, the OPEN fails.
2518 */
2519 if (stp) {
2520 /* Stateid was found, this is an OPEN upgrade */
2521 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
2522 if (status)
2523 goto out;
NeilBrown444c2c02005-07-07 17:59:22 -07002524 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 } else {
2526 /* Stateid was not found, this is a new OPEN */
2527 int flags = 0;
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -07002528 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002529 flags |= NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002531 flags |= NFSD_MAY_WRITE;
NeilBrown567d9822005-06-23 22:02:53 -07002532 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
2533 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 goto out;
2535 init_stateid(stp, fp, open);
2536 status = nfsd4_truncate(rqstp, current_fh, open);
2537 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05002538 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 goto out;
2540 }
Andy Adamson66689582009-04-03 08:28:45 +03002541 if (nfsd4_has_session(&resp->cstate))
2542 update_stateid(&stp->st_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2545
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002546 if (nfsd4_has_session(&resp->cstate))
Andy Adamson66689582009-04-03 08:28:45 +03002547 open->op_stateowner->so_confirmed = 1;
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /*
2550 * Attempt to hand out a delegation. No error return, because the
2551 * OPEN succeeds even if we fail.
2552 */
2553 nfs4_open_delegation(current_fh, open, stp);
2554
2555 status = nfs_ok;
2556
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002557 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2558 STATEID_VAL(&stp->st_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559out:
NeilBrown13cd2182005-06-23 22:03:10 -07002560 if (fp)
2561 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07002562 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2563 nfs4_set_claim_prev(open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /*
2565 * To finish the open response, we just need to set the rflags.
2566 */
2567 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Andy Adamson66689582009-04-03 08:28:45 +03002568 if (!open->op_stateowner->so_confirmed &&
2569 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2571
2572 return status;
2573}
2574
Al Virob37ad282006-10-19 23:28:59 -07002575__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002576nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2577 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07002580 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 nfs4_lock_state();
2583 dprintk("process_renew(%08x/%08x): starting\n",
2584 clid->cl_boot, clid->cl_id);
2585 status = nfserr_stale_clientid;
2586 if (STALE_CLIENTID(clid))
2587 goto out;
2588 clp = find_confirmed_client(clid);
2589 status = nfserr_expired;
2590 if (clp == NULL) {
2591 /* We assume the client took too long to RENEW. */
2592 dprintk("nfsd4_renew: clientid not found!\n");
2593 goto out;
2594 }
2595 renew_client(clp);
2596 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07002597 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002598 && !atomic_read(&clp->cl_cb_set))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 goto out;
2600 status = nfs_ok;
2601out:
2602 nfs4_unlock_state();
2603 return status;
2604}
2605
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002606struct lock_manager nfsd4_manager = {
2607};
2608
NeilBrowna76b4312005-06-23 22:04:01 -07002609static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002610nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07002611{
2612 dprintk("NFSD: end of grace period\n");
NeilBrownc7b9a452005-06-23 22:04:30 -07002613 nfsd4_recdir_purge_old();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002614 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05002615 /*
2616 * Now that every NFSv4 client has had the chance to recover and
2617 * to see the (possibly new, possibly shorter) lease time, we
2618 * can safely set the next grace time to the current lease time:
2619 */
2620 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07002621}
2622
NeilBrownfd39ca92005-06-23 22:04:03 -07002623static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624nfs4_laundromat(void)
2625{
2626 struct nfs4_client *clp;
2627 struct nfs4_stateowner *sop;
2628 struct nfs4_delegation *dp;
2629 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002630 time_t cutoff = get_seconds() - nfsd4_lease;
2631 time_t t, clientid_val = nfsd4_lease;
2632 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 nfs4_lock_state();
2635
2636 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002637 if (locks_in_grace())
2638 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03002639 INIT_LIST_HEAD(&reaplist);
2640 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 list_for_each_safe(pos, next, &client_lru) {
2642 clp = list_entry(pos, struct nfs4_client, cl_lru);
2643 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2644 t = clp->cl_time - cutoff;
2645 if (clientid_val > t)
2646 clientid_val = t;
2647 break;
2648 }
Benny Halevyd7682982010-05-12 00:13:54 +03002649 if (atomic_read(&clp->cl_refcount)) {
2650 dprintk("NFSD: client in use (clientid %08x)\n",
2651 clp->cl_clientid.cl_id);
2652 continue;
2653 }
2654 unhash_client_locked(clp);
2655 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03002656 }
2657 spin_unlock(&client_lock);
2658 list_for_each_safe(pos, next, &reaplist) {
2659 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 dprintk("NFSD: purging unused client (clientid %08x)\n",
2661 clp->cl_clientid.cl_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07002662 nfsd4_remove_clid_dir(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 expire_client(clp);
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 spin_lock(&recall_lock);
2666 list_for_each_safe(pos, next, &del_recall_lru) {
2667 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2668 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2669 u = dp->dl_time - cutoff;
2670 if (test_val > u)
2671 test_val = u;
2672 break;
2673 }
2674 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
2675 dp, dp->dl_flock);
2676 list_move(&dp->dl_recall_lru, &reaplist);
2677 }
2678 spin_unlock(&recall_lock);
2679 list_for_each_safe(pos, next, &reaplist) {
2680 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2681 list_del_init(&dp->dl_recall_lru);
2682 unhash_delegation(dp);
2683 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002684 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 list_for_each_safe(pos, next, &close_lru) {
2686 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2687 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2688 u = sop->so_time - cutoff;
2689 if (test_val > u)
2690 test_val = u;
2691 break;
2692 }
2693 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2694 sop->so_id);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05002695 release_openowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
2697 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2698 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2699 nfs4_unlock_state();
2700 return clientid_val;
2701}
2702
Harvey Harrisona254b242008-02-20 12:49:00 -08002703static struct workqueue_struct *laundry_wq;
2704static void laundromat_main(struct work_struct *);
2705static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2706
2707static void
David Howellsc4028952006-11-22 14:57:56 +00002708laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 time_t t;
2711
2712 t = nfs4_laundromat();
2713 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07002714 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
NeilBrownfd39ca92005-06-23 22:04:03 -07002717static struct nfs4_stateowner *
NeilBrownf8816512005-07-07 17:59:25 -07002718search_close_lru(u32 st_id, int flags)
2719{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct nfs4_stateowner *local = NULL;
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (flags & CLOSE_STATE) {
2723 list_for_each_entry(local, &close_lru, so_close_lru) {
2724 if (local->so_id == st_id)
2725 return local;
2726 }
2727 }
2728 return NULL;
2729}
2730
2731static inline int
2732nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2733{
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08002734 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
2737static int
2738STALE_STATEID(stateid_t *stateid)
2739{
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002740 if (stateid->si_boot == boot_time)
2741 return 0;
2742 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002743 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002744 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747static inline int
2748access_permit_read(unsigned long access_bmap)
2749{
2750 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2751 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2752 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2753}
2754
2755static inline int
2756access_permit_write(unsigned long access_bmap)
2757{
2758 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2759 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2760}
2761
2762static
Al Virob37ad282006-10-19 23:28:59 -07002763__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Al Virob37ad282006-10-19 23:28:59 -07002765 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2768 goto out;
2769 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2770 goto out;
2771 status = nfs_ok;
2772out:
2773 return status;
2774}
2775
Al Virob37ad282006-10-19 23:28:59 -07002776static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2778{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002779 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002781 else if (locks_in_grace()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /* Answer in remaining cases depends on existance of
2783 * conflicting state; so we must wait out the grace period. */
2784 return nfserr_grace;
2785 } else if (flags & WR_STATE)
2786 return nfs4_share_conflict(current_fh,
2787 NFS4_SHARE_DENY_WRITE);
2788 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2789 return nfs4_share_conflict(current_fh,
2790 NFS4_SHARE_DENY_READ);
2791}
2792
2793/*
2794 * Allow READ/WRITE during grace period on recovered state only for files
2795 * that are not able to provide mandatory locking.
2796 */
2797static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08002798grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08002800 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Andy Adamson66689582009-04-03 08:28:45 +03002803static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
J. Bruce Fields0836f582008-01-26 19:08:12 -05002804{
Andy Adamson66689582009-04-03 08:28:45 +03002805 /*
2806 * When sessions are used the stateid generation number is ignored
2807 * when it is zero.
2808 */
2809 if ((flags & HAS_SESSION) && in->si_generation == 0)
2810 goto out;
2811
J. Bruce Fields0836f582008-01-26 19:08:12 -05002812 /* If the client sends us a stateid from the future, it's buggy: */
2813 if (in->si_generation > ref->si_generation)
2814 return nfserr_bad_stateid;
2815 /*
2816 * The following, however, can happen. For example, if the
2817 * client sends an open and some IO at the same time, the open
2818 * may bump si_generation while the IO is still in flight.
2819 * Thanks to hard links and renames, the client never knows what
2820 * file an open will affect. So it could avoid that situation
2821 * only by serializing all opens and IO from the same open
2822 * owner. To recover from the old_stateid error, the client
2823 * will just have to retry the IO:
2824 */
2825 if (in->si_generation < ref->si_generation)
2826 return nfserr_old_stateid;
Andy Adamson66689582009-04-03 08:28:45 +03002827out:
J. Bruce Fields0836f582008-01-26 19:08:12 -05002828 return nfs_ok;
2829}
2830
J. Bruce Fields3e633072009-02-21 13:17:19 -08002831static int is_delegation_stateid(stateid_t *stateid)
2832{
2833 return stateid->si_fileid == 0;
2834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836/*
2837* Checks for stateid operations
2838*/
Al Virob37ad282006-10-19 23:28:59 -07002839__be32
Benny Halevydd453df2009-04-03 08:28:41 +03002840nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
2841 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct nfs4_stateid *stp = NULL;
2844 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03002845 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07002847 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (filpp)
2850 *filpp = NULL;
2851
J. Bruce Fields18f82732009-02-21 15:23:01 -08002852 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return nfserr_grace;
2854
Andy Adamson66689582009-04-03 08:28:45 +03002855 if (nfsd4_has_session(cstate))
2856 flags |= HAS_SESSION;
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2859 return check_special_stateids(current_fh, stateid, flags);
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 status = nfserr_stale_stateid;
2862 if (STALE_STATEID(stateid))
2863 goto out;
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 status = nfserr_bad_stateid;
J. Bruce Fields3e633072009-02-21 13:17:19 -08002866 if (is_delegation_stateid(stateid)) {
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002867 dp = find_delegation_stateid(ino, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002868 if (!dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03002870 status = check_stateid_generation(stateid, &dp->dl_stateid,
2871 flags);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002872 if (status)
2873 goto out;
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002874 status = nfs4_check_delegmode(dp, flags);
2875 if (status)
2876 goto out;
2877 renew_client(dp->dl_client);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08002878 if (filpp)
2879 *filpp = dp->dl_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 } else { /* open or lock stateid */
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002881 stp = find_stateid(stateid, flags);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002882 if (!stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 goto out;
J. Bruce Fields6150ef02009-02-21 13:36:16 -08002884 if (nfs4_check_fh(current_fh, stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 goto out;
2886 if (!stp->st_stateowner->so_confirmed)
2887 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03002888 status = check_stateid_generation(stateid, &stp->st_stateid,
2889 flags);
J. Bruce Fields0c2a4982009-02-21 11:11:50 -08002890 if (status)
2891 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08002892 status = nfs4_check_openmode(stp, flags);
2893 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 goto out;
2895 renew_client(stp->st_stateowner->so_client);
2896 if (filpp)
2897 *filpp = stp->st_vfs_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899 status = nfs_ok;
2900out:
2901 return status;
2902}
2903
NeilBrown4c4cd222005-07-07 17:59:27 -07002904static inline int
2905setlkflg (int type)
2906{
2907 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2908 RD_STATE : WR_STATE;
2909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911/*
2912 * Checks for sequence id mutating operations.
2913 */
Al Virob37ad282006-10-19 23:28:59 -07002914static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03002915nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2916 stateid_t *stateid, int flags,
2917 struct nfs4_stateowner **sopp,
2918 struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct nfs4_stateid *stp;
2921 struct nfs4_stateowner *sop;
Benny Halevydd453df2009-04-03 08:28:41 +03002922 struct svc_fh *current_fh = &cstate->current_fh;
J. Bruce Fields0836f582008-01-26 19:08:12 -05002923 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002925 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
2926 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *stpp = NULL;
2929 *sopp = NULL;
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002932 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002933 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (STALE_STATEID(stateid))
NeilBrown3a4f98b2005-07-07 17:59:26 -07002937 return nfserr_stale_stateid;
Andy Adamson66689582009-04-03 08:28:45 +03002938
2939 if (nfsd4_has_session(cstate))
2940 flags |= HAS_SESSION;
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /*
2943 * We return BAD_STATEID if filehandle doesn't match stateid,
2944 * the confirmed flag is incorrecly set, or the generation
2945 * number is incorrect.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 */
NeilBrownf8816512005-07-07 17:59:25 -07002947 stp = find_stateid(stateid, flags);
2948 if (stp == NULL) {
2949 /*
2950 * Also, we should make sure this isn't just the result of
2951 * a replayed close:
2952 */
2953 sop = search_close_lru(stateid->si_stateownerid, flags);
2954 if (sop == NULL)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04002955 return nfserr_bad_stateid;
NeilBrownf8816512005-07-07 17:59:25 -07002956 *sopp = sop;
2957 goto check_replay;
2958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
J. Bruce Fields39325bd2007-11-26 17:06:39 -05002960 *stpp = stp;
2961 *sopp = sop = stp->st_stateowner;
2962
NeilBrown4c4cd222005-07-07 17:59:27 -07002963 if (lock) {
NeilBrown4c4cd222005-07-07 17:59:27 -07002964 clientid_t *lockclid = &lock->v.new.clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct nfs4_client *clp = sop->so_client;
NeilBrown4c4cd222005-07-07 17:59:27 -07002966 int lkflg = 0;
Al Virob37ad282006-10-19 23:28:59 -07002967 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
NeilBrown4c4cd222005-07-07 17:59:27 -07002969 lkflg = setlkflg(lock->lk_type);
2970
2971 if (lock->lk_is_new) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002972 if (!sop->so_is_open_owner)
2973 return nfserr_bad_stateid;
Andy Adamson60adfc52009-04-03 08:28:50 +03002974 if (!(flags & HAS_SESSION) &&
2975 !same_clid(&clp->cl_clientid, lockclid))
2976 return nfserr_bad_stateid;
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002977 /* stp is the open stateid */
2978 status = nfs4_check_openmode(stp, lkflg);
2979 if (status)
2980 return status;
2981 } else {
2982 /* stp is the lock stateid */
2983 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2984 if (status)
2985 return status;
NeilBrown4c4cd222005-07-07 17:59:27 -07002986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05002989 if (nfs4_check_fh(current_fh, stp)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04002990 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
NeilBrown3a4f98b2005-07-07 17:59:26 -07002991 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /*
2995 * We now validate the seqid and stateid generation numbers.
2996 * For the moment, we ignore the possibility of
2997 * generation number wraparound.
2998 */
Andy Adamson66689582009-04-03 08:28:45 +03002999 if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto check_replay;
3001
NeilBrown3a4f98b2005-07-07 17:59:26 -07003002 if (sop->so_confirmed && flags & CONFIRM) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003003 dprintk("NFSD: preprocess_seqid_op: expected"
NeilBrown3a4f98b2005-07-07 17:59:26 -07003004 " unconfirmed stateowner!\n");
3005 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
NeilBrown3a4f98b2005-07-07 17:59:26 -07003007 if (!sop->so_confirmed && !(flags & CONFIRM)) {
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003008 dprintk("NFSD: preprocess_seqid_op: stateowner not"
NeilBrown3a4f98b2005-07-07 17:59:26 -07003009 " confirmed yet!\n");
3010 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
Andy Adamson66689582009-04-03 08:28:45 +03003012 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
J. Bruce Fields0836f582008-01-26 19:08:12 -05003013 if (status)
3014 return status;
NeilBrown52fd0042005-07-07 17:59:24 -07003015 renew_client(sop->so_client);
NeilBrown3a4f98b2005-07-07 17:59:26 -07003016 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018check_replay:
NeilBrownbd9aac52005-07-07 17:59:19 -07003019 if (seqid == sop->so_seqid - 1) {
Neil Brown849823c2005-09-13 01:25:36 -07003020 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 /* indicate replay to calling function */
Al Viroa90b0612006-10-19 23:29:03 -07003022 return nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003024 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
NeilBrown3a4f98b2005-07-07 17:59:26 -07003025 sop->so_seqid, seqid);
3026 *sopp = NULL;
3027 return nfserr_bad_seqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Al Virob37ad282006-10-19 23:28:59 -07003030__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003031nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003032 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Al Virob37ad282006-10-19 23:28:59 -07003034 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct nfs4_stateowner *sop;
3036 struct nfs4_stateid *stp;
3037
3038 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003039 (int)cstate->current_fh.fh_dentry->d_name.len,
3040 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003042 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003043 if (status)
3044 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 nfs4_lock_state();
3047
Benny Halevydd453df2009-04-03 08:28:41 +03003048 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003049 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003050 CONFIRM | OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 &oc->oc_stateowner, &stp, NULL)))
3052 goto out;
3053
3054 sop = oc->oc_stateowner;
3055 sop->so_confirmed = 1;
3056 update_stateid(&stp->st_stateid);
3057 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003058 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3059 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003060
3061 nfsd4_create_clid_dir(sop->so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062out:
Neil Brownf2327d92005-09-13 01:25:37 -07003063 if (oc->oc_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 nfs4_get_stateowner(oc->oc_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003065 cstate->replay_owner = oc->oc_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 nfs4_unlock_state();
3068 return status;
3069}
3070
3071
3072/*
3073 * unset all bits in union bitmap (bmap) that
3074 * do not exist in share (from successful OPEN_DOWNGRADE)
3075 */
3076static void
3077reset_union_bmap_access(unsigned long access, unsigned long *bmap)
3078{
3079 int i;
3080 for (i = 1; i < 4; i++) {
3081 if ((i & access) != i)
3082 __clear_bit(i, bmap);
3083 }
3084}
3085
3086static void
3087reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3088{
3089 int i;
3090 for (i = 0; i < 4; i++) {
3091 if ((i & deny) != i)
3092 __clear_bit(i, bmap);
3093 }
3094}
3095
Al Virob37ad282006-10-19 23:28:59 -07003096__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003097nfsd4_open_downgrade(struct svc_rqst *rqstp,
3098 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003099 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Al Virob37ad282006-10-19 23:28:59 -07003101 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct nfs4_stateid *stp;
3103 unsigned int share_access;
3104
3105 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003106 (int)cstate->current_fh.fh_dentry->d_name.len,
3107 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Andy Adamsond87a8ad2009-04-03 08:28:53 +03003109 if (!access_valid(od->od_share_access, cstate->minorversion)
J. Bruce Fieldsba5a6a12006-06-30 01:56:16 -07003110 || !deny_valid(od->od_share_deny))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return nfserr_inval;
3112
3113 nfs4_lock_state();
Benny Halevydd453df2009-04-03 08:28:41 +03003114 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003115 od->od_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 &od->od_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003117 OPEN_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 &od->od_stateowner, &stp, NULL)))
3119 goto out;
3120
3121 status = nfserr_inval;
3122 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3123 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3124 stp->st_access_bmap, od->od_share_access);
3125 goto out;
3126 }
3127 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3128 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3129 stp->st_deny_bmap, od->od_share_deny);
3130 goto out;
3131 }
3132 set_access(&share_access, stp->st_access_bmap);
3133 nfs4_file_downgrade(stp->st_vfs_file,
3134 share_access & ~od->od_share_access);
3135
3136 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
3137 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3138
3139 update_stateid(&stp->st_stateid);
3140 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3141 status = nfs_ok;
3142out:
Neil Brownf2327d92005-09-13 01:25:37 -07003143 if (od->od_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 nfs4_get_stateowner(od->od_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003145 cstate->replay_owner = od->od_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 nfs4_unlock_state();
3148 return status;
3149}
3150
3151/*
3152 * nfs4_unlock_state() called after encode
3153 */
Al Virob37ad282006-10-19 23:28:59 -07003154__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003155nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003156 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Al Virob37ad282006-10-19 23:28:59 -07003158 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct nfs4_stateid *stp;
3160
3161 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003162 (int)cstate->current_fh.fh_dentry->d_name.len,
3163 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 nfs4_lock_state();
3166 /* check close_lru for replay */
Benny Halevydd453df2009-04-03 08:28:41 +03003167 if ((status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003168 close->cl_seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 &close->cl_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003170 OPEN_STATE | CLOSE_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 &close->cl_stateowner, &stp, NULL)))
3172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 status = nfs_ok;
3174 update_stateid(&stp->st_stateid);
3175 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3176
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003177 /* release_stateid() calls nfsd_close() if needed */
J. Bruce Fields22839632009-01-11 14:27:17 -05003178 release_open_stateid(stp);
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003179
3180 /* place unused nfs4_stateowners on so_close_lru list to be
3181 * released by the laundromat service after the lease period
3182 * to enable us to handle CLOSE replay
3183 */
3184 if (list_empty(&close->cl_stateowner->so_stateids))
3185 move_to_close_lru(close->cl_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186out:
Neil Brownf2327d92005-09-13 01:25:37 -07003187 if (close->cl_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 nfs4_get_stateowner(close->cl_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003189 cstate->replay_owner = close->cl_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 nfs4_unlock_state();
3192 return status;
3193}
3194
Al Virob37ad282006-10-19 23:28:59 -07003195__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003196nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3197 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003199 struct nfs4_delegation *dp;
3200 stateid_t *stateid = &dr->dr_stateid;
3201 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003202 __be32 status;
Andy Adamson66689582009-04-03 08:28:45 +03003203 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003205 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003206 return status;
3207 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Andy Adamson66689582009-04-03 08:28:45 +03003209 if (nfsd4_has_session(cstate))
3210 flags |= HAS_SESSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 nfs4_lock_state();
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003212 status = nfserr_bad_stateid;
3213 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3214 goto out;
3215 status = nfserr_stale_stateid;
3216 if (STALE_STATEID(stateid))
3217 goto out;
J. Bruce Fields7e0f7cf2009-02-21 13:32:28 -08003218 status = nfserr_bad_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003219 if (!is_delegation_stateid(stateid))
3220 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003221 dp = find_delegation_stateid(inode, stateid);
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003222 if (!dp)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003223 goto out;
Andy Adamson66689582009-04-03 08:28:45 +03003224 status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003225 if (status)
3226 goto out;
3227 renew_client(dp->dl_client);
3228
3229 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003231 nfs4_unlock_state();
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return status;
3234}
3235
3236
3237/*
3238 * Lock owner state (byte-range locks)
3239 */
3240#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3241#define LOCK_HASH_BITS 8
3242#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3243#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3244
Benny Halevy87df4de2008-12-15 19:42:03 +02003245static inline u64
3246end_offset(u64 start, u64 len)
3247{
3248 u64 end;
3249
3250 end = start + len;
3251 return end >= start ? end: NFS4_MAX_UINT64;
3252}
3253
3254/* last octet in a range */
3255static inline u64
3256last_byte_offset(u64 start, u64 len)
3257{
3258 u64 end;
3259
3260 BUG_ON(!len);
3261 end = start + len;
3262 return end > start ? end - 1: NFS4_MAX_UINT64;
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265#define lockownerid_hashval(id) \
3266 ((id) & LOCK_HASH_MASK)
3267
3268static inline unsigned int
3269lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3270 struct xdr_netobj *ownername)
3271{
3272 return (file_hashval(inode) + cl_id
3273 + opaque_hashval(ownername->data, ownername->len))
3274 & LOCK_HASH_MASK;
3275}
3276
3277static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3278static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3279static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3280
NeilBrownfd39ca92005-06-23 22:04:03 -07003281static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282find_stateid(stateid_t *stid, int flags)
3283{
Krishna Kumar9346eff2008-10-20 11:44:28 +05303284 struct nfs4_stateid *local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 u32 st_id = stid->si_stateownerid;
3286 u32 f_id = stid->si_fileid;
3287 unsigned int hashval;
3288
3289 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
Krishna Kumar9346eff2008-10-20 11:44:28 +05303290 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 hashval = stateid_hashval(st_id, f_id);
3292 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3293 if ((local->st_stateid.si_stateownerid == st_id) &&
3294 (local->st_stateid.si_fileid == f_id))
3295 return local;
3296 }
3297 }
Krishna Kumar9346eff2008-10-20 11:44:28 +05303298
3299 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 hashval = stateid_hashval(st_id, f_id);
3301 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3302 if ((local->st_stateid.si_stateownerid == st_id) &&
3303 (local->st_stateid.si_fileid == f_id))
3304 return local;
3305 }
Neil Brown849823c2005-09-13 01:25:36 -07003306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return NULL;
3308}
3309
3310static struct nfs4_delegation *
3311find_delegation_stateid(struct inode *ino, stateid_t *stid)
3312{
NeilBrown13cd2182005-06-23 22:03:10 -07003313 struct nfs4_file *fp;
3314 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003316 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3317 STATEID_VAL(stid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003320 if (!fp)
3321 return NULL;
3322 dl = find_delegation_file(fp, stid);
3323 put_nfs4_file(fp);
3324 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
3327/*
3328 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3329 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3330 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3331 * locking, this prevents us from being completely protocol-compliant. The
3332 * real solution to this problem is to start using unsigned file offsets in
3333 * the VFS, but this is a very deep change!
3334 */
3335static inline void
3336nfs4_transform_lock_offset(struct file_lock *lock)
3337{
3338 if (lock->fl_start < 0)
3339 lock->fl_start = OFFSET_MAX;
3340 if (lock->fl_end < 0)
3341 lock->fl_end = OFFSET_MAX;
3342}
3343
NeilBrownd5b90262006-04-10 22:55:22 -07003344/* Hack!: For now, we're defining this just so we can use a pointer to it
3345 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003346static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003347};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349static inline void
3350nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3351{
NeilBrownd5b90262006-04-10 22:55:22 -07003352 struct nfs4_stateowner *sop;
3353 unsigned int hval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
NeilBrownd5b90262006-04-10 22:55:22 -07003355 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3356 sop = (struct nfs4_stateowner *) fl->fl_owner;
3357 hval = lockownerid_hashval(sop->so_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 kref_get(&sop->so_ref);
3359 deny->ld_sop = sop;
3360 deny->ld_clientid = sop->so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003361 } else {
3362 deny->ld_sop = NULL;
3363 deny->ld_clientid.cl_boot = 0;
3364 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
3366 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003367 deny->ld_length = NFS4_MAX_UINT64;
3368 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3370 deny->ld_type = NFS4_READ_LT;
3371 if (fl->fl_type != F_RDLCK)
3372 deny->ld_type = NFS4_WRITE_LT;
3373}
3374
3375static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3377 struct xdr_netobj *owner)
3378{
3379 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3380 struct nfs4_stateowner *op;
3381
3382 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003383 if (same_owner_str(op, owner, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return op;
3385 }
3386 return NULL;
3387}
3388
3389/*
3390 * Alloc a lock owner structure.
3391 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3392 * occured.
3393 *
3394 * strhashval = lock_ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 */
3396
3397static struct nfs4_stateowner *
3398alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3399 struct nfs4_stateowner *sop;
3400 struct nfs4_replay *rp;
3401 unsigned int idhashval;
3402
3403 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3404 return NULL;
3405 idhashval = lockownerid_hashval(current_ownerid);
3406 INIT_LIST_HEAD(&sop->so_idhash);
3407 INIT_LIST_HEAD(&sop->so_strhash);
3408 INIT_LIST_HEAD(&sop->so_perclient);
NeilBrownea1da632005-06-23 22:04:17 -07003409 INIT_LIST_HEAD(&sop->so_stateids);
3410 INIT_LIST_HEAD(&sop->so_perstateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3412 sop->so_time = 0;
3413 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3414 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
NeilBrownea1da632005-06-23 22:04:17 -07003415 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 sop->so_is_open_owner = 0;
3417 sop->so_id = current_ownerid++;
3418 sop->so_client = clp;
Neil Brownb59e3c02005-09-13 01:25:38 -07003419 /* It is the openowner seqid that will be incremented in encode in the
3420 * case of new lockowners; so increment the lock seqid manually: */
3421 sop->so_seqid = lock->lk_new_lock_seqid + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 sop->so_confirmed = 1;
3423 rp = &sop->so_replay;
Al Virode1ae282006-01-18 17:43:47 -08003424 rp->rp_status = nfserr_serverfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 rp->rp_buflen = 0;
3426 rp->rp_buf = rp->rp_ibuf;
3427 return sop;
3428}
3429
NeilBrownfd39ca92005-06-23 22:04:03 -07003430static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3432{
3433 struct nfs4_stateid *stp;
3434 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3435
NeilBrown5ac049a2005-06-23 22:03:03 -07003436 stp = nfs4_alloc_stateid();
3437 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 goto out;
3439 INIT_LIST_HEAD(&stp->st_hash);
3440 INIT_LIST_HEAD(&stp->st_perfile);
NeilBrownea1da632005-06-23 22:04:17 -07003441 INIT_LIST_HEAD(&stp->st_perstateowner);
3442 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07003444 list_add(&stp->st_perfile, &fp->fi_stateids);
NeilBrownea1da632005-06-23 22:04:17 -07003445 list_add(&stp->st_perstateowner, &sop->so_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07003447 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 stp->st_file = fp;
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003449 stp->st_stateid.si_boot = boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 stp->st_stateid.si_stateownerid = sop->so_id;
3451 stp->st_stateid.si_fileid = fp->fi_id;
3452 stp->st_stateid.si_generation = 0;
3453 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
3454 stp->st_access_bmap = open_stp->st_access_bmap;
3455 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003456 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458out:
3459 return stp;
3460}
3461
NeilBrownfd39ca92005-06-23 22:04:03 -07003462static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463check_lock_length(u64 offset, u64 length)
3464{
Benny Halevy87df4de2008-12-15 19:42:03 +02003465 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 LOFF_OVERFLOW(offset, length)));
3467}
3468
3469/*
3470 * LOCK operation
3471 */
Al Virob37ad282006-10-19 23:28:59 -07003472__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003473nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003474 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
NeilBrown3e9e3db2005-06-23 22:04:20 -07003476 struct nfs4_stateowner *open_sop = NULL;
Neil Brownb59e3c02005-09-13 01:25:38 -07003477 struct nfs4_stateowner *lock_sop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct nfs4_stateid *lock_stp;
3479 struct file *filp;
3480 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05003481 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07003482 __be32 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 unsigned int strhashval;
Marc Eshel150b3932007-01-18 16:15:35 -05003484 unsigned int cmd;
Al Virob8dd7b92006-10-19 23:29:01 -07003485 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3488 (long long) lock->lk_offset,
3489 (long long) lock->lk_length);
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 if (check_lock_length(lock->lk_offset, lock->lk_length))
3492 return nfserr_inval;
3493
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003494 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02003495 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08003496 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3497 return status;
3498 }
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 nfs4_lock_state();
3501
3502 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07003503 /*
3504 * Client indicates that this is a new lockowner.
3505 * Use open owner and open stateid to create lock owner and
3506 * lock stateid.
3507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 struct nfs4_stateid *open_stp = NULL;
3509 struct nfs4_file *fp;
3510
3511 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003512 if (!nfsd4_has_session(cstate) &&
3513 STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 /* validate and update open stateid and open seqid */
Benny Halevydd453df2009-04-03 08:28:41 +03003517 status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 lock->lk_new_open_seqid,
3519 &lock->lk_new_open_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003520 OPEN_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08003521 &lock->lk_replay_owner, &open_stp,
Neil Brownb59e3c02005-09-13 01:25:38 -07003522 lock);
NeilBrown37515172005-07-07 17:59:16 -07003523 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08003525 open_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 /* create lockowner and lock stateid */
3527 fp = open_stp->st_file;
3528 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3529 open_sop->so_client->cl_clientid.cl_id,
3530 &lock->v.new.owner);
NeilBrown3e9e3db2005-06-23 22:04:20 -07003531 /* XXX: Do we need to check for duplicate stateowners on
3532 * the same file, or should they just be allowed (and
3533 * create new stateids)? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 status = nfserr_resource;
Neil Brownb59e3c02005-09-13 01:25:38 -07003535 lock_sop = alloc_init_lock_stateowner(strhashval,
3536 open_sop->so_client, open_stp, lock);
3537 if (lock_sop == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 goto out;
Neil Brownb59e3c02005-09-13 01:25:38 -07003539 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
J. Bruce Fields8a280512006-01-18 17:43:18 -08003540 if (lock_stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 } else {
3543 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03003544 status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 lock->lk_old_lock_seqid,
3546 &lock->lk_old_lock_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003547 LOCK_STATE,
J. Bruce Fields3a655882006-01-18 17:43:19 -08003548 &lock->lk_replay_owner, &lock_stp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if (status)
3550 goto out;
J. Bruce Fields3a655882006-01-18 17:43:19 -08003551 lock_sop = lock->lk_replay_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
J. Bruce Fields3a655882006-01-18 17:43:19 -08003553 /* lock->lk_replay_owner and lock_stp have been created or found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 filp = lock_stp->st_vfs_file;
3555
NeilBrown0dd395d2005-07-07 17:59:15 -07003556 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003557 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003558 goto out;
3559 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003560 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07003561 goto out;
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 locks_init_lock(&file_lock);
3564 switch (lock->lk_type) {
3565 case NFS4_READ_LT:
3566 case NFS4_READW_LT:
3567 file_lock.fl_type = F_RDLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05003568 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 break;
3570 case NFS4_WRITE_LT:
3571 case NFS4_WRITEW_LT:
3572 file_lock.fl_type = F_WRLCK;
Marc Eshel150b3932007-01-18 16:15:35 -05003573 cmd = F_SETLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 break;
3575 default:
3576 status = nfserr_inval;
3577 goto out;
3578 }
Neil Brownb59e3c02005-09-13 01:25:38 -07003579 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 file_lock.fl_pid = current->tgid;
3581 file_lock.fl_file = filp;
3582 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003583 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003586 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 nfs4_transform_lock_offset(&file_lock);
3588
3589 /*
3590 * Try to lock the file in the VFS.
3591 * Note: locks.c uses the BKL to protect the inode's lock list.
3592 */
3593
Marc Eshelfd85b812006-11-28 16:26:41 -05003594 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07003595 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 case 0: /* success! */
3597 update_stateid(&lock_stp->st_stateid);
3598 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3599 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07003600 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003601 break;
3602 case (EAGAIN): /* conflock holds conflicting lock */
3603 status = nfserr_denied;
3604 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3605 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 case (EDEADLK):
3608 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05003611 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08003612 status = nfserr_resource;
3613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615out:
J. Bruce Fields8a280512006-01-18 17:43:18 -08003616 if (status && lock->lk_is_new && lock_sop)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003617 release_lockowner(lock_sop);
J. Bruce Fields3a655882006-01-18 17:43:19 -08003618 if (lock->lk_replay_owner) {
3619 nfs4_get_stateowner(lock->lk_replay_owner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003620 cstate->replay_owner = lock->lk_replay_owner;
Neil Brownf2327d92005-09-13 01:25:37 -07003621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 nfs4_unlock_state();
3623 return status;
3624}
3625
3626/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08003627 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3628 * so we do a temporary open here just to get an open file to pass to
3629 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3630 * inode operation.)
3631 */
3632static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3633{
3634 struct file *file;
3635 int err;
3636
3637 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3638 if (err)
3639 return err;
3640 err = vfs_test_lock(file, lock);
3641 nfsd_close(file);
3642 return err;
3643}
3644
3645/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 * LOCKT operation
3647 */
Al Virob37ad282006-10-19 23:28:59 -07003648__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003649nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3650 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct file_lock file_lock;
Marc Eshelfd85b812006-11-28 16:26:41 -05003654 int error;
Al Virob37ad282006-10-19 23:28:59 -07003655 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003657 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return nfserr_grace;
3659
3660 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3661 return nfserr_inval;
3662
3663 lockt->lt_stateowner = NULL;
3664 nfs4_lock_state();
3665
3666 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03003667 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003670 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
Neil Brown849823c2005-09-13 01:25:36 -07003671 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (status == nfserr_symlink)
3673 status = nfserr_inval;
3674 goto out;
3675 }
3676
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003677 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 locks_init_lock(&file_lock);
3679 switch (lockt->lt_type) {
3680 case NFS4_READ_LT:
3681 case NFS4_READW_LT:
3682 file_lock.fl_type = F_RDLCK;
3683 break;
3684 case NFS4_WRITE_LT:
3685 case NFS4_WRITEW_LT:
3686 file_lock.fl_type = F_WRLCK;
3687 break;
3688 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04003689 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 status = nfserr_inval;
3691 goto out;
3692 }
3693
3694 lockt->lt_stateowner = find_lockstateowner_str(inode,
3695 &lockt->lt_clientid, &lockt->lt_owner);
3696 if (lockt->lt_stateowner)
3697 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
3698 file_lock.fl_pid = current->tgid;
3699 file_lock.fl_flags = FL_POSIX;
3700
3701 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02003702 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 nfs4_transform_lock_offset(&file_lock);
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08003707 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05003708 if (error) {
3709 status = nfserrno(error);
3710 goto out;
3711 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05003712 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05003714 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
3716out:
3717 nfs4_unlock_state();
3718 return status;
3719}
3720
Al Virob37ad282006-10-19 23:28:59 -07003721__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003722nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003723 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 struct nfs4_stateid *stp;
3726 struct file *filp = NULL;
3727 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07003728 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07003729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
3731 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3732 (long long) locku->lu_offset,
3733 (long long) locku->lu_length);
3734
3735 if (check_lock_length(locku->lu_offset, locku->lu_length))
3736 return nfserr_inval;
3737
3738 nfs4_lock_state();
3739
Benny Halevydd453df2009-04-03 08:28:41 +03003740 if ((status = nfs4_preprocess_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 locku->lu_seqid,
3742 &locku->lu_stateid,
J. Bruce Fieldsf3362732008-01-26 14:58:45 -05003743 LOCK_STATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 &locku->lu_stateowner, &stp, NULL)))
3745 goto out;
3746
3747 filp = stp->st_vfs_file;
3748 BUG_ON(!filp);
3749 locks_init_lock(&file_lock);
3750 file_lock.fl_type = F_UNLCK;
3751 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3752 file_lock.fl_pid = current->tgid;
3753 file_lock.fl_file = filp;
3754 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07003755 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 file_lock.fl_start = locku->lu_offset;
3757
Benny Halevy87df4de2008-12-15 19:42:03 +02003758 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 nfs4_transform_lock_offset(&file_lock);
3760
3761 /*
3762 * Try to unlock the file in the VFS.
3763 */
Marc Eshelfd85b812006-11-28 16:26:41 -05003764 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07003765 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05003766 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 goto out_nfserr;
3768 }
3769 /*
3770 * OK, unlock succeeded; the only thing left to do is update the stateid.
3771 */
3772 update_stateid(&stp->st_stateid);
3773 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3774
3775out:
Neil Brownf2327d92005-09-13 01:25:37 -07003776 if (locku->lu_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 nfs4_get_stateowner(locku->lu_stateowner);
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003778 cstate->replay_owner = locku->lu_stateowner;
Neil Brownf2327d92005-09-13 01:25:37 -07003779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 nfs4_unlock_state();
3781 return status;
3782
3783out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003784 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 goto out;
3786}
3787
3788/*
3789 * returns
3790 * 1: locks held by lockowner
3791 * 0: no locks held by lockowner
3792 */
3793static int
3794check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3795{
3796 struct file_lock **flpp;
Josef "Jeff" Sipek7eaa36e2006-12-08 02:36:41 -08003797 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 int status = 0;
3799
3800 lock_kernel();
3801 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003802 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 status = 1;
3804 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08003805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
3807out:
3808 unlock_kernel();
3809 return status;
3810}
3811
Al Virob37ad282006-10-19 23:28:59 -07003812__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003813nfsd4_release_lockowner(struct svc_rqst *rqstp,
3814 struct nfsd4_compound_state *cstate,
3815 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003818 struct nfs4_stateowner *sop;
3819 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07003821 struct list_head matches;
3822 int i;
Al Virob37ad282006-10-19 23:28:59 -07003823 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3826 clid->cl_boot, clid->cl_id);
3827
3828 /* XXX check for lease expiration */
3829
3830 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07003831 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
3834 nfs4_lock_state();
3835
NeilBrown3e9e3db2005-06-23 22:04:20 -07003836 status = nfserr_locks_held;
3837 /* XXX: we're doing a linear search through all the lockowners.
3838 * Yipes! For now we'll just hope clients aren't really using
3839 * release_lockowner much, but eventually we have to fix these
3840 * data structures. */
3841 INIT_LIST_HEAD(&matches);
3842 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3843 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003844 if (!same_owner_str(sop, owner, clid))
NeilBrown3e9e3db2005-06-23 22:04:20 -07003845 continue;
3846 list_for_each_entry(stp, &sop->so_stateids,
3847 st_perstateowner) {
3848 if (check_for_locks(stp->st_vfs_file, sop))
3849 goto out;
3850 /* Note: so_perclient unused for lockowners,
3851 * so it's OK to fool with here. */
3852 list_add(&sop->so_perclient, &matches);
3853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07003855 }
3856 /* Clients probably won't expect us to return with some (but not all)
3857 * of the lockowner state released; so don't release any until all
3858 * have been checked. */
3859 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07003860 while (!list_empty(&matches)) {
3861 sop = list_entry(matches.next, struct nfs4_stateowner,
3862 so_perclient);
3863 /* unhash_stateowner deletes so_perclient only
3864 * for openowners. */
3865 list_del(&sop->so_perclient);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05003866 release_lockowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
3868out:
3869 nfs4_unlock_state();
3870 return status;
3871}
3872
3873static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003874alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
NeilBrowna55370a2005-06-23 22:03:52 -07003876 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
NeilBrownc7b9a452005-06-23 22:04:30 -07003879int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003880nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07003881{
3882 unsigned int strhashval = clientstr_hashval(name);
3883 struct nfs4_client *clp;
3884
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003885 clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
NeilBrownc7b9a452005-06-23 22:04:30 -07003886 return clp ? 1 : 0;
3887}
3888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889/*
3890 * failure => all reset bets are off, nfserr_no_grace...
3891 */
NeilBrown190e4fb2005-06-23 22:04:25 -07003892int
3893nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
3895 unsigned int strhashval;
3896 struct nfs4_client_reclaim *crp = NULL;
3897
NeilBrowna55370a2005-06-23 22:03:52 -07003898 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3899 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (!crp)
3901 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003902 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 INIT_LIST_HEAD(&crp->cr_strhash);
3904 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003905 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 reclaim_str_hashtbl_size++;
3907 return 1;
3908}
3909
3910static void
3911nfs4_release_reclaim(void)
3912{
3913 struct nfs4_client_reclaim *crp = NULL;
3914 int i;
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3917 while (!list_empty(&reclaim_str_hashtbl[i])) {
3918 crp = list_entry(reclaim_str_hashtbl[i].next,
3919 struct nfs4_client_reclaim, cr_strhash);
3920 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 kfree(crp);
3922 reclaim_str_hashtbl_size--;
3923 }
3924 }
3925 BUG_ON(reclaim_str_hashtbl_size);
3926}
3927
3928/*
3929 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003930static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931nfs4_find_reclaim_client(clientid_t *clid)
3932{
3933 unsigned int strhashval;
3934 struct nfs4_client *clp;
3935 struct nfs4_client_reclaim *crp = NULL;
3936
3937
3938 /* find clientid in conf_id_hashtbl */
3939 clp = find_confirmed_client(clid);
3940 if (clp == NULL)
3941 return NULL;
3942
NeilBrowna55370a2005-06-23 22:03:52 -07003943 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3944 clp->cl_name.len, clp->cl_name.data,
3945 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003948 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003950 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return crp;
3952 }
3953 }
3954 return NULL;
3955}
3956
3957/*
3958* Called from OPEN. Look for clientid in reclaim list.
3959*/
Al Virob37ad282006-10-19 23:28:59 -07003960__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961nfs4_check_open_reclaim(clientid_t *clid)
3962{
NeilBrowndfc83562005-06-23 22:03:16 -07003963 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
NeilBrownac4d8ff2005-06-23 22:03:30 -07003966/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003968int
NeilBrownac4d8ff2005-06-23 22:03:30 -07003969nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003971 int i, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003973 status = nfsd4_init_slabs();
3974 if (status)
3975 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3977 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3978 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3979 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3980 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08003981 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
Marc Eshel5282fd72009-04-03 08:27:52 +03003983 for (i = 0; i < SESSION_HASH_SIZE; i++)
3984 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 for (i = 0; i < FILE_HASH_SIZE; i++) {
3986 INIT_LIST_HEAD(&file_hashtbl[i]);
3987 }
3988 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3989 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3990 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3991 }
3992 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3993 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3994 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3995 }
3996 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3997 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3998 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 INIT_LIST_HEAD(&close_lru);
4002 INIT_LIST_HEAD(&client_lru);
4003 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff2005-06-23 22:03:30 -07004004 reclaim_str_hashtbl_size = 0;
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004005 return 0;
NeilBrownac4d8ff2005-06-23 22:03:30 -07004006}
4007
NeilBrown190e4fb2005-06-23 22:04:25 -07004008static void
4009nfsd4_load_reboot_recovery_data(void)
4010{
4011 int status;
4012
NeilBrown0964a3d2005-06-23 22:04:32 -07004013 nfs4_lock_state();
4014 nfsd4_init_recdir(user_recovery_dirname);
NeilBrown190e4fb2005-06-23 22:04:25 -07004015 status = nfsd4_recdir_load();
NeilBrown0964a3d2005-06-23 22:04:32 -07004016 nfs4_unlock_state();
NeilBrown190e4fb2005-06-23 22:04:25 -07004017 if (status)
4018 printk("NFSD: Failure reading reboot recovery data\n");
4019}
4020
Meelap Shahc2f1a552007-07-17 04:04:39 -07004021/*
4022 * Since the lifetime of a delegation isn't limited to that of an open, a
4023 * client may quite reasonably hang on to a delegation as long as it has
4024 * the inode cached. This becomes an obvious problem the first time a
4025 * client's inode cache approaches the size of the server's total memory.
4026 *
4027 * For now we avoid this problem by imposing a hard limit on the number
4028 * of delegations, which varies according to the server's memory size.
4029 */
4030static void
4031set_max_delegations(void)
4032{
4033 /*
4034 * Allow at most 4 delegations per megabyte of RAM. Quick
4035 * estimates suggest that in the worst case (where every delegation
4036 * is for a different inode), a delegation could take about 1.5K,
4037 * giving a worst case usage of about 6% of memory.
4038 */
4039 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4040}
4041
NeilBrownac4d8ff2005-06-23 22:03:30 -07004042/* initialization to perform when the nfsd service is started: */
4043
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004044static int
NeilBrownac4d8ff2005-06-23 22:03:30 -07004045__nfs4_state_start(void)
4046{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004047 int ret;
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004050 locks_start_grace(&nfsd4_manager);
Marc Eshel9a8db972007-07-17 04:04:35 -07004051 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004052 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004053 ret = set_callback_cred();
4054 if (ret)
4055 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004056 laundry_wq = create_singlethread_workqueue("nfsd4");
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004057 if (laundry_wq == NULL)
4058 return -ENOMEM;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004059 ret = nfsd4_create_callback_queue();
4060 if (ret)
4061 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004062 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004063 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004064 return 0;
4065out_free_laundry:
4066 destroy_workqueue(laundry_wq);
4067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
J. Bruce Fields29ab23c2009-09-15 15:56:50 -04004070int
NeilBrown76a35502005-06-23 22:03:26 -07004071nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
NeilBrown190e4fb2005-06-23 22:04:25 -07004073 nfsd4_load_reboot_recovery_data();
Jeff Layton4ad9a342010-07-19 16:50:04 -04004074 return __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077static void
4078__nfs4_state_shutdown(void)
4079{
4080 int i;
4081 struct nfs4_client *clp = NULL;
4082 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct list_head *pos, *next, reaplist;
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4086 while (!list_empty(&conf_id_hashtbl[i])) {
4087 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4088 expire_client(clp);
4089 }
4090 while (!list_empty(&unconf_str_hashtbl[i])) {
4091 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4092 expire_client(clp);
4093 }
4094 }
4095 INIT_LIST_HEAD(&reaplist);
4096 spin_lock(&recall_lock);
4097 list_for_each_safe(pos, next, &del_recall_lru) {
4098 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4099 list_move(&dp->dl_recall_lru, &reaplist);
4100 }
4101 spin_unlock(&recall_lock);
4102 list_for_each_safe(pos, next, &reaplist) {
4103 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4104 list_del_init(&dp->dl_recall_lru);
4105 unhash_delegation(dp);
4106 }
4107
NeilBrown190e4fb2005-06-23 22:04:25 -07004108 nfsd4_shutdown_recdir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
4111void
4112nfs4_state_shutdown(void)
4113{
NeilBrown5e8d5c22006-04-10 22:55:37 -07004114 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
4115 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004116 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 nfs4_lock_state();
4118 nfs4_release_reclaim();
4119 __nfs4_state_shutdown();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004120 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 nfs4_unlock_state();
4122}
4123
Jeff Layton3dd98a32008-06-10 08:40:36 -04004124/*
4125 * user_recovery_dirname is protected by the nfsd_mutex since it's only
4126 * accessed when nfsd is starting.
4127 */
NeilBrown0964a3d2005-06-23 22:04:32 -07004128static void
4129nfs4_set_recdir(char *recdir)
4130{
NeilBrown0964a3d2005-06-23 22:04:32 -07004131 strcpy(user_recovery_dirname, recdir);
NeilBrown0964a3d2005-06-23 22:04:32 -07004132}
4133
4134/*
4135 * Change the NFSv4 recovery directory to recdir.
4136 */
4137int
4138nfs4_reset_recoverydir(char *recdir)
4139{
4140 int status;
Al Viroa63bb992008-08-02 01:03:36 -04004141 struct path path;
NeilBrown0964a3d2005-06-23 22:04:32 -07004142
Al Viroa63bb992008-08-02 01:03:36 -04004143 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
NeilBrown0964a3d2005-06-23 22:04:32 -07004144 if (status)
4145 return status;
4146 status = -ENOTDIR;
Al Viroa63bb992008-08-02 01:03:36 -04004147 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
NeilBrown0964a3d2005-06-23 22:04:32 -07004148 nfs4_set_recdir(recdir);
4149 status = 0;
4150 }
Al Viroa63bb992008-08-02 01:03:36 -04004151 path_put(&path);
NeilBrown0964a3d2005-06-23 22:04:32 -07004152 return status;
4153}
4154
Jeff Layton3dd98a32008-06-10 08:40:36 -04004155char *
4156nfs4_recoverydir(void)
4157{
4158 return user_recovery_dirname;
4159}