]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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 | ||
aceaf78d | 35 | #include <linux/file.h> |
b89f4321 | 36 | #include <linux/fs.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
0964a3d3 | 38 | #include <linux/namei.h> |
c2f1a551 | 39 | #include <linux/swap.h> |
68e76ad0 | 40 | #include <linux/sunrpc/svcauth_gss.h> |
363168b4 | 41 | #include <linux/sunrpc/clnt.h> |
9a74af21 | 42 | #include "xdr4.h" |
0a3adade | 43 | #include "vfs.h" |
1da177e4 LT |
44 | |
45 | #define NFSDDBG_FACILITY NFSDDBG_PROC | |
46 | ||
47 | /* Globals */ | |
cf07d2ea | 48 | time_t nfsd4_lease = 90; /* default lease time */ |
efc4bb4f | 49 | time_t nfsd4_grace = 90; |
fd39ca9a | 50 | static time_t boot_time; |
1da177e4 LT |
51 | static u32 current_ownerid = 1; |
52 | static u32 current_fileid = 1; | |
53 | static u32 current_delegid = 1; | |
fd39ca9a N |
54 | static stateid_t zerostateid; /* bits all 0 */ |
55 | static stateid_t onestateid; /* bits all 1 */ | |
ec6b5d7b | 56 | static u64 current_sessionid = 1; |
fd39ca9a N |
57 | |
58 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t))) | |
59 | #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t))) | |
1da177e4 | 60 | |
1da177e4 | 61 | /* forward declarations */ |
fd39ca9a | 62 | static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags); |
1da177e4 | 63 | static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid); |
0964a3d3 N |
64 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
65 | static void nfs4_set_recdir(char *recdir); | |
1da177e4 | 66 | |
8b671b80 BF |
67 | /* Locking: */ |
68 | ||
69 | /* Currently used for almost all code touching nfsv4 state: */ | |
353ab6e9 | 70 | static DEFINE_MUTEX(client_mutex); |
1da177e4 | 71 | |
8b671b80 BF |
72 | /* |
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 | */ | |
77 | static DEFINE_SPINLOCK(recall_lock); | |
78 | ||
e18b890b CL |
79 | static struct kmem_cache *stateowner_slab = NULL; |
80 | static struct kmem_cache *file_slab = NULL; | |
81 | static struct kmem_cache *stateid_slab = NULL; | |
82 | static struct kmem_cache *deleg_slab = NULL; | |
e60d4398 | 83 | |
1da177e4 LT |
84 | void |
85 | nfs4_lock_state(void) | |
86 | { | |
353ab6e9 | 87 | mutex_lock(&client_mutex); |
1da177e4 LT |
88 | } |
89 | ||
90 | void | |
91 | nfs4_unlock_state(void) | |
92 | { | |
353ab6e9 | 93 | mutex_unlock(&client_mutex); |
1da177e4 LT |
94 | } |
95 | ||
96 | static inline u32 | |
97 | opaque_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 | ||
1da177e4 LT |
109 | static struct list_head del_recall_lru; |
110 | ||
13cd2184 N |
111 | static inline void |
112 | put_nfs4_file(struct nfs4_file *fi) | |
113 | { | |
8b671b80 BF |
114 | 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 | } | |
13cd2184 N |
120 | } |
121 | ||
122 | static inline void | |
123 | get_nfs4_file(struct nfs4_file *fi) | |
124 | { | |
8b671b80 | 125 | atomic_inc(&fi->fi_ref); |
13cd2184 N |
126 | } |
127 | ||
ef0f3390 | 128 | static int num_delegations; |
c2f1a551 | 129 | unsigned int max_delegations; |
ef0f3390 N |
130 | |
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 | ||
145 | static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE]; | |
146 | static 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) | |
35079582 | 151 | |
ef0f3390 N |
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 | ||
162 | static struct list_head file_hashtbl[FILE_HASH_SIZE]; | |
163 | static struct list_head stateid_hashtbl[STATEID_HASH_SIZE]; | |
164 | ||
998db52c | 165 | static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
f9d7562f BF |
166 | { |
167 | BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR])); | |
168 | atomic_inc(&fp->fi_access[oflag]); | |
169 | } | |
170 | ||
998db52c BF |
171 | static void nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
172 | { | |
173 | if (oflag == O_RDWR) { | |
174 | __nfs4_file_get_access(fp, O_RDONLY); | |
175 | __nfs4_file_get_access(fp, O_WRONLY); | |
176 | } else | |
177 | __nfs4_file_get_access(fp, oflag); | |
178 | } | |
179 | ||
180 | static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag) | |
f9d7562f BF |
181 | { |
182 | if (fp->fi_fds[oflag]) { | |
183 | fput(fp->fi_fds[oflag]); | |
184 | fp->fi_fds[oflag] = NULL; | |
185 | } | |
186 | } | |
187 | ||
998db52c | 188 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
f9d7562f BF |
189 | { |
190 | if (atomic_dec_and_test(&fp->fi_access[oflag])) { | |
191 | nfs4_file_put_fd(fp, O_RDWR); | |
192 | nfs4_file_put_fd(fp, oflag); | |
193 | } | |
194 | } | |
195 | ||
998db52c BF |
196 | static void nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
197 | { | |
198 | if (oflag == O_RDWR) { | |
199 | __nfs4_file_put_access(fp, O_RDONLY); | |
200 | __nfs4_file_put_access(fp, O_WRONLY); | |
201 | } else | |
202 | __nfs4_file_put_access(fp, oflag); | |
203 | } | |
204 | ||
1da177e4 LT |
205 | static struct nfs4_delegation * |
206 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type) | |
207 | { | |
208 | struct nfs4_delegation *dp; | |
209 | struct nfs4_file *fp = stp->st_file; | |
1da177e4 LT |
210 | |
211 | dprintk("NFSD alloc_init_deleg\n"); | |
c3e48080 BF |
212 | /* |
213 | * Major work on the lease subsystem (for example, to support | |
214 | * calbacks on stat) will be required before we can support | |
215 | * write delegations properly. | |
216 | */ | |
217 | if (type != NFS4_OPEN_DELEGATE_READ) | |
218 | return NULL; | |
47f9940c MS |
219 | if (fp->fi_had_conflict) |
220 | return NULL; | |
c2f1a551 | 221 | if (num_delegations > max_delegations) |
ef0f3390 | 222 | return NULL; |
5b2d21c1 N |
223 | dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL); |
224 | if (dp == NULL) | |
1da177e4 | 225 | return dp; |
ef0f3390 | 226 | num_delegations++; |
ea1da636 N |
227 | INIT_LIST_HEAD(&dp->dl_perfile); |
228 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 LT |
229 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
230 | dp->dl_client = clp; | |
13cd2184 | 231 | get_nfs4_file(fp); |
1da177e4 | 232 | dp->dl_file = fp; |
1da177e4 | 233 | dp->dl_type = type; |
e4e83ea4 | 234 | dp->dl_stateid.si_boot = boot_time; |
1da177e4 LT |
235 | dp->dl_stateid.si_stateownerid = current_delegid++; |
236 | dp->dl_stateid.si_fileid = 0; | |
237 | dp->dl_stateid.si_generation = 0; | |
6c02eaa1 | 238 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
1da177e4 LT |
239 | dp->dl_time = 0; |
240 | atomic_set(&dp->dl_count, 1); | |
b5a1a81e | 241 | INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc); |
1da177e4 LT |
242 | return dp; |
243 | } | |
244 | ||
245 | void | |
246 | nfs4_put_delegation(struct nfs4_delegation *dp) | |
247 | { | |
248 | if (atomic_dec_and_test(&dp->dl_count)) { | |
249 | dprintk("NFSD: freeing dp %p\n",dp); | |
13cd2184 | 250 | put_nfs4_file(dp->dl_file); |
5b2d21c1 | 251 | kmem_cache_free(deleg_slab, dp); |
ef0f3390 | 252 | num_delegations--; |
1da177e4 LT |
253 | } |
254 | } | |
255 | ||
acfdf5c3 | 256 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
1da177e4 | 257 | { |
acfdf5c3 BF |
258 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
259 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); | |
260 | fp->fi_lease = NULL; | |
261 | fp->fi_deleg_file = NULL; | |
262 | } | |
1da177e4 LT |
263 | } |
264 | ||
265 | /* Called under the state lock. */ | |
266 | static void | |
267 | unhash_delegation(struct nfs4_delegation *dp) | |
268 | { | |
ea1da636 | 269 | list_del_init(&dp->dl_perclnt); |
1da177e4 | 270 | spin_lock(&recall_lock); |
5d926e8c | 271 | list_del_init(&dp->dl_perfile); |
1da177e4 LT |
272 | list_del_init(&dp->dl_recall_lru); |
273 | spin_unlock(&recall_lock); | |
acfdf5c3 | 274 | nfs4_put_deleg_lease(dp->dl_file); |
1da177e4 LT |
275 | nfs4_put_delegation(dp); |
276 | } | |
277 | ||
278 | /* | |
279 | * SETCLIENTID state | |
280 | */ | |
281 | ||
36acb66b | 282 | /* client_lock protects the client lru list and session hash table */ |
9089f1b4 BH |
283 | static DEFINE_SPINLOCK(client_lock); |
284 | ||
1da177e4 LT |
285 | /* Hash tables for nfs4_clientid state */ |
286 | #define CLIENT_HASH_BITS 4 | |
287 | #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS) | |
288 | #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1) | |
289 | ||
290 | #define clientid_hashval(id) \ | |
291 | ((id) & CLIENT_HASH_MASK) | |
a55370a3 N |
292 | #define clientstr_hashval(name) \ |
293 | (opaque_hashval((name), 8) & CLIENT_HASH_MASK) | |
1da177e4 LT |
294 | /* |
295 | * reclaim_str_hashtbl[] holds known client info from previous reset/reboot | |
296 | * used in reboot/reset lease grace period processing | |
297 | * | |
298 | * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed | |
299 | * setclientid_confirmed info. | |
300 | * | |
301 | * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed | |
302 | * setclientid info. | |
303 | * | |
304 | * client_lru holds client queue ordered by nfs4_client.cl_time | |
305 | * for lease renewal. | |
306 | * | |
307 | * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time | |
308 | * for last close replay. | |
309 | */ | |
310 | static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE]; | |
311 | static int reclaim_str_hashtbl_size = 0; | |
312 | static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE]; | |
313 | static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE]; | |
314 | static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE]; | |
315 | static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE]; | |
316 | static struct list_head client_lru; | |
317 | static struct list_head close_lru; | |
318 | ||
f9d7562f BF |
319 | /* |
320 | * We store the NONE, READ, WRITE, and BOTH bits separately in the | |
321 | * st_{access,deny}_bmap field of the stateid, in order to track not | |
322 | * only what share bits are currently in force, but also what | |
323 | * combinations of share bits previous opens have used. This allows us | |
324 | * to enforce the recommendation of rfc 3530 14.2.19 that the server | |
325 | * return an error if the client attempt to downgrade to a combination | |
326 | * of share bits not explicable by closing some of its previous opens. | |
327 | * | |
328 | * XXX: This enforcement is actually incomplete, since we don't keep | |
329 | * track of access/deny bit combinations; so, e.g., we allow: | |
330 | * | |
331 | * OPEN allow read, deny write | |
332 | * OPEN allow both, deny none | |
333 | * DOWNGRADE allow read, deny none | |
334 | * | |
335 | * which we should reject. | |
336 | */ | |
337 | static void | |
338 | set_access(unsigned int *access, unsigned long bmap) { | |
339 | int i; | |
340 | ||
341 | *access = 0; | |
342 | for (i = 1; i < 4; i++) { | |
343 | if (test_bit(i, &bmap)) | |
344 | *access |= i; | |
345 | } | |
346 | } | |
347 | ||
348 | static void | |
349 | set_deny(unsigned int *deny, unsigned long bmap) { | |
350 | int i; | |
351 | ||
352 | *deny = 0; | |
353 | for (i = 0; i < 4; i++) { | |
354 | if (test_bit(i, &bmap)) | |
355 | *deny |= i ; | |
356 | } | |
357 | } | |
358 | ||
359 | static int | |
360 | test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) { | |
361 | unsigned int access, deny; | |
362 | ||
363 | set_access(&access, stp->st_access_bmap); | |
364 | set_deny(&deny, stp->st_deny_bmap); | |
365 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) | |
366 | return 0; | |
367 | return 1; | |
368 | } | |
369 | ||
370 | static int nfs4_access_to_omode(u32 access) | |
371 | { | |
8f34a430 | 372 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
f9d7562f BF |
373 | case NFS4_SHARE_ACCESS_READ: |
374 | return O_RDONLY; | |
375 | case NFS4_SHARE_ACCESS_WRITE: | |
376 | return O_WRONLY; | |
377 | case NFS4_SHARE_ACCESS_BOTH: | |
378 | return O_RDWR; | |
379 | } | |
380 | BUG(); | |
381 | } | |
382 | ||
383 | static int nfs4_access_bmap_to_omode(struct nfs4_stateid *stp) | |
384 | { | |
385 | unsigned int access; | |
386 | ||
387 | set_access(&access, stp->st_access_bmap); | |
388 | return nfs4_access_to_omode(access); | |
389 | } | |
390 | ||
529d7b2a BF |
391 | static void unhash_generic_stateid(struct nfs4_stateid *stp) |
392 | { | |
393 | list_del(&stp->st_hash); | |
394 | list_del(&stp->st_perfile); | |
395 | list_del(&stp->st_perstateowner); | |
396 | } | |
397 | ||
398 | static void free_generic_stateid(struct nfs4_stateid *stp) | |
399 | { | |
0997b173 BF |
400 | int oflag = nfs4_access_bmap_to_omode(stp); |
401 | ||
402 | nfs4_file_put_access(stp->st_file, oflag); | |
529d7b2a BF |
403 | put_nfs4_file(stp->st_file); |
404 | kmem_cache_free(stateid_slab, stp); | |
405 | } | |
406 | ||
407 | static void release_lock_stateid(struct nfs4_stateid *stp) | |
408 | { | |
409 | struct file *file; | |
410 | ||
411 | unhash_generic_stateid(stp); | |
412 | file = find_any_file(stp->st_file); | |
413 | if (file) | |
414 | locks_remove_posix(file, (fl_owner_t)stp->st_stateowner); | |
415 | free_generic_stateid(stp); | |
416 | } | |
417 | ||
418 | static void unhash_lockowner(struct nfs4_stateowner *sop) | |
419 | { | |
420 | struct nfs4_stateid *stp; | |
421 | ||
422 | list_del(&sop->so_idhash); | |
423 | list_del(&sop->so_strhash); | |
424 | list_del(&sop->so_perstateid); | |
425 | while (!list_empty(&sop->so_stateids)) { | |
426 | stp = list_first_entry(&sop->so_stateids, | |
427 | struct nfs4_stateid, st_perstateowner); | |
428 | release_lock_stateid(stp); | |
429 | } | |
430 | } | |
431 | ||
432 | static void release_lockowner(struct nfs4_stateowner *sop) | |
433 | { | |
434 | unhash_lockowner(sop); | |
435 | nfs4_put_stateowner(sop); | |
436 | } | |
437 | ||
438 | static void | |
439 | release_stateid_lockowners(struct nfs4_stateid *open_stp) | |
440 | { | |
441 | struct nfs4_stateowner *lock_sop; | |
442 | ||
443 | while (!list_empty(&open_stp->st_lockowners)) { | |
444 | lock_sop = list_entry(open_stp->st_lockowners.next, | |
445 | struct nfs4_stateowner, so_perstateid); | |
446 | /* list_del(&open_stp->st_lockowners); */ | |
447 | BUG_ON(lock_sop->so_is_open_owner); | |
448 | release_lockowner(lock_sop); | |
449 | } | |
450 | } | |
451 | ||
2283963f BF |
452 | static void release_open_stateid(struct nfs4_stateid *stp) |
453 | { | |
454 | unhash_generic_stateid(stp); | |
455 | release_stateid_lockowners(stp); | |
2283963f BF |
456 | free_generic_stateid(stp); |
457 | } | |
458 | ||
f044ff83 | 459 | static void unhash_openowner(struct nfs4_stateowner *sop) |
f1d110ca BF |
460 | { |
461 | struct nfs4_stateid *stp; | |
462 | ||
463 | list_del(&sop->so_idhash); | |
464 | list_del(&sop->so_strhash); | |
f044ff83 BF |
465 | list_del(&sop->so_perclient); |
466 | list_del(&sop->so_perstateid); /* XXX: necessary? */ | |
f1d110ca | 467 | while (!list_empty(&sop->so_stateids)) { |
f044ff83 BF |
468 | stp = list_first_entry(&sop->so_stateids, |
469 | struct nfs4_stateid, st_perstateowner); | |
470 | release_open_stateid(stp); | |
f1d110ca BF |
471 | } |
472 | } | |
473 | ||
f044ff83 | 474 | static void release_openowner(struct nfs4_stateowner *sop) |
f1d110ca | 475 | { |
f044ff83 | 476 | unhash_openowner(sop); |
f1d110ca BF |
477 | list_del(&sop->so_close_lru); |
478 | nfs4_put_stateowner(sop); | |
479 | } | |
480 | ||
5282fd72 ME |
481 | #define SESSION_HASH_SIZE 512 |
482 | static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE]; | |
483 | ||
484 | static inline int | |
485 | hash_sessionid(struct nfs4_sessionid *sessionid) | |
486 | { | |
487 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; | |
488 | ||
489 | return sid->sequence % SESSION_HASH_SIZE; | |
490 | } | |
491 | ||
492 | static inline void | |
493 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
494 | { | |
495 | u32 *ptr = (u32 *)(&sessionid->data[0]); | |
496 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); | |
497 | } | |
498 | ||
ec6b5d7b AA |
499 | static void |
500 | gen_sessionid(struct nfsd4_session *ses) | |
501 | { | |
502 | struct nfs4_client *clp = ses->se_client; | |
503 | struct nfsd4_sessionid *sid; | |
504 | ||
505 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; | |
506 | sid->clientid = clp->cl_clientid; | |
507 | sid->sequence = current_sessionid++; | |
508 | sid->reserved = 0; | |
509 | } | |
510 | ||
511 | /* | |
a649637c AA |
512 | * The protocol defines ca_maxresponssize_cached to include the size of |
513 | * the rpc header, but all we need to cache is the data starting after | |
514 | * the end of the initial SEQUENCE operation--the rest we regenerate | |
515 | * each time. Therefore we can advertise a ca_maxresponssize_cached | |
516 | * value that is the number of bytes in our cache plus a few additional | |
517 | * bytes. In order to stay on the safe side, and not promise more than | |
518 | * we can cache, those additional bytes must be the minimum possible: 24 | |
519 | * bytes of rpc header (xid through accept state, with AUTH_NULL | |
520 | * verifier), 12 for the compound header (with zero-length tag), and 44 | |
521 | * for the SEQUENCE op response: | |
522 | */ | |
523 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) | |
524 | ||
557ce264 AA |
525 | static void |
526 | free_session_slots(struct nfsd4_session *ses) | |
527 | { | |
528 | int i; | |
529 | ||
530 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) | |
531 | kfree(ses->se_slots[i]); | |
532 | } | |
533 | ||
a649637c | 534 | /* |
efe0cb6d BF |
535 | * We don't actually need to cache the rpc and session headers, so we |
536 | * can allocate a little less for each slot: | |
537 | */ | |
538 | static inline int slot_bytes(struct nfsd4_channel_attrs *ca) | |
539 | { | |
540 | return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; | |
541 | } | |
542 | ||
5b6feee9 | 543 | static int nfsd4_sanitize_slot_size(u32 size) |
ec6b5d7b | 544 | { |
5b6feee9 BF |
545 | size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */ |
546 | size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE); | |
ec6b5d7b | 547 | |
5b6feee9 BF |
548 | return size; |
549 | } | |
ec6b5d7b | 550 | |
5b6feee9 BF |
551 | /* |
552 | * XXX: If we run out of reserved DRC memory we could (up to a point) | |
a649637c AA |
553 | * re-negotiate active sessions and reduce their slot usage to make |
554 | * rooom for new connections. For now we just fail the create session. | |
ec6b5d7b | 555 | */ |
5b6feee9 | 556 | static int nfsd4_get_drc_mem(int slotsize, u32 num) |
ec6b5d7b | 557 | { |
5b6feee9 | 558 | int avail; |
ec6b5d7b | 559 | |
5b6feee9 | 560 | num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION); |
5d77ddfb | 561 | |
5b6feee9 BF |
562 | spin_lock(&nfsd_drc_lock); |
563 | avail = min_t(int, NFSD_MAX_MEM_PER_SESSION, | |
564 | nfsd_drc_max_mem - nfsd_drc_mem_used); | |
565 | num = min_t(int, num, avail / slotsize); | |
566 | nfsd_drc_mem_used += num * slotsize; | |
567 | spin_unlock(&nfsd_drc_lock); | |
ec6b5d7b | 568 | |
5b6feee9 BF |
569 | return num; |
570 | } | |
ec6b5d7b | 571 | |
5b6feee9 BF |
572 | static void nfsd4_put_drc_mem(int slotsize, int num) |
573 | { | |
4bd9b0f4 | 574 | spin_lock(&nfsd_drc_lock); |
5b6feee9 | 575 | nfsd_drc_mem_used -= slotsize * num; |
4bd9b0f4 | 576 | spin_unlock(&nfsd_drc_lock); |
5b6feee9 | 577 | } |
ec6b5d7b | 578 | |
5b6feee9 BF |
579 | static struct nfsd4_session *alloc_session(int slotsize, int numslots) |
580 | { | |
581 | struct nfsd4_session *new; | |
582 | int mem, i; | |
a649637c | 583 | |
5b6feee9 BF |
584 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
585 | + sizeof(struct nfsd4_session) > PAGE_SIZE); | |
586 | mem = numslots * sizeof(struct nfsd4_slot *); | |
ec6b5d7b | 587 | |
5b6feee9 BF |
588 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
589 | if (!new) | |
590 | return NULL; | |
557ce264 | 591 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
5b6feee9 BF |
592 | for (i = 0; i < numslots; i++) { |
593 | mem = sizeof(struct nfsd4_slot) + slotsize; | |
594 | new->se_slots[i] = kzalloc(mem, GFP_KERNEL); | |
595 | if (!new->se_slots[i]) | |
557ce264 | 596 | goto out_free; |
557ce264 | 597 | } |
5b6feee9 BF |
598 | return new; |
599 | out_free: | |
600 | while (i--) | |
601 | kfree(new->se_slots[i]); | |
602 | kfree(new); | |
603 | return NULL; | |
ec6b5d7b AA |
604 | } |
605 | ||
5b6feee9 | 606 | static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize) |
ec6b5d7b | 607 | { |
5b6feee9 | 608 | u32 maxrpc = nfsd_serv->sv_max_mesg; |
ec6b5d7b | 609 | |
5b6feee9 | 610 | new->maxreqs = numslots; |
d2b21743 MJ |
611 | new->maxresp_cached = min_t(u32, req->maxresp_cached, |
612 | slotsize + NFSD_MIN_HDR_SEQ_SZ); | |
5b6feee9 BF |
613 | new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc); |
614 | new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc); | |
615 | new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND); | |
616 | } | |
ec6b5d7b | 617 | |
19cf5c02 BF |
618 | static void free_conn(struct nfsd4_conn *c) |
619 | { | |
620 | svc_xprt_put(c->cn_xprt); | |
621 | kfree(c); | |
622 | } | |
ec6b5d7b | 623 | |
19cf5c02 BF |
624 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
625 | { | |
626 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); | |
627 | struct nfs4_client *clp = c->cn_session->se_client; | |
ec6b5d7b | 628 | |
19cf5c02 BF |
629 | spin_lock(&clp->cl_lock); |
630 | if (!list_empty(&c->cn_persession)) { | |
631 | list_del(&c->cn_persession); | |
632 | free_conn(c); | |
633 | } | |
634 | spin_unlock(&clp->cl_lock); | |
eea49806 | 635 | nfsd4_probe_callback(clp); |
19cf5c02 | 636 | } |
ec6b5d7b | 637 | |
d29c374c | 638 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
c7662518 | 639 | { |
c7662518 | 640 | struct nfsd4_conn *conn; |
ec6b5d7b | 641 | |
c7662518 BF |
642 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
643 | if (!conn) | |
db90681d | 644 | return NULL; |
c7662518 BF |
645 | svc_xprt_get(rqstp->rq_xprt); |
646 | conn->cn_xprt = rqstp->rq_xprt; | |
d29c374c | 647 | conn->cn_flags = flags; |
db90681d BF |
648 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
649 | return conn; | |
650 | } | |
a649637c | 651 | |
328ead28 BF |
652 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
653 | { | |
654 | conn->cn_session = ses; | |
655 | list_add(&conn->cn_persession, &ses->se_conns); | |
ec6b5d7b AA |
656 | } |
657 | ||
db90681d | 658 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
557ce264 | 659 | { |
db90681d | 660 | struct nfs4_client *clp = ses->se_client; |
557ce264 | 661 | |
c7662518 | 662 | spin_lock(&clp->cl_lock); |
328ead28 | 663 | __nfsd4_hash_conn(conn, ses); |
c7662518 | 664 | spin_unlock(&clp->cl_lock); |
557ce264 AA |
665 | } |
666 | ||
21b75b01 | 667 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
efe0cb6d | 668 | { |
19cf5c02 | 669 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
21b75b01 | 670 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
efe0cb6d BF |
671 | } |
672 | ||
1d1bc8f2 | 673 | static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir) |
ec6b5d7b | 674 | { |
db90681d | 675 | struct nfsd4_conn *conn; |
21b75b01 | 676 | int ret; |
ec6b5d7b | 677 | |
1d1bc8f2 | 678 | conn = alloc_conn(rqstp, dir); |
db90681d BF |
679 | if (!conn) |
680 | return nfserr_jukebox; | |
681 | nfsd4_hash_conn(conn, ses); | |
21b75b01 BF |
682 | ret = nfsd4_register_conn(conn); |
683 | if (ret) | |
684 | /* oops; xprt is already down: */ | |
685 | nfsd4_conn_lost(&conn->cn_xpt_user); | |
c7662518 BF |
686 | return nfs_ok; |
687 | } | |
ec6b5d7b | 688 | |
1d1bc8f2 BF |
689 | static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses) |
690 | { | |
691 | u32 dir = NFS4_CDFC4_FORE; | |
692 | ||
693 | if (ses->se_flags & SESSION4_BACK_CHAN) | |
694 | dir |= NFS4_CDFC4_BACK; | |
695 | ||
696 | return nfsd4_new_conn(rqstp, ses, dir); | |
697 | } | |
698 | ||
699 | /* must be called under client_lock */ | |
19cf5c02 | 700 | static void nfsd4_del_conns(struct nfsd4_session *s) |
c7662518 | 701 | { |
19cf5c02 BF |
702 | struct nfs4_client *clp = s->se_client; |
703 | struct nfsd4_conn *c; | |
ec6b5d7b | 704 | |
19cf5c02 BF |
705 | spin_lock(&clp->cl_lock); |
706 | while (!list_empty(&s->se_conns)) { | |
707 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); | |
708 | list_del_init(&c->cn_persession); | |
709 | spin_unlock(&clp->cl_lock); | |
557ce264 | 710 | |
19cf5c02 BF |
711 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
712 | free_conn(c); | |
ec6b5d7b | 713 | |
19cf5c02 BF |
714 | spin_lock(&clp->cl_lock); |
715 | } | |
716 | spin_unlock(&clp->cl_lock); | |
c7662518 | 717 | } |
ec6b5d7b | 718 | |
c7662518 BF |
719 | void free_session(struct kref *kref) |
720 | { | |
721 | struct nfsd4_session *ses; | |
722 | int mem; | |
723 | ||
724 | ses = container_of(kref, struct nfsd4_session, se_ref); | |
19cf5c02 | 725 | nfsd4_del_conns(ses); |
c7662518 BF |
726 | spin_lock(&nfsd_drc_lock); |
727 | mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel); | |
728 | nfsd_drc_mem_used -= mem; | |
729 | spin_unlock(&nfsd_drc_lock); | |
730 | free_session_slots(ses); | |
731 | kfree(ses); | |
732 | } | |
733 | ||
ac7c46f2 | 734 | static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
5b6feee9 BF |
735 | { |
736 | struct nfsd4_session *new; | |
737 | struct nfsd4_channel_attrs *fchan = &cses->fore_channel; | |
738 | int numslots, slotsize; | |
c7662518 | 739 | int status; |
5b6feee9 BF |
740 | int idx; |
741 | ||
742 | /* | |
743 | * Note decreasing slot size below client's request may | |
744 | * make it difficult for client to function correctly, whereas | |
745 | * decreasing the number of slots will (just?) affect | |
746 | * performance. When short on memory we therefore prefer to | |
747 | * decrease number of slots instead of their size. | |
748 | */ | |
749 | slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached); | |
750 | numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs); | |
ced6dfe9 MJ |
751 | if (numslots < 1) |
752 | return NULL; | |
5b6feee9 BF |
753 | |
754 | new = alloc_session(slotsize, numslots); | |
755 | if (!new) { | |
756 | nfsd4_put_drc_mem(slotsize, fchan->maxreqs); | |
ac7c46f2 | 757 | return NULL; |
557ce264 | 758 | } |
5b6feee9 | 759 | init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize); |
557ce264 | 760 | |
ec6b5d7b AA |
761 | new->se_client = clp; |
762 | gen_sessionid(new); | |
ec6b5d7b | 763 | |
c7662518 BF |
764 | INIT_LIST_HEAD(&new->se_conns); |
765 | ||
ac7c46f2 | 766 | new->se_cb_seq_nr = 1; |
ec6b5d7b | 767 | new->se_flags = cses->flags; |
8b5ce5cd | 768 | new->se_cb_prog = cses->callback_prog; |
ec6b5d7b | 769 | kref_init(&new->se_ref); |
5b6feee9 | 770 | idx = hash_sessionid(&new->se_sessionid); |
9089f1b4 | 771 | spin_lock(&client_lock); |
ec6b5d7b | 772 | list_add(&new->se_hash, &sessionid_hashtbl[idx]); |
4c649378 | 773 | spin_lock(&clp->cl_lock); |
ec6b5d7b | 774 | list_add(&new->se_perclnt, &clp->cl_sessions); |
4c649378 | 775 | spin_unlock(&clp->cl_lock); |
9089f1b4 | 776 | spin_unlock(&client_lock); |
ec6b5d7b | 777 | |
1d1bc8f2 | 778 | status = nfsd4_new_conn_from_crses(rqstp, new); |
ac7c46f2 | 779 | /* whoops: benny points out, status is ignored! (err, or bogus) */ |
c7662518 BF |
780 | if (status) { |
781 | free_session(&new->se_ref); | |
ac7c46f2 | 782 | return NULL; |
c7662518 | 783 | } |
dcbeaa68 | 784 | if (cses->flags & SESSION4_BACK_CHAN) { |
edd76786 | 785 | struct sockaddr *sa = svc_addr(rqstp); |
dcbeaa68 BF |
786 | /* |
787 | * This is a little silly; with sessions there's no real | |
788 | * use for the callback address. Use the peer address | |
789 | * as a reasonable default for now, but consider fixing | |
790 | * the rpc client not to require an address in the | |
791 | * future: | |
792 | */ | |
edd76786 BF |
793 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
794 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); | |
edd76786 | 795 | } |
dcbeaa68 | 796 | nfsd4_probe_callback(clp); |
ac7c46f2 | 797 | return new; |
ec6b5d7b AA |
798 | } |
799 | ||
9089f1b4 | 800 | /* caller must hold client_lock */ |
5282fd72 ME |
801 | static struct nfsd4_session * |
802 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid) | |
803 | { | |
804 | struct nfsd4_session *elem; | |
805 | int idx; | |
806 | ||
807 | dump_sessionid(__func__, sessionid); | |
808 | idx = hash_sessionid(sessionid); | |
5282fd72 ME |
809 | /* Search in the appropriate list */ |
810 | list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) { | |
5282fd72 ME |
811 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
812 | NFS4_MAX_SESSIONID_LEN)) { | |
813 | return elem; | |
814 | } | |
815 | } | |
816 | ||
817 | dprintk("%s: session not found\n", __func__); | |
818 | return NULL; | |
819 | } | |
820 | ||
9089f1b4 | 821 | /* caller must hold client_lock */ |
7116ed6b | 822 | static void |
5282fd72 | 823 | unhash_session(struct nfsd4_session *ses) |
7116ed6b AA |
824 | { |
825 | list_del(&ses->se_hash); | |
4c649378 | 826 | spin_lock(&ses->se_client->cl_lock); |
7116ed6b | 827 | list_del(&ses->se_perclnt); |
4c649378 | 828 | spin_unlock(&ses->se_client->cl_lock); |
5282fd72 ME |
829 | } |
830 | ||
36acb66b | 831 | /* must be called under the client_lock */ |
1da177e4 | 832 | static inline void |
36acb66b | 833 | renew_client_locked(struct nfs4_client *clp) |
1da177e4 | 834 | { |
07cd4909 BH |
835 | if (is_client_expired(clp)) { |
836 | dprintk("%s: client (clientid %08x/%08x) already expired\n", | |
837 | __func__, | |
838 | clp->cl_clientid.cl_boot, | |
839 | clp->cl_clientid.cl_id); | |
840 | return; | |
841 | } | |
842 | ||
1da177e4 LT |
843 | /* |
844 | * Move client to the end to the LRU list. | |
845 | */ | |
846 | dprintk("renewing client (clientid %08x/%08x)\n", | |
847 | clp->cl_clientid.cl_boot, | |
848 | clp->cl_clientid.cl_id); | |
849 | list_move_tail(&clp->cl_lru, &client_lru); | |
850 | clp->cl_time = get_seconds(); | |
851 | } | |
852 | ||
36acb66b BH |
853 | static inline void |
854 | renew_client(struct nfs4_client *clp) | |
855 | { | |
856 | spin_lock(&client_lock); | |
857 | renew_client_locked(clp); | |
858 | spin_unlock(&client_lock); | |
859 | } | |
860 | ||
1da177e4 LT |
861 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
862 | static int | |
863 | STALE_CLIENTID(clientid_t *clid) | |
864 | { | |
865 | if (clid->cl_boot == boot_time) | |
866 | return 0; | |
60adfc50 AA |
867 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
868 | clid->cl_boot, clid->cl_id, boot_time); | |
1da177e4 LT |
869 | return 1; |
870 | } | |
871 | ||
872 | /* | |
873 | * XXX Should we use a slab cache ? | |
874 | * This type of memory management is somewhat inefficient, but we use it | |
875 | * anyway since SETCLIENTID is not a common operation. | |
876 | */ | |
35bba9a3 | 877 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
1da177e4 LT |
878 | { |
879 | struct nfs4_client *clp; | |
880 | ||
35bba9a3 BF |
881 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
882 | if (clp == NULL) | |
883 | return NULL; | |
884 | clp->cl_name.data = kmalloc(name.len, GFP_KERNEL); | |
885 | if (clp->cl_name.data == NULL) { | |
886 | kfree(clp); | |
887 | return NULL; | |
1da177e4 | 888 | } |
35bba9a3 BF |
889 | memcpy(clp->cl_name.data, name.data, name.len); |
890 | clp->cl_name.len = name.len; | |
1da177e4 LT |
891 | return clp; |
892 | } | |
893 | ||
894 | static inline void | |
895 | free_client(struct nfs4_client *clp) | |
896 | { | |
792c95dd BF |
897 | while (!list_empty(&clp->cl_sessions)) { |
898 | struct nfsd4_session *ses; | |
899 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, | |
900 | se_perclnt); | |
901 | list_del(&ses->se_perclnt); | |
902 | nfsd4_put_session(ses); | |
903 | } | |
1da177e4 LT |
904 | if (clp->cl_cred.cr_group_info) |
905 | put_group_info(clp->cl_cred.cr_group_info); | |
68e76ad0 | 906 | kfree(clp->cl_principal); |
1da177e4 LT |
907 | kfree(clp->cl_name.data); |
908 | kfree(clp); | |
909 | } | |
910 | ||
d7682988 BH |
911 | void |
912 | release_session_client(struct nfsd4_session *session) | |
913 | { | |
914 | struct nfs4_client *clp = session->se_client; | |
915 | ||
916 | if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock)) | |
917 | return; | |
918 | if (is_client_expired(clp)) { | |
919 | free_client(clp); | |
920 | session->se_client = NULL; | |
921 | } else | |
922 | renew_client_locked(clp); | |
923 | spin_unlock(&client_lock); | |
d7682988 BH |
924 | } |
925 | ||
84d38ac9 BH |
926 | /* must be called under the client_lock */ |
927 | static inline void | |
928 | unhash_client_locked(struct nfs4_client *clp) | |
929 | { | |
792c95dd BF |
930 | struct nfsd4_session *ses; |
931 | ||
07cd4909 | 932 | mark_client_expired(clp); |
84d38ac9 | 933 | list_del(&clp->cl_lru); |
4c649378 | 934 | spin_lock(&clp->cl_lock); |
792c95dd BF |
935 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
936 | list_del_init(&ses->se_hash); | |
4c649378 | 937 | spin_unlock(&clp->cl_lock); |
84d38ac9 BH |
938 | } |
939 | ||
1da177e4 LT |
940 | static void |
941 | expire_client(struct nfs4_client *clp) | |
942 | { | |
943 | struct nfs4_stateowner *sop; | |
944 | struct nfs4_delegation *dp; | |
1da177e4 LT |
945 | struct list_head reaplist; |
946 | ||
1da177e4 LT |
947 | INIT_LIST_HEAD(&reaplist); |
948 | spin_lock(&recall_lock); | |
ea1da636 N |
949 | while (!list_empty(&clp->cl_delegations)) { |
950 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
ea1da636 | 951 | list_del_init(&dp->dl_perclnt); |
1da177e4 LT |
952 | list_move(&dp->dl_recall_lru, &reaplist); |
953 | } | |
954 | spin_unlock(&recall_lock); | |
955 | while (!list_empty(&reaplist)) { | |
956 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
957 | list_del_init(&dp->dl_recall_lru); | |
958 | unhash_delegation(dp); | |
959 | } | |
ea1da636 N |
960 | while (!list_empty(&clp->cl_openowners)) { |
961 | sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient); | |
f044ff83 | 962 | release_openowner(sop); |
1da177e4 | 963 | } |
6ff8da08 | 964 | nfsd4_shutdown_callback(clp); |
84d38ac9 BH |
965 | if (clp->cl_cb_conn.cb_xprt) |
966 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); | |
36acb66b BH |
967 | list_del(&clp->cl_idhash); |
968 | list_del(&clp->cl_strhash); | |
be1fdf6c | 969 | spin_lock(&client_lock); |
84d38ac9 | 970 | unhash_client_locked(clp); |
46583e25 BH |
971 | if (atomic_read(&clp->cl_refcount) == 0) |
972 | free_client(clp); | |
be1fdf6c | 973 | spin_unlock(&client_lock); |
1da177e4 LT |
974 | } |
975 | ||
35bba9a3 BF |
976 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
977 | { | |
978 | memcpy(target->cl_verifier.data, source->data, | |
979 | sizeof(target->cl_verifier.data)); | |
1da177e4 LT |
980 | } |
981 | ||
35bba9a3 BF |
982 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
983 | { | |
1da177e4 LT |
984 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
985 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
986 | } | |
987 | ||
35bba9a3 BF |
988 | static void copy_cred(struct svc_cred *target, struct svc_cred *source) |
989 | { | |
1da177e4 LT |
990 | target->cr_uid = source->cr_uid; |
991 | target->cr_gid = source->cr_gid; | |
992 | target->cr_group_info = source->cr_group_info; | |
993 | get_group_info(target->cr_group_info); | |
994 | } | |
995 | ||
35bba9a3 | 996 | static int same_name(const char *n1, const char *n2) |
599e0a22 | 997 | { |
a55370a3 | 998 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
1da177e4 LT |
999 | } |
1000 | ||
1001 | static int | |
599e0a22 BF |
1002 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
1003 | { | |
1004 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); | |
1da177e4 LT |
1005 | } |
1006 | ||
1007 | static int | |
599e0a22 BF |
1008 | same_clid(clientid_t *cl1, clientid_t *cl2) |
1009 | { | |
1010 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); | |
1da177e4 LT |
1011 | } |
1012 | ||
1013 | /* XXX what about NGROUP */ | |
1014 | static int | |
599e0a22 BF |
1015 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
1016 | { | |
1017 | return cr1->cr_uid == cr2->cr_uid; | |
1da177e4 LT |
1018 | } |
1019 | ||
5ec7b46c BF |
1020 | static void gen_clid(struct nfs4_client *clp) |
1021 | { | |
1022 | static u32 current_clientid = 1; | |
1023 | ||
1da177e4 LT |
1024 | clp->cl_clientid.cl_boot = boot_time; |
1025 | clp->cl_clientid.cl_id = current_clientid++; | |
1026 | } | |
1027 | ||
deda2faa BF |
1028 | static void gen_confirm(struct nfs4_client *clp) |
1029 | { | |
1030 | static u32 i; | |
1031 | u32 *p; | |
1da177e4 | 1032 | |
1da177e4 | 1033 | p = (u32 *)clp->cl_confirm.data; |
deda2faa BF |
1034 | *p++ = get_seconds(); |
1035 | *p++ = i++; | |
1da177e4 LT |
1036 | } |
1037 | ||
b09333c4 RL |
1038 | static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir, |
1039 | struct svc_rqst *rqstp, nfs4_verifier *verf) | |
1040 | { | |
1041 | struct nfs4_client *clp; | |
1042 | struct sockaddr *sa = svc_addr(rqstp); | |
1043 | char *princ; | |
1044 | ||
1045 | clp = alloc_client(name); | |
1046 | if (clp == NULL) | |
1047 | return NULL; | |
1048 | ||
792c95dd BF |
1049 | INIT_LIST_HEAD(&clp->cl_sessions); |
1050 | ||
b09333c4 RL |
1051 | princ = svc_gss_principal(rqstp); |
1052 | if (princ) { | |
1053 | clp->cl_principal = kstrdup(princ, GFP_KERNEL); | |
1054 | if (clp->cl_principal == NULL) { | |
1055 | free_client(clp); | |
1056 | return NULL; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | memcpy(clp->cl_recdir, recdir, HEXDIR_LEN); | |
46583e25 | 1061 | atomic_set(&clp->cl_refcount, 0); |
77a3569d | 1062 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
b09333c4 RL |
1063 | INIT_LIST_HEAD(&clp->cl_idhash); |
1064 | INIT_LIST_HEAD(&clp->cl_strhash); | |
1065 | INIT_LIST_HEAD(&clp->cl_openowners); | |
1066 | INIT_LIST_HEAD(&clp->cl_delegations); | |
b09333c4 | 1067 | INIT_LIST_HEAD(&clp->cl_lru); |
5ce8ba25 | 1068 | INIT_LIST_HEAD(&clp->cl_callbacks); |
6ff8da08 | 1069 | spin_lock_init(&clp->cl_lock); |
cee277d9 | 1070 | INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc); |
07cd4909 | 1071 | clp->cl_time = get_seconds(); |
b09333c4 RL |
1072 | clear_bit(0, &clp->cl_cb_slot_busy); |
1073 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); | |
1074 | copy_verf(clp, verf); | |
1075 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); | |
1076 | clp->cl_flavor = rqstp->rq_flavor; | |
1077 | copy_cred(&clp->cl_cred, &rqstp->rq_cred); | |
1078 | gen_confirm(clp); | |
edd76786 | 1079 | clp->cl_cb_session = NULL; |
b09333c4 RL |
1080 | return clp; |
1081 | } | |
1082 | ||
35bba9a3 BF |
1083 | static int check_name(struct xdr_netobj name) |
1084 | { | |
1da177e4 LT |
1085 | if (name.len == 0) |
1086 | return 0; | |
1087 | if (name.len > NFS4_OPAQUE_LIMIT) { | |
2fdada03 | 1088 | dprintk("NFSD: check_name: name too long(%d)!\n", name.len); |
1da177e4 LT |
1089 | return 0; |
1090 | } | |
1091 | return 1; | |
1092 | } | |
1093 | ||
fd39ca9a | 1094 | static void |
1da177e4 LT |
1095 | add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval) |
1096 | { | |
1097 | unsigned int idhashval; | |
1098 | ||
1099 | list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]); | |
1100 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
1101 | list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]); | |
36acb66b | 1102 | renew_client(clp); |
1da177e4 LT |
1103 | } |
1104 | ||
fd39ca9a | 1105 | static void |
1da177e4 LT |
1106 | move_to_confirmed(struct nfs4_client *clp) |
1107 | { | |
1108 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
1109 | unsigned int strhashval; | |
1110 | ||
1111 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); | |
f116629d | 1112 | list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]); |
a55370a3 | 1113 | strhashval = clientstr_hashval(clp->cl_recdir); |
328efbab | 1114 | list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]); |
1da177e4 LT |
1115 | renew_client(clp); |
1116 | } | |
1117 | ||
1118 | static struct nfs4_client * | |
1119 | find_confirmed_client(clientid_t *clid) | |
1120 | { | |
1121 | struct nfs4_client *clp; | |
1122 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
1123 | ||
1124 | list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) { | |
599e0a22 | 1125 | if (same_clid(&clp->cl_clientid, clid)) |
1da177e4 LT |
1126 | return clp; |
1127 | } | |
1128 | return NULL; | |
1129 | } | |
1130 | ||
1131 | static struct nfs4_client * | |
1132 | find_unconfirmed_client(clientid_t *clid) | |
1133 | { | |
1134 | struct nfs4_client *clp; | |
1135 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
1136 | ||
1137 | list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) { | |
599e0a22 | 1138 | if (same_clid(&clp->cl_clientid, clid)) |
1da177e4 LT |
1139 | return clp; |
1140 | } | |
1141 | return NULL; | |
1142 | } | |
1143 | ||
6e5f15c9 | 1144 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
a1bcecd2 | 1145 | { |
6e5f15c9 | 1146 | return clp->cl_exchange_flags != 0; |
e203d506 | 1147 | } |
a1bcecd2 | 1148 | |
28ce6054 | 1149 | static struct nfs4_client * |
e203d506 | 1150 | find_confirmed_client_by_str(const char *dname, unsigned int hashval) |
28ce6054 N |
1151 | { |
1152 | struct nfs4_client *clp; | |
1153 | ||
1154 | list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) { | |
e203d506 | 1155 | if (same_name(clp->cl_recdir, dname)) |
28ce6054 N |
1156 | return clp; |
1157 | } | |
1158 | return NULL; | |
1159 | } | |
1160 | ||
1161 | static struct nfs4_client * | |
e203d506 | 1162 | find_unconfirmed_client_by_str(const char *dname, unsigned int hashval) |
28ce6054 N |
1163 | { |
1164 | struct nfs4_client *clp; | |
1165 | ||
1166 | list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) { | |
e203d506 | 1167 | if (same_name(clp->cl_recdir, dname)) |
28ce6054 N |
1168 | return clp; |
1169 | } | |
1170 | return NULL; | |
1171 | } | |
1172 | ||
6f3d772f TU |
1173 | static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr) |
1174 | { | |
1175 | switch (family) { | |
1176 | case AF_INET: | |
1177 | ((struct sockaddr_in *)sa)->sin_family = AF_INET; | |
1178 | ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr; | |
1179 | return; | |
1180 | case AF_INET6: | |
1181 | ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6; | |
1182 | ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6; | |
1183 | return; | |
1184 | } | |
1185 | } | |
1186 | ||
fd39ca9a | 1187 | static void |
6f3d772f | 1188 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
1da177e4 | 1189 | { |
07263f1e | 1190 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
6f3d772f TU |
1191 | struct sockaddr *sa = svc_addr(rqstp); |
1192 | u32 scopeid = rpc_get_scope_id(sa); | |
7077ecba JL |
1193 | unsigned short expected_family; |
1194 | ||
1195 | /* Currently, we only support tcp and tcp6 for the callback channel */ | |
1196 | if (se->se_callback_netid_len == 3 && | |
1197 | !memcmp(se->se_callback_netid_val, "tcp", 3)) | |
1198 | expected_family = AF_INET; | |
1199 | else if (se->se_callback_netid_len == 4 && | |
1200 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) | |
1201 | expected_family = AF_INET6; | |
1202 | else | |
1da177e4 LT |
1203 | goto out_err; |
1204 | ||
07263f1e | 1205 | conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val, |
aa9a4ec7 | 1206 | se->se_callback_addr_len, |
07263f1e BF |
1207 | (struct sockaddr *)&conn->cb_addr, |
1208 | sizeof(conn->cb_addr)); | |
aa9a4ec7 | 1209 | |
07263f1e | 1210 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
1da177e4 | 1211 | goto out_err; |
aa9a4ec7 | 1212 | |
07263f1e BF |
1213 | if (conn->cb_addr.ss_family == AF_INET6) |
1214 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; | |
fbf4665f | 1215 | |
07263f1e BF |
1216 | conn->cb_prog = se->se_callback_prog; |
1217 | conn->cb_ident = se->se_callback_ident; | |
6f3d772f | 1218 | rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr); |
1da177e4 LT |
1219 | return; |
1220 | out_err: | |
07263f1e BF |
1221 | conn->cb_addr.ss_family = AF_UNSPEC; |
1222 | conn->cb_addrlen = 0; | |
849823c5 | 1223 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
1da177e4 LT |
1224 | "will not receive delegations\n", |
1225 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); | |
1226 | ||
1da177e4 LT |
1227 | return; |
1228 | } | |
1229 | ||
074fe897 | 1230 | /* |
557ce264 | 1231 | * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size. |
074fe897 | 1232 | */ |
074fe897 AA |
1233 | void |
1234 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) | |
074fe897 | 1235 | { |
557ce264 AA |
1236 | struct nfsd4_slot *slot = resp->cstate.slot; |
1237 | unsigned int base; | |
074fe897 | 1238 | |
557ce264 | 1239 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 1240 | |
557ce264 AA |
1241 | slot->sl_opcnt = resp->opcnt; |
1242 | slot->sl_status = resp->cstate.status; | |
074fe897 | 1243 | |
bf864a31 | 1244 | if (nfsd4_not_cached(resp)) { |
557ce264 | 1245 | slot->sl_datalen = 0; |
bf864a31 | 1246 | return; |
074fe897 | 1247 | } |
557ce264 AA |
1248 | slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap; |
1249 | base = (char *)resp->cstate.datap - | |
1250 | (char *)resp->xbuf->head[0].iov_base; | |
1251 | if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data, | |
1252 | slot->sl_datalen)) | |
1253 | WARN("%s: sessions DRC could not cache compound\n", __func__); | |
1254 | return; | |
074fe897 AA |
1255 | } |
1256 | ||
1257 | /* | |
abfabf8c AA |
1258 | * Encode the replay sequence operation from the slot values. |
1259 | * If cachethis is FALSE encode the uncached rep error on the next | |
1260 | * operation which sets resp->p and increments resp->opcnt for | |
1261 | * nfs4svc_encode_compoundres. | |
074fe897 | 1262 | * |
074fe897 | 1263 | */ |
abfabf8c AA |
1264 | static __be32 |
1265 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, | |
1266 | struct nfsd4_compoundres *resp) | |
074fe897 | 1267 | { |
abfabf8c AA |
1268 | struct nfsd4_op *op; |
1269 | struct nfsd4_slot *slot = resp->cstate.slot; | |
bf864a31 | 1270 | |
abfabf8c | 1271 | dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__, |
557ce264 | 1272 | resp->opcnt, resp->cstate.slot->sl_cachethis); |
bf864a31 | 1273 | |
abfabf8c AA |
1274 | /* Encode the replayed sequence operation */ |
1275 | op = &args->ops[resp->opcnt - 1]; | |
1276 | nfsd4_encode_operation(resp, op); | |
bf864a31 | 1277 | |
abfabf8c | 1278 | /* Return nfserr_retry_uncached_rep in next operation. */ |
557ce264 | 1279 | if (args->opcnt > 1 && slot->sl_cachethis == 0) { |
abfabf8c AA |
1280 | op = &args->ops[resp->opcnt++]; |
1281 | op->status = nfserr_retry_uncached_rep; | |
1282 | nfsd4_encode_operation(resp, op); | |
074fe897 | 1283 | } |
abfabf8c | 1284 | return op->status; |
074fe897 AA |
1285 | } |
1286 | ||
1287 | /* | |
557ce264 AA |
1288 | * The sequence operation is not cached because we can use the slot and |
1289 | * session values. | |
074fe897 AA |
1290 | */ |
1291 | __be32 | |
bf864a31 AA |
1292 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
1293 | struct nfsd4_sequence *seq) | |
074fe897 | 1294 | { |
557ce264 | 1295 | struct nfsd4_slot *slot = resp->cstate.slot; |
074fe897 AA |
1296 | __be32 status; |
1297 | ||
557ce264 | 1298 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 1299 | |
abfabf8c AA |
1300 | /* Either returns 0 or nfserr_retry_uncached */ |
1301 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); | |
1302 | if (status == nfserr_retry_uncached_rep) | |
1303 | return status; | |
074fe897 | 1304 | |
557ce264 AA |
1305 | /* The sequence operation has been encoded, cstate->datap set. */ |
1306 | memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen); | |
074fe897 | 1307 | |
557ce264 AA |
1308 | resp->opcnt = slot->sl_opcnt; |
1309 | resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen); | |
1310 | status = slot->sl_status; | |
074fe897 AA |
1311 | |
1312 | return status; | |
1313 | } | |
1314 | ||
0733d213 AA |
1315 | /* |
1316 | * Set the exchange_id flags returned by the server. | |
1317 | */ | |
1318 | static void | |
1319 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) | |
1320 | { | |
1321 | /* pNFS is not supported */ | |
1322 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; | |
1323 | ||
1324 | /* Referrals are supported, Migration is not. */ | |
1325 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; | |
1326 | ||
1327 | /* set the wire flags to return to client. */ | |
1328 | clid->flags = new->cl_exchange_flags; | |
1329 | } | |
1330 | ||
069b6ad4 AA |
1331 | __be32 |
1332 | nfsd4_exchange_id(struct svc_rqst *rqstp, | |
1333 | struct nfsd4_compound_state *cstate, | |
1334 | struct nfsd4_exchange_id *exid) | |
1335 | { | |
0733d213 AA |
1336 | struct nfs4_client *unconf, *conf, *new; |
1337 | int status; | |
1338 | unsigned int strhashval; | |
1339 | char dname[HEXDIR_LEN]; | |
363168b4 | 1340 | char addr_str[INET6_ADDRSTRLEN]; |
0733d213 | 1341 | nfs4_verifier verf = exid->verifier; |
363168b4 | 1342 | struct sockaddr *sa = svc_addr(rqstp); |
0733d213 | 1343 | |
363168b4 | 1344 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
0733d213 | 1345 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
363168b4 | 1346 | "ip_addr=%s flags %x, spa_how %d\n", |
0733d213 | 1347 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
363168b4 | 1348 | addr_str, exid->flags, exid->spa_how); |
0733d213 AA |
1349 | |
1350 | if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A)) | |
1351 | return nfserr_inval; | |
1352 | ||
1353 | /* Currently only support SP4_NONE */ | |
1354 | switch (exid->spa_how) { | |
1355 | case SP4_NONE: | |
1356 | break; | |
1357 | case SP4_SSV: | |
044bc1d4 | 1358 | return nfserr_serverfault; |
0733d213 AA |
1359 | default: |
1360 | BUG(); /* checked by xdr code */ | |
1361 | case SP4_MACH_CRED: | |
1362 | return nfserr_serverfault; /* no excuse :-/ */ | |
1363 | } | |
1364 | ||
1365 | status = nfs4_make_rec_clidname(dname, &exid->clname); | |
1366 | ||
1367 | if (status) | |
1368 | goto error; | |
1369 | ||
1370 | strhashval = clientstr_hashval(dname); | |
1371 | ||
1372 | nfs4_lock_state(); | |
1373 | status = nfs_ok; | |
1374 | ||
e203d506 | 1375 | conf = find_confirmed_client_by_str(dname, strhashval); |
0733d213 | 1376 | if (conf) { |
e203d506 BF |
1377 | if (!clp_used_exchangeid(conf)) { |
1378 | status = nfserr_clid_inuse; /* XXX: ? */ | |
1379 | goto out; | |
1380 | } | |
0733d213 AA |
1381 | if (!same_verf(&verf, &conf->cl_verifier)) { |
1382 | /* 18.35.4 case 8 */ | |
1383 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { | |
1384 | status = nfserr_not_same; | |
1385 | goto out; | |
1386 | } | |
1387 | /* Client reboot: destroy old state */ | |
1388 | expire_client(conf); | |
1389 | goto out_new; | |
1390 | } | |
1391 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { | |
1392 | /* 18.35.4 case 9 */ | |
1393 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { | |
1394 | status = nfserr_perm; | |
1395 | goto out; | |
1396 | } | |
1397 | expire_client(conf); | |
1398 | goto out_new; | |
1399 | } | |
0733d213 AA |
1400 | /* |
1401 | * Set bit when the owner id and verifier map to an already | |
1402 | * confirmed client id (18.35.3). | |
1403 | */ | |
1404 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
1405 | ||
1406 | /* | |
1407 | * Falling into 18.35.4 case 2, possible router replay. | |
1408 | * Leave confirmed record intact and return same result. | |
1409 | */ | |
1410 | copy_verf(conf, &verf); | |
1411 | new = conf; | |
1412 | goto out_copy; | |
6ddbbbfe MS |
1413 | } |
1414 | ||
1415 | /* 18.35.4 case 7 */ | |
1416 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { | |
1417 | status = nfserr_noent; | |
1418 | goto out; | |
0733d213 AA |
1419 | } |
1420 | ||
e203d506 | 1421 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
0733d213 AA |
1422 | if (unconf) { |
1423 | /* | |
1424 | * Possible retry or client restart. Per 18.35.4 case 4, | |
1425 | * a new unconfirmed record should be generated regardless | |
1426 | * of whether any properties have changed. | |
1427 | */ | |
1428 | expire_client(unconf); | |
1429 | } | |
1430 | ||
1431 | out_new: | |
1432 | /* Normal case */ | |
b09333c4 | 1433 | new = create_client(exid->clname, dname, rqstp, &verf); |
0733d213 | 1434 | if (new == NULL) { |
4731030d | 1435 | status = nfserr_jukebox; |
0733d213 AA |
1436 | goto out; |
1437 | } | |
1438 | ||
0733d213 | 1439 | gen_clid(new); |
0733d213 AA |
1440 | add_to_unconfirmed(new, strhashval); |
1441 | out_copy: | |
1442 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; | |
1443 | exid->clientid.cl_id = new->cl_clientid.cl_id; | |
1444 | ||
38eb76a5 | 1445 | exid->seqid = 1; |
0733d213 AA |
1446 | nfsd4_set_ex_flags(new, exid); |
1447 | ||
1448 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", | |
49557cc7 | 1449 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
0733d213 AA |
1450 | status = nfs_ok; |
1451 | ||
1452 | out: | |
1453 | nfs4_unlock_state(); | |
1454 | error: | |
1455 | dprintk("nfsd4_exchange_id returns %d\n", ntohl(status)); | |
1456 | return status; | |
069b6ad4 AA |
1457 | } |
1458 | ||
b85d4c01 | 1459 | static int |
88e588d5 | 1460 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
b85d4c01 | 1461 | { |
88e588d5 AA |
1462 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
1463 | slot_seqid); | |
b85d4c01 BH |
1464 | |
1465 | /* The slot is in use, and no response has been sent. */ | |
88e588d5 AA |
1466 | if (slot_inuse) { |
1467 | if (seqid == slot_seqid) | |
b85d4c01 BH |
1468 | return nfserr_jukebox; |
1469 | else | |
1470 | return nfserr_seq_misordered; | |
1471 | } | |
1472 | /* Normal */ | |
88e588d5 | 1473 | if (likely(seqid == slot_seqid + 1)) |
b85d4c01 BH |
1474 | return nfs_ok; |
1475 | /* Replay */ | |
88e588d5 | 1476 | if (seqid == slot_seqid) |
b85d4c01 BH |
1477 | return nfserr_replay_cache; |
1478 | /* Wraparound */ | |
88e588d5 | 1479 | if (seqid == 1 && (slot_seqid + 1) == 0) |
b85d4c01 BH |
1480 | return nfs_ok; |
1481 | /* Misordered replay or misordered new request */ | |
1482 | return nfserr_seq_misordered; | |
1483 | } | |
1484 | ||
49557cc7 AA |
1485 | /* |
1486 | * Cache the create session result into the create session single DRC | |
1487 | * slot cache by saving the xdr structure. sl_seqid has been set. | |
1488 | * Do this for solo or embedded create session operations. | |
1489 | */ | |
1490 | static void | |
1491 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, | |
1492 | struct nfsd4_clid_slot *slot, int nfserr) | |
1493 | { | |
1494 | slot->sl_status = nfserr; | |
1495 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); | |
1496 | } | |
1497 | ||
1498 | static __be32 | |
1499 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, | |
1500 | struct nfsd4_clid_slot *slot) | |
1501 | { | |
1502 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); | |
1503 | return slot->sl_status; | |
1504 | } | |
1505 | ||
069b6ad4 AA |
1506 | __be32 |
1507 | nfsd4_create_session(struct svc_rqst *rqstp, | |
1508 | struct nfsd4_compound_state *cstate, | |
1509 | struct nfsd4_create_session *cr_ses) | |
1510 | { | |
363168b4 | 1511 | struct sockaddr *sa = svc_addr(rqstp); |
ec6b5d7b | 1512 | struct nfs4_client *conf, *unconf; |
ac7c46f2 | 1513 | struct nfsd4_session *new; |
49557cc7 | 1514 | struct nfsd4_clid_slot *cs_slot = NULL; |
86c3e16c | 1515 | bool confirm_me = false; |
ec6b5d7b AA |
1516 | int status = 0; |
1517 | ||
a62573dc MJ |
1518 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
1519 | return nfserr_inval; | |
1520 | ||
ec6b5d7b AA |
1521 | nfs4_lock_state(); |
1522 | unconf = find_unconfirmed_client(&cr_ses->clientid); | |
1523 | conf = find_confirmed_client(&cr_ses->clientid); | |
1524 | ||
1525 | if (conf) { | |
49557cc7 AA |
1526 | cs_slot = &conf->cl_cs_slot; |
1527 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 | 1528 | if (status == nfserr_replay_cache) { |
ec6b5d7b | 1529 | dprintk("Got a create_session replay! seqid= %d\n", |
49557cc7 | 1530 | cs_slot->sl_seqid); |
38eb76a5 | 1531 | /* Return the cached reply status */ |
49557cc7 | 1532 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
38eb76a5 | 1533 | goto out; |
49557cc7 | 1534 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
ec6b5d7b AA |
1535 | status = nfserr_seq_misordered; |
1536 | dprintk("Sequence misordered!\n"); | |
1537 | dprintk("Expected seqid= %d but got seqid= %d\n", | |
49557cc7 | 1538 | cs_slot->sl_seqid, cr_ses->seqid); |
ec6b5d7b AA |
1539 | goto out; |
1540 | } | |
ec6b5d7b AA |
1541 | } else if (unconf) { |
1542 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || | |
363168b4 | 1543 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
ec6b5d7b AA |
1544 | status = nfserr_clid_inuse; |
1545 | goto out; | |
1546 | } | |
1547 | ||
49557cc7 AA |
1548 | cs_slot = &unconf->cl_cs_slot; |
1549 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 AA |
1550 | if (status) { |
1551 | /* an unconfirmed replay returns misordered */ | |
ec6b5d7b | 1552 | status = nfserr_seq_misordered; |
cd5b8144 | 1553 | goto out; |
ec6b5d7b AA |
1554 | } |
1555 | ||
86c3e16c | 1556 | confirm_me = true; |
ec6b5d7b AA |
1557 | conf = unconf; |
1558 | } else { | |
1559 | status = nfserr_stale_clientid; | |
1560 | goto out; | |
1561 | } | |
1562 | ||
8323c3b2 BF |
1563 | /* |
1564 | * XXX: we should probably set this at creation time, and check | |
1565 | * for consistent minorversion use throughout: | |
1566 | */ | |
1567 | conf->cl_minorversion = 1; | |
408b79bc BF |
1568 | /* |
1569 | * We do not support RDMA or persistent sessions | |
1570 | */ | |
1571 | cr_ses->flags &= ~SESSION4_PERSIST; | |
1572 | cr_ses->flags &= ~SESSION4_RDMA; | |
1573 | ||
ac7c46f2 BF |
1574 | status = nfserr_jukebox; |
1575 | new = alloc_init_session(rqstp, conf, cr_ses); | |
1576 | if (!new) | |
ec6b5d7b | 1577 | goto out; |
ac7c46f2 BF |
1578 | status = nfs_ok; |
1579 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, | |
ec6b5d7b | 1580 | NFS4_MAX_SESSIONID_LEN); |
12050657 MJ |
1581 | memcpy(&cr_ses->fore_channel, &new->se_fchannel, |
1582 | sizeof(struct nfsd4_channel_attrs)); | |
86c3e16c | 1583 | cs_slot->sl_seqid++; |
49557cc7 | 1584 | cr_ses->seqid = cs_slot->sl_seqid; |
ec6b5d7b | 1585 | |
49557cc7 AA |
1586 | /* cache solo and embedded create sessions under the state lock */ |
1587 | nfsd4_cache_create_session(cr_ses, cs_slot, status); | |
86c3e16c BF |
1588 | if (confirm_me) |
1589 | move_to_confirmed(conf); | |
ec6b5d7b AA |
1590 | out: |
1591 | nfs4_unlock_state(); | |
1592 | dprintk("%s returns %d\n", __func__, ntohl(status)); | |
1593 | return status; | |
069b6ad4 AA |
1594 | } |
1595 | ||
57716355 BF |
1596 | static bool nfsd4_last_compound_op(struct svc_rqst *rqstp) |
1597 | { | |
1598 | struct nfsd4_compoundres *resp = rqstp->rq_resp; | |
1599 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; | |
1600 | ||
1601 | return argp->opcnt == resp->opcnt; | |
1602 | } | |
1603 | ||
1d1bc8f2 BF |
1604 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
1605 | { | |
1606 | switch (*dir) { | |
1607 | case NFS4_CDFC4_FORE: | |
1608 | case NFS4_CDFC4_BACK: | |
1609 | return nfs_ok; | |
1610 | case NFS4_CDFC4_FORE_OR_BOTH: | |
1611 | case NFS4_CDFC4_BACK_OR_BOTH: | |
1612 | *dir = NFS4_CDFC4_BOTH; | |
1613 | return nfs_ok; | |
1614 | }; | |
1615 | return nfserr_inval; | |
1616 | } | |
1617 | ||
1618 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, | |
1619 | struct nfsd4_compound_state *cstate, | |
1620 | struct nfsd4_bind_conn_to_session *bcts) | |
1621 | { | |
1622 | __be32 status; | |
1623 | ||
1624 | if (!nfsd4_last_compound_op(rqstp)) | |
1625 | return nfserr_not_only_op; | |
1626 | spin_lock(&client_lock); | |
1627 | cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid); | |
1628 | /* Sorta weird: we only need the refcnt'ing because new_conn acquires | |
1629 | * client_lock iself: */ | |
1630 | if (cstate->session) { | |
1631 | nfsd4_get_session(cstate->session); | |
1632 | atomic_inc(&cstate->session->se_client->cl_refcount); | |
1633 | } | |
1634 | spin_unlock(&client_lock); | |
1635 | if (!cstate->session) | |
1636 | return nfserr_badsession; | |
1637 | ||
1638 | status = nfsd4_map_bcts_dir(&bcts->dir); | |
1639 | nfsd4_new_conn(rqstp, cstate->session, bcts->dir); | |
1640 | return nfs_ok; | |
1641 | } | |
1642 | ||
5d4cec2f BF |
1643 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
1644 | { | |
1645 | if (!session) | |
1646 | return 0; | |
1647 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); | |
1648 | } | |
1649 | ||
069b6ad4 AA |
1650 | __be32 |
1651 | nfsd4_destroy_session(struct svc_rqst *r, | |
1652 | struct nfsd4_compound_state *cstate, | |
1653 | struct nfsd4_destroy_session *sessionid) | |
1654 | { | |
e10e0cfc BH |
1655 | struct nfsd4_session *ses; |
1656 | u32 status = nfserr_badsession; | |
1657 | ||
1658 | /* Notes: | |
1659 | * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid | |
1660 | * - Should we return nfserr_back_chan_busy if waiting for | |
1661 | * callbacks on to-be-destroyed session? | |
1662 | * - Do we need to clear any callback info from previous session? | |
1663 | */ | |
1664 | ||
5d4cec2f | 1665 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
57716355 BF |
1666 | if (!nfsd4_last_compound_op(r)) |
1667 | return nfserr_not_only_op; | |
1668 | } | |
e10e0cfc | 1669 | dump_sessionid(__func__, &sessionid->sessionid); |
9089f1b4 | 1670 | spin_lock(&client_lock); |
e10e0cfc BH |
1671 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid); |
1672 | if (!ses) { | |
9089f1b4 | 1673 | spin_unlock(&client_lock); |
e10e0cfc BH |
1674 | goto out; |
1675 | } | |
1676 | ||
1677 | unhash_session(ses); | |
9089f1b4 | 1678 | spin_unlock(&client_lock); |
e10e0cfc | 1679 | |
ab707e15 | 1680 | nfs4_lock_state(); |
84f5f7cc | 1681 | nfsd4_probe_callback_sync(ses->se_client); |
ab707e15 | 1682 | nfs4_unlock_state(); |
19cf5c02 BF |
1683 | |
1684 | nfsd4_del_conns(ses); | |
1685 | ||
e10e0cfc BH |
1686 | nfsd4_put_session(ses); |
1687 | status = nfs_ok; | |
1688 | out: | |
1689 | dprintk("%s returns %d\n", __func__, ntohl(status)); | |
1690 | return status; | |
069b6ad4 AA |
1691 | } |
1692 | ||
a663bdd8 | 1693 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
328ead28 BF |
1694 | { |
1695 | struct nfsd4_conn *c; | |
1696 | ||
1697 | list_for_each_entry(c, &s->se_conns, cn_persession) { | |
a663bdd8 | 1698 | if (c->cn_xprt == xpt) { |
328ead28 BF |
1699 | return c; |
1700 | } | |
1701 | } | |
1702 | return NULL; | |
1703 | } | |
1704 | ||
a663bdd8 | 1705 | static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
328ead28 BF |
1706 | { |
1707 | struct nfs4_client *clp = ses->se_client; | |
a663bdd8 | 1708 | struct nfsd4_conn *c; |
21b75b01 | 1709 | int ret; |
328ead28 BF |
1710 | |
1711 | spin_lock(&clp->cl_lock); | |
a663bdd8 | 1712 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
328ead28 BF |
1713 | if (c) { |
1714 | spin_unlock(&clp->cl_lock); | |
1715 | free_conn(new); | |
1716 | return; | |
1717 | } | |
1718 | __nfsd4_hash_conn(new, ses); | |
1719 | spin_unlock(&clp->cl_lock); | |
21b75b01 BF |
1720 | ret = nfsd4_register_conn(new); |
1721 | if (ret) | |
1722 | /* oops; xprt is already down: */ | |
1723 | nfsd4_conn_lost(&new->cn_xpt_user); | |
328ead28 BF |
1724 | return; |
1725 | } | |
1726 | ||
868b89c3 MJ |
1727 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
1728 | { | |
1729 | struct nfsd4_compoundargs *args = rqstp->rq_argp; | |
1730 | ||
1731 | return args->opcnt > session->se_fchannel.maxops; | |
1732 | } | |
1733 | ||
069b6ad4 | 1734 | __be32 |
b85d4c01 | 1735 | nfsd4_sequence(struct svc_rqst *rqstp, |
069b6ad4 AA |
1736 | struct nfsd4_compound_state *cstate, |
1737 | struct nfsd4_sequence *seq) | |
1738 | { | |
f9bb94c4 | 1739 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
b85d4c01 BH |
1740 | struct nfsd4_session *session; |
1741 | struct nfsd4_slot *slot; | |
a663bdd8 | 1742 | struct nfsd4_conn *conn; |
b85d4c01 BH |
1743 | int status; |
1744 | ||
f9bb94c4 AA |
1745 | if (resp->opcnt != 1) |
1746 | return nfserr_sequence_pos; | |
1747 | ||
a663bdd8 BF |
1748 | /* |
1749 | * Will be either used or freed by nfsd4_sequence_check_conn | |
1750 | * below. | |
1751 | */ | |
1752 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); | |
1753 | if (!conn) | |
1754 | return nfserr_jukebox; | |
1755 | ||
9089f1b4 | 1756 | spin_lock(&client_lock); |
b85d4c01 BH |
1757 | status = nfserr_badsession; |
1758 | session = find_in_sessionid_hashtbl(&seq->sessionid); | |
1759 | if (!session) | |
1760 | goto out; | |
1761 | ||
868b89c3 MJ |
1762 | status = nfserr_too_many_ops; |
1763 | if (nfsd4_session_too_many_ops(rqstp, session)) | |
1764 | goto out; | |
1765 | ||
b85d4c01 | 1766 | status = nfserr_badslot; |
6c18ba9f | 1767 | if (seq->slotid >= session->se_fchannel.maxreqs) |
b85d4c01 BH |
1768 | goto out; |
1769 | ||
557ce264 | 1770 | slot = session->se_slots[seq->slotid]; |
b85d4c01 BH |
1771 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
1772 | ||
a8dfdaeb AA |
1773 | /* We do not negotiate the number of slots yet, so set the |
1774 | * maxslots to the session maxreqs which is used to encode | |
1775 | * sr_highest_slotid and the sr_target_slot id to maxslots */ | |
1776 | seq->maxslots = session->se_fchannel.maxreqs; | |
1777 | ||
88e588d5 | 1778 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse); |
b85d4c01 BH |
1779 | if (status == nfserr_replay_cache) { |
1780 | cstate->slot = slot; | |
1781 | cstate->session = session; | |
da3846a2 | 1782 | /* Return the cached reply status and set cstate->status |
557ce264 | 1783 | * for nfsd4_proc_compound processing */ |
bf864a31 | 1784 | status = nfsd4_replay_cache_entry(resp, seq); |
da3846a2 | 1785 | cstate->status = nfserr_replay_cache; |
aaf84eb9 | 1786 | goto out; |
b85d4c01 BH |
1787 | } |
1788 | if (status) | |
1789 | goto out; | |
1790 | ||
a663bdd8 BF |
1791 | nfsd4_sequence_check_conn(conn, session); |
1792 | conn = NULL; | |
328ead28 | 1793 | |
b85d4c01 BH |
1794 | /* Success! bump slot seqid */ |
1795 | slot->sl_inuse = true; | |
1796 | slot->sl_seqid = seq->seqid; | |
557ce264 | 1797 | slot->sl_cachethis = seq->cachethis; |
b85d4c01 BH |
1798 | |
1799 | cstate->slot = slot; | |
1800 | cstate->session = session; | |
1801 | ||
b85d4c01 | 1802 | out: |
26c0c75e | 1803 | /* Hold a session reference until done processing the compound. */ |
aaf84eb9 | 1804 | if (cstate->session) { |
0d7bb719 BF |
1805 | struct nfs4_client *clp = session->se_client; |
1806 | ||
36acb66b | 1807 | nfsd4_get_session(cstate->session); |
0d7bb719 BF |
1808 | atomic_inc(&clp->cl_refcount); |
1809 | if (clp->cl_cb_state == NFSD4_CB_DOWN) | |
1810 | seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN; | |
aaf84eb9 | 1811 | } |
a663bdd8 | 1812 | kfree(conn); |
36acb66b | 1813 | spin_unlock(&client_lock); |
b85d4c01 BH |
1814 | dprintk("%s: return %d\n", __func__, ntohl(status)); |
1815 | return status; | |
069b6ad4 AA |
1816 | } |
1817 | ||
4dc6ec00 BF |
1818 | __be32 |
1819 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) | |
1820 | { | |
1821 | if (rc->rca_one_fs) { | |
1822 | if (!cstate->current_fh.fh_dentry) | |
1823 | return nfserr_nofilehandle; | |
1824 | /* | |
1825 | * We don't take advantage of the rca_one_fs case. | |
1826 | * That's OK, it's optional, we can safely ignore it. | |
1827 | */ | |
1828 | return nfs_ok; | |
1829 | } | |
1830 | nfs4_lock_state(); | |
1831 | if (is_client_expired(cstate->session->se_client)) { | |
1832 | nfs4_unlock_state(); | |
1833 | /* | |
1834 | * The following error isn't really legal. | |
1835 | * But we only get here if the client just explicitly | |
1836 | * destroyed the client. Surely it no longer cares what | |
1837 | * error it gets back on an operation for the dead | |
1838 | * client. | |
1839 | */ | |
1840 | return nfserr_stale_clientid; | |
1841 | } | |
1842 | nfsd4_create_clid_dir(cstate->session->se_client); | |
1843 | nfs4_unlock_state(); | |
1844 | return nfs_ok; | |
1845 | } | |
1846 | ||
b37ad28b | 1847 | __be32 |
b591480b BF |
1848 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
1849 | struct nfsd4_setclientid *setclid) | |
1da177e4 | 1850 | { |
1da177e4 LT |
1851 | struct xdr_netobj clname = { |
1852 | .len = setclid->se_namelen, | |
1853 | .data = setclid->se_name, | |
1854 | }; | |
1855 | nfs4_verifier clverifier = setclid->se_verf; | |
1856 | unsigned int strhashval; | |
28ce6054 | 1857 | struct nfs4_client *conf, *unconf, *new; |
b37ad28b | 1858 | __be32 status; |
a55370a3 | 1859 | char dname[HEXDIR_LEN]; |
1da177e4 | 1860 | |
1da177e4 | 1861 | if (!check_name(clname)) |
73aea4ec | 1862 | return nfserr_inval; |
1da177e4 | 1863 | |
a55370a3 N |
1864 | status = nfs4_make_rec_clidname(dname, &clname); |
1865 | if (status) | |
73aea4ec | 1866 | return status; |
a55370a3 | 1867 | |
1da177e4 LT |
1868 | /* |
1869 | * XXX The Duplicate Request Cache (DRC) has been checked (??) | |
1870 | * We get here on a DRC miss. | |
1871 | */ | |
1872 | ||
a55370a3 | 1873 | strhashval = clientstr_hashval(dname); |
1da177e4 | 1874 | |
1da177e4 | 1875 | nfs4_lock_state(); |
e203d506 | 1876 | conf = find_confirmed_client_by_str(dname, strhashval); |
28ce6054 | 1877 | if (conf) { |
a186e767 | 1878 | /* RFC 3530 14.2.33 CASE 0: */ |
1da177e4 | 1879 | status = nfserr_clid_inuse; |
e203d506 BF |
1880 | if (clp_used_exchangeid(conf)) |
1881 | goto out; | |
026722c2 | 1882 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
363168b4 JL |
1883 | char addr_str[INET6_ADDRSTRLEN]; |
1884 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, | |
1885 | sizeof(addr_str)); | |
1886 | dprintk("NFSD: setclientid: string in use by client " | |
1887 | "at %s\n", addr_str); | |
1da177e4 LT |
1888 | goto out; |
1889 | } | |
1da177e4 | 1890 | } |
a186e767 BF |
1891 | /* |
1892 | * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION") | |
1893 | * has a description of SETCLIENTID request processing consisting | |
1894 | * of 5 bullet points, labeled as CASE0 - CASE4 below. | |
1895 | */ | |
e203d506 | 1896 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
1da177e4 LT |
1897 | status = nfserr_resource; |
1898 | if (!conf) { | |
a186e767 BF |
1899 | /* |
1900 | * RFC 3530 14.2.33 CASE 4: | |
1901 | * placed first, because it is the normal case | |
1da177e4 LT |
1902 | */ |
1903 | if (unconf) | |
1904 | expire_client(unconf); | |
b09333c4 | 1905 | new = create_client(clname, dname, rqstp, &clverifier); |
a55370a3 | 1906 | if (new == NULL) |
1da177e4 | 1907 | goto out; |
1da177e4 | 1908 | gen_clid(new); |
599e0a22 | 1909 | } else if (same_verf(&conf->cl_verifier, &clverifier)) { |
1da177e4 | 1910 | /* |
a186e767 BF |
1911 | * RFC 3530 14.2.33 CASE 1: |
1912 | * probable callback update | |
1da177e4 | 1913 | */ |
31f4a6c1 N |
1914 | if (unconf) { |
1915 | /* Note this is removing unconfirmed {*x***}, | |
1916 | * which is stronger than RFC recommended {vxc**}. | |
1917 | * This has the advantage that there is at most | |
1918 | * one {*x***} in either list at any time. | |
1919 | */ | |
1920 | expire_client(unconf); | |
1da177e4 | 1921 | } |
b09333c4 | 1922 | new = create_client(clname, dname, rqstp, &clverifier); |
a55370a3 | 1923 | if (new == NULL) |
1da177e4 | 1924 | goto out; |
1da177e4 | 1925 | copy_clid(new, conf); |
1da177e4 LT |
1926 | } else if (!unconf) { |
1927 | /* | |
a186e767 BF |
1928 | * RFC 3530 14.2.33 CASE 2: |
1929 | * probable client reboot; state will be removed if | |
1930 | * confirmed. | |
1da177e4 | 1931 | */ |
b09333c4 | 1932 | new = create_client(clname, dname, rqstp, &clverifier); |
a55370a3 | 1933 | if (new == NULL) |
1da177e4 | 1934 | goto out; |
1da177e4 | 1935 | gen_clid(new); |
49ba8781 | 1936 | } else { |
a186e767 BF |
1937 | /* |
1938 | * RFC 3530 14.2.33 CASE 3: | |
1939 | * probable client reboot; state will be removed if | |
1940 | * confirmed. | |
1da177e4 LT |
1941 | */ |
1942 | expire_client(unconf); | |
b09333c4 | 1943 | new = create_client(clname, dname, rqstp, &clverifier); |
a55370a3 | 1944 | if (new == NULL) |
1da177e4 | 1945 | goto out; |
1da177e4 | 1946 | gen_clid(new); |
1da177e4 | 1947 | } |
8323c3b2 BF |
1948 | /* |
1949 | * XXX: we should probably set this at creation time, and check | |
1950 | * for consistent minorversion use throughout: | |
1951 | */ | |
1952 | new->cl_minorversion = 0; | |
6f3d772f | 1953 | gen_callback(new, setclid, rqstp); |
c175b83c | 1954 | add_to_unconfirmed(new, strhashval); |
1da177e4 LT |
1955 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
1956 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
1957 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
1958 | status = nfs_ok; | |
1959 | out: | |
1960 | nfs4_unlock_state(); | |
1961 | return status; | |
1962 | } | |
1963 | ||
1964 | ||
1965 | /* | |
a186e767 BF |
1966 | * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has |
1967 | * a description of SETCLIENTID_CONFIRM request processing consisting of 4 | |
1968 | * bullets, labeled as CASE1 - CASE4 below. | |
1da177e4 | 1969 | */ |
b37ad28b | 1970 | __be32 |
b591480b BF |
1971 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
1972 | struct nfsd4_compound_state *cstate, | |
1973 | struct nfsd4_setclientid_confirm *setclientid_confirm) | |
1da177e4 | 1974 | { |
363168b4 | 1975 | struct sockaddr *sa = svc_addr(rqstp); |
21ab45a4 | 1976 | struct nfs4_client *conf, *unconf; |
1da177e4 LT |
1977 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
1978 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
b37ad28b | 1979 | __be32 status; |
1da177e4 LT |
1980 | |
1981 | if (STALE_CLIENTID(clid)) | |
1982 | return nfserr_stale_clientid; | |
1983 | /* | |
1984 | * XXX The Duplicate Request Cache (DRC) has been checked (??) | |
1985 | * We get here on a DRC miss. | |
1986 | */ | |
1987 | ||
1988 | nfs4_lock_state(); | |
21ab45a4 N |
1989 | |
1990 | conf = find_confirmed_client(clid); | |
1991 | unconf = find_unconfirmed_client(clid); | |
1992 | ||
1993 | status = nfserr_clid_inuse; | |
363168b4 | 1994 | if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa)) |
21ab45a4 | 1995 | goto out; |
363168b4 | 1996 | if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa)) |
21ab45a4 N |
1997 | goto out; |
1998 | ||
a186e767 BF |
1999 | /* |
2000 | * section 14.2.34 of RFC 3530 has a description of | |
2001 | * SETCLIENTID_CONFIRM request processing consisting | |
2002 | * of 4 bullet points, labeled as CASE1 - CASE4 below. | |
2003 | */ | |
366e0c1d | 2004 | if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) { |
a186e767 BF |
2005 | /* |
2006 | * RFC 3530 14.2.34 CASE 1: | |
2007 | * callback update | |
2008 | */ | |
599e0a22 | 2009 | if (!same_creds(&conf->cl_cred, &unconf->cl_cred)) |
1da177e4 LT |
2010 | status = nfserr_clid_inuse; |
2011 | else { | |
5a3c9d71 BF |
2012 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
2013 | nfsd4_probe_callback(conf); | |
1a69c179 | 2014 | expire_client(unconf); |
1da177e4 | 2015 | status = nfs_ok; |
1a69c179 | 2016 | |
1da177e4 | 2017 | } |
f3aba4e5 | 2018 | } else if (conf && !unconf) { |
a186e767 BF |
2019 | /* |
2020 | * RFC 3530 14.2.34 CASE 2: | |
2021 | * probable retransmitted request; play it safe and | |
2022 | * do nothing. | |
7c79f737 | 2023 | */ |
599e0a22 | 2024 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
1da177e4 | 2025 | status = nfserr_clid_inuse; |
21ab45a4 | 2026 | else |
1da177e4 | 2027 | status = nfs_ok; |
7c79f737 | 2028 | } else if (!conf && unconf |
599e0a22 | 2029 | && same_verf(&unconf->cl_confirm, &confirm)) { |
a186e767 BF |
2030 | /* |
2031 | * RFC 3530 14.2.34 CASE 3: | |
2032 | * Normal case; new or rebooted client: | |
7c79f737 | 2033 | */ |
599e0a22 | 2034 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
1da177e4 LT |
2035 | status = nfserr_clid_inuse; |
2036 | } else { | |
1a69c179 N |
2037 | unsigned int hash = |
2038 | clientstr_hashval(unconf->cl_recdir); | |
2039 | conf = find_confirmed_client_by_str(unconf->cl_recdir, | |
e203d506 | 2040 | hash); |
1a69c179 | 2041 | if (conf) { |
c7b9a459 | 2042 | nfsd4_remove_clid_dir(conf); |
1a69c179 N |
2043 | expire_client(conf); |
2044 | } | |
1da177e4 | 2045 | move_to_confirmed(unconf); |
21ab45a4 | 2046 | conf = unconf; |
5a3c9d71 | 2047 | nfsd4_probe_callback(conf); |
1a69c179 | 2048 | status = nfs_ok; |
1da177e4 | 2049 | } |
599e0a22 BF |
2050 | } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm))) |
2051 | && (!unconf || (unconf && !same_verf(&unconf->cl_confirm, | |
7c79f737 | 2052 | &confirm)))) { |
a186e767 BF |
2053 | /* |
2054 | * RFC 3530 14.2.34 CASE 4: | |
2055 | * Client probably hasn't noticed that we rebooted yet. | |
7c79f737 | 2056 | */ |
1da177e4 | 2057 | status = nfserr_stale_clientid; |
7c79f737 | 2058 | } else { |
08e8987c N |
2059 | /* check that we have hit one of the cases...*/ |
2060 | status = nfserr_clid_inuse; | |
2061 | } | |
1da177e4 | 2062 | out: |
1da177e4 LT |
2063 | nfs4_unlock_state(); |
2064 | return status; | |
2065 | } | |
2066 | ||
1da177e4 LT |
2067 | /* OPEN Share state helper functions */ |
2068 | static inline struct nfs4_file * | |
2069 | alloc_init_file(struct inode *ino) | |
2070 | { | |
2071 | struct nfs4_file *fp; | |
2072 | unsigned int hashval = file_hashval(ino); | |
2073 | ||
e60d4398 N |
2074 | fp = kmem_cache_alloc(file_slab, GFP_KERNEL); |
2075 | if (fp) { | |
8b671b80 | 2076 | atomic_set(&fp->fi_ref, 1); |
1da177e4 | 2077 | INIT_LIST_HEAD(&fp->fi_hash); |
8beefa24 N |
2078 | INIT_LIST_HEAD(&fp->fi_stateids); |
2079 | INIT_LIST_HEAD(&fp->fi_delegations); | |
1da177e4 LT |
2080 | fp->fi_inode = igrab(ino); |
2081 | fp->fi_id = current_fileid++; | |
47f9940c | 2082 | fp->fi_had_conflict = false; |
acfdf5c3 | 2083 | fp->fi_lease = NULL; |
f9d7562f BF |
2084 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
2085 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); | |
47cee541 PE |
2086 | spin_lock(&recall_lock); |
2087 | list_add(&fp->fi_hash, &file_hashtbl[hashval]); | |
2088 | spin_unlock(&recall_lock); | |
1da177e4 LT |
2089 | return fp; |
2090 | } | |
2091 | return NULL; | |
2092 | } | |
2093 | ||
e60d4398 | 2094 | static void |
e18b890b | 2095 | nfsd4_free_slab(struct kmem_cache **slab) |
1da177e4 | 2096 | { |
e60d4398 N |
2097 | if (*slab == NULL) |
2098 | return; | |
1a1d92c1 | 2099 | kmem_cache_destroy(*slab); |
e60d4398 | 2100 | *slab = NULL; |
1da177e4 LT |
2101 | } |
2102 | ||
e8ff2a84 | 2103 | void |
1da177e4 LT |
2104 | nfsd4_free_slabs(void) |
2105 | { | |
e60d4398 N |
2106 | nfsd4_free_slab(&stateowner_slab); |
2107 | nfsd4_free_slab(&file_slab); | |
5ac049ac | 2108 | nfsd4_free_slab(&stateid_slab); |
5b2d21c1 | 2109 | nfsd4_free_slab(&deleg_slab); |
e60d4398 | 2110 | } |
1da177e4 | 2111 | |
e60d4398 N |
2112 | static int |
2113 | nfsd4_init_slabs(void) | |
2114 | { | |
2115 | stateowner_slab = kmem_cache_create("nfsd4_stateowners", | |
20c2df83 | 2116 | sizeof(struct nfs4_stateowner), 0, 0, NULL); |
e60d4398 N |
2117 | if (stateowner_slab == NULL) |
2118 | goto out_nomem; | |
2119 | file_slab = kmem_cache_create("nfsd4_files", | |
20c2df83 | 2120 | sizeof(struct nfs4_file), 0, 0, NULL); |
e60d4398 N |
2121 | if (file_slab == NULL) |
2122 | goto out_nomem; | |
5ac049ac | 2123 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
20c2df83 | 2124 | sizeof(struct nfs4_stateid), 0, 0, NULL); |
5ac049ac N |
2125 | if (stateid_slab == NULL) |
2126 | goto out_nomem; | |
5b2d21c1 | 2127 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
20c2df83 | 2128 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
5b2d21c1 N |
2129 | if (deleg_slab == NULL) |
2130 | goto out_nomem; | |
e60d4398 N |
2131 | return 0; |
2132 | out_nomem: | |
2133 | nfsd4_free_slabs(); | |
2134 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); | |
2135 | return -ENOMEM; | |
1da177e4 LT |
2136 | } |
2137 | ||
2138 | void | |
2139 | nfs4_free_stateowner(struct kref *kref) | |
2140 | { | |
2141 | struct nfs4_stateowner *sop = | |
2142 | container_of(kref, struct nfs4_stateowner, so_ref); | |
2143 | kfree(sop->so_owner.data); | |
2144 | kmem_cache_free(stateowner_slab, sop); | |
2145 | } | |
2146 | ||
2147 | static inline struct nfs4_stateowner * | |
2148 | alloc_stateowner(struct xdr_netobj *owner) | |
2149 | { | |
2150 | struct nfs4_stateowner *sop; | |
2151 | ||
2152 | if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) { | |
2153 | if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) { | |
2154 | memcpy(sop->so_owner.data, owner->data, owner->len); | |
2155 | sop->so_owner.len = owner->len; | |
2156 | kref_init(&sop->so_ref); | |
2157 | return sop; | |
2158 | } | |
2159 | kmem_cache_free(stateowner_slab, sop); | |
2160 | } | |
2161 | return NULL; | |
2162 | } | |
2163 | ||
2164 | static struct nfs4_stateowner * | |
2165 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) { | |
2166 | struct nfs4_stateowner *sop; | |
2167 | struct nfs4_replay *rp; | |
2168 | unsigned int idhashval; | |
2169 | ||
2170 | if (!(sop = alloc_stateowner(&open->op_owner))) | |
2171 | return NULL; | |
2172 | idhashval = ownerid_hashval(current_ownerid); | |
2173 | INIT_LIST_HEAD(&sop->so_idhash); | |
2174 | INIT_LIST_HEAD(&sop->so_strhash); | |
2175 | INIT_LIST_HEAD(&sop->so_perclient); | |
ea1da636 N |
2176 | INIT_LIST_HEAD(&sop->so_stateids); |
2177 | INIT_LIST_HEAD(&sop->so_perstateid); /* not used */ | |
1da177e4 LT |
2178 | INIT_LIST_HEAD(&sop->so_close_lru); |
2179 | sop->so_time = 0; | |
2180 | list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]); | |
2181 | list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]); | |
ea1da636 | 2182 | list_add(&sop->so_perclient, &clp->cl_openowners); |
1da177e4 LT |
2183 | sop->so_is_open_owner = 1; |
2184 | sop->so_id = current_ownerid++; | |
2185 | sop->so_client = clp; | |
2186 | sop->so_seqid = open->op_seqid; | |
2187 | sop->so_confirmed = 0; | |
2188 | rp = &sop->so_replay; | |
de1ae286 | 2189 | rp->rp_status = nfserr_serverfault; |
1da177e4 LT |
2190 | rp->rp_buflen = 0; |
2191 | rp->rp_buf = rp->rp_ibuf; | |
2192 | return sop; | |
2193 | } | |
2194 | ||
1da177e4 LT |
2195 | static inline void |
2196 | init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { | |
2197 | struct nfs4_stateowner *sop = open->op_stateowner; | |
2198 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); | |
2199 | ||
2200 | INIT_LIST_HEAD(&stp->st_hash); | |
ea1da636 N |
2201 | INIT_LIST_HEAD(&stp->st_perstateowner); |
2202 | INIT_LIST_HEAD(&stp->st_lockowners); | |
1da177e4 LT |
2203 | INIT_LIST_HEAD(&stp->st_perfile); |
2204 | list_add(&stp->st_hash, &stateid_hashtbl[hashval]); | |
ea1da636 | 2205 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
8beefa24 | 2206 | list_add(&stp->st_perfile, &fp->fi_stateids); |
1da177e4 | 2207 | stp->st_stateowner = sop; |
13cd2184 | 2208 | get_nfs4_file(fp); |
1da177e4 | 2209 | stp->st_file = fp; |
e4e83ea4 | 2210 | stp->st_stateid.si_boot = boot_time; |
1da177e4 LT |
2211 | stp->st_stateid.si_stateownerid = sop->so_id; |
2212 | stp->st_stateid.si_fileid = fp->fi_id; | |
2213 | stp->st_stateid.si_generation = 0; | |
2214 | stp->st_access_bmap = 0; | |
2215 | stp->st_deny_bmap = 0; | |
84459a11 AA |
2216 | __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK, |
2217 | &stp->st_access_bmap); | |
1da177e4 | 2218 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
4c4cd222 | 2219 | stp->st_openstp = NULL; |
1da177e4 LT |
2220 | } |
2221 | ||
fd39ca9a | 2222 | static void |
1da177e4 LT |
2223 | move_to_close_lru(struct nfs4_stateowner *sop) |
2224 | { | |
2225 | dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop); | |
2226 | ||
358dd55a | 2227 | list_move_tail(&sop->so_close_lru, &close_lru); |
1da177e4 LT |
2228 | sop->so_time = get_seconds(); |
2229 | } | |
2230 | ||
1da177e4 | 2231 | static int |
599e0a22 BF |
2232 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
2233 | clientid_t *clid) | |
2234 | { | |
2235 | return (sop->so_owner.len == owner->len) && | |
2236 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && | |
2237 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); | |
1da177e4 LT |
2238 | } |
2239 | ||
2240 | static struct nfs4_stateowner * | |
2241 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open) | |
2242 | { | |
2243 | struct nfs4_stateowner *so = NULL; | |
2244 | ||
2245 | list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) { | |
599e0a22 | 2246 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) |
1da177e4 LT |
2247 | return so; |
2248 | } | |
2249 | return NULL; | |
2250 | } | |
2251 | ||
2252 | /* search file_hashtbl[] for file */ | |
2253 | static struct nfs4_file * | |
2254 | find_file(struct inode *ino) | |
2255 | { | |
2256 | unsigned int hashval = file_hashval(ino); | |
2257 | struct nfs4_file *fp; | |
2258 | ||
8b671b80 | 2259 | spin_lock(&recall_lock); |
1da177e4 | 2260 | list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
13cd2184 N |
2261 | if (fp->fi_inode == ino) { |
2262 | get_nfs4_file(fp); | |
8b671b80 | 2263 | spin_unlock(&recall_lock); |
1da177e4 | 2264 | return fp; |
13cd2184 | 2265 | } |
1da177e4 | 2266 | } |
8b671b80 | 2267 | spin_unlock(&recall_lock); |
1da177e4 LT |
2268 | return NULL; |
2269 | } | |
2270 | ||
d87a8ade | 2271 | static inline int access_valid(u32 x, u32 minorversion) |
ba5a6a19 | 2272 | { |
d87a8ade | 2273 | if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ) |
8838dc43 | 2274 | return 0; |
d87a8ade AA |
2275 | if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH) |
2276 | return 0; | |
2277 | x &= ~NFS4_SHARE_ACCESS_MASK; | |
2278 | if (minorversion && x) { | |
2279 | if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL) | |
2280 | return 0; | |
2281 | if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED) | |
2282 | return 0; | |
2283 | x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK); | |
2284 | } | |
2285 | if (x) | |
8838dc43 BF |
2286 | return 0; |
2287 | return 1; | |
ba5a6a19 BF |
2288 | } |
2289 | ||
8838dc43 | 2290 | static inline int deny_valid(u32 x) |
ba5a6a19 | 2291 | { |
8838dc43 BF |
2292 | /* Note: unlike access bits, deny bits may be zero. */ |
2293 | return x <= NFS4_SHARE_DENY_BOTH; | |
ba5a6a19 | 2294 | } |
1da177e4 | 2295 | |
1da177e4 LT |
2296 | /* |
2297 | * Called to check deny when READ with all zero stateid or | |
2298 | * WRITE with all zero or all one stateid | |
2299 | */ | |
b37ad28b | 2300 | static __be32 |
1da177e4 LT |
2301 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
2302 | { | |
2303 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
2304 | struct nfs4_file *fp; | |
2305 | struct nfs4_stateid *stp; | |
b37ad28b | 2306 | __be32 ret; |
1da177e4 LT |
2307 | |
2308 | dprintk("NFSD: nfs4_share_conflict\n"); | |
2309 | ||
2310 | fp = find_file(ino); | |
13cd2184 N |
2311 | if (!fp) |
2312 | return nfs_ok; | |
b700949b | 2313 | ret = nfserr_locked; |
1da177e4 | 2314 | /* Search for conflicting share reservations */ |
13cd2184 N |
2315 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
2316 | if (test_bit(deny_type, &stp->st_deny_bmap) || | |
2317 | test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap)) | |
2318 | goto out; | |
1da177e4 | 2319 | } |
13cd2184 N |
2320 | ret = nfs_ok; |
2321 | out: | |
2322 | put_nfs4_file(fp); | |
2323 | return ret; | |
1da177e4 LT |
2324 | } |
2325 | ||
2326 | static inline void | |
f9d7562f | 2327 | nfs4_file_downgrade(struct nfs4_file *fp, unsigned int share_access) |
1da177e4 | 2328 | { |
f9d7562f BF |
2329 | if (share_access & NFS4_SHARE_ACCESS_WRITE) |
2330 | nfs4_file_put_access(fp, O_WRONLY); | |
2331 | if (share_access & NFS4_SHARE_ACCESS_READ) | |
2332 | nfs4_file_put_access(fp, O_RDONLY); | |
1da177e4 LT |
2333 | } |
2334 | ||
6b57d9c8 | 2335 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
1da177e4 | 2336 | { |
1da177e4 LT |
2337 | /* We're assuming the state code never drops its reference |
2338 | * without first removing the lease. Since we're in this lease | |
2339 | * callback (and since the lease code is serialized by the kernel | |
2340 | * lock) we know the server hasn't removed the lease yet, we know | |
2341 | * it's safe to take a reference: */ | |
2342 | atomic_inc(&dp->dl_count); | |
2343 | ||
1da177e4 | 2344 | list_add_tail(&dp->dl_recall_lru, &del_recall_lru); |
1da177e4 | 2345 | |
460781b5 | 2346 | /* only place dl_time is set. protected by lock_flocks*/ |
1da177e4 LT |
2347 | dp->dl_time = get_seconds(); |
2348 | ||
6b57d9c8 BF |
2349 | nfsd4_cb_recall(dp); |
2350 | } | |
2351 | ||
acfdf5c3 | 2352 | /* Called from break_lease() with lock_flocks() held. */ |
6b57d9c8 BF |
2353 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
2354 | { | |
acfdf5c3 BF |
2355 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
2356 | struct nfs4_delegation *dp; | |
6b57d9c8 | 2357 | |
acfdf5c3 BF |
2358 | BUG_ON(!fp); |
2359 | /* We assume break_lease is only called once per lease: */ | |
2360 | BUG_ON(fp->fi_had_conflict); | |
0272e1fd BF |
2361 | /* |
2362 | * We don't want the locks code to timeout the lease for us; | |
acfdf5c3 | 2363 | * we'll remove it ourself if a delegation isn't returned |
6b57d9c8 | 2364 | * in time: |
0272e1fd BF |
2365 | */ |
2366 | fl->fl_break_time = 0; | |
1da177e4 | 2367 | |
5d926e8c | 2368 | spin_lock(&recall_lock); |
acfdf5c3 BF |
2369 | fp->fi_had_conflict = true; |
2370 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) | |
2371 | nfsd_break_one_deleg(dp); | |
5d926e8c | 2372 | spin_unlock(&recall_lock); |
1da177e4 LT |
2373 | } |
2374 | ||
1da177e4 LT |
2375 | static |
2376 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) | |
2377 | { | |
2378 | if (arg & F_UNLCK) | |
2379 | return lease_modify(onlist, arg); | |
2380 | else | |
2381 | return -EAGAIN; | |
2382 | } | |
2383 | ||
7b021967 | 2384 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
1da177e4 | 2385 | .fl_break = nfsd_break_deleg_cb, |
1da177e4 LT |
2386 | .fl_change = nfsd_change_deleg_cb, |
2387 | }; | |
2388 | ||
2389 | ||
b37ad28b | 2390 | __be32 |
6668958f AA |
2391 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
2392 | struct nfsd4_open *open) | |
1da177e4 | 2393 | { |
1da177e4 LT |
2394 | clientid_t *clientid = &open->op_clientid; |
2395 | struct nfs4_client *clp = NULL; | |
2396 | unsigned int strhashval; | |
2397 | struct nfs4_stateowner *sop = NULL; | |
2398 | ||
1da177e4 | 2399 | if (!check_name(open->op_owner)) |
0f442aa2 | 2400 | return nfserr_inval; |
1da177e4 LT |
2401 | |
2402 | if (STALE_CLIENTID(&open->op_clientid)) | |
2403 | return nfserr_stale_clientid; | |
2404 | ||
2405 | strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner); | |
2406 | sop = find_openstateowner_str(strhashval, open); | |
0f442aa2 BF |
2407 | open->op_stateowner = sop; |
2408 | if (!sop) { | |
2409 | /* Make sure the client's lease hasn't expired. */ | |
1da177e4 LT |
2410 | clp = find_confirmed_client(clientid); |
2411 | if (clp == NULL) | |
0f442aa2 BF |
2412 | return nfserr_expired; |
2413 | goto renew; | |
1da177e4 | 2414 | } |
6668958f AA |
2415 | /* When sessions are used, skip open sequenceid processing */ |
2416 | if (nfsd4_has_session(cstate)) | |
2417 | goto renew; | |
0f442aa2 BF |
2418 | if (!sop->so_confirmed) { |
2419 | /* Replace unconfirmed owners without checking for replay. */ | |
2420 | clp = sop->so_client; | |
f044ff83 | 2421 | release_openowner(sop); |
0f442aa2 BF |
2422 | open->op_stateowner = NULL; |
2423 | goto renew; | |
2424 | } | |
2425 | if (open->op_seqid == sop->so_seqid - 1) { | |
2426 | if (sop->so_replay.rp_buflen) | |
a90b061c | 2427 | return nfserr_replay_me; |
0f442aa2 BF |
2428 | /* The original OPEN failed so spectacularly |
2429 | * that we don't even have replay data saved! | |
2430 | * Therefore, we have no choice but to continue | |
2431 | * processing this OPEN; presumably, we'll | |
2432 | * fail again for the same reason. | |
2433 | */ | |
2434 | dprintk("nfsd4_process_open1: replay with no replay cache\n"); | |
2435 | goto renew; | |
2436 | } | |
2437 | if (open->op_seqid != sop->so_seqid) | |
2438 | return nfserr_bad_seqid; | |
1da177e4 | 2439 | renew: |
0f442aa2 BF |
2440 | if (open->op_stateowner == NULL) { |
2441 | sop = alloc_init_open_stateowner(strhashval, clp, open); | |
2442 | if (sop == NULL) | |
2443 | return nfserr_resource; | |
2444 | open->op_stateowner = sop; | |
2445 | } | |
2446 | list_del_init(&sop->so_close_lru); | |
1da177e4 | 2447 | renew_client(sop->so_client); |
0f442aa2 | 2448 | return nfs_ok; |
1da177e4 LT |
2449 | } |
2450 | ||
b37ad28b | 2451 | static inline __be32 |
4a6e43e6 N |
2452 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
2453 | { | |
2454 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
2455 | return nfserr_openmode; | |
2456 | else | |
2457 | return nfs_ok; | |
2458 | } | |
2459 | ||
52f4fb43 N |
2460 | static struct nfs4_delegation * |
2461 | find_delegation_file(struct nfs4_file *fp, stateid_t *stid) | |
2462 | { | |
32b007b4 | 2463 | struct nfs4_delegation *dp; |
52f4fb43 | 2464 | |
acfdf5c3 | 2465 | spin_lock(&recall_lock); |
32b007b4 BF |
2466 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
2467 | if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) { | |
2468 | spin_unlock(&recall_lock); | |
2469 | return dp; | |
2470 | } | |
acfdf5c3 | 2471 | spin_unlock(&recall_lock); |
32b007b4 | 2472 | return NULL; |
52f4fb43 N |
2473 | } |
2474 | ||
24a0111e BF |
2475 | int share_access_to_flags(u32 share_access) |
2476 | { | |
2477 | share_access &= ~NFS4_SHARE_WANT_MASK; | |
2478 | ||
2479 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; | |
2480 | } | |
2481 | ||
b37ad28b | 2482 | static __be32 |
567d9829 N |
2483 | nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open, |
2484 | struct nfs4_delegation **dp) | |
2485 | { | |
2486 | int flags; | |
b37ad28b | 2487 | __be32 status = nfserr_bad_stateid; |
567d9829 N |
2488 | |
2489 | *dp = find_delegation_file(fp, &open->op_delegate_stateid); | |
2490 | if (*dp == NULL) | |
c44c5eeb | 2491 | goto out; |
24a0111e | 2492 | flags = share_access_to_flags(open->op_share_access); |
567d9829 N |
2493 | status = nfs4_check_delegmode(*dp, flags); |
2494 | if (status) | |
2495 | *dp = NULL; | |
c44c5eeb N |
2496 | out: |
2497 | if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR) | |
2498 | return nfs_ok; | |
2499 | if (status) | |
2500 | return status; | |
2501 | open->op_stateowner->so_confirmed = 1; | |
2502 | return nfs_ok; | |
567d9829 N |
2503 | } |
2504 | ||
b37ad28b | 2505 | static __be32 |
1da177e4 LT |
2506 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp) |
2507 | { | |
2508 | struct nfs4_stateid *local; | |
b37ad28b | 2509 | __be32 status = nfserr_share_denied; |
1da177e4 LT |
2510 | struct nfs4_stateowner *sop = open->op_stateowner; |
2511 | ||
8beefa24 | 2512 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
1da177e4 LT |
2513 | /* ignore lock owners */ |
2514 | if (local->st_stateowner->so_is_open_owner == 0) | |
2515 | continue; | |
2516 | /* remember if we have seen this open owner */ | |
2517 | if (local->st_stateowner == sop) | |
2518 | *stpp = local; | |
2519 | /* check for conflicting share reservations */ | |
2520 | if (!test_share(local, open)) | |
2521 | goto out; | |
2522 | } | |
2523 | status = 0; | |
2524 | out: | |
2525 | return status; | |
2526 | } | |
2527 | ||
5ac049ac N |
2528 | static inline struct nfs4_stateid * |
2529 | nfs4_alloc_stateid(void) | |
2530 | { | |
2531 | return kmem_cache_alloc(stateid_slab, GFP_KERNEL); | |
2532 | } | |
2533 | ||
21fb4016 BF |
2534 | static inline int nfs4_access_to_access(u32 nfs4_access) |
2535 | { | |
2536 | int flags = 0; | |
2537 | ||
2538 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) | |
2539 | flags |= NFSD_MAY_READ; | |
2540 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) | |
2541 | flags |= NFSD_MAY_WRITE; | |
2542 | return flags; | |
2543 | } | |
2544 | ||
f9d7562f BF |
2545 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file |
2546 | *fp, struct svc_fh *cur_fh, u32 nfs4_access) | |
2547 | { | |
2548 | __be32 status; | |
2549 | int oflag = nfs4_access_to_omode(nfs4_access); | |
2550 | int access = nfs4_access_to_access(nfs4_access); | |
2551 | ||
2552 | if (!fp->fi_fds[oflag]) { | |
2553 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, | |
2554 | &fp->fi_fds[oflag]); | |
f9d7562f BF |
2555 | if (status) |
2556 | return status; | |
2557 | } | |
2558 | nfs4_file_get_access(fp, oflag); | |
2559 | ||
2560 | return nfs_ok; | |
2561 | } | |
2562 | ||
b37ad28b | 2563 | static __be32 |
1da177e4 | 2564 | nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp, |
f9d7562f BF |
2565 | struct nfs4_file *fp, struct svc_fh *cur_fh, |
2566 | struct nfsd4_open *open) | |
1da177e4 LT |
2567 | { |
2568 | struct nfs4_stateid *stp; | |
f9d7562f | 2569 | __be32 status; |
1da177e4 | 2570 | |
5ac049ac | 2571 | stp = nfs4_alloc_stateid(); |
1da177e4 LT |
2572 | if (stp == NULL) |
2573 | return nfserr_resource; | |
2574 | ||
f9d7562f BF |
2575 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open->op_share_access); |
2576 | if (status) { | |
2577 | kmem_cache_free(stateid_slab, stp); | |
2578 | return status; | |
1da177e4 | 2579 | } |
1da177e4 LT |
2580 | *stpp = stp; |
2581 | return 0; | |
2582 | } | |
2583 | ||
b37ad28b | 2584 | static inline __be32 |
1da177e4 LT |
2585 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
2586 | struct nfsd4_open *open) | |
2587 | { | |
2588 | struct iattr iattr = { | |
2589 | .ia_valid = ATTR_SIZE, | |
2590 | .ia_size = 0, | |
2591 | }; | |
2592 | if (!open->op_truncate) | |
2593 | return 0; | |
2594 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
9246585a | 2595 | return nfserr_inval; |
1da177e4 LT |
2596 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
2597 | } | |
2598 | ||
b37ad28b | 2599 | static __be32 |
f9d7562f | 2600 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open) |
1da177e4 | 2601 | { |
7d947842 BF |
2602 | u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK; |
2603 | bool new_access; | |
b37ad28b | 2604 | __be32 status; |
1da177e4 | 2605 | |
7d947842 | 2606 | new_access = !test_bit(op_share_access, &stp->st_access_bmap); |
f9d7562f | 2607 | if (new_access) { |
7d947842 | 2608 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, op_share_access); |
f9d7562f BF |
2609 | if (status) |
2610 | return status; | |
6c26d08f | 2611 | } |
1da177e4 LT |
2612 | status = nfsd4_truncate(rqstp, cur_fh, open); |
2613 | if (status) { | |
f9d7562f BF |
2614 | if (new_access) { |
2615 | int oflag = nfs4_access_to_omode(new_access); | |
2616 | nfs4_file_put_access(fp, oflag); | |
2617 | } | |
1da177e4 LT |
2618 | return status; |
2619 | } | |
2620 | /* remember the open */ | |
24a0111e | 2621 | __set_bit(op_share_access, &stp->st_access_bmap); |
b55e0ba1 | 2622 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
1da177e4 LT |
2623 | |
2624 | return nfs_ok; | |
2625 | } | |
2626 | ||
2627 | ||
1da177e4 | 2628 | static void |
37515177 | 2629 | nfs4_set_claim_prev(struct nfsd4_open *open) |
1da177e4 | 2630 | { |
37515177 N |
2631 | open->op_stateowner->so_confirmed = 1; |
2632 | open->op_stateowner->so_client->cl_firststate = 1; | |
1da177e4 LT |
2633 | } |
2634 | ||
14a24e99 BF |
2635 | /* Should we give out recallable state?: */ |
2636 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) | |
2637 | { | |
2638 | if (clp->cl_cb_state == NFSD4_CB_UP) | |
2639 | return true; | |
2640 | /* | |
2641 | * In the sessions case, since we don't have to establish a | |
2642 | * separate connection for callbacks, we assume it's OK | |
2643 | * until we hear otherwise: | |
2644 | */ | |
2645 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; | |
2646 | } | |
2647 | ||
22d38c4c BF |
2648 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag) |
2649 | { | |
2650 | struct file_lock *fl; | |
2651 | ||
2652 | fl = locks_alloc_lock(); | |
2653 | if (!fl) | |
2654 | return NULL; | |
2655 | locks_init_lock(fl); | |
2656 | fl->fl_lmops = &nfsd_lease_mng_ops; | |
2657 | fl->fl_flags = FL_LEASE; | |
2658 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; | |
2659 | fl->fl_end = OFFSET_MAX; | |
acfdf5c3 | 2660 | fl->fl_owner = (fl_owner_t)(dp->dl_file); |
22d38c4c | 2661 | fl->fl_pid = current->tgid; |
22d38c4c BF |
2662 | return fl; |
2663 | } | |
2664 | ||
edab9782 BF |
2665 | static int nfs4_setlease(struct nfs4_delegation *dp, int flag) |
2666 | { | |
acfdf5c3 | 2667 | struct nfs4_file *fp = dp->dl_file; |
edab9782 BF |
2668 | struct file_lock *fl; |
2669 | int status; | |
2670 | ||
2671 | fl = nfs4_alloc_init_lease(dp, flag); | |
2672 | if (!fl) | |
2673 | return -ENOMEM; | |
acfdf5c3 BF |
2674 | fl->fl_file = find_readable_file(fp); |
2675 | list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations); | |
2676 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl); | |
edab9782 | 2677 | if (status) { |
acfdf5c3 | 2678 | list_del_init(&dp->dl_perclnt); |
edab9782 BF |
2679 | locks_free_lock(fl); |
2680 | return -ENOMEM; | |
2681 | } | |
acfdf5c3 BF |
2682 | fp->fi_lease = fl; |
2683 | fp->fi_deleg_file = fl->fl_file; | |
2684 | get_file(fp->fi_deleg_file); | |
2685 | atomic_set(&fp->fi_delegees, 1); | |
2686 | list_add(&dp->dl_perfile, &fp->fi_delegations); | |
2687 | return 0; | |
2688 | } | |
2689 | ||
2690 | static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag) | |
2691 | { | |
2692 | struct nfs4_file *fp = dp->dl_file; | |
2693 | ||
2694 | if (!fp->fi_lease) | |
2695 | return nfs4_setlease(dp, flag); | |
2696 | spin_lock(&recall_lock); | |
2697 | if (fp->fi_had_conflict) { | |
2698 | spin_unlock(&recall_lock); | |
2699 | return -EAGAIN; | |
2700 | } | |
2701 | atomic_inc(&fp->fi_delegees); | |
2702 | list_add(&dp->dl_perfile, &fp->fi_delegations); | |
2703 | spin_unlock(&recall_lock); | |
2704 | list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations); | |
edab9782 BF |
2705 | return 0; |
2706 | } | |
2707 | ||
1da177e4 LT |
2708 | /* |
2709 | * Attempt to hand out a delegation. | |
2710 | */ | |
2711 | static void | |
2712 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp) | |
2713 | { | |
2714 | struct nfs4_delegation *dp; | |
2715 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
14a24e99 | 2716 | int cb_up; |
1da177e4 LT |
2717 | int status, flag = 0; |
2718 | ||
14a24e99 | 2719 | cb_up = nfsd4_cb_channel_good(sop->so_client); |
1da177e4 | 2720 | flag = NFS4_OPEN_DELEGATE_NONE; |
7b190fec N |
2721 | open->op_recall = 0; |
2722 | switch (open->op_claim_type) { | |
2723 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
2bf23875 | 2724 | if (!cb_up) |
7b190fec N |
2725 | open->op_recall = 1; |
2726 | flag = open->op_delegate_type; | |
2727 | if (flag == NFS4_OPEN_DELEGATE_NONE) | |
2728 | goto out; | |
2729 | break; | |
2730 | case NFS4_OPEN_CLAIM_NULL: | |
2731 | /* Let's not give out any delegations till everyone's | |
2732 | * had the chance to reclaim theirs.... */ | |
af558e33 | 2733 | if (locks_in_grace()) |
7b190fec | 2734 | goto out; |
2bf23875 | 2735 | if (!cb_up || !sop->so_confirmed) |
7b190fec N |
2736 | goto out; |
2737 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) | |
2738 | flag = NFS4_OPEN_DELEGATE_WRITE; | |
2739 | else | |
2740 | flag = NFS4_OPEN_DELEGATE_READ; | |
2741 | break; | |
2742 | default: | |
2743 | goto out; | |
2744 | } | |
1da177e4 LT |
2745 | |
2746 | dp = alloc_init_deleg(sop->so_client, stp, fh, flag); | |
dd239cc0 BF |
2747 | if (dp == NULL) |
2748 | goto out_no_deleg; | |
acfdf5c3 | 2749 | status = nfs4_set_delegation(dp, flag); |
edab9782 | 2750 | if (status) |
dd239cc0 | 2751 | goto out_free; |
1da177e4 LT |
2752 | |
2753 | memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid)); | |
2754 | ||
8c10cbdb BH |
2755 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
2756 | STATEID_VAL(&dp->dl_stateid)); | |
1da177e4 | 2757 | out: |
7b190fec N |
2758 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS |
2759 | && flag == NFS4_OPEN_DELEGATE_NONE | |
2760 | && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) | |
2fdada03 | 2761 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
1da177e4 | 2762 | open->op_delegate_type = flag; |
dd239cc0 BF |
2763 | return; |
2764 | out_free: | |
acfdf5c3 | 2765 | nfs4_put_delegation(dp); |
dd239cc0 BF |
2766 | out_no_deleg: |
2767 | flag = NFS4_OPEN_DELEGATE_NONE; | |
2768 | goto out; | |
1da177e4 LT |
2769 | } |
2770 | ||
2771 | /* | |
2772 | * called with nfs4_lock_state() held. | |
2773 | */ | |
b37ad28b | 2774 | __be32 |
1da177e4 LT |
2775 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
2776 | { | |
6668958f | 2777 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
1da177e4 LT |
2778 | struct nfs4_file *fp = NULL; |
2779 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
2780 | struct nfs4_stateid *stp = NULL; | |
567d9829 | 2781 | struct nfs4_delegation *dp = NULL; |
b37ad28b | 2782 | __be32 status; |
1da177e4 LT |
2783 | |
2784 | status = nfserr_inval; | |
d87a8ade | 2785 | if (!access_valid(open->op_share_access, resp->cstate.minorversion) |
ba5a6a19 | 2786 | || !deny_valid(open->op_share_deny)) |
1da177e4 LT |
2787 | goto out; |
2788 | /* | |
2789 | * Lookup file; if found, lookup stateid and check open request, | |
2790 | * and check for delegations in the process of being recalled. | |
2791 | * If not found, create the nfs4_file struct | |
2792 | */ | |
2793 | fp = find_file(ino); | |
2794 | if (fp) { | |
2795 | if ((status = nfs4_check_open(fp, open, &stp))) | |
2796 | goto out; | |
c44c5eeb N |
2797 | status = nfs4_check_deleg(fp, open, &dp); |
2798 | if (status) | |
2799 | goto out; | |
1da177e4 | 2800 | } else { |
c44c5eeb N |
2801 | status = nfserr_bad_stateid; |
2802 | if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) | |
2803 | goto out; | |
1da177e4 LT |
2804 | status = nfserr_resource; |
2805 | fp = alloc_init_file(ino); | |
2806 | if (fp == NULL) | |
2807 | goto out; | |
2808 | } | |
2809 | ||
2810 | /* | |
2811 | * OPEN the file, or upgrade an existing OPEN. | |
2812 | * If truncate fails, the OPEN fails. | |
2813 | */ | |
2814 | if (stp) { | |
2815 | /* Stateid was found, this is an OPEN upgrade */ | |
f9d7562f | 2816 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
1da177e4 LT |
2817 | if (status) |
2818 | goto out; | |
444c2c07 | 2819 | update_stateid(&stp->st_stateid); |
1da177e4 | 2820 | } else { |
f9d7562f | 2821 | status = nfs4_new_open(rqstp, &stp, fp, current_fh, open); |
567d9829 | 2822 | if (status) |
1da177e4 LT |
2823 | goto out; |
2824 | init_stateid(stp, fp, open); | |
2825 | status = nfsd4_truncate(rqstp, current_fh, open); | |
2826 | if (status) { | |
2283963f | 2827 | release_open_stateid(stp); |
1da177e4 LT |
2828 | goto out; |
2829 | } | |
6668958f AA |
2830 | if (nfsd4_has_session(&resp->cstate)) |
2831 | update_stateid(&stp->st_stateid); | |
1da177e4 LT |
2832 | } |
2833 | memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
2834 | ||
4dc6ec00 | 2835 | if (nfsd4_has_session(&resp->cstate)) |
6668958f AA |
2836 | open->op_stateowner->so_confirmed = 1; |
2837 | ||
1da177e4 LT |
2838 | /* |
2839 | * Attempt to hand out a delegation. No error return, because the | |
2840 | * OPEN succeeds even if we fail. | |
2841 | */ | |
2842 | nfs4_open_delegation(current_fh, open, stp); | |
2843 | ||
2844 | status = nfs_ok; | |
2845 | ||
8c10cbdb BH |
2846 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
2847 | STATEID_VAL(&stp->st_stateid)); | |
1da177e4 | 2848 | out: |
13cd2184 N |
2849 | if (fp) |
2850 | put_nfs4_file(fp); | |
37515177 N |
2851 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
2852 | nfs4_set_claim_prev(open); | |
1da177e4 LT |
2853 | /* |
2854 | * To finish the open response, we just need to set the rflags. | |
2855 | */ | |
2856 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
6668958f AA |
2857 | if (!open->op_stateowner->so_confirmed && |
2858 | !nfsd4_has_session(&resp->cstate)) | |
1da177e4 LT |
2859 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
2860 | ||
2861 | return status; | |
2862 | } | |
2863 | ||
b37ad28b | 2864 | __be32 |
b591480b BF |
2865 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
2866 | clientid_t *clid) | |
1da177e4 LT |
2867 | { |
2868 | struct nfs4_client *clp; | |
b37ad28b | 2869 | __be32 status; |
1da177e4 LT |
2870 | |
2871 | nfs4_lock_state(); | |
2872 | dprintk("process_renew(%08x/%08x): starting\n", | |
2873 | clid->cl_boot, clid->cl_id); | |
2874 | status = nfserr_stale_clientid; | |
2875 | if (STALE_CLIENTID(clid)) | |
2876 | goto out; | |
2877 | clp = find_confirmed_client(clid); | |
2878 | status = nfserr_expired; | |
2879 | if (clp == NULL) { | |
2880 | /* We assume the client took too long to RENEW. */ | |
2881 | dprintk("nfsd4_renew: clientid not found!\n"); | |
2882 | goto out; | |
2883 | } | |
2884 | renew_client(clp); | |
2885 | status = nfserr_cb_path_down; | |
ea1da636 | 2886 | if (!list_empty(&clp->cl_delegations) |
77a3569d | 2887 | && clp->cl_cb_state != NFSD4_CB_UP) |
1da177e4 LT |
2888 | goto out; |
2889 | status = nfs_ok; | |
2890 | out: | |
2891 | nfs4_unlock_state(); | |
2892 | return status; | |
2893 | } | |
2894 | ||
af558e33 BF |
2895 | struct lock_manager nfsd4_manager = { |
2896 | }; | |
2897 | ||
a76b4319 | 2898 | static void |
af558e33 | 2899 | nfsd4_end_grace(void) |
a76b4319 N |
2900 | { |
2901 | dprintk("NFSD: end of grace period\n"); | |
c7b9a459 | 2902 | nfsd4_recdir_purge_old(); |
af558e33 | 2903 | locks_end_grace(&nfsd4_manager); |
e46b498c BF |
2904 | /* |
2905 | * Now that every NFSv4 client has had the chance to recover and | |
2906 | * to see the (possibly new, possibly shorter) lease time, we | |
2907 | * can safely set the next grace time to the current lease time: | |
2908 | */ | |
2909 | nfsd4_grace = nfsd4_lease; | |
a76b4319 N |
2910 | } |
2911 | ||
fd39ca9a | 2912 | static time_t |
1da177e4 LT |
2913 | nfs4_laundromat(void) |
2914 | { | |
2915 | struct nfs4_client *clp; | |
2916 | struct nfs4_stateowner *sop; | |
2917 | struct nfs4_delegation *dp; | |
2918 | struct list_head *pos, *next, reaplist; | |
cf07d2ea BF |
2919 | time_t cutoff = get_seconds() - nfsd4_lease; |
2920 | time_t t, clientid_val = nfsd4_lease; | |
2921 | time_t u, test_val = nfsd4_lease; | |
1da177e4 LT |
2922 | |
2923 | nfs4_lock_state(); | |
2924 | ||
2925 | dprintk("NFSD: laundromat service - starting\n"); | |
af558e33 BF |
2926 | if (locks_in_grace()) |
2927 | nfsd4_end_grace(); | |
36acb66b BH |
2928 | INIT_LIST_HEAD(&reaplist); |
2929 | spin_lock(&client_lock); | |
1da177e4 LT |
2930 | list_for_each_safe(pos, next, &client_lru) { |
2931 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
2932 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { | |
2933 | t = clp->cl_time - cutoff; | |
2934 | if (clientid_val > t) | |
2935 | clientid_val = t; | |
2936 | break; | |
2937 | } | |
d7682988 BH |
2938 | if (atomic_read(&clp->cl_refcount)) { |
2939 | dprintk("NFSD: client in use (clientid %08x)\n", | |
2940 | clp->cl_clientid.cl_id); | |
2941 | continue; | |
2942 | } | |
2943 | unhash_client_locked(clp); | |
2944 | list_add(&clp->cl_lru, &reaplist); | |
36acb66b BH |
2945 | } |
2946 | spin_unlock(&client_lock); | |
2947 | list_for_each_safe(pos, next, &reaplist) { | |
2948 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
1da177e4 LT |
2949 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
2950 | clp->cl_clientid.cl_id); | |
c7b9a459 | 2951 | nfsd4_remove_clid_dir(clp); |
1da177e4 LT |
2952 | expire_client(clp); |
2953 | } | |
1da177e4 LT |
2954 | spin_lock(&recall_lock); |
2955 | list_for_each_safe(pos, next, &del_recall_lru) { | |
2956 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
2957 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { | |
2958 | u = dp->dl_time - cutoff; | |
2959 | if (test_val > u) | |
2960 | test_val = u; | |
2961 | break; | |
2962 | } | |
1da177e4 LT |
2963 | list_move(&dp->dl_recall_lru, &reaplist); |
2964 | } | |
2965 | spin_unlock(&recall_lock); | |
2966 | list_for_each_safe(pos, next, &reaplist) { | |
2967 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
2968 | list_del_init(&dp->dl_recall_lru); | |
2969 | unhash_delegation(dp); | |
2970 | } | |
cf07d2ea | 2971 | test_val = nfsd4_lease; |
1da177e4 LT |
2972 | list_for_each_safe(pos, next, &close_lru) { |
2973 | sop = list_entry(pos, struct nfs4_stateowner, so_close_lru); | |
2974 | if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) { | |
2975 | u = sop->so_time - cutoff; | |
2976 | if (test_val > u) | |
2977 | test_val = u; | |
2978 | break; | |
2979 | } | |
2980 | dprintk("NFSD: purging unused open stateowner (so_id %d)\n", | |
2981 | sop->so_id); | |
f044ff83 | 2982 | release_openowner(sop); |
1da177e4 LT |
2983 | } |
2984 | if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT) | |
2985 | clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT; | |
2986 | nfs4_unlock_state(); | |
2987 | return clientid_val; | |
2988 | } | |
2989 | ||
a254b246 HH |
2990 | static struct workqueue_struct *laundry_wq; |
2991 | static void laundromat_main(struct work_struct *); | |
2992 | static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main); | |
2993 | ||
2994 | static void | |
c4028958 | 2995 | laundromat_main(struct work_struct *not_used) |
1da177e4 LT |
2996 | { |
2997 | time_t t; | |
2998 | ||
2999 | t = nfs4_laundromat(); | |
3000 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); | |
58da282b | 3001 | queue_delayed_work(laundry_wq, &laundromat_work, t*HZ); |
1da177e4 LT |
3002 | } |
3003 | ||
fd39ca9a | 3004 | static struct nfs4_stateowner * |
f8816512 N |
3005 | search_close_lru(u32 st_id, int flags) |
3006 | { | |
1da177e4 LT |
3007 | struct nfs4_stateowner *local = NULL; |
3008 | ||
1da177e4 LT |
3009 | if (flags & CLOSE_STATE) { |
3010 | list_for_each_entry(local, &close_lru, so_close_lru) { | |
3011 | if (local->so_id == st_id) | |
3012 | return local; | |
3013 | } | |
3014 | } | |
3015 | return NULL; | |
3016 | } | |
3017 | ||
3018 | static inline int | |
3019 | nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp) | |
3020 | { | |
f9d7562f | 3021 | return fhp->fh_dentry->d_inode != stp->st_file->fi_inode; |
1da177e4 LT |
3022 | } |
3023 | ||
3024 | static int | |
3025 | STALE_STATEID(stateid_t *stateid) | |
3026 | { | |
e4e83ea4 BF |
3027 | if (stateid->si_boot == boot_time) |
3028 | return 0; | |
3029 | dprintk("NFSD: stale stateid " STATEID_FMT "!\n", | |
8c10cbdb | 3030 | STATEID_VAL(stateid)); |
e4e83ea4 | 3031 | return 1; |
1da177e4 LT |
3032 | } |
3033 | ||
3034 | static inline int | |
3035 | access_permit_read(unsigned long access_bmap) | |
3036 | { | |
3037 | return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) || | |
3038 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) || | |
3039 | test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap); | |
3040 | } | |
3041 | ||
3042 | static inline int | |
3043 | access_permit_write(unsigned long access_bmap) | |
3044 | { | |
3045 | return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) || | |
3046 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap); | |
3047 | } | |
3048 | ||
3049 | static | |
b37ad28b | 3050 | __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags) |
1da177e4 | 3051 | { |
b37ad28b | 3052 | __be32 status = nfserr_openmode; |
1da177e4 | 3053 | |
02921914 BF |
3054 | /* For lock stateid's, we test the parent open, not the lock: */ |
3055 | if (stp->st_openstp) | |
3056 | stp = stp->st_openstp; | |
1da177e4 LT |
3057 | if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap))) |
3058 | goto out; | |
3059 | if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap))) | |
3060 | goto out; | |
3061 | status = nfs_ok; | |
3062 | out: | |
3063 | return status; | |
3064 | } | |
3065 | ||
b37ad28b | 3066 | static inline __be32 |
1da177e4 LT |
3067 | check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags) |
3068 | { | |
203a8c8e | 3069 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
1da177e4 | 3070 | return nfs_ok; |
af558e33 | 3071 | else if (locks_in_grace()) { |
1da177e4 LT |
3072 | /* Answer in remaining cases depends on existance of |
3073 | * conflicting state; so we must wait out the grace period. */ | |
3074 | return nfserr_grace; | |
3075 | } else if (flags & WR_STATE) | |
3076 | return nfs4_share_conflict(current_fh, | |
3077 | NFS4_SHARE_DENY_WRITE); | |
3078 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
3079 | return nfs4_share_conflict(current_fh, | |
3080 | NFS4_SHARE_DENY_READ); | |
3081 | } | |
3082 | ||
3083 | /* | |
3084 | * Allow READ/WRITE during grace period on recovered state only for files | |
3085 | * that are not able to provide mandatory locking. | |
3086 | */ | |
3087 | static inline int | |
18f82731 | 3088 | grace_disallows_io(struct inode *inode) |
1da177e4 | 3089 | { |
203a8c8e | 3090 | return locks_in_grace() && mandatory_lock(inode); |
1da177e4 LT |
3091 | } |
3092 | ||
6668958f | 3093 | static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags) |
0836f587 | 3094 | { |
6668958f AA |
3095 | /* |
3096 | * When sessions are used the stateid generation number is ignored | |
3097 | * when it is zero. | |
3098 | */ | |
3099 | if ((flags & HAS_SESSION) && in->si_generation == 0) | |
3100 | goto out; | |
3101 | ||
0836f587 BF |
3102 | /* If the client sends us a stateid from the future, it's buggy: */ |
3103 | if (in->si_generation > ref->si_generation) | |
3104 | return nfserr_bad_stateid; | |
3105 | /* | |
3106 | * The following, however, can happen. For example, if the | |
3107 | * client sends an open and some IO at the same time, the open | |
3108 | * may bump si_generation while the IO is still in flight. | |
3109 | * Thanks to hard links and renames, the client never knows what | |
3110 | * file an open will affect. So it could avoid that situation | |
3111 | * only by serializing all opens and IO from the same open | |
3112 | * owner. To recover from the old_stateid error, the client | |
3113 | * will just have to retry the IO: | |
3114 | */ | |
3115 | if (in->si_generation < ref->si_generation) | |
3116 | return nfserr_old_stateid; | |
6668958f | 3117 | out: |
0836f587 BF |
3118 | return nfs_ok; |
3119 | } | |
3120 | ||
3e633079 BF |
3121 | static int is_delegation_stateid(stateid_t *stateid) |
3122 | { | |
3123 | return stateid->si_fileid == 0; | |
3124 | } | |
3125 | ||
1da177e4 LT |
3126 | /* |
3127 | * Checks for stateid operations | |
3128 | */ | |
b37ad28b | 3129 | __be32 |
dd453dfd BH |
3130 | nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate, |
3131 | stateid_t *stateid, int flags, struct file **filpp) | |
1da177e4 LT |
3132 | { |
3133 | struct nfs4_stateid *stp = NULL; | |
3134 | struct nfs4_delegation *dp = NULL; | |
dd453dfd | 3135 | struct svc_fh *current_fh = &cstate->current_fh; |
1da177e4 | 3136 | struct inode *ino = current_fh->fh_dentry->d_inode; |
b37ad28b | 3137 | __be32 status; |
1da177e4 | 3138 | |
1da177e4 LT |
3139 | if (filpp) |
3140 | *filpp = NULL; | |
3141 | ||
18f82731 | 3142 | if (grace_disallows_io(ino)) |
1da177e4 LT |
3143 | return nfserr_grace; |
3144 | ||
6668958f AA |
3145 | if (nfsd4_has_session(cstate)) |
3146 | flags |= HAS_SESSION; | |
3147 | ||
1da177e4 LT |
3148 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
3149 | return check_special_stateids(current_fh, stateid, flags); | |
3150 | ||
1da177e4 LT |
3151 | status = nfserr_stale_stateid; |
3152 | if (STALE_STATEID(stateid)) | |
3153 | goto out; | |
3154 | ||
33515142 BF |
3155 | /* |
3156 | * We assume that any stateid that has the current boot time, | |
3157 | * but that we can't find, is expired: | |
3158 | */ | |
3159 | status = nfserr_expired; | |
3e633079 | 3160 | if (is_delegation_stateid(stateid)) { |
a4455be0 | 3161 | dp = find_delegation_stateid(ino, stateid); |
e4e83ea4 | 3162 | if (!dp) |
1da177e4 | 3163 | goto out; |
6668958f AA |
3164 | status = check_stateid_generation(stateid, &dp->dl_stateid, |
3165 | flags); | |
0c2a498f BF |
3166 | if (status) |
3167 | goto out; | |
dc9bf700 BF |
3168 | status = nfs4_check_delegmode(dp, flags); |
3169 | if (status) | |
3170 | goto out; | |
3171 | renew_client(dp->dl_client); | |
43b0178e | 3172 | if (filpp) { |
acfdf5c3 | 3173 | *filpp = dp->dl_file->fi_deleg_file; |
43b0178e DC |
3174 | BUG_ON(!*filpp); |
3175 | } | |
1da177e4 | 3176 | } else { /* open or lock stateid */ |
a4455be0 | 3177 | stp = find_stateid(stateid, flags); |
e4e83ea4 | 3178 | if (!stp) |
1da177e4 | 3179 | goto out; |
33515142 | 3180 | status = nfserr_bad_stateid; |
6150ef0d | 3181 | if (nfs4_check_fh(current_fh, stp)) |
1da177e4 LT |
3182 | goto out; |
3183 | if (!stp->st_stateowner->so_confirmed) | |
3184 | goto out; | |
6668958f AA |
3185 | status = check_stateid_generation(stateid, &stp->st_stateid, |
3186 | flags); | |
0c2a498f BF |
3187 | if (status) |
3188 | goto out; | |
a4455be0 BF |
3189 | status = nfs4_check_openmode(stp, flags); |
3190 | if (status) | |
1da177e4 LT |
3191 | goto out; |
3192 | renew_client(stp->st_stateowner->so_client); | |
f9d7562f BF |
3193 | if (filpp) { |
3194 | if (flags & RD_STATE) | |
3195 | *filpp = find_readable_file(stp->st_file); | |
3196 | else | |
3197 | *filpp = find_writeable_file(stp->st_file); | |
f9d7562f | 3198 | } |
1da177e4 LT |
3199 | } |
3200 | status = nfs_ok; | |
3201 | out: | |
3202 | return status; | |
3203 | } | |
3204 | ||
4c4cd222 N |
3205 | static inline int |
3206 | setlkflg (int type) | |
3207 | { | |
3208 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
3209 | RD_STATE : WR_STATE; | |
3210 | } | |
1da177e4 LT |
3211 | |
3212 | /* | |
3213 | * Checks for sequence id mutating operations. | |
3214 | */ | |
b37ad28b | 3215 | static __be32 |
dd453dfd BH |
3216 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
3217 | stateid_t *stateid, int flags, | |
3218 | struct nfs4_stateowner **sopp, | |
3219 | struct nfs4_stateid **stpp, struct nfsd4_lock *lock) | |
1da177e4 | 3220 | { |
1da177e4 LT |
3221 | struct nfs4_stateid *stp; |
3222 | struct nfs4_stateowner *sop; | |
dd453dfd | 3223 | struct svc_fh *current_fh = &cstate->current_fh; |
0836f587 | 3224 | __be32 status; |
1da177e4 | 3225 | |
8c10cbdb BH |
3226 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
3227 | seqid, STATEID_VAL(stateid)); | |
3a4f98bb | 3228 | |
1da177e4 LT |
3229 | *stpp = NULL; |
3230 | *sopp = NULL; | |
3231 | ||
1da177e4 | 3232 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
2fdada03 | 3233 | dprintk("NFSD: preprocess_seqid_op: magic stateid!\n"); |
3a4f98bb | 3234 | return nfserr_bad_stateid; |
1da177e4 LT |
3235 | } |
3236 | ||
1da177e4 | 3237 | if (STALE_STATEID(stateid)) |
3a4f98bb | 3238 | return nfserr_stale_stateid; |
6668958f AA |
3239 | |
3240 | if (nfsd4_has_session(cstate)) | |
3241 | flags |= HAS_SESSION; | |
3242 | ||
1da177e4 LT |
3243 | /* |
3244 | * We return BAD_STATEID if filehandle doesn't match stateid, | |
3245 | * the confirmed flag is incorrecly set, or the generation | |
3246 | * number is incorrect. | |
1da177e4 | 3247 | */ |
f8816512 N |
3248 | stp = find_stateid(stateid, flags); |
3249 | if (stp == NULL) { | |
3250 | /* | |
3251 | * Also, we should make sure this isn't just the result of | |
3252 | * a replayed close: | |
3253 | */ | |
3254 | sop = search_close_lru(stateid->si_stateownerid, flags); | |
33515142 | 3255 | /* It's not stale; let's assume it's expired: */ |
f8816512 | 3256 | if (sop == NULL) |
33515142 | 3257 | return nfserr_expired; |
f8816512 N |
3258 | *sopp = sop; |
3259 | goto check_replay; | |
3260 | } | |
1da177e4 | 3261 | |
39325bd0 BF |
3262 | *stpp = stp; |
3263 | *sopp = sop = stp->st_stateowner; | |
3264 | ||
4c4cd222 | 3265 | if (lock) { |
4c4cd222 | 3266 | clientid_t *lockclid = &lock->v.new.clientid; |
1da177e4 | 3267 | struct nfs4_client *clp = sop->so_client; |
4c4cd222 | 3268 | int lkflg = 0; |
b37ad28b | 3269 | __be32 status; |
4c4cd222 N |
3270 | |
3271 | lkflg = setlkflg(lock->lk_type); | |
3272 | ||
3273 | if (lock->lk_is_new) { | |
599e0a22 BF |
3274 | if (!sop->so_is_open_owner) |
3275 | return nfserr_bad_stateid; | |
60adfc50 AA |
3276 | if (!(flags & HAS_SESSION) && |
3277 | !same_clid(&clp->cl_clientid, lockclid)) | |
3278 | return nfserr_bad_stateid; | |
599e0a22 BF |
3279 | /* stp is the open stateid */ |
3280 | status = nfs4_check_openmode(stp, lkflg); | |
3281 | if (status) | |
3282 | return status; | |
3283 | } else { | |
3284 | /* stp is the lock stateid */ | |
3285 | status = nfs4_check_openmode(stp->st_openstp, lkflg); | |
3286 | if (status) | |
3287 | return status; | |
4c4cd222 | 3288 | } |
1da177e4 LT |
3289 | } |
3290 | ||
f3362737 | 3291 | if (nfs4_check_fh(current_fh, stp)) { |
2fdada03 | 3292 | dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n"); |
3a4f98bb | 3293 | return nfserr_bad_stateid; |
1da177e4 LT |
3294 | } |
3295 | ||
1da177e4 LT |
3296 | /* |
3297 | * We now validate the seqid and stateid generation numbers. | |
3298 | * For the moment, we ignore the possibility of | |
3299 | * generation number wraparound. | |
3300 | */ | |
6668958f | 3301 | if (!(flags & HAS_SESSION) && seqid != sop->so_seqid) |
1da177e4 LT |
3302 | goto check_replay; |
3303 | ||
3a4f98bb | 3304 | if (sop->so_confirmed && flags & CONFIRM) { |
2fdada03 | 3305 | dprintk("NFSD: preprocess_seqid_op: expected" |
3a4f98bb N |
3306 | " unconfirmed stateowner!\n"); |
3307 | return nfserr_bad_stateid; | |
1da177e4 | 3308 | } |
3a4f98bb | 3309 | if (!sop->so_confirmed && !(flags & CONFIRM)) { |
2fdada03 | 3310 | dprintk("NFSD: preprocess_seqid_op: stateowner not" |
3a4f98bb N |
3311 | " confirmed yet!\n"); |
3312 | return nfserr_bad_stateid; | |
1da177e4 | 3313 | } |
6668958f | 3314 | status = check_stateid_generation(stateid, &stp->st_stateid, flags); |
0836f587 BF |
3315 | if (status) |
3316 | return status; | |
52fd004e | 3317 | renew_client(sop->so_client); |
3a4f98bb | 3318 | return nfs_ok; |
1da177e4 | 3319 | |
1da177e4 | 3320 | check_replay: |
bd9aac52 | 3321 | if (seqid == sop->so_seqid - 1) { |
849823c5 | 3322 | dprintk("NFSD: preprocess_seqid_op: retransmission?\n"); |
1da177e4 | 3323 | /* indicate replay to calling function */ |
a90b061c | 3324 | return nfserr_replay_me; |
1da177e4 | 3325 | } |
2fdada03 | 3326 | dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n", |
3a4f98bb N |
3327 | sop->so_seqid, seqid); |
3328 | *sopp = NULL; | |
3329 | return nfserr_bad_seqid; | |
1da177e4 LT |
3330 | } |
3331 | ||
b37ad28b | 3332 | __be32 |
ca364317 | 3333 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 3334 | struct nfsd4_open_confirm *oc) |
1da177e4 | 3335 | { |
b37ad28b | 3336 | __be32 status; |
1da177e4 LT |
3337 | struct nfs4_stateowner *sop; |
3338 | struct nfs4_stateid *stp; | |
3339 | ||
3340 | dprintk("NFSD: nfsd4_open_confirm on file %.*s\n", | |
ca364317 BF |
3341 | (int)cstate->current_fh.fh_dentry->d_name.len, |
3342 | cstate->current_fh.fh_dentry->d_name.name); | |
1da177e4 | 3343 | |
ca364317 | 3344 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
a8cddc5d BF |
3345 | if (status) |
3346 | return status; | |
1da177e4 LT |
3347 | |
3348 | nfs4_lock_state(); | |
3349 | ||
dd453dfd | 3350 | if ((status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 3351 | oc->oc_seqid, &oc->oc_req_stateid, |
f3362737 | 3352 | CONFIRM | OPEN_STATE, |
1da177e4 LT |
3353 | &oc->oc_stateowner, &stp, NULL))) |
3354 | goto out; | |
3355 | ||
3356 | sop = oc->oc_stateowner; | |
3357 | sop->so_confirmed = 1; | |
3358 | update_stateid(&stp->st_stateid); | |
3359 | memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
8c10cbdb BH |
3360 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
3361 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid)); | |
c7b9a459 N |
3362 | |
3363 | nfsd4_create_clid_dir(sop->so_client); | |
1da177e4 | 3364 | out: |
f2327d9a | 3365 | if (oc->oc_stateowner) { |
1da177e4 | 3366 | nfs4_get_stateowner(oc->oc_stateowner); |
a4f1706a | 3367 | cstate->replay_owner = oc->oc_stateowner; |
f2327d9a | 3368 | } |
1da177e4 LT |
3369 | nfs4_unlock_state(); |
3370 | return status; | |
3371 | } | |
3372 | ||
3373 | ||
3374 | /* | |
3375 | * unset all bits in union bitmap (bmap) that | |
3376 | * do not exist in share (from successful OPEN_DOWNGRADE) | |
3377 | */ | |
3378 | static void | |
3379 | reset_union_bmap_access(unsigned long access, unsigned long *bmap) | |
3380 | { | |
3381 | int i; | |
3382 | for (i = 1; i < 4; i++) { | |
3383 | if ((i & access) != i) | |
3384 | __clear_bit(i, bmap); | |
3385 | } | |
3386 | } | |
3387 | ||
3388 | static void | |
3389 | reset_union_bmap_deny(unsigned long deny, unsigned long *bmap) | |
3390 | { | |
3391 | int i; | |
3392 | for (i = 0; i < 4; i++) { | |
3393 | if ((i & deny) != i) | |
3394 | __clear_bit(i, bmap); | |
3395 | } | |
3396 | } | |
3397 | ||
b37ad28b | 3398 | __be32 |
ca364317 BF |
3399 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
3400 | struct nfsd4_compound_state *cstate, | |
a4f1706a | 3401 | struct nfsd4_open_downgrade *od) |
1da177e4 | 3402 | { |
b37ad28b | 3403 | __be32 status; |
1da177e4 LT |
3404 | struct nfs4_stateid *stp; |
3405 | unsigned int share_access; | |
3406 | ||
3407 | dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", | |
ca364317 BF |
3408 | (int)cstate->current_fh.fh_dentry->d_name.len, |
3409 | cstate->current_fh.fh_dentry->d_name.name); | |
1da177e4 | 3410 | |
d87a8ade | 3411 | if (!access_valid(od->od_share_access, cstate->minorversion) |
ba5a6a19 | 3412 | || !deny_valid(od->od_share_deny)) |
1da177e4 LT |
3413 | return nfserr_inval; |
3414 | ||
3415 | nfs4_lock_state(); | |
dd453dfd | 3416 | if ((status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 3417 | od->od_seqid, |
1da177e4 | 3418 | &od->od_stateid, |
f3362737 | 3419 | OPEN_STATE, |
1da177e4 LT |
3420 | &od->od_stateowner, &stp, NULL))) |
3421 | goto out; | |
3422 | ||
3423 | status = nfserr_inval; | |
3424 | if (!test_bit(od->od_share_access, &stp->st_access_bmap)) { | |
3425 | dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n", | |
3426 | stp->st_access_bmap, od->od_share_access); | |
3427 | goto out; | |
3428 | } | |
3429 | if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) { | |
3430 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", | |
3431 | stp->st_deny_bmap, od->od_share_deny); | |
3432 | goto out; | |
3433 | } | |
3434 | set_access(&share_access, stp->st_access_bmap); | |
f9d7562f | 3435 | nfs4_file_downgrade(stp->st_file, share_access & ~od->od_share_access); |
1da177e4 LT |
3436 | |
3437 | reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap); | |
3438 | reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap); | |
3439 | ||
3440 | update_stateid(&stp->st_stateid); | |
3441 | memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
3442 | status = nfs_ok; | |
3443 | out: | |
f2327d9a | 3444 | if (od->od_stateowner) { |
1da177e4 | 3445 | nfs4_get_stateowner(od->od_stateowner); |
a4f1706a | 3446 | cstate->replay_owner = od->od_stateowner; |
f2327d9a | 3447 | } |
1da177e4 LT |
3448 | nfs4_unlock_state(); |
3449 | return status; | |
3450 | } | |
3451 | ||
3452 | /* | |
3453 | * nfs4_unlock_state() called after encode | |
3454 | */ | |
b37ad28b | 3455 | __be32 |
ca364317 | 3456 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 3457 | struct nfsd4_close *close) |
1da177e4 | 3458 | { |
b37ad28b | 3459 | __be32 status; |
1da177e4 LT |
3460 | struct nfs4_stateid *stp; |
3461 | ||
3462 | dprintk("NFSD: nfsd4_close on file %.*s\n", | |
ca364317 BF |
3463 | (int)cstate->current_fh.fh_dentry->d_name.len, |
3464 | cstate->current_fh.fh_dentry->d_name.name); | |
1da177e4 LT |
3465 | |
3466 | nfs4_lock_state(); | |
3467 | /* check close_lru for replay */ | |
dd453dfd | 3468 | if ((status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 3469 | close->cl_seqid, |
1da177e4 | 3470 | &close->cl_stateid, |
f3362737 | 3471 | OPEN_STATE | CLOSE_STATE, |
1da177e4 LT |
3472 | &close->cl_stateowner, &stp, NULL))) |
3473 | goto out; | |
1da177e4 LT |
3474 | status = nfs_ok; |
3475 | update_stateid(&stp->st_stateid); | |
3476 | memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
3477 | ||
04ef5954 | 3478 | /* release_stateid() calls nfsd_close() if needed */ |
2283963f | 3479 | release_open_stateid(stp); |
04ef5954 BF |
3480 | |
3481 | /* place unused nfs4_stateowners on so_close_lru list to be | |
3482 | * released by the laundromat service after the lease period | |
3483 | * to enable us to handle CLOSE replay | |
3484 | */ | |
3485 | if (list_empty(&close->cl_stateowner->so_stateids)) | |
3486 | move_to_close_lru(close->cl_stateowner); | |
1da177e4 | 3487 | out: |
f2327d9a | 3488 | if (close->cl_stateowner) { |
1da177e4 | 3489 | nfs4_get_stateowner(close->cl_stateowner); |
a4f1706a | 3490 | cstate->replay_owner = close->cl_stateowner; |
f2327d9a | 3491 | } |
1da177e4 LT |
3492 | nfs4_unlock_state(); |
3493 | return status; | |
3494 | } | |
3495 | ||
b37ad28b | 3496 | __be32 |
ca364317 BF |
3497 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3498 | struct nfsd4_delegreturn *dr) | |
1da177e4 | 3499 | { |
203a8c8e BF |
3500 | struct nfs4_delegation *dp; |
3501 | stateid_t *stateid = &dr->dr_stateid; | |
3502 | struct inode *inode; | |
b37ad28b | 3503 | __be32 status; |
6668958f | 3504 | int flags = 0; |
1da177e4 | 3505 | |
ca364317 | 3506 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
203a8c8e BF |
3507 | return status; |
3508 | inode = cstate->current_fh.fh_dentry->d_inode; | |
1da177e4 | 3509 | |
6668958f AA |
3510 | if (nfsd4_has_session(cstate)) |
3511 | flags |= HAS_SESSION; | |
1da177e4 | 3512 | nfs4_lock_state(); |
203a8c8e BF |
3513 | status = nfserr_bad_stateid; |
3514 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) | |
3515 | goto out; | |
3516 | status = nfserr_stale_stateid; | |
3517 | if (STALE_STATEID(stateid)) | |
3518 | goto out; | |
7e0f7cf5 | 3519 | status = nfserr_bad_stateid; |
203a8c8e BF |
3520 | if (!is_delegation_stateid(stateid)) |
3521 | goto out; | |
33515142 | 3522 | status = nfserr_expired; |
203a8c8e | 3523 | dp = find_delegation_stateid(inode, stateid); |
e4e83ea4 | 3524 | if (!dp) |
203a8c8e | 3525 | goto out; |
6668958f | 3526 | status = check_stateid_generation(stateid, &dp->dl_stateid, flags); |
203a8c8e BF |
3527 | if (status) |
3528 | goto out; | |
3529 | renew_client(dp->dl_client); | |
3530 | ||
3531 | unhash_delegation(dp); | |
1da177e4 | 3532 | out: |
203a8c8e BF |
3533 | nfs4_unlock_state(); |
3534 | ||
1da177e4 LT |
3535 | return status; |
3536 | } | |
3537 | ||
3538 | ||
3539 | /* | |
3540 | * Lock owner state (byte-range locks) | |
3541 | */ | |
3542 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) | |
3543 | #define LOCK_HASH_BITS 8 | |
3544 | #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS) | |
3545 | #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1) | |
3546 | ||
87df4de8 BH |
3547 | static inline u64 |
3548 | end_offset(u64 start, u64 len) | |
3549 | { | |
3550 | u64 end; | |
3551 | ||
3552 | end = start + len; | |
3553 | return end >= start ? end: NFS4_MAX_UINT64; | |
3554 | } | |
3555 | ||
3556 | /* last octet in a range */ | |
3557 | static inline u64 | |
3558 | last_byte_offset(u64 start, u64 len) | |
3559 | { | |
3560 | u64 end; | |
3561 | ||
3562 | BUG_ON(!len); | |
3563 | end = start + len; | |
3564 | return end > start ? end - 1: NFS4_MAX_UINT64; | |
3565 | } | |
3566 | ||
1da177e4 LT |
3567 | #define lockownerid_hashval(id) \ |
3568 | ((id) & LOCK_HASH_MASK) | |
3569 | ||
3570 | static inline unsigned int | |
3571 | lock_ownerstr_hashval(struct inode *inode, u32 cl_id, | |
3572 | struct xdr_netobj *ownername) | |
3573 | { | |
3574 | return (file_hashval(inode) + cl_id | |
3575 | + opaque_hashval(ownername->data, ownername->len)) | |
3576 | & LOCK_HASH_MASK; | |
3577 | } | |
3578 | ||
3579 | static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE]; | |
3580 | static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE]; | |
3581 | static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE]; | |
3582 | ||
fd39ca9a | 3583 | static struct nfs4_stateid * |
1da177e4 LT |
3584 | find_stateid(stateid_t *stid, int flags) |
3585 | { | |
9346eff0 | 3586 | struct nfs4_stateid *local; |
1da177e4 LT |
3587 | u32 st_id = stid->si_stateownerid; |
3588 | u32 f_id = stid->si_fileid; | |
3589 | unsigned int hashval; | |
3590 | ||
3591 | dprintk("NFSD: find_stateid flags 0x%x\n",flags); | |
9346eff0 | 3592 | if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) { |
1da177e4 LT |
3593 | hashval = stateid_hashval(st_id, f_id); |
3594 | list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) { | |
3595 | if ((local->st_stateid.si_stateownerid == st_id) && | |
3596 | (local->st_stateid.si_fileid == f_id)) | |
3597 | return local; | |
3598 | } | |
3599 | } | |
9346eff0 KK |
3600 | |
3601 | if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) { | |
1da177e4 LT |
3602 | hashval = stateid_hashval(st_id, f_id); |
3603 | list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) { | |
3604 | if ((local->st_stateid.si_stateownerid == st_id) && | |
3605 | (local->st_stateid.si_fileid == f_id)) | |
3606 | return local; | |
3607 | } | |
849823c5 | 3608 | } |
1da177e4 LT |
3609 | return NULL; |
3610 | } | |
3611 | ||
3612 | static struct nfs4_delegation * | |
3613 | find_delegation_stateid(struct inode *ino, stateid_t *stid) | |
3614 | { | |
13cd2184 N |
3615 | struct nfs4_file *fp; |
3616 | struct nfs4_delegation *dl; | |
1da177e4 | 3617 | |
8c10cbdb BH |
3618 | dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__, |
3619 | STATEID_VAL(stid)); | |
1da177e4 | 3620 | |
1da177e4 | 3621 | fp = find_file(ino); |
13cd2184 N |
3622 | if (!fp) |
3623 | return NULL; | |
3624 | dl = find_delegation_file(fp, stid); | |
3625 | put_nfs4_file(fp); | |
3626 | return dl; | |
1da177e4 LT |
3627 | } |
3628 | ||
3629 | /* | |
3630 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
3631 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
3632 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
3633 | * locking, this prevents us from being completely protocol-compliant. The | |
3634 | * real solution to this problem is to start using unsigned file offsets in | |
3635 | * the VFS, but this is a very deep change! | |
3636 | */ | |
3637 | static inline void | |
3638 | nfs4_transform_lock_offset(struct file_lock *lock) | |
3639 | { | |
3640 | if (lock->fl_start < 0) | |
3641 | lock->fl_start = OFFSET_MAX; | |
3642 | if (lock->fl_end < 0) | |
3643 | lock->fl_end = OFFSET_MAX; | |
3644 | } | |
3645 | ||
d5b9026a N |
3646 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
3647 | * as a unique cookie to identify our (NFSv4's) posix locks. */ | |
7b021967 | 3648 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
d5b9026a | 3649 | }; |
1da177e4 LT |
3650 | |
3651 | static inline void | |
3652 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
3653 | { | |
d5b9026a | 3654 | struct nfs4_stateowner *sop; |
1da177e4 | 3655 | |
d5b9026a N |
3656 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
3657 | sop = (struct nfs4_stateowner *) fl->fl_owner; | |
1da177e4 LT |
3658 | kref_get(&sop->so_ref); |
3659 | deny->ld_sop = sop; | |
3660 | deny->ld_clientid = sop->so_client->cl_clientid; | |
d5b9026a N |
3661 | } else { |
3662 | deny->ld_sop = NULL; | |
3663 | deny->ld_clientid.cl_boot = 0; | |
3664 | deny->ld_clientid.cl_id = 0; | |
1da177e4 LT |
3665 | } |
3666 | deny->ld_start = fl->fl_start; | |
87df4de8 BH |
3667 | deny->ld_length = NFS4_MAX_UINT64; |
3668 | if (fl->fl_end != NFS4_MAX_UINT64) | |
1da177e4 LT |
3669 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
3670 | deny->ld_type = NFS4_READ_LT; | |
3671 | if (fl->fl_type != F_RDLCK) | |
3672 | deny->ld_type = NFS4_WRITE_LT; | |
3673 | } | |
3674 | ||
1da177e4 LT |
3675 | static struct nfs4_stateowner * |
3676 | find_lockstateowner_str(struct inode *inode, clientid_t *clid, | |
3677 | struct xdr_netobj *owner) | |
3678 | { | |
3679 | unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner); | |
3680 | struct nfs4_stateowner *op; | |
3681 | ||
3682 | list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) { | |
599e0a22 | 3683 | if (same_owner_str(op, owner, clid)) |
1da177e4 LT |
3684 | return op; |
3685 | } | |
3686 | return NULL; | |
3687 | } | |
3688 | ||
3689 | /* | |
3690 | * Alloc a lock owner structure. | |
3691 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
3692 | * occured. | |
3693 | * | |
3694 | * strhashval = lock_ownerstr_hashval | |
1da177e4 LT |
3695 | */ |
3696 | ||
3697 | static struct nfs4_stateowner * | |
3698 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) { | |
3699 | struct nfs4_stateowner *sop; | |
3700 | struct nfs4_replay *rp; | |
3701 | unsigned int idhashval; | |
3702 | ||
3703 | if (!(sop = alloc_stateowner(&lock->lk_new_owner))) | |
3704 | return NULL; | |
3705 | idhashval = lockownerid_hashval(current_ownerid); | |
3706 | INIT_LIST_HEAD(&sop->so_idhash); | |
3707 | INIT_LIST_HEAD(&sop->so_strhash); | |
3708 | INIT_LIST_HEAD(&sop->so_perclient); | |
ea1da636 N |
3709 | INIT_LIST_HEAD(&sop->so_stateids); |
3710 | INIT_LIST_HEAD(&sop->so_perstateid); | |
1da177e4 LT |
3711 | INIT_LIST_HEAD(&sop->so_close_lru); /* not used */ |
3712 | sop->so_time = 0; | |
3713 | list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]); | |
3714 | list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]); | |
ea1da636 | 3715 | list_add(&sop->so_perstateid, &open_stp->st_lockowners); |
1da177e4 LT |
3716 | sop->so_is_open_owner = 0; |
3717 | sop->so_id = current_ownerid++; | |
3718 | sop->so_client = clp; | |
b59e3c0e NB |
3719 | /* It is the openowner seqid that will be incremented in encode in the |
3720 | * case of new lockowners; so increment the lock seqid manually: */ | |
3721 | sop->so_seqid = lock->lk_new_lock_seqid + 1; | |
1da177e4 LT |
3722 | sop->so_confirmed = 1; |
3723 | rp = &sop->so_replay; | |
de1ae286 | 3724 | rp->rp_status = nfserr_serverfault; |
1da177e4 LT |
3725 | rp->rp_buflen = 0; |
3726 | rp->rp_buf = rp->rp_ibuf; | |
3727 | return sop; | |
3728 | } | |
3729 | ||
fd39ca9a | 3730 | static struct nfs4_stateid * |
1da177e4 LT |
3731 | alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp) |
3732 | { | |
3733 | struct nfs4_stateid *stp; | |
3734 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); | |
3735 | ||
5ac049ac N |
3736 | stp = nfs4_alloc_stateid(); |
3737 | if (stp == NULL) | |
1da177e4 LT |
3738 | goto out; |
3739 | INIT_LIST_HEAD(&stp->st_hash); | |
3740 | INIT_LIST_HEAD(&stp->st_perfile); | |
ea1da636 N |
3741 | INIT_LIST_HEAD(&stp->st_perstateowner); |
3742 | INIT_LIST_HEAD(&stp->st_lockowners); /* not used */ | |
1da177e4 | 3743 | list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]); |
8beefa24 | 3744 | list_add(&stp->st_perfile, &fp->fi_stateids); |
ea1da636 | 3745 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
1da177e4 | 3746 | stp->st_stateowner = sop; |
13cd2184 | 3747 | get_nfs4_file(fp); |
1da177e4 | 3748 | stp->st_file = fp; |
e4e83ea4 | 3749 | stp->st_stateid.si_boot = boot_time; |
1da177e4 LT |
3750 | stp->st_stateid.si_stateownerid = sop->so_id; |
3751 | stp->st_stateid.si_fileid = fp->fi_id; | |
3752 | stp->st_stateid.si_generation = 0; | |
0997b173 | 3753 | stp->st_access_bmap = 0; |
1da177e4 | 3754 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
4c4cd222 | 3755 | stp->st_openstp = open_stp; |
1da177e4 LT |
3756 | |
3757 | out: | |
3758 | return stp; | |
3759 | } | |
3760 | ||
fd39ca9a | 3761 | static int |
1da177e4 LT |
3762 | check_lock_length(u64 offset, u64 length) |
3763 | { | |
87df4de8 | 3764 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
1da177e4 LT |
3765 | LOFF_OVERFLOW(offset, length))); |
3766 | } | |
3767 | ||
0997b173 BF |
3768 | static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access) |
3769 | { | |
3770 | struct nfs4_file *fp = lock_stp->st_file; | |
3771 | int oflag = nfs4_access_to_omode(access); | |
3772 | ||
3773 | if (test_bit(access, &lock_stp->st_access_bmap)) | |
3774 | return; | |
3775 | nfs4_file_get_access(fp, oflag); | |
3776 | __set_bit(access, &lock_stp->st_access_bmap); | |
3777 | } | |
3778 | ||
1da177e4 LT |
3779 | /* |
3780 | * LOCK operation | |
3781 | */ | |
b37ad28b | 3782 | __be32 |
ca364317 | 3783 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 3784 | struct nfsd4_lock *lock) |
1da177e4 | 3785 | { |
3e9e3dbe | 3786 | struct nfs4_stateowner *open_sop = NULL; |
b59e3c0e | 3787 | struct nfs4_stateowner *lock_sop = NULL; |
1da177e4 | 3788 | struct nfs4_stateid *lock_stp; |
7d947842 BF |
3789 | struct nfs4_file *fp; |
3790 | struct file *filp = NULL; | |
1da177e4 | 3791 | struct file_lock file_lock; |
8dc7c311 | 3792 | struct file_lock conflock; |
b37ad28b | 3793 | __be32 status = 0; |
1da177e4 | 3794 | unsigned int strhashval; |
b8dd7b9a | 3795 | int err; |
1da177e4 LT |
3796 | |
3797 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
3798 | (long long) lock->lk_offset, | |
3799 | (long long) lock->lk_length); | |
3800 | ||
1da177e4 LT |
3801 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
3802 | return nfserr_inval; | |
3803 | ||
ca364317 | 3804 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
8837abca | 3805 | S_IFREG, NFSD_MAY_LOCK))) { |
a6f6ef2f AA |
3806 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
3807 | return status; | |
3808 | } | |
3809 | ||
1da177e4 LT |
3810 | nfs4_lock_state(); |
3811 | ||
3812 | if (lock->lk_is_new) { | |
893f8770 N |
3813 | /* |
3814 | * Client indicates that this is a new lockowner. | |
3815 | * Use open owner and open stateid to create lock owner and | |
3816 | * lock stateid. | |
3817 | */ | |
1da177e4 | 3818 | struct nfs4_stateid *open_stp = NULL; |
1da177e4 LT |
3819 | |
3820 | status = nfserr_stale_clientid; | |
60adfc50 AA |
3821 | if (!nfsd4_has_session(cstate) && |
3822 | STALE_CLIENTID(&lock->lk_new_clientid)) | |
1da177e4 | 3823 | goto out; |
1da177e4 | 3824 | |
1da177e4 | 3825 | /* validate and update open stateid and open seqid */ |
dd453dfd | 3826 | status = nfs4_preprocess_seqid_op(cstate, |
1da177e4 LT |
3827 | lock->lk_new_open_seqid, |
3828 | &lock->lk_new_open_stateid, | |
f3362737 | 3829 | OPEN_STATE, |
3a65588a | 3830 | &lock->lk_replay_owner, &open_stp, |
b59e3c0e | 3831 | lock); |
37515177 | 3832 | if (status) |
1da177e4 | 3833 | goto out; |
3a65588a | 3834 | open_sop = lock->lk_replay_owner; |
1da177e4 LT |
3835 | /* create lockowner and lock stateid */ |
3836 | fp = open_stp->st_file; | |
3837 | strhashval = lock_ownerstr_hashval(fp->fi_inode, | |
3838 | open_sop->so_client->cl_clientid.cl_id, | |
3839 | &lock->v.new.owner); | |
3e9e3dbe N |
3840 | /* XXX: Do we need to check for duplicate stateowners on |
3841 | * the same file, or should they just be allowed (and | |
3842 | * create new stateids)? */ | |
1da177e4 | 3843 | status = nfserr_resource; |
b59e3c0e NB |
3844 | lock_sop = alloc_init_lock_stateowner(strhashval, |
3845 | open_sop->so_client, open_stp, lock); | |
3846 | if (lock_sop == NULL) | |
1da177e4 | 3847 | goto out; |
b59e3c0e | 3848 | lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp); |
8a280510 | 3849 | if (lock_stp == NULL) |
1da177e4 | 3850 | goto out; |
1da177e4 LT |
3851 | } else { |
3852 | /* lock (lock owner + lock stateid) already exists */ | |
dd453dfd | 3853 | status = nfs4_preprocess_seqid_op(cstate, |
1da177e4 LT |
3854 | lock->lk_old_lock_seqid, |
3855 | &lock->lk_old_lock_stateid, | |
f3362737 | 3856 | LOCK_STATE, |
3a65588a | 3857 | &lock->lk_replay_owner, &lock_stp, lock); |
1da177e4 LT |
3858 | if (status) |
3859 | goto out; | |
3a65588a | 3860 | lock_sop = lock->lk_replay_owner; |
7d947842 | 3861 | fp = lock_stp->st_file; |
1da177e4 | 3862 | } |
3a65588a | 3863 | /* lock->lk_replay_owner and lock_stp have been created or found */ |
1da177e4 | 3864 | |
0dd395dc | 3865 | status = nfserr_grace; |
af558e33 | 3866 | if (locks_in_grace() && !lock->lk_reclaim) |
0dd395dc N |
3867 | goto out; |
3868 | status = nfserr_no_grace; | |
af558e33 | 3869 | if (!locks_in_grace() && lock->lk_reclaim) |
0dd395dc N |
3870 | goto out; |
3871 | ||
1da177e4 LT |
3872 | locks_init_lock(&file_lock); |
3873 | switch (lock->lk_type) { | |
3874 | case NFS4_READ_LT: | |
3875 | case NFS4_READW_LT: | |
0997b173 BF |
3876 | filp = find_readable_file(lock_stp->st_file); |
3877 | if (filp) | |
3878 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); | |
1da177e4 | 3879 | file_lock.fl_type = F_RDLCK; |
529d7b2a | 3880 | break; |
1da177e4 LT |
3881 | case NFS4_WRITE_LT: |
3882 | case NFS4_WRITEW_LT: | |
0997b173 BF |
3883 | filp = find_writeable_file(lock_stp->st_file); |
3884 | if (filp) | |
3885 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); | |
1da177e4 | 3886 | file_lock.fl_type = F_WRLCK; |
529d7b2a | 3887 | break; |
1da177e4 LT |
3888 | default: |
3889 | status = nfserr_inval; | |
3890 | goto out; | |
3891 | } | |
f9d7562f BF |
3892 | if (!filp) { |
3893 | status = nfserr_openmode; | |
3894 | goto out; | |
3895 | } | |
b59e3c0e | 3896 | file_lock.fl_owner = (fl_owner_t)lock_sop; |
1da177e4 LT |
3897 | file_lock.fl_pid = current->tgid; |
3898 | file_lock.fl_file = filp; | |
3899 | file_lock.fl_flags = FL_POSIX; | |
d5b9026a | 3900 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
1da177e4 LT |
3901 | |
3902 | file_lock.fl_start = lock->lk_offset; | |
87df4de8 | 3903 | file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
1da177e4 LT |
3904 | nfs4_transform_lock_offset(&file_lock); |
3905 | ||
3906 | /* | |
3907 | * Try to lock the file in the VFS. | |
3908 | * Note: locks.c uses the BKL to protect the inode's lock list. | |
3909 | */ | |
3910 | ||
529d7b2a | 3911 | err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock); |
b8dd7b9a | 3912 | switch (-err) { |
1da177e4 LT |
3913 | case 0: /* success! */ |
3914 | update_stateid(&lock_stp->st_stateid); | |
3915 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, | |
3916 | sizeof(stateid_t)); | |
b8dd7b9a | 3917 | status = 0; |
eb76b3fd AA |
3918 | break; |
3919 | case (EAGAIN): /* conflock holds conflicting lock */ | |
3920 | status = nfserr_denied; | |
3921 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
3922 | nfs4_set_lock_denied(&conflock, &lock->lk_denied); | |
3923 | break; | |
1da177e4 LT |
3924 | case (EDEADLK): |
3925 | status = nfserr_deadlock; | |
eb76b3fd | 3926 | break; |
1da177e4 | 3927 | default: |
fd85b817 | 3928 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
eb76b3fd AA |
3929 | status = nfserr_resource; |
3930 | break; | |
1da177e4 | 3931 | } |
1da177e4 | 3932 | out: |
8a280510 | 3933 | if (status && lock->lk_is_new && lock_sop) |
f044ff83 | 3934 | release_lockowner(lock_sop); |
3a65588a BF |
3935 | if (lock->lk_replay_owner) { |
3936 | nfs4_get_stateowner(lock->lk_replay_owner); | |
a4f1706a | 3937 | cstate->replay_owner = lock->lk_replay_owner; |
f2327d9a | 3938 | } |
1da177e4 LT |
3939 | nfs4_unlock_state(); |
3940 | return status; | |
3941 | } | |
3942 | ||
55ef1274 BF |
3943 | /* |
3944 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, | |
3945 | * so we do a temporary open here just to get an open file to pass to | |
3946 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an | |
3947 | * inode operation.) | |
3948 | */ | |
3949 | static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) | |
3950 | { | |
3951 | struct file *file; | |
3952 | int err; | |
3953 | ||
3954 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); | |
3955 | if (err) | |
3956 | return err; | |
3957 | err = vfs_test_lock(file, lock); | |
3958 | nfsd_close(file); | |
3959 | return err; | |
3960 | } | |
3961 | ||
1da177e4 LT |
3962 | /* |
3963 | * LOCKT operation | |
3964 | */ | |
b37ad28b | 3965 | __be32 |
ca364317 BF |
3966 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3967 | struct nfsd4_lockt *lockt) | |
1da177e4 LT |
3968 | { |
3969 | struct inode *inode; | |
1da177e4 | 3970 | struct file_lock file_lock; |
fd85b817 | 3971 | int error; |
b37ad28b | 3972 | __be32 status; |
1da177e4 | 3973 | |
af558e33 | 3974 | if (locks_in_grace()) |
1da177e4 LT |
3975 | return nfserr_grace; |
3976 | ||
3977 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
3978 | return nfserr_inval; | |
3979 | ||
3980 | lockt->lt_stateowner = NULL; | |
3981 | nfs4_lock_state(); | |
3982 | ||
3983 | status = nfserr_stale_clientid; | |
60adfc50 | 3984 | if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid)) |
1da177e4 | 3985 | goto out; |
1da177e4 | 3986 | |
ca364317 | 3987 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) { |
849823c5 | 3988 | dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n"); |
1da177e4 LT |
3989 | if (status == nfserr_symlink) |
3990 | status = nfserr_inval; | |
3991 | goto out; | |
3992 | } | |
3993 | ||
ca364317 | 3994 | inode = cstate->current_fh.fh_dentry->d_inode; |
1da177e4 LT |
3995 | locks_init_lock(&file_lock); |
3996 | switch (lockt->lt_type) { | |
3997 | case NFS4_READ_LT: | |
3998 | case NFS4_READW_LT: | |
3999 | file_lock.fl_type = F_RDLCK; | |
4000 | break; | |
4001 | case NFS4_WRITE_LT: | |
4002 | case NFS4_WRITEW_LT: | |
4003 | file_lock.fl_type = F_WRLCK; | |
4004 | break; | |
4005 | default: | |
2fdada03 | 4006 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
1da177e4 LT |
4007 | status = nfserr_inval; |
4008 | goto out; | |
4009 | } | |
4010 | ||
4011 | lockt->lt_stateowner = find_lockstateowner_str(inode, | |
4012 | &lockt->lt_clientid, &lockt->lt_owner); | |
4013 | if (lockt->lt_stateowner) | |
4014 | file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner; | |
4015 | file_lock.fl_pid = current->tgid; | |
4016 | file_lock.fl_flags = FL_POSIX; | |
4017 | ||
4018 | file_lock.fl_start = lockt->lt_offset; | |
87df4de8 | 4019 | file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); |
1da177e4 LT |
4020 | |
4021 | nfs4_transform_lock_offset(&file_lock); | |
4022 | ||
1da177e4 | 4023 | status = nfs_ok; |
55ef1274 | 4024 | error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock); |
fd85b817 ME |
4025 | if (error) { |
4026 | status = nfserrno(error); | |
4027 | goto out; | |
4028 | } | |
9d6a8c5c | 4029 | if (file_lock.fl_type != F_UNLCK) { |
1da177e4 | 4030 | status = nfserr_denied; |
9d6a8c5c | 4031 | nfs4_set_lock_denied(&file_lock, &lockt->lt_denied); |
1da177e4 LT |
4032 | } |
4033 | out: | |
4034 | nfs4_unlock_state(); | |
4035 | return status; | |
4036 | } | |
4037 | ||
b37ad28b | 4038 | __be32 |
ca364317 | 4039 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 4040 | struct nfsd4_locku *locku) |
1da177e4 LT |
4041 | { |
4042 | struct nfs4_stateid *stp; | |
4043 | struct file *filp = NULL; | |
4044 | struct file_lock file_lock; | |
b37ad28b | 4045 | __be32 status; |
b8dd7b9a | 4046 | int err; |
1da177e4 LT |
4047 | |
4048 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", | |
4049 | (long long) locku->lu_offset, | |
4050 | (long long) locku->lu_length); | |
4051 | ||
4052 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
4053 | return nfserr_inval; | |
4054 | ||
4055 | nfs4_lock_state(); | |
4056 | ||
dd453dfd | 4057 | if ((status = nfs4_preprocess_seqid_op(cstate, |
1da177e4 LT |
4058 | locku->lu_seqid, |
4059 | &locku->lu_stateid, | |
f3362737 | 4060 | LOCK_STATE, |
1da177e4 LT |
4061 | &locku->lu_stateowner, &stp, NULL))) |
4062 | goto out; | |
4063 | ||
f9d7562f BF |
4064 | filp = find_any_file(stp->st_file); |
4065 | if (!filp) { | |
4066 | status = nfserr_lock_range; | |
4067 | goto out; | |
4068 | } | |
1da177e4 LT |
4069 | BUG_ON(!filp); |
4070 | locks_init_lock(&file_lock); | |
4071 | file_lock.fl_type = F_UNLCK; | |
4072 | file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner; | |
4073 | file_lock.fl_pid = current->tgid; | |
4074 | file_lock.fl_file = filp; | |
4075 | file_lock.fl_flags = FL_POSIX; | |
d5b9026a | 4076 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
1da177e4 LT |
4077 | file_lock.fl_start = locku->lu_offset; |
4078 | ||
87df4de8 | 4079 | file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length); |
1da177e4 LT |
4080 | nfs4_transform_lock_offset(&file_lock); |
4081 | ||
4082 | /* | |
4083 | * Try to unlock the file in the VFS. | |
4084 | */ | |
fd85b817 | 4085 | err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL); |
b8dd7b9a | 4086 | if (err) { |
fd85b817 | 4087 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
1da177e4 LT |
4088 | goto out_nfserr; |
4089 | } | |
4090 | /* | |
4091 | * OK, unlock succeeded; the only thing left to do is update the stateid. | |
4092 | */ | |
4093 | update_stateid(&stp->st_stateid); | |
4094 | memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
4095 | ||
4096 | out: | |
f2327d9a | 4097 | if (locku->lu_stateowner) { |
1da177e4 | 4098 | nfs4_get_stateowner(locku->lu_stateowner); |
a4f1706a | 4099 | cstate->replay_owner = locku->lu_stateowner; |
f2327d9a | 4100 | } |
1da177e4 LT |
4101 | nfs4_unlock_state(); |
4102 | return status; | |
4103 | ||
4104 | out_nfserr: | |
b8dd7b9a | 4105 | status = nfserrno(err); |
1da177e4 LT |
4106 | goto out; |
4107 | } | |
4108 | ||
4109 | /* | |
4110 | * returns | |
4111 | * 1: locks held by lockowner | |
4112 | * 0: no locks held by lockowner | |
4113 | */ | |
4114 | static int | |
f9d7562f | 4115 | check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner) |
1da177e4 LT |
4116 | { |
4117 | struct file_lock **flpp; | |
f9d7562f | 4118 | struct inode *inode = filp->fi_inode; |
1da177e4 LT |
4119 | int status = 0; |
4120 | ||
b89f4321 | 4121 | lock_flocks(); |
1da177e4 | 4122 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
796dadfd | 4123 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
1da177e4 LT |
4124 | status = 1; |
4125 | goto out; | |
796dadfd | 4126 | } |
1da177e4 LT |
4127 | } |
4128 | out: | |
b89f4321 | 4129 | unlock_flocks(); |
1da177e4 LT |
4130 | return status; |
4131 | } | |
4132 | ||
b37ad28b | 4133 | __be32 |
b591480b BF |
4134 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
4135 | struct nfsd4_compound_state *cstate, | |
4136 | struct nfsd4_release_lockowner *rlockowner) | |
1da177e4 LT |
4137 | { |
4138 | clientid_t *clid = &rlockowner->rl_clientid; | |
3e9e3dbe N |
4139 | struct nfs4_stateowner *sop; |
4140 | struct nfs4_stateid *stp; | |
1da177e4 | 4141 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
3e9e3dbe N |
4142 | struct list_head matches; |
4143 | int i; | |
b37ad28b | 4144 | __be32 status; |
1da177e4 LT |
4145 | |
4146 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
4147 | clid->cl_boot, clid->cl_id); | |
4148 | ||
4149 | /* XXX check for lease expiration */ | |
4150 | ||
4151 | status = nfserr_stale_clientid; | |
849823c5 | 4152 | if (STALE_CLIENTID(clid)) |
1da177e4 | 4153 | return status; |
1da177e4 LT |
4154 | |
4155 | nfs4_lock_state(); | |
4156 | ||
3e9e3dbe N |
4157 | status = nfserr_locks_held; |
4158 | /* XXX: we're doing a linear search through all the lockowners. | |
4159 | * Yipes! For now we'll just hope clients aren't really using | |
4160 | * release_lockowner much, but eventually we have to fix these | |
4161 | * data structures. */ | |
4162 | INIT_LIST_HEAD(&matches); | |
4163 | for (i = 0; i < LOCK_HASH_SIZE; i++) { | |
4164 | list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) { | |
599e0a22 | 4165 | if (!same_owner_str(sop, owner, clid)) |
3e9e3dbe N |
4166 | continue; |
4167 | list_for_each_entry(stp, &sop->so_stateids, | |
4168 | st_perstateowner) { | |
f9d7562f | 4169 | if (check_for_locks(stp->st_file, sop)) |
3e9e3dbe N |
4170 | goto out; |
4171 | /* Note: so_perclient unused for lockowners, | |
4172 | * so it's OK to fool with here. */ | |
4173 | list_add(&sop->so_perclient, &matches); | |
4174 | } | |
1da177e4 | 4175 | } |
3e9e3dbe N |
4176 | } |
4177 | /* Clients probably won't expect us to return with some (but not all) | |
4178 | * of the lockowner state released; so don't release any until all | |
4179 | * have been checked. */ | |
4180 | status = nfs_ok; | |
0fa822e4 N |
4181 | while (!list_empty(&matches)) { |
4182 | sop = list_entry(matches.next, struct nfs4_stateowner, | |
4183 | so_perclient); | |
4184 | /* unhash_stateowner deletes so_perclient only | |
4185 | * for openowners. */ | |
4186 | list_del(&sop->so_perclient); | |
f044ff83 | 4187 | release_lockowner(sop); |
1da177e4 LT |
4188 | } |
4189 | out: | |
4190 | nfs4_unlock_state(); | |
4191 | return status; | |
4192 | } | |
4193 | ||
4194 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 4195 | alloc_reclaim(void) |
1da177e4 | 4196 | { |
a55370a3 | 4197 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
4198 | } |
4199 | ||
c7b9a459 | 4200 | int |
a1bcecd2 | 4201 | nfs4_has_reclaimed_state(const char *name, bool use_exchange_id) |
c7b9a459 N |
4202 | { |
4203 | unsigned int strhashval = clientstr_hashval(name); | |
4204 | struct nfs4_client *clp; | |
4205 | ||
e203d506 | 4206 | clp = find_confirmed_client_by_str(name, strhashval); |
c7b9a459 N |
4207 | return clp ? 1 : 0; |
4208 | } | |
4209 | ||
1da177e4 LT |
4210 | /* |
4211 | * failure => all reset bets are off, nfserr_no_grace... | |
4212 | */ | |
190e4fbf N |
4213 | int |
4214 | nfs4_client_to_reclaim(const char *name) | |
1da177e4 LT |
4215 | { |
4216 | unsigned int strhashval; | |
4217 | struct nfs4_client_reclaim *crp = NULL; | |
4218 | ||
a55370a3 N |
4219 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
4220 | crp = alloc_reclaim(); | |
1da177e4 LT |
4221 | if (!crp) |
4222 | return 0; | |
a55370a3 | 4223 | strhashval = clientstr_hashval(name); |
1da177e4 LT |
4224 | INIT_LIST_HEAD(&crp->cr_strhash); |
4225 | list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]); | |
a55370a3 | 4226 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
1da177e4 LT |
4227 | reclaim_str_hashtbl_size++; |
4228 | return 1; | |
4229 | } | |
4230 | ||
4231 | static void | |
4232 | nfs4_release_reclaim(void) | |
4233 | { | |
4234 | struct nfs4_client_reclaim *crp = NULL; | |
4235 | int i; | |
4236 | ||
1da177e4 LT |
4237 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
4238 | while (!list_empty(&reclaim_str_hashtbl[i])) { | |
4239 | crp = list_entry(reclaim_str_hashtbl[i].next, | |
4240 | struct nfs4_client_reclaim, cr_strhash); | |
4241 | list_del(&crp->cr_strhash); | |
1da177e4 LT |
4242 | kfree(crp); |
4243 | reclaim_str_hashtbl_size--; | |
4244 | } | |
4245 | } | |
4246 | BUG_ON(reclaim_str_hashtbl_size); | |
4247 | } | |
4248 | ||
4249 | /* | |
4250 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
fd39ca9a | 4251 | static struct nfs4_client_reclaim * |
1da177e4 LT |
4252 | nfs4_find_reclaim_client(clientid_t *clid) |
4253 | { | |
4254 | unsigned int strhashval; | |
4255 | struct nfs4_client *clp; | |
4256 | struct nfs4_client_reclaim *crp = NULL; | |
4257 | ||
4258 | ||
4259 | /* find clientid in conf_id_hashtbl */ | |
4260 | clp = find_confirmed_client(clid); | |
4261 | if (clp == NULL) | |
4262 | return NULL; | |
4263 | ||
a55370a3 N |
4264 | dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n", |
4265 | clp->cl_name.len, clp->cl_name.data, | |
4266 | clp->cl_recdir); | |
1da177e4 LT |
4267 | |
4268 | /* find clp->cl_name in reclaim_str_hashtbl */ | |
a55370a3 | 4269 | strhashval = clientstr_hashval(clp->cl_recdir); |
1da177e4 | 4270 | list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) { |
a55370a3 | 4271 | if (same_name(crp->cr_recdir, clp->cl_recdir)) { |
1da177e4 LT |
4272 | return crp; |
4273 | } | |
4274 | } | |
4275 | return NULL; | |
4276 | } | |
4277 | ||
4278 | /* | |
4279 | * Called from OPEN. Look for clientid in reclaim list. | |
4280 | */ | |
b37ad28b | 4281 | __be32 |
1da177e4 LT |
4282 | nfs4_check_open_reclaim(clientid_t *clid) |
4283 | { | |
dfc83565 | 4284 | return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad; |
1da177e4 LT |
4285 | } |
4286 | ||
ac4d8ff2 | 4287 | /* initialization to perform at module load time: */ |
1da177e4 | 4288 | |
e8ff2a84 | 4289 | int |
ac4d8ff2 | 4290 | nfs4_state_init(void) |
1da177e4 | 4291 | { |
e8ff2a84 | 4292 | int i, status; |
1da177e4 | 4293 | |
e8ff2a84 BF |
4294 | status = nfsd4_init_slabs(); |
4295 | if (status) | |
4296 | return status; | |
1da177e4 LT |
4297 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
4298 | INIT_LIST_HEAD(&conf_id_hashtbl[i]); | |
4299 | INIT_LIST_HEAD(&conf_str_hashtbl[i]); | |
4300 | INIT_LIST_HEAD(&unconf_str_hashtbl[i]); | |
4301 | INIT_LIST_HEAD(&unconf_id_hashtbl[i]); | |
02cb2858 | 4302 | INIT_LIST_HEAD(&reclaim_str_hashtbl[i]); |
1da177e4 | 4303 | } |
5282fd72 ME |
4304 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
4305 | INIT_LIST_HEAD(&sessionid_hashtbl[i]); | |
1da177e4 LT |
4306 | for (i = 0; i < FILE_HASH_SIZE; i++) { |
4307 | INIT_LIST_HEAD(&file_hashtbl[i]); | |
4308 | } | |
4309 | for (i = 0; i < OWNER_HASH_SIZE; i++) { | |
4310 | INIT_LIST_HEAD(&ownerstr_hashtbl[i]); | |
4311 | INIT_LIST_HEAD(&ownerid_hashtbl[i]); | |
4312 | } | |
4313 | for (i = 0; i < STATEID_HASH_SIZE; i++) { | |
4314 | INIT_LIST_HEAD(&stateid_hashtbl[i]); | |
4315 | INIT_LIST_HEAD(&lockstateid_hashtbl[i]); | |
4316 | } | |
4317 | for (i = 0; i < LOCK_HASH_SIZE; i++) { | |
4318 | INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]); | |
4319 | INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]); | |
4320 | } | |
1da177e4 | 4321 | memset(&onestateid, ~0, sizeof(stateid_t)); |
1da177e4 LT |
4322 | INIT_LIST_HEAD(&close_lru); |
4323 | INIT_LIST_HEAD(&client_lru); | |
4324 | INIT_LIST_HEAD(&del_recall_lru); | |
ac4d8ff2 | 4325 | reclaim_str_hashtbl_size = 0; |
e8ff2a84 | 4326 | return 0; |
ac4d8ff2 N |
4327 | } |
4328 | ||
190e4fbf N |
4329 | static void |
4330 | nfsd4_load_reboot_recovery_data(void) | |
4331 | { | |
4332 | int status; | |
4333 | ||
0964a3d3 N |
4334 | nfs4_lock_state(); |
4335 | nfsd4_init_recdir(user_recovery_dirname); | |
190e4fbf | 4336 | status = nfsd4_recdir_load(); |
0964a3d3 | 4337 | nfs4_unlock_state(); |
190e4fbf N |
4338 | if (status) |
4339 | printk("NFSD: Failure reading reboot recovery data\n"); | |
4340 | } | |
4341 | ||
c2f1a551 MS |
4342 | /* |
4343 | * Since the lifetime of a delegation isn't limited to that of an open, a | |
4344 | * client may quite reasonably hang on to a delegation as long as it has | |
4345 | * the inode cached. This becomes an obvious problem the first time a | |
4346 | * client's inode cache approaches the size of the server's total memory. | |
4347 | * | |
4348 | * For now we avoid this problem by imposing a hard limit on the number | |
4349 | * of delegations, which varies according to the server's memory size. | |
4350 | */ | |
4351 | static void | |
4352 | set_max_delegations(void) | |
4353 | { | |
4354 | /* | |
4355 | * Allow at most 4 delegations per megabyte of RAM. Quick | |
4356 | * estimates suggest that in the worst case (where every delegation | |
4357 | * is for a different inode), a delegation could take about 1.5K, | |
4358 | * giving a worst case usage of about 6% of memory. | |
4359 | */ | |
4360 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); | |
4361 | } | |
4362 | ||
ac4d8ff2 N |
4363 | /* initialization to perform when the nfsd service is started: */ |
4364 | ||
29ab23cc | 4365 | static int |
ac4d8ff2 N |
4366 | __nfs4_state_start(void) |
4367 | { | |
b5a1a81e BF |
4368 | int ret; |
4369 | ||
1da177e4 | 4370 | boot_time = get_seconds(); |
af558e33 | 4371 | locks_start_grace(&nfsd4_manager); |
9a8db97e | 4372 | printk(KERN_INFO "NFSD: starting %ld-second grace period\n", |
e46b498c | 4373 | nfsd4_grace); |
b5a1a81e BF |
4374 | ret = set_callback_cred(); |
4375 | if (ret) | |
4376 | return -ENOMEM; | |
58da282b | 4377 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
29ab23cc BF |
4378 | if (laundry_wq == NULL) |
4379 | return -ENOMEM; | |
b5a1a81e BF |
4380 | ret = nfsd4_create_callback_queue(); |
4381 | if (ret) | |
4382 | goto out_free_laundry; | |
e46b498c | 4383 | queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ); |
c2f1a551 | 4384 | set_max_delegations(); |
b5a1a81e BF |
4385 | return 0; |
4386 | out_free_laundry: | |
4387 | destroy_workqueue(laundry_wq); | |
4388 | return ret; | |
1da177e4 LT |
4389 | } |
4390 | ||
29ab23cc | 4391 | int |
76a3550e | 4392 | nfs4_state_start(void) |
1da177e4 | 4393 | { |
190e4fbf | 4394 | nfsd4_load_reboot_recovery_data(); |
4ad9a344 | 4395 | return __nfs4_state_start(); |
1da177e4 LT |
4396 | } |
4397 | ||
1da177e4 LT |
4398 | static void |
4399 | __nfs4_state_shutdown(void) | |
4400 | { | |
4401 | int i; | |
4402 | struct nfs4_client *clp = NULL; | |
4403 | struct nfs4_delegation *dp = NULL; | |
1da177e4 LT |
4404 | struct list_head *pos, *next, reaplist; |
4405 | ||
1da177e4 LT |
4406 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
4407 | while (!list_empty(&conf_id_hashtbl[i])) { | |
4408 | clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
4409 | expire_client(clp); | |
4410 | } | |
4411 | while (!list_empty(&unconf_str_hashtbl[i])) { | |
4412 | clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash); | |
4413 | expire_client(clp); | |
4414 | } | |
4415 | } | |
4416 | INIT_LIST_HEAD(&reaplist); | |
4417 | spin_lock(&recall_lock); | |
4418 | list_for_each_safe(pos, next, &del_recall_lru) { | |
4419 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
4420 | list_move(&dp->dl_recall_lru, &reaplist); | |
4421 | } | |
4422 | spin_unlock(&recall_lock); | |
4423 | list_for_each_safe(pos, next, &reaplist) { | |
4424 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
4425 | list_del_init(&dp->dl_recall_lru); | |
4426 | unhash_delegation(dp); | |
4427 | } | |
4428 | ||
190e4fbf | 4429 | nfsd4_shutdown_recdir(); |
1da177e4 LT |
4430 | } |
4431 | ||
4432 | void | |
4433 | nfs4_state_shutdown(void) | |
4434 | { | |
afe2c511 | 4435 | cancel_delayed_work_sync(&laundromat_work); |
5e8d5c29 | 4436 | destroy_workqueue(laundry_wq); |
2c5e7615 | 4437 | locks_end_grace(&nfsd4_manager); |
1da177e4 LT |
4438 | nfs4_lock_state(); |
4439 | nfs4_release_reclaim(); | |
4440 | __nfs4_state_shutdown(); | |
1da177e4 | 4441 | nfs4_unlock_state(); |
c3935e30 | 4442 | nfsd4_destroy_callback_queue(); |
1da177e4 LT |
4443 | } |
4444 | ||
3dd98a3b JL |
4445 | /* |
4446 | * user_recovery_dirname is protected by the nfsd_mutex since it's only | |
4447 | * accessed when nfsd is starting. | |
4448 | */ | |
0964a3d3 N |
4449 | static void |
4450 | nfs4_set_recdir(char *recdir) | |
4451 | { | |
0964a3d3 | 4452 | strcpy(user_recovery_dirname, recdir); |
0964a3d3 N |
4453 | } |
4454 | ||
4455 | /* | |
4456 | * Change the NFSv4 recovery directory to recdir. | |
4457 | */ | |
4458 | int | |
4459 | nfs4_reset_recoverydir(char *recdir) | |
4460 | { | |
4461 | int status; | |
a63bb996 | 4462 | struct path path; |
0964a3d3 | 4463 | |
a63bb996 | 4464 | status = kern_path(recdir, LOOKUP_FOLLOW, &path); |
0964a3d3 N |
4465 | if (status) |
4466 | return status; | |
4467 | status = -ENOTDIR; | |
a63bb996 | 4468 | if (S_ISDIR(path.dentry->d_inode->i_mode)) { |
0964a3d3 N |
4469 | nfs4_set_recdir(recdir); |
4470 | status = 0; | |
4471 | } | |
a63bb996 | 4472 | path_put(&path); |
0964a3d3 N |
4473 | return status; |
4474 | } | |
4475 | ||
3dd98a3b JL |
4476 | char * |
4477 | nfs4_recoverydir(void) | |
4478 | { | |
4479 | return user_recovery_dirname; | |
4480 | } |