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> |
17456804 | 40 | #include <linux/pagemap.h> |
7df302f7 | 41 | #include <linux/ratelimit.h> |
68e76ad0 | 42 | #include <linux/sunrpc/svcauth_gss.h> |
5976687a | 43 | #include <linux/sunrpc/addr.h> |
6282cd56 | 44 | #include <linux/hash.h> |
9a74af21 | 45 | #include "xdr4.h" |
06b332a5 | 46 | #include "xdr4cb.h" |
0a3adade | 47 | #include "vfs.h" |
bfa4b365 | 48 | #include "current_stateid.h" |
1da177e4 | 49 | |
5e1533c7 SK |
50 | #include "netns.h" |
51 | ||
1da177e4 LT |
52 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
53 | ||
f32f3c2d BF |
54 | #define all_ones {{~0,~0},~0} |
55 | static const stateid_t one_stateid = { | |
56 | .si_generation = ~0, | |
57 | .si_opaque = all_ones, | |
58 | }; | |
59 | static const stateid_t zero_stateid = { | |
60 | /* all fields zero */ | |
61 | }; | |
19ff0f28 TM |
62 | static const stateid_t currentstateid = { |
63 | .si_generation = 1, | |
64 | }; | |
f32f3c2d | 65 | |
ec6b5d7b | 66 | static u64 current_sessionid = 1; |
fd39ca9a | 67 | |
f32f3c2d BF |
68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) | |
19ff0f28 | 70 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
1da177e4 | 71 | |
1da177e4 | 72 | /* forward declarations */ |
fe0750e5 | 73 | static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner); |
1da177e4 | 74 | |
8b671b80 BF |
75 | /* Locking: */ |
76 | ||
77 | /* Currently used for almost all code touching nfsv4 state: */ | |
353ab6e9 | 78 | static DEFINE_MUTEX(client_mutex); |
1da177e4 | 79 | |
8b671b80 BF |
80 | /* |
81 | * Currently used for the del_recall_lru and file hash table. In an | |
82 | * effort to decrease the scope of the client_mutex, this spinlock may | |
83 | * eventually cover more: | |
84 | */ | |
cdc97505 | 85 | static DEFINE_SPINLOCK(state_lock); |
8b671b80 | 86 | |
abf1135b CH |
87 | static struct kmem_cache *openowner_slab; |
88 | static struct kmem_cache *lockowner_slab; | |
89 | static struct kmem_cache *file_slab; | |
90 | static struct kmem_cache *stateid_slab; | |
91 | static struct kmem_cache *deleg_slab; | |
e60d4398 | 92 | |
1da177e4 LT |
93 | void |
94 | nfs4_lock_state(void) | |
95 | { | |
353ab6e9 | 96 | mutex_lock(&client_mutex); |
1da177e4 LT |
97 | } |
98 | ||
66b2b9b2 | 99 | static void free_session(struct nfsd4_session *); |
508dc6e1 | 100 | |
f0f51f5c | 101 | static bool is_session_dead(struct nfsd4_session *ses) |
66b2b9b2 | 102 | { |
f0f51f5c | 103 | return ses->se_flags & NFS4_SESSION_DEAD; |
66b2b9b2 BF |
104 | } |
105 | ||
f0f51f5c | 106 | void nfsd4_put_session(struct nfsd4_session *ses) |
508dc6e1 | 107 | { |
f0f51f5c BF |
108 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
109 | free_session(ses); | |
66b2b9b2 BF |
110 | } |
111 | ||
f0f51f5c | 112 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
508dc6e1 | 113 | { |
f0f51f5c | 114 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
66b2b9b2 BF |
115 | return nfserr_jukebox; |
116 | ses->se_flags |= NFS4_SESSION_DEAD; | |
117 | return nfs_ok; | |
508dc6e1 BH |
118 | } |
119 | ||
66b2b9b2 | 120 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
508dc6e1 | 121 | { |
66b2b9b2 BF |
122 | if (is_session_dead(ses)) |
123 | return nfserr_badsession; | |
124 | atomic_inc(&ses->se_ref); | |
125 | return nfs_ok; | |
508dc6e1 BH |
126 | } |
127 | ||
1da177e4 LT |
128 | void |
129 | nfs4_unlock_state(void) | |
130 | { | |
353ab6e9 | 131 | mutex_unlock(&client_mutex); |
1da177e4 LT |
132 | } |
133 | ||
221a6876 BF |
134 | static bool is_client_expired(struct nfs4_client *clp) |
135 | { | |
136 | return clp->cl_time == 0; | |
137 | } | |
138 | ||
139 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) | |
140 | { | |
141 | if (atomic_read(&clp->cl_refcount)) | |
142 | return nfserr_jukebox; | |
143 | clp->cl_time = 0; | |
144 | return nfs_ok; | |
145 | } | |
146 | ||
147 | static __be32 mark_client_expired(struct nfs4_client *clp) | |
148 | { | |
149 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
150 | __be32 ret; | |
151 | ||
152 | spin_lock(&nn->client_lock); | |
153 | ret = mark_client_expired_locked(clp); | |
154 | spin_unlock(&nn->client_lock); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static __be32 get_client_locked(struct nfs4_client *clp) | |
159 | { | |
160 | if (is_client_expired(clp)) | |
161 | return nfserr_expired; | |
162 | atomic_inc(&clp->cl_refcount); | |
163 | return nfs_ok; | |
164 | } | |
165 | ||
166 | /* must be called under the client_lock */ | |
167 | static inline void | |
168 | renew_client_locked(struct nfs4_client *clp) | |
169 | { | |
170 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
171 | ||
172 | if (is_client_expired(clp)) { | |
173 | WARN_ON(1); | |
174 | printk("%s: client (clientid %08x/%08x) already expired\n", | |
175 | __func__, | |
176 | clp->cl_clientid.cl_boot, | |
177 | clp->cl_clientid.cl_id); | |
178 | return; | |
179 | } | |
180 | ||
181 | dprintk("renewing client (clientid %08x/%08x)\n", | |
182 | clp->cl_clientid.cl_boot, | |
183 | clp->cl_clientid.cl_id); | |
184 | list_move_tail(&clp->cl_lru, &nn->client_lru); | |
185 | clp->cl_time = get_seconds(); | |
186 | } | |
187 | ||
188 | static inline void | |
189 | renew_client(struct nfs4_client *clp) | |
190 | { | |
191 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
192 | ||
193 | spin_lock(&nn->client_lock); | |
194 | renew_client_locked(clp); | |
195 | spin_unlock(&nn->client_lock); | |
196 | } | |
197 | ||
ba138435 | 198 | static void put_client_renew_locked(struct nfs4_client *clp) |
221a6876 BF |
199 | { |
200 | if (!atomic_dec_and_test(&clp->cl_refcount)) | |
201 | return; | |
202 | if (!is_client_expired(clp)) | |
203 | renew_client_locked(clp); | |
204 | } | |
205 | ||
1da177e4 LT |
206 | static inline u32 |
207 | opaque_hashval(const void *ptr, int nbytes) | |
208 | { | |
209 | unsigned char *cptr = (unsigned char *) ptr; | |
210 | ||
211 | u32 x = 0; | |
212 | while (nbytes--) { | |
213 | x *= 37; | |
214 | x += *cptr++; | |
215 | } | |
216 | return x; | |
217 | } | |
218 | ||
32513b40 BF |
219 | static void nfsd4_free_file(struct nfs4_file *f) |
220 | { | |
221 | kmem_cache_free(file_slab, f); | |
222 | } | |
223 | ||
13cd2184 N |
224 | static inline void |
225 | put_nfs4_file(struct nfs4_file *fi) | |
226 | { | |
cdc97505 | 227 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
89876f8c | 228 | hlist_del(&fi->fi_hash); |
cdc97505 | 229 | spin_unlock(&state_lock); |
8b671b80 | 230 | iput(fi->fi_inode); |
32513b40 | 231 | nfsd4_free_file(fi); |
8b671b80 | 232 | } |
13cd2184 N |
233 | } |
234 | ||
235 | static inline void | |
236 | get_nfs4_file(struct nfs4_file *fi) | |
237 | { | |
8b671b80 | 238 | atomic_inc(&fi->fi_ref); |
13cd2184 N |
239 | } |
240 | ||
ef0f3390 | 241 | static int num_delegations; |
697ce9be | 242 | unsigned long max_delegations; |
ef0f3390 N |
243 | |
244 | /* | |
245 | * Open owner state (share locks) | |
246 | */ | |
247 | ||
16bfdaaf BF |
248 | /* hash tables for lock and open owners */ |
249 | #define OWNER_HASH_BITS 8 | |
250 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) | |
251 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) | |
ef0f3390 | 252 | |
16bfdaaf | 253 | static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
ddc04c41 BF |
254 | { |
255 | unsigned int ret; | |
256 | ||
257 | ret = opaque_hashval(ownername->data, ownername->len); | |
258 | ret += clientid; | |
16bfdaaf | 259 | return ret & OWNER_HASH_MASK; |
ddc04c41 | 260 | } |
ef0f3390 | 261 | |
ef0f3390 N |
262 | /* hash table for nfs4_file */ |
263 | #define FILE_HASH_BITS 8 | |
264 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) | |
35079582 | 265 | |
ddc04c41 BF |
266 | static unsigned int file_hashval(struct inode *ino) |
267 | { | |
268 | /* XXX: why are we hashing on inode pointer, anyway? */ | |
269 | return hash_ptr(ino, FILE_HASH_BITS); | |
270 | } | |
271 | ||
89876f8c | 272 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
ef0f3390 | 273 | |
3477565e BF |
274 | static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
275 | { | |
276 | WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR])); | |
277 | atomic_inc(&fp->fi_access[oflag]); | |
278 | } | |
279 | ||
998db52c BF |
280 | static void nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
281 | { | |
282 | if (oflag == O_RDWR) { | |
3477565e BF |
283 | __nfs4_file_get_access(fp, O_RDONLY); |
284 | __nfs4_file_get_access(fp, O_WRONLY); | |
998db52c | 285 | } else |
3477565e | 286 | __nfs4_file_get_access(fp, oflag); |
998db52c BF |
287 | } |
288 | ||
289 | static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag) | |
f9d7562f BF |
290 | { |
291 | if (fp->fi_fds[oflag]) { | |
292 | fput(fp->fi_fds[oflag]); | |
293 | fp->fi_fds[oflag] = NULL; | |
294 | } | |
295 | } | |
296 | ||
998db52c | 297 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
f9d7562f BF |
298 | { |
299 | if (atomic_dec_and_test(&fp->fi_access[oflag])) { | |
f9d7562f | 300 | nfs4_file_put_fd(fp, oflag); |
0c7c3e67 | 301 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
3d02fa29 | 302 | nfs4_file_put_fd(fp, O_RDWR); |
f9d7562f BF |
303 | } |
304 | } | |
305 | ||
998db52c BF |
306 | static void nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
307 | { | |
308 | if (oflag == O_RDWR) { | |
309 | __nfs4_file_put_access(fp, O_RDONLY); | |
310 | __nfs4_file_put_access(fp, O_WRONLY); | |
311 | } else | |
312 | __nfs4_file_put_access(fp, oflag); | |
313 | } | |
314 | ||
3abdb607 BF |
315 | static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct |
316 | kmem_cache *slab) | |
2a74aba7 | 317 | { |
3abdb607 | 318 | struct idr *stateids = &cl->cl_stateids; |
3abdb607 | 319 | struct nfs4_stid *stid; |
6136d2b4 | 320 | int new_id; |
2a74aba7 | 321 | |
3abdb607 BF |
322 | stid = kmem_cache_alloc(slab, GFP_KERNEL); |
323 | if (!stid) | |
324 | return NULL; | |
325 | ||
398c33aa | 326 | new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL); |
ebd6c707 | 327 | if (new_id < 0) |
3abdb607 | 328 | goto out_free; |
2a74aba7 | 329 | stid->sc_client = cl; |
3abdb607 BF |
330 | stid->sc_type = 0; |
331 | stid->sc_stateid.si_opaque.so_id = new_id; | |
332 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; | |
2a74aba7 | 333 | /* Will be incremented before return to client: */ |
3abdb607 | 334 | stid->sc_stateid.si_generation = 0; |
996e0938 | 335 | |
996e0938 | 336 | /* |
3abdb607 BF |
337 | * It shouldn't be a problem to reuse an opaque stateid value. |
338 | * I don't think it is for 4.1. But with 4.0 I worry that, for | |
339 | * example, a stray write retransmission could be accepted by | |
340 | * the server when it should have been rejected. Therefore, | |
341 | * adopt a trick from the sctp code to attempt to maximize the | |
342 | * amount of time until an id is reused, by ensuring they always | |
343 | * "increase" (mod INT_MAX): | |
996e0938 | 344 | */ |
3abdb607 BF |
345 | return stid; |
346 | out_free: | |
2c44a234 | 347 | kmem_cache_free(slab, stid); |
3abdb607 | 348 | return NULL; |
2a74aba7 BF |
349 | } |
350 | ||
4cdc951b BF |
351 | static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp) |
352 | { | |
353 | return openlockstateid(nfs4_alloc_stid(clp, stateid_slab)); | |
354 | } | |
355 | ||
6282cd56 N |
356 | /* |
357 | * When we recall a delegation, we should be careful not to hand it | |
358 | * out again straight away. | |
359 | * To ensure this we keep a pair of bloom filters ('new' and 'old') | |
360 | * in which the filehandles of recalled delegations are "stored". | |
361 | * If a filehandle appear in either filter, a delegation is blocked. | |
362 | * When a delegation is recalled, the filehandle is stored in the "new" | |
363 | * filter. | |
364 | * Every 30 seconds we swap the filters and clear the "new" one, | |
365 | * unless both are empty of course. | |
366 | * | |
367 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the | |
368 | * low 3 bytes as hash-table indices. | |
369 | * | |
370 | * 'state_lock', which is always held when block_delegations() is called, | |
371 | * is used to manage concurrent access. Testing does not need the lock | |
372 | * except when swapping the two filters. | |
373 | */ | |
374 | static struct bloom_pair { | |
375 | int entries, old_entries; | |
376 | time_t swap_time; | |
377 | int new; /* index into 'set' */ | |
378 | DECLARE_BITMAP(set[2], 256); | |
379 | } blocked_delegations; | |
380 | ||
381 | static int delegation_blocked(struct knfsd_fh *fh) | |
382 | { | |
383 | u32 hash; | |
384 | struct bloom_pair *bd = &blocked_delegations; | |
385 | ||
386 | if (bd->entries == 0) | |
387 | return 0; | |
388 | if (seconds_since_boot() - bd->swap_time > 30) { | |
389 | spin_lock(&state_lock); | |
390 | if (seconds_since_boot() - bd->swap_time > 30) { | |
391 | bd->entries -= bd->old_entries; | |
392 | bd->old_entries = bd->entries; | |
393 | memset(bd->set[bd->new], 0, | |
394 | sizeof(bd->set[0])); | |
395 | bd->new = 1-bd->new; | |
396 | bd->swap_time = seconds_since_boot(); | |
397 | } | |
398 | spin_unlock(&state_lock); | |
399 | } | |
400 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); | |
401 | if (test_bit(hash&255, bd->set[0]) && | |
402 | test_bit((hash>>8)&255, bd->set[0]) && | |
403 | test_bit((hash>>16)&255, bd->set[0])) | |
404 | return 1; | |
405 | ||
406 | if (test_bit(hash&255, bd->set[1]) && | |
407 | test_bit((hash>>8)&255, bd->set[1]) && | |
408 | test_bit((hash>>16)&255, bd->set[1])) | |
409 | return 1; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | static void block_delegations(struct knfsd_fh *fh) | |
415 | { | |
416 | u32 hash; | |
417 | struct bloom_pair *bd = &blocked_delegations; | |
418 | ||
419 | hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0); | |
420 | ||
421 | __set_bit(hash&255, bd->set[bd->new]); | |
422 | __set_bit((hash>>8)&255, bd->set[bd->new]); | |
423 | __set_bit((hash>>16)&255, bd->set[bd->new]); | |
424 | if (bd->entries == 0) | |
425 | bd->swap_time = seconds_since_boot(); | |
426 | bd->entries += 1; | |
427 | } | |
428 | ||
1da177e4 | 429 | static struct nfs4_delegation * |
99c41515 | 430 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh) |
1da177e4 LT |
431 | { |
432 | struct nfs4_delegation *dp; | |
1da177e4 LT |
433 | |
434 | dprintk("NFSD alloc_init_deleg\n"); | |
c2f1a551 | 435 | if (num_delegations > max_delegations) |
ef0f3390 | 436 | return NULL; |
6282cd56 N |
437 | if (delegation_blocked(¤t_fh->fh_handle)) |
438 | return NULL; | |
996e0938 | 439 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
5b2d21c1 | 440 | if (dp == NULL) |
1da177e4 | 441 | return dp; |
2a74aba7 BF |
442 | /* |
443 | * delegation seqid's are never incremented. The 4.1 special | |
6136d2b4 BF |
444 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
445 | * 0 anyway just for consistency and use 1: | |
2a74aba7 BF |
446 | */ |
447 | dp->dl_stid.sc_stateid.si_generation = 1; | |
ef0f3390 | 448 | num_delegations++; |
ea1da636 N |
449 | INIT_LIST_HEAD(&dp->dl_perfile); |
450 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 | 451 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
bf7bd3e9 | 452 | dp->dl_file = NULL; |
99c41515 | 453 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
6c02eaa1 | 454 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
1da177e4 LT |
455 | dp->dl_time = 0; |
456 | atomic_set(&dp->dl_count, 1); | |
57725155 | 457 | nfsd4_init_callback(&dp->dl_recall); |
1da177e4 LT |
458 | return dp; |
459 | } | |
460 | ||
68a33961 | 461 | static void remove_stid(struct nfs4_stid *s) |
3abdb607 BF |
462 | { |
463 | struct idr *stateids = &s->sc_client->cl_stateids; | |
464 | ||
465 | idr_remove(stateids, s->sc_stateid.si_opaque.so_id); | |
3abdb607 BF |
466 | } |
467 | ||
9857df81 BH |
468 | static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s) |
469 | { | |
470 | kmem_cache_free(slab, s); | |
471 | } | |
472 | ||
1da177e4 LT |
473 | void |
474 | nfs4_put_delegation(struct nfs4_delegation *dp) | |
475 | { | |
476 | if (atomic_dec_and_test(&dp->dl_count)) { | |
9857df81 | 477 | nfs4_free_stid(deleg_slab, &dp->dl_stid); |
ef0f3390 | 478 | num_delegations--; |
1da177e4 LT |
479 | } |
480 | } | |
481 | ||
acfdf5c3 | 482 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
1da177e4 | 483 | { |
cbf7a75b BF |
484 | if (!fp->fi_lease) |
485 | return; | |
acfdf5c3 BF |
486 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
487 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); | |
488 | fp->fi_lease = NULL; | |
4ee63624 | 489 | fput(fp->fi_deleg_file); |
acfdf5c3 BF |
490 | fp->fi_deleg_file = NULL; |
491 | } | |
1da177e4 LT |
492 | } |
493 | ||
6136d2b4 BF |
494 | static void unhash_stid(struct nfs4_stid *s) |
495 | { | |
3abdb607 | 496 | s->sc_type = 0; |
6136d2b4 BF |
497 | } |
498 | ||
931ee56c BH |
499 | static void |
500 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) | |
501 | { | |
cdc97505 | 502 | lockdep_assert_held(&state_lock); |
931ee56c | 503 | |
3fb87d13 | 504 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
931ee56c BH |
505 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
506 | list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations); | |
507 | } | |
508 | ||
1da177e4 LT |
509 | /* Called under the state lock. */ |
510 | static void | |
511 | unhash_delegation(struct nfs4_delegation *dp) | |
512 | { | |
cdc97505 | 513 | spin_lock(&state_lock); |
931ee56c | 514 | list_del_init(&dp->dl_perclnt); |
5d926e8c | 515 | list_del_init(&dp->dl_perfile); |
1da177e4 | 516 | list_del_init(&dp->dl_recall_lru); |
cdc97505 | 517 | spin_unlock(&state_lock); |
cbf7a75b BF |
518 | if (dp->dl_file) { |
519 | nfs4_put_deleg_lease(dp->dl_file); | |
520 | put_nfs4_file(dp->dl_file); | |
521 | dp->dl_file = NULL; | |
522 | } | |
3bd64a5b BF |
523 | } |
524 | ||
525 | ||
526 | ||
527 | static void destroy_revoked_delegation(struct nfs4_delegation *dp) | |
528 | { | |
529 | list_del_init(&dp->dl_recall_lru); | |
68a33961 | 530 | remove_stid(&dp->dl_stid); |
1da177e4 LT |
531 | nfs4_put_delegation(dp); |
532 | } | |
533 | ||
3bd64a5b BF |
534 | static void destroy_delegation(struct nfs4_delegation *dp) |
535 | { | |
536 | unhash_delegation(dp); | |
537 | remove_stid(&dp->dl_stid); | |
538 | nfs4_put_delegation(dp); | |
539 | } | |
540 | ||
541 | static void revoke_delegation(struct nfs4_delegation *dp) | |
542 | { | |
543 | struct nfs4_client *clp = dp->dl_stid.sc_client; | |
544 | ||
545 | if (clp->cl_minorversion == 0) | |
546 | destroy_delegation(dp); | |
547 | else { | |
548 | unhash_delegation(dp); | |
549 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; | |
550 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); | |
551 | } | |
552 | } | |
553 | ||
1da177e4 LT |
554 | /* |
555 | * SETCLIENTID state | |
556 | */ | |
557 | ||
ddc04c41 BF |
558 | static unsigned int clientid_hashval(u32 id) |
559 | { | |
560 | return id & CLIENT_HASH_MASK; | |
561 | } | |
562 | ||
563 | static unsigned int clientstr_hashval(const char *name) | |
564 | { | |
565 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; | |
566 | } | |
567 | ||
f9d7562f BF |
568 | /* |
569 | * We store the NONE, READ, WRITE, and BOTH bits separately in the | |
570 | * st_{access,deny}_bmap field of the stateid, in order to track not | |
571 | * only what share bits are currently in force, but also what | |
572 | * combinations of share bits previous opens have used. This allows us | |
573 | * to enforce the recommendation of rfc 3530 14.2.19 that the server | |
574 | * return an error if the client attempt to downgrade to a combination | |
575 | * of share bits not explicable by closing some of its previous opens. | |
576 | * | |
577 | * XXX: This enforcement is actually incomplete, since we don't keep | |
578 | * track of access/deny bit combinations; so, e.g., we allow: | |
579 | * | |
580 | * OPEN allow read, deny write | |
581 | * OPEN allow both, deny none | |
582 | * DOWNGRADE allow read, deny none | |
583 | * | |
584 | * which we should reject. | |
585 | */ | |
5ae037e5 JL |
586 | static unsigned int |
587 | bmap_to_share_mode(unsigned long bmap) { | |
f9d7562f | 588 | int i; |
5ae037e5 | 589 | unsigned int access = 0; |
f9d7562f | 590 | |
f9d7562f BF |
591 | for (i = 1; i < 4; i++) { |
592 | if (test_bit(i, &bmap)) | |
5ae037e5 | 593 | access |= i; |
f9d7562f | 594 | } |
5ae037e5 | 595 | return access; |
f9d7562f BF |
596 | } |
597 | ||
3a328614 | 598 | static bool |
dcef0413 | 599 | test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) { |
f9d7562f BF |
600 | unsigned int access, deny; |
601 | ||
5ae037e5 JL |
602 | access = bmap_to_share_mode(stp->st_access_bmap); |
603 | deny = bmap_to_share_mode(stp->st_deny_bmap); | |
f9d7562f | 604 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) |
3a328614 JL |
605 | return false; |
606 | return true; | |
f9d7562f BF |
607 | } |
608 | ||
82c5ff1b JL |
609 | /* set share access for a given stateid */ |
610 | static inline void | |
611 | set_access(u32 access, struct nfs4_ol_stateid *stp) | |
612 | { | |
613 | __set_bit(access, &stp->st_access_bmap); | |
614 | } | |
615 | ||
616 | /* clear share access for a given stateid */ | |
617 | static inline void | |
618 | clear_access(u32 access, struct nfs4_ol_stateid *stp) | |
619 | { | |
620 | __clear_bit(access, &stp->st_access_bmap); | |
621 | } | |
622 | ||
623 | /* test whether a given stateid has access */ | |
624 | static inline bool | |
625 | test_access(u32 access, struct nfs4_ol_stateid *stp) | |
626 | { | |
627 | return test_bit(access, &stp->st_access_bmap); | |
628 | } | |
629 | ||
ce0fc43c JL |
630 | /* set share deny for a given stateid */ |
631 | static inline void | |
632 | set_deny(u32 access, struct nfs4_ol_stateid *stp) | |
633 | { | |
634 | __set_bit(access, &stp->st_deny_bmap); | |
635 | } | |
636 | ||
637 | /* clear share deny for a given stateid */ | |
638 | static inline void | |
639 | clear_deny(u32 access, struct nfs4_ol_stateid *stp) | |
640 | { | |
641 | __clear_bit(access, &stp->st_deny_bmap); | |
642 | } | |
643 | ||
644 | /* test whether a given stateid is denying specific access */ | |
645 | static inline bool | |
646 | test_deny(u32 access, struct nfs4_ol_stateid *stp) | |
647 | { | |
648 | return test_bit(access, &stp->st_deny_bmap); | |
f9d7562f BF |
649 | } |
650 | ||
651 | static int nfs4_access_to_omode(u32 access) | |
652 | { | |
8f34a430 | 653 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
f9d7562f BF |
654 | case NFS4_SHARE_ACCESS_READ: |
655 | return O_RDONLY; | |
656 | case NFS4_SHARE_ACCESS_WRITE: | |
657 | return O_WRONLY; | |
658 | case NFS4_SHARE_ACCESS_BOTH: | |
659 | return O_RDWR; | |
660 | } | |
063b0fb9 BF |
661 | WARN_ON_ONCE(1); |
662 | return O_RDONLY; | |
f9d7562f BF |
663 | } |
664 | ||
82c5ff1b JL |
665 | /* release all access and file references for a given stateid */ |
666 | static void | |
667 | release_all_access(struct nfs4_ol_stateid *stp) | |
668 | { | |
669 | int i; | |
670 | ||
671 | for (i = 1; i < 4; i++) { | |
672 | if (test_access(i, stp)) | |
673 | nfs4_file_put_access(stp->st_file, | |
674 | nfs4_access_to_omode(i)); | |
675 | clear_access(i, stp); | |
676 | } | |
677 | } | |
678 | ||
dcef0413 | 679 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a | 680 | { |
529d7b2a BF |
681 | list_del(&stp->st_perfile); |
682 | list_del(&stp->st_perstateowner); | |
683 | } | |
684 | ||
dcef0413 | 685 | static void close_generic_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a | 686 | { |
82c5ff1b | 687 | release_all_access(stp); |
a96e5b90 | 688 | put_nfs4_file(stp->st_file); |
4665e2ba BF |
689 | stp->st_file = NULL; |
690 | } | |
691 | ||
dcef0413 | 692 | static void free_generic_stateid(struct nfs4_ol_stateid *stp) |
4665e2ba | 693 | { |
68a33961 | 694 | remove_stid(&stp->st_stid); |
9857df81 | 695 | nfs4_free_stid(stateid_slab, &stp->st_stid); |
529d7b2a BF |
696 | } |
697 | ||
dcef0413 | 698 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a BF |
699 | { |
700 | struct file *file; | |
701 | ||
702 | unhash_generic_stateid(stp); | |
6136d2b4 | 703 | unhash_stid(&stp->st_stid); |
529d7b2a BF |
704 | file = find_any_file(stp->st_file); |
705 | if (file) | |
fe0750e5 | 706 | locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner)); |
38c387b5 | 707 | close_generic_stateid(stp); |
529d7b2a BF |
708 | free_generic_stateid(stp); |
709 | } | |
710 | ||
fe0750e5 | 711 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
529d7b2a | 712 | { |
dcef0413 | 713 | struct nfs4_ol_stateid *stp; |
529d7b2a | 714 | |
fe0750e5 BF |
715 | list_del(&lo->lo_owner.so_strhash); |
716 | list_del(&lo->lo_perstateid); | |
009673b4 | 717 | list_del(&lo->lo_owner_ino_hash); |
fe0750e5 BF |
718 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
719 | stp = list_first_entry(&lo->lo_owner.so_stateids, | |
dcef0413 | 720 | struct nfs4_ol_stateid, st_perstateowner); |
529d7b2a BF |
721 | release_lock_stateid(stp); |
722 | } | |
723 | } | |
724 | ||
50cc6231 TM |
725 | static void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
726 | { | |
727 | kfree(lo->lo_owner.so_owner.data); | |
728 | kmem_cache_free(lockowner_slab, lo); | |
729 | } | |
730 | ||
fe0750e5 | 731 | static void release_lockowner(struct nfs4_lockowner *lo) |
529d7b2a | 732 | { |
fe0750e5 BF |
733 | unhash_lockowner(lo); |
734 | nfs4_free_lockowner(lo); | |
529d7b2a BF |
735 | } |
736 | ||
737 | static void | |
dcef0413 | 738 | release_stateid_lockowners(struct nfs4_ol_stateid *open_stp) |
529d7b2a | 739 | { |
fe0750e5 | 740 | struct nfs4_lockowner *lo; |
529d7b2a BF |
741 | |
742 | while (!list_empty(&open_stp->st_lockowners)) { | |
fe0750e5 BF |
743 | lo = list_entry(open_stp->st_lockowners.next, |
744 | struct nfs4_lockowner, lo_perstateid); | |
745 | release_lockowner(lo); | |
529d7b2a BF |
746 | } |
747 | } | |
748 | ||
38c387b5 | 749 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
2283963f BF |
750 | { |
751 | unhash_generic_stateid(stp); | |
752 | release_stateid_lockowners(stp); | |
38c387b5 BF |
753 | close_generic_stateid(stp); |
754 | } | |
755 | ||
756 | static void release_open_stateid(struct nfs4_ol_stateid *stp) | |
757 | { | |
758 | unhash_open_stateid(stp); | |
2283963f BF |
759 | free_generic_stateid(stp); |
760 | } | |
761 | ||
fe0750e5 | 762 | static void unhash_openowner(struct nfs4_openowner *oo) |
f1d110ca | 763 | { |
dcef0413 | 764 | struct nfs4_ol_stateid *stp; |
f1d110ca | 765 | |
fe0750e5 BF |
766 | list_del(&oo->oo_owner.so_strhash); |
767 | list_del(&oo->oo_perclient); | |
768 | while (!list_empty(&oo->oo_owner.so_stateids)) { | |
769 | stp = list_first_entry(&oo->oo_owner.so_stateids, | |
dcef0413 | 770 | struct nfs4_ol_stateid, st_perstateowner); |
f044ff83 | 771 | release_open_stateid(stp); |
f1d110ca BF |
772 | } |
773 | } | |
774 | ||
f7a4d872 BF |
775 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
776 | { | |
777 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; | |
778 | ||
779 | if (s) { | |
f7a4d872 BF |
780 | free_generic_stateid(s); |
781 | oo->oo_last_closed_stid = NULL; | |
782 | } | |
783 | } | |
784 | ||
50cc6231 TM |
785 | static void nfs4_free_openowner(struct nfs4_openowner *oo) |
786 | { | |
787 | kfree(oo->oo_owner.so_owner.data); | |
788 | kmem_cache_free(openowner_slab, oo); | |
789 | } | |
790 | ||
fe0750e5 | 791 | static void release_openowner(struct nfs4_openowner *oo) |
f1d110ca | 792 | { |
fe0750e5 BF |
793 | unhash_openowner(oo); |
794 | list_del(&oo->oo_close_lru); | |
f7a4d872 | 795 | release_last_closed_stateid(oo); |
fe0750e5 | 796 | nfs4_free_openowner(oo); |
f1d110ca BF |
797 | } |
798 | ||
5282fd72 ME |
799 | static inline int |
800 | hash_sessionid(struct nfs4_sessionid *sessionid) | |
801 | { | |
802 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; | |
803 | ||
804 | return sid->sequence % SESSION_HASH_SIZE; | |
805 | } | |
806 | ||
8f199b82 | 807 | #ifdef NFSD_DEBUG |
5282fd72 ME |
808 | static inline void |
809 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
810 | { | |
811 | u32 *ptr = (u32 *)(&sessionid->data[0]); | |
812 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); | |
813 | } | |
8f199b82 TM |
814 | #else |
815 | static inline void | |
816 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
817 | { | |
818 | } | |
819 | #endif | |
820 | ||
9411b1d4 BF |
821 | /* |
822 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it | |
823 | * won't be used for replay. | |
824 | */ | |
825 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) | |
826 | { | |
827 | struct nfs4_stateowner *so = cstate->replay_owner; | |
828 | ||
829 | if (nfserr == nfserr_replay_me) | |
830 | return; | |
831 | ||
832 | if (!seqid_mutating_err(ntohl(nfserr))) { | |
833 | cstate->replay_owner = NULL; | |
834 | return; | |
835 | } | |
836 | if (!so) | |
837 | return; | |
838 | if (so->so_is_open_owner) | |
839 | release_last_closed_stateid(openowner(so)); | |
840 | so->so_seqid++; | |
841 | return; | |
842 | } | |
5282fd72 | 843 | |
ec6b5d7b AA |
844 | static void |
845 | gen_sessionid(struct nfsd4_session *ses) | |
846 | { | |
847 | struct nfs4_client *clp = ses->se_client; | |
848 | struct nfsd4_sessionid *sid; | |
849 | ||
850 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; | |
851 | sid->clientid = clp->cl_clientid; | |
852 | sid->sequence = current_sessionid++; | |
853 | sid->reserved = 0; | |
854 | } | |
855 | ||
856 | /* | |
a649637c AA |
857 | * The protocol defines ca_maxresponssize_cached to include the size of |
858 | * the rpc header, but all we need to cache is the data starting after | |
859 | * the end of the initial SEQUENCE operation--the rest we regenerate | |
860 | * each time. Therefore we can advertise a ca_maxresponssize_cached | |
861 | * value that is the number of bytes in our cache plus a few additional | |
862 | * bytes. In order to stay on the safe side, and not promise more than | |
863 | * we can cache, those additional bytes must be the minimum possible: 24 | |
864 | * bytes of rpc header (xid through accept state, with AUTH_NULL | |
865 | * verifier), 12 for the compound header (with zero-length tag), and 44 | |
866 | * for the SEQUENCE op response: | |
867 | */ | |
868 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) | |
869 | ||
557ce264 AA |
870 | static void |
871 | free_session_slots(struct nfsd4_session *ses) | |
872 | { | |
873 | int i; | |
874 | ||
875 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) | |
876 | kfree(ses->se_slots[i]); | |
877 | } | |
878 | ||
a649637c | 879 | /* |
efe0cb6d BF |
880 | * We don't actually need to cache the rpc and session headers, so we |
881 | * can allocate a little less for each slot: | |
882 | */ | |
55c760cf | 883 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
efe0cb6d | 884 | { |
55c760cf | 885 | u32 size; |
efe0cb6d | 886 | |
55c760cf BF |
887 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
888 | size = 0; | |
889 | else | |
890 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; | |
891 | return size + sizeof(struct nfsd4_slot); | |
5b6feee9 | 892 | } |
ec6b5d7b | 893 | |
5b6feee9 BF |
894 | /* |
895 | * XXX: If we run out of reserved DRC memory we could (up to a point) | |
a649637c | 896 | * re-negotiate active sessions and reduce their slot usage to make |
42b2aa86 | 897 | * room for new connections. For now we just fail the create session. |
ec6b5d7b | 898 | */ |
55c760cf | 899 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
ec6b5d7b | 900 | { |
55c760cf BF |
901 | u32 slotsize = slot_bytes(ca); |
902 | u32 num = ca->maxreqs; | |
5b6feee9 | 903 | int avail; |
ec6b5d7b | 904 | |
5b6feee9 | 905 | spin_lock(&nfsd_drc_lock); |
697ce9be ZY |
906 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
907 | nfsd_drc_max_mem - nfsd_drc_mem_used); | |
5b6feee9 BF |
908 | num = min_t(int, num, avail / slotsize); |
909 | nfsd_drc_mem_used += num * slotsize; | |
910 | spin_unlock(&nfsd_drc_lock); | |
ec6b5d7b | 911 | |
5b6feee9 BF |
912 | return num; |
913 | } | |
ec6b5d7b | 914 | |
55c760cf | 915 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
5b6feee9 | 916 | { |
55c760cf BF |
917 | int slotsize = slot_bytes(ca); |
918 | ||
4bd9b0f4 | 919 | spin_lock(&nfsd_drc_lock); |
55c760cf | 920 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
4bd9b0f4 | 921 | spin_unlock(&nfsd_drc_lock); |
5b6feee9 | 922 | } |
ec6b5d7b | 923 | |
60810e54 KM |
924 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
925 | struct nfsd4_channel_attrs *battrs) | |
5b6feee9 | 926 | { |
60810e54 KM |
927 | int numslots = fattrs->maxreqs; |
928 | int slotsize = slot_bytes(fattrs); | |
5b6feee9 BF |
929 | struct nfsd4_session *new; |
930 | int mem, i; | |
a649637c | 931 | |
5b6feee9 BF |
932 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
933 | + sizeof(struct nfsd4_session) > PAGE_SIZE); | |
934 | mem = numslots * sizeof(struct nfsd4_slot *); | |
ec6b5d7b | 935 | |
5b6feee9 BF |
936 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
937 | if (!new) | |
938 | return NULL; | |
557ce264 | 939 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
5b6feee9 | 940 | for (i = 0; i < numslots; i++) { |
55c760cf | 941 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
5b6feee9 | 942 | if (!new->se_slots[i]) |
557ce264 | 943 | goto out_free; |
557ce264 | 944 | } |
60810e54 KM |
945 | |
946 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); | |
947 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); | |
948 | ||
5b6feee9 BF |
949 | return new; |
950 | out_free: | |
951 | while (i--) | |
952 | kfree(new->se_slots[i]); | |
953 | kfree(new); | |
954 | return NULL; | |
ec6b5d7b AA |
955 | } |
956 | ||
19cf5c02 BF |
957 | static void free_conn(struct nfsd4_conn *c) |
958 | { | |
959 | svc_xprt_put(c->cn_xprt); | |
960 | kfree(c); | |
961 | } | |
ec6b5d7b | 962 | |
19cf5c02 BF |
963 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
964 | { | |
965 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); | |
966 | struct nfs4_client *clp = c->cn_session->se_client; | |
ec6b5d7b | 967 | |
19cf5c02 BF |
968 | spin_lock(&clp->cl_lock); |
969 | if (!list_empty(&c->cn_persession)) { | |
970 | list_del(&c->cn_persession); | |
971 | free_conn(c); | |
972 | } | |
eea49806 | 973 | nfsd4_probe_callback(clp); |
2e4b7239 | 974 | spin_unlock(&clp->cl_lock); |
19cf5c02 | 975 | } |
ec6b5d7b | 976 | |
d29c374c | 977 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
c7662518 | 978 | { |
c7662518 | 979 | struct nfsd4_conn *conn; |
ec6b5d7b | 980 | |
c7662518 BF |
981 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
982 | if (!conn) | |
db90681d | 983 | return NULL; |
c7662518 BF |
984 | svc_xprt_get(rqstp->rq_xprt); |
985 | conn->cn_xprt = rqstp->rq_xprt; | |
d29c374c | 986 | conn->cn_flags = flags; |
db90681d BF |
987 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
988 | return conn; | |
989 | } | |
a649637c | 990 | |
328ead28 BF |
991 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
992 | { | |
993 | conn->cn_session = ses; | |
994 | list_add(&conn->cn_persession, &ses->se_conns); | |
ec6b5d7b AA |
995 | } |
996 | ||
db90681d | 997 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
557ce264 | 998 | { |
db90681d | 999 | struct nfs4_client *clp = ses->se_client; |
557ce264 | 1000 | |
c7662518 | 1001 | spin_lock(&clp->cl_lock); |
328ead28 | 1002 | __nfsd4_hash_conn(conn, ses); |
c7662518 | 1003 | spin_unlock(&clp->cl_lock); |
557ce264 AA |
1004 | } |
1005 | ||
21b75b01 | 1006 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
efe0cb6d | 1007 | { |
19cf5c02 | 1008 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
21b75b01 | 1009 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
efe0cb6d BF |
1010 | } |
1011 | ||
e1ff371f | 1012 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
ec6b5d7b | 1013 | { |
21b75b01 | 1014 | int ret; |
ec6b5d7b | 1015 | |
db90681d | 1016 | nfsd4_hash_conn(conn, ses); |
21b75b01 BF |
1017 | ret = nfsd4_register_conn(conn); |
1018 | if (ret) | |
1019 | /* oops; xprt is already down: */ | |
1020 | nfsd4_conn_lost(&conn->cn_xpt_user); | |
6a3b1563 | 1021 | if (conn->cn_flags & NFS4_CDFC4_BACK) { |
24119673 WAA |
1022 | /* callback channel may be back up */ |
1023 | nfsd4_probe_callback(ses->se_client); | |
1024 | } | |
c7662518 | 1025 | } |
ec6b5d7b | 1026 | |
e1ff371f | 1027 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
1d1bc8f2 BF |
1028 | { |
1029 | u32 dir = NFS4_CDFC4_FORE; | |
1030 | ||
e1ff371f | 1031 | if (cses->flags & SESSION4_BACK_CHAN) |
1d1bc8f2 | 1032 | dir |= NFS4_CDFC4_BACK; |
e1ff371f | 1033 | return alloc_conn(rqstp, dir); |
1d1bc8f2 BF |
1034 | } |
1035 | ||
1036 | /* must be called under client_lock */ | |
19cf5c02 | 1037 | static void nfsd4_del_conns(struct nfsd4_session *s) |
c7662518 | 1038 | { |
19cf5c02 BF |
1039 | struct nfs4_client *clp = s->se_client; |
1040 | struct nfsd4_conn *c; | |
ec6b5d7b | 1041 | |
19cf5c02 BF |
1042 | spin_lock(&clp->cl_lock); |
1043 | while (!list_empty(&s->se_conns)) { | |
1044 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); | |
1045 | list_del_init(&c->cn_persession); | |
1046 | spin_unlock(&clp->cl_lock); | |
557ce264 | 1047 | |
19cf5c02 BF |
1048 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
1049 | free_conn(c); | |
ec6b5d7b | 1050 | |
19cf5c02 BF |
1051 | spin_lock(&clp->cl_lock); |
1052 | } | |
1053 | spin_unlock(&clp->cl_lock); | |
c7662518 | 1054 | } |
ec6b5d7b | 1055 | |
1377b69e BF |
1056 | static void __free_session(struct nfsd4_session *ses) |
1057 | { | |
1377b69e BF |
1058 | free_session_slots(ses); |
1059 | kfree(ses); | |
1060 | } | |
1061 | ||
66b2b9b2 | 1062 | static void free_session(struct nfsd4_session *ses) |
c7662518 | 1063 | { |
66b2b9b2 | 1064 | struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id); |
c9a49628 SK |
1065 | |
1066 | lockdep_assert_held(&nn->client_lock); | |
19cf5c02 | 1067 | nfsd4_del_conns(ses); |
55c760cf | 1068 | nfsd4_put_drc_mem(&ses->se_fchannel); |
1377b69e | 1069 | __free_session(ses); |
c7662518 BF |
1070 | } |
1071 | ||
135ae827 | 1072 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
a827bcb2 | 1073 | { |
a827bcb2 | 1074 | int idx; |
1872de0e | 1075 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
a827bcb2 | 1076 | |
ec6b5d7b AA |
1077 | new->se_client = clp; |
1078 | gen_sessionid(new); | |
ec6b5d7b | 1079 | |
c7662518 BF |
1080 | INIT_LIST_HEAD(&new->se_conns); |
1081 | ||
ac7c46f2 | 1082 | new->se_cb_seq_nr = 1; |
ec6b5d7b | 1083 | new->se_flags = cses->flags; |
8b5ce5cd | 1084 | new->se_cb_prog = cses->callback_prog; |
c6bb3ca2 | 1085 | new->se_cb_sec = cses->cb_sec; |
66b2b9b2 | 1086 | atomic_set(&new->se_ref, 0); |
5b6feee9 | 1087 | idx = hash_sessionid(&new->se_sessionid); |
c9a49628 | 1088 | spin_lock(&nn->client_lock); |
1872de0e | 1089 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
4c649378 | 1090 | spin_lock(&clp->cl_lock); |
ec6b5d7b | 1091 | list_add(&new->se_perclnt, &clp->cl_sessions); |
4c649378 | 1092 | spin_unlock(&clp->cl_lock); |
c9a49628 | 1093 | spin_unlock(&nn->client_lock); |
60810e54 | 1094 | |
dcbeaa68 | 1095 | if (cses->flags & SESSION4_BACK_CHAN) { |
edd76786 | 1096 | struct sockaddr *sa = svc_addr(rqstp); |
dcbeaa68 BF |
1097 | /* |
1098 | * This is a little silly; with sessions there's no real | |
1099 | * use for the callback address. Use the peer address | |
1100 | * as a reasonable default for now, but consider fixing | |
1101 | * the rpc client not to require an address in the | |
1102 | * future: | |
1103 | */ | |
edd76786 BF |
1104 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
1105 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); | |
edd76786 | 1106 | } |
ec6b5d7b AA |
1107 | } |
1108 | ||
9089f1b4 | 1109 | /* caller must hold client_lock */ |
5282fd72 | 1110 | static struct nfsd4_session * |
1872de0e | 1111 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
5282fd72 ME |
1112 | { |
1113 | struct nfsd4_session *elem; | |
1114 | int idx; | |
1872de0e | 1115 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
5282fd72 ME |
1116 | |
1117 | dump_sessionid(__func__, sessionid); | |
1118 | idx = hash_sessionid(sessionid); | |
5282fd72 | 1119 | /* Search in the appropriate list */ |
1872de0e | 1120 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
5282fd72 ME |
1121 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
1122 | NFS4_MAX_SESSIONID_LEN)) { | |
1123 | return elem; | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | dprintk("%s: session not found\n", __func__); | |
1128 | return NULL; | |
1129 | } | |
1130 | ||
9089f1b4 | 1131 | /* caller must hold client_lock */ |
7116ed6b | 1132 | static void |
5282fd72 | 1133 | unhash_session(struct nfsd4_session *ses) |
7116ed6b AA |
1134 | { |
1135 | list_del(&ses->se_hash); | |
4c649378 | 1136 | spin_lock(&ses->se_client->cl_lock); |
7116ed6b | 1137 | list_del(&ses->se_perclnt); |
4c649378 | 1138 | spin_unlock(&ses->se_client->cl_lock); |
5282fd72 ME |
1139 | } |
1140 | ||
1da177e4 LT |
1141 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
1142 | static int | |
2c142baa | 1143 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
1da177e4 | 1144 | { |
2c142baa | 1145 | if (clid->cl_boot == nn->boot_time) |
1da177e4 | 1146 | return 0; |
60adfc50 | 1147 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
2c142baa | 1148 | clid->cl_boot, clid->cl_id, nn->boot_time); |
1da177e4 LT |
1149 | return 1; |
1150 | } | |
1151 | ||
1152 | /* | |
1153 | * XXX Should we use a slab cache ? | |
1154 | * This type of memory management is somewhat inefficient, but we use it | |
1155 | * anyway since SETCLIENTID is not a common operation. | |
1156 | */ | |
35bba9a3 | 1157 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
1da177e4 LT |
1158 | { |
1159 | struct nfs4_client *clp; | |
1160 | ||
35bba9a3 BF |
1161 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
1162 | if (clp == NULL) | |
1163 | return NULL; | |
67114fe6 | 1164 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
35bba9a3 BF |
1165 | if (clp->cl_name.data == NULL) { |
1166 | kfree(clp); | |
1167 | return NULL; | |
1da177e4 | 1168 | } |
35bba9a3 | 1169 | clp->cl_name.len = name.len; |
5694c93e TM |
1170 | INIT_LIST_HEAD(&clp->cl_sessions); |
1171 | idr_init(&clp->cl_stateids); | |
1172 | atomic_set(&clp->cl_refcount, 0); | |
1173 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; | |
1174 | INIT_LIST_HEAD(&clp->cl_idhash); | |
1175 | INIT_LIST_HEAD(&clp->cl_openowners); | |
1176 | INIT_LIST_HEAD(&clp->cl_delegations); | |
1177 | INIT_LIST_HEAD(&clp->cl_lru); | |
1178 | INIT_LIST_HEAD(&clp->cl_callbacks); | |
1179 | INIT_LIST_HEAD(&clp->cl_revoked); | |
1180 | spin_lock_init(&clp->cl_lock); | |
1181 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); | |
1da177e4 LT |
1182 | return clp; |
1183 | } | |
1184 | ||
4dd86e15 | 1185 | static void |
1da177e4 LT |
1186 | free_client(struct nfs4_client *clp) |
1187 | { | |
bca0ec65 | 1188 | struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id); |
c9a49628 SK |
1189 | |
1190 | lockdep_assert_held(&nn->client_lock); | |
792c95dd BF |
1191 | while (!list_empty(&clp->cl_sessions)) { |
1192 | struct nfsd4_session *ses; | |
1193 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, | |
1194 | se_perclnt); | |
1195 | list_del(&ses->se_perclnt); | |
66b2b9b2 BF |
1196 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
1197 | free_session(ses); | |
792c95dd | 1198 | } |
4cb57e30 | 1199 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
03a4e1f6 | 1200 | free_svc_cred(&clp->cl_cred); |
1da177e4 | 1201 | kfree(clp->cl_name.data); |
2d32b29a | 1202 | idr_destroy(&clp->cl_stateids); |
1da177e4 LT |
1203 | kfree(clp); |
1204 | } | |
1205 | ||
84d38ac9 BH |
1206 | /* must be called under the client_lock */ |
1207 | static inline void | |
1208 | unhash_client_locked(struct nfs4_client *clp) | |
1209 | { | |
792c95dd BF |
1210 | struct nfsd4_session *ses; |
1211 | ||
84d38ac9 | 1212 | list_del(&clp->cl_lru); |
4c649378 | 1213 | spin_lock(&clp->cl_lock); |
792c95dd BF |
1214 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
1215 | list_del_init(&ses->se_hash); | |
4c649378 | 1216 | spin_unlock(&clp->cl_lock); |
84d38ac9 BH |
1217 | } |
1218 | ||
1da177e4 | 1219 | static void |
0d22f68f | 1220 | destroy_client(struct nfs4_client *clp) |
1da177e4 | 1221 | { |
fe0750e5 | 1222 | struct nfs4_openowner *oo; |
1da177e4 | 1223 | struct nfs4_delegation *dp; |
1da177e4 | 1224 | struct list_head reaplist; |
382a62e7 | 1225 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 1226 | |
1da177e4 | 1227 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 1228 | spin_lock(&state_lock); |
ea1da636 N |
1229 | while (!list_empty(&clp->cl_delegations)) { |
1230 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
ea1da636 | 1231 | list_del_init(&dp->dl_perclnt); |
1da177e4 LT |
1232 | list_move(&dp->dl_recall_lru, &reaplist); |
1233 | } | |
cdc97505 | 1234 | spin_unlock(&state_lock); |
1da177e4 LT |
1235 | while (!list_empty(&reaplist)) { |
1236 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
3bd64a5b | 1237 | destroy_delegation(dp); |
1da177e4 | 1238 | } |
956c4fee BH |
1239 | list_splice_init(&clp->cl_revoked, &reaplist); |
1240 | while (!list_empty(&reaplist)) { | |
1241 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
1242 | destroy_revoked_delegation(dp); | |
1243 | } | |
ea1da636 | 1244 | while (!list_empty(&clp->cl_openowners)) { |
fe0750e5 BF |
1245 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
1246 | release_openowner(oo); | |
1da177e4 | 1247 | } |
6ff8da08 | 1248 | nfsd4_shutdown_callback(clp); |
84d38ac9 BH |
1249 | if (clp->cl_cb_conn.cb_xprt) |
1250 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); | |
36acb66b | 1251 | list_del(&clp->cl_idhash); |
ac55fdc4 | 1252 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
382a62e7 | 1253 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
ac55fdc4 | 1254 | else |
a99454aa | 1255 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
c9a49628 | 1256 | spin_lock(&nn->client_lock); |
84d38ac9 | 1257 | unhash_client_locked(clp); |
221a6876 BF |
1258 | WARN_ON_ONCE(atomic_read(&clp->cl_refcount)); |
1259 | free_client(clp); | |
c9a49628 | 1260 | spin_unlock(&nn->client_lock); |
1da177e4 LT |
1261 | } |
1262 | ||
0d22f68f BF |
1263 | static void expire_client(struct nfs4_client *clp) |
1264 | { | |
1265 | nfsd4_client_record_remove(clp); | |
1266 | destroy_client(clp); | |
1267 | } | |
1268 | ||
35bba9a3 BF |
1269 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
1270 | { | |
1271 | memcpy(target->cl_verifier.data, source->data, | |
1272 | sizeof(target->cl_verifier.data)); | |
1da177e4 LT |
1273 | } |
1274 | ||
35bba9a3 BF |
1275 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
1276 | { | |
1da177e4 LT |
1277 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
1278 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
1279 | } | |
1280 | ||
03a4e1f6 | 1281 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
35bba9a3 | 1282 | { |
03a4e1f6 BF |
1283 | if (source->cr_principal) { |
1284 | target->cr_principal = | |
1285 | kstrdup(source->cr_principal, GFP_KERNEL); | |
1286 | if (target->cr_principal == NULL) | |
1287 | return -ENOMEM; | |
1288 | } else | |
1289 | target->cr_principal = NULL; | |
d5497fc6 | 1290 | target->cr_flavor = source->cr_flavor; |
1da177e4 LT |
1291 | target->cr_uid = source->cr_uid; |
1292 | target->cr_gid = source->cr_gid; | |
1293 | target->cr_group_info = source->cr_group_info; | |
1294 | get_group_info(target->cr_group_info); | |
0dc1531a BF |
1295 | target->cr_gss_mech = source->cr_gss_mech; |
1296 | if (source->cr_gss_mech) | |
1297 | gss_mech_get(source->cr_gss_mech); | |
03a4e1f6 | 1298 | return 0; |
1da177e4 LT |
1299 | } |
1300 | ||
ac55fdc4 JL |
1301 | static long long |
1302 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) | |
1303 | { | |
1304 | long long res; | |
1305 | ||
1306 | res = o1->len - o2->len; | |
1307 | if (res) | |
1308 | return res; | |
1309 | return (long long)memcmp(o1->data, o2->data, o1->len); | |
1310 | } | |
1311 | ||
35bba9a3 | 1312 | static int same_name(const char *n1, const char *n2) |
599e0a22 | 1313 | { |
a55370a3 | 1314 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
1da177e4 LT |
1315 | } |
1316 | ||
1317 | static int | |
599e0a22 BF |
1318 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
1319 | { | |
1320 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); | |
1da177e4 LT |
1321 | } |
1322 | ||
1323 | static int | |
599e0a22 BF |
1324 | same_clid(clientid_t *cl1, clientid_t *cl2) |
1325 | { | |
1326 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); | |
1da177e4 LT |
1327 | } |
1328 | ||
8fbba96e BF |
1329 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
1330 | { | |
1331 | int i; | |
1332 | ||
1333 | if (g1->ngroups != g2->ngroups) | |
1334 | return false; | |
1335 | for (i=0; i<g1->ngroups; i++) | |
6fab8779 | 1336 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
8fbba96e BF |
1337 | return false; |
1338 | return true; | |
1339 | } | |
1340 | ||
68eb3508 BF |
1341 | /* |
1342 | * RFC 3530 language requires clid_inuse be returned when the | |
1343 | * "principal" associated with a requests differs from that previously | |
1344 | * used. We use uid, gid's, and gss principal string as our best | |
1345 | * approximation. We also don't want to allow non-gss use of a client | |
1346 | * established using gss: in theory cr_principal should catch that | |
1347 | * change, but in practice cr_principal can be null even in the gss case | |
1348 | * since gssd doesn't always pass down a principal string. | |
1349 | */ | |
1350 | static bool is_gss_cred(struct svc_cred *cr) | |
1351 | { | |
1352 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ | |
1353 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); | |
1354 | } | |
1355 | ||
1356 | ||
5559b50a | 1357 | static bool |
599e0a22 BF |
1358 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
1359 | { | |
68eb3508 | 1360 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
6fab8779 EB |
1361 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
1362 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) | |
8fbba96e BF |
1363 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
1364 | return false; | |
1365 | if (cr1->cr_principal == cr2->cr_principal) | |
1366 | return true; | |
1367 | if (!cr1->cr_principal || !cr2->cr_principal) | |
1368 | return false; | |
5559b50a | 1369 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
1da177e4 LT |
1370 | } |
1371 | ||
57266a6e BF |
1372 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
1373 | { | |
1374 | struct svc_cred *cr = &rqstp->rq_cred; | |
1375 | u32 service; | |
1376 | ||
c4720591 BF |
1377 | if (!cr->cr_gss_mech) |
1378 | return false; | |
57266a6e BF |
1379 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
1380 | return service == RPC_GSS_SVC_INTEGRITY || | |
1381 | service == RPC_GSS_SVC_PRIVACY; | |
1382 | } | |
1383 | ||
1384 | static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) | |
1385 | { | |
1386 | struct svc_cred *cr = &rqstp->rq_cred; | |
1387 | ||
1388 | if (!cl->cl_mach_cred) | |
1389 | return true; | |
1390 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) | |
1391 | return false; | |
1392 | if (!svc_rqst_integrity_protected(rqstp)) | |
1393 | return false; | |
1394 | if (!cr->cr_principal) | |
1395 | return false; | |
1396 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); | |
1397 | } | |
1398 | ||
c212cecf | 1399 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
5ec7b46c BF |
1400 | { |
1401 | static u32 current_clientid = 1; | |
1402 | ||
2c142baa | 1403 | clp->cl_clientid.cl_boot = nn->boot_time; |
1da177e4 LT |
1404 | clp->cl_clientid.cl_id = current_clientid++; |
1405 | } | |
1406 | ||
deda2faa BF |
1407 | static void gen_confirm(struct nfs4_client *clp) |
1408 | { | |
ab4684d1 | 1409 | __be32 verf[2]; |
deda2faa | 1410 | static u32 i; |
1da177e4 | 1411 | |
f419992c JL |
1412 | /* |
1413 | * This is opaque to client, so no need to byte-swap. Use | |
1414 | * __force to keep sparse happy | |
1415 | */ | |
1416 | verf[0] = (__force __be32)get_seconds(); | |
1417 | verf[1] = (__force __be32)i++; | |
ab4684d1 | 1418 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
1da177e4 LT |
1419 | } |
1420 | ||
38c2f4b1 | 1421 | static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t) |
4581d140 | 1422 | { |
3abdb607 BF |
1423 | struct nfs4_stid *ret; |
1424 | ||
1425 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); | |
1426 | if (!ret || !ret->sc_type) | |
1427 | return NULL; | |
1428 | return ret; | |
4d71ab87 BF |
1429 | } |
1430 | ||
38c2f4b1 | 1431 | static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) |
f459e453 BF |
1432 | { |
1433 | struct nfs4_stid *s; | |
4d71ab87 | 1434 | |
38c2f4b1 | 1435 | s = find_stateid(cl, t); |
4d71ab87 BF |
1436 | if (!s) |
1437 | return NULL; | |
f459e453 | 1438 | if (typemask & s->sc_type) |
4581d140 | 1439 | return s; |
4581d140 BF |
1440 | return NULL; |
1441 | } | |
1442 | ||
2216d449 | 1443 | static struct nfs4_client *create_client(struct xdr_netobj name, |
b09333c4 RL |
1444 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
1445 | { | |
1446 | struct nfs4_client *clp; | |
1447 | struct sockaddr *sa = svc_addr(rqstp); | |
03a4e1f6 | 1448 | int ret; |
c212cecf | 1449 | struct net *net = SVC_NET(rqstp); |
c9a49628 | 1450 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b09333c4 RL |
1451 | |
1452 | clp = alloc_client(name); | |
1453 | if (clp == NULL) | |
1454 | return NULL; | |
1455 | ||
03a4e1f6 BF |
1456 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
1457 | if (ret) { | |
c9a49628 | 1458 | spin_lock(&nn->client_lock); |
03a4e1f6 | 1459 | free_client(clp); |
c9a49628 | 1460 | spin_unlock(&nn->client_lock); |
03a4e1f6 | 1461 | return NULL; |
b09333c4 | 1462 | } |
57725155 | 1463 | nfsd4_init_callback(&clp->cl_cb_null); |
07cd4909 | 1464 | clp->cl_time = get_seconds(); |
b09333c4 | 1465 | clear_bit(0, &clp->cl_cb_slot_busy); |
b09333c4 RL |
1466 | copy_verf(clp, verf); |
1467 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); | |
b09333c4 | 1468 | gen_confirm(clp); |
edd76786 | 1469 | clp->cl_cb_session = NULL; |
c212cecf | 1470 | clp->net = net; |
b09333c4 RL |
1471 | return clp; |
1472 | } | |
1473 | ||
fd39ca9a | 1474 | static void |
ac55fdc4 JL |
1475 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
1476 | { | |
1477 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
1478 | struct nfs4_client *clp; | |
1479 | ||
1480 | while (*new) { | |
1481 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); | |
1482 | parent = *new; | |
1483 | ||
1484 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) | |
1485 | new = &((*new)->rb_left); | |
1486 | else | |
1487 | new = &((*new)->rb_right); | |
1488 | } | |
1489 | ||
1490 | rb_link_node(&new_clp->cl_namenode, parent, new); | |
1491 | rb_insert_color(&new_clp->cl_namenode, root); | |
1492 | } | |
1493 | ||
1494 | static struct nfs4_client * | |
1495 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) | |
1496 | { | |
1497 | long long cmp; | |
1498 | struct rb_node *node = root->rb_node; | |
1499 | struct nfs4_client *clp; | |
1500 | ||
1501 | while (node) { | |
1502 | clp = rb_entry(node, struct nfs4_client, cl_namenode); | |
1503 | cmp = compare_blob(&clp->cl_name, name); | |
1504 | if (cmp > 0) | |
1505 | node = node->rb_left; | |
1506 | else if (cmp < 0) | |
1507 | node = node->rb_right; | |
1508 | else | |
1509 | return clp; | |
1510 | } | |
1511 | return NULL; | |
1512 | } | |
1513 | ||
1514 | static void | |
1515 | add_to_unconfirmed(struct nfs4_client *clp) | |
1da177e4 LT |
1516 | { |
1517 | unsigned int idhashval; | |
0a7ec377 | 1518 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 1519 | |
ac55fdc4 | 1520 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
a99454aa | 1521 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
1da177e4 | 1522 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
0a7ec377 | 1523 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
36acb66b | 1524 | renew_client(clp); |
1da177e4 LT |
1525 | } |
1526 | ||
fd39ca9a | 1527 | static void |
1da177e4 LT |
1528 | move_to_confirmed(struct nfs4_client *clp) |
1529 | { | |
1530 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
8daae4dc | 1531 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 LT |
1532 | |
1533 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); | |
8daae4dc | 1534 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
a99454aa | 1535 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
382a62e7 | 1536 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
ac55fdc4 | 1537 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
1da177e4 LT |
1538 | renew_client(clp); |
1539 | } | |
1540 | ||
1541 | static struct nfs4_client * | |
bfa85e83 | 1542 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
1da177e4 LT |
1543 | { |
1544 | struct nfs4_client *clp; | |
1545 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
1546 | ||
bfa85e83 | 1547 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
a50d2ad1 | 1548 | if (same_clid(&clp->cl_clientid, clid)) { |
d15c077e BF |
1549 | if ((bool)clp->cl_minorversion != sessions) |
1550 | return NULL; | |
a50d2ad1 | 1551 | renew_client(clp); |
1da177e4 | 1552 | return clp; |
a50d2ad1 | 1553 | } |
1da177e4 LT |
1554 | } |
1555 | return NULL; | |
1556 | } | |
1557 | ||
bfa85e83 BF |
1558 | static struct nfs4_client * |
1559 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) | |
1560 | { | |
1561 | struct list_head *tbl = nn->conf_id_hashtbl; | |
1562 | ||
1563 | return find_client_in_id_table(tbl, clid, sessions); | |
1564 | } | |
1565 | ||
1da177e4 | 1566 | static struct nfs4_client * |
0a7ec377 | 1567 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
1da177e4 | 1568 | { |
bfa85e83 | 1569 | struct list_head *tbl = nn->unconf_id_hashtbl; |
1da177e4 | 1570 | |
bfa85e83 | 1571 | return find_client_in_id_table(tbl, clid, sessions); |
1da177e4 LT |
1572 | } |
1573 | ||
6e5f15c9 | 1574 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
a1bcecd2 | 1575 | { |
6e5f15c9 | 1576 | return clp->cl_exchange_flags != 0; |
e203d506 | 1577 | } |
a1bcecd2 | 1578 | |
28ce6054 | 1579 | static struct nfs4_client * |
382a62e7 | 1580 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 1581 | { |
382a62e7 | 1582 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
28ce6054 N |
1583 | } |
1584 | ||
1585 | static struct nfs4_client * | |
a99454aa | 1586 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 1587 | { |
a99454aa | 1588 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
28ce6054 N |
1589 | } |
1590 | ||
fd39ca9a | 1591 | static void |
6f3d772f | 1592 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
1da177e4 | 1593 | { |
07263f1e | 1594 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
6f3d772f TU |
1595 | struct sockaddr *sa = svc_addr(rqstp); |
1596 | u32 scopeid = rpc_get_scope_id(sa); | |
7077ecba JL |
1597 | unsigned short expected_family; |
1598 | ||
1599 | /* Currently, we only support tcp and tcp6 for the callback channel */ | |
1600 | if (se->se_callback_netid_len == 3 && | |
1601 | !memcmp(se->se_callback_netid_val, "tcp", 3)) | |
1602 | expected_family = AF_INET; | |
1603 | else if (se->se_callback_netid_len == 4 && | |
1604 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) | |
1605 | expected_family = AF_INET6; | |
1606 | else | |
1da177e4 LT |
1607 | goto out_err; |
1608 | ||
c212cecf | 1609 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
aa9a4ec7 | 1610 | se->se_callback_addr_len, |
07263f1e BF |
1611 | (struct sockaddr *)&conn->cb_addr, |
1612 | sizeof(conn->cb_addr)); | |
aa9a4ec7 | 1613 | |
07263f1e | 1614 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
1da177e4 | 1615 | goto out_err; |
aa9a4ec7 | 1616 | |
07263f1e BF |
1617 | if (conn->cb_addr.ss_family == AF_INET6) |
1618 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; | |
fbf4665f | 1619 | |
07263f1e BF |
1620 | conn->cb_prog = se->se_callback_prog; |
1621 | conn->cb_ident = se->se_callback_ident; | |
849a1cf1 | 1622 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
1da177e4 LT |
1623 | return; |
1624 | out_err: | |
07263f1e BF |
1625 | conn->cb_addr.ss_family = AF_UNSPEC; |
1626 | conn->cb_addrlen = 0; | |
849823c5 | 1627 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
1da177e4 LT |
1628 | "will not receive delegations\n", |
1629 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); | |
1630 | ||
1da177e4 LT |
1631 | return; |
1632 | } | |
1633 | ||
074fe897 | 1634 | /* |
067e1ace | 1635 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
074fe897 | 1636 | */ |
b607664e | 1637 | static void |
074fe897 | 1638 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
074fe897 | 1639 | { |
f5236013 | 1640 | struct xdr_buf *buf = resp->xdr.buf; |
557ce264 AA |
1641 | struct nfsd4_slot *slot = resp->cstate.slot; |
1642 | unsigned int base; | |
074fe897 | 1643 | |
557ce264 | 1644 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 1645 | |
557ce264 AA |
1646 | slot->sl_opcnt = resp->opcnt; |
1647 | slot->sl_status = resp->cstate.status; | |
074fe897 | 1648 | |
bf5c43c8 | 1649 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
bf864a31 | 1650 | if (nfsd4_not_cached(resp)) { |
557ce264 | 1651 | slot->sl_datalen = 0; |
bf864a31 | 1652 | return; |
074fe897 | 1653 | } |
f5236013 BF |
1654 | base = resp->cstate.data_offset; |
1655 | slot->sl_datalen = buf->len - base; | |
1656 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) | |
557ce264 AA |
1657 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
1658 | return; | |
074fe897 AA |
1659 | } |
1660 | ||
1661 | /* | |
abfabf8c AA |
1662 | * Encode the replay sequence operation from the slot values. |
1663 | * If cachethis is FALSE encode the uncached rep error on the next | |
1664 | * operation which sets resp->p and increments resp->opcnt for | |
1665 | * nfs4svc_encode_compoundres. | |
074fe897 | 1666 | * |
074fe897 | 1667 | */ |
abfabf8c AA |
1668 | static __be32 |
1669 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, | |
1670 | struct nfsd4_compoundres *resp) | |
074fe897 | 1671 | { |
abfabf8c AA |
1672 | struct nfsd4_op *op; |
1673 | struct nfsd4_slot *slot = resp->cstate.slot; | |
bf864a31 | 1674 | |
abfabf8c AA |
1675 | /* Encode the replayed sequence operation */ |
1676 | op = &args->ops[resp->opcnt - 1]; | |
1677 | nfsd4_encode_operation(resp, op); | |
bf864a31 | 1678 | |
abfabf8c | 1679 | /* Return nfserr_retry_uncached_rep in next operation. */ |
73e79482 | 1680 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
abfabf8c AA |
1681 | op = &args->ops[resp->opcnt++]; |
1682 | op->status = nfserr_retry_uncached_rep; | |
1683 | nfsd4_encode_operation(resp, op); | |
074fe897 | 1684 | } |
abfabf8c | 1685 | return op->status; |
074fe897 AA |
1686 | } |
1687 | ||
1688 | /* | |
557ce264 AA |
1689 | * The sequence operation is not cached because we can use the slot and |
1690 | * session values. | |
074fe897 | 1691 | */ |
3ca2eb98 | 1692 | static __be32 |
bf864a31 AA |
1693 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
1694 | struct nfsd4_sequence *seq) | |
074fe897 | 1695 | { |
557ce264 | 1696 | struct nfsd4_slot *slot = resp->cstate.slot; |
f5236013 BF |
1697 | struct xdr_stream *xdr = &resp->xdr; |
1698 | __be32 *p; | |
074fe897 AA |
1699 | __be32 status; |
1700 | ||
557ce264 | 1701 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 1702 | |
abfabf8c | 1703 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
0da7b19c | 1704 | if (status) |
abfabf8c | 1705 | return status; |
074fe897 | 1706 | |
f5236013 BF |
1707 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
1708 | if (!p) { | |
1709 | WARN_ON_ONCE(1); | |
1710 | return nfserr_serverfault; | |
1711 | } | |
1712 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); | |
1713 | xdr_commit_encode(xdr); | |
074fe897 | 1714 | |
557ce264 | 1715 | resp->opcnt = slot->sl_opcnt; |
f5236013 | 1716 | return slot->sl_status; |
074fe897 AA |
1717 | } |
1718 | ||
0733d213 AA |
1719 | /* |
1720 | * Set the exchange_id flags returned by the server. | |
1721 | */ | |
1722 | static void | |
1723 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) | |
1724 | { | |
1725 | /* pNFS is not supported */ | |
1726 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; | |
1727 | ||
1728 | /* Referrals are supported, Migration is not. */ | |
1729 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; | |
1730 | ||
1731 | /* set the wire flags to return to client. */ | |
1732 | clid->flags = new->cl_exchange_flags; | |
1733 | } | |
1734 | ||
631fc9ea BF |
1735 | static bool client_has_state(struct nfs4_client *clp) |
1736 | { | |
1737 | /* | |
1738 | * Note clp->cl_openowners check isn't quite right: there's no | |
1739 | * need to count owners without stateid's. | |
1740 | * | |
1741 | * Also note we should probably be using this in 4.0 case too. | |
1742 | */ | |
6eccece9 BF |
1743 | return !list_empty(&clp->cl_openowners) |
1744 | || !list_empty(&clp->cl_delegations) | |
1745 | || !list_empty(&clp->cl_sessions); | |
631fc9ea BF |
1746 | } |
1747 | ||
069b6ad4 AA |
1748 | __be32 |
1749 | nfsd4_exchange_id(struct svc_rqst *rqstp, | |
1750 | struct nfsd4_compound_state *cstate, | |
1751 | struct nfsd4_exchange_id *exid) | |
1752 | { | |
0733d213 | 1753 | struct nfs4_client *unconf, *conf, *new; |
57b7b43b | 1754 | __be32 status; |
363168b4 | 1755 | char addr_str[INET6_ADDRSTRLEN]; |
0733d213 | 1756 | nfs4_verifier verf = exid->verifier; |
363168b4 | 1757 | struct sockaddr *sa = svc_addr(rqstp); |
83e08fd4 | 1758 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
c212cecf | 1759 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
0733d213 | 1760 | |
363168b4 | 1761 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
0733d213 | 1762 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
363168b4 | 1763 | "ip_addr=%s flags %x, spa_how %d\n", |
0733d213 | 1764 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
363168b4 | 1765 | addr_str, exid->flags, exid->spa_how); |
0733d213 | 1766 | |
a084daf5 | 1767 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
0733d213 AA |
1768 | return nfserr_inval; |
1769 | ||
0733d213 | 1770 | switch (exid->spa_how) { |
57266a6e BF |
1771 | case SP4_MACH_CRED: |
1772 | if (!svc_rqst_integrity_protected(rqstp)) | |
1773 | return nfserr_inval; | |
0733d213 AA |
1774 | case SP4_NONE: |
1775 | break; | |
063b0fb9 BF |
1776 | default: /* checked by xdr code */ |
1777 | WARN_ON_ONCE(1); | |
0733d213 | 1778 | case SP4_SSV: |
dd30333c | 1779 | return nfserr_encr_alg_unsupp; |
0733d213 AA |
1780 | } |
1781 | ||
2dbb269d | 1782 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
0733d213 | 1783 | nfs4_lock_state(); |
382a62e7 | 1784 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
0733d213 | 1785 | if (conf) { |
83e08fd4 BF |
1786 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
1787 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); | |
1788 | ||
136e658d BF |
1789 | if (update) { |
1790 | if (!clp_used_exchangeid(conf)) { /* buggy client */ | |
2dbb269d | 1791 | status = nfserr_inval; |
1a308118 BF |
1792 | goto out; |
1793 | } | |
57266a6e BF |
1794 | if (!mach_creds_match(conf, rqstp)) { |
1795 | status = nfserr_wrong_cred; | |
1796 | goto out; | |
1797 | } | |
136e658d | 1798 | if (!creds_match) { /* case 9 */ |
ea236d07 | 1799 | status = nfserr_perm; |
136e658d BF |
1800 | goto out; |
1801 | } | |
1802 | if (!verfs_match) { /* case 8 */ | |
0733d213 AA |
1803 | status = nfserr_not_same; |
1804 | goto out; | |
1805 | } | |
136e658d BF |
1806 | /* case 6 */ |
1807 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
1808 | new = conf; | |
1809 | goto out_copy; | |
0733d213 | 1810 | } |
136e658d | 1811 | if (!creds_match) { /* case 3 */ |
631fc9ea BF |
1812 | if (client_has_state(conf)) { |
1813 | status = nfserr_clid_inuse; | |
0733d213 AA |
1814 | goto out; |
1815 | } | |
1816 | expire_client(conf); | |
1817 | goto out_new; | |
1818 | } | |
136e658d | 1819 | if (verfs_match) { /* case 2 */ |
0f1ba0ef | 1820 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
136e658d BF |
1821 | new = conf; |
1822 | goto out_copy; | |
1823 | } | |
1824 | /* case 5, client reboot */ | |
136e658d | 1825 | goto out_new; |
6ddbbbfe MS |
1826 | } |
1827 | ||
2dbb269d | 1828 | if (update) { /* case 7 */ |
6ddbbbfe MS |
1829 | status = nfserr_noent; |
1830 | goto out; | |
0733d213 AA |
1831 | } |
1832 | ||
a99454aa | 1833 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
2dbb269d | 1834 | if (unconf) /* case 4, possible retry or client restart */ |
0733d213 | 1835 | expire_client(unconf); |
0733d213 | 1836 | |
2dbb269d | 1837 | /* case 1 (normal case) */ |
0733d213 | 1838 | out_new: |
2216d449 | 1839 | new = create_client(exid->clname, rqstp, &verf); |
0733d213 | 1840 | if (new == NULL) { |
4731030d | 1841 | status = nfserr_jukebox; |
0733d213 AA |
1842 | goto out; |
1843 | } | |
4f540e29 | 1844 | new->cl_minorversion = cstate->minorversion; |
57266a6e | 1845 | new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED); |
0733d213 | 1846 | |
c212cecf | 1847 | gen_clid(new, nn); |
ac55fdc4 | 1848 | add_to_unconfirmed(new); |
0733d213 AA |
1849 | out_copy: |
1850 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; | |
1851 | exid->clientid.cl_id = new->cl_clientid.cl_id; | |
1852 | ||
778df3f0 | 1853 | exid->seqid = new->cl_cs_slot.sl_seqid + 1; |
0733d213 AA |
1854 | nfsd4_set_ex_flags(new, exid); |
1855 | ||
1856 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", | |
49557cc7 | 1857 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
0733d213 AA |
1858 | status = nfs_ok; |
1859 | ||
1860 | out: | |
1861 | nfs4_unlock_state(); | |
0733d213 | 1862 | return status; |
069b6ad4 AA |
1863 | } |
1864 | ||
57b7b43b | 1865 | static __be32 |
88e588d5 | 1866 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
b85d4c01 | 1867 | { |
88e588d5 AA |
1868 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
1869 | slot_seqid); | |
b85d4c01 BH |
1870 | |
1871 | /* The slot is in use, and no response has been sent. */ | |
88e588d5 AA |
1872 | if (slot_inuse) { |
1873 | if (seqid == slot_seqid) | |
b85d4c01 BH |
1874 | return nfserr_jukebox; |
1875 | else | |
1876 | return nfserr_seq_misordered; | |
1877 | } | |
f6d82485 | 1878 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
88e588d5 | 1879 | if (likely(seqid == slot_seqid + 1)) |
b85d4c01 | 1880 | return nfs_ok; |
88e588d5 | 1881 | if (seqid == slot_seqid) |
b85d4c01 | 1882 | return nfserr_replay_cache; |
b85d4c01 BH |
1883 | return nfserr_seq_misordered; |
1884 | } | |
1885 | ||
49557cc7 AA |
1886 | /* |
1887 | * Cache the create session result into the create session single DRC | |
1888 | * slot cache by saving the xdr structure. sl_seqid has been set. | |
1889 | * Do this for solo or embedded create session operations. | |
1890 | */ | |
1891 | static void | |
1892 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, | |
57b7b43b | 1893 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
49557cc7 AA |
1894 | { |
1895 | slot->sl_status = nfserr; | |
1896 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); | |
1897 | } | |
1898 | ||
1899 | static __be32 | |
1900 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, | |
1901 | struct nfsd4_clid_slot *slot) | |
1902 | { | |
1903 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); | |
1904 | return slot->sl_status; | |
1905 | } | |
1906 | ||
1b74c25b MJ |
1907 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
1908 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ | |
1909 | 1 + /* MIN tag is length with zero, only length */ \ | |
1910 | 3 + /* version, opcount, opcode */ \ | |
1911 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
1912 | /* seqid, slotID, slotID, cache */ \ | |
1913 | 4 ) * sizeof(__be32)) | |
1914 | ||
1915 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ | |
1916 | 2 + /* verifier: AUTH_NULL, length 0 */\ | |
1917 | 1 + /* status */ \ | |
1918 | 1 + /* MIN tag is length with zero, only length */ \ | |
1919 | 3 + /* opcount, opcode, opstatus*/ \ | |
1920 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
1921 | /* seqid, slotID, slotID, slotID, status */ \ | |
1922 | 5 ) * sizeof(__be32)) | |
1923 | ||
55c760cf | 1924 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
1b74c25b | 1925 | { |
55c760cf BF |
1926 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
1927 | ||
373cd409 BF |
1928 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
1929 | return nfserr_toosmall; | |
1930 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) | |
1931 | return nfserr_toosmall; | |
55c760cf BF |
1932 | ca->headerpadsz = 0; |
1933 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); | |
1934 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); | |
1935 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); | |
1936 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, | |
1937 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); | |
1938 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); | |
1939 | /* | |
1940 | * Note decreasing slot size below client's request may make it | |
1941 | * difficult for client to function correctly, whereas | |
1942 | * decreasing the number of slots will (just?) affect | |
1943 | * performance. When short on memory we therefore prefer to | |
1944 | * decrease number of slots instead of their size. Clients that | |
1945 | * request larger slots than they need will get poor results: | |
1946 | */ | |
1947 | ca->maxreqs = nfsd4_get_drc_mem(ca); | |
1948 | if (!ca->maxreqs) | |
1949 | return nfserr_jukebox; | |
1950 | ||
373cd409 | 1951 | return nfs_ok; |
1b74c25b MJ |
1952 | } |
1953 | ||
8a891633 KM |
1954 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
1955 | RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32)) | |
1956 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ | |
1957 | RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32)) | |
1958 | ||
06b332a5 | 1959 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
1b74c25b | 1960 | { |
06b332a5 BF |
1961 | ca->headerpadsz = 0; |
1962 | ||
1963 | /* | |
1964 | * These RPC_MAX_HEADER macros are overkill, especially since we | |
1965 | * don't even do gss on the backchannel yet. But this is still | |
1966 | * less than 1k. Tighten up this estimate in the unlikely event | |
1967 | * it turns out to be a problem for some client: | |
1968 | */ | |
8a891633 | 1969 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
06b332a5 | 1970 | return nfserr_toosmall; |
8a891633 | 1971 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
06b332a5 BF |
1972 | return nfserr_toosmall; |
1973 | ca->maxresp_cached = 0; | |
1974 | if (ca->maxops < 2) | |
1975 | return nfserr_toosmall; | |
1976 | ||
1977 | return nfs_ok; | |
1b74c25b MJ |
1978 | } |
1979 | ||
b78724b7 BF |
1980 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
1981 | { | |
1982 | switch (cbs->flavor) { | |
1983 | case RPC_AUTH_NULL: | |
1984 | case RPC_AUTH_UNIX: | |
1985 | return nfs_ok; | |
1986 | default: | |
1987 | /* | |
1988 | * GSS case: the spec doesn't allow us to return this | |
1989 | * error. But it also doesn't allow us not to support | |
1990 | * GSS. | |
1991 | * I'd rather this fail hard than return some error the | |
1992 | * client might think it can already handle: | |
1993 | */ | |
1994 | return nfserr_encr_alg_unsupp; | |
1995 | } | |
1996 | } | |
1997 | ||
069b6ad4 AA |
1998 | __be32 |
1999 | nfsd4_create_session(struct svc_rqst *rqstp, | |
2000 | struct nfsd4_compound_state *cstate, | |
2001 | struct nfsd4_create_session *cr_ses) | |
2002 | { | |
363168b4 | 2003 | struct sockaddr *sa = svc_addr(rqstp); |
ec6b5d7b | 2004 | struct nfs4_client *conf, *unconf; |
ac7c46f2 | 2005 | struct nfsd4_session *new; |
81f0b2a4 | 2006 | struct nfsd4_conn *conn; |
49557cc7 | 2007 | struct nfsd4_clid_slot *cs_slot = NULL; |
57b7b43b | 2008 | __be32 status = 0; |
8daae4dc | 2009 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
ec6b5d7b | 2010 | |
a62573dc MJ |
2011 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
2012 | return nfserr_inval; | |
b78724b7 BF |
2013 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
2014 | if (status) | |
2015 | return status; | |
55c760cf | 2016 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
06b332a5 BF |
2017 | if (status) |
2018 | return status; | |
2019 | status = check_backchannel_attrs(&cr_ses->back_channel); | |
373cd409 | 2020 | if (status) |
f403e450 | 2021 | goto out_release_drc_mem; |
81f0b2a4 | 2022 | status = nfserr_jukebox; |
60810e54 | 2023 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
55c760cf BF |
2024 | if (!new) |
2025 | goto out_release_drc_mem; | |
81f0b2a4 BF |
2026 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
2027 | if (!conn) | |
2028 | goto out_free_session; | |
a62573dc | 2029 | |
ec6b5d7b | 2030 | nfs4_lock_state(); |
0a7ec377 | 2031 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
8daae4dc | 2032 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
78389046 | 2033 | WARN_ON_ONCE(conf && unconf); |
ec6b5d7b AA |
2034 | |
2035 | if (conf) { | |
57266a6e BF |
2036 | status = nfserr_wrong_cred; |
2037 | if (!mach_creds_match(conf, rqstp)) | |
2038 | goto out_free_conn; | |
49557cc7 AA |
2039 | cs_slot = &conf->cl_cs_slot; |
2040 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 | 2041 | if (status == nfserr_replay_cache) { |
49557cc7 | 2042 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
81f0b2a4 | 2043 | goto out_free_conn; |
49557cc7 | 2044 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
ec6b5d7b | 2045 | status = nfserr_seq_misordered; |
81f0b2a4 | 2046 | goto out_free_conn; |
ec6b5d7b | 2047 | } |
ec6b5d7b | 2048 | } else if (unconf) { |
8f9d3d3b | 2049 | struct nfs4_client *old; |
ec6b5d7b | 2050 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
363168b4 | 2051 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
ec6b5d7b | 2052 | status = nfserr_clid_inuse; |
81f0b2a4 | 2053 | goto out_free_conn; |
ec6b5d7b | 2054 | } |
57266a6e BF |
2055 | status = nfserr_wrong_cred; |
2056 | if (!mach_creds_match(unconf, rqstp)) | |
2057 | goto out_free_conn; | |
49557cc7 AA |
2058 | cs_slot = &unconf->cl_cs_slot; |
2059 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 AA |
2060 | if (status) { |
2061 | /* an unconfirmed replay returns misordered */ | |
ec6b5d7b | 2062 | status = nfserr_seq_misordered; |
81f0b2a4 | 2063 | goto out_free_conn; |
ec6b5d7b | 2064 | } |
382a62e7 | 2065 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
221a6876 BF |
2066 | if (old) { |
2067 | status = mark_client_expired(old); | |
2068 | if (status) | |
2069 | goto out_free_conn; | |
8f9d3d3b | 2070 | expire_client(old); |
221a6876 | 2071 | } |
8f9d3d3b | 2072 | move_to_confirmed(unconf); |
ec6b5d7b AA |
2073 | conf = unconf; |
2074 | } else { | |
2075 | status = nfserr_stale_clientid; | |
81f0b2a4 | 2076 | goto out_free_conn; |
ec6b5d7b | 2077 | } |
81f0b2a4 | 2078 | status = nfs_ok; |
408b79bc BF |
2079 | /* |
2080 | * We do not support RDMA or persistent sessions | |
2081 | */ | |
2082 | cr_ses->flags &= ~SESSION4_PERSIST; | |
2083 | cr_ses->flags &= ~SESSION4_RDMA; | |
2084 | ||
81f0b2a4 BF |
2085 | init_session(rqstp, new, conf, cr_ses); |
2086 | nfsd4_init_conn(rqstp, conn, new); | |
2087 | ||
ac7c46f2 | 2088 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
ec6b5d7b | 2089 | NFS4_MAX_SESSIONID_LEN); |
86c3e16c | 2090 | cs_slot->sl_seqid++; |
49557cc7 | 2091 | cr_ses->seqid = cs_slot->sl_seqid; |
ec6b5d7b | 2092 | |
49557cc7 AA |
2093 | /* cache solo and embedded create sessions under the state lock */ |
2094 | nfsd4_cache_create_session(cr_ses, cs_slot, status); | |
ec6b5d7b | 2095 | nfs4_unlock_state(); |
ec6b5d7b | 2096 | return status; |
81f0b2a4 | 2097 | out_free_conn: |
266533c6 | 2098 | nfs4_unlock_state(); |
81f0b2a4 BF |
2099 | free_conn(conn); |
2100 | out_free_session: | |
2101 | __free_session(new); | |
55c760cf BF |
2102 | out_release_drc_mem: |
2103 | nfsd4_put_drc_mem(&cr_ses->fore_channel); | |
1ca50792 | 2104 | return status; |
069b6ad4 AA |
2105 | } |
2106 | ||
1d1bc8f2 BF |
2107 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
2108 | { | |
2109 | switch (*dir) { | |
2110 | case NFS4_CDFC4_FORE: | |
2111 | case NFS4_CDFC4_BACK: | |
2112 | return nfs_ok; | |
2113 | case NFS4_CDFC4_FORE_OR_BOTH: | |
2114 | case NFS4_CDFC4_BACK_OR_BOTH: | |
2115 | *dir = NFS4_CDFC4_BOTH; | |
2116 | return nfs_ok; | |
2117 | }; | |
2118 | return nfserr_inval; | |
2119 | } | |
2120 | ||
cb73a9f4 BF |
2121 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
2122 | { | |
2123 | struct nfsd4_session *session = cstate->session; | |
c9a49628 | 2124 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
b78724b7 | 2125 | __be32 status; |
cb73a9f4 | 2126 | |
b78724b7 BF |
2127 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
2128 | if (status) | |
2129 | return status; | |
c9a49628 | 2130 | spin_lock(&nn->client_lock); |
cb73a9f4 BF |
2131 | session->se_cb_prog = bc->bc_cb_program; |
2132 | session->se_cb_sec = bc->bc_cb_sec; | |
c9a49628 | 2133 | spin_unlock(&nn->client_lock); |
cb73a9f4 BF |
2134 | |
2135 | nfsd4_probe_callback(session->se_client); | |
2136 | ||
2137 | return nfs_ok; | |
2138 | } | |
2139 | ||
1d1bc8f2 BF |
2140 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
2141 | struct nfsd4_compound_state *cstate, | |
2142 | struct nfsd4_bind_conn_to_session *bcts) | |
2143 | { | |
2144 | __be32 status; | |
3ba63671 | 2145 | struct nfsd4_conn *conn; |
4f6e6c17 | 2146 | struct nfsd4_session *session; |
c9a49628 | 2147 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1d1bc8f2 BF |
2148 | |
2149 | if (!nfsd4_last_compound_op(rqstp)) | |
2150 | return nfserr_not_only_op; | |
4f6e6c17 | 2151 | nfs4_lock_state(); |
c9a49628 | 2152 | spin_lock(&nn->client_lock); |
4f6e6c17 | 2153 | session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp)); |
c9a49628 | 2154 | spin_unlock(&nn->client_lock); |
4f6e6c17 BF |
2155 | status = nfserr_badsession; |
2156 | if (!session) | |
2157 | goto out; | |
57266a6e BF |
2158 | status = nfserr_wrong_cred; |
2159 | if (!mach_creds_match(session->se_client, rqstp)) | |
2160 | goto out; | |
1d1bc8f2 | 2161 | status = nfsd4_map_bcts_dir(&bcts->dir); |
3ba63671 | 2162 | if (status) |
4f6e6c17 | 2163 | goto out; |
3ba63671 | 2164 | conn = alloc_conn(rqstp, bcts->dir); |
4f6e6c17 | 2165 | status = nfserr_jukebox; |
3ba63671 | 2166 | if (!conn) |
4f6e6c17 BF |
2167 | goto out; |
2168 | nfsd4_init_conn(rqstp, conn, session); | |
2169 | status = nfs_ok; | |
2170 | out: | |
2171 | nfs4_unlock_state(); | |
2172 | return status; | |
1d1bc8f2 BF |
2173 | } |
2174 | ||
5d4cec2f BF |
2175 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
2176 | { | |
2177 | if (!session) | |
2178 | return 0; | |
2179 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); | |
2180 | } | |
2181 | ||
069b6ad4 AA |
2182 | __be32 |
2183 | nfsd4_destroy_session(struct svc_rqst *r, | |
2184 | struct nfsd4_compound_state *cstate, | |
2185 | struct nfsd4_destroy_session *sessionid) | |
2186 | { | |
e10e0cfc | 2187 | struct nfsd4_session *ses; |
abcdff09 | 2188 | __be32 status; |
f0f51f5c | 2189 | int ref_held_by_me = 0; |
c9a49628 | 2190 | struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id); |
e10e0cfc | 2191 | |
abcdff09 BF |
2192 | nfs4_lock_state(); |
2193 | status = nfserr_not_only_op; | |
5d4cec2f | 2194 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
57716355 | 2195 | if (!nfsd4_last_compound_op(r)) |
abcdff09 | 2196 | goto out; |
f0f51f5c | 2197 | ref_held_by_me++; |
57716355 | 2198 | } |
e10e0cfc | 2199 | dump_sessionid(__func__, &sessionid->sessionid); |
c9a49628 | 2200 | spin_lock(&nn->client_lock); |
1872de0e | 2201 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r)); |
abcdff09 BF |
2202 | status = nfserr_badsession; |
2203 | if (!ses) | |
2204 | goto out_client_lock; | |
57266a6e BF |
2205 | status = nfserr_wrong_cred; |
2206 | if (!mach_creds_match(ses->se_client, r)) | |
66b2b9b2 | 2207 | goto out_client_lock; |
f0f51f5c BF |
2208 | nfsd4_get_session_locked(ses); |
2209 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); | |
66b2b9b2 | 2210 | if (status) |
f0f51f5c | 2211 | goto out_put_session; |
e10e0cfc | 2212 | unhash_session(ses); |
c9a49628 | 2213 | spin_unlock(&nn->client_lock); |
e10e0cfc | 2214 | |
84f5f7cc | 2215 | nfsd4_probe_callback_sync(ses->se_client); |
19cf5c02 | 2216 | |
c9a49628 | 2217 | spin_lock(&nn->client_lock); |
e10e0cfc | 2218 | status = nfs_ok; |
f0f51f5c BF |
2219 | out_put_session: |
2220 | nfsd4_put_session(ses); | |
abcdff09 BF |
2221 | out_client_lock: |
2222 | spin_unlock(&nn->client_lock); | |
e10e0cfc | 2223 | out: |
abcdff09 | 2224 | nfs4_unlock_state(); |
e10e0cfc | 2225 | return status; |
069b6ad4 AA |
2226 | } |
2227 | ||
a663bdd8 | 2228 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
328ead28 BF |
2229 | { |
2230 | struct nfsd4_conn *c; | |
2231 | ||
2232 | list_for_each_entry(c, &s->se_conns, cn_persession) { | |
a663bdd8 | 2233 | if (c->cn_xprt == xpt) { |
328ead28 BF |
2234 | return c; |
2235 | } | |
2236 | } | |
2237 | return NULL; | |
2238 | } | |
2239 | ||
57266a6e | 2240 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
328ead28 BF |
2241 | { |
2242 | struct nfs4_client *clp = ses->se_client; | |
a663bdd8 | 2243 | struct nfsd4_conn *c; |
57266a6e | 2244 | __be32 status = nfs_ok; |
21b75b01 | 2245 | int ret; |
328ead28 BF |
2246 | |
2247 | spin_lock(&clp->cl_lock); | |
a663bdd8 | 2248 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
57266a6e BF |
2249 | if (c) |
2250 | goto out_free; | |
2251 | status = nfserr_conn_not_bound_to_session; | |
2252 | if (clp->cl_mach_cred) | |
2253 | goto out_free; | |
328ead28 BF |
2254 | __nfsd4_hash_conn(new, ses); |
2255 | spin_unlock(&clp->cl_lock); | |
21b75b01 BF |
2256 | ret = nfsd4_register_conn(new); |
2257 | if (ret) | |
2258 | /* oops; xprt is already down: */ | |
2259 | nfsd4_conn_lost(&new->cn_xpt_user); | |
57266a6e BF |
2260 | return nfs_ok; |
2261 | out_free: | |
2262 | spin_unlock(&clp->cl_lock); | |
2263 | free_conn(new); | |
2264 | return status; | |
328ead28 BF |
2265 | } |
2266 | ||
868b89c3 MJ |
2267 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
2268 | { | |
2269 | struct nfsd4_compoundargs *args = rqstp->rq_argp; | |
2270 | ||
2271 | return args->opcnt > session->se_fchannel.maxops; | |
2272 | } | |
2273 | ||
ae82a8d0 MJ |
2274 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
2275 | struct nfsd4_session *session) | |
2276 | { | |
2277 | struct xdr_buf *xb = &rqstp->rq_arg; | |
2278 | ||
2279 | return xb->len > session->se_fchannel.maxreq_sz; | |
2280 | } | |
2281 | ||
069b6ad4 | 2282 | __be32 |
b85d4c01 | 2283 | nfsd4_sequence(struct svc_rqst *rqstp, |
069b6ad4 AA |
2284 | struct nfsd4_compound_state *cstate, |
2285 | struct nfsd4_sequence *seq) | |
2286 | { | |
f9bb94c4 | 2287 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
47ee5298 | 2288 | struct xdr_stream *xdr = &resp->xdr; |
b85d4c01 | 2289 | struct nfsd4_session *session; |
221a6876 | 2290 | struct nfs4_client *clp; |
b85d4c01 | 2291 | struct nfsd4_slot *slot; |
a663bdd8 | 2292 | struct nfsd4_conn *conn; |
57b7b43b | 2293 | __be32 status; |
47ee5298 | 2294 | int buflen; |
c9a49628 | 2295 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
b85d4c01 | 2296 | |
f9bb94c4 AA |
2297 | if (resp->opcnt != 1) |
2298 | return nfserr_sequence_pos; | |
2299 | ||
a663bdd8 BF |
2300 | /* |
2301 | * Will be either used or freed by nfsd4_sequence_check_conn | |
2302 | * below. | |
2303 | */ | |
2304 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); | |
2305 | if (!conn) | |
2306 | return nfserr_jukebox; | |
2307 | ||
c9a49628 | 2308 | spin_lock(&nn->client_lock); |
b85d4c01 | 2309 | status = nfserr_badsession; |
1872de0e | 2310 | session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp)); |
b85d4c01 | 2311 | if (!session) |
221a6876 BF |
2312 | goto out_no_session; |
2313 | clp = session->se_client; | |
2314 | status = get_client_locked(clp); | |
2315 | if (status) | |
2316 | goto out_no_session; | |
66b2b9b2 BF |
2317 | status = nfsd4_get_session_locked(session); |
2318 | if (status) | |
2319 | goto out_put_client; | |
b85d4c01 | 2320 | |
868b89c3 MJ |
2321 | status = nfserr_too_many_ops; |
2322 | if (nfsd4_session_too_many_ops(rqstp, session)) | |
66b2b9b2 | 2323 | goto out_put_session; |
868b89c3 | 2324 | |
ae82a8d0 MJ |
2325 | status = nfserr_req_too_big; |
2326 | if (nfsd4_request_too_big(rqstp, session)) | |
66b2b9b2 | 2327 | goto out_put_session; |
ae82a8d0 | 2328 | |
b85d4c01 | 2329 | status = nfserr_badslot; |
6c18ba9f | 2330 | if (seq->slotid >= session->se_fchannel.maxreqs) |
66b2b9b2 | 2331 | goto out_put_session; |
b85d4c01 | 2332 | |
557ce264 | 2333 | slot = session->se_slots[seq->slotid]; |
b85d4c01 BH |
2334 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
2335 | ||
a8dfdaeb AA |
2336 | /* We do not negotiate the number of slots yet, so set the |
2337 | * maxslots to the session maxreqs which is used to encode | |
2338 | * sr_highest_slotid and the sr_target_slot id to maxslots */ | |
2339 | seq->maxslots = session->se_fchannel.maxreqs; | |
2340 | ||
73e79482 BF |
2341 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
2342 | slot->sl_flags & NFSD4_SLOT_INUSE); | |
b85d4c01 | 2343 | if (status == nfserr_replay_cache) { |
bf5c43c8 BF |
2344 | status = nfserr_seq_misordered; |
2345 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) | |
66b2b9b2 | 2346 | goto out_put_session; |
b85d4c01 BH |
2347 | cstate->slot = slot; |
2348 | cstate->session = session; | |
da3846a2 | 2349 | /* Return the cached reply status and set cstate->status |
557ce264 | 2350 | * for nfsd4_proc_compound processing */ |
bf864a31 | 2351 | status = nfsd4_replay_cache_entry(resp, seq); |
da3846a2 | 2352 | cstate->status = nfserr_replay_cache; |
aaf84eb9 | 2353 | goto out; |
b85d4c01 BH |
2354 | } |
2355 | if (status) | |
66b2b9b2 | 2356 | goto out_put_session; |
b85d4c01 | 2357 | |
57266a6e | 2358 | status = nfsd4_sequence_check_conn(conn, session); |
a663bdd8 | 2359 | conn = NULL; |
57266a6e BF |
2360 | if (status) |
2361 | goto out_put_session; | |
328ead28 | 2362 | |
47ee5298 BF |
2363 | buflen = (seq->cachethis) ? |
2364 | session->se_fchannel.maxresp_cached : | |
2365 | session->se_fchannel.maxresp_sz; | |
2366 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : | |
2367 | nfserr_rep_too_big; | |
a5cddc88 | 2368 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
47ee5298 | 2369 | goto out_put_session; |
32aaa62e | 2370 | svc_reserve(rqstp, buflen); |
47ee5298 BF |
2371 | |
2372 | status = nfs_ok; | |
b85d4c01 | 2373 | /* Success! bump slot seqid */ |
b85d4c01 | 2374 | slot->sl_seqid = seq->seqid; |
bf5c43c8 | 2375 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
73e79482 BF |
2376 | if (seq->cachethis) |
2377 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; | |
bf5c43c8 BF |
2378 | else |
2379 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; | |
b85d4c01 BH |
2380 | |
2381 | cstate->slot = slot; | |
2382 | cstate->session = session; | |
2383 | ||
b85d4c01 | 2384 | out: |
221a6876 BF |
2385 | switch (clp->cl_cb_state) { |
2386 | case NFSD4_CB_DOWN: | |
2387 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; | |
2388 | break; | |
2389 | case NFSD4_CB_FAULT: | |
2390 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; | |
2391 | break; | |
2392 | default: | |
2393 | seq->status_flags = 0; | |
aaf84eb9 | 2394 | } |
3bd64a5b BF |
2395 | if (!list_empty(&clp->cl_revoked)) |
2396 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; | |
221a6876 | 2397 | out_no_session: |
3f42d2c4 KM |
2398 | if (conn) |
2399 | free_conn(conn); | |
c9a49628 | 2400 | spin_unlock(&nn->client_lock); |
b85d4c01 | 2401 | return status; |
66b2b9b2 BF |
2402 | out_put_session: |
2403 | nfsd4_put_session(session); | |
221a6876 BF |
2404 | out_put_client: |
2405 | put_client_renew_locked(clp); | |
2406 | goto out_no_session; | |
069b6ad4 AA |
2407 | } |
2408 | ||
b607664e TM |
2409 | void |
2410 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) | |
2411 | { | |
2412 | struct nfsd4_compound_state *cs = &resp->cstate; | |
2413 | ||
2414 | if (nfsd4_has_session(cs)) { | |
2415 | struct nfs4_client *clp = cs->session->se_client; | |
2416 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2417 | ||
2418 | if (cs->status != nfserr_replay_cache) { | |
2419 | nfsd4_store_cache_entry(resp); | |
2420 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; | |
2421 | } | |
2422 | /* Renew the clientid on success and on replay */ | |
2423 | spin_lock(&nn->client_lock); | |
2424 | nfsd4_put_session(cs->session); | |
2425 | put_client_renew_locked(clp); | |
2426 | spin_unlock(&nn->client_lock); | |
2427 | } | |
2428 | } | |
2429 | ||
345c2842 MJ |
2430 | __be32 |
2431 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) | |
2432 | { | |
2433 | struct nfs4_client *conf, *unconf, *clp; | |
57b7b43b | 2434 | __be32 status = 0; |
8daae4dc | 2435 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
345c2842 MJ |
2436 | |
2437 | nfs4_lock_state(); | |
0a7ec377 | 2438 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
8daae4dc | 2439 | conf = find_confirmed_client(&dc->clientid, true, nn); |
78389046 | 2440 | WARN_ON_ONCE(conf && unconf); |
345c2842 MJ |
2441 | |
2442 | if (conf) { | |
2443 | clp = conf; | |
2444 | ||
c0293b01 | 2445 | if (client_has_state(conf)) { |
345c2842 MJ |
2446 | status = nfserr_clientid_busy; |
2447 | goto out; | |
2448 | } | |
2449 | } else if (unconf) | |
2450 | clp = unconf; | |
2451 | else { | |
2452 | status = nfserr_stale_clientid; | |
2453 | goto out; | |
2454 | } | |
57266a6e BF |
2455 | if (!mach_creds_match(clp, rqstp)) { |
2456 | status = nfserr_wrong_cred; | |
2457 | goto out; | |
2458 | } | |
345c2842 MJ |
2459 | expire_client(clp); |
2460 | out: | |
2461 | nfs4_unlock_state(); | |
345c2842 MJ |
2462 | return status; |
2463 | } | |
2464 | ||
4dc6ec00 BF |
2465 | __be32 |
2466 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) | |
2467 | { | |
57b7b43b | 2468 | __be32 status = 0; |
bcecf1cc | 2469 | |
4dc6ec00 BF |
2470 | if (rc->rca_one_fs) { |
2471 | if (!cstate->current_fh.fh_dentry) | |
2472 | return nfserr_nofilehandle; | |
2473 | /* | |
2474 | * We don't take advantage of the rca_one_fs case. | |
2475 | * That's OK, it's optional, we can safely ignore it. | |
2476 | */ | |
2477 | return nfs_ok; | |
2478 | } | |
bcecf1cc | 2479 | |
4dc6ec00 | 2480 | nfs4_lock_state(); |
bcecf1cc | 2481 | status = nfserr_complete_already; |
a52d726b JL |
2482 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
2483 | &cstate->session->se_client->cl_flags)) | |
bcecf1cc MJ |
2484 | goto out; |
2485 | ||
2486 | status = nfserr_stale_clientid; | |
2487 | if (is_client_expired(cstate->session->se_client)) | |
4dc6ec00 BF |
2488 | /* |
2489 | * The following error isn't really legal. | |
2490 | * But we only get here if the client just explicitly | |
2491 | * destroyed the client. Surely it no longer cares what | |
2492 | * error it gets back on an operation for the dead | |
2493 | * client. | |
2494 | */ | |
bcecf1cc MJ |
2495 | goto out; |
2496 | ||
2497 | status = nfs_ok; | |
2a4317c5 | 2498 | nfsd4_client_record_create(cstate->session->se_client); |
bcecf1cc | 2499 | out: |
4dc6ec00 | 2500 | nfs4_unlock_state(); |
bcecf1cc | 2501 | return status; |
4dc6ec00 BF |
2502 | } |
2503 | ||
b37ad28b | 2504 | __be32 |
b591480b BF |
2505 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
2506 | struct nfsd4_setclientid *setclid) | |
1da177e4 | 2507 | { |
a084daf5 | 2508 | struct xdr_netobj clname = setclid->se_name; |
1da177e4 | 2509 | nfs4_verifier clverifier = setclid->se_verf; |
28ce6054 | 2510 | struct nfs4_client *conf, *unconf, *new; |
b37ad28b | 2511 | __be32 status; |
c212cecf SK |
2512 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
2513 | ||
63db4632 | 2514 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
1da177e4 | 2515 | nfs4_lock_state(); |
382a62e7 | 2516 | conf = find_confirmed_client_by_name(&clname, nn); |
28ce6054 | 2517 | if (conf) { |
63db4632 | 2518 | /* case 0: */ |
1da177e4 | 2519 | status = nfserr_clid_inuse; |
e203d506 BF |
2520 | if (clp_used_exchangeid(conf)) |
2521 | goto out; | |
026722c2 | 2522 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
363168b4 JL |
2523 | char addr_str[INET6_ADDRSTRLEN]; |
2524 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, | |
2525 | sizeof(addr_str)); | |
2526 | dprintk("NFSD: setclientid: string in use by client " | |
2527 | "at %s\n", addr_str); | |
1da177e4 LT |
2528 | goto out; |
2529 | } | |
1da177e4 | 2530 | } |
a99454aa | 2531 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
8f930711 BF |
2532 | if (unconf) |
2533 | expire_client(unconf); | |
3e772463 | 2534 | status = nfserr_jukebox; |
2216d449 | 2535 | new = create_client(clname, rqstp, &clverifier); |
8f930711 BF |
2536 | if (new == NULL) |
2537 | goto out; | |
34b232bb | 2538 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) |
63db4632 | 2539 | /* case 1: probable callback update */ |
1da177e4 | 2540 | copy_clid(new, conf); |
34b232bb | 2541 | else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
c212cecf | 2542 | gen_clid(new, nn); |
8323c3b2 | 2543 | new->cl_minorversion = 0; |
6f3d772f | 2544 | gen_callback(new, setclid, rqstp); |
ac55fdc4 | 2545 | add_to_unconfirmed(new); |
1da177e4 LT |
2546 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
2547 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
2548 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
2549 | status = nfs_ok; | |
2550 | out: | |
2551 | nfs4_unlock_state(); | |
2552 | return status; | |
2553 | } | |
2554 | ||
2555 | ||
b37ad28b | 2556 | __be32 |
b591480b BF |
2557 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
2558 | struct nfsd4_compound_state *cstate, | |
2559 | struct nfsd4_setclientid_confirm *setclientid_confirm) | |
1da177e4 | 2560 | { |
21ab45a4 | 2561 | struct nfs4_client *conf, *unconf; |
1da177e4 LT |
2562 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
2563 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
b37ad28b | 2564 | __be32 status; |
7f2210fa | 2565 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 2566 | |
2c142baa | 2567 | if (STALE_CLIENTID(clid, nn)) |
1da177e4 | 2568 | return nfserr_stale_clientid; |
1da177e4 | 2569 | nfs4_lock_state(); |
21ab45a4 | 2570 | |
8daae4dc | 2571 | conf = find_confirmed_client(clid, false, nn); |
0a7ec377 | 2572 | unconf = find_unconfirmed_client(clid, false, nn); |
a186e767 | 2573 | /* |
8695b90a BF |
2574 | * We try hard to give out unique clientid's, so if we get an |
2575 | * attempt to confirm the same clientid with a different cred, | |
2576 | * there's a bug somewhere. Let's charitably assume it's our | |
2577 | * bug. | |
a186e767 | 2578 | */ |
8695b90a BF |
2579 | status = nfserr_serverfault; |
2580 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) | |
2581 | goto out; | |
2582 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) | |
2583 | goto out; | |
63db4632 | 2584 | /* cases below refer to rfc 3530 section 14.2.34: */ |
90d700b7 BF |
2585 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
2586 | if (conf && !unconf) /* case 2: probable retransmit */ | |
1da177e4 | 2587 | status = nfs_ok; |
90d700b7 BF |
2588 | else /* case 4: client hasn't noticed we rebooted yet? */ |
2589 | status = nfserr_stale_clientid; | |
2590 | goto out; | |
2591 | } | |
2592 | status = nfs_ok; | |
2593 | if (conf) { /* case 1: callback update */ | |
8695b90a BF |
2594 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
2595 | nfsd4_probe_callback(conf); | |
2596 | expire_client(unconf); | |
90d700b7 | 2597 | } else { /* case 3: normal case; new or rebooted client */ |
382a62e7 | 2598 | conf = find_confirmed_client_by_name(&unconf->cl_name, nn); |
221a6876 BF |
2599 | if (conf) { |
2600 | status = mark_client_expired(conf); | |
2601 | if (status) | |
2602 | goto out; | |
8695b90a | 2603 | expire_client(conf); |
221a6876 | 2604 | } |
8695b90a | 2605 | move_to_confirmed(unconf); |
f3d03b92 | 2606 | nfsd4_probe_callback(unconf); |
08e8987c | 2607 | } |
1da177e4 | 2608 | out: |
1da177e4 LT |
2609 | nfs4_unlock_state(); |
2610 | return status; | |
2611 | } | |
2612 | ||
32513b40 BF |
2613 | static struct nfs4_file *nfsd4_alloc_file(void) |
2614 | { | |
2615 | return kmem_cache_alloc(file_slab, GFP_KERNEL); | |
2616 | } | |
2617 | ||
1da177e4 | 2618 | /* OPEN Share state helper functions */ |
32513b40 | 2619 | static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino) |
1da177e4 | 2620 | { |
1da177e4 LT |
2621 | unsigned int hashval = file_hashval(ino); |
2622 | ||
950e0118 TM |
2623 | lockdep_assert_held(&state_lock); |
2624 | ||
32513b40 | 2625 | atomic_set(&fp->fi_ref, 1); |
32513b40 BF |
2626 | INIT_LIST_HEAD(&fp->fi_stateids); |
2627 | INIT_LIST_HEAD(&fp->fi_delegations); | |
950e0118 TM |
2628 | ihold(ino); |
2629 | fp->fi_inode = ino; | |
32513b40 BF |
2630 | fp->fi_had_conflict = false; |
2631 | fp->fi_lease = NULL; | |
2632 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); | |
2633 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); | |
89876f8c | 2634 | hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); |
1da177e4 LT |
2635 | } |
2636 | ||
e8ff2a84 | 2637 | void |
1da177e4 LT |
2638 | nfsd4_free_slabs(void) |
2639 | { | |
abf1135b CH |
2640 | kmem_cache_destroy(openowner_slab); |
2641 | kmem_cache_destroy(lockowner_slab); | |
2642 | kmem_cache_destroy(file_slab); | |
2643 | kmem_cache_destroy(stateid_slab); | |
2644 | kmem_cache_destroy(deleg_slab); | |
e60d4398 | 2645 | } |
1da177e4 | 2646 | |
72083396 | 2647 | int |
e60d4398 N |
2648 | nfsd4_init_slabs(void) |
2649 | { | |
fe0750e5 BF |
2650 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
2651 | sizeof(struct nfs4_openowner), 0, 0, NULL); | |
2652 | if (openowner_slab == NULL) | |
abf1135b | 2653 | goto out; |
fe0750e5 | 2654 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
3c40794b | 2655 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
fe0750e5 | 2656 | if (lockowner_slab == NULL) |
abf1135b | 2657 | goto out_free_openowner_slab; |
e60d4398 | 2658 | file_slab = kmem_cache_create("nfsd4_files", |
20c2df83 | 2659 | sizeof(struct nfs4_file), 0, 0, NULL); |
e60d4398 | 2660 | if (file_slab == NULL) |
abf1135b | 2661 | goto out_free_lockowner_slab; |
5ac049ac | 2662 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
dcef0413 | 2663 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
5ac049ac | 2664 | if (stateid_slab == NULL) |
abf1135b | 2665 | goto out_free_file_slab; |
5b2d21c1 | 2666 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
20c2df83 | 2667 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
5b2d21c1 | 2668 | if (deleg_slab == NULL) |
abf1135b | 2669 | goto out_free_stateid_slab; |
e60d4398 | 2670 | return 0; |
abf1135b CH |
2671 | |
2672 | out_free_stateid_slab: | |
2673 | kmem_cache_destroy(stateid_slab); | |
2674 | out_free_file_slab: | |
2675 | kmem_cache_destroy(file_slab); | |
2676 | out_free_lockowner_slab: | |
2677 | kmem_cache_destroy(lockowner_slab); | |
2678 | out_free_openowner_slab: | |
2679 | kmem_cache_destroy(openowner_slab); | |
2680 | out: | |
e60d4398 N |
2681 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
2682 | return -ENOMEM; | |
1da177e4 LT |
2683 | } |
2684 | ||
ff194bd9 | 2685 | static void init_nfs4_replay(struct nfs4_replay *rp) |
1da177e4 | 2686 | { |
ff194bd9 BF |
2687 | rp->rp_status = nfserr_serverfault; |
2688 | rp->rp_buflen = 0; | |
2689 | rp->rp_buf = rp->rp_ibuf; | |
1da177e4 LT |
2690 | } |
2691 | ||
fe0750e5 | 2692 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
ff194bd9 | 2693 | { |
1da177e4 | 2694 | struct nfs4_stateowner *sop; |
1da177e4 | 2695 | |
fe0750e5 | 2696 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
ff194bd9 BF |
2697 | if (!sop) |
2698 | return NULL; | |
2699 | ||
2700 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); | |
2701 | if (!sop->so_owner.data) { | |
fe0750e5 | 2702 | kmem_cache_free(slab, sop); |
1da177e4 | 2703 | return NULL; |
ff194bd9 BF |
2704 | } |
2705 | sop->so_owner.len = owner->len; | |
2706 | ||
ea1da636 | 2707 | INIT_LIST_HEAD(&sop->so_stateids); |
ff194bd9 BF |
2708 | sop->so_client = clp; |
2709 | init_nfs4_replay(&sop->so_replay); | |
2710 | return sop; | |
2711 | } | |
2712 | ||
fe0750e5 | 2713 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
ff194bd9 | 2714 | { |
9b531137 SK |
2715 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
2716 | ||
2717 | list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); | |
fe0750e5 | 2718 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
ff194bd9 BF |
2719 | } |
2720 | ||
fe0750e5 | 2721 | static struct nfs4_openowner * |
db24b3b4 JL |
2722 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
2723 | struct nfsd4_open *open, | |
2724 | struct nfsd4_compound_state *cstate) | |
2725 | { | |
fe0750e5 | 2726 | struct nfs4_openowner *oo; |
ff194bd9 | 2727 | |
fe0750e5 BF |
2728 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
2729 | if (!oo) | |
ff194bd9 | 2730 | return NULL; |
fe0750e5 BF |
2731 | oo->oo_owner.so_is_open_owner = 1; |
2732 | oo->oo_owner.so_seqid = open->op_seqid; | |
d29b20cd | 2733 | oo->oo_flags = NFS4_OO_NEW; |
db24b3b4 JL |
2734 | if (nfsd4_has_session(cstate)) |
2735 | oo->oo_flags |= NFS4_OO_CONFIRMED; | |
fe0750e5 | 2736 | oo->oo_time = 0; |
38c387b5 | 2737 | oo->oo_last_closed_stid = NULL; |
fe0750e5 BF |
2738 | INIT_LIST_HEAD(&oo->oo_close_lru); |
2739 | hash_openowner(oo, clp, strhashval); | |
2740 | return oo; | |
1da177e4 LT |
2741 | } |
2742 | ||
996e0938 | 2743 | static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { |
fe0750e5 | 2744 | struct nfs4_openowner *oo = open->op_openowner; |
1da177e4 | 2745 | |
3abdb607 | 2746 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
ea1da636 | 2747 | INIT_LIST_HEAD(&stp->st_lockowners); |
fe0750e5 | 2748 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
8beefa24 | 2749 | list_add(&stp->st_perfile, &fp->fi_stateids); |
fe0750e5 | 2750 | stp->st_stateowner = &oo->oo_owner; |
13cd2184 | 2751 | get_nfs4_file(fp); |
1da177e4 | 2752 | stp->st_file = fp; |
1da177e4 LT |
2753 | stp->st_access_bmap = 0; |
2754 | stp->st_deny_bmap = 0; | |
82c5ff1b | 2755 | set_access(open->op_share_access, stp); |
ce0fc43c | 2756 | set_deny(open->op_share_deny, stp); |
4c4cd222 | 2757 | stp->st_openstp = NULL; |
1da177e4 LT |
2758 | } |
2759 | ||
fd39ca9a | 2760 | static void |
73758fed | 2761 | move_to_close_lru(struct nfs4_openowner *oo, struct net *net) |
1da177e4 | 2762 | { |
73758fed SK |
2763 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
2764 | ||
fe0750e5 | 2765 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
1da177e4 | 2766 | |
73758fed | 2767 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
fe0750e5 | 2768 | oo->oo_time = get_seconds(); |
1da177e4 LT |
2769 | } |
2770 | ||
1da177e4 | 2771 | static int |
599e0a22 BF |
2772 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
2773 | clientid_t *clid) | |
2774 | { | |
2775 | return (sop->so_owner.len == owner->len) && | |
2776 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && | |
2777 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); | |
1da177e4 LT |
2778 | } |
2779 | ||
fe0750e5 | 2780 | static struct nfs4_openowner * |
9b531137 SK |
2781 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
2782 | bool sessions, struct nfsd_net *nn) | |
1da177e4 | 2783 | { |
a50d2ad1 BF |
2784 | struct nfs4_stateowner *so; |
2785 | struct nfs4_openowner *oo; | |
d15c077e | 2786 | struct nfs4_client *clp; |
1da177e4 | 2787 | |
9b531137 | 2788 | list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
16bfdaaf BF |
2789 | if (!so->so_is_open_owner) |
2790 | continue; | |
a50d2ad1 BF |
2791 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) { |
2792 | oo = openowner(so); | |
d15c077e BF |
2793 | clp = oo->oo_owner.so_client; |
2794 | if ((bool)clp->cl_minorversion != sessions) | |
2795 | return NULL; | |
a50d2ad1 BF |
2796 | renew_client(oo->oo_owner.so_client); |
2797 | return oo; | |
2798 | } | |
1da177e4 LT |
2799 | } |
2800 | return NULL; | |
2801 | } | |
2802 | ||
2803 | /* search file_hashtbl[] for file */ | |
2804 | static struct nfs4_file * | |
950e0118 | 2805 | find_file_locked(struct inode *ino) |
1da177e4 LT |
2806 | { |
2807 | unsigned int hashval = file_hashval(ino); | |
2808 | struct nfs4_file *fp; | |
2809 | ||
950e0118 TM |
2810 | lockdep_assert_held(&state_lock); |
2811 | ||
89876f8c | 2812 | hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
13cd2184 N |
2813 | if (fp->fi_inode == ino) { |
2814 | get_nfs4_file(fp); | |
1da177e4 | 2815 | return fp; |
13cd2184 | 2816 | } |
1da177e4 LT |
2817 | } |
2818 | return NULL; | |
2819 | } | |
2820 | ||
950e0118 TM |
2821 | static struct nfs4_file * |
2822 | find_file(struct inode *ino) | |
2823 | { | |
2824 | struct nfs4_file *fp; | |
2825 | ||
2826 | spin_lock(&state_lock); | |
2827 | fp = find_file_locked(ino); | |
2828 | spin_unlock(&state_lock); | |
2829 | return fp; | |
2830 | } | |
2831 | ||
2832 | static struct nfs4_file * | |
2833 | find_or_add_file(struct inode *ino, struct nfs4_file *new) | |
2834 | { | |
2835 | struct nfs4_file *fp; | |
2836 | ||
2837 | spin_lock(&state_lock); | |
2838 | fp = find_file_locked(ino); | |
2839 | if (fp == NULL) { | |
2840 | nfsd4_init_file(new, ino); | |
2841 | fp = new; | |
2842 | } | |
2843 | spin_unlock(&state_lock); | |
2844 | ||
2845 | return fp; | |
2846 | } | |
2847 | ||
1da177e4 LT |
2848 | /* |
2849 | * Called to check deny when READ with all zero stateid or | |
2850 | * WRITE with all zero or all one stateid | |
2851 | */ | |
b37ad28b | 2852 | static __be32 |
1da177e4 LT |
2853 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
2854 | { | |
2855 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
2856 | struct nfs4_file *fp; | |
dcef0413 | 2857 | struct nfs4_ol_stateid *stp; |
b37ad28b | 2858 | __be32 ret; |
1da177e4 | 2859 | |
1da177e4 | 2860 | fp = find_file(ino); |
13cd2184 N |
2861 | if (!fp) |
2862 | return nfs_ok; | |
b700949b | 2863 | ret = nfserr_locked; |
1da177e4 | 2864 | /* Search for conflicting share reservations */ |
13cd2184 | 2865 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
ce0fc43c JL |
2866 | if (test_deny(deny_type, stp) || |
2867 | test_deny(NFS4_SHARE_DENY_BOTH, stp)) | |
13cd2184 | 2868 | goto out; |
1da177e4 | 2869 | } |
13cd2184 N |
2870 | ret = nfs_ok; |
2871 | out: | |
2872 | put_nfs4_file(fp); | |
2873 | return ret; | |
1da177e4 LT |
2874 | } |
2875 | ||
6b57d9c8 | 2876 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
1da177e4 | 2877 | { |
e8c69d17 BF |
2878 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
2879 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2880 | ||
cdc97505 | 2881 | lockdep_assert_held(&state_lock); |
1da177e4 LT |
2882 | /* We're assuming the state code never drops its reference |
2883 | * without first removing the lease. Since we're in this lease | |
2884 | * callback (and since the lease code is serialized by the kernel | |
2885 | * lock) we know the server hasn't removed the lease yet, we know | |
2886 | * it's safe to take a reference: */ | |
2887 | atomic_inc(&dp->dl_count); | |
2888 | ||
e8c69d17 | 2889 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
1da177e4 | 2890 | |
1c8c601a | 2891 | /* Only place dl_time is set; protected by i_lock: */ |
1da177e4 LT |
2892 | dp->dl_time = get_seconds(); |
2893 | ||
6282cd56 N |
2894 | block_delegations(&dp->dl_fh); |
2895 | ||
6b57d9c8 BF |
2896 | nfsd4_cb_recall(dp); |
2897 | } | |
2898 | ||
1c8c601a | 2899 | /* Called from break_lease() with i_lock held. */ |
6b57d9c8 BF |
2900 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
2901 | { | |
acfdf5c3 BF |
2902 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
2903 | struct nfs4_delegation *dp; | |
6b57d9c8 | 2904 | |
7fa10cd1 BF |
2905 | if (!fp) { |
2906 | WARN(1, "(%p)->fl_owner NULL\n", fl); | |
2907 | return; | |
2908 | } | |
2909 | if (fp->fi_had_conflict) { | |
2910 | WARN(1, "duplicate break on %p\n", fp); | |
2911 | return; | |
2912 | } | |
0272e1fd BF |
2913 | /* |
2914 | * We don't want the locks code to timeout the lease for us; | |
acfdf5c3 | 2915 | * we'll remove it ourself if a delegation isn't returned |
6b57d9c8 | 2916 | * in time: |
0272e1fd BF |
2917 | */ |
2918 | fl->fl_break_time = 0; | |
1da177e4 | 2919 | |
cdc97505 | 2920 | spin_lock(&state_lock); |
acfdf5c3 BF |
2921 | fp->fi_had_conflict = true; |
2922 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) | |
2923 | nfsd_break_one_deleg(dp); | |
cdc97505 | 2924 | spin_unlock(&state_lock); |
1da177e4 LT |
2925 | } |
2926 | ||
1da177e4 LT |
2927 | static |
2928 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) | |
2929 | { | |
2930 | if (arg & F_UNLCK) | |
2931 | return lease_modify(onlist, arg); | |
2932 | else | |
2933 | return -EAGAIN; | |
2934 | } | |
2935 | ||
7b021967 | 2936 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
8fb47a4f BF |
2937 | .lm_break = nfsd_break_deleg_cb, |
2938 | .lm_change = nfsd_change_deleg_cb, | |
1da177e4 LT |
2939 | }; |
2940 | ||
7a8711c9 BF |
2941 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
2942 | { | |
2943 | if (nfsd4_has_session(cstate)) | |
2944 | return nfs_ok; | |
2945 | if (seqid == so->so_seqid - 1) | |
2946 | return nfserr_replay_me; | |
2947 | if (seqid == so->so_seqid) | |
2948 | return nfs_ok; | |
2949 | return nfserr_bad_seqid; | |
2950 | } | |
1da177e4 | 2951 | |
b37ad28b | 2952 | __be32 |
6668958f | 2953 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
3320fef1 | 2954 | struct nfsd4_open *open, struct nfsd_net *nn) |
1da177e4 | 2955 | { |
1da177e4 LT |
2956 | clientid_t *clientid = &open->op_clientid; |
2957 | struct nfs4_client *clp = NULL; | |
2958 | unsigned int strhashval; | |
fe0750e5 | 2959 | struct nfs4_openowner *oo = NULL; |
4cdc951b | 2960 | __be32 status; |
1da177e4 | 2961 | |
2c142baa | 2962 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
1da177e4 | 2963 | return nfserr_stale_clientid; |
32513b40 BF |
2964 | /* |
2965 | * In case we need it later, after we've already created the | |
2966 | * file and don't want to risk a further failure: | |
2967 | */ | |
2968 | open->op_file = nfsd4_alloc_file(); | |
2969 | if (open->op_file == NULL) | |
2970 | return nfserr_jukebox; | |
1da177e4 | 2971 | |
16bfdaaf | 2972 | strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner); |
9b531137 | 2973 | oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn); |
fe0750e5 BF |
2974 | open->op_openowner = oo; |
2975 | if (!oo) { | |
8daae4dc SK |
2976 | clp = find_confirmed_client(clientid, cstate->minorversion, |
2977 | nn); | |
1da177e4 | 2978 | if (clp == NULL) |
0f442aa2 | 2979 | return nfserr_expired; |
bcf130f9 | 2980 | goto new_owner; |
1da177e4 | 2981 | } |
dad1c067 | 2982 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
0f442aa2 | 2983 | /* Replace unconfirmed owners without checking for replay. */ |
fe0750e5 BF |
2984 | clp = oo->oo_owner.so_client; |
2985 | release_openowner(oo); | |
2986 | open->op_openowner = NULL; | |
bcf130f9 | 2987 | goto new_owner; |
0f442aa2 | 2988 | } |
4cdc951b BF |
2989 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
2990 | if (status) | |
2991 | return status; | |
2992 | clp = oo->oo_owner.so_client; | |
2993 | goto alloc_stateid; | |
bcf130f9 | 2994 | new_owner: |
db24b3b4 | 2995 | oo = alloc_init_open_stateowner(strhashval, clp, open, cstate); |
bcf130f9 BF |
2996 | if (oo == NULL) |
2997 | return nfserr_jukebox; | |
2998 | open->op_openowner = oo; | |
4cdc951b BF |
2999 | alloc_stateid: |
3000 | open->op_stp = nfs4_alloc_stateid(clp); | |
3001 | if (!open->op_stp) | |
3002 | return nfserr_jukebox; | |
0f442aa2 | 3003 | return nfs_ok; |
1da177e4 LT |
3004 | } |
3005 | ||
b37ad28b | 3006 | static inline __be32 |
4a6e43e6 N |
3007 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
3008 | { | |
3009 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
3010 | return nfserr_openmode; | |
3011 | else | |
3012 | return nfs_ok; | |
3013 | } | |
3014 | ||
f459e453 | 3015 | static int share_access_to_flags(u32 share_access) |
52f4fb43 | 3016 | { |
f459e453 | 3017 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
52f4fb43 N |
3018 | } |
3019 | ||
38c2f4b1 | 3020 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) |
24a0111e | 3021 | { |
f459e453 | 3022 | struct nfs4_stid *ret; |
24a0111e | 3023 | |
38c2f4b1 | 3024 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
f459e453 BF |
3025 | if (!ret) |
3026 | return NULL; | |
3027 | return delegstateid(ret); | |
24a0111e BF |
3028 | } |
3029 | ||
8b289b2c BF |
3030 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
3031 | { | |
3032 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || | |
3033 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; | |
3034 | } | |
3035 | ||
b37ad28b | 3036 | static __be32 |
41d22663 | 3037 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
567d9829 N |
3038 | struct nfs4_delegation **dp) |
3039 | { | |
3040 | int flags; | |
b37ad28b | 3041 | __be32 status = nfserr_bad_stateid; |
567d9829 | 3042 | |
38c2f4b1 | 3043 | *dp = find_deleg_stateid(cl, &open->op_delegate_stateid); |
567d9829 | 3044 | if (*dp == NULL) |
c44c5eeb | 3045 | goto out; |
24a0111e | 3046 | flags = share_access_to_flags(open->op_share_access); |
567d9829 N |
3047 | status = nfs4_check_delegmode(*dp, flags); |
3048 | if (status) | |
3049 | *dp = NULL; | |
c44c5eeb | 3050 | out: |
8b289b2c | 3051 | if (!nfsd4_is_deleg_cur(open)) |
c44c5eeb N |
3052 | return nfs_ok; |
3053 | if (status) | |
3054 | return status; | |
dad1c067 | 3055 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
c44c5eeb | 3056 | return nfs_ok; |
567d9829 N |
3057 | } |
3058 | ||
b37ad28b | 3059 | static __be32 |
dcef0413 | 3060 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp) |
1da177e4 | 3061 | { |
dcef0413 | 3062 | struct nfs4_ol_stateid *local; |
fe0750e5 | 3063 | struct nfs4_openowner *oo = open->op_openowner; |
1da177e4 | 3064 | |
8beefa24 | 3065 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
1da177e4 LT |
3066 | /* ignore lock owners */ |
3067 | if (local->st_stateowner->so_is_open_owner == 0) | |
3068 | continue; | |
3069 | /* remember if we have seen this open owner */ | |
fe0750e5 | 3070 | if (local->st_stateowner == &oo->oo_owner) |
1da177e4 LT |
3071 | *stpp = local; |
3072 | /* check for conflicting share reservations */ | |
3073 | if (!test_share(local, open)) | |
77eaae8d | 3074 | return nfserr_share_denied; |
1da177e4 | 3075 | } |
77eaae8d | 3076 | return nfs_ok; |
1da177e4 LT |
3077 | } |
3078 | ||
21fb4016 BF |
3079 | static inline int nfs4_access_to_access(u32 nfs4_access) |
3080 | { | |
3081 | int flags = 0; | |
3082 | ||
3083 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) | |
3084 | flags |= NFSD_MAY_READ; | |
3085 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) | |
3086 | flags |= NFSD_MAY_WRITE; | |
3087 | return flags; | |
3088 | } | |
3089 | ||
7e6a72e5 CH |
3090 | static inline __be32 |
3091 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, | |
3092 | struct nfsd4_open *open) | |
3093 | { | |
3094 | struct iattr iattr = { | |
3095 | .ia_valid = ATTR_SIZE, | |
3096 | .ia_size = 0, | |
3097 | }; | |
3098 | if (!open->op_truncate) | |
3099 | return 0; | |
3100 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
3101 | return nfserr_inval; | |
3102 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); | |
3103 | } | |
3104 | ||
0c12eaff CB |
3105 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
3106 | struct svc_fh *cur_fh, struct nfsd4_open *open) | |
f9d7562f BF |
3107 | { |
3108 | __be32 status; | |
0c12eaff CB |
3109 | int oflag = nfs4_access_to_omode(open->op_share_access); |
3110 | int access = nfs4_access_to_access(open->op_share_access); | |
3111 | ||
f9d7562f BF |
3112 | if (!fp->fi_fds[oflag]) { |
3113 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, | |
3114 | &fp->fi_fds[oflag]); | |
f9d7562f | 3115 | if (status) |
7e6a72e5 | 3116 | goto out; |
f9d7562f BF |
3117 | } |
3118 | nfs4_file_get_access(fp, oflag); | |
3119 | ||
7e6a72e5 CH |
3120 | status = nfsd4_truncate(rqstp, cur_fh, open); |
3121 | if (status) | |
3122 | goto out_put_access; | |
3123 | ||
f9d7562f | 3124 | return nfs_ok; |
f9d7562f | 3125 | |
7e6a72e5 CH |
3126 | out_put_access: |
3127 | nfs4_file_put_access(fp, oflag); | |
3128 | out: | |
3129 | return status; | |
1da177e4 LT |
3130 | } |
3131 | ||
b37ad28b | 3132 | static __be32 |
dcef0413 | 3133 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) |
1da177e4 | 3134 | { |
b6d2f1ca | 3135 | u32 op_share_access = open->op_share_access; |
b37ad28b | 3136 | __be32 status; |
1da177e4 | 3137 | |
7e6a72e5 | 3138 | if (!test_access(op_share_access, stp)) |
0c12eaff | 3139 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open); |
7e6a72e5 CH |
3140 | else |
3141 | status = nfsd4_truncate(rqstp, cur_fh, open); | |
3142 | ||
3143 | if (status) | |
1da177e4 | 3144 | return status; |
7e6a72e5 | 3145 | |
1da177e4 | 3146 | /* remember the open */ |
82c5ff1b | 3147 | set_access(op_share_access, stp); |
ce0fc43c | 3148 | set_deny(open->op_share_deny, stp); |
1da177e4 LT |
3149 | return nfs_ok; |
3150 | } | |
3151 | ||
3152 | ||
1da177e4 | 3153 | static void |
1255a8f3 | 3154 | nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session) |
1da177e4 | 3155 | { |
dad1c067 | 3156 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
1da177e4 LT |
3157 | } |
3158 | ||
14a24e99 BF |
3159 | /* Should we give out recallable state?: */ |
3160 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) | |
3161 | { | |
3162 | if (clp->cl_cb_state == NFSD4_CB_UP) | |
3163 | return true; | |
3164 | /* | |
3165 | * In the sessions case, since we don't have to establish a | |
3166 | * separate connection for callbacks, we assume it's OK | |
3167 | * until we hear otherwise: | |
3168 | */ | |
3169 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; | |
3170 | } | |
3171 | ||
22d38c4c BF |
3172 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag) |
3173 | { | |
3174 | struct file_lock *fl; | |
3175 | ||
3176 | fl = locks_alloc_lock(); | |
3177 | if (!fl) | |
3178 | return NULL; | |
3179 | locks_init_lock(fl); | |
3180 | fl->fl_lmops = &nfsd_lease_mng_ops; | |
617588d5 | 3181 | fl->fl_flags = FL_DELEG; |
22d38c4c BF |
3182 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
3183 | fl->fl_end = OFFSET_MAX; | |
acfdf5c3 | 3184 | fl->fl_owner = (fl_owner_t)(dp->dl_file); |
22d38c4c | 3185 | fl->fl_pid = current->tgid; |
22d38c4c BF |
3186 | return fl; |
3187 | } | |
3188 | ||
99c41515 | 3189 | static int nfs4_setlease(struct nfs4_delegation *dp) |
edab9782 | 3190 | { |
acfdf5c3 | 3191 | struct nfs4_file *fp = dp->dl_file; |
edab9782 BF |
3192 | struct file_lock *fl; |
3193 | int status; | |
3194 | ||
99c41515 | 3195 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
edab9782 BF |
3196 | if (!fl) |
3197 | return -ENOMEM; | |
acfdf5c3 | 3198 | fl->fl_file = find_readable_file(fp); |
acfdf5c3 | 3199 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl); |
e873088f BF |
3200 | if (status) |
3201 | goto out_free; | |
acfdf5c3 | 3202 | fp->fi_lease = fl; |
cb0942b8 | 3203 | fp->fi_deleg_file = get_file(fl->fl_file); |
acfdf5c3 | 3204 | atomic_set(&fp->fi_delegees, 1); |
cdc97505 | 3205 | spin_lock(&state_lock); |
931ee56c | 3206 | hash_delegation_locked(dp, fp); |
cdc97505 | 3207 | spin_unlock(&state_lock); |
acfdf5c3 | 3208 | return 0; |
e873088f BF |
3209 | out_free: |
3210 | locks_free_lock(fl); | |
3211 | return status; | |
acfdf5c3 BF |
3212 | } |
3213 | ||
bf7bd3e9 | 3214 | static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp) |
acfdf5c3 | 3215 | { |
bf7bd3e9 BF |
3216 | if (fp->fi_had_conflict) |
3217 | return -EAGAIN; | |
3218 | get_nfs4_file(fp); | |
3219 | dp->dl_file = fp; | |
cbf7a75b BF |
3220 | if (!fp->fi_lease) |
3221 | return nfs4_setlease(dp); | |
cdc97505 | 3222 | spin_lock(&state_lock); |
cbf7a75b | 3223 | atomic_inc(&fp->fi_delegees); |
acfdf5c3 | 3224 | if (fp->fi_had_conflict) { |
cdc97505 | 3225 | spin_unlock(&state_lock); |
cbf7a75b | 3226 | return -EAGAIN; |
acfdf5c3 | 3227 | } |
931ee56c | 3228 | hash_delegation_locked(dp, fp); |
cdc97505 | 3229 | spin_unlock(&state_lock); |
edab9782 BF |
3230 | return 0; |
3231 | } | |
3232 | ||
4aa8913c BH |
3233 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
3234 | { | |
3235 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
3236 | if (status == -EAGAIN) | |
3237 | open->op_why_no_deleg = WND4_CONTENTION; | |
3238 | else { | |
3239 | open->op_why_no_deleg = WND4_RESOURCE; | |
3240 | switch (open->op_deleg_want) { | |
3241 | case NFS4_SHARE_WANT_READ_DELEG: | |
3242 | case NFS4_SHARE_WANT_WRITE_DELEG: | |
3243 | case NFS4_SHARE_WANT_ANY_DELEG: | |
3244 | break; | |
3245 | case NFS4_SHARE_WANT_CANCEL: | |
3246 | open->op_why_no_deleg = WND4_CANCELLED; | |
3247 | break; | |
3248 | case NFS4_SHARE_WANT_NO_DELEG: | |
063b0fb9 | 3249 | WARN_ON_ONCE(1); |
4aa8913c BH |
3250 | } |
3251 | } | |
3252 | } | |
3253 | ||
1da177e4 LT |
3254 | /* |
3255 | * Attempt to hand out a delegation. | |
99c41515 BF |
3256 | * |
3257 | * Note we don't support write delegations, and won't until the vfs has | |
3258 | * proper support for them. | |
1da177e4 LT |
3259 | */ |
3260 | static void | |
5ccb0066 SK |
3261 | nfs4_open_delegation(struct net *net, struct svc_fh *fh, |
3262 | struct nfsd4_open *open, struct nfs4_ol_stateid *stp) | |
1da177e4 LT |
3263 | { |
3264 | struct nfs4_delegation *dp; | |
fe0750e5 | 3265 | struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner); |
14a24e99 | 3266 | int cb_up; |
99c41515 | 3267 | int status = 0; |
1da177e4 | 3268 | |
fe0750e5 | 3269 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
7b190fec N |
3270 | open->op_recall = 0; |
3271 | switch (open->op_claim_type) { | |
3272 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
2bf23875 | 3273 | if (!cb_up) |
7b190fec | 3274 | open->op_recall = 1; |
99c41515 BF |
3275 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
3276 | goto out_no_deleg; | |
7b190fec N |
3277 | break; |
3278 | case NFS4_OPEN_CLAIM_NULL: | |
ed47b062 | 3279 | case NFS4_OPEN_CLAIM_FH: |
99c41515 BF |
3280 | /* |
3281 | * Let's not give out any delegations till everyone's | |
3282 | * had the chance to reclaim theirs.... | |
3283 | */ | |
5ccb0066 | 3284 | if (locks_in_grace(net)) |
99c41515 | 3285 | goto out_no_deleg; |
dad1c067 | 3286 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
99c41515 | 3287 | goto out_no_deleg; |
9a0590ae SD |
3288 | /* |
3289 | * Also, if the file was opened for write or | |
3290 | * create, there's a good chance the client's | |
3291 | * about to write to it, resulting in an | |
3292 | * immediate recall (since we don't support | |
3293 | * write delegations): | |
3294 | */ | |
7b190fec | 3295 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
99c41515 BF |
3296 | goto out_no_deleg; |
3297 | if (open->op_create == NFS4_OPEN_CREATE) | |
3298 | goto out_no_deleg; | |
7b190fec N |
3299 | break; |
3300 | default: | |
99c41515 | 3301 | goto out_no_deleg; |
7b190fec | 3302 | } |
99c41515 | 3303 | dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh); |
dd239cc0 BF |
3304 | if (dp == NULL) |
3305 | goto out_no_deleg; | |
bf7bd3e9 | 3306 | status = nfs4_set_delegation(dp, stp->st_file); |
edab9782 | 3307 | if (status) |
dd239cc0 | 3308 | goto out_free; |
1da177e4 | 3309 | |
d5477a8d | 3310 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
1da177e4 | 3311 | |
8c10cbdb | 3312 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
d5477a8d | 3313 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
99c41515 | 3314 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
dd239cc0 BF |
3315 | return; |
3316 | out_free: | |
cbf7a75b | 3317 | destroy_delegation(dp); |
dd239cc0 | 3318 | out_no_deleg: |
99c41515 BF |
3319 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
3320 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && | |
d08d32e6 | 3321 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
99c41515 | 3322 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
d08d32e6 BF |
3323 | open->op_recall = 1; |
3324 | } | |
99c41515 BF |
3325 | |
3326 | /* 4.1 client asking for a delegation? */ | |
3327 | if (open->op_deleg_want) | |
3328 | nfsd4_open_deleg_none_ext(open, status); | |
3329 | return; | |
1da177e4 LT |
3330 | } |
3331 | ||
e27f49c3 BH |
3332 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
3333 | struct nfs4_delegation *dp) | |
3334 | { | |
3335 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && | |
3336 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
3337 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
3338 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; | |
3339 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && | |
3340 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
3341 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
3342 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; | |
3343 | } | |
3344 | /* Otherwise the client must be confused wanting a delegation | |
3345 | * it already has, therefore we don't return | |
3346 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. | |
3347 | */ | |
3348 | } | |
3349 | ||
1da177e4 LT |
3350 | /* |
3351 | * called with nfs4_lock_state() held. | |
3352 | */ | |
b37ad28b | 3353 | __be32 |
1da177e4 LT |
3354 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
3355 | { | |
6668958f | 3356 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
38c2f4b1 | 3357 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
1da177e4 LT |
3358 | struct nfs4_file *fp = NULL; |
3359 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
dcef0413 | 3360 | struct nfs4_ol_stateid *stp = NULL; |
567d9829 | 3361 | struct nfs4_delegation *dp = NULL; |
b37ad28b | 3362 | __be32 status; |
1da177e4 | 3363 | |
1da177e4 LT |
3364 | /* |
3365 | * Lookup file; if found, lookup stateid and check open request, | |
3366 | * and check for delegations in the process of being recalled. | |
3367 | * If not found, create the nfs4_file struct | |
3368 | */ | |
950e0118 TM |
3369 | fp = find_or_add_file(ino, open->op_file); |
3370 | if (fp != open->op_file) { | |
1da177e4 LT |
3371 | if ((status = nfs4_check_open(fp, open, &stp))) |
3372 | goto out; | |
41d22663 | 3373 | status = nfs4_check_deleg(cl, open, &dp); |
c44c5eeb N |
3374 | if (status) |
3375 | goto out; | |
1da177e4 | 3376 | } else { |
950e0118 | 3377 | open->op_file = NULL; |
c44c5eeb | 3378 | status = nfserr_bad_stateid; |
8b289b2c | 3379 | if (nfsd4_is_deleg_cur(open)) |
c44c5eeb | 3380 | goto out; |
3e772463 | 3381 | status = nfserr_jukebox; |
1da177e4 LT |
3382 | } |
3383 | ||
3384 | /* | |
3385 | * OPEN the file, or upgrade an existing OPEN. | |
3386 | * If truncate fails, the OPEN fails. | |
3387 | */ | |
3388 | if (stp) { | |
3389 | /* Stateid was found, this is an OPEN upgrade */ | |
f9d7562f | 3390 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
1da177e4 LT |
3391 | if (status) |
3392 | goto out; | |
3393 | } else { | |
4cdc951b | 3394 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, open); |
567d9829 | 3395 | if (status) |
1da177e4 | 3396 | goto out; |
4cdc951b BF |
3397 | stp = open->op_stp; |
3398 | open->op_stp = NULL; | |
996e0938 | 3399 | init_open_stateid(stp, fp, open); |
1da177e4 | 3400 | } |
dcef0413 BF |
3401 | update_stateid(&stp->st_stid.sc_stateid); |
3402 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); | |
1da177e4 | 3403 | |
d24433cd | 3404 | if (nfsd4_has_session(&resp->cstate)) { |
d24433cd BH |
3405 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
3406 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
3407 | open->op_why_no_deleg = WND4_NOT_WANTED; | |
3408 | goto nodeleg; | |
3409 | } | |
3410 | } | |
3411 | ||
1da177e4 LT |
3412 | /* |
3413 | * Attempt to hand out a delegation. No error return, because the | |
3414 | * OPEN succeeds even if we fail. | |
3415 | */ | |
5ccb0066 | 3416 | nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp); |
d24433cd | 3417 | nodeleg: |
1da177e4 LT |
3418 | status = nfs_ok; |
3419 | ||
8c10cbdb | 3420 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
dcef0413 | 3421 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
1da177e4 | 3422 | out: |
d24433cd BH |
3423 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
3424 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && | |
e27f49c3 BH |
3425 | open->op_deleg_want) |
3426 | nfsd4_deleg_xgrade_none_ext(open, dp); | |
d24433cd | 3427 | |
13cd2184 N |
3428 | if (fp) |
3429 | put_nfs4_file(fp); | |
37515177 | 3430 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
1255a8f3 | 3431 | nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate)); |
1da177e4 LT |
3432 | /* |
3433 | * To finish the open response, we just need to set the rflags. | |
3434 | */ | |
3435 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
dad1c067 | 3436 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
6668958f | 3437 | !nfsd4_has_session(&resp->cstate)) |
1da177e4 LT |
3438 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
3439 | ||
3440 | return status; | |
3441 | } | |
3442 | ||
d29b20cd BF |
3443 | void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status) |
3444 | { | |
3445 | if (open->op_openowner) { | |
3446 | struct nfs4_openowner *oo = open->op_openowner; | |
3447 | ||
3448 | if (!list_empty(&oo->oo_owner.so_stateids)) | |
3449 | list_del_init(&oo->oo_close_lru); | |
3450 | if (oo->oo_flags & NFS4_OO_NEW) { | |
3451 | if (status) { | |
3452 | release_openowner(oo); | |
3453 | open->op_openowner = NULL; | |
3454 | } else | |
3455 | oo->oo_flags &= ~NFS4_OO_NEW; | |
3456 | } | |
3457 | } | |
32513b40 BF |
3458 | if (open->op_file) |
3459 | nfsd4_free_file(open->op_file); | |
4cdc951b | 3460 | if (open->op_stp) |
ef79859e | 3461 | free_generic_stateid(open->op_stp); |
d29b20cd BF |
3462 | } |
3463 | ||
9b2ef62b BF |
3464 | static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp) |
3465 | { | |
3466 | struct nfs4_client *found; | |
3467 | ||
3468 | if (STALE_CLIENTID(clid, nn)) | |
3469 | return nfserr_stale_clientid; | |
3470 | found = find_confirmed_client(clid, session, nn); | |
3471 | if (clp) | |
3472 | *clp = found; | |
3473 | return found ? nfs_ok : nfserr_expired; | |
3474 | } | |
3475 | ||
b37ad28b | 3476 | __be32 |
b591480b BF |
3477 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3478 | clientid_t *clid) | |
1da177e4 LT |
3479 | { |
3480 | struct nfs4_client *clp; | |
b37ad28b | 3481 | __be32 status; |
7f2210fa | 3482 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 LT |
3483 | |
3484 | nfs4_lock_state(); | |
3485 | dprintk("process_renew(%08x/%08x): starting\n", | |
3486 | clid->cl_boot, clid->cl_id); | |
9b2ef62b BF |
3487 | status = lookup_clientid(clid, cstate->minorversion, nn, &clp); |
3488 | if (status) | |
1da177e4 | 3489 | goto out; |
1da177e4 | 3490 | status = nfserr_cb_path_down; |
ea1da636 | 3491 | if (!list_empty(&clp->cl_delegations) |
77a3569d | 3492 | && clp->cl_cb_state != NFSD4_CB_UP) |
1da177e4 LT |
3493 | goto out; |
3494 | status = nfs_ok; | |
3495 | out: | |
3496 | nfs4_unlock_state(); | |
3497 | return status; | |
3498 | } | |
3499 | ||
a76b4319 | 3500 | static void |
12760c66 | 3501 | nfsd4_end_grace(struct nfsd_net *nn) |
a76b4319 | 3502 | { |
33dcc481 | 3503 | /* do nothing if grace period already ended */ |
a51c84ed | 3504 | if (nn->grace_ended) |
33dcc481 JL |
3505 | return; |
3506 | ||
a76b4319 | 3507 | dprintk("NFSD: end of grace period\n"); |
a51c84ed | 3508 | nn->grace_ended = true; |
12760c66 | 3509 | nfsd4_record_grace_done(nn, nn->boot_time); |
5e1533c7 | 3510 | locks_end_grace(&nn->nfsd4_manager); |
e46b498c BF |
3511 | /* |
3512 | * Now that every NFSv4 client has had the chance to recover and | |
3513 | * to see the (possibly new, possibly shorter) lease time, we | |
3514 | * can safely set the next grace time to the current lease time: | |
3515 | */ | |
5284b44e | 3516 | nn->nfsd4_grace = nn->nfsd4_lease; |
a76b4319 N |
3517 | } |
3518 | ||
fd39ca9a | 3519 | static time_t |
09121281 | 3520 | nfs4_laundromat(struct nfsd_net *nn) |
1da177e4 LT |
3521 | { |
3522 | struct nfs4_client *clp; | |
fe0750e5 | 3523 | struct nfs4_openowner *oo; |
1da177e4 LT |
3524 | struct nfs4_delegation *dp; |
3525 | struct list_head *pos, *next, reaplist; | |
3d733711 | 3526 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
a832e7ae | 3527 | time_t t, new_timeo = nn->nfsd4_lease; |
1da177e4 LT |
3528 | |
3529 | nfs4_lock_state(); | |
3530 | ||
3531 | dprintk("NFSD: laundromat service - starting\n"); | |
12760c66 | 3532 | nfsd4_end_grace(nn); |
36acb66b | 3533 | INIT_LIST_HEAD(&reaplist); |
c9a49628 | 3534 | spin_lock(&nn->client_lock); |
5ed58bb2 | 3535 | list_for_each_safe(pos, next, &nn->client_lru) { |
1da177e4 LT |
3536 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
3537 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { | |
3538 | t = clp->cl_time - cutoff; | |
a832e7ae | 3539 | new_timeo = min(new_timeo, t); |
1da177e4 LT |
3540 | break; |
3541 | } | |
221a6876 | 3542 | if (mark_client_expired_locked(clp)) { |
d7682988 BH |
3543 | dprintk("NFSD: client in use (clientid %08x)\n", |
3544 | clp->cl_clientid.cl_id); | |
3545 | continue; | |
3546 | } | |
221a6876 | 3547 | list_move(&clp->cl_lru, &reaplist); |
36acb66b | 3548 | } |
c9a49628 | 3549 | spin_unlock(&nn->client_lock); |
36acb66b BH |
3550 | list_for_each_safe(pos, next, &reaplist) { |
3551 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
1da177e4 LT |
3552 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
3553 | clp->cl_clientid.cl_id); | |
3554 | expire_client(clp); | |
3555 | } | |
cdc97505 | 3556 | spin_lock(&state_lock); |
e8c69d17 | 3557 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 3558 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
4e37a7c2 SK |
3559 | if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn) |
3560 | continue; | |
1da177e4 | 3561 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
a832e7ae JL |
3562 | t = dp->dl_time - cutoff; |
3563 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
3564 | break; |
3565 | } | |
1da177e4 LT |
3566 | list_move(&dp->dl_recall_lru, &reaplist); |
3567 | } | |
cdc97505 | 3568 | spin_unlock(&state_lock); |
1da177e4 LT |
3569 | list_for_each_safe(pos, next, &reaplist) { |
3570 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
3bd64a5b | 3571 | revoke_delegation(dp); |
1da177e4 | 3572 | } |
73758fed | 3573 | list_for_each_safe(pos, next, &nn->close_lru) { |
fe0750e5 BF |
3574 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
3575 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { | |
a832e7ae JL |
3576 | t = oo->oo_time - cutoff; |
3577 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
3578 | break; |
3579 | } | |
fe0750e5 | 3580 | release_openowner(oo); |
1da177e4 | 3581 | } |
a832e7ae | 3582 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
1da177e4 | 3583 | nfs4_unlock_state(); |
a832e7ae | 3584 | return new_timeo; |
1da177e4 LT |
3585 | } |
3586 | ||
a254b246 HH |
3587 | static struct workqueue_struct *laundry_wq; |
3588 | static void laundromat_main(struct work_struct *); | |
a254b246 HH |
3589 | |
3590 | static void | |
09121281 | 3591 | laundromat_main(struct work_struct *laundry) |
1da177e4 LT |
3592 | { |
3593 | time_t t; | |
09121281 SK |
3594 | struct delayed_work *dwork = container_of(laundry, struct delayed_work, |
3595 | work); | |
3596 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, | |
3597 | laundromat_work); | |
1da177e4 | 3598 | |
09121281 | 3599 | t = nfs4_laundromat(nn); |
1da177e4 | 3600 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
09121281 | 3601 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
1da177e4 LT |
3602 | } |
3603 | ||
f7a4d872 | 3604 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp) |
1da177e4 | 3605 | { |
f7a4d872 BF |
3606 | if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode) |
3607 | return nfserr_bad_stateid; | |
3608 | return nfs_ok; | |
1da177e4 LT |
3609 | } |
3610 | ||
1da177e4 | 3611 | static inline int |
82c5ff1b | 3612 | access_permit_read(struct nfs4_ol_stateid *stp) |
1da177e4 | 3613 | { |
82c5ff1b JL |
3614 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
3615 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || | |
3616 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); | |
1da177e4 LT |
3617 | } |
3618 | ||
3619 | static inline int | |
82c5ff1b | 3620 | access_permit_write(struct nfs4_ol_stateid *stp) |
1da177e4 | 3621 | { |
82c5ff1b JL |
3622 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
3623 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); | |
1da177e4 LT |
3624 | } |
3625 | ||
3626 | static | |
dcef0413 | 3627 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
1da177e4 | 3628 | { |
b37ad28b | 3629 | __be32 status = nfserr_openmode; |
1da177e4 | 3630 | |
02921914 BF |
3631 | /* For lock stateid's, we test the parent open, not the lock: */ |
3632 | if (stp->st_openstp) | |
3633 | stp = stp->st_openstp; | |
82c5ff1b | 3634 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
1da177e4 | 3635 | goto out; |
82c5ff1b | 3636 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
1da177e4 LT |
3637 | goto out; |
3638 | status = nfs_ok; | |
3639 | out: | |
3640 | return status; | |
3641 | } | |
3642 | ||
b37ad28b | 3643 | static inline __be32 |
5ccb0066 | 3644 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
1da177e4 | 3645 | { |
203a8c8e | 3646 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
1da177e4 | 3647 | return nfs_ok; |
5ccb0066 | 3648 | else if (locks_in_grace(net)) { |
25985edc | 3649 | /* Answer in remaining cases depends on existence of |
1da177e4 LT |
3650 | * conflicting state; so we must wait out the grace period. */ |
3651 | return nfserr_grace; | |
3652 | } else if (flags & WR_STATE) | |
3653 | return nfs4_share_conflict(current_fh, | |
3654 | NFS4_SHARE_DENY_WRITE); | |
3655 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
3656 | return nfs4_share_conflict(current_fh, | |
3657 | NFS4_SHARE_DENY_READ); | |
3658 | } | |
3659 | ||
3660 | /* | |
3661 | * Allow READ/WRITE during grace period on recovered state only for files | |
3662 | * that are not able to provide mandatory locking. | |
3663 | */ | |
3664 | static inline int | |
5ccb0066 | 3665 | grace_disallows_io(struct net *net, struct inode *inode) |
1da177e4 | 3666 | { |
5ccb0066 | 3667 | return locks_in_grace(net) && mandatory_lock(inode); |
1da177e4 LT |
3668 | } |
3669 | ||
81b82965 BF |
3670 | /* Returns true iff a is later than b: */ |
3671 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) | |
3672 | { | |
1a9357f4 | 3673 | return (s32)(a->si_generation - b->si_generation) > 0; |
81b82965 BF |
3674 | } |
3675 | ||
57b7b43b | 3676 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
0836f587 | 3677 | { |
6668958f AA |
3678 | /* |
3679 | * When sessions are used the stateid generation number is ignored | |
3680 | * when it is zero. | |
3681 | */ | |
28dde241 | 3682 | if (has_session && in->si_generation == 0) |
81b82965 BF |
3683 | return nfs_ok; |
3684 | ||
3685 | if (in->si_generation == ref->si_generation) | |
3686 | return nfs_ok; | |
6668958f | 3687 | |
0836f587 | 3688 | /* If the client sends us a stateid from the future, it's buggy: */ |
81b82965 | 3689 | if (stateid_generation_after(in, ref)) |
0836f587 BF |
3690 | return nfserr_bad_stateid; |
3691 | /* | |
81b82965 BF |
3692 | * However, we could see a stateid from the past, even from a |
3693 | * non-buggy client. For example, if the client sends a lock | |
3694 | * while some IO is outstanding, the lock may bump si_generation | |
3695 | * while the IO is still in flight. The client could avoid that | |
3696 | * situation by waiting for responses on all the IO requests, | |
3697 | * but better performance may result in retrying IO that | |
3698 | * receives an old_stateid error if requests are rarely | |
3699 | * reordered in flight: | |
0836f587 | 3700 | */ |
81b82965 | 3701 | return nfserr_old_stateid; |
0836f587 BF |
3702 | } |
3703 | ||
7df302f7 | 3704 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
17456804 | 3705 | { |
97b7e3b6 BF |
3706 | struct nfs4_stid *s; |
3707 | struct nfs4_ol_stateid *ols; | |
3708 | __be32 status; | |
17456804 | 3709 | |
7df302f7 CL |
3710 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
3711 | return nfserr_bad_stateid; | |
3712 | /* Client debugging aid. */ | |
3713 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { | |
3714 | char addr_str[INET6_ADDRSTRLEN]; | |
3715 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, | |
3716 | sizeof(addr_str)); | |
3717 | pr_warn_ratelimited("NFSD: client %s testing state ID " | |
3718 | "with incorrect client ID\n", addr_str); | |
3719 | return nfserr_bad_stateid; | |
3720 | } | |
38c2f4b1 | 3721 | s = find_stateid(cl, stateid); |
97b7e3b6 | 3722 | if (!s) |
7df302f7 | 3723 | return nfserr_bad_stateid; |
36279ac1 | 3724 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
17456804 | 3725 | if (status) |
97b7e3b6 | 3726 | return status; |
23340032 BF |
3727 | switch (s->sc_type) { |
3728 | case NFS4_DELEG_STID: | |
97b7e3b6 | 3729 | return nfs_ok; |
3bd64a5b BF |
3730 | case NFS4_REVOKED_DELEG_STID: |
3731 | return nfserr_deleg_revoked; | |
23340032 BF |
3732 | case NFS4_OPEN_STID: |
3733 | case NFS4_LOCK_STID: | |
3734 | ols = openlockstateid(s); | |
3735 | if (ols->st_stateowner->so_is_open_owner | |
3736 | && !(openowner(ols->st_stateowner)->oo_flags | |
3737 | & NFS4_OO_CONFIRMED)) | |
3738 | return nfserr_bad_stateid; | |
97b7e3b6 | 3739 | return nfs_ok; |
23340032 BF |
3740 | default: |
3741 | printk("unknown stateid type %x\n", s->sc_type); | |
3742 | case NFS4_CLOSED_STID: | |
97b7e3b6 | 3743 | return nfserr_bad_stateid; |
23340032 | 3744 | } |
17456804 BS |
3745 | } |
3746 | ||
3320fef1 SK |
3747 | static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, |
3748 | struct nfs4_stid **s, bool sessions, | |
3749 | struct nfsd_net *nn) | |
38c2f4b1 BF |
3750 | { |
3751 | struct nfs4_client *cl; | |
0eb6f20a | 3752 | __be32 status; |
38c2f4b1 BF |
3753 | |
3754 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) | |
3755 | return nfserr_bad_stateid; | |
0eb6f20a BF |
3756 | status = lookup_clientid(&stateid->si_opaque.so_clid, sessions, |
3757 | nn, &cl); | |
a8a7c677 TM |
3758 | if (status == nfserr_stale_clientid) { |
3759 | if (sessions) | |
3760 | return nfserr_bad_stateid; | |
38c2f4b1 | 3761 | return nfserr_stale_stateid; |
a8a7c677 | 3762 | } |
0eb6f20a BF |
3763 | if (status) |
3764 | return status; | |
38c2f4b1 BF |
3765 | *s = find_stateid_by_type(cl, stateid, typemask); |
3766 | if (!*s) | |
3767 | return nfserr_bad_stateid; | |
3768 | return nfs_ok; | |
38c2f4b1 BF |
3769 | } |
3770 | ||
1da177e4 LT |
3771 | /* |
3772 | * Checks for stateid operations | |
3773 | */ | |
b37ad28b | 3774 | __be32 |
5ccb0066 | 3775 | nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, |
dd453dfd | 3776 | stateid_t *stateid, int flags, struct file **filpp) |
1da177e4 | 3777 | { |
69064a27 | 3778 | struct nfs4_stid *s; |
dcef0413 | 3779 | struct nfs4_ol_stateid *stp = NULL; |
1da177e4 | 3780 | struct nfs4_delegation *dp = NULL; |
dd453dfd | 3781 | struct svc_fh *current_fh = &cstate->current_fh; |
1da177e4 | 3782 | struct inode *ino = current_fh->fh_dentry->d_inode; |
3320fef1 | 3783 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
14bcab1a | 3784 | struct file *file = NULL; |
b37ad28b | 3785 | __be32 status; |
1da177e4 | 3786 | |
1da177e4 LT |
3787 | if (filpp) |
3788 | *filpp = NULL; | |
3789 | ||
5ccb0066 | 3790 | if (grace_disallows_io(net, ino)) |
1da177e4 LT |
3791 | return nfserr_grace; |
3792 | ||
3793 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) | |
5ccb0066 | 3794 | return check_special_stateids(net, current_fh, stateid, flags); |
1da177e4 | 3795 | |
14bcab1a TM |
3796 | nfs4_lock_state(); |
3797 | ||
db24b3b4 JL |
3798 | status = nfsd4_lookup_stateid(stateid, |
3799 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, | |
3800 | &s, cstate->minorversion, nn); | |
38c2f4b1 | 3801 | if (status) |
14bcab1a | 3802 | goto out; |
69064a27 BF |
3803 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
3804 | if (status) | |
3805 | goto out; | |
f7a4d872 BF |
3806 | switch (s->sc_type) { |
3807 | case NFS4_DELEG_STID: | |
69064a27 | 3808 | dp = delegstateid(s); |
dc9bf700 BF |
3809 | status = nfs4_check_delegmode(dp, flags); |
3810 | if (status) | |
3811 | goto out; | |
43b0178e | 3812 | if (filpp) { |
14bcab1a TM |
3813 | file = dp->dl_file->fi_deleg_file; |
3814 | if (!file) { | |
063b0fb9 BF |
3815 | WARN_ON_ONCE(1); |
3816 | status = nfserr_serverfault; | |
3817 | goto out; | |
3818 | } | |
43b0178e | 3819 | } |
f7a4d872 BF |
3820 | break; |
3821 | case NFS4_OPEN_STID: | |
3822 | case NFS4_LOCK_STID: | |
69064a27 | 3823 | stp = openlockstateid(s); |
f7a4d872 BF |
3824 | status = nfs4_check_fh(current_fh, stp); |
3825 | if (status) | |
1da177e4 | 3826 | goto out; |
fe0750e5 | 3827 | if (stp->st_stateowner->so_is_open_owner |
dad1c067 | 3828 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
1da177e4 | 3829 | goto out; |
a4455be0 BF |
3830 | status = nfs4_check_openmode(stp, flags); |
3831 | if (status) | |
1da177e4 | 3832 | goto out; |
f9d7562f BF |
3833 | if (filpp) { |
3834 | if (flags & RD_STATE) | |
14bcab1a | 3835 | file = find_readable_file(stp->st_file); |
f9d7562f | 3836 | else |
14bcab1a | 3837 | file = find_writeable_file(stp->st_file); |
f9d7562f | 3838 | } |
f7a4d872 BF |
3839 | break; |
3840 | default: | |
14bcab1a TM |
3841 | status = nfserr_bad_stateid; |
3842 | goto out; | |
1da177e4 LT |
3843 | } |
3844 | status = nfs_ok; | |
14bcab1a TM |
3845 | if (file) |
3846 | *filpp = get_file(file); | |
1da177e4 | 3847 | out: |
14bcab1a | 3848 | nfs4_unlock_state(); |
1da177e4 LT |
3849 | return status; |
3850 | } | |
3851 | ||
e1ca12df | 3852 | static __be32 |
dcef0413 | 3853 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
e1ca12df | 3854 | { |
a1b8ff4c BF |
3855 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
3856 | ||
3857 | if (check_for_locks(stp->st_file, lo)) | |
e1ca12df | 3858 | return nfserr_locks_held; |
a1b8ff4c BF |
3859 | /* |
3860 | * Currently there's a 1-1 lock stateid<->lockowner | |
3861 | * correspondance, and we have to delete the lockowner when we | |
3862 | * delete the lock stateid: | |
3863 | */ | |
48385408 | 3864 | release_lockowner(lo); |
e1ca12df BS |
3865 | return nfs_ok; |
3866 | } | |
3867 | ||
17456804 BS |
3868 | /* |
3869 | * Test if the stateid is valid | |
3870 | */ | |
3871 | __be32 | |
3872 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
3873 | struct nfsd4_test_stateid *test_stateid) | |
3874 | { | |
03cfb420 BS |
3875 | struct nfsd4_test_stateid_id *stateid; |
3876 | struct nfs4_client *cl = cstate->session->se_client; | |
3877 | ||
3878 | nfs4_lock_state(); | |
3879 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) | |
7df302f7 CL |
3880 | stateid->ts_id_status = |
3881 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); | |
03cfb420 BS |
3882 | nfs4_unlock_state(); |
3883 | ||
17456804 BS |
3884 | return nfs_ok; |
3885 | } | |
3886 | ||
e1ca12df BS |
3887 | __be32 |
3888 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
3889 | struct nfsd4_free_stateid *free_stateid) | |
3890 | { | |
3891 | stateid_t *stateid = &free_stateid->fr_stateid; | |
2da1cec7 | 3892 | struct nfs4_stid *s; |
3bd64a5b | 3893 | struct nfs4_delegation *dp; |
38c2f4b1 | 3894 | struct nfs4_client *cl = cstate->session->se_client; |
2da1cec7 | 3895 | __be32 ret = nfserr_bad_stateid; |
e1ca12df BS |
3896 | |
3897 | nfs4_lock_state(); | |
38c2f4b1 | 3898 | s = find_stateid(cl, stateid); |
2da1cec7 | 3899 | if (!s) |
81b82965 | 3900 | goto out; |
2da1cec7 BF |
3901 | switch (s->sc_type) { |
3902 | case NFS4_DELEG_STID: | |
e1ca12df BS |
3903 | ret = nfserr_locks_held; |
3904 | goto out; | |
2da1cec7 BF |
3905 | case NFS4_OPEN_STID: |
3906 | case NFS4_LOCK_STID: | |
3907 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); | |
3908 | if (ret) | |
3909 | goto out; | |
3910 | if (s->sc_type == NFS4_LOCK_STID) | |
3911 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); | |
3912 | else | |
3913 | ret = nfserr_locks_held; | |
f7a4d872 | 3914 | break; |
3bd64a5b BF |
3915 | case NFS4_REVOKED_DELEG_STID: |
3916 | dp = delegstateid(s); | |
3917 | destroy_revoked_delegation(dp); | |
3918 | ret = nfs_ok; | |
3919 | break; | |
f7a4d872 BF |
3920 | default: |
3921 | ret = nfserr_bad_stateid; | |
e1ca12df | 3922 | } |
e1ca12df BS |
3923 | out: |
3924 | nfs4_unlock_state(); | |
3925 | return ret; | |
3926 | } | |
3927 | ||
4c4cd222 N |
3928 | static inline int |
3929 | setlkflg (int type) | |
3930 | { | |
3931 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
3932 | RD_STATE : WR_STATE; | |
3933 | } | |
1da177e4 | 3934 | |
dcef0413 | 3935 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
c0a5d93e BF |
3936 | { |
3937 | struct svc_fh *current_fh = &cstate->current_fh; | |
3938 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
3939 | __be32 status; | |
3940 | ||
c0a5d93e BF |
3941 | status = nfsd4_check_seqid(cstate, sop, seqid); |
3942 | if (status) | |
3943 | return status; | |
3bd64a5b BF |
3944 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
3945 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) | |
f7a4d872 BF |
3946 | /* |
3947 | * "Closed" stateid's exist *only* to return | |
3bd64a5b BF |
3948 | * nfserr_replay_me from the previous step, and |
3949 | * revoked delegations are kept only for free_stateid. | |
f7a4d872 BF |
3950 | */ |
3951 | return nfserr_bad_stateid; | |
3952 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); | |
3953 | if (status) | |
3954 | return status; | |
3955 | return nfs4_check_fh(current_fh, stp); | |
c0a5d93e BF |
3956 | } |
3957 | ||
1da177e4 LT |
3958 | /* |
3959 | * Checks for sequence id mutating operations. | |
3960 | */ | |
b37ad28b | 3961 | static __be32 |
dd453dfd | 3962 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
2288d0e3 | 3963 | stateid_t *stateid, char typemask, |
3320fef1 SK |
3964 | struct nfs4_ol_stateid **stpp, |
3965 | struct nfsd_net *nn) | |
1da177e4 | 3966 | { |
0836f587 | 3967 | __be32 status; |
38c2f4b1 | 3968 | struct nfs4_stid *s; |
e17f99b7 | 3969 | struct nfs4_ol_stateid *stp = NULL; |
1da177e4 | 3970 | |
8c10cbdb BH |
3971 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
3972 | seqid, STATEID_VAL(stateid)); | |
3a4f98bb | 3973 | |
1da177e4 | 3974 | *stpp = NULL; |
3320fef1 SK |
3975 | status = nfsd4_lookup_stateid(stateid, typemask, &s, |
3976 | cstate->minorversion, nn); | |
c0a5d93e BF |
3977 | if (status) |
3978 | return status; | |
e17f99b7 | 3979 | stp = openlockstateid(s); |
3d74e6a5 | 3980 | if (!nfsd4_has_session(cstate)) |
e17f99b7 | 3981 | cstate->replay_owner = stp->st_stateowner; |
1da177e4 | 3982 | |
e17f99b7 TM |
3983 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
3984 | if (!status) | |
3985 | *stpp = stp; | |
3986 | return status; | |
c0a5d93e | 3987 | } |
39325bd0 | 3988 | |
3320fef1 SK |
3989 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
3990 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) | |
c0a5d93e BF |
3991 | { |
3992 | __be32 status; | |
3993 | struct nfs4_openowner *oo; | |
1da177e4 | 3994 | |
c0a5d93e | 3995 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
3320fef1 | 3996 | NFS4_OPEN_STID, stpp, nn); |
7a8711c9 BF |
3997 | if (status) |
3998 | return status; | |
c0a5d93e | 3999 | oo = openowner((*stpp)->st_stateowner); |
dad1c067 | 4000 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
3a4f98bb | 4001 | return nfserr_bad_stateid; |
3a4f98bb | 4002 | return nfs_ok; |
1da177e4 LT |
4003 | } |
4004 | ||
b37ad28b | 4005 | __be32 |
ca364317 | 4006 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 4007 | struct nfsd4_open_confirm *oc) |
1da177e4 | 4008 | { |
b37ad28b | 4009 | __be32 status; |
fe0750e5 | 4010 | struct nfs4_openowner *oo; |
dcef0413 | 4011 | struct nfs4_ol_stateid *stp; |
3320fef1 | 4012 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4013 | |
a6a9f18f AV |
4014 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
4015 | cstate->current_fh.fh_dentry); | |
1da177e4 | 4016 | |
ca364317 | 4017 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
a8cddc5d BF |
4018 | if (status) |
4019 | return status; | |
1da177e4 LT |
4020 | |
4021 | nfs4_lock_state(); | |
4022 | ||
9072d5c6 | 4023 | status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 4024 | oc->oc_seqid, &oc->oc_req_stateid, |
3320fef1 | 4025 | NFS4_OPEN_STID, &stp, nn); |
9072d5c6 | 4026 | if (status) |
68b66e82 | 4027 | goto out; |
fe0750e5 | 4028 | oo = openowner(stp->st_stateowner); |
68b66e82 | 4029 | status = nfserr_bad_stateid; |
dad1c067 | 4030 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
68b66e82 | 4031 | goto out; |
dad1c067 | 4032 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
dcef0413 BF |
4033 | update_stateid(&stp->st_stid.sc_stateid); |
4034 | memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); | |
8c10cbdb | 4035 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
dcef0413 | 4036 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
c7b9a459 | 4037 | |
2a4317c5 | 4038 | nfsd4_client_record_create(oo->oo_owner.so_client); |
68b66e82 | 4039 | status = nfs_ok; |
1da177e4 | 4040 | out: |
9411b1d4 | 4041 | nfsd4_bump_seqid(cstate, status); |
5ec094c1 BF |
4042 | if (!cstate->replay_owner) |
4043 | nfs4_unlock_state(); | |
1da177e4 LT |
4044 | return status; |
4045 | } | |
4046 | ||
6409a5a6 | 4047 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
1da177e4 | 4048 | { |
82c5ff1b | 4049 | if (!test_access(access, stp)) |
6409a5a6 BF |
4050 | return; |
4051 | nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access)); | |
82c5ff1b | 4052 | clear_access(access, stp); |
6409a5a6 | 4053 | } |
f197c271 | 4054 | |
6409a5a6 BF |
4055 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
4056 | { | |
4057 | switch (to_access) { | |
4058 | case NFS4_SHARE_ACCESS_READ: | |
4059 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); | |
4060 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
4061 | break; | |
4062 | case NFS4_SHARE_ACCESS_WRITE: | |
4063 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); | |
4064 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
4065 | break; | |
4066 | case NFS4_SHARE_ACCESS_BOTH: | |
4067 | break; | |
4068 | default: | |
063b0fb9 | 4069 | WARN_ON_ONCE(1); |
1da177e4 LT |
4070 | } |
4071 | } | |
4072 | ||
4073 | static void | |
ce0fc43c | 4074 | reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp) |
1da177e4 LT |
4075 | { |
4076 | int i; | |
4077 | for (i = 0; i < 4; i++) { | |
4078 | if ((i & deny) != i) | |
ce0fc43c | 4079 | clear_deny(i, stp); |
1da177e4 LT |
4080 | } |
4081 | } | |
4082 | ||
b37ad28b | 4083 | __be32 |
ca364317 BF |
4084 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
4085 | struct nfsd4_compound_state *cstate, | |
a4f1706a | 4086 | struct nfsd4_open_downgrade *od) |
1da177e4 | 4087 | { |
b37ad28b | 4088 | __be32 status; |
dcef0413 | 4089 | struct nfs4_ol_stateid *stp; |
3320fef1 | 4090 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4091 | |
a6a9f18f AV |
4092 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
4093 | cstate->current_fh.fh_dentry); | |
1da177e4 | 4094 | |
c30e92df | 4095 | /* We don't yet support WANT bits: */ |
2c8bd7e0 BH |
4096 | if (od->od_deleg_want) |
4097 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, | |
4098 | od->od_deleg_want); | |
1da177e4 LT |
4099 | |
4100 | nfs4_lock_state(); | |
c0a5d93e | 4101 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
3320fef1 | 4102 | &od->od_stateid, &stp, nn); |
9072d5c6 | 4103 | if (status) |
1da177e4 | 4104 | goto out; |
1da177e4 | 4105 | status = nfserr_inval; |
82c5ff1b JL |
4106 | if (!test_access(od->od_share_access, stp)) { |
4107 | dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n", | |
1da177e4 LT |
4108 | stp->st_access_bmap, od->od_share_access); |
4109 | goto out; | |
4110 | } | |
ce0fc43c | 4111 | if (!test_deny(od->od_share_deny, stp)) { |
1da177e4 LT |
4112 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", |
4113 | stp->st_deny_bmap, od->od_share_deny); | |
4114 | goto out; | |
4115 | } | |
6409a5a6 | 4116 | nfs4_stateid_downgrade(stp, od->od_share_access); |
1da177e4 | 4117 | |
ce0fc43c | 4118 | reset_union_bmap_deny(od->od_share_deny, stp); |
1da177e4 | 4119 | |
dcef0413 BF |
4120 | update_stateid(&stp->st_stid.sc_stateid); |
4121 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); | |
1da177e4 LT |
4122 | status = nfs_ok; |
4123 | out: | |
9411b1d4 | 4124 | nfsd4_bump_seqid(cstate, status); |
5ec094c1 BF |
4125 | if (!cstate->replay_owner) |
4126 | nfs4_unlock_state(); | |
1da177e4 LT |
4127 | return status; |
4128 | } | |
4129 | ||
f7a4d872 BF |
4130 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
4131 | { | |
4132 | unhash_open_stateid(s); | |
4133 | s->st_stid.sc_type = NFS4_CLOSED_STID; | |
38c387b5 BF |
4134 | } |
4135 | ||
1da177e4 LT |
4136 | /* |
4137 | * nfs4_unlock_state() called after encode | |
4138 | */ | |
b37ad28b | 4139 | __be32 |
ca364317 | 4140 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 4141 | struct nfsd4_close *close) |
1da177e4 | 4142 | { |
b37ad28b | 4143 | __be32 status; |
fe0750e5 | 4144 | struct nfs4_openowner *oo; |
dcef0413 | 4145 | struct nfs4_ol_stateid *stp; |
3320fef1 SK |
4146 | struct net *net = SVC_NET(rqstp); |
4147 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 | 4148 | |
a6a9f18f AV |
4149 | dprintk("NFSD: nfsd4_close on file %pd\n", |
4150 | cstate->current_fh.fh_dentry); | |
1da177e4 LT |
4151 | |
4152 | nfs4_lock_state(); | |
f7a4d872 BF |
4153 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
4154 | &close->cl_stateid, | |
4155 | NFS4_OPEN_STID|NFS4_CLOSED_STID, | |
3320fef1 | 4156 | &stp, nn); |
9411b1d4 | 4157 | nfsd4_bump_seqid(cstate, status); |
9072d5c6 | 4158 | if (status) |
1da177e4 | 4159 | goto out; |
fe0750e5 | 4160 | oo = openowner(stp->st_stateowner); |
dcef0413 BF |
4161 | update_stateid(&stp->st_stid.sc_stateid); |
4162 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); | |
1da177e4 | 4163 | |
f7a4d872 | 4164 | nfsd4_close_open_stateid(stp); |
9411b1d4 | 4165 | |
01a87d91 | 4166 | if (cstate->minorversion) |
9411b1d4 | 4167 | free_generic_stateid(stp); |
01a87d91 | 4168 | else |
9411b1d4 | 4169 | oo->oo_last_closed_stid = stp; |
04ef5954 | 4170 | |
74dbafaf | 4171 | if (list_empty(&oo->oo_owner.so_stateids)) { |
3d74e6a5 | 4172 | if (cstate->minorversion) |
74dbafaf | 4173 | release_openowner(oo); |
3d74e6a5 | 4174 | else { |
74dbafaf BF |
4175 | /* |
4176 | * In the 4.0 case we need to keep the owners around a | |
4177 | * little while to handle CLOSE replay. | |
4178 | */ | |
53584f66 | 4179 | move_to_close_lru(oo, SVC_NET(rqstp)); |
74dbafaf BF |
4180 | } |
4181 | } | |
1da177e4 | 4182 | out: |
5ec094c1 BF |
4183 | if (!cstate->replay_owner) |
4184 | nfs4_unlock_state(); | |
1da177e4 LT |
4185 | return status; |
4186 | } | |
4187 | ||
b37ad28b | 4188 | __be32 |
ca364317 BF |
4189 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
4190 | struct nfsd4_delegreturn *dr) | |
1da177e4 | 4191 | { |
203a8c8e BF |
4192 | struct nfs4_delegation *dp; |
4193 | stateid_t *stateid = &dr->dr_stateid; | |
38c2f4b1 | 4194 | struct nfs4_stid *s; |
b37ad28b | 4195 | __be32 status; |
3320fef1 | 4196 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4197 | |
ca364317 | 4198 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
203a8c8e | 4199 | return status; |
1da177e4 LT |
4200 | |
4201 | nfs4_lock_state(); | |
3320fef1 SK |
4202 | status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s, |
4203 | cstate->minorversion, nn); | |
38c2f4b1 | 4204 | if (status) |
203a8c8e | 4205 | goto out; |
38c2f4b1 | 4206 | dp = delegstateid(s); |
d5477a8d | 4207 | status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate)); |
203a8c8e BF |
4208 | if (status) |
4209 | goto out; | |
203a8c8e | 4210 | |
3bd64a5b | 4211 | destroy_delegation(dp); |
1da177e4 | 4212 | out: |
203a8c8e BF |
4213 | nfs4_unlock_state(); |
4214 | ||
1da177e4 LT |
4215 | return status; |
4216 | } | |
4217 | ||
4218 | ||
1da177e4 | 4219 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
1da177e4 | 4220 | |
009673b4 | 4221 | #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1) |
1da177e4 | 4222 | |
87df4de8 BH |
4223 | static inline u64 |
4224 | end_offset(u64 start, u64 len) | |
4225 | { | |
4226 | u64 end; | |
4227 | ||
4228 | end = start + len; | |
4229 | return end >= start ? end: NFS4_MAX_UINT64; | |
4230 | } | |
4231 | ||
4232 | /* last octet in a range */ | |
4233 | static inline u64 | |
4234 | last_byte_offset(u64 start, u64 len) | |
4235 | { | |
4236 | u64 end; | |
4237 | ||
063b0fb9 | 4238 | WARN_ON_ONCE(!len); |
87df4de8 BH |
4239 | end = start + len; |
4240 | return end > start ? end - 1: NFS4_MAX_UINT64; | |
4241 | } | |
4242 | ||
009673b4 | 4243 | static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername) |
1da177e4 LT |
4244 | { |
4245 | return (file_hashval(inode) + cl_id | |
4246 | + opaque_hashval(ownername->data, ownername->len)) | |
009673b4 | 4247 | & LOCKOWNER_INO_HASH_MASK; |
1da177e4 LT |
4248 | } |
4249 | ||
1da177e4 LT |
4250 | /* |
4251 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
4252 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
4253 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
4254 | * locking, this prevents us from being completely protocol-compliant. The | |
4255 | * real solution to this problem is to start using unsigned file offsets in | |
4256 | * the VFS, but this is a very deep change! | |
4257 | */ | |
4258 | static inline void | |
4259 | nfs4_transform_lock_offset(struct file_lock *lock) | |
4260 | { | |
4261 | if (lock->fl_start < 0) | |
4262 | lock->fl_start = OFFSET_MAX; | |
4263 | if (lock->fl_end < 0) | |
4264 | lock->fl_end = OFFSET_MAX; | |
4265 | } | |
4266 | ||
d5b9026a N |
4267 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
4268 | * as a unique cookie to identify our (NFSv4's) posix locks. */ | |
7b021967 | 4269 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
d5b9026a | 4270 | }; |
1da177e4 LT |
4271 | |
4272 | static inline void | |
4273 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
4274 | { | |
fe0750e5 | 4275 | struct nfs4_lockowner *lo; |
1da177e4 | 4276 | |
d5b9026a | 4277 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
fe0750e5 BF |
4278 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
4279 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, | |
4280 | lo->lo_owner.so_owner.len, GFP_KERNEL); | |
7c13f344 BF |
4281 | if (!deny->ld_owner.data) |
4282 | /* We just don't care that much */ | |
4283 | goto nevermind; | |
fe0750e5 BF |
4284 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
4285 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; | |
d5b9026a | 4286 | } else { |
7c13f344 BF |
4287 | nevermind: |
4288 | deny->ld_owner.len = 0; | |
4289 | deny->ld_owner.data = NULL; | |
d5b9026a N |
4290 | deny->ld_clientid.cl_boot = 0; |
4291 | deny->ld_clientid.cl_id = 0; | |
1da177e4 LT |
4292 | } |
4293 | deny->ld_start = fl->fl_start; | |
87df4de8 BH |
4294 | deny->ld_length = NFS4_MAX_UINT64; |
4295 | if (fl->fl_end != NFS4_MAX_UINT64) | |
1da177e4 LT |
4296 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
4297 | deny->ld_type = NFS4_READ_LT; | |
4298 | if (fl->fl_type != F_RDLCK) | |
4299 | deny->ld_type = NFS4_WRITE_LT; | |
4300 | } | |
4301 | ||
b93d87c1 BF |
4302 | static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner) |
4303 | { | |
4304 | struct nfs4_ol_stateid *lst; | |
4305 | ||
4306 | if (!same_owner_str(&lo->lo_owner, owner, clid)) | |
4307 | return false; | |
27b11428 BF |
4308 | if (list_empty(&lo->lo_owner.so_stateids)) { |
4309 | WARN_ON_ONCE(1); | |
4310 | return false; | |
4311 | } | |
b93d87c1 BF |
4312 | lst = list_first_entry(&lo->lo_owner.so_stateids, |
4313 | struct nfs4_ol_stateid, st_perstateowner); | |
4314 | return lst->st_file->fi_inode == inode; | |
4315 | } | |
4316 | ||
fe0750e5 BF |
4317 | static struct nfs4_lockowner * |
4318 | find_lockowner_str(struct inode *inode, clientid_t *clid, | |
20e9e2bc | 4319 | struct xdr_netobj *owner, struct nfsd_net *nn) |
1da177e4 | 4320 | { |
009673b4 | 4321 | unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner); |
b93d87c1 | 4322 | struct nfs4_lockowner *lo; |
1da177e4 | 4323 | |
20e9e2bc | 4324 | list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) { |
b93d87c1 BF |
4325 | if (same_lockowner_ino(lo, inode, clid, owner)) |
4326 | return lo; | |
1da177e4 LT |
4327 | } |
4328 | return NULL; | |
4329 | } | |
4330 | ||
dcef0413 | 4331 | static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp) |
ff194bd9 | 4332 | { |
009673b4 BF |
4333 | struct inode *inode = open_stp->st_file->fi_inode; |
4334 | unsigned int inohash = lockowner_ino_hashval(inode, | |
4335 | clp->cl_clientid.cl_id, &lo->lo_owner.so_owner); | |
9b531137 | 4336 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
009673b4 | 4337 | |
9b531137 | 4338 | list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]); |
20e9e2bc | 4339 | list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]); |
fe0750e5 | 4340 | list_add(&lo->lo_perstateid, &open_stp->st_lockowners); |
ff194bd9 BF |
4341 | } |
4342 | ||
1da177e4 LT |
4343 | /* |
4344 | * Alloc a lock owner structure. | |
4345 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
25985edc | 4346 | * occurred. |
1da177e4 | 4347 | * |
16bfdaaf | 4348 | * strhashval = ownerstr_hashval |
1da177e4 LT |
4349 | */ |
4350 | ||
fe0750e5 | 4351 | static struct nfs4_lockowner * |
dcef0413 | 4352 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) { |
fe0750e5 | 4353 | struct nfs4_lockowner *lo; |
1da177e4 | 4354 | |
fe0750e5 BF |
4355 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
4356 | if (!lo) | |
1da177e4 | 4357 | return NULL; |
fe0750e5 BF |
4358 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
4359 | lo->lo_owner.so_is_open_owner = 0; | |
b59e3c0e NB |
4360 | /* It is the openowner seqid that will be incremented in encode in the |
4361 | * case of new lockowners; so increment the lock seqid manually: */ | |
fe0750e5 BF |
4362 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
4363 | hash_lockowner(lo, strhashval, clp, open_stp); | |
4364 | return lo; | |
1da177e4 LT |
4365 | } |
4366 | ||
dcef0413 BF |
4367 | static struct nfs4_ol_stateid * |
4368 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) | |
1da177e4 | 4369 | { |
dcef0413 | 4370 | struct nfs4_ol_stateid *stp; |
d3b313a4 | 4371 | struct nfs4_client *clp = lo->lo_owner.so_client; |
1da177e4 | 4372 | |
996e0938 | 4373 | stp = nfs4_alloc_stateid(clp); |
5ac049ac | 4374 | if (stp == NULL) |
6136d2b4 | 4375 | return NULL; |
3abdb607 | 4376 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
8beefa24 | 4377 | list_add(&stp->st_perfile, &fp->fi_stateids); |
fe0750e5 BF |
4378 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
4379 | stp->st_stateowner = &lo->lo_owner; | |
13cd2184 | 4380 | get_nfs4_file(fp); |
1da177e4 | 4381 | stp->st_file = fp; |
0997b173 | 4382 | stp->st_access_bmap = 0; |
1da177e4 | 4383 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
4c4cd222 | 4384 | stp->st_openstp = open_stp; |
1da177e4 LT |
4385 | return stp; |
4386 | } | |
4387 | ||
fd39ca9a | 4388 | static int |
1da177e4 LT |
4389 | check_lock_length(u64 offset, u64 length) |
4390 | { | |
87df4de8 | 4391 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
1da177e4 LT |
4392 | LOFF_OVERFLOW(offset, length))); |
4393 | } | |
4394 | ||
dcef0413 | 4395 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
0997b173 BF |
4396 | { |
4397 | struct nfs4_file *fp = lock_stp->st_file; | |
4398 | int oflag = nfs4_access_to_omode(access); | |
4399 | ||
82c5ff1b | 4400 | if (test_access(access, lock_stp)) |
0997b173 BF |
4401 | return; |
4402 | nfs4_file_get_access(fp, oflag); | |
82c5ff1b | 4403 | set_access(access, lock_stp); |
0997b173 BF |
4404 | } |
4405 | ||
2355c596 | 4406 | static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new) |
64a284d0 BF |
4407 | { |
4408 | struct nfs4_file *fi = ost->st_file; | |
4409 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); | |
4410 | struct nfs4_client *cl = oo->oo_owner.so_client; | |
4411 | struct nfs4_lockowner *lo; | |
4412 | unsigned int strhashval; | |
20e9e2bc | 4413 | struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id); |
64a284d0 | 4414 | |
20e9e2bc SK |
4415 | lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, |
4416 | &lock->v.new.owner, nn); | |
64a284d0 BF |
4417 | if (lo) { |
4418 | if (!cstate->minorversion) | |
4419 | return nfserr_bad_seqid; | |
4420 | /* XXX: a lockowner always has exactly one stateid: */ | |
4421 | *lst = list_first_entry(&lo->lo_owner.so_stateids, | |
4422 | struct nfs4_ol_stateid, st_perstateowner); | |
4423 | return nfs_ok; | |
4424 | } | |
16bfdaaf | 4425 | strhashval = ownerstr_hashval(cl->cl_clientid.cl_id, |
64a284d0 BF |
4426 | &lock->v.new.owner); |
4427 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); | |
4428 | if (lo == NULL) | |
4429 | return nfserr_jukebox; | |
4430 | *lst = alloc_init_lock_stateid(lo, fi, ost); | |
4431 | if (*lst == NULL) { | |
4432 | release_lockowner(lo); | |
4433 | return nfserr_jukebox; | |
4434 | } | |
4435 | *new = true; | |
4436 | return nfs_ok; | |
4437 | } | |
4438 | ||
1da177e4 LT |
4439 | /* |
4440 | * LOCK operation | |
4441 | */ | |
b37ad28b | 4442 | __be32 |
ca364317 | 4443 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 4444 | struct nfsd4_lock *lock) |
1da177e4 | 4445 | { |
fe0750e5 BF |
4446 | struct nfs4_openowner *open_sop = NULL; |
4447 | struct nfs4_lockowner *lock_sop = NULL; | |
dcef0413 | 4448 | struct nfs4_ol_stateid *lock_stp; |
7d947842 | 4449 | struct file *filp = NULL; |
21179d81 JL |
4450 | struct file_lock *file_lock = NULL; |
4451 | struct file_lock *conflock = NULL; | |
b37ad28b | 4452 | __be32 status = 0; |
64a284d0 | 4453 | bool new_state = false; |
b34f27aa | 4454 | int lkflg; |
b8dd7b9a | 4455 | int err; |
3320fef1 SK |
4456 | struct net *net = SVC_NET(rqstp); |
4457 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 LT |
4458 | |
4459 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
4460 | (long long) lock->lk_offset, | |
4461 | (long long) lock->lk_length); | |
4462 | ||
1da177e4 LT |
4463 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
4464 | return nfserr_inval; | |
4465 | ||
ca364317 | 4466 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
8837abca | 4467 | S_IFREG, NFSD_MAY_LOCK))) { |
a6f6ef2f AA |
4468 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
4469 | return status; | |
4470 | } | |
4471 | ||
1da177e4 LT |
4472 | nfs4_lock_state(); |
4473 | ||
4474 | if (lock->lk_is_new) { | |
dcef0413 | 4475 | struct nfs4_ol_stateid *open_stp = NULL; |
684e5638 BF |
4476 | |
4477 | if (nfsd4_has_session(cstate)) | |
4478 | /* See rfc 5661 18.10.3: given clientid is ignored: */ | |
4479 | memcpy(&lock->v.new.clientid, | |
4480 | &cstate->session->se_client->cl_clientid, | |
4481 | sizeof(clientid_t)); | |
4482 | ||
1da177e4 | 4483 | status = nfserr_stale_clientid; |
2c142baa | 4484 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
1da177e4 | 4485 | goto out; |
1da177e4 | 4486 | |
1da177e4 | 4487 | /* validate and update open stateid and open seqid */ |
c0a5d93e | 4488 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
1da177e4 LT |
4489 | lock->lk_new_open_seqid, |
4490 | &lock->lk_new_open_stateid, | |
3320fef1 | 4491 | &open_stp, nn); |
37515177 | 4492 | if (status) |
1da177e4 | 4493 | goto out; |
fe0750e5 | 4494 | open_sop = openowner(open_stp->st_stateowner); |
b34f27aa | 4495 | status = nfserr_bad_stateid; |
684e5638 | 4496 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
b34f27aa BF |
4497 | &lock->v.new.clientid)) |
4498 | goto out; | |
64a284d0 BF |
4499 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
4500 | &lock_stp, &new_state); | |
e1aaa891 | 4501 | } else |
dd453dfd | 4502 | status = nfs4_preprocess_seqid_op(cstate, |
fe0750e5 BF |
4503 | lock->lk_old_lock_seqid, |
4504 | &lock->lk_old_lock_stateid, | |
3320fef1 | 4505 | NFS4_LOCK_STID, &lock_stp, nn); |
e1aaa891 BF |
4506 | if (status) |
4507 | goto out; | |
64a284d0 | 4508 | lock_sop = lockowner(lock_stp->st_stateowner); |
1da177e4 | 4509 | |
b34f27aa BF |
4510 | lkflg = setlkflg(lock->lk_type); |
4511 | status = nfs4_check_openmode(lock_stp, lkflg); | |
4512 | if (status) | |
4513 | goto out; | |
4514 | ||
0dd395dc | 4515 | status = nfserr_grace; |
3320fef1 | 4516 | if (locks_in_grace(net) && !lock->lk_reclaim) |
0dd395dc N |
4517 | goto out; |
4518 | status = nfserr_no_grace; | |
3320fef1 | 4519 | if (!locks_in_grace(net) && lock->lk_reclaim) |
0dd395dc N |
4520 | goto out; |
4521 | ||
21179d81 JL |
4522 | file_lock = locks_alloc_lock(); |
4523 | if (!file_lock) { | |
4524 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
4525 | status = nfserr_jukebox; | |
4526 | goto out; | |
4527 | } | |
4528 | ||
4529 | locks_init_lock(file_lock); | |
1da177e4 LT |
4530 | switch (lock->lk_type) { |
4531 | case NFS4_READ_LT: | |
4532 | case NFS4_READW_LT: | |
0997b173 BF |
4533 | filp = find_readable_file(lock_stp->st_file); |
4534 | if (filp) | |
4535 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); | |
21179d81 | 4536 | file_lock->fl_type = F_RDLCK; |
529d7b2a | 4537 | break; |
1da177e4 LT |
4538 | case NFS4_WRITE_LT: |
4539 | case NFS4_WRITEW_LT: | |
0997b173 BF |
4540 | filp = find_writeable_file(lock_stp->st_file); |
4541 | if (filp) | |
4542 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); | |
21179d81 | 4543 | file_lock->fl_type = F_WRLCK; |
529d7b2a | 4544 | break; |
1da177e4 LT |
4545 | default: |
4546 | status = nfserr_inval; | |
4547 | goto out; | |
4548 | } | |
f9d7562f BF |
4549 | if (!filp) { |
4550 | status = nfserr_openmode; | |
4551 | goto out; | |
4552 | } | |
21179d81 JL |
4553 | file_lock->fl_owner = (fl_owner_t)lock_sop; |
4554 | file_lock->fl_pid = current->tgid; | |
4555 | file_lock->fl_file = filp; | |
4556 | file_lock->fl_flags = FL_POSIX; | |
4557 | file_lock->fl_lmops = &nfsd_posix_mng_ops; | |
4558 | file_lock->fl_start = lock->lk_offset; | |
4559 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); | |
4560 | nfs4_transform_lock_offset(file_lock); | |
4561 | ||
4562 | conflock = locks_alloc_lock(); | |
4563 | if (!conflock) { | |
4564 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
4565 | status = nfserr_jukebox; | |
4566 | goto out; | |
4567 | } | |
1da177e4 | 4568 | |
21179d81 | 4569 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
b8dd7b9a | 4570 | switch (-err) { |
1da177e4 | 4571 | case 0: /* success! */ |
dcef0413 BF |
4572 | update_stateid(&lock_stp->st_stid.sc_stateid); |
4573 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, | |
1da177e4 | 4574 | sizeof(stateid_t)); |
b8dd7b9a | 4575 | status = 0; |
eb76b3fd AA |
4576 | break; |
4577 | case (EAGAIN): /* conflock holds conflicting lock */ | |
4578 | status = nfserr_denied; | |
4579 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
21179d81 | 4580 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
eb76b3fd | 4581 | break; |
1da177e4 LT |
4582 | case (EDEADLK): |
4583 | status = nfserr_deadlock; | |
eb76b3fd | 4584 | break; |
3e772463 | 4585 | default: |
fd85b817 | 4586 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
3e772463 | 4587 | status = nfserrno(err); |
eb76b3fd | 4588 | break; |
1da177e4 | 4589 | } |
1da177e4 | 4590 | out: |
64a284d0 | 4591 | if (status && new_state) |
f044ff83 | 4592 | release_lockowner(lock_sop); |
9411b1d4 | 4593 | nfsd4_bump_seqid(cstate, status); |
5ec094c1 BF |
4594 | if (!cstate->replay_owner) |
4595 | nfs4_unlock_state(); | |
21179d81 JL |
4596 | if (file_lock) |
4597 | locks_free_lock(file_lock); | |
4598 | if (conflock) | |
4599 | locks_free_lock(conflock); | |
1da177e4 LT |
4600 | return status; |
4601 | } | |
4602 | ||
55ef1274 BF |
4603 | /* |
4604 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, | |
4605 | * so we do a temporary open here just to get an open file to pass to | |
4606 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an | |
4607 | * inode operation.) | |
4608 | */ | |
04da6e9d | 4609 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
55ef1274 BF |
4610 | { |
4611 | struct file *file; | |
04da6e9d AV |
4612 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
4613 | if (!err) { | |
4614 | err = nfserrno(vfs_test_lock(file, lock)); | |
4615 | nfsd_close(file); | |
4616 | } | |
55ef1274 BF |
4617 | return err; |
4618 | } | |
4619 | ||
1da177e4 LT |
4620 | /* |
4621 | * LOCKT operation | |
4622 | */ | |
b37ad28b | 4623 | __be32 |
ca364317 BF |
4624 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
4625 | struct nfsd4_lockt *lockt) | |
1da177e4 LT |
4626 | { |
4627 | struct inode *inode; | |
21179d81 | 4628 | struct file_lock *file_lock = NULL; |
fe0750e5 | 4629 | struct nfs4_lockowner *lo; |
b37ad28b | 4630 | __be32 status; |
7f2210fa | 4631 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4632 | |
5ccb0066 | 4633 | if (locks_in_grace(SVC_NET(rqstp))) |
1da177e4 LT |
4634 | return nfserr_grace; |
4635 | ||
4636 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
4637 | return nfserr_inval; | |
4638 | ||
1da177e4 LT |
4639 | nfs4_lock_state(); |
4640 | ||
9b2ef62b BF |
4641 | if (!nfsd4_has_session(cstate)) { |
4642 | status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL); | |
4643 | if (status) | |
4644 | goto out; | |
4645 | } | |
1da177e4 | 4646 | |
75c096f7 | 4647 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
1da177e4 | 4648 | goto out; |
1da177e4 | 4649 | |
ca364317 | 4650 | inode = cstate->current_fh.fh_dentry->d_inode; |
21179d81 JL |
4651 | file_lock = locks_alloc_lock(); |
4652 | if (!file_lock) { | |
4653 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
4654 | status = nfserr_jukebox; | |
4655 | goto out; | |
4656 | } | |
4657 | locks_init_lock(file_lock); | |
1da177e4 LT |
4658 | switch (lockt->lt_type) { |
4659 | case NFS4_READ_LT: | |
4660 | case NFS4_READW_LT: | |
21179d81 | 4661 | file_lock->fl_type = F_RDLCK; |
1da177e4 LT |
4662 | break; |
4663 | case NFS4_WRITE_LT: | |
4664 | case NFS4_WRITEW_LT: | |
21179d81 | 4665 | file_lock->fl_type = F_WRLCK; |
1da177e4 LT |
4666 | break; |
4667 | default: | |
2fdada03 | 4668 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
1da177e4 LT |
4669 | status = nfserr_inval; |
4670 | goto out; | |
4671 | } | |
4672 | ||
20e9e2bc | 4673 | lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn); |
fe0750e5 | 4674 | if (lo) |
21179d81 JL |
4675 | file_lock->fl_owner = (fl_owner_t)lo; |
4676 | file_lock->fl_pid = current->tgid; | |
4677 | file_lock->fl_flags = FL_POSIX; | |
1da177e4 | 4678 | |
21179d81 JL |
4679 | file_lock->fl_start = lockt->lt_offset; |
4680 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); | |
1da177e4 | 4681 | |
21179d81 | 4682 | nfs4_transform_lock_offset(file_lock); |
1da177e4 | 4683 | |
21179d81 | 4684 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
04da6e9d | 4685 | if (status) |
fd85b817 | 4686 | goto out; |
04da6e9d | 4687 | |
21179d81 | 4688 | if (file_lock->fl_type != F_UNLCK) { |
1da177e4 | 4689 | status = nfserr_denied; |
21179d81 | 4690 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
1da177e4 LT |
4691 | } |
4692 | out: | |
4693 | nfs4_unlock_state(); | |
21179d81 JL |
4694 | if (file_lock) |
4695 | locks_free_lock(file_lock); | |
1da177e4 LT |
4696 | return status; |
4697 | } | |
4698 | ||
b37ad28b | 4699 | __be32 |
ca364317 | 4700 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 4701 | struct nfsd4_locku *locku) |
1da177e4 | 4702 | { |
dcef0413 | 4703 | struct nfs4_ol_stateid *stp; |
1da177e4 | 4704 | struct file *filp = NULL; |
21179d81 | 4705 | struct file_lock *file_lock = NULL; |
b37ad28b | 4706 | __be32 status; |
b8dd7b9a | 4707 | int err; |
3320fef1 SK |
4708 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
4709 | ||
1da177e4 LT |
4710 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
4711 | (long long) locku->lu_offset, | |
4712 | (long long) locku->lu_length); | |
4713 | ||
4714 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
4715 | return nfserr_inval; | |
4716 | ||
4717 | nfs4_lock_state(); | |
4718 | ||
9072d5c6 | 4719 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
3320fef1 SK |
4720 | &locku->lu_stateid, NFS4_LOCK_STID, |
4721 | &stp, nn); | |
9072d5c6 | 4722 | if (status) |
1da177e4 | 4723 | goto out; |
f9d7562f BF |
4724 | filp = find_any_file(stp->st_file); |
4725 | if (!filp) { | |
4726 | status = nfserr_lock_range; | |
4727 | goto out; | |
4728 | } | |
21179d81 JL |
4729 | file_lock = locks_alloc_lock(); |
4730 | if (!file_lock) { | |
4731 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
4732 | status = nfserr_jukebox; | |
4733 | goto out; | |
4734 | } | |
4735 | locks_init_lock(file_lock); | |
4736 | file_lock->fl_type = F_UNLCK; | |
0a262ffb | 4737 | file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
21179d81 JL |
4738 | file_lock->fl_pid = current->tgid; |
4739 | file_lock->fl_file = filp; | |
4740 | file_lock->fl_flags = FL_POSIX; | |
4741 | file_lock->fl_lmops = &nfsd_posix_mng_ops; | |
4742 | file_lock->fl_start = locku->lu_offset; | |
4743 | ||
4744 | file_lock->fl_end = last_byte_offset(locku->lu_offset, | |
4745 | locku->lu_length); | |
4746 | nfs4_transform_lock_offset(file_lock); | |
1da177e4 | 4747 | |
21179d81 | 4748 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
b8dd7b9a | 4749 | if (err) { |
fd85b817 | 4750 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
1da177e4 LT |
4751 | goto out_nfserr; |
4752 | } | |
dcef0413 BF |
4753 | update_stateid(&stp->st_stid.sc_stateid); |
4754 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); | |
1da177e4 LT |
4755 | |
4756 | out: | |
9411b1d4 | 4757 | nfsd4_bump_seqid(cstate, status); |
71c3bcd7 BF |
4758 | if (!cstate->replay_owner) |
4759 | nfs4_unlock_state(); | |
21179d81 JL |
4760 | if (file_lock) |
4761 | locks_free_lock(file_lock); | |
1da177e4 LT |
4762 | return status; |
4763 | ||
4764 | out_nfserr: | |
b8dd7b9a | 4765 | status = nfserrno(err); |
1da177e4 LT |
4766 | goto out; |
4767 | } | |
4768 | ||
4769 | /* | |
4770 | * returns | |
4771 | * 1: locks held by lockowner | |
4772 | * 0: no locks held by lockowner | |
4773 | */ | |
4774 | static int | |
fe0750e5 | 4775 | check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) |
1da177e4 LT |
4776 | { |
4777 | struct file_lock **flpp; | |
f9d7562f | 4778 | struct inode *inode = filp->fi_inode; |
1da177e4 LT |
4779 | int status = 0; |
4780 | ||
1c8c601a | 4781 | spin_lock(&inode->i_lock); |
1da177e4 | 4782 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
796dadfd | 4783 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
1da177e4 LT |
4784 | status = 1; |
4785 | goto out; | |
796dadfd | 4786 | } |
1da177e4 LT |
4787 | } |
4788 | out: | |
1c8c601a | 4789 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
4790 | return status; |
4791 | } | |
4792 | ||
b37ad28b | 4793 | __be32 |
b591480b BF |
4794 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
4795 | struct nfsd4_compound_state *cstate, | |
4796 | struct nfsd4_release_lockowner *rlockowner) | |
1da177e4 LT |
4797 | { |
4798 | clientid_t *clid = &rlockowner->rl_clientid; | |
3e9e3dbe | 4799 | struct nfs4_stateowner *sop; |
fe0750e5 | 4800 | struct nfs4_lockowner *lo; |
dcef0413 | 4801 | struct nfs4_ol_stateid *stp; |
1da177e4 | 4802 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
3e9e3dbe | 4803 | struct list_head matches; |
16bfdaaf | 4804 | unsigned int hashval = ownerstr_hashval(clid->cl_id, owner); |
b37ad28b | 4805 | __be32 status; |
7f2210fa | 4806 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 LT |
4807 | |
4808 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
4809 | clid->cl_boot, clid->cl_id); | |
4810 | ||
1da177e4 LT |
4811 | nfs4_lock_state(); |
4812 | ||
9b2ef62b BF |
4813 | status = lookup_clientid(clid, cstate->minorversion, nn, NULL); |
4814 | if (status) | |
4815 | goto out; | |
4816 | ||
3e9e3dbe | 4817 | status = nfserr_locks_held; |
3e9e3dbe | 4818 | INIT_LIST_HEAD(&matches); |
06f1f864 | 4819 | |
9b531137 | 4820 | list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) { |
16bfdaaf BF |
4821 | if (sop->so_is_open_owner) |
4822 | continue; | |
06f1f864 BF |
4823 | if (!same_owner_str(sop, owner, clid)) |
4824 | continue; | |
4825 | list_for_each_entry(stp, &sop->so_stateids, | |
4826 | st_perstateowner) { | |
4827 | lo = lockowner(sop); | |
4828 | if (check_for_locks(stp->st_file, lo)) | |
4829 | goto out; | |
4830 | list_add(&lo->lo_list, &matches); | |
1da177e4 | 4831 | } |
3e9e3dbe N |
4832 | } |
4833 | /* Clients probably won't expect us to return with some (but not all) | |
4834 | * of the lockowner state released; so don't release any until all | |
4835 | * have been checked. */ | |
4836 | status = nfs_ok; | |
0fa822e4 | 4837 | while (!list_empty(&matches)) { |
fe0750e5 BF |
4838 | lo = list_entry(matches.next, struct nfs4_lockowner, |
4839 | lo_list); | |
0fa822e4 N |
4840 | /* unhash_stateowner deletes so_perclient only |
4841 | * for openowners. */ | |
fe0750e5 BF |
4842 | list_del(&lo->lo_list); |
4843 | release_lockowner(lo); | |
1da177e4 LT |
4844 | } |
4845 | out: | |
4846 | nfs4_unlock_state(); | |
4847 | return status; | |
4848 | } | |
4849 | ||
4850 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 4851 | alloc_reclaim(void) |
1da177e4 | 4852 | { |
a55370a3 | 4853 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
4854 | } |
4855 | ||
0ce0c2b5 | 4856 | bool |
52e19c09 | 4857 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
c7b9a459 | 4858 | { |
0ce0c2b5 | 4859 | struct nfs4_client_reclaim *crp; |
c7b9a459 | 4860 | |
52e19c09 | 4861 | crp = nfsd4_find_reclaim_client(name, nn); |
0ce0c2b5 | 4862 | return (crp && crp->cr_clp); |
c7b9a459 N |
4863 | } |
4864 | ||
1da177e4 LT |
4865 | /* |
4866 | * failure => all reset bets are off, nfserr_no_grace... | |
4867 | */ | |
772a9bbb | 4868 | struct nfs4_client_reclaim * |
52e19c09 | 4869 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
1da177e4 LT |
4870 | { |
4871 | unsigned int strhashval; | |
772a9bbb | 4872 | struct nfs4_client_reclaim *crp; |
1da177e4 | 4873 | |
a55370a3 N |
4874 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
4875 | crp = alloc_reclaim(); | |
772a9bbb JL |
4876 | if (crp) { |
4877 | strhashval = clientstr_hashval(name); | |
4878 | INIT_LIST_HEAD(&crp->cr_strhash); | |
52e19c09 | 4879 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
772a9bbb | 4880 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
0ce0c2b5 | 4881 | crp->cr_clp = NULL; |
52e19c09 | 4882 | nn->reclaim_str_hashtbl_size++; |
772a9bbb JL |
4883 | } |
4884 | return crp; | |
1da177e4 LT |
4885 | } |
4886 | ||
ce30e539 | 4887 | void |
52e19c09 | 4888 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
ce30e539 JL |
4889 | { |
4890 | list_del(&crp->cr_strhash); | |
4891 | kfree(crp); | |
52e19c09 | 4892 | nn->reclaim_str_hashtbl_size--; |
ce30e539 JL |
4893 | } |
4894 | ||
2a4317c5 | 4895 | void |
52e19c09 | 4896 | nfs4_release_reclaim(struct nfsd_net *nn) |
1da177e4 LT |
4897 | { |
4898 | struct nfs4_client_reclaim *crp = NULL; | |
4899 | int i; | |
4900 | ||
1da177e4 | 4901 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
52e19c09 SK |
4902 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
4903 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, | |
1da177e4 | 4904 | struct nfs4_client_reclaim, cr_strhash); |
52e19c09 | 4905 | nfs4_remove_reclaim_record(crp, nn); |
1da177e4 LT |
4906 | } |
4907 | } | |
063b0fb9 | 4908 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
1da177e4 LT |
4909 | } |
4910 | ||
4911 | /* | |
4912 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
2a4317c5 | 4913 | struct nfs4_client_reclaim * |
52e19c09 | 4914 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
1da177e4 LT |
4915 | { |
4916 | unsigned int strhashval; | |
1da177e4 LT |
4917 | struct nfs4_client_reclaim *crp = NULL; |
4918 | ||
278c931c | 4919 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
1da177e4 | 4920 | |
278c931c | 4921 | strhashval = clientstr_hashval(recdir); |
52e19c09 | 4922 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
278c931c | 4923 | if (same_name(crp->cr_recdir, recdir)) { |
1da177e4 LT |
4924 | return crp; |
4925 | } | |
4926 | } | |
4927 | return NULL; | |
4928 | } | |
4929 | ||
4930 | /* | |
4931 | * Called from OPEN. Look for clientid in reclaim list. | |
4932 | */ | |
b37ad28b | 4933 | __be32 |
3320fef1 | 4934 | nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
1da177e4 | 4935 | { |
a52d726b JL |
4936 | struct nfs4_client *clp; |
4937 | ||
4938 | /* find clientid in conf_id_hashtbl */ | |
8daae4dc | 4939 | clp = find_confirmed_client(clid, sessions, nn); |
a52d726b JL |
4940 | if (clp == NULL) |
4941 | return nfserr_reclaim_bad; | |
4942 | ||
4943 | return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok; | |
1da177e4 LT |
4944 | } |
4945 | ||
65178db4 BS |
4946 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
4947 | ||
44e34da6 BS |
4948 | u64 nfsd_forget_client(struct nfs4_client *clp, u64 max) |
4949 | { | |
221a6876 BF |
4950 | if (mark_client_expired(clp)) |
4951 | return 0; | |
44e34da6 BS |
4952 | expire_client(clp); |
4953 | return 1; | |
4954 | } | |
4955 | ||
184c1847 BS |
4956 | u64 nfsd_print_client(struct nfs4_client *clp, u64 num) |
4957 | { | |
4958 | char buf[INET6_ADDRSTRLEN]; | |
0a5c33e2 | 4959 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
184c1847 BS |
4960 | printk(KERN_INFO "NFS Client: %s\n", buf); |
4961 | return 1; | |
4962 | } | |
4963 | ||
4964 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, | |
4965 | const char *type) | |
4966 | { | |
4967 | char buf[INET6_ADDRSTRLEN]; | |
0a5c33e2 | 4968 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
184c1847 BS |
4969 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
4970 | } | |
4971 | ||
fc29171f BS |
4972 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *)) |
4973 | { | |
4974 | struct nfs4_openowner *oop; | |
4975 | struct nfs4_lockowner *lop, *lo_next; | |
4976 | struct nfs4_ol_stateid *stp, *st_next; | |
4977 | u64 count = 0; | |
4978 | ||
4979 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { | |
4980 | list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) { | |
4981 | list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) { | |
4982 | if (func) | |
4983 | func(lop); | |
4984 | if (++count == max) | |
4985 | return count; | |
4986 | } | |
4987 | } | |
4988 | } | |
4989 | ||
4990 | return count; | |
4991 | } | |
4992 | ||
4993 | u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max) | |
4994 | { | |
4995 | return nfsd_foreach_client_lock(clp, max, release_lockowner); | |
4996 | } | |
4997 | ||
184c1847 BS |
4998 | u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max) |
4999 | { | |
5000 | u64 count = nfsd_foreach_client_lock(clp, max, NULL); | |
5001 | nfsd_print_count(clp, count, "locked files"); | |
5002 | return count; | |
5003 | } | |
5004 | ||
4dbdbda8 BS |
5005 | static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *)) |
5006 | { | |
5007 | struct nfs4_openowner *oop, *next; | |
5008 | u64 count = 0; | |
5009 | ||
5010 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { | |
5011 | if (func) | |
5012 | func(oop); | |
5013 | if (++count == max) | |
5014 | break; | |
5015 | } | |
5016 | ||
5017 | return count; | |
5018 | } | |
5019 | ||
5020 | u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max) | |
5021 | { | |
5022 | return nfsd_foreach_client_open(clp, max, release_openowner); | |
5023 | } | |
5024 | ||
184c1847 BS |
5025 | u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max) |
5026 | { | |
5027 | u64 count = nfsd_foreach_client_open(clp, max, NULL); | |
5028 | nfsd_print_count(clp, count, "open files"); | |
5029 | return count; | |
5030 | } | |
5031 | ||
269de30f BS |
5032 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
5033 | struct list_head *victims) | |
5034 | { | |
5035 | struct nfs4_delegation *dp, *next; | |
5036 | u64 count = 0; | |
5037 | ||
cdc97505 | 5038 | lockdep_assert_held(&state_lock); |
269de30f BS |
5039 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
5040 | if (victims) | |
5041 | list_move(&dp->dl_recall_lru, victims); | |
5042 | if (++count == max) | |
5043 | break; | |
5044 | } | |
5045 | return count; | |
5046 | } | |
5047 | ||
5048 | u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max) | |
5049 | { | |
5050 | struct nfs4_delegation *dp, *next; | |
5051 | LIST_HEAD(victims); | |
5052 | u64 count; | |
5053 | ||
cdc97505 | 5054 | spin_lock(&state_lock); |
269de30f | 5055 | count = nfsd_find_all_delegations(clp, max, &victims); |
cdc97505 | 5056 | spin_unlock(&state_lock); |
269de30f BS |
5057 | |
5058 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) | |
3bd64a5b | 5059 | revoke_delegation(dp); |
269de30f BS |
5060 | |
5061 | return count; | |
5062 | } | |
5063 | ||
5064 | u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max) | |
5065 | { | |
5066 | struct nfs4_delegation *dp, *next; | |
5067 | LIST_HEAD(victims); | |
5068 | u64 count; | |
5069 | ||
cdc97505 | 5070 | spin_lock(&state_lock); |
269de30f BS |
5071 | count = nfsd_find_all_delegations(clp, max, &victims); |
5072 | list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) | |
5073 | nfsd_break_one_deleg(dp); | |
cdc97505 | 5074 | spin_unlock(&state_lock); |
269de30f BS |
5075 | |
5076 | return count; | |
5077 | } | |
5078 | ||
184c1847 BS |
5079 | u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max) |
5080 | { | |
5081 | u64 count = 0; | |
5082 | ||
cdc97505 | 5083 | spin_lock(&state_lock); |
184c1847 | 5084 | count = nfsd_find_all_delegations(clp, max, NULL); |
cdc97505 | 5085 | spin_unlock(&state_lock); |
184c1847 BS |
5086 | |
5087 | nfsd_print_count(clp, count, "delegations"); | |
5088 | return count; | |
5089 | } | |
5090 | ||
44e34da6 | 5091 | u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64)) |
65178db4 BS |
5092 | { |
5093 | struct nfs4_client *clp, *next; | |
44e34da6 | 5094 | u64 count = 0; |
3320fef1 | 5095 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); |
65178db4 | 5096 | |
44e34da6 BS |
5097 | if (!nfsd_netns_ready(nn)) |
5098 | return 0; | |
5099 | ||
5ed58bb2 | 5100 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
44e34da6 BS |
5101 | count += func(clp, max - count); |
5102 | if ((max != 0) && (count >= max)) | |
65178db4 BS |
5103 | break; |
5104 | } | |
65178db4 | 5105 | |
44e34da6 BS |
5106 | return count; |
5107 | } | |
5108 | ||
6c1e82a4 BS |
5109 | struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
5110 | { | |
5111 | struct nfs4_client *clp; | |
5112 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id); | |
5113 | ||
5114 | if (!nfsd_netns_ready(nn)) | |
5115 | return NULL; | |
5116 | ||
5117 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
5118 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) | |
5119 | return clp; | |
5120 | } | |
5121 | return NULL; | |
5122 | } | |
5123 | ||
65178db4 BS |
5124 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
5125 | ||
c2f1a551 MS |
5126 | /* |
5127 | * Since the lifetime of a delegation isn't limited to that of an open, a | |
5128 | * client may quite reasonably hang on to a delegation as long as it has | |
5129 | * the inode cached. This becomes an obvious problem the first time a | |
5130 | * client's inode cache approaches the size of the server's total memory. | |
5131 | * | |
5132 | * For now we avoid this problem by imposing a hard limit on the number | |
5133 | * of delegations, which varies according to the server's memory size. | |
5134 | */ | |
5135 | static void | |
5136 | set_max_delegations(void) | |
5137 | { | |
5138 | /* | |
5139 | * Allow at most 4 delegations per megabyte of RAM. Quick | |
5140 | * estimates suggest that in the worst case (where every delegation | |
5141 | * is for a different inode), a delegation could take about 1.5K, | |
5142 | * giving a worst case usage of about 6% of memory. | |
5143 | */ | |
5144 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); | |
5145 | } | |
5146 | ||
d85ed443 | 5147 | static int nfs4_state_create_net(struct net *net) |
8daae4dc SK |
5148 | { |
5149 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
5150 | int i; | |
5151 | ||
5152 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * | |
5153 | CLIENT_HASH_SIZE, GFP_KERNEL); | |
5154 | if (!nn->conf_id_hashtbl) | |
382a62e7 | 5155 | goto err; |
0a7ec377 SK |
5156 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
5157 | CLIENT_HASH_SIZE, GFP_KERNEL); | |
5158 | if (!nn->unconf_id_hashtbl) | |
5159 | goto err_unconf_id; | |
9b531137 SK |
5160 | nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * |
5161 | OWNER_HASH_SIZE, GFP_KERNEL); | |
5162 | if (!nn->ownerstr_hashtbl) | |
5163 | goto err_ownerstr; | |
20e9e2bc SK |
5164 | nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) * |
5165 | LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL); | |
5166 | if (!nn->lockowner_ino_hashtbl) | |
5167 | goto err_lockowner_ino; | |
1872de0e SK |
5168 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
5169 | SESSION_HASH_SIZE, GFP_KERNEL); | |
5170 | if (!nn->sessionid_hashtbl) | |
5171 | goto err_sessionid; | |
8daae4dc | 5172 | |
382a62e7 | 5173 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
8daae4dc | 5174 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
0a7ec377 | 5175 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
382a62e7 | 5176 | } |
9b531137 SK |
5177 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
5178 | INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]); | |
20e9e2bc SK |
5179 | for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++) |
5180 | INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]); | |
1872de0e SK |
5181 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
5182 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); | |
382a62e7 | 5183 | nn->conf_name_tree = RB_ROOT; |
a99454aa | 5184 | nn->unconf_name_tree = RB_ROOT; |
5ed58bb2 | 5185 | INIT_LIST_HEAD(&nn->client_lru); |
73758fed | 5186 | INIT_LIST_HEAD(&nn->close_lru); |
e8c69d17 | 5187 | INIT_LIST_HEAD(&nn->del_recall_lru); |
c9a49628 | 5188 | spin_lock_init(&nn->client_lock); |
8daae4dc | 5189 | |
09121281 | 5190 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
d85ed443 | 5191 | get_net(net); |
09121281 | 5192 | |
8daae4dc | 5193 | return 0; |
382a62e7 | 5194 | |
1872de0e SK |
5195 | err_sessionid: |
5196 | kfree(nn->lockowner_ino_hashtbl); | |
20e9e2bc SK |
5197 | err_lockowner_ino: |
5198 | kfree(nn->ownerstr_hashtbl); | |
9b531137 SK |
5199 | err_ownerstr: |
5200 | kfree(nn->unconf_id_hashtbl); | |
0a7ec377 SK |
5201 | err_unconf_id: |
5202 | kfree(nn->conf_id_hashtbl); | |
382a62e7 SK |
5203 | err: |
5204 | return -ENOMEM; | |
8daae4dc SK |
5205 | } |
5206 | ||
5207 | static void | |
4dce0ac9 | 5208 | nfs4_state_destroy_net(struct net *net) |
8daae4dc SK |
5209 | { |
5210 | int i; | |
5211 | struct nfs4_client *clp = NULL; | |
5212 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
5213 | ||
5214 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { | |
5215 | while (!list_empty(&nn->conf_id_hashtbl[i])) { | |
5216 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
5217 | destroy_client(clp); | |
5218 | } | |
5219 | } | |
a99454aa | 5220 | |
2b905635 KM |
5221 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
5222 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { | |
5223 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
5224 | destroy_client(clp); | |
5225 | } | |
a99454aa SK |
5226 | } |
5227 | ||
1872de0e | 5228 | kfree(nn->sessionid_hashtbl); |
20e9e2bc | 5229 | kfree(nn->lockowner_ino_hashtbl); |
9b531137 | 5230 | kfree(nn->ownerstr_hashtbl); |
0a7ec377 | 5231 | kfree(nn->unconf_id_hashtbl); |
8daae4dc | 5232 | kfree(nn->conf_id_hashtbl); |
4dce0ac9 | 5233 | put_net(net); |
8daae4dc SK |
5234 | } |
5235 | ||
f252bc68 | 5236 | int |
d85ed443 | 5237 | nfs4_state_start_net(struct net *net) |
ac4d8ff2 | 5238 | { |
5e1533c7 | 5239 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b5a1a81e BF |
5240 | int ret; |
5241 | ||
d85ed443 | 5242 | ret = nfs4_state_create_net(net); |
8daae4dc SK |
5243 | if (ret) |
5244 | return ret; | |
5e1533c7 | 5245 | nfsd4_client_tracking_init(net); |
2c142baa | 5246 | nn->boot_time = get_seconds(); |
5ccb0066 | 5247 | locks_start_grace(net, &nn->nfsd4_manager); |
a51c84ed | 5248 | nn->grace_ended = false; |
d85ed443 | 5249 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
5284b44e SK |
5250 | nn->nfsd4_grace, net); |
5251 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); | |
d85ed443 SK |
5252 | return 0; |
5253 | } | |
5254 | ||
5255 | /* initialization to perform when the nfsd service is started: */ | |
5256 | ||
5257 | int | |
5258 | nfs4_state_start(void) | |
5259 | { | |
5260 | int ret; | |
5261 | ||
b5a1a81e | 5262 | ret = set_callback_cred(); |
d85ed443 SK |
5263 | if (ret) |
5264 | return -ENOMEM; | |
58da282b | 5265 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
a6d6b781 JL |
5266 | if (laundry_wq == NULL) { |
5267 | ret = -ENOMEM; | |
5268 | goto out_recovery; | |
5269 | } | |
b5a1a81e BF |
5270 | ret = nfsd4_create_callback_queue(); |
5271 | if (ret) | |
5272 | goto out_free_laundry; | |
09121281 | 5273 | |
c2f1a551 | 5274 | set_max_delegations(); |
d85ed443 | 5275 | |
b5a1a81e | 5276 | return 0; |
d85ed443 | 5277 | |
b5a1a81e BF |
5278 | out_free_laundry: |
5279 | destroy_workqueue(laundry_wq); | |
a6d6b781 | 5280 | out_recovery: |
b5a1a81e | 5281 | return ret; |
1da177e4 LT |
5282 | } |
5283 | ||
f252bc68 | 5284 | void |
4dce0ac9 | 5285 | nfs4_state_shutdown_net(struct net *net) |
1da177e4 | 5286 | { |
1da177e4 | 5287 | struct nfs4_delegation *dp = NULL; |
1da177e4 | 5288 | struct list_head *pos, *next, reaplist; |
4dce0ac9 | 5289 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
1da177e4 | 5290 | |
4dce0ac9 SK |
5291 | cancel_delayed_work_sync(&nn->laundromat_work); |
5292 | locks_end_grace(&nn->nfsd4_manager); | |
ac55fdc4 | 5293 | |
e50a26dc | 5294 | nfs4_lock_state(); |
1da177e4 | 5295 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 5296 | spin_lock(&state_lock); |
e8c69d17 | 5297 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 LT |
5298 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
5299 | list_move(&dp->dl_recall_lru, &reaplist); | |
5300 | } | |
cdc97505 | 5301 | spin_unlock(&state_lock); |
1da177e4 LT |
5302 | list_for_each_safe(pos, next, &reaplist) { |
5303 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
3bd64a5b | 5304 | destroy_delegation(dp); |
1da177e4 LT |
5305 | } |
5306 | ||
3320fef1 | 5307 | nfsd4_client_tracking_exit(net); |
4dce0ac9 | 5308 | nfs4_state_destroy_net(net); |
e50a26dc | 5309 | nfs4_unlock_state(); |
1da177e4 LT |
5310 | } |
5311 | ||
5312 | void | |
5313 | nfs4_state_shutdown(void) | |
5314 | { | |
5e8d5c29 | 5315 | destroy_workqueue(laundry_wq); |
c3935e30 | 5316 | nfsd4_destroy_callback_queue(); |
1da177e4 | 5317 | } |
8b70484c TM |
5318 | |
5319 | static void | |
5320 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
5321 | { | |
37c593c5 TM |
5322 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
5323 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); | |
8b70484c TM |
5324 | } |
5325 | ||
5326 | static void | |
5327 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
5328 | { | |
37c593c5 TM |
5329 | if (cstate->minorversion) { |
5330 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); | |
5331 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); | |
5332 | } | |
5333 | } | |
5334 | ||
5335 | void | |
5336 | clear_current_stateid(struct nfsd4_compound_state *cstate) | |
5337 | { | |
5338 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); | |
8b70484c TM |
5339 | } |
5340 | ||
62cd4a59 TM |
5341 | /* |
5342 | * functions to set current state id | |
5343 | */ | |
9428fe1a TM |
5344 | void |
5345 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) | |
5346 | { | |
5347 | put_stateid(cstate, &odp->od_stateid); | |
5348 | } | |
5349 | ||
8b70484c TM |
5350 | void |
5351 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) | |
5352 | { | |
5353 | put_stateid(cstate, &open->op_stateid); | |
5354 | } | |
5355 | ||
62cd4a59 TM |
5356 | void |
5357 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) | |
5358 | { | |
5359 | put_stateid(cstate, &close->cl_stateid); | |
5360 | } | |
5361 | ||
5362 | void | |
5363 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) | |
5364 | { | |
5365 | put_stateid(cstate, &lock->lk_resp_stateid); | |
5366 | } | |
5367 | ||
5368 | /* | |
5369 | * functions to consume current state id | |
5370 | */ | |
1e97b519 | 5371 | |
9428fe1a TM |
5372 | void |
5373 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) | |
5374 | { | |
5375 | get_stateid(cstate, &odp->od_stateid); | |
5376 | } | |
5377 | ||
5378 | void | |
5379 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) | |
5380 | { | |
5381 | get_stateid(cstate, &drp->dr_stateid); | |
5382 | } | |
5383 | ||
1e97b519 TM |
5384 | void |
5385 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) | |
5386 | { | |
5387 | get_stateid(cstate, &fsp->fr_stateid); | |
5388 | } | |
5389 | ||
5390 | void | |
5391 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) | |
5392 | { | |
5393 | get_stateid(cstate, &setattr->sa_stateid); | |
5394 | } | |
5395 | ||
8b70484c TM |
5396 | void |
5397 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) | |
5398 | { | |
5399 | get_stateid(cstate, &close->cl_stateid); | |
5400 | } | |
5401 | ||
5402 | void | |
62cd4a59 | 5403 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
8b70484c | 5404 | { |
62cd4a59 | 5405 | get_stateid(cstate, &locku->lu_stateid); |
8b70484c | 5406 | } |
30813e27 TM |
5407 | |
5408 | void | |
5409 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) | |
5410 | { | |
5411 | get_stateid(cstate, &read->rd_stateid); | |
5412 | } | |
5413 | ||
5414 | void | |
5415 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) | |
5416 | { | |
5417 | get_stateid(cstate, &write->wr_stateid); | |
5418 | } |