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