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