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