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