Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
3 | * All rights reserved. | |
4 | * | |
5 | * Kendrick Smith <kmsmith@umich.edu> | |
6 | * Andy Adamson <kandros@umich.edu> | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * 3. Neither the name of the University nor the names of its | |
18 | * contributors may be used to endorse or promote products derived | |
19 | * from this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
33 | */ | |
34 | ||
aceaf78d | 35 | #include <linux/file.h> |
b89f4321 | 36 | #include <linux/fs.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
0964a3d3 | 38 | #include <linux/namei.h> |
c2f1a551 | 39 | #include <linux/swap.h> |
17456804 | 40 | #include <linux/pagemap.h> |
7df302f7 | 41 | #include <linux/ratelimit.h> |
68e76ad0 | 42 | #include <linux/sunrpc/svcauth_gss.h> |
5976687a | 43 | #include <linux/sunrpc/addr.h> |
87545899 | 44 | #include <linux/jhash.h> |
169319f1 | 45 | #include <linux/string_helpers.h> |
472d155a | 46 | #include <linux/fsnotify.h> |
d47b295e | 47 | #include <linux/rhashtable.h> |
f4e44b39 | 48 | #include <linux/nfs_ssc.h> |
d47b295e | 49 | |
9a74af21 | 50 | #include "xdr4.h" |
06b332a5 | 51 | #include "xdr4cb.h" |
0a3adade | 52 | #include "vfs.h" |
bfa4b365 | 53 | #include "current_stateid.h" |
1da177e4 | 54 | |
5e1533c7 | 55 | #include "netns.h" |
9cf514cc | 56 | #include "pnfs.h" |
fd4f83fd | 57 | #include "filecache.h" |
dd5e3fbc | 58 | #include "trace.h" |
5e1533c7 | 59 | |
1da177e4 LT |
60 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
61 | ||
03a0497f | 62 | #define all_ones {{ ~0, ~0}, ~0} |
f32f3c2d BF |
63 | static const stateid_t one_stateid = { |
64 | .si_generation = ~0, | |
65 | .si_opaque = all_ones, | |
66 | }; | |
67 | static const stateid_t zero_stateid = { | |
68 | /* all fields zero */ | |
69 | }; | |
19ff0f28 TM |
70 | static const stateid_t currentstateid = { |
71 | .si_generation = 1, | |
72 | }; | |
fb500a7c TM |
73 | static const stateid_t close_stateid = { |
74 | .si_generation = 0xffffffffU, | |
75 | }; | |
f32f3c2d | 76 | |
ec6b5d7b | 77 | static u64 current_sessionid = 1; |
fd39ca9a | 78 | |
f32f3c2d BF |
79 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
80 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) | |
19ff0f28 | 81 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
ae254dac | 82 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
1da177e4 | 83 | |
1da177e4 | 84 | /* forward declarations */ |
f9c00c3a | 85 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
6011695d | 86 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
362063a5 | 87 | void nfsd4_end_grace(struct nfsd_net *nn); |
624322f1 | 88 | static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); |
3341678f | 89 | static void nfsd4_file_hash_remove(struct nfs4_file *fi); |
bad4c585 | 90 | static void deleg_reaper(struct nfsd_net *nn); |
1da177e4 | 91 | |
8b671b80 BF |
92 | /* Locking: */ |
93 | ||
8b671b80 BF |
94 | /* |
95 | * Currently used for the del_recall_lru and file hash table. In an | |
96 | * effort to decrease the scope of the client_mutex, this spinlock may | |
97 | * eventually cover more: | |
98 | */ | |
cdc97505 | 99 | static DEFINE_SPINLOCK(state_lock); |
8b671b80 | 100 | |
4f34bd05 AE |
101 | enum nfsd4_st_mutex_lock_subclass { |
102 | OPEN_STATEID_MUTEX = 0, | |
103 | LOCK_STATEID_MUTEX = 1, | |
104 | }; | |
105 | ||
b401be22 JL |
106 | /* |
107 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for | |
108 | * the refcount on the open stateid to drop. | |
109 | */ | |
110 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); | |
111 | ||
89c905be BF |
112 | /* |
113 | * A waitqueue where a writer to clients/#/ctl destroying a client can | |
114 | * wait for cl_rpc_users to drop to 0 and then for the client to be | |
115 | * unhashed. | |
116 | */ | |
117 | static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); | |
118 | ||
9258a2d5 | 119 | static struct kmem_cache *client_slab; |
abf1135b CH |
120 | static struct kmem_cache *openowner_slab; |
121 | static struct kmem_cache *lockowner_slab; | |
122 | static struct kmem_cache *file_slab; | |
123 | static struct kmem_cache *stateid_slab; | |
124 | static struct kmem_cache *deleg_slab; | |
8287f009 | 125 | static struct kmem_cache *odstate_slab; |
e60d4398 | 126 | |
66b2b9b2 | 127 | static void free_session(struct nfsd4_session *); |
508dc6e1 | 128 | |
c4cb8974 | 129 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
76d348fa | 130 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
c5967721 | 131 | static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops; |
0162ac2b | 132 | |
66af2579 DN |
133 | static struct workqueue_struct *laundry_wq; |
134 | ||
d76cc46b DN |
135 | int nfsd4_create_laundry_wq(void) |
136 | { | |
137 | int rc = 0; | |
138 | ||
139 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); | |
140 | if (laundry_wq == NULL) | |
141 | rc = -ENOMEM; | |
142 | return rc; | |
143 | } | |
144 | ||
145 | void nfsd4_destroy_laundry_wq(void) | |
146 | { | |
147 | destroy_workqueue(laundry_wq); | |
148 | } | |
149 | ||
f0f51f5c | 150 | static bool is_session_dead(struct nfsd4_session *ses) |
66b2b9b2 | 151 | { |
f0f51f5c | 152 | return ses->se_flags & NFS4_SESSION_DEAD; |
66b2b9b2 BF |
153 | } |
154 | ||
f0f51f5c | 155 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
508dc6e1 | 156 | { |
f0f51f5c | 157 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
66b2b9b2 BF |
158 | return nfserr_jukebox; |
159 | ses->se_flags |= NFS4_SESSION_DEAD; | |
160 | return nfs_ok; | |
508dc6e1 BH |
161 | } |
162 | ||
221a6876 BF |
163 | static bool is_client_expired(struct nfs4_client *clp) |
164 | { | |
165 | return clp->cl_time == 0; | |
166 | } | |
167 | ||
3a4ea23d DN |
168 | static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn, |
169 | struct nfs4_client *clp) | |
170 | { | |
171 | if (clp->cl_state != NFSD4_ACTIVE) | |
172 | atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0); | |
173 | } | |
174 | ||
221a6876 BF |
175 | static __be32 get_client_locked(struct nfs4_client *clp) |
176 | { | |
0a880a28 TM |
177 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
178 | ||
179 | lockdep_assert_held(&nn->client_lock); | |
180 | ||
221a6876 BF |
181 | if (is_client_expired(clp)) |
182 | return nfserr_expired; | |
14ed14cc | 183 | atomic_inc(&clp->cl_rpc_users); |
3a4ea23d | 184 | nfsd4_dec_courtesy_client_count(nn, clp); |
66af2579 | 185 | clp->cl_state = NFSD4_ACTIVE; |
221a6876 BF |
186 | return nfs_ok; |
187 | } | |
188 | ||
189 | /* must be called under the client_lock */ | |
190 | static inline void | |
191 | renew_client_locked(struct nfs4_client *clp) | |
192 | { | |
193 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
194 | ||
195 | if (is_client_expired(clp)) { | |
196 | WARN_ON(1); | |
197 | printk("%s: client (clientid %08x/%08x) already expired\n", | |
198 | __func__, | |
199 | clp->cl_clientid.cl_boot, | |
200 | clp->cl_clientid.cl_id); | |
201 | return; | |
202 | } | |
203 | ||
221a6876 | 204 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
20b7d86f | 205 | clp->cl_time = ktime_get_boottime_seconds(); |
3a4ea23d | 206 | nfsd4_dec_courtesy_client_count(nn, clp); |
66af2579 | 207 | clp->cl_state = NFSD4_ACTIVE; |
221a6876 BF |
208 | } |
209 | ||
ba138435 | 210 | static void put_client_renew_locked(struct nfs4_client *clp) |
221a6876 | 211 | { |
0a880a28 TM |
212 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
213 | ||
214 | lockdep_assert_held(&nn->client_lock); | |
215 | ||
14ed14cc | 216 | if (!atomic_dec_and_test(&clp->cl_rpc_users)) |
221a6876 BF |
217 | return; |
218 | if (!is_client_expired(clp)) | |
219 | renew_client_locked(clp); | |
89c905be BF |
220 | else |
221 | wake_up_all(&expiry_wq); | |
221a6876 BF |
222 | } |
223 | ||
4b24ca7d JL |
224 | static void put_client_renew(struct nfs4_client *clp) |
225 | { | |
226 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
227 | ||
14ed14cc | 228 | if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) |
d6c249b4 JL |
229 | return; |
230 | if (!is_client_expired(clp)) | |
231 | renew_client_locked(clp); | |
89c905be BF |
232 | else |
233 | wake_up_all(&expiry_wq); | |
4b24ca7d JL |
234 | spin_unlock(&nn->client_lock); |
235 | } | |
236 | ||
d4e19e70 TM |
237 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
238 | { | |
239 | __be32 status; | |
240 | ||
241 | if (is_session_dead(ses)) | |
242 | return nfserr_badsession; | |
243 | status = get_client_locked(ses->se_client); | |
244 | if (status) | |
245 | return status; | |
246 | atomic_inc(&ses->se_ref); | |
247 | return nfs_ok; | |
248 | } | |
249 | ||
250 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) | |
251 | { | |
252 | struct nfs4_client *clp = ses->se_client; | |
0a880a28 TM |
253 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
254 | ||
255 | lockdep_assert_held(&nn->client_lock); | |
d4e19e70 TM |
256 | |
257 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) | |
258 | free_session(ses); | |
259 | put_client_renew_locked(clp); | |
260 | } | |
261 | ||
262 | static void nfsd4_put_session(struct nfsd4_session *ses) | |
263 | { | |
264 | struct nfs4_client *clp = ses->se_client; | |
265 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
266 | ||
267 | spin_lock(&nn->client_lock); | |
268 | nfsd4_put_session_locked(ses); | |
269 | spin_unlock(&nn->client_lock); | |
270 | } | |
271 | ||
76d348fa JL |
272 | static struct nfsd4_blocked_lock * |
273 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, | |
274 | struct nfsd_net *nn) | |
275 | { | |
276 | struct nfsd4_blocked_lock *cur, *found = NULL; | |
277 | ||
0cc11a61 | 278 | spin_lock(&nn->blocked_locks_lock); |
76d348fa JL |
279 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
280 | if (fh_match(fh, &cur->nbl_fh)) { | |
281 | list_del_init(&cur->nbl_list); | |
47446d74 | 282 | WARN_ON(list_empty(&cur->nbl_lru)); |
7919d0a2 | 283 | list_del_init(&cur->nbl_lru); |
76d348fa JL |
284 | found = cur; |
285 | break; | |
286 | } | |
287 | } | |
0cc11a61 | 288 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa | 289 | if (found) |
cb03f94f | 290 | locks_delete_block(&found->nbl_lock); |
76d348fa JL |
291 | return found; |
292 | } | |
293 | ||
294 | static struct nfsd4_blocked_lock * | |
295 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, | |
296 | struct nfsd_net *nn) | |
297 | { | |
298 | struct nfsd4_blocked_lock *nbl; | |
299 | ||
300 | nbl = find_blocked_lock(lo, fh, nn); | |
301 | if (!nbl) { | |
03a0497f | 302 | nbl = kmalloc(sizeof(*nbl), GFP_KERNEL); |
76d348fa | 303 | if (nbl) { |
e1e8399e VA |
304 | INIT_LIST_HEAD(&nbl->nbl_list); |
305 | INIT_LIST_HEAD(&nbl->nbl_lru); | |
76d348fa JL |
306 | fh_copy_shallow(&nbl->nbl_fh, fh); |
307 | locks_init_lock(&nbl->nbl_lock); | |
47446d74 | 308 | kref_init(&nbl->nbl_kref); |
76d348fa JL |
309 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
310 | &nfsd4_cb_notify_lock_ops, | |
311 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); | |
312 | } | |
313 | } | |
314 | return nbl; | |
315 | } | |
316 | ||
47446d74 VA |
317 | static void |
318 | free_nbl(struct kref *kref) | |
319 | { | |
320 | struct nfsd4_blocked_lock *nbl; | |
321 | ||
322 | nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); | |
05eda6e7 | 323 | locks_release_private(&nbl->nbl_lock); |
47446d74 VA |
324 | kfree(nbl); |
325 | } | |
326 | ||
76d348fa JL |
327 | static void |
328 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) | |
329 | { | |
6aaafc43 | 330 | locks_delete_block(&nbl->nbl_lock); |
47446d74 | 331 | kref_put(&nbl->nbl_kref, free_nbl); |
76d348fa JL |
332 | } |
333 | ||
68ef3bc3 JL |
334 | static void |
335 | remove_blocked_locks(struct nfs4_lockowner *lo) | |
336 | { | |
337 | struct nfs4_client *clp = lo->lo_owner.so_client; | |
338 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
339 | struct nfsd4_blocked_lock *nbl; | |
340 | LIST_HEAD(reaplist); | |
341 | ||
342 | /* Dequeue all blocked locks */ | |
343 | spin_lock(&nn->blocked_locks_lock); | |
344 | while (!list_empty(&lo->lo_blocked)) { | |
345 | nbl = list_first_entry(&lo->lo_blocked, | |
346 | struct nfsd4_blocked_lock, | |
347 | nbl_list); | |
348 | list_del_init(&nbl->nbl_list); | |
47446d74 | 349 | WARN_ON(list_empty(&nbl->nbl_lru)); |
68ef3bc3 JL |
350 | list_move(&nbl->nbl_lru, &reaplist); |
351 | } | |
352 | spin_unlock(&nn->blocked_locks_lock); | |
353 | ||
354 | /* Now free them */ | |
355 | while (!list_empty(&reaplist)) { | |
356 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, | |
357 | nbl_lru); | |
358 | list_del_init(&nbl->nbl_lru); | |
68ef3bc3 JL |
359 | free_blocked_lock(nbl); |
360 | } | |
361 | } | |
362 | ||
f456458e JL |
363 | static void |
364 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) | |
365 | { | |
366 | struct nfsd4_blocked_lock *nbl = container_of(cb, | |
367 | struct nfsd4_blocked_lock, nbl_cb); | |
368 | locks_delete_block(&nbl->nbl_lock); | |
369 | } | |
370 | ||
76d348fa JL |
371 | static int |
372 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) | |
373 | { | |
1035d654 CL |
374 | trace_nfsd_cb_notify_lock_done(&zero_stateid, task); |
375 | ||
76d348fa JL |
376 | /* |
377 | * Since this is just an optimization, we don't try very hard if it | |
378 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and | |
379 | * just quit trying on anything else. | |
380 | */ | |
381 | switch (task->tk_status) { | |
382 | case -NFS4ERR_DELAY: | |
383 | rpc_delay(task, 1 * HZ); | |
384 | return 0; | |
385 | default: | |
386 | return 1; | |
387 | } | |
388 | } | |
389 | ||
390 | static void | |
391 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) | |
392 | { | |
393 | struct nfsd4_blocked_lock *nbl = container_of(cb, | |
394 | struct nfsd4_blocked_lock, nbl_cb); | |
395 | ||
396 | free_blocked_lock(nbl); | |
397 | } | |
398 | ||
399 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { | |
f456458e | 400 | .prepare = nfsd4_cb_notify_lock_prepare, |
76d348fa JL |
401 | .done = nfsd4_cb_notify_lock_done, |
402 | .release = nfsd4_cb_notify_lock_release, | |
403 | }; | |
404 | ||
ebd9d2c2 BF |
405 | /* |
406 | * We store the NONE, READ, WRITE, and BOTH bits separately in the | |
407 | * st_{access,deny}_bmap field of the stateid, in order to track not | |
408 | * only what share bits are currently in force, but also what | |
409 | * combinations of share bits previous opens have used. This allows us | |
3dcd1d8a BF |
410 | * to enforce the recommendation in |
411 | * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that | |
412 | * the server return an error if the client attempt to downgrade to a | |
413 | * combination of share bits not explicable by closing some of its | |
414 | * previous opens. | |
ebd9d2c2 | 415 | * |
3dcd1d8a | 416 | * This enforcement is arguably incomplete, since we don't keep |
ebd9d2c2 BF |
417 | * track of access/deny bit combinations; so, e.g., we allow: |
418 | * | |
419 | * OPEN allow read, deny write | |
420 | * OPEN allow both, deny none | |
421 | * DOWNGRADE allow read, deny none | |
422 | * | |
423 | * which we should reject. | |
3dcd1d8a BF |
424 | * |
425 | * But you could also argue that our current code is already overkill, | |
426 | * since it only exists to return NFS4ERR_INVAL on incorrect client | |
427 | * behavior. | |
ebd9d2c2 BF |
428 | */ |
429 | static unsigned int | |
430 | bmap_to_share_mode(unsigned long bmap) | |
431 | { | |
432 | int i; | |
433 | unsigned int access = 0; | |
434 | ||
435 | for (i = 1; i < 4; i++) { | |
436 | if (test_bit(i, &bmap)) | |
437 | access |= i; | |
438 | } | |
439 | return access; | |
440 | } | |
441 | ||
442 | /* set share access for a given stateid */ | |
443 | static inline void | |
444 | set_access(u32 access, struct nfs4_ol_stateid *stp) | |
445 | { | |
446 | unsigned char mask = 1 << access; | |
447 | ||
448 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
449 | stp->st_access_bmap |= mask; | |
450 | } | |
451 | ||
452 | /* clear share access for a given stateid */ | |
453 | static inline void | |
454 | clear_access(u32 access, struct nfs4_ol_stateid *stp) | |
455 | { | |
456 | unsigned char mask = 1 << access; | |
457 | ||
458 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
459 | stp->st_access_bmap &= ~mask; | |
460 | } | |
461 | ||
462 | /* test whether a given stateid has access */ | |
463 | static inline bool | |
464 | test_access(u32 access, struct nfs4_ol_stateid *stp) | |
465 | { | |
466 | unsigned char mask = 1 << access; | |
467 | ||
468 | return (bool)(stp->st_access_bmap & mask); | |
469 | } | |
470 | ||
471 | /* set share deny for a given stateid */ | |
472 | static inline void | |
473 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
474 | { | |
475 | unsigned char mask = 1 << deny; | |
476 | ||
477 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
478 | stp->st_deny_bmap |= mask; | |
479 | } | |
480 | ||
481 | /* clear share deny for a given stateid */ | |
482 | static inline void | |
483 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
484 | { | |
485 | unsigned char mask = 1 << deny; | |
486 | ||
487 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
488 | stp->st_deny_bmap &= ~mask; | |
489 | } | |
490 | ||
491 | /* test whether a given stateid is denying specific access */ | |
492 | static inline bool | |
493 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
494 | { | |
495 | unsigned char mask = 1 << deny; | |
496 | ||
497 | return (bool)(stp->st_deny_bmap & mask); | |
498 | } | |
499 | ||
500 | static int nfs4_access_to_omode(u32 access) | |
501 | { | |
502 | switch (access & NFS4_SHARE_ACCESS_BOTH) { | |
503 | case NFS4_SHARE_ACCESS_READ: | |
504 | return O_RDONLY; | |
505 | case NFS4_SHARE_ACCESS_WRITE: | |
506 | return O_WRONLY; | |
507 | case NFS4_SHARE_ACCESS_BOTH: | |
508 | return O_RDWR; | |
509 | } | |
510 | WARN_ON_ONCE(1); | |
511 | return O_RDONLY; | |
512 | } | |
513 | ||
514 | static inline int | |
515 | access_permit_read(struct nfs4_ol_stateid *stp) | |
516 | { | |
517 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || | |
518 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || | |
519 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); | |
520 | } | |
521 | ||
522 | static inline int | |
523 | access_permit_write(struct nfs4_ol_stateid *stp) | |
524 | { | |
525 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || | |
526 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); | |
527 | } | |
528 | ||
b5971afa KM |
529 | static inline struct nfs4_stateowner * |
530 | nfs4_get_stateowner(struct nfs4_stateowner *sop) | |
531 | { | |
532 | atomic_inc(&sop->so_count); | |
533 | return sop; | |
534 | } | |
535 | ||
7ffb5880 | 536 | static int |
d4f0489f | 537 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
7ffb5880 TM |
538 | { |
539 | return (sop->so_owner.len == owner->len) && | |
d4f0489f | 540 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
7ffb5880 TM |
541 | } |
542 | ||
543 | static struct nfs4_openowner * | |
23df1778 | 544 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
d4f0489f | 545 | struct nfs4_client *clp) |
7ffb5880 TM |
546 | { |
547 | struct nfs4_stateowner *so; | |
7ffb5880 | 548 | |
d4f0489f | 549 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 550 | |
d4f0489f TM |
551 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
552 | so_strhash) { | |
7ffb5880 TM |
553 | if (!so->so_is_open_owner) |
554 | continue; | |
b5971afa KM |
555 | if (same_owner_str(so, &open->op_owner)) |
556 | return openowner(nfs4_get_stateowner(so)); | |
7ffb5880 TM |
557 | } |
558 | return NULL; | |
559 | } | |
560 | ||
1da177e4 LT |
561 | static inline u32 |
562 | opaque_hashval(const void *ptr, int nbytes) | |
563 | { | |
564 | unsigned char *cptr = (unsigned char *) ptr; | |
565 | ||
566 | u32 x = 0; | |
567 | while (nbytes--) { | |
568 | x *= 37; | |
569 | x += *cptr++; | |
570 | } | |
571 | return x; | |
572 | } | |
573 | ||
5b095e99 | 574 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
32513b40 | 575 | { |
5b095e99 JL |
576 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
577 | ||
578 | kmem_cache_free(file_slab, fp); | |
32513b40 BF |
579 | } |
580 | ||
e6ba76e1 | 581 | void |
13cd2184 N |
582 | put_nfs4_file(struct nfs4_file *fi) |
583 | { | |
d47b295e | 584 | if (refcount_dec_and_test(&fi->fi_ref)) { |
3341678f | 585 | nfsd4_file_hash_remove(fi); |
8287f009 | 586 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
5b095e99 JL |
587 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
588 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); | |
8b671b80 | 589 | } |
13cd2184 N |
590 | } |
591 | ||
eb82dd39 | 592 | static struct nfsd_file * |
de18643d TM |
593 | find_writeable_file_locked(struct nfs4_file *f) |
594 | { | |
eb82dd39 | 595 | struct nfsd_file *ret; |
de18643d TM |
596 | |
597 | lockdep_assert_held(&f->fi_lock); | |
598 | ||
edd2f552 | 599 | ret = nfsd_file_get(f->fi_fds[O_WRONLY]); |
de18643d | 600 | if (!ret) |
edd2f552 | 601 | ret = nfsd_file_get(f->fi_fds[O_RDWR]); |
de18643d TM |
602 | return ret; |
603 | } | |
604 | ||
eb82dd39 | 605 | static struct nfsd_file * |
de18643d TM |
606 | find_writeable_file(struct nfs4_file *f) |
607 | { | |
eb82dd39 | 608 | struct nfsd_file *ret; |
de18643d TM |
609 | |
610 | spin_lock(&f->fi_lock); | |
611 | ret = find_writeable_file_locked(f); | |
612 | spin_unlock(&f->fi_lock); | |
613 | ||
614 | return ret; | |
615 | } | |
616 | ||
eb82dd39 JL |
617 | static struct nfsd_file * |
618 | find_readable_file_locked(struct nfs4_file *f) | |
de18643d | 619 | { |
eb82dd39 | 620 | struct nfsd_file *ret; |
de18643d TM |
621 | |
622 | lockdep_assert_held(&f->fi_lock); | |
623 | ||
edd2f552 | 624 | ret = nfsd_file_get(f->fi_fds[O_RDONLY]); |
de18643d | 625 | if (!ret) |
edd2f552 | 626 | ret = nfsd_file_get(f->fi_fds[O_RDWR]); |
de18643d TM |
627 | return ret; |
628 | } | |
629 | ||
eb82dd39 | 630 | static struct nfsd_file * |
de18643d TM |
631 | find_readable_file(struct nfs4_file *f) |
632 | { | |
eb82dd39 | 633 | struct nfsd_file *ret; |
de18643d TM |
634 | |
635 | spin_lock(&f->fi_lock); | |
636 | ret = find_readable_file_locked(f); | |
637 | spin_unlock(&f->fi_lock); | |
638 | ||
639 | return ret; | |
640 | } | |
641 | ||
1d3dd1d5 DN |
642 | static struct nfsd_file * |
643 | find_rw_file(struct nfs4_file *f) | |
644 | { | |
645 | struct nfsd_file *ret; | |
646 | ||
647 | spin_lock(&f->fi_lock); | |
648 | ret = nfsd_file_get(f->fi_fds[O_RDWR]); | |
649 | spin_unlock(&f->fi_lock); | |
650 | ||
651 | return ret; | |
652 | } | |
653 | ||
eb82dd39 | 654 | struct nfsd_file * |
de18643d TM |
655 | find_any_file(struct nfs4_file *f) |
656 | { | |
eb82dd39 | 657 | struct nfsd_file *ret; |
de18643d | 658 | |
a451b123 TM |
659 | if (!f) |
660 | return NULL; | |
de18643d | 661 | spin_lock(&f->fi_lock); |
edd2f552 | 662 | ret = nfsd_file_get(f->fi_fds[O_RDWR]); |
de18643d | 663 | if (!ret) { |
edd2f552 | 664 | ret = nfsd_file_get(f->fi_fds[O_WRONLY]); |
de18643d | 665 | if (!ret) |
edd2f552 | 666 | ret = nfsd_file_get(f->fi_fds[O_RDONLY]); |
de18643d TM |
667 | } |
668 | spin_unlock(&f->fi_lock); | |
669 | return ret; | |
670 | } | |
671 | ||
e0aa6510 | 672 | static struct nfsd_file *find_any_file_locked(struct nfs4_file *f) |
9affa435 | 673 | { |
e0aa6510 JL |
674 | lockdep_assert_held(&f->fi_lock); |
675 | ||
676 | if (f->fi_fds[O_RDWR]) | |
677 | return f->fi_fds[O_RDWR]; | |
678 | if (f->fi_fds[O_WRONLY]) | |
679 | return f->fi_fds[O_WRONLY]; | |
680 | if (f->fi_fds[O_RDONLY]) | |
681 | return f->fi_fds[O_RDONLY]; | |
682 | return NULL; | |
683 | } | |
684 | ||
02a3508d | 685 | static atomic_long_t num_delegations; |
697ce9be | 686 | unsigned long max_delegations; |
ef0f3390 N |
687 | |
688 | /* | |
689 | * Open owner state (share locks) | |
690 | */ | |
691 | ||
16bfdaaf BF |
692 | /* hash tables for lock and open owners */ |
693 | #define OWNER_HASH_BITS 8 | |
694 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) | |
695 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) | |
ef0f3390 | 696 | |
d4f0489f | 697 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
ddc04c41 BF |
698 | { |
699 | unsigned int ret; | |
700 | ||
701 | ret = opaque_hashval(ownername->data, ownername->len); | |
16bfdaaf | 702 | return ret & OWNER_HASH_MASK; |
ddc04c41 | 703 | } |
ef0f3390 | 704 | |
d47b295e | 705 | static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp; |
ca943217 | 706 | |
d47b295e CL |
707 | static const struct rhashtable_params nfs4_file_rhash_params = { |
708 | .key_len = sizeof_field(struct nfs4_file, fi_inode), | |
709 | .key_offset = offsetof(struct nfs4_file, fi_inode), | |
710 | .head_offset = offsetof(struct nfs4_file, fi_rlist), | |
ca943217 | 711 | |
d47b295e CL |
712 | /* |
713 | * Start with a single page hash table to reduce resizing churn | |
714 | * on light workloads. | |
715 | */ | |
716 | .min_size = 256, | |
717 | .automatic_shrinking = true, | |
718 | }; | |
ef0f3390 | 719 | |
3d694271 DN |
720 | /* |
721 | * Check if courtesy clients have conflicting access and resolve it if possible | |
722 | * | |
723 | * access: is op_share_access if share_access is true. | |
724 | * Check if access mode, op_share_access, would conflict with | |
725 | * the current deny mode of the file 'fp'. | |
726 | * access: is op_share_deny if share_access is false. | |
727 | * Check if the deny mode, op_share_deny, would conflict with | |
728 | * current access of the file 'fp'. | |
729 | * stp: skip checking this entry. | |
730 | * new_stp: normal open, not open upgrade. | |
731 | * | |
732 | * Function returns: | |
733 | * false - access/deny mode conflict with normal client. | |
734 | * true - no conflict or conflict with courtesy client(s) is resolved. | |
735 | */ | |
736 | static bool | |
737 | nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp, | |
738 | struct nfs4_ol_stateid *stp, u32 access, bool share_access) | |
739 | { | |
740 | struct nfs4_ol_stateid *st; | |
741 | bool resolvable = true; | |
742 | unsigned char bmap; | |
743 | struct nfsd_net *nn; | |
744 | struct nfs4_client *clp; | |
745 | ||
746 | lockdep_assert_held(&fp->fi_lock); | |
747 | list_for_each_entry(st, &fp->fi_stateids, st_perfile) { | |
748 | /* ignore lock stateid */ | |
749 | if (st->st_openstp) | |
750 | continue; | |
751 | if (st == stp && new_stp) | |
752 | continue; | |
753 | /* check file access against deny mode or vice versa */ | |
754 | bmap = share_access ? st->st_deny_bmap : st->st_access_bmap; | |
755 | if (!(access & bmap_to_share_mode(bmap))) | |
756 | continue; | |
757 | clp = st->st_stid.sc_client; | |
758 | if (try_to_expire_client(clp)) | |
759 | continue; | |
760 | resolvable = false; | |
761 | break; | |
762 | } | |
763 | if (resolvable) { | |
764 | clp = stp->st_stid.sc_client; | |
765 | nn = net_generic(clp->net, nfsd_net_id); | |
766 | mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); | |
767 | } | |
768 | return resolvable; | |
769 | } | |
770 | ||
12659651 JL |
771 | static void |
772 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
3477565e | 773 | { |
7214e860 JL |
774 | lockdep_assert_held(&fp->fi_lock); |
775 | ||
12659651 JL |
776 | if (access & NFS4_SHARE_ACCESS_WRITE) |
777 | atomic_inc(&fp->fi_access[O_WRONLY]); | |
778 | if (access & NFS4_SHARE_ACCESS_READ) | |
779 | atomic_inc(&fp->fi_access[O_RDONLY]); | |
3477565e BF |
780 | } |
781 | ||
12659651 JL |
782 | static __be32 |
783 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
998db52c | 784 | { |
7214e860 JL |
785 | lockdep_assert_held(&fp->fi_lock); |
786 | ||
12659651 JL |
787 | /* Does this access mode make sense? */ |
788 | if (access & ~NFS4_SHARE_ACCESS_BOTH) | |
789 | return nfserr_inval; | |
790 | ||
baeb4ff0 JL |
791 | /* Does it conflict with a deny mode already set? */ |
792 | if ((access & fp->fi_share_deny) != 0) | |
793 | return nfserr_share_denied; | |
794 | ||
12659651 JL |
795 | __nfs4_file_get_access(fp, access); |
796 | return nfs_ok; | |
998db52c BF |
797 | } |
798 | ||
baeb4ff0 JL |
799 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
800 | { | |
801 | /* Common case is that there is no deny mode. */ | |
802 | if (deny) { | |
803 | /* Does this deny mode make sense? */ | |
804 | if (deny & ~NFS4_SHARE_DENY_BOTH) | |
805 | return nfserr_inval; | |
806 | ||
807 | if ((deny & NFS4_SHARE_DENY_READ) && | |
808 | atomic_read(&fp->fi_access[O_RDONLY])) | |
809 | return nfserr_share_denied; | |
810 | ||
811 | if ((deny & NFS4_SHARE_DENY_WRITE) && | |
812 | atomic_read(&fp->fi_access[O_WRONLY])) | |
813 | return nfserr_share_denied; | |
814 | } | |
815 | return nfs_ok; | |
816 | } | |
817 | ||
998db52c | 818 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
f9d7562f | 819 | { |
de18643d TM |
820 | might_lock(&fp->fi_lock); |
821 | ||
822 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { | |
fd4f83fd JL |
823 | struct nfsd_file *f1 = NULL; |
824 | struct nfsd_file *f2 = NULL; | |
de18643d | 825 | |
6d338b51 | 826 | swap(f1, fp->fi_fds[oflag]); |
0c7c3e67 | 827 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
6d338b51 | 828 | swap(f2, fp->fi_fds[O_RDWR]); |
de18643d TM |
829 | spin_unlock(&fp->fi_lock); |
830 | if (f1) | |
dcf3f809 | 831 | nfsd_file_put(f1); |
de18643d | 832 | if (f2) |
dcf3f809 | 833 | nfsd_file_put(f2); |
f9d7562f BF |
834 | } |
835 | } | |
836 | ||
12659651 | 837 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
998db52c | 838 | { |
12659651 JL |
839 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
840 | ||
841 | if (access & NFS4_SHARE_ACCESS_WRITE) | |
998db52c | 842 | __nfs4_file_put_access(fp, O_WRONLY); |
12659651 JL |
843 | if (access & NFS4_SHARE_ACCESS_READ) |
844 | __nfs4_file_put_access(fp, O_RDONLY); | |
998db52c BF |
845 | } |
846 | ||
8287f009 SB |
847 | /* |
848 | * Allocate a new open/delegation state counter. This is needed for | |
849 | * pNFS for proper return on close semantics. | |
850 | * | |
851 | * Note that we only allocate it for pNFS-enabled exports, otherwise | |
852 | * all pointers to struct nfs4_clnt_odstate are always NULL. | |
853 | */ | |
854 | static struct nfs4_clnt_odstate * | |
855 | alloc_clnt_odstate(struct nfs4_client *clp) | |
856 | { | |
857 | struct nfs4_clnt_odstate *co; | |
858 | ||
859 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); | |
860 | if (co) { | |
861 | co->co_client = clp; | |
cff7cb2e | 862 | refcount_set(&co->co_odcount, 1); |
8287f009 SB |
863 | } |
864 | return co; | |
865 | } | |
866 | ||
867 | static void | |
868 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) | |
869 | { | |
870 | struct nfs4_file *fp = co->co_file; | |
871 | ||
872 | lockdep_assert_held(&fp->fi_lock); | |
873 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); | |
874 | } | |
875 | ||
876 | static inline void | |
877 | get_clnt_odstate(struct nfs4_clnt_odstate *co) | |
878 | { | |
879 | if (co) | |
cff7cb2e | 880 | refcount_inc(&co->co_odcount); |
8287f009 SB |
881 | } |
882 | ||
883 | static void | |
884 | put_clnt_odstate(struct nfs4_clnt_odstate *co) | |
885 | { | |
886 | struct nfs4_file *fp; | |
887 | ||
888 | if (!co) | |
889 | return; | |
890 | ||
891 | fp = co->co_file; | |
cff7cb2e | 892 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
8287f009 SB |
893 | list_del(&co->co_perfile); |
894 | spin_unlock(&fp->fi_lock); | |
895 | ||
896 | nfsd4_return_all_file_layouts(co->co_client, fp); | |
897 | kmem_cache_free(odstate_slab, co); | |
898 | } | |
899 | } | |
900 | ||
901 | static struct nfs4_clnt_odstate * | |
902 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) | |
903 | { | |
904 | struct nfs4_clnt_odstate *co; | |
905 | struct nfs4_client *cl; | |
906 | ||
907 | if (!new) | |
908 | return NULL; | |
909 | ||
910 | cl = new->co_client; | |
911 | ||
912 | spin_lock(&fp->fi_lock); | |
913 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { | |
914 | if (co->co_client == cl) { | |
915 | get_clnt_odstate(co); | |
916 | goto out; | |
917 | } | |
918 | } | |
919 | co = new; | |
920 | co->co_file = fp; | |
921 | hash_clnt_odstate_locked(new); | |
922 | out: | |
923 | spin_unlock(&fp->fi_lock); | |
924 | return co; | |
925 | } | |
926 | ||
d19fb70d KM |
927 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
928 | void (*sc_free)(struct nfs4_stid *)) | |
2a74aba7 | 929 | { |
3abdb607 | 930 | struct nfs4_stid *stid; |
6136d2b4 | 931 | int new_id; |
2a74aba7 | 932 | |
f8338834 | 933 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
3abdb607 BF |
934 | if (!stid) |
935 | return NULL; | |
936 | ||
4770d722 JL |
937 | idr_preload(GFP_KERNEL); |
938 | spin_lock(&cl->cl_lock); | |
78599c42 BF |
939 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
940 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); | |
4770d722 JL |
941 | spin_unlock(&cl->cl_lock); |
942 | idr_preload_end(); | |
ebd6c707 | 943 | if (new_id < 0) |
3abdb607 | 944 | goto out_free; |
d19fb70d KM |
945 | |
946 | stid->sc_free = sc_free; | |
2a74aba7 | 947 | stid->sc_client = cl; |
3abdb607 BF |
948 | stid->sc_stateid.si_opaque.so_id = new_id; |
949 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; | |
2a74aba7 | 950 | /* Will be incremented before return to client: */ |
a15dfcd5 | 951 | refcount_set(&stid->sc_count, 1); |
9767feb2 | 952 | spin_lock_init(&stid->sc_lock); |
624322f1 | 953 | INIT_LIST_HEAD(&stid->sc_cp_list); |
996e0938 | 954 | |
996e0938 | 955 | /* |
3abdb607 BF |
956 | * It shouldn't be a problem to reuse an opaque stateid value. |
957 | * I don't think it is for 4.1. But with 4.0 I worry that, for | |
958 | * example, a stray write retransmission could be accepted by | |
959 | * the server when it should have been rejected. Therefore, | |
960 | * adopt a trick from the sctp code to attempt to maximize the | |
961 | * amount of time until an id is reused, by ensuring they always | |
962 | * "increase" (mod INT_MAX): | |
996e0938 | 963 | */ |
3abdb607 BF |
964 | return stid; |
965 | out_free: | |
2c44a234 | 966 | kmem_cache_free(slab, stid); |
3abdb607 | 967 | return NULL; |
2a74aba7 BF |
968 | } |
969 | ||
e0639dc5 OK |
970 | /* |
971 | * Create a unique stateid_t to represent each COPY. | |
972 | */ | |
624322f1 | 973 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
781fde1a | 974 | unsigned char cs_type) |
e0639dc5 OK |
975 | { |
976 | int new_id; | |
977 | ||
781fde1a CL |
978 | stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
979 | stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; | |
624322f1 | 980 | |
e0639dc5 OK |
981 | idr_preload(GFP_KERNEL); |
982 | spin_lock(&nn->s2s_cp_lock); | |
624322f1 | 983 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
781fde1a CL |
984 | stid->cs_stid.si_opaque.so_id = new_id; |
985 | stid->cs_stid.si_generation = 1; | |
e0639dc5 OK |
986 | spin_unlock(&nn->s2s_cp_lock); |
987 | idr_preload_end(); | |
988 | if (new_id < 0) | |
989 | return 0; | |
81e72297 | 990 | stid->cs_type = cs_type; |
e0639dc5 OK |
991 | return 1; |
992 | } | |
993 | ||
624322f1 OK |
994 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
995 | { | |
996 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); | |
997 | } | |
998 | ||
999 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, | |
1000 | struct nfs4_stid *p_stid) | |
1001 | { | |
1002 | struct nfs4_cpntf_state *cps; | |
1003 | ||
1004 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); | |
1005 | if (!cps) | |
1006 | return NULL; | |
20b7d86f | 1007 | cps->cpntf_time = ktime_get_boottime_seconds(); |
781fde1a | 1008 | refcount_set(&cps->cp_stateid.cs_count, 1); |
624322f1 OK |
1009 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
1010 | goto out_free; | |
1011 | spin_lock(&nn->s2s_cp_lock); | |
1012 | list_add(&cps->cp_list, &p_stid->sc_cp_list); | |
1013 | spin_unlock(&nn->s2s_cp_lock); | |
1014 | return cps; | |
1015 | out_free: | |
1016 | kfree(cps); | |
1017 | return NULL; | |
1018 | } | |
1019 | ||
1020 | void nfs4_free_copy_state(struct nfsd4_copy *copy) | |
e0639dc5 OK |
1021 | { |
1022 | struct nfsd_net *nn; | |
1023 | ||
81e72297 DN |
1024 | if (copy->cp_stateid.cs_type != NFS4_COPY_STID) |
1025 | return; | |
e0639dc5 OK |
1026 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
1027 | spin_lock(&nn->s2s_cp_lock); | |
624322f1 | 1028 | idr_remove(&nn->s2s_cp_stateids, |
781fde1a | 1029 | copy->cp_stateid.cs_stid.si_opaque.so_id); |
624322f1 OK |
1030 | spin_unlock(&nn->s2s_cp_lock); |
1031 | } | |
1032 | ||
1033 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) | |
1034 | { | |
1035 | struct nfs4_cpntf_state *cps; | |
1036 | struct nfsd_net *nn; | |
1037 | ||
1038 | nn = net_generic(net, nfsd_net_id); | |
1039 | spin_lock(&nn->s2s_cp_lock); | |
1040 | while (!list_empty(&stid->sc_cp_list)) { | |
1041 | cps = list_first_entry(&stid->sc_cp_list, | |
1042 | struct nfs4_cpntf_state, cp_list); | |
1043 | _free_cpntf_state_locked(nn, cps); | |
1044 | } | |
e0639dc5 OK |
1045 | spin_unlock(&nn->s2s_cp_lock); |
1046 | } | |
1047 | ||
b49e084d | 1048 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
4cdc951b | 1049 | { |
6011695d | 1050 | struct nfs4_stid *stid; |
6011695d | 1051 | |
d19fb70d | 1052 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
6011695d TM |
1053 | if (!stid) |
1054 | return NULL; | |
1055 | ||
d19fb70d | 1056 | return openlockstateid(stid); |
6011695d TM |
1057 | } |
1058 | ||
1059 | static void nfs4_free_deleg(struct nfs4_stid *stid) | |
1060 | { | |
895ddf5e JL |
1061 | struct nfs4_delegation *dp = delegstateid(stid); |
1062 | ||
1063 | WARN_ON_ONCE(!list_empty(&stid->sc_cp_list)); | |
1064 | WARN_ON_ONCE(!list_empty(&dp->dl_perfile)); | |
1065 | WARN_ON_ONCE(!list_empty(&dp->dl_perclnt)); | |
1066 | WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru)); | |
6011695d TM |
1067 | kmem_cache_free(deleg_slab, stid); |
1068 | atomic_long_dec(&num_delegations); | |
4cdc951b BF |
1069 | } |
1070 | ||
6282cd56 N |
1071 | /* |
1072 | * When we recall a delegation, we should be careful not to hand it | |
1073 | * out again straight away. | |
1074 | * To ensure this we keep a pair of bloom filters ('new' and 'old') | |
1075 | * in which the filehandles of recalled delegations are "stored". | |
1076 | * If a filehandle appear in either filter, a delegation is blocked. | |
1077 | * When a delegation is recalled, the filehandle is stored in the "new" | |
1078 | * filter. | |
1079 | * Every 30 seconds we swap the filters and clear the "new" one, | |
1080 | * unless both are empty of course. | |
1081 | * | |
1082 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the | |
1083 | * low 3 bytes as hash-table indices. | |
1084 | * | |
f54fe962 | 1085 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
6282cd56 N |
1086 | * is used to manage concurrent access. Testing does not need the lock |
1087 | * except when swapping the two filters. | |
1088 | */ | |
f54fe962 | 1089 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
6282cd56 N |
1090 | static struct bloom_pair { |
1091 | int entries, old_entries; | |
b3f255ef | 1092 | time64_t swap_time; |
6282cd56 N |
1093 | int new; /* index into 'set' */ |
1094 | DECLARE_BITMAP(set[2], 256); | |
1095 | } blocked_delegations; | |
1096 | ||
1097 | static int delegation_blocked(struct knfsd_fh *fh) | |
1098 | { | |
1099 | u32 hash; | |
1100 | struct bloom_pair *bd = &blocked_delegations; | |
1101 | ||
1102 | if (bd->entries == 0) | |
1103 | return 0; | |
b3f255ef | 1104 | if (ktime_get_seconds() - bd->swap_time > 30) { |
f54fe962 | 1105 | spin_lock(&blocked_delegations_lock); |
b3f255ef | 1106 | if (ktime_get_seconds() - bd->swap_time > 30) { |
6282cd56 N |
1107 | bd->entries -= bd->old_entries; |
1108 | bd->old_entries = bd->entries; | |
1109 | memset(bd->set[bd->new], 0, | |
1110 | sizeof(bd->set[0])); | |
1111 | bd->new = 1-bd->new; | |
b3f255ef | 1112 | bd->swap_time = ktime_get_seconds(); |
6282cd56 | 1113 | } |
f54fe962 | 1114 | spin_unlock(&blocked_delegations_lock); |
6282cd56 | 1115 | } |
d8b26071 | 1116 | hash = jhash(&fh->fh_raw, fh->fh_size, 0); |
6282cd56 N |
1117 | if (test_bit(hash&255, bd->set[0]) && |
1118 | test_bit((hash>>8)&255, bd->set[0]) && | |
1119 | test_bit((hash>>16)&255, bd->set[0])) | |
1120 | return 1; | |
1121 | ||
1122 | if (test_bit(hash&255, bd->set[1]) && | |
1123 | test_bit((hash>>8)&255, bd->set[1]) && | |
1124 | test_bit((hash>>16)&255, bd->set[1])) | |
1125 | return 1; | |
1126 | ||
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | static void block_delegations(struct knfsd_fh *fh) | |
1131 | { | |
1132 | u32 hash; | |
1133 | struct bloom_pair *bd = &blocked_delegations; | |
1134 | ||
d8b26071 | 1135 | hash = jhash(&fh->fh_raw, fh->fh_size, 0); |
6282cd56 | 1136 | |
f54fe962 | 1137 | spin_lock(&blocked_delegations_lock); |
6282cd56 N |
1138 | __set_bit(hash&255, bd->set[bd->new]); |
1139 | __set_bit((hash>>8)&255, bd->set[bd->new]); | |
1140 | __set_bit((hash>>16)&255, bd->set[bd->new]); | |
1141 | if (bd->entries == 0) | |
b3f255ef | 1142 | bd->swap_time = ktime_get_seconds(); |
6282cd56 | 1143 | bd->entries += 1; |
f54fe962 | 1144 | spin_unlock(&blocked_delegations_lock); |
6282cd56 N |
1145 | } |
1146 | ||
1da177e4 | 1147 | static struct nfs4_delegation * |
86d29b10 | 1148 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
1d3dd1d5 | 1149 | struct nfs4_clnt_odstate *odstate, u32 dl_type) |
1da177e4 LT |
1150 | { |
1151 | struct nfs4_delegation *dp; | |
2ffda63c | 1152 | struct nfs4_stid *stid; |
02a3508d | 1153 | long n; |
1da177e4 LT |
1154 | |
1155 | dprintk("NFSD alloc_init_deleg\n"); | |
02a3508d TM |
1156 | n = atomic_long_inc_return(&num_delegations); |
1157 | if (n < 0 || n > max_delegations) | |
1158 | goto out_dec; | |
bbf936ed | 1159 | if (delegation_blocked(&fp->fi_fhandle)) |
02a3508d | 1160 | goto out_dec; |
2ffda63c SH |
1161 | stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg); |
1162 | if (stid == NULL) | |
02a3508d | 1163 | goto out_dec; |
2ffda63c | 1164 | dp = delegstateid(stid); |
6011695d | 1165 | |
2a74aba7 BF |
1166 | /* |
1167 | * delegation seqid's are never incremented. The 4.1 special | |
6136d2b4 BF |
1168 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
1169 | * 0 anyway just for consistency and use 1: | |
2a74aba7 BF |
1170 | */ |
1171 | dp->dl_stid.sc_stateid.si_generation = 1; | |
ea1da636 N |
1172 | INIT_LIST_HEAD(&dp->dl_perfile); |
1173 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 | 1174 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
8287f009 SB |
1175 | dp->dl_clnt_odstate = odstate; |
1176 | get_clnt_odstate(odstate); | |
1d3dd1d5 | 1177 | dp->dl_type = dl_type; |
f0b5de1b | 1178 | dp->dl_retries = 1; |
66af2579 | 1179 | dp->dl_recalled = false; |
f0b5de1b | 1180 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
0162ac2b | 1181 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
c5967721 DN |
1182 | nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client, |
1183 | &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR); | |
1184 | dp->dl_cb_fattr.ncf_file_modified = false; | |
1185 | dp->dl_cb_fattr.ncf_cb_bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE; | |
86d29b10 BF |
1186 | get_nfs4_file(fp); |
1187 | dp->dl_stid.sc_file = fp; | |
1da177e4 | 1188 | return dp; |
02a3508d TM |
1189 | out_dec: |
1190 | atomic_long_dec(&num_delegations); | |
1191 | return NULL; | |
1da177e4 LT |
1192 | } |
1193 | ||
1194 | void | |
6011695d | 1195 | nfs4_put_stid(struct nfs4_stid *s) |
1da177e4 | 1196 | { |
11b9164a | 1197 | struct nfs4_file *fp = s->sc_file; |
6011695d TM |
1198 | struct nfs4_client *clp = s->sc_client; |
1199 | ||
4770d722 JL |
1200 | might_lock(&clp->cl_lock); |
1201 | ||
a15dfcd5 | 1202 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
b401be22 | 1203 | wake_up_all(&close_wq); |
6011695d | 1204 | return; |
b401be22 | 1205 | } |
6011695d | 1206 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
1ac3629b N |
1207 | if (s->sc_status & SC_STATUS_ADMIN_REVOKED) |
1208 | atomic_dec(&s->sc_client->cl_admin_revoked); | |
624322f1 | 1209 | nfs4_free_cpntf_statelist(clp->net, s); |
4770d722 | 1210 | spin_unlock(&clp->cl_lock); |
6011695d | 1211 | s->sc_free(s); |
11b9164a TM |
1212 | if (fp) |
1213 | put_nfs4_file(fp); | |
1da177e4 LT |
1214 | } |
1215 | ||
9767feb2 JL |
1216 | void |
1217 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) | |
1218 | { | |
1219 | stateid_t *src = &stid->sc_stateid; | |
1220 | ||
1221 | spin_lock(&stid->sc_lock); | |
1222 | if (unlikely(++src->si_generation == 0)) | |
1223 | src->si_generation = 1; | |
1224 | memcpy(dst, src, sizeof(*dst)); | |
1225 | spin_unlock(&stid->sc_lock); | |
1226 | } | |
1227 | ||
353601e7 | 1228 | static void put_deleg_file(struct nfs4_file *fp) |
1da177e4 | 1229 | { |
eb82dd39 | 1230 | struct nfsd_file *nf = NULL; |
b8232d33 | 1231 | |
6bcc034e | 1232 | spin_lock(&fp->fi_lock); |
353601e7 | 1233 | if (--fp->fi_delegees == 0) |
eb82dd39 | 1234 | swap(nf, fp->fi_deleg_file); |
6bcc034e JL |
1235 | spin_unlock(&fp->fi_lock); |
1236 | ||
eb82dd39 JL |
1237 | if (nf) |
1238 | nfsd_file_put(nf); | |
353601e7 BF |
1239 | } |
1240 | ||
1241 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) | |
1242 | { | |
1243 | struct nfs4_file *fp = dp->dl_stid.sc_file; | |
eb82dd39 | 1244 | struct nfsd_file *nf = fp->fi_deleg_file; |
353601e7 BF |
1245 | |
1246 | WARN_ON_ONCE(!fp->fi_delegees); | |
1247 | ||
7b800101 | 1248 | kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
353601e7 | 1249 | put_deleg_file(fp); |
1da177e4 LT |
1250 | } |
1251 | ||
0af6e690 BF |
1252 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
1253 | { | |
1254 | put_clnt_odstate(dp->dl_clnt_odstate); | |
353601e7 | 1255 | nfs4_unlock_deleg_lease(dp); |
0af6e690 BF |
1256 | nfs4_put_stid(&dp->dl_stid); |
1257 | } | |
1258 | ||
34ed9872 | 1259 | /** |
68b18f52 | 1260 | * nfs4_delegation_exists - Discover if this delegation already exists |
34ed9872 AE |
1261 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
1262 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
1263 | * | |
1264 | * Return: | |
68b18f52 | 1265 | * On success: true iff an existing delegation is found |
34ed9872 AE |
1266 | */ |
1267 | ||
68b18f52 BF |
1268 | static bool |
1269 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) | |
34ed9872 AE |
1270 | { |
1271 | struct nfs4_delegation *searchdp = NULL; | |
1272 | struct nfs4_client *searchclp = NULL; | |
1273 | ||
1274 | lockdep_assert_held(&state_lock); | |
1275 | lockdep_assert_held(&fp->fi_lock); | |
1276 | ||
1277 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { | |
1278 | searchclp = searchdp->dl_stid.sc_client; | |
1279 | if (clp == searchclp) { | |
51d87bc2 | 1280 | return true; |
34ed9872 AE |
1281 | } |
1282 | } | |
51d87bc2 | 1283 | return false; |
34ed9872 AE |
1284 | } |
1285 | ||
1286 | /** | |
1287 | * hash_delegation_locked - Add a delegation to the appropriate lists | |
1288 | * @dp: a pointer to the nfs4_delegation we are adding. | |
1289 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
1290 | * | |
1291 | * Return: | |
1292 | * On success: NULL if the delegation was successfully hashed. | |
1293 | * | |
1294 | * On error: -EAGAIN if one was previously granted to this | |
1295 | * nfs4_client for this nfs4_file. Delegation is not hashed. | |
1296 | * | |
1297 | */ | |
1298 | ||
1299 | static int | |
931ee56c BH |
1300 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
1301 | { | |
34ed9872 AE |
1302 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
1303 | ||
cdc97505 | 1304 | lockdep_assert_held(&state_lock); |
417c6629 | 1305 | lockdep_assert_held(&fp->fi_lock); |
77945728 | 1306 | lockdep_assert_held(&clp->cl_lock); |
931ee56c | 1307 | |
68b18f52 BF |
1308 | if (nfs4_delegation_exists(clp, fp)) |
1309 | return -EAGAIN; | |
a15dfcd5 | 1310 | refcount_inc(&dp->dl_stid.sc_count); |
3f29cc82 | 1311 | dp->dl_stid.sc_type = SC_TYPE_DELEG; |
931ee56c | 1312 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
34ed9872 AE |
1313 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
1314 | return 0; | |
931ee56c BH |
1315 | } |
1316 | ||
548ec080 BF |
1317 | static bool delegation_hashed(struct nfs4_delegation *dp) |
1318 | { | |
1319 | return !(list_empty(&dp->dl_perfile)); | |
1320 | } | |
1321 | ||
3fcbbd24 | 1322 | static bool |
3f29cc82 | 1323 | unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask) |
1da177e4 | 1324 | { |
11b9164a | 1325 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
02e1215f | 1326 | |
42690676 JL |
1327 | lockdep_assert_held(&state_lock); |
1328 | ||
548ec080 | 1329 | if (!delegation_hashed(dp)) |
3fcbbd24 JL |
1330 | return false; |
1331 | ||
06efa667 N |
1332 | if (statusmask == SC_STATUS_REVOKED && |
1333 | dp->dl_stid.sc_client->cl_minorversion == 0) | |
3f29cc82 N |
1334 | statusmask = SC_STATUS_CLOSED; |
1335 | dp->dl_stid.sc_status |= statusmask; | |
06efa667 N |
1336 | if (statusmask & SC_STATUS_ADMIN_REVOKED) |
1337 | atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked); | |
3f29cc82 | 1338 | |
d55a166c JL |
1339 | /* Ensure that deleg break won't try to requeue it */ |
1340 | ++dp->dl_time; | |
417c6629 | 1341 | spin_lock(&fp->fi_lock); |
931ee56c | 1342 | list_del_init(&dp->dl_perclnt); |
1da177e4 | 1343 | list_del_init(&dp->dl_recall_lru); |
02e1215f JL |
1344 | list_del_init(&dp->dl_perfile); |
1345 | spin_unlock(&fp->fi_lock); | |
3fcbbd24 | 1346 | return true; |
3bd64a5b BF |
1347 | } |
1348 | ||
3bd64a5b BF |
1349 | static void destroy_delegation(struct nfs4_delegation *dp) |
1350 | { | |
3fcbbd24 JL |
1351 | bool unhashed; |
1352 | ||
42690676 | 1353 | spin_lock(&state_lock); |
3f29cc82 | 1354 | unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED); |
42690676 | 1355 | spin_unlock(&state_lock); |
0af6e690 BF |
1356 | if (unhashed) |
1357 | destroy_unhashed_deleg(dp); | |
3bd64a5b BF |
1358 | } |
1359 | ||
1360 | static void revoke_delegation(struct nfs4_delegation *dp) | |
1361 | { | |
1362 | struct nfs4_client *clp = dp->dl_stid.sc_client; | |
1363 | ||
2d4a532d JL |
1364 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
1365 | ||
a1c74569 CL |
1366 | trace_nfsd_stid_revoke(&dp->dl_stid); |
1367 | ||
06efa667 N |
1368 | if (dp->dl_stid.sc_status & |
1369 | (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)) { | |
3b816601 | 1370 | spin_lock(&clp->cl_lock); |
0af6e690 | 1371 | refcount_inc(&dp->dl_stid.sc_count); |
2d4a532d JL |
1372 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
1373 | spin_unlock(&clp->cl_lock); | |
3bd64a5b | 1374 | } |
0af6e690 | 1375 | destroy_unhashed_deleg(dp); |
3bd64a5b BF |
1376 | } |
1377 | ||
3f29cc82 N |
1378 | /* |
1379 | * SETCLIENTID state | |
1da177e4 LT |
1380 | */ |
1381 | ||
ddc04c41 BF |
1382 | static unsigned int clientid_hashval(u32 id) |
1383 | { | |
1384 | return id & CLIENT_HASH_MASK; | |
1385 | } | |
1386 | ||
6b189105 | 1387 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
ddc04c41 | 1388 | { |
6b189105 | 1389 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
ddc04c41 BF |
1390 | } |
1391 | ||
baeb4ff0 JL |
1392 | /* |
1393 | * A stateid that had a deny mode associated with it is being released | |
1394 | * or downgraded. Recalculate the deny mode on the file. | |
1395 | */ | |
1396 | static void | |
1397 | recalculate_deny_mode(struct nfs4_file *fp) | |
1398 | { | |
1399 | struct nfs4_ol_stateid *stp; | |
1400 | ||
1401 | spin_lock(&fp->fi_lock); | |
1402 | fp->fi_share_deny = 0; | |
1403 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) | |
1404 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); | |
1405 | spin_unlock(&fp->fi_lock); | |
1406 | } | |
1407 | ||
1408 | static void | |
1409 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
1410 | { | |
1411 | int i; | |
1412 | bool change = false; | |
1413 | ||
1414 | for (i = 1; i < 4; i++) { | |
1415 | if ((i & deny) != i) { | |
1416 | change = true; | |
1417 | clear_deny(i, stp); | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | /* Recalculate per-file deny mode if there was a change */ | |
1422 | if (change) | |
11b9164a | 1423 | recalculate_deny_mode(stp->st_stid.sc_file); |
baeb4ff0 JL |
1424 | } |
1425 | ||
82c5ff1b JL |
1426 | /* release all access and file references for a given stateid */ |
1427 | static void | |
1428 | release_all_access(struct nfs4_ol_stateid *stp) | |
1429 | { | |
1430 | int i; | |
11b9164a | 1431 | struct nfs4_file *fp = stp->st_stid.sc_file; |
baeb4ff0 JL |
1432 | |
1433 | if (fp && stp->st_deny_bmap != 0) | |
1434 | recalculate_deny_mode(fp); | |
82c5ff1b JL |
1435 | |
1436 | for (i = 1; i < 4; i++) { | |
1437 | if (test_access(i, stp)) | |
11b9164a | 1438 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
82c5ff1b JL |
1439 | clear_access(i, stp); |
1440 | } | |
1441 | } | |
1442 | ||
d50ffded KM |
1443 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
1444 | { | |
1445 | kfree(sop->so_owner.data); | |
1446 | sop->so_ops->so_free(sop); | |
1447 | } | |
1448 | ||
6b180f0b JL |
1449 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
1450 | { | |
a819ecc1 JL |
1451 | struct nfs4_client *clp = sop->so_client; |
1452 | ||
1453 | might_lock(&clp->cl_lock); | |
1454 | ||
1455 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) | |
6b180f0b | 1456 | return; |
8f4b54c5 | 1457 | sop->so_ops->so_unhash(sop); |
a819ecc1 | 1458 | spin_unlock(&clp->cl_lock); |
d50ffded | 1459 | nfs4_free_stateowner(sop); |
6b180f0b JL |
1460 | } |
1461 | ||
a451b123 TM |
1462 | static bool |
1463 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) | |
1464 | { | |
1465 | return list_empty(&stp->st_perfile); | |
1466 | } | |
1467 | ||
e8568739 | 1468 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a | 1469 | { |
11b9164a | 1470 | struct nfs4_file *fp = stp->st_stid.sc_file; |
1d31a253 | 1471 | |
1c755dc1 JL |
1472 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
1473 | ||
e8568739 JL |
1474 | if (list_empty(&stp->st_perfile)) |
1475 | return false; | |
1476 | ||
1d31a253 | 1477 | spin_lock(&fp->fi_lock); |
e8568739 | 1478 | list_del_init(&stp->st_perfile); |
1d31a253 | 1479 | spin_unlock(&fp->fi_lock); |
529d7b2a | 1480 | list_del(&stp->st_perstateowner); |
e8568739 | 1481 | return true; |
529d7b2a BF |
1482 | } |
1483 | ||
6011695d | 1484 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
529d7b2a | 1485 | { |
6011695d | 1486 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
4665e2ba | 1487 | |
8287f009 | 1488 | put_clnt_odstate(stp->st_clnt_odstate); |
6011695d | 1489 | release_all_access(stp); |
d3134b10 JL |
1490 | if (stp->st_stateowner) |
1491 | nfs4_put_stateowner(stp->st_stateowner); | |
019805fe | 1492 | WARN_ON(!list_empty(&stid->sc_cp_list)); |
6011695d | 1493 | kmem_cache_free(stateid_slab, stid); |
529d7b2a BF |
1494 | } |
1495 | ||
b49e084d | 1496 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
529d7b2a | 1497 | { |
b49e084d JL |
1498 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
1499 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); | |
eb82dd39 | 1500 | struct nfsd_file *nf; |
529d7b2a | 1501 | |
eb82dd39 JL |
1502 | nf = find_any_file(stp->st_stid.sc_file); |
1503 | if (nf) { | |
1504 | get_file(nf->nf_file); | |
1505 | filp_close(nf->nf_file, (fl_owner_t)lo); | |
1506 | nfsd_file_put(nf); | |
1507 | } | |
b49e084d JL |
1508 | nfs4_free_ol_stateid(stid); |
1509 | } | |
1510 | ||
2c41beb0 JL |
1511 | /* |
1512 | * Put the persistent reference to an already unhashed generic stateid, while | |
1513 | * holding the cl_lock. If it's the last reference, then put it onto the | |
1514 | * reaplist for later destruction. | |
1515 | */ | |
1516 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, | |
1517 | struct list_head *reaplist) | |
1518 | { | |
1519 | struct nfs4_stid *s = &stp->st_stid; | |
1520 | struct nfs4_client *clp = s->sc_client; | |
1521 | ||
1522 | lockdep_assert_held(&clp->cl_lock); | |
1523 | ||
1524 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); | |
1525 | ||
a15dfcd5 | 1526 | if (!refcount_dec_and_test(&s->sc_count)) { |
2c41beb0 JL |
1527 | wake_up_all(&close_wq); |
1528 | return; | |
1529 | } | |
1530 | ||
1531 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); | |
1ac3629b N |
1532 | if (s->sc_status & SC_STATUS_ADMIN_REVOKED) |
1533 | atomic_dec(&s->sc_client->cl_admin_revoked); | |
2c41beb0 JL |
1534 | list_add(&stp->st_locks, reaplist); |
1535 | } | |
1536 | ||
e8568739 | 1537 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
b49e084d | 1538 | { |
f46c445b | 1539 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
3c1c995c | 1540 | |
a451b123 TM |
1541 | if (!unhash_ol_stateid(stp)) |
1542 | return false; | |
3c1c995c | 1543 | list_del_init(&stp->st_locks); |
3f29cc82 | 1544 | stp->st_stid.sc_status |= SC_STATUS_CLOSED; |
a451b123 | 1545 | return true; |
3c1c995c JL |
1546 | } |
1547 | ||
1548 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) | |
1549 | { | |
f46c445b | 1550 | struct nfs4_client *clp = stp->st_stid.sc_client; |
e8568739 | 1551 | bool unhashed; |
3c1c995c | 1552 | |
f46c445b | 1553 | spin_lock(&clp->cl_lock); |
e8568739 | 1554 | unhashed = unhash_lock_stateid(stp); |
f46c445b | 1555 | spin_unlock(&clp->cl_lock); |
e8568739 JL |
1556 | if (unhashed) |
1557 | nfs4_put_stid(&stp->st_stid); | |
529d7b2a BF |
1558 | } |
1559 | ||
c58c6610 | 1560 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
8f4b54c5 | 1561 | { |
d4f0489f | 1562 | struct nfs4_client *clp = lo->lo_owner.so_client; |
c58c6610 | 1563 | |
d4f0489f | 1564 | lockdep_assert_held(&clp->cl_lock); |
c58c6610 | 1565 | |
8f4b54c5 JL |
1566 | list_del_init(&lo->lo_owner.so_strhash); |
1567 | } | |
1568 | ||
2c41beb0 JL |
1569 | /* |
1570 | * Free a list of generic stateids that were collected earlier after being | |
1571 | * fully unhashed. | |
1572 | */ | |
1573 | static void | |
1574 | free_ol_stateid_reaplist(struct list_head *reaplist) | |
1575 | { | |
1576 | struct nfs4_ol_stateid *stp; | |
fb94d766 | 1577 | struct nfs4_file *fp; |
2c41beb0 JL |
1578 | |
1579 | might_sleep(); | |
1580 | ||
1581 | while (!list_empty(reaplist)) { | |
1582 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, | |
1583 | st_locks); | |
1584 | list_del(&stp->st_locks); | |
fb94d766 | 1585 | fp = stp->st_stid.sc_file; |
2c41beb0 | 1586 | stp->st_stid.sc_free(&stp->st_stid); |
fb94d766 KM |
1587 | if (fp) |
1588 | put_nfs4_file(fp); | |
2c41beb0 JL |
1589 | } |
1590 | } | |
1591 | ||
d83017f9 JL |
1592 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
1593 | struct list_head *reaplist) | |
3c87b9b7 TM |
1594 | { |
1595 | struct nfs4_ol_stateid *stp; | |
1596 | ||
e8568739 JL |
1597 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
1598 | ||
3c87b9b7 TM |
1599 | while (!list_empty(&open_stp->st_locks)) { |
1600 | stp = list_entry(open_stp->st_locks.next, | |
1601 | struct nfs4_ol_stateid, st_locks); | |
c6540026 | 1602 | unhash_lock_stateid(stp); |
d83017f9 | 1603 | put_ol_stateid_locked(stp, reaplist); |
529d7b2a BF |
1604 | } |
1605 | } | |
1606 | ||
e8568739 | 1607 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
d83017f9 | 1608 | struct list_head *reaplist) |
2283963f | 1609 | { |
2c41beb0 JL |
1610 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
1611 | ||
a451b123 TM |
1612 | if (!unhash_ol_stateid(stp)) |
1613 | return false; | |
d83017f9 | 1614 | release_open_stateid_locks(stp, reaplist); |
a451b123 | 1615 | return true; |
38c387b5 BF |
1616 | } |
1617 | ||
1618 | static void release_open_stateid(struct nfs4_ol_stateid *stp) | |
1619 | { | |
2c41beb0 JL |
1620 | LIST_HEAD(reaplist); |
1621 | ||
1622 | spin_lock(&stp->st_stid.sc_client->cl_lock); | |
3f29cc82 | 1623 | stp->st_stid.sc_status |= SC_STATUS_CLOSED; |
e8568739 JL |
1624 | if (unhash_open_stateid(stp, &reaplist)) |
1625 | put_ol_stateid_locked(stp, &reaplist); | |
2c41beb0 JL |
1626 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
1627 | free_ol_stateid_reaplist(&reaplist); | |
2283963f BF |
1628 | } |
1629 | ||
7ffb5880 | 1630 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
f1d110ca | 1631 | { |
d4f0489f | 1632 | struct nfs4_client *clp = oo->oo_owner.so_client; |
7ffb5880 | 1633 | |
d4f0489f | 1634 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 1635 | |
8f4b54c5 JL |
1636 | list_del_init(&oo->oo_owner.so_strhash); |
1637 | list_del_init(&oo->oo_perclient); | |
f1d110ca BF |
1638 | } |
1639 | ||
f7a4d872 BF |
1640 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
1641 | { | |
217526e7 JL |
1642 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
1643 | nfsd_net_id); | |
1644 | struct nfs4_ol_stateid *s; | |
f7a4d872 | 1645 | |
217526e7 JL |
1646 | spin_lock(&nn->client_lock); |
1647 | s = oo->oo_last_closed_stid; | |
f7a4d872 | 1648 | if (s) { |
d3134b10 | 1649 | list_del_init(&oo->oo_close_lru); |
f7a4d872 BF |
1650 | oo->oo_last_closed_stid = NULL; |
1651 | } | |
217526e7 JL |
1652 | spin_unlock(&nn->client_lock); |
1653 | if (s) | |
1654 | nfs4_put_stid(&s->st_stid); | |
f7a4d872 BF |
1655 | } |
1656 | ||
2c41beb0 | 1657 | static void release_openowner(struct nfs4_openowner *oo) |
8f4b54c5 JL |
1658 | { |
1659 | struct nfs4_ol_stateid *stp; | |
d4f0489f | 1660 | struct nfs4_client *clp = oo->oo_owner.so_client; |
2c41beb0 | 1661 | struct list_head reaplist; |
7ffb5880 | 1662 | |
2c41beb0 | 1663 | INIT_LIST_HEAD(&reaplist); |
8f4b54c5 | 1664 | |
2c41beb0 JL |
1665 | spin_lock(&clp->cl_lock); |
1666 | unhash_openowner_locked(oo); | |
8f4b54c5 JL |
1667 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
1668 | stp = list_first_entry(&oo->oo_owner.so_stateids, | |
1669 | struct nfs4_ol_stateid, st_perstateowner); | |
e8568739 JL |
1670 | if (unhash_open_stateid(stp, &reaplist)) |
1671 | put_ol_stateid_locked(stp, &reaplist); | |
8f4b54c5 | 1672 | } |
d4f0489f | 1673 | spin_unlock(&clp->cl_lock); |
2c41beb0 | 1674 | free_ol_stateid_reaplist(&reaplist); |
f7a4d872 | 1675 | release_last_closed_stateid(oo); |
6b180f0b | 1676 | nfs4_put_stateowner(&oo->oo_owner); |
f1d110ca BF |
1677 | } |
1678 | ||
1ac3629b N |
1679 | static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp, |
1680 | struct super_block *sb, | |
1681 | unsigned int sc_types) | |
1682 | { | |
1683 | unsigned long id, tmp; | |
1684 | struct nfs4_stid *stid; | |
1685 | ||
1686 | spin_lock(&clp->cl_lock); | |
1687 | idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) | |
1688 | if ((stid->sc_type & sc_types) && | |
1689 | stid->sc_status == 0 && | |
1690 | stid->sc_file->fi_inode->i_sb == sb) { | |
1691 | refcount_inc(&stid->sc_count); | |
1692 | break; | |
1693 | } | |
1694 | spin_unlock(&clp->cl_lock); | |
1695 | return stid; | |
1696 | } | |
1697 | ||
1698 | /** | |
1699 | * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem | |
1700 | * @net: used to identify instance of nfsd (there is one per net namespace) | |
1701 | * @sb: super_block used to identify target filesystem | |
1702 | * | |
1703 | * All nfs4 states (open, lock, delegation, layout) held by the server instance | |
1704 | * and associated with a file on the given filesystem will be revoked resulting | |
1705 | * in any files being closed and so all references from nfsd to the filesystem | |
1706 | * being released. Thus nfsd will no longer prevent the filesystem from being | |
1707 | * unmounted. | |
1708 | * | |
1709 | * The clients which own the states will subsequently being notified that the | |
1710 | * states have been "admin-revoked". | |
1711 | */ | |
1712 | void nfsd4_revoke_states(struct net *net, struct super_block *sb) | |
1713 | { | |
1714 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1715 | unsigned int idhashval; | |
1716 | unsigned int sc_types; | |
1717 | ||
1e33e141 | 1718 | sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT; |
1ac3629b N |
1719 | |
1720 | spin_lock(&nn->client_lock); | |
1721 | for (idhashval = 0; idhashval < CLIENT_HASH_MASK; idhashval++) { | |
1722 | struct list_head *head = &nn->conf_id_hashtbl[idhashval]; | |
1723 | struct nfs4_client *clp; | |
1724 | retry: | |
1725 | list_for_each_entry(clp, head, cl_idhash) { | |
1726 | struct nfs4_stid *stid = find_one_sb_stid(clp, sb, | |
1727 | sc_types); | |
1728 | if (stid) { | |
1c13bf9f | 1729 | struct nfs4_ol_stateid *stp; |
06efa667 | 1730 | struct nfs4_delegation *dp; |
1e33e141 | 1731 | struct nfs4_layout_stateid *ls; |
1c13bf9f | 1732 | |
1ac3629b N |
1733 | spin_unlock(&nn->client_lock); |
1734 | switch (stid->sc_type) { | |
39657c74 N |
1735 | case SC_TYPE_OPEN: |
1736 | stp = openlockstateid(stid); | |
1737 | mutex_lock_nested(&stp->st_mutex, | |
1738 | OPEN_STATEID_MUTEX); | |
1739 | ||
1740 | spin_lock(&clp->cl_lock); | |
1741 | if (stid->sc_status == 0) { | |
1742 | stid->sc_status |= | |
1743 | SC_STATUS_ADMIN_REVOKED; | |
1744 | atomic_inc(&clp->cl_admin_revoked); | |
1745 | spin_unlock(&clp->cl_lock); | |
1746 | release_all_access(stp); | |
1747 | } else | |
1748 | spin_unlock(&clp->cl_lock); | |
1749 | mutex_unlock(&stp->st_mutex); | |
1750 | break; | |
1c13bf9f N |
1751 | case SC_TYPE_LOCK: |
1752 | stp = openlockstateid(stid); | |
1753 | mutex_lock_nested(&stp->st_mutex, | |
1754 | LOCK_STATEID_MUTEX); | |
1755 | spin_lock(&clp->cl_lock); | |
1756 | if (stid->sc_status == 0) { | |
1757 | struct nfs4_lockowner *lo = | |
1758 | lockowner(stp->st_stateowner); | |
1759 | struct nfsd_file *nf; | |
1760 | ||
1761 | stid->sc_status |= | |
1762 | SC_STATUS_ADMIN_REVOKED; | |
1763 | atomic_inc(&clp->cl_admin_revoked); | |
1764 | spin_unlock(&clp->cl_lock); | |
1765 | nf = find_any_file(stp->st_stid.sc_file); | |
1766 | if (nf) { | |
1767 | get_file(nf->nf_file); | |
1768 | filp_close(nf->nf_file, | |
1769 | (fl_owner_t)lo); | |
1770 | nfsd_file_put(nf); | |
1771 | } | |
1772 | release_all_access(stp); | |
1773 | } else | |
1774 | spin_unlock(&clp->cl_lock); | |
1775 | mutex_unlock(&stp->st_mutex); | |
1776 | break; | |
06efa667 N |
1777 | case SC_TYPE_DELEG: |
1778 | dp = delegstateid(stid); | |
1779 | spin_lock(&state_lock); | |
1780 | if (!unhash_delegation_locked( | |
1781 | dp, SC_STATUS_ADMIN_REVOKED)) | |
1782 | dp = NULL; | |
1783 | spin_unlock(&state_lock); | |
1784 | if (dp) | |
1785 | revoke_delegation(dp); | |
1786 | break; | |
1e33e141 N |
1787 | case SC_TYPE_LAYOUT: |
1788 | ls = layoutstateid(stid); | |
1789 | nfsd4_close_layout(ls); | |
1790 | break; | |
1ac3629b N |
1791 | } |
1792 | nfs4_put_stid(stid); | |
1793 | spin_lock(&nn->client_lock); | |
d688d858 N |
1794 | if (clp->cl_minorversion == 0) |
1795 | /* Allow cleanup after a lease period. | |
1796 | * store_release ensures cleanup will | |
1797 | * see any newly revoked states if it | |
1798 | * sees the time updated. | |
1799 | */ | |
1800 | nn->nfs40_last_revoke = | |
1801 | ktime_get_boottime_seconds(); | |
1ac3629b N |
1802 | goto retry; |
1803 | } | |
1804 | } | |
1805 | } | |
1806 | spin_unlock(&nn->client_lock); | |
1807 | } | |
1808 | ||
5282fd72 ME |
1809 | static inline int |
1810 | hash_sessionid(struct nfs4_sessionid *sessionid) | |
1811 | { | |
1812 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; | |
1813 | ||
1814 | return sid->sequence % SESSION_HASH_SIZE; | |
1815 | } | |
1816 | ||
135dd002 | 1817 | #ifdef CONFIG_SUNRPC_DEBUG |
5282fd72 ME |
1818 | static inline void |
1819 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1820 | { | |
1821 | u32 *ptr = (u32 *)(&sessionid->data[0]); | |
1822 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); | |
1823 | } | |
8f199b82 TM |
1824 | #else |
1825 | static inline void | |
1826 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1827 | { | |
1828 | } | |
1829 | #endif | |
1830 | ||
9411b1d4 BF |
1831 | /* |
1832 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it | |
1833 | * won't be used for replay. | |
1834 | */ | |
1835 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) | |
1836 | { | |
1837 | struct nfs4_stateowner *so = cstate->replay_owner; | |
1838 | ||
1839 | if (nfserr == nfserr_replay_me) | |
1840 | return; | |
1841 | ||
1842 | if (!seqid_mutating_err(ntohl(nfserr))) { | |
58fb12e6 | 1843 | nfsd4_cstate_clear_replay(cstate); |
9411b1d4 BF |
1844 | return; |
1845 | } | |
1846 | if (!so) | |
1847 | return; | |
1848 | if (so->so_is_open_owner) | |
1849 | release_last_closed_stateid(openowner(so)); | |
1850 | so->so_seqid++; | |
1851 | return; | |
1852 | } | |
5282fd72 | 1853 | |
ec6b5d7b AA |
1854 | static void |
1855 | gen_sessionid(struct nfsd4_session *ses) | |
1856 | { | |
1857 | struct nfs4_client *clp = ses->se_client; | |
1858 | struct nfsd4_sessionid *sid; | |
1859 | ||
1860 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; | |
1861 | sid->clientid = clp->cl_clientid; | |
1862 | sid->sequence = current_sessionid++; | |
1863 | sid->reserved = 0; | |
1864 | } | |
1865 | ||
1866 | /* | |
a649637c AA |
1867 | * The protocol defines ca_maxresponssize_cached to include the size of |
1868 | * the rpc header, but all we need to cache is the data starting after | |
1869 | * the end of the initial SEQUENCE operation--the rest we regenerate | |
1870 | * each time. Therefore we can advertise a ca_maxresponssize_cached | |
1871 | * value that is the number of bytes in our cache plus a few additional | |
1872 | * bytes. In order to stay on the safe side, and not promise more than | |
1873 | * we can cache, those additional bytes must be the minimum possible: 24 | |
1874 | * bytes of rpc header (xid through accept state, with AUTH_NULL | |
1875 | * verifier), 12 for the compound header (with zero-length tag), and 44 | |
1876 | * for the SEQUENCE op response: | |
1877 | */ | |
1878 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) | |
1879 | ||
557ce264 AA |
1880 | static void |
1881 | free_session_slots(struct nfsd4_session *ses) | |
1882 | { | |
1883 | int i; | |
1884 | ||
53da6a53 BF |
1885 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
1886 | free_svc_cred(&ses->se_slots[i]->sl_cred); | |
557ce264 | 1887 | kfree(ses->se_slots[i]); |
53da6a53 | 1888 | } |
557ce264 AA |
1889 | } |
1890 | ||
a649637c | 1891 | /* |
efe0cb6d BF |
1892 | * We don't actually need to cache the rpc and session headers, so we |
1893 | * can allocate a little less for each slot: | |
1894 | */ | |
55c760cf | 1895 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
efe0cb6d | 1896 | { |
55c760cf | 1897 | u32 size; |
efe0cb6d | 1898 | |
55c760cf BF |
1899 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
1900 | size = 0; | |
1901 | else | |
1902 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; | |
1903 | return size + sizeof(struct nfsd4_slot); | |
5b6feee9 | 1904 | } |
ec6b5d7b | 1905 | |
5b6feee9 BF |
1906 | /* |
1907 | * XXX: If we run out of reserved DRC memory we could (up to a point) | |
a649637c | 1908 | * re-negotiate active sessions and reduce their slot usage to make |
42b2aa86 | 1909 | * room for new connections. For now we just fail the create session. |
ec6b5d7b | 1910 | */ |
2030ca56 | 1911 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
ec6b5d7b | 1912 | { |
55c760cf BF |
1913 | u32 slotsize = slot_bytes(ca); |
1914 | u32 num = ca->maxreqs; | |
c54f24e3 | 1915 | unsigned long avail, total_avail; |
2030ca56 | 1916 | unsigned int scale_factor; |
ec6b5d7b | 1917 | |
5b6feee9 | 1918 | spin_lock(&nfsd_drc_lock); |
7f49fd5d N |
1919 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
1920 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; | |
1921 | else | |
1922 | /* We have handed out more space than we chose in | |
1923 | * set_max_drc() to allow. That isn't really a | |
1924 | * problem as long as that doesn't make us think we | |
1925 | * have lots more due to integer overflow. | |
1926 | */ | |
1927 | total_avail = 0; | |
c54f24e3 | 1928 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
de766e57 | 1929 | /* |
2030ca56 | 1930 | * Never use more than a fraction of the remaining memory, |
7f49fd5d | 1931 | * unless it's the only way to give this client a slot. |
2030ca56 N |
1932 | * The chosen fraction is either 1/8 or 1/number of threads, |
1933 | * whichever is smaller. This ensures there are adequate | |
1934 | * slots to support multiple clients per thread. | |
7f49fd5d N |
1935 | * Give the client one slot even if that would require |
1936 | * over-allocation--it is better than failure. | |
de766e57 | 1937 | */ |
2030ca56 N |
1938 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
1939 | ||
1940 | avail = clamp_t(unsigned long, avail, slotsize, | |
1941 | total_avail/scale_factor); | |
5b6feee9 | 1942 | num = min_t(int, num, avail / slotsize); |
7f49fd5d | 1943 | num = max_t(int, num, 1); |
5b6feee9 BF |
1944 | nfsd_drc_mem_used += num * slotsize; |
1945 | spin_unlock(&nfsd_drc_lock); | |
ec6b5d7b | 1946 | |
5b6feee9 BF |
1947 | return num; |
1948 | } | |
ec6b5d7b | 1949 | |
55c760cf | 1950 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
5b6feee9 | 1951 | { |
55c760cf BF |
1952 | int slotsize = slot_bytes(ca); |
1953 | ||
4bd9b0f4 | 1954 | spin_lock(&nfsd_drc_lock); |
55c760cf | 1955 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
4bd9b0f4 | 1956 | spin_unlock(&nfsd_drc_lock); |
5b6feee9 | 1957 | } |
ec6b5d7b | 1958 | |
60810e54 KM |
1959 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
1960 | struct nfsd4_channel_attrs *battrs) | |
5b6feee9 | 1961 | { |
60810e54 KM |
1962 | int numslots = fattrs->maxreqs; |
1963 | int slotsize = slot_bytes(fattrs); | |
5b6feee9 | 1964 | struct nfsd4_session *new; |
85a0d0c9 | 1965 | int i; |
a649637c | 1966 | |
85a0d0c9 XJ |
1967 | BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION) |
1968 | > PAGE_SIZE); | |
ec6b5d7b | 1969 | |
85a0d0c9 | 1970 | new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL); |
5b6feee9 BF |
1971 | if (!new) |
1972 | return NULL; | |
557ce264 | 1973 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
5b6feee9 | 1974 | for (i = 0; i < numslots; i++) { |
55c760cf | 1975 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
5b6feee9 | 1976 | if (!new->se_slots[i]) |
557ce264 | 1977 | goto out_free; |
557ce264 | 1978 | } |
60810e54 KM |
1979 | |
1980 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); | |
1981 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); | |
1982 | ||
5b6feee9 BF |
1983 | return new; |
1984 | out_free: | |
1985 | while (i--) | |
1986 | kfree(new->se_slots[i]); | |
1987 | kfree(new); | |
1988 | return NULL; | |
ec6b5d7b AA |
1989 | } |
1990 | ||
19cf5c02 BF |
1991 | static void free_conn(struct nfsd4_conn *c) |
1992 | { | |
1993 | svc_xprt_put(c->cn_xprt); | |
1994 | kfree(c); | |
1995 | } | |
ec6b5d7b | 1996 | |
19cf5c02 BF |
1997 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
1998 | { | |
1999 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); | |
2000 | struct nfs4_client *clp = c->cn_session->se_client; | |
ec6b5d7b | 2001 | |
806d65b6 CL |
2002 | trace_nfsd_cb_lost(clp); |
2003 | ||
19cf5c02 BF |
2004 | spin_lock(&clp->cl_lock); |
2005 | if (!list_empty(&c->cn_persession)) { | |
2006 | list_del(&c->cn_persession); | |
2007 | free_conn(c); | |
2008 | } | |
eea49806 | 2009 | nfsd4_probe_callback(clp); |
2e4b7239 | 2010 | spin_unlock(&clp->cl_lock); |
19cf5c02 | 2011 | } |
ec6b5d7b | 2012 | |
d29c374c | 2013 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
c7662518 | 2014 | { |
c7662518 | 2015 | struct nfsd4_conn *conn; |
ec6b5d7b | 2016 | |
c7662518 BF |
2017 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
2018 | if (!conn) | |
db90681d | 2019 | return NULL; |
c7662518 BF |
2020 | svc_xprt_get(rqstp->rq_xprt); |
2021 | conn->cn_xprt = rqstp->rq_xprt; | |
d29c374c | 2022 | conn->cn_flags = flags; |
db90681d BF |
2023 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
2024 | return conn; | |
2025 | } | |
a649637c | 2026 | |
328ead28 BF |
2027 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
2028 | { | |
2029 | conn->cn_session = ses; | |
2030 | list_add(&conn->cn_persession, &ses->se_conns); | |
ec6b5d7b AA |
2031 | } |
2032 | ||
db90681d | 2033 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
557ce264 | 2034 | { |
db90681d | 2035 | struct nfs4_client *clp = ses->se_client; |
557ce264 | 2036 | |
c7662518 | 2037 | spin_lock(&clp->cl_lock); |
328ead28 | 2038 | __nfsd4_hash_conn(conn, ses); |
c7662518 | 2039 | spin_unlock(&clp->cl_lock); |
557ce264 AA |
2040 | } |
2041 | ||
21b75b01 | 2042 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
efe0cb6d | 2043 | { |
19cf5c02 | 2044 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
21b75b01 | 2045 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
efe0cb6d BF |
2046 | } |
2047 | ||
e1ff371f | 2048 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
ec6b5d7b | 2049 | { |
21b75b01 | 2050 | int ret; |
ec6b5d7b | 2051 | |
db90681d | 2052 | nfsd4_hash_conn(conn, ses); |
21b75b01 BF |
2053 | ret = nfsd4_register_conn(conn); |
2054 | if (ret) | |
2055 | /* oops; xprt is already down: */ | |
2056 | nfsd4_conn_lost(&conn->cn_xpt_user); | |
57a37144 BF |
2057 | /* We may have gained or lost a callback channel: */ |
2058 | nfsd4_probe_callback_sync(ses->se_client); | |
c7662518 | 2059 | } |
ec6b5d7b | 2060 | |
e1ff371f | 2061 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
1d1bc8f2 BF |
2062 | { |
2063 | u32 dir = NFS4_CDFC4_FORE; | |
2064 | ||
e1ff371f | 2065 | if (cses->flags & SESSION4_BACK_CHAN) |
1d1bc8f2 | 2066 | dir |= NFS4_CDFC4_BACK; |
e1ff371f | 2067 | return alloc_conn(rqstp, dir); |
1d1bc8f2 BF |
2068 | } |
2069 | ||
2070 | /* must be called under client_lock */ | |
19cf5c02 | 2071 | static void nfsd4_del_conns(struct nfsd4_session *s) |
c7662518 | 2072 | { |
19cf5c02 BF |
2073 | struct nfs4_client *clp = s->se_client; |
2074 | struct nfsd4_conn *c; | |
ec6b5d7b | 2075 | |
19cf5c02 BF |
2076 | spin_lock(&clp->cl_lock); |
2077 | while (!list_empty(&s->se_conns)) { | |
2078 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); | |
2079 | list_del_init(&c->cn_persession); | |
2080 | spin_unlock(&clp->cl_lock); | |
557ce264 | 2081 | |
19cf5c02 BF |
2082 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
2083 | free_conn(c); | |
ec6b5d7b | 2084 | |
19cf5c02 BF |
2085 | spin_lock(&clp->cl_lock); |
2086 | } | |
2087 | spin_unlock(&clp->cl_lock); | |
c7662518 | 2088 | } |
ec6b5d7b | 2089 | |
1377b69e BF |
2090 | static void __free_session(struct nfsd4_session *ses) |
2091 | { | |
1377b69e BF |
2092 | free_session_slots(ses); |
2093 | kfree(ses); | |
2094 | } | |
2095 | ||
66b2b9b2 | 2096 | static void free_session(struct nfsd4_session *ses) |
c7662518 | 2097 | { |
19cf5c02 | 2098 | nfsd4_del_conns(ses); |
55c760cf | 2099 | nfsd4_put_drc_mem(&ses->se_fchannel); |
1377b69e | 2100 | __free_session(ses); |
c7662518 BF |
2101 | } |
2102 | ||
135ae827 | 2103 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
a827bcb2 | 2104 | { |
a827bcb2 | 2105 | int idx; |
1872de0e | 2106 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
a827bcb2 | 2107 | |
ec6b5d7b AA |
2108 | new->se_client = clp; |
2109 | gen_sessionid(new); | |
ec6b5d7b | 2110 | |
c7662518 BF |
2111 | INIT_LIST_HEAD(&new->se_conns); |
2112 | ||
ac7c46f2 | 2113 | new->se_cb_seq_nr = 1; |
ec6b5d7b | 2114 | new->se_flags = cses->flags; |
8b5ce5cd | 2115 | new->se_cb_prog = cses->callback_prog; |
c6bb3ca2 | 2116 | new->se_cb_sec = cses->cb_sec; |
66b2b9b2 | 2117 | atomic_set(&new->se_ref, 0); |
5b6feee9 | 2118 | idx = hash_sessionid(&new->se_sessionid); |
1872de0e | 2119 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
4c649378 | 2120 | spin_lock(&clp->cl_lock); |
ec6b5d7b | 2121 | list_add(&new->se_perclnt, &clp->cl_sessions); |
4c649378 | 2122 | spin_unlock(&clp->cl_lock); |
60810e54 | 2123 | |
b0d2e42c | 2124 | { |
edd76786 | 2125 | struct sockaddr *sa = svc_addr(rqstp); |
dcbeaa68 BF |
2126 | /* |
2127 | * This is a little silly; with sessions there's no real | |
2128 | * use for the callback address. Use the peer address | |
2129 | * as a reasonable default for now, but consider fixing | |
2130 | * the rpc client not to require an address in the | |
2131 | * future: | |
2132 | */ | |
edd76786 BF |
2133 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
2134 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); | |
edd76786 | 2135 | } |
ec6b5d7b AA |
2136 | } |
2137 | ||
9089f1b4 | 2138 | /* caller must hold client_lock */ |
5282fd72 | 2139 | static struct nfsd4_session * |
d4e19e70 | 2140 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
5282fd72 ME |
2141 | { |
2142 | struct nfsd4_session *elem; | |
2143 | int idx; | |
1872de0e | 2144 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
5282fd72 | 2145 | |
0a880a28 TM |
2146 | lockdep_assert_held(&nn->client_lock); |
2147 | ||
5282fd72 ME |
2148 | dump_sessionid(__func__, sessionid); |
2149 | idx = hash_sessionid(sessionid); | |
5282fd72 | 2150 | /* Search in the appropriate list */ |
1872de0e | 2151 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
5282fd72 ME |
2152 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
2153 | NFS4_MAX_SESSIONID_LEN)) { | |
2154 | return elem; | |
2155 | } | |
2156 | } | |
2157 | ||
2158 | dprintk("%s: session not found\n", __func__); | |
2159 | return NULL; | |
2160 | } | |
2161 | ||
d4e19e70 TM |
2162 | static struct nfsd4_session * |
2163 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, | |
2164 | __be32 *ret) | |
2165 | { | |
2166 | struct nfsd4_session *session; | |
2167 | __be32 status = nfserr_badsession; | |
2168 | ||
2169 | session = __find_in_sessionid_hashtbl(sessionid, net); | |
2170 | if (!session) | |
2171 | goto out; | |
2172 | status = nfsd4_get_session_locked(session); | |
2173 | if (status) | |
2174 | session = NULL; | |
2175 | out: | |
2176 | *ret = status; | |
2177 | return session; | |
2178 | } | |
2179 | ||
9089f1b4 | 2180 | /* caller must hold client_lock */ |
7116ed6b | 2181 | static void |
5282fd72 | 2182 | unhash_session(struct nfsd4_session *ses) |
7116ed6b | 2183 | { |
0a880a28 TM |
2184 | struct nfs4_client *clp = ses->se_client; |
2185 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2186 | ||
2187 | lockdep_assert_held(&nn->client_lock); | |
2188 | ||
7116ed6b | 2189 | list_del(&ses->se_hash); |
4c649378 | 2190 | spin_lock(&ses->se_client->cl_lock); |
7116ed6b | 2191 | list_del(&ses->se_perclnt); |
4c649378 | 2192 | spin_unlock(&ses->se_client->cl_lock); |
5282fd72 ME |
2193 | } |
2194 | ||
1da177e4 LT |
2195 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
2196 | static int | |
2c142baa | 2197 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
1da177e4 | 2198 | { |
bbc7f33a BF |
2199 | /* |
2200 | * We're assuming the clid was not given out from a boot | |
2201 | * precisely 2^32 (about 136 years) before this one. That seems | |
2202 | * a safe assumption: | |
2203 | */ | |
2204 | if (clid->cl_boot == (u32)nn->boot_time) | |
1da177e4 | 2205 | return 0; |
dd5e3fbc | 2206 | trace_nfsd_clid_stale(clid); |
1da177e4 LT |
2207 | return 1; |
2208 | } | |
2209 | ||
2210 | /* | |
2211 | * XXX Should we use a slab cache ? | |
2212 | * This type of memory management is somewhat inefficient, but we use it | |
2213 | * anyway since SETCLIENTID is not a common operation. | |
2214 | */ | |
0926c395 DN |
2215 | static struct nfs4_client *alloc_client(struct xdr_netobj name, |
2216 | struct nfsd_net *nn) | |
1da177e4 LT |
2217 | { |
2218 | struct nfs4_client *clp; | |
d4f0489f | 2219 | int i; |
1da177e4 | 2220 | |
4271c2c0 DN |
2221 | if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) { |
2222 | mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); | |
2223 | return NULL; | |
2224 | } | |
9258a2d5 | 2225 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
35bba9a3 BF |
2226 | if (clp == NULL) |
2227 | return NULL; | |
6f4859b8 | 2228 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
d4f0489f TM |
2229 | if (clp->cl_name.data == NULL) |
2230 | goto err_no_name; | |
6da2ec56 KC |
2231 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
2232 | sizeof(struct list_head), | |
2233 | GFP_KERNEL); | |
d4f0489f TM |
2234 | if (!clp->cl_ownerstr_hashtbl) |
2235 | goto err_no_hashtbl; | |
38f080f3 CL |
2236 | clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0); |
2237 | if (!clp->cl_callback_wq) | |
2238 | goto err_no_callback_wq; | |
2239 | ||
d4f0489f TM |
2240 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
2241 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); | |
5694c93e TM |
2242 | INIT_LIST_HEAD(&clp->cl_sessions); |
2243 | idr_init(&clp->cl_stateids); | |
14ed14cc | 2244 | atomic_set(&clp->cl_rpc_users, 0); |
5694c93e | 2245 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
66af2579 | 2246 | clp->cl_state = NFSD4_ACTIVE; |
0926c395 | 2247 | atomic_inc(&nn->nfs4_client_count); |
66af2579 | 2248 | atomic_set(&clp->cl_delegs_in_recall, 0); |
5694c93e TM |
2249 | INIT_LIST_HEAD(&clp->cl_idhash); |
2250 | INIT_LIST_HEAD(&clp->cl_openowners); | |
2251 | INIT_LIST_HEAD(&clp->cl_delegations); | |
2252 | INIT_LIST_HEAD(&clp->cl_lru); | |
5694c93e | 2253 | INIT_LIST_HEAD(&clp->cl_revoked); |
9cf514cc CH |
2254 | #ifdef CONFIG_NFSD_PNFS |
2255 | INIT_LIST_HEAD(&clp->cl_lo_states); | |
2256 | #endif | |
e0639dc5 OK |
2257 | INIT_LIST_HEAD(&clp->async_copies); |
2258 | spin_lock_init(&clp->async_lock); | |
5694c93e TM |
2259 | spin_lock_init(&clp->cl_lock); |
2260 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); | |
1da177e4 | 2261 | return clp; |
38f080f3 CL |
2262 | err_no_callback_wq: |
2263 | kfree(clp->cl_ownerstr_hashtbl); | |
d4f0489f TM |
2264 | err_no_hashtbl: |
2265 | kfree(clp->cl_name.data); | |
2266 | err_no_name: | |
9258a2d5 | 2267 | kmem_cache_free(client_slab, clp); |
d4f0489f | 2268 | return NULL; |
1da177e4 LT |
2269 | } |
2270 | ||
59f8e91b BF |
2271 | static void __free_client(struct kref *k) |
2272 | { | |
e8a79fb1 BF |
2273 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
2274 | struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); | |
59f8e91b BF |
2275 | |
2276 | free_svc_cred(&clp->cl_cred); | |
38f080f3 | 2277 | destroy_workqueue(clp->cl_callback_wq); |
59f8e91b BF |
2278 | kfree(clp->cl_ownerstr_hashtbl); |
2279 | kfree(clp->cl_name.data); | |
79123444 BF |
2280 | kfree(clp->cl_nii_domain.data); |
2281 | kfree(clp->cl_nii_name.data); | |
59f8e91b | 2282 | idr_destroy(&clp->cl_stateids); |
44df6f43 | 2283 | kfree(clp->cl_ra); |
59f8e91b BF |
2284 | kmem_cache_free(client_slab, clp); |
2285 | } | |
2286 | ||
297e57a2 | 2287 | static void drop_client(struct nfs4_client *clp) |
59f8e91b | 2288 | { |
e8a79fb1 | 2289 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
59f8e91b BF |
2290 | } |
2291 | ||
4dd86e15 | 2292 | static void |
1da177e4 LT |
2293 | free_client(struct nfs4_client *clp) |
2294 | { | |
792c95dd BF |
2295 | while (!list_empty(&clp->cl_sessions)) { |
2296 | struct nfsd4_session *ses; | |
2297 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, | |
2298 | se_perclnt); | |
2299 | list_del(&ses->se_perclnt); | |
66b2b9b2 BF |
2300 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
2301 | free_session(ses); | |
792c95dd | 2302 | } |
4cb57e30 | 2303 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
89c905be | 2304 | if (clp->cl_nfsd_dentry) { |
e8a79fb1 | 2305 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
89c905be BF |
2306 | clp->cl_nfsd_dentry = NULL; |
2307 | wake_up_all(&expiry_wq); | |
2308 | } | |
59f8e91b | 2309 | drop_client(clp); |
1da177e4 LT |
2310 | } |
2311 | ||
84d38ac9 | 2312 | /* must be called under the client_lock */ |
4beb345b | 2313 | static void |
84d38ac9 BH |
2314 | unhash_client_locked(struct nfs4_client *clp) |
2315 | { | |
4beb345b | 2316 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
792c95dd BF |
2317 | struct nfsd4_session *ses; |
2318 | ||
0a880a28 TM |
2319 | lockdep_assert_held(&nn->client_lock); |
2320 | ||
4beb345b TM |
2321 | /* Mark the client as expired! */ |
2322 | clp->cl_time = 0; | |
2323 | /* Make it invisible */ | |
2324 | if (!list_empty(&clp->cl_idhash)) { | |
2325 | list_del_init(&clp->cl_idhash); | |
2326 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) | |
2327 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); | |
2328 | else | |
2329 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); | |
2330 | } | |
2331 | list_del_init(&clp->cl_lru); | |
4c649378 | 2332 | spin_lock(&clp->cl_lock); |
792c95dd BF |
2333 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
2334 | list_del_init(&ses->se_hash); | |
4c649378 | 2335 | spin_unlock(&clp->cl_lock); |
84d38ac9 BH |
2336 | } |
2337 | ||
1da177e4 | 2338 | static void |
4beb345b TM |
2339 | unhash_client(struct nfs4_client *clp) |
2340 | { | |
2341 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2342 | ||
2343 | spin_lock(&nn->client_lock); | |
2344 | unhash_client_locked(clp); | |
2345 | spin_unlock(&nn->client_lock); | |
2346 | } | |
2347 | ||
97403d95 JL |
2348 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
2349 | { | |
14ed14cc | 2350 | if (atomic_read(&clp->cl_rpc_users)) |
97403d95 JL |
2351 | return nfserr_jukebox; |
2352 | unhash_client_locked(clp); | |
2353 | return nfs_ok; | |
2354 | } | |
2355 | ||
4beb345b TM |
2356 | static void |
2357 | __destroy_client(struct nfs4_client *clp) | |
1da177e4 | 2358 | { |
0926c395 | 2359 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
68ef3bc3 | 2360 | int i; |
fe0750e5 | 2361 | struct nfs4_openowner *oo; |
1da177e4 | 2362 | struct nfs4_delegation *dp; |
1da177e4 LT |
2363 | struct list_head reaplist; |
2364 | ||
1da177e4 | 2365 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 2366 | spin_lock(&state_lock); |
ea1da636 N |
2367 | while (!list_empty(&clp->cl_delegations)) { |
2368 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
3f29cc82 | 2369 | unhash_delegation_locked(dp, SC_STATUS_CLOSED); |
42690676 | 2370 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 2371 | } |
cdc97505 | 2372 | spin_unlock(&state_lock); |
1da177e4 LT |
2373 | while (!list_empty(&reaplist)) { |
2374 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 2375 | list_del_init(&dp->dl_recall_lru); |
0af6e690 | 2376 | destroy_unhashed_deleg(dp); |
1da177e4 | 2377 | } |
2d4a532d | 2378 | while (!list_empty(&clp->cl_revoked)) { |
c876486b | 2379 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
2d4a532d | 2380 | list_del_init(&dp->dl_recall_lru); |
6011695d | 2381 | nfs4_put_stid(&dp->dl_stid); |
956c4fee | 2382 | } |
ea1da636 | 2383 | while (!list_empty(&clp->cl_openowners)) { |
fe0750e5 | 2384 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
b5971afa | 2385 | nfs4_get_stateowner(&oo->oo_owner); |
fe0750e5 | 2386 | release_openowner(oo); |
1da177e4 | 2387 | } |
68ef3bc3 JL |
2388 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
2389 | struct nfs4_stateowner *so, *tmp; | |
2390 | ||
2391 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], | |
2392 | so_strhash) { | |
2393 | /* Should be no openowners at this point */ | |
2394 | WARN_ON_ONCE(so->so_is_open_owner); | |
2395 | remove_blocked_locks(lockowner(so)); | |
2396 | } | |
2397 | } | |
9cf514cc | 2398 | nfsd4_return_all_client_layouts(clp); |
e0639dc5 | 2399 | nfsd4_shutdown_copy(clp); |
6ff8da08 | 2400 | nfsd4_shutdown_callback(clp); |
84d38ac9 BH |
2401 | if (clp->cl_cb_conn.cb_xprt) |
2402 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); | |
0926c395 | 2403 | atomic_add_unless(&nn->nfs4_client_count, -1, 0); |
3a4ea23d | 2404 | nfsd4_dec_courtesy_client_count(nn, clp); |
221a6876 | 2405 | free_client(clp); |
89c905be | 2406 | wake_up_all(&expiry_wq); |
1da177e4 LT |
2407 | } |
2408 | ||
4beb345b TM |
2409 | static void |
2410 | destroy_client(struct nfs4_client *clp) | |
2411 | { | |
2412 | unhash_client(clp); | |
2413 | __destroy_client(clp); | |
2414 | } | |
2415 | ||
362063a5 SM |
2416 | static void inc_reclaim_complete(struct nfs4_client *clp) |
2417 | { | |
2418 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2419 | ||
2420 | if (!nn->track_reclaim_completes) | |
2421 | return; | |
2422 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) | |
2423 | return; | |
2424 | if (atomic_inc_return(&nn->nr_reclaim_complete) == | |
2425 | nn->reclaim_str_hashtbl_size) { | |
2426 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", | |
2427 | clp->net->ns.inum); | |
2428 | nfsd4_end_grace(nn); | |
2429 | } | |
2430 | } | |
2431 | ||
0d22f68f BF |
2432 | static void expire_client(struct nfs4_client *clp) |
2433 | { | |
4beb345b | 2434 | unhash_client(clp); |
0d22f68f | 2435 | nfsd4_client_record_remove(clp); |
4beb345b | 2436 | __destroy_client(clp); |
0d22f68f BF |
2437 | } |
2438 | ||
35bba9a3 BF |
2439 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
2440 | { | |
2441 | memcpy(target->cl_verifier.data, source->data, | |
2442 | sizeof(target->cl_verifier.data)); | |
1da177e4 LT |
2443 | } |
2444 | ||
35bba9a3 BF |
2445 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
2446 | { | |
1da177e4 LT |
2447 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
2448 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
2449 | } | |
2450 | ||
50043859 BF |
2451 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
2452 | { | |
2f10fdcb N |
2453 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
2454 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, | |
2455 | GFP_KERNEL); | |
9abdda5d CL |
2456 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
2457 | if ((source->cr_principal && !target->cr_principal) || | |
2458 | (source->cr_raw_principal && !target->cr_raw_principal) || | |
2459 | (source->cr_targ_princ && !target->cr_targ_princ)) | |
2f10fdcb | 2460 | return -ENOMEM; |
50043859 | 2461 | |
d5497fc6 | 2462 | target->cr_flavor = source->cr_flavor; |
1da177e4 LT |
2463 | target->cr_uid = source->cr_uid; |
2464 | target->cr_gid = source->cr_gid; | |
2465 | target->cr_group_info = source->cr_group_info; | |
2466 | get_group_info(target->cr_group_info); | |
0dc1531a BF |
2467 | target->cr_gss_mech = source->cr_gss_mech; |
2468 | if (source->cr_gss_mech) | |
2469 | gss_mech_get(source->cr_gss_mech); | |
03a4e1f6 | 2470 | return 0; |
1da177e4 LT |
2471 | } |
2472 | ||
ef17af2a | 2473 | static int |
ac55fdc4 JL |
2474 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
2475 | { | |
ef17af2a RV |
2476 | if (o1->len < o2->len) |
2477 | return -1; | |
2478 | if (o1->len > o2->len) | |
2479 | return 1; | |
2480 | return memcmp(o1->data, o2->data, o1->len); | |
ac55fdc4 JL |
2481 | } |
2482 | ||
1da177e4 | 2483 | static int |
599e0a22 BF |
2484 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
2485 | { | |
2486 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); | |
1da177e4 LT |
2487 | } |
2488 | ||
2489 | static int | |
599e0a22 BF |
2490 | same_clid(clientid_t *cl1, clientid_t *cl2) |
2491 | { | |
2492 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); | |
1da177e4 LT |
2493 | } |
2494 | ||
8fbba96e BF |
2495 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
2496 | { | |
2497 | int i; | |
2498 | ||
2499 | if (g1->ngroups != g2->ngroups) | |
2500 | return false; | |
2501 | for (i=0; i<g1->ngroups; i++) | |
81243eac | 2502 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
8fbba96e BF |
2503 | return false; |
2504 | return true; | |
2505 | } | |
2506 | ||
68eb3508 BF |
2507 | /* |
2508 | * RFC 3530 language requires clid_inuse be returned when the | |
2509 | * "principal" associated with a requests differs from that previously | |
2510 | * used. We use uid, gid's, and gss principal string as our best | |
2511 | * approximation. We also don't want to allow non-gss use of a client | |
2512 | * established using gss: in theory cr_principal should catch that | |
2513 | * change, but in practice cr_principal can be null even in the gss case | |
2514 | * since gssd doesn't always pass down a principal string. | |
2515 | */ | |
2516 | static bool is_gss_cred(struct svc_cred *cr) | |
2517 | { | |
2518 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ | |
2519 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); | |
2520 | } | |
2521 | ||
2522 | ||
5559b50a | 2523 | static bool |
599e0a22 BF |
2524 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
2525 | { | |
68eb3508 | 2526 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
6fab8779 EB |
2527 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
2528 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) | |
8fbba96e BF |
2529 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
2530 | return false; | |
9abdda5d | 2531 | /* XXX: check that cr_targ_princ fields match ? */ |
8fbba96e BF |
2532 | if (cr1->cr_principal == cr2->cr_principal) |
2533 | return true; | |
2534 | if (!cr1->cr_principal || !cr2->cr_principal) | |
2535 | return false; | |
5559b50a | 2536 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
1da177e4 LT |
2537 | } |
2538 | ||
57266a6e BF |
2539 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
2540 | { | |
2541 | struct svc_cred *cr = &rqstp->rq_cred; | |
2542 | u32 service; | |
2543 | ||
c4720591 BF |
2544 | if (!cr->cr_gss_mech) |
2545 | return false; | |
57266a6e BF |
2546 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
2547 | return service == RPC_GSS_SVC_INTEGRITY || | |
2548 | service == RPC_GSS_SVC_PRIVACY; | |
2549 | } | |
2550 | ||
dedeb13f | 2551 | bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
57266a6e BF |
2552 | { |
2553 | struct svc_cred *cr = &rqstp->rq_cred; | |
2554 | ||
2555 | if (!cl->cl_mach_cred) | |
2556 | return true; | |
2557 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) | |
2558 | return false; | |
2559 | if (!svc_rqst_integrity_protected(rqstp)) | |
2560 | return false; | |
414ca017 BF |
2561 | if (cl->cl_cred.cr_raw_principal) |
2562 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, | |
2563 | cr->cr_raw_principal); | |
57266a6e BF |
2564 | if (!cr->cr_principal) |
2565 | return false; | |
2566 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); | |
2567 | } | |
2568 | ||
294ac32e | 2569 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
deda2faa | 2570 | { |
ab4684d1 | 2571 | __be32 verf[2]; |
1da177e4 | 2572 | |
f419992c JL |
2573 | /* |
2574 | * This is opaque to client, so no need to byte-swap. Use | |
2575 | * __force to keep sparse happy | |
2576 | */ | |
9104ae49 | 2577 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
19311aa8 | 2578 | verf[1] = (__force __be32)nn->clverifier_counter++; |
ab4684d1 | 2579 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
1da177e4 LT |
2580 | } |
2581 | ||
294ac32e JL |
2582 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
2583 | { | |
9cc76801 | 2584 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
294ac32e JL |
2585 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
2586 | gen_confirm(clp, nn); | |
2587 | } | |
2588 | ||
4770d722 JL |
2589 | static struct nfs4_stid * |
2590 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) | |
4581d140 | 2591 | { |
3abdb607 BF |
2592 | struct nfs4_stid *ret; |
2593 | ||
2594 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); | |
2595 | if (!ret || !ret->sc_type) | |
2596 | return NULL; | |
2597 | return ret; | |
4d71ab87 BF |
2598 | } |
2599 | ||
4770d722 | 2600 | static struct nfs4_stid * |
3f29cc82 N |
2601 | find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, |
2602 | unsigned short typemask, unsigned short ok_states) | |
f459e453 BF |
2603 | { |
2604 | struct nfs4_stid *s; | |
4d71ab87 | 2605 | |
4770d722 JL |
2606 | spin_lock(&cl->cl_lock); |
2607 | s = find_stateid_locked(cl, t); | |
2d3f9668 | 2608 | if (s != NULL) { |
3f29cc82 N |
2609 | if ((s->sc_status & ~ok_states) == 0 && |
2610 | (typemask & s->sc_type)) | |
a15dfcd5 | 2611 | refcount_inc(&s->sc_count); |
2d3f9668 TM |
2612 | else |
2613 | s = NULL; | |
2614 | } | |
4770d722 JL |
2615 | spin_unlock(&cl->cl_lock); |
2616 | return s; | |
4581d140 BF |
2617 | } |
2618 | ||
a204f25e BF |
2619 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
2620 | { | |
2621 | struct nfsdfs_client *nc; | |
2622 | nc = get_nfsdfs_client(inode); | |
2623 | if (!nc) | |
2624 | return NULL; | |
2625 | return container_of(nc, struct nfs4_client, cl_nfsdfs); | |
2626 | } | |
2627 | ||
169319f1 BF |
2628 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
2629 | { | |
39e1be64 | 2630 | seq_puts(m, "\""); |
c0546391 | 2631 | seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\"); |
39e1be64 | 2632 | seq_puts(m, "\""); |
169319f1 BF |
2633 | } |
2634 | ||
3518c866 DW |
2635 | static const char *cb_state2str(int state) |
2636 | { | |
2637 | switch (state) { | |
2638 | case NFSD4_CB_UP: | |
2639 | return "UP"; | |
2640 | case NFSD4_CB_UNKNOWN: | |
2641 | return "UNKNOWN"; | |
2642 | case NFSD4_CB_DOWN: | |
2643 | return "DOWN"; | |
2644 | case NFSD4_CB_FAULT: | |
2645 | return "FAULT"; | |
2646 | } | |
2647 | return "UNDEFINED"; | |
2648 | } | |
2649 | ||
97ad4031 BF |
2650 | static int client_info_show(struct seq_file *m, void *v) |
2651 | { | |
1d7f6b30 | 2652 | struct inode *inode = file_inode(m->file); |
97ad4031 BF |
2653 | struct nfs4_client *clp; |
2654 | u64 clid; | |
2655 | ||
a204f25e BF |
2656 | clp = get_nfsdfs_clp(inode); |
2657 | if (!clp) | |
97ad4031 | 2658 | return -ENXIO; |
97ad4031 BF |
2659 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
2660 | seq_printf(m, "clientid: 0x%llx\n", clid); | |
169319f1 | 2661 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
e9488d5a DN |
2662 | |
2663 | if (clp->cl_state == NFSD4_COURTESY) | |
2664 | seq_puts(m, "status: courtesy\n"); | |
2665 | else if (clp->cl_state == NFSD4_EXPIRABLE) | |
2666 | seq_puts(m, "status: expirable\n"); | |
2667 | else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) | |
472d155a N |
2668 | seq_puts(m, "status: confirmed\n"); |
2669 | else | |
2670 | seq_puts(m, "status: unconfirmed\n"); | |
e9488d5a DN |
2671 | seq_printf(m, "seconds from last renew: %lld\n", |
2672 | ktime_get_boottime_seconds() - clp->cl_time); | |
39e1be64 | 2673 | seq_puts(m, "name: "); |
169319f1 BF |
2674 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
2675 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); | |
79123444 | 2676 | if (clp->cl_nii_domain.data) { |
39e1be64 | 2677 | seq_puts(m, "Implementation domain: "); |
79123444 BF |
2678 | seq_quote_mem(m, clp->cl_nii_domain.data, |
2679 | clp->cl_nii_domain.len); | |
39e1be64 | 2680 | seq_puts(m, "\nImplementation name: "); |
79123444 | 2681 | seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); |
e29f4703 | 2682 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
79123444 BF |
2683 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
2684 | } | |
3518c866 DW |
2685 | seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state)); |
2686 | seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr); | |
1ac3629b N |
2687 | seq_printf(m, "admin-revoked states: %d\n", |
2688 | atomic_read(&clp->cl_admin_revoked)); | |
97ad4031 BF |
2689 | drop_client(clp); |
2690 | ||
2691 | return 0; | |
2692 | } | |
2693 | ||
1d7f6b30 | 2694 | DEFINE_SHOW_ATTRIBUTE(client_info); |
97ad4031 | 2695 | |
78599c42 BF |
2696 | static void *states_start(struct seq_file *s, loff_t *pos) |
2697 | __acquires(&clp->cl_lock) | |
2698 | { | |
2699 | struct nfs4_client *clp = s->private; | |
2700 | unsigned long id = *pos; | |
2701 | void *ret; | |
2702 | ||
2703 | spin_lock(&clp->cl_lock); | |
2704 | ret = idr_get_next_ul(&clp->cl_stateids, &id); | |
2705 | *pos = id; | |
2706 | return ret; | |
2707 | } | |
2708 | ||
2709 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) | |
2710 | { | |
2711 | struct nfs4_client *clp = s->private; | |
2712 | unsigned long id = *pos; | |
2713 | void *ret; | |
2714 | ||
2715 | id = *pos; | |
2716 | id++; | |
2717 | ret = idr_get_next_ul(&clp->cl_stateids, &id); | |
2718 | *pos = id; | |
2719 | return ret; | |
2720 | } | |
2721 | ||
2722 | static void states_stop(struct seq_file *s, void *v) | |
2723 | __releases(&clp->cl_lock) | |
2724 | { | |
2725 | struct nfs4_client *clp = s->private; | |
2726 | ||
2727 | spin_unlock(&clp->cl_lock); | |
2728 | } | |
2729 | ||
580da465 AG |
2730 | static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) |
2731 | { | |
2732 | seq_printf(s, "filename: \"%pD2\"", f->nf_file); | |
2733 | } | |
2734 | ||
fd4f83fd | 2735 | static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) |
78599c42 | 2736 | { |
427f5f83 | 2737 | struct inode *inode = file_inode(f->nf_file); |
78599c42 BF |
2738 | |
2739 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", | |
2740 | MAJOR(inode->i_sb->s_dev), | |
2741 | MINOR(inode->i_sb->s_dev), | |
2742 | inode->i_ino); | |
2743 | } | |
2744 | ||
2745 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) | |
2746 | { | |
39e1be64 | 2747 | seq_puts(s, "owner: "); |
78599c42 BF |
2748 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
2749 | } | |
2750 | ||
ace7ade4 BF |
2751 | static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) |
2752 | { | |
ee590d25 BF |
2753 | seq_printf(s, "0x%.8x", stid->si_generation); |
2754 | seq_printf(s, "%12phN", &stid->si_opaque); | |
ace7ade4 BF |
2755 | } |
2756 | ||
78599c42 BF |
2757 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
2758 | { | |
2759 | struct nfs4_ol_stateid *ols; | |
2760 | struct nfs4_file *nf; | |
fd4f83fd | 2761 | struct nfsd_file *file; |
78599c42 BF |
2762 | struct nfs4_stateowner *oo; |
2763 | unsigned int access, deny; | |
2764 | ||
78599c42 BF |
2765 | ols = openlockstateid(st); |
2766 | oo = ols->st_stateowner; | |
2767 | nf = st->sc_file; | |
e0aa6510 | 2768 | |
39e1be64 | 2769 | seq_puts(s, "- "); |
ace7ade4 | 2770 | nfs4_show_stateid(s, &st->sc_stateid); |
39e1be64 | 2771 | seq_puts(s, ": { type: open, "); |
78599c42 BF |
2772 | |
2773 | access = bmap_to_share_mode(ols->st_access_bmap); | |
2774 | deny = bmap_to_share_mode(ols->st_deny_bmap); | |
2775 | ||
c4b77edb | 2776 | seq_printf(s, "access: %s%s, ", |
78599c42 BF |
2777 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
2778 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); | |
c4b77edb | 2779 | seq_printf(s, "deny: %s%s, ", |
78599c42 BF |
2780 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
2781 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); | |
2782 | ||
39e1be64 N |
2783 | spin_lock(&nf->fi_lock); |
2784 | file = find_any_file_locked(nf); | |
2785 | if (file) { | |
2786 | nfs4_show_superblock(s, file); | |
2787 | seq_puts(s, ", "); | |
2788 | nfs4_show_fname(s, file); | |
2789 | seq_puts(s, ", "); | |
2790 | } | |
e0aa6510 | 2791 | spin_unlock(&nf->fi_lock); |
39e1be64 | 2792 | nfs4_show_owner(s, oo); |
11b2cfbf N |
2793 | if (st->sc_status & SC_STATUS_ADMIN_REVOKED) |
2794 | seq_puts(s, ", admin-revoked"); | |
39e1be64 | 2795 | seq_puts(s, " }\n"); |
16d36e09 BF |
2796 | return 0; |
2797 | } | |
2798 | ||
2799 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) | |
2800 | { | |
2801 | struct nfs4_ol_stateid *ols; | |
2802 | struct nfs4_file *nf; | |
fd4f83fd | 2803 | struct nfsd_file *file; |
16d36e09 BF |
2804 | struct nfs4_stateowner *oo; |
2805 | ||
2806 | ols = openlockstateid(st); | |
2807 | oo = ols->st_stateowner; | |
2808 | nf = st->sc_file; | |
16d36e09 | 2809 | |
39e1be64 | 2810 | seq_puts(s, "- "); |
ace7ade4 | 2811 | nfs4_show_stateid(s, &st->sc_stateid); |
39e1be64 | 2812 | seq_puts(s, ": { type: lock, "); |
16d36e09 | 2813 | |
39e1be64 N |
2814 | spin_lock(&nf->fi_lock); |
2815 | file = find_any_file_locked(nf); | |
2816 | if (file) { | |
2817 | /* | |
2818 | * Note: a lock stateid isn't really the same thing as a lock, | |
2819 | * it's the locking state held by one owner on a file, and there | |
2820 | * may be multiple (or no) lock ranges associated with it. | |
2821 | * (Same for the matter is true of open stateids.) | |
2822 | */ | |
78599c42 | 2823 | |
39e1be64 N |
2824 | nfs4_show_superblock(s, file); |
2825 | /* XXX: open stateid? */ | |
2826 | seq_puts(s, ", "); | |
2827 | nfs4_show_fname(s, file); | |
2828 | seq_puts(s, ", "); | |
2829 | } | |
16d36e09 | 2830 | nfs4_show_owner(s, oo); |
11b2cfbf N |
2831 | if (st->sc_status & SC_STATUS_ADMIN_REVOKED) |
2832 | seq_puts(s, ", admin-revoked"); | |
39e1be64 | 2833 | seq_puts(s, " }\n"); |
e0aa6510 | 2834 | spin_unlock(&nf->fi_lock); |
78599c42 BF |
2835 | return 0; |
2836 | } | |
2837 | ||
16d36e09 BF |
2838 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
2839 | { | |
2840 | struct nfs4_delegation *ds; | |
2841 | struct nfs4_file *nf; | |
eb82dd39 | 2842 | struct nfsd_file *file; |
16d36e09 BF |
2843 | |
2844 | ds = delegstateid(st); | |
2845 | nf = st->sc_file; | |
16d36e09 | 2846 | |
39e1be64 | 2847 | seq_puts(s, "- "); |
ace7ade4 | 2848 | nfs4_show_stateid(s, &st->sc_stateid); |
39e1be64 | 2849 | seq_puts(s, ": { type: deleg, "); |
16d36e09 | 2850 | |
39e1be64 N |
2851 | seq_printf(s, "access: %s", |
2852 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); | |
16d36e09 BF |
2853 | |
2854 | /* XXX: lease time, whether it's being recalled. */ | |
2855 | ||
39e1be64 N |
2856 | spin_lock(&nf->fi_lock); |
2857 | file = nf->fi_deleg_file; | |
2858 | if (file) { | |
2859 | seq_puts(s, ", "); | |
2860 | nfs4_show_superblock(s, file); | |
2861 | seq_puts(s, ", "); | |
2862 | nfs4_show_fname(s, file); | |
2863 | } | |
e0aa6510 | 2864 | spin_unlock(&nf->fi_lock); |
11b2cfbf N |
2865 | if (st->sc_status & SC_STATUS_ADMIN_REVOKED) |
2866 | seq_puts(s, ", admin-revoked"); | |
2867 | seq_puts(s, " }\n"); | |
16d36e09 BF |
2868 | return 0; |
2869 | } | |
2870 | ||
0c4b62b0 BF |
2871 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
2872 | { | |
2873 | struct nfs4_layout_stateid *ls; | |
eb82dd39 | 2874 | struct nfsd_file *file; |
0c4b62b0 BF |
2875 | |
2876 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); | |
0c4b62b0 | 2877 | |
39e1be64 | 2878 | seq_puts(s, "- "); |
ace7ade4 | 2879 | nfs4_show_stateid(s, &st->sc_stateid); |
39e1be64 | 2880 | seq_puts(s, ": { type: layout"); |
0c4b62b0 BF |
2881 | |
2882 | /* XXX: What else would be useful? */ | |
2883 | ||
1e33e141 N |
2884 | spin_lock(&ls->ls_stid.sc_file->fi_lock); |
2885 | file = ls->ls_file; | |
39e1be64 N |
2886 | if (file) { |
2887 | seq_puts(s, ", "); | |
2888 | nfs4_show_superblock(s, file); | |
2889 | seq_puts(s, ", "); | |
2890 | nfs4_show_fname(s, file); | |
2891 | } | |
1e33e141 | 2892 | spin_unlock(&ls->ls_stid.sc_file->fi_lock); |
11b2cfbf N |
2893 | if (st->sc_status & SC_STATUS_ADMIN_REVOKED) |
2894 | seq_puts(s, ", admin-revoked"); | |
39e1be64 | 2895 | seq_puts(s, " }\n"); |
0c4b62b0 BF |
2896 | |
2897 | return 0; | |
2898 | } | |
2899 | ||
78599c42 BF |
2900 | static int states_show(struct seq_file *s, void *v) |
2901 | { | |
2902 | struct nfs4_stid *st = v; | |
2903 | ||
2904 | switch (st->sc_type) { | |
3f29cc82 | 2905 | case SC_TYPE_OPEN: |
78599c42 | 2906 | return nfs4_show_open(s, st); |
3f29cc82 | 2907 | case SC_TYPE_LOCK: |
16d36e09 | 2908 | return nfs4_show_lock(s, st); |
3f29cc82 | 2909 | case SC_TYPE_DELEG: |
16d36e09 | 2910 | return nfs4_show_deleg(s, st); |
3f29cc82 | 2911 | case SC_TYPE_LAYOUT: |
0c4b62b0 | 2912 | return nfs4_show_layout(s, st); |
78599c42 BF |
2913 | default: |
2914 | return 0; /* XXX: or SEQ_SKIP? */ | |
2915 | } | |
16d36e09 | 2916 | /* XXX: copy stateids? */ |
78599c42 BF |
2917 | } |
2918 | ||
2919 | static struct seq_operations states_seq_ops = { | |
2920 | .start = states_start, | |
2921 | .next = states_next, | |
2922 | .stop = states_stop, | |
2923 | .show = states_show | |
2924 | }; | |
2925 | ||
2926 | static int client_states_open(struct inode *inode, struct file *file) | |
2927 | { | |
78599c42 BF |
2928 | struct seq_file *s; |
2929 | struct nfs4_client *clp; | |
2930 | int ret; | |
2931 | ||
a204f25e BF |
2932 | clp = get_nfsdfs_clp(inode); |
2933 | if (!clp) | |
78599c42 | 2934 | return -ENXIO; |
78599c42 BF |
2935 | |
2936 | ret = seq_open(file, &states_seq_ops); | |
2937 | if (ret) | |
2938 | return ret; | |
2939 | s = file->private_data; | |
2940 | s->private = clp; | |
2941 | return 0; | |
2942 | } | |
2943 | ||
2944 | static int client_opens_release(struct inode *inode, struct file *file) | |
2945 | { | |
2946 | struct seq_file *m = file->private_data; | |
2947 | struct nfs4_client *clp = m->private; | |
2948 | ||
2949 | /* XXX: alternatively, we could get/drop in seq start/stop */ | |
2950 | drop_client(clp); | |
bc1b5acb | 2951 | return seq_release(inode, file); |
78599c42 BF |
2952 | } |
2953 | ||
2954 | static const struct file_operations client_states_fops = { | |
2955 | .open = client_states_open, | |
2956 | .read = seq_read, | |
2957 | .llseek = seq_lseek, | |
2958 | .release = client_opens_release, | |
2959 | }; | |
2960 | ||
89c905be BF |
2961 | /* |
2962 | * Normally we refuse to destroy clients that are in use, but here the | |
2963 | * administrator is telling us to just do it. We also want to wait | |
2964 | * so the caller has a guarantee that the client's locks are gone by | |
2965 | * the time the write returns: | |
2966 | */ | |
297e57a2 | 2967 | static void force_expire_client(struct nfs4_client *clp) |
89c905be BF |
2968 | { |
2969 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2970 | bool already_expired; | |
2971 | ||
2958d2ee CL |
2972 | trace_nfsd_clid_admin_expired(&clp->cl_clientid); |
2973 | ||
f7104cc1 | 2974 | spin_lock(&nn->client_lock); |
89c905be | 2975 | clp->cl_time = 0; |
f7104cc1 | 2976 | spin_unlock(&nn->client_lock); |
89c905be BF |
2977 | |
2978 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); | |
2979 | spin_lock(&nn->client_lock); | |
2980 | already_expired = list_empty(&clp->cl_lru); | |
2981 | if (!already_expired) | |
2982 | unhash_client_locked(clp); | |
2983 | spin_unlock(&nn->client_lock); | |
2984 | ||
2985 | if (!already_expired) | |
2986 | expire_client(clp); | |
2987 | else | |
2988 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); | |
2989 | } | |
2990 | ||
2991 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, | |
2992 | size_t size, loff_t *pos) | |
2993 | { | |
2994 | char *data; | |
2995 | struct nfs4_client *clp; | |
2996 | ||
2997 | data = simple_transaction_get(file, buf, size); | |
2998 | if (IS_ERR(data)) | |
2999 | return PTR_ERR(data); | |
3000 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) | |
3001 | return -EINVAL; | |
3002 | clp = get_nfsdfs_clp(file_inode(file)); | |
3003 | if (!clp) | |
3004 | return -ENXIO; | |
3005 | force_expire_client(clp); | |
3006 | drop_client(clp); | |
3007 | return 7; | |
3008 | } | |
3009 | ||
3010 | static const struct file_operations client_ctl_fops = { | |
3011 | .write = client_ctl_write, | |
3012 | .release = simple_transaction_release, | |
3013 | }; | |
3014 | ||
97ad4031 BF |
3015 | static const struct tree_descr client_files[] = { |
3016 | [0] = {"info", &client_info_fops, S_IRUSR}, | |
78599c42 | 3017 | [1] = {"states", &client_states_fops, S_IRUSR}, |
6cbfad5f | 3018 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
78599c42 | 3019 | [3] = {""}, |
97ad4031 BF |
3020 | }; |
3021 | ||
44df6f43 DN |
3022 | static int |
3023 | nfsd4_cb_recall_any_done(struct nfsd4_callback *cb, | |
3024 | struct rpc_task *task) | |
3025 | { | |
638593be | 3026 | trace_nfsd_cb_recall_any_done(cb, task); |
44df6f43 DN |
3027 | switch (task->tk_status) { |
3028 | case -NFS4ERR_DELAY: | |
3029 | rpc_delay(task, 2 * HZ); | |
3030 | return 0; | |
3031 | default: | |
3032 | return 1; | |
3033 | } | |
3034 | } | |
3035 | ||
3036 | static void | |
3037 | nfsd4_cb_recall_any_release(struct nfsd4_callback *cb) | |
3038 | { | |
3039 | struct nfs4_client *clp = cb->cb_clp; | |
44df6f43 | 3040 | |
44df6f43 | 3041 | clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags); |
10396f4d | 3042 | drop_client(clp); |
44df6f43 DN |
3043 | } |
3044 | ||
c5967721 DN |
3045 | static int |
3046 | nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task) | |
3047 | { | |
3048 | struct nfs4_cb_fattr *ncf = | |
3049 | container_of(cb, struct nfs4_cb_fattr, ncf_getattr); | |
3050 | ||
3051 | ncf->ncf_cb_status = task->tk_status; | |
3052 | switch (task->tk_status) { | |
3053 | case -NFS4ERR_DELAY: | |
3054 | rpc_delay(task, 2 * HZ); | |
3055 | return 0; | |
3056 | default: | |
3057 | return 1; | |
3058 | } | |
3059 | } | |
3060 | ||
3061 | static void | |
3062 | nfsd4_cb_getattr_release(struct nfsd4_callback *cb) | |
3063 | { | |
3064 | struct nfs4_cb_fattr *ncf = | |
3065 | container_of(cb, struct nfs4_cb_fattr, ncf_getattr); | |
3066 | struct nfs4_delegation *dp = | |
3067 | container_of(ncf, struct nfs4_delegation, dl_cb_fattr); | |
3068 | ||
3069 | nfs4_put_stid(&dp->dl_stid); | |
3070 | clear_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags); | |
3071 | wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY); | |
3072 | } | |
3073 | ||
44df6f43 DN |
3074 | static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = { |
3075 | .done = nfsd4_cb_recall_any_done, | |
3076 | .release = nfsd4_cb_recall_any_release, | |
3077 | }; | |
3078 | ||
c5967721 DN |
3079 | static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = { |
3080 | .done = nfsd4_cb_getattr_done, | |
3081 | .release = nfsd4_cb_getattr_release, | |
3082 | }; | |
3083 | ||
3084 | static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf) | |
3085 | { | |
3086 | struct nfs4_delegation *dp = | |
3087 | container_of(ncf, struct nfs4_delegation, dl_cb_fattr); | |
3088 | ||
3089 | if (test_and_set_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags)) | |
3090 | return; | |
3091 | /* set to proper status when nfsd4_cb_getattr_done runs */ | |
3092 | ncf->ncf_cb_status = NFS4ERR_IO; | |
3093 | ||
3094 | refcount_inc(&dp->dl_stid.sc_count); | |
3095 | nfsd4_run_cb(&ncf->ncf_getattr); | |
3096 | } | |
3097 | ||
2216d449 | 3098 | static struct nfs4_client *create_client(struct xdr_netobj name, |
b09333c4 RL |
3099 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
3100 | { | |
3101 | struct nfs4_client *clp; | |
3102 | struct sockaddr *sa = svc_addr(rqstp); | |
03a4e1f6 | 3103 | int ret; |
c212cecf | 3104 | struct net *net = SVC_NET(rqstp); |
e8a79fb1 | 3105 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
472d155a | 3106 | struct dentry *dentries[ARRAY_SIZE(client_files)]; |
b09333c4 | 3107 | |
0926c395 | 3108 | clp = alloc_client(name, nn); |
b09333c4 RL |
3109 | if (clp == NULL) |
3110 | return NULL; | |
3111 | ||
03a4e1f6 BF |
3112 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
3113 | if (ret) { | |
03a4e1f6 | 3114 | free_client(clp); |
03a4e1f6 | 3115 | return NULL; |
b09333c4 | 3116 | } |
e8a79fb1 BF |
3117 | gen_clid(clp, nn); |
3118 | kref_init(&clp->cl_nfsdfs.cl_ref); | |
0162ac2b | 3119 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
20b7d86f | 3120 | clp->cl_time = ktime_get_boottime_seconds(); |
b09333c4 | 3121 | clear_bit(0, &clp->cl_cb_slot_busy); |
b09333c4 | 3122 | copy_verf(clp, verf); |
3bade247 | 3123 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
edd76786 | 3124 | clp->cl_cb_session = NULL; |
c212cecf | 3125 | clp->net = net; |
472d155a N |
3126 | clp->cl_nfsd_dentry = nfsd_client_mkdir( |
3127 | nn, &clp->cl_nfsdfs, | |
3128 | clp->cl_clientid.cl_id - nn->clientid_base, | |
3129 | client_files, dentries); | |
3130 | clp->cl_nfsd_info_dentry = dentries[0]; | |
e8a79fb1 BF |
3131 | if (!clp->cl_nfsd_dentry) { |
3132 | free_client(clp); | |
3133 | return NULL; | |
3134 | } | |
44df6f43 DN |
3135 | clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL); |
3136 | if (!clp->cl_ra) { | |
3137 | free_client(clp); | |
3138 | return NULL; | |
3139 | } | |
3140 | clp->cl_ra_time = 0; | |
3141 | nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops, | |
3142 | NFSPROC4_CLNT_CB_RECALL_ANY); | |
b09333c4 RL |
3143 | return clp; |
3144 | } | |
3145 | ||
fd39ca9a | 3146 | static void |
ac55fdc4 JL |
3147 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
3148 | { | |
3149 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
3150 | struct nfs4_client *clp; | |
3151 | ||
3152 | while (*new) { | |
3153 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); | |
3154 | parent = *new; | |
3155 | ||
3156 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) | |
3157 | new = &((*new)->rb_left); | |
3158 | else | |
3159 | new = &((*new)->rb_right); | |
3160 | } | |
3161 | ||
3162 | rb_link_node(&new_clp->cl_namenode, parent, new); | |
3163 | rb_insert_color(&new_clp->cl_namenode, root); | |
3164 | } | |
3165 | ||
3166 | static struct nfs4_client * | |
3167 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) | |
3168 | { | |
ef17af2a | 3169 | int cmp; |
ac55fdc4 JL |
3170 | struct rb_node *node = root->rb_node; |
3171 | struct nfs4_client *clp; | |
3172 | ||
3173 | while (node) { | |
3174 | clp = rb_entry(node, struct nfs4_client, cl_namenode); | |
3175 | cmp = compare_blob(&clp->cl_name, name); | |
3176 | if (cmp > 0) | |
3177 | node = node->rb_left; | |
3178 | else if (cmp < 0) | |
3179 | node = node->rb_right; | |
3180 | else | |
3181 | return clp; | |
3182 | } | |
3183 | return NULL; | |
3184 | } | |
3185 | ||
3186 | static void | |
3187 | add_to_unconfirmed(struct nfs4_client *clp) | |
1da177e4 LT |
3188 | { |
3189 | unsigned int idhashval; | |
0a7ec377 | 3190 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 3191 | |
0a880a28 TM |
3192 | lockdep_assert_held(&nn->client_lock); |
3193 | ||
ac55fdc4 | 3194 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
a99454aa | 3195 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
1da177e4 | 3196 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
0a7ec377 | 3197 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
3dbacee6 | 3198 | renew_client_locked(clp); |
1da177e4 LT |
3199 | } |
3200 | ||
fd39ca9a | 3201 | static void |
1da177e4 LT |
3202 | move_to_confirmed(struct nfs4_client *clp) |
3203 | { | |
3204 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
8daae4dc | 3205 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 3206 | |
0a880a28 TM |
3207 | lockdep_assert_held(&nn->client_lock); |
3208 | ||
8daae4dc | 3209 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
a99454aa | 3210 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
382a62e7 | 3211 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
934bd07f BF |
3212 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
3213 | trace_nfsd_clid_confirmed(&clp->cl_clientid); | |
3dbacee6 | 3214 | renew_client_locked(clp); |
1da177e4 LT |
3215 | } |
3216 | ||
3217 | static struct nfs4_client * | |
bfa85e83 | 3218 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
1da177e4 LT |
3219 | { |
3220 | struct nfs4_client *clp; | |
3221 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
3222 | ||
bfa85e83 | 3223 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
a50d2ad1 | 3224 | if (same_clid(&clp->cl_clientid, clid)) { |
d15c077e BF |
3225 | if ((bool)clp->cl_minorversion != sessions) |
3226 | return NULL; | |
3dbacee6 | 3227 | renew_client_locked(clp); |
1da177e4 | 3228 | return clp; |
a50d2ad1 | 3229 | } |
1da177e4 LT |
3230 | } |
3231 | return NULL; | |
3232 | } | |
3233 | ||
bfa85e83 BF |
3234 | static struct nfs4_client * |
3235 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) | |
3236 | { | |
3237 | struct list_head *tbl = nn->conf_id_hashtbl; | |
3238 | ||
0a880a28 | 3239 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 BF |
3240 | return find_client_in_id_table(tbl, clid, sessions); |
3241 | } | |
3242 | ||
1da177e4 | 3243 | static struct nfs4_client * |
0a7ec377 | 3244 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
1da177e4 | 3245 | { |
bfa85e83 | 3246 | struct list_head *tbl = nn->unconf_id_hashtbl; |
1da177e4 | 3247 | |
0a880a28 | 3248 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 | 3249 | return find_client_in_id_table(tbl, clid, sessions); |
1da177e4 LT |
3250 | } |
3251 | ||
6e5f15c9 | 3252 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
a1bcecd2 | 3253 | { |
6e5f15c9 | 3254 | return clp->cl_exchange_flags != 0; |
e203d506 | 3255 | } |
a1bcecd2 | 3256 | |
28ce6054 | 3257 | static struct nfs4_client * |
382a62e7 | 3258 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 3259 | { |
0a880a28 | 3260 | lockdep_assert_held(&nn->client_lock); |
382a62e7 | 3261 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
28ce6054 N |
3262 | } |
3263 | ||
3264 | static struct nfs4_client * | |
a99454aa | 3265 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 3266 | { |
0a880a28 | 3267 | lockdep_assert_held(&nn->client_lock); |
a99454aa | 3268 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
28ce6054 N |
3269 | } |
3270 | ||
fd39ca9a | 3271 | static void |
6f3d772f | 3272 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
1da177e4 | 3273 | { |
07263f1e | 3274 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
6f3d772f TU |
3275 | struct sockaddr *sa = svc_addr(rqstp); |
3276 | u32 scopeid = rpc_get_scope_id(sa); | |
7077ecba JL |
3277 | unsigned short expected_family; |
3278 | ||
3279 | /* Currently, we only support tcp and tcp6 for the callback channel */ | |
3280 | if (se->se_callback_netid_len == 3 && | |
3281 | !memcmp(se->se_callback_netid_val, "tcp", 3)) | |
3282 | expected_family = AF_INET; | |
3283 | else if (se->se_callback_netid_len == 4 && | |
3284 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) | |
3285 | expected_family = AF_INET6; | |
3286 | else | |
1da177e4 LT |
3287 | goto out_err; |
3288 | ||
c212cecf | 3289 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
aa9a4ec7 | 3290 | se->se_callback_addr_len, |
07263f1e BF |
3291 | (struct sockaddr *)&conn->cb_addr, |
3292 | sizeof(conn->cb_addr)); | |
aa9a4ec7 | 3293 | |
07263f1e | 3294 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
1da177e4 | 3295 | goto out_err; |
aa9a4ec7 | 3296 | |
07263f1e BF |
3297 | if (conn->cb_addr.ss_family == AF_INET6) |
3298 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; | |
fbf4665f | 3299 | |
07263f1e BF |
3300 | conn->cb_prog = se->se_callback_prog; |
3301 | conn->cb_ident = se->se_callback_ident; | |
849a1cf1 | 3302 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
1eace0d1 | 3303 | trace_nfsd_cb_args(clp, conn); |
1da177e4 LT |
3304 | return; |
3305 | out_err: | |
07263f1e BF |
3306 | conn->cb_addr.ss_family = AF_UNSPEC; |
3307 | conn->cb_addrlen = 0; | |
1eace0d1 | 3308 | trace_nfsd_cb_nodelegs(clp); |
1da177e4 LT |
3309 | return; |
3310 | } | |
3311 | ||
074fe897 | 3312 | /* |
067e1ace | 3313 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
074fe897 | 3314 | */ |
b607664e | 3315 | static void |
074fe897 | 3316 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
074fe897 | 3317 | { |
bddfdbcd | 3318 | struct xdr_buf *buf = resp->xdr->buf; |
557ce264 AA |
3319 | struct nfsd4_slot *slot = resp->cstate.slot; |
3320 | unsigned int base; | |
074fe897 | 3321 | |
557ce264 | 3322 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 3323 | |
085def3a | 3324 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
557ce264 AA |
3325 | slot->sl_opcnt = resp->opcnt; |
3326 | slot->sl_status = resp->cstate.status; | |
53da6a53 BF |
3327 | free_svc_cred(&slot->sl_cred); |
3328 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); | |
074fe897 | 3329 | |
085def3a BF |
3330 | if (!nfsd4_cache_this(resp)) { |
3331 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; | |
bf864a31 | 3332 | return; |
074fe897 | 3333 | } |
085def3a BF |
3334 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
3335 | ||
f5236013 BF |
3336 | base = resp->cstate.data_offset; |
3337 | slot->sl_datalen = buf->len - base; | |
3338 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) | |
d3f03403 DC |
3339 | WARN(1, "%s: sessions DRC could not cache compound\n", |
3340 | __func__); | |
557ce264 | 3341 | return; |
074fe897 AA |
3342 | } |
3343 | ||
3344 | /* | |
abfabf8c AA |
3345 | * Encode the replay sequence operation from the slot values. |
3346 | * If cachethis is FALSE encode the uncached rep error on the next | |
3347 | * operation which sets resp->p and increments resp->opcnt for | |
3348 | * nfs4svc_encode_compoundres. | |
074fe897 | 3349 | * |
074fe897 | 3350 | */ |
abfabf8c AA |
3351 | static __be32 |
3352 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, | |
3353 | struct nfsd4_compoundres *resp) | |
074fe897 | 3354 | { |
abfabf8c AA |
3355 | struct nfsd4_op *op; |
3356 | struct nfsd4_slot *slot = resp->cstate.slot; | |
bf864a31 | 3357 | |
abfabf8c AA |
3358 | /* Encode the replayed sequence operation */ |
3359 | op = &args->ops[resp->opcnt - 1]; | |
3360 | nfsd4_encode_operation(resp, op); | |
bf864a31 | 3361 | |
085def3a BF |
3362 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
3363 | return op->status; | |
3364 | if (args->opcnt == 1) { | |
3365 | /* | |
3366 | * The original operation wasn't a solo sequence--we | |
3367 | * always cache those--so this retry must not match the | |
3368 | * original: | |
3369 | */ | |
3370 | op->status = nfserr_seq_false_retry; | |
3371 | } else { | |
abfabf8c AA |
3372 | op = &args->ops[resp->opcnt++]; |
3373 | op->status = nfserr_retry_uncached_rep; | |
3374 | nfsd4_encode_operation(resp, op); | |
074fe897 | 3375 | } |
abfabf8c | 3376 | return op->status; |
074fe897 AA |
3377 | } |
3378 | ||
3379 | /* | |
557ce264 AA |
3380 | * The sequence operation is not cached because we can use the slot and |
3381 | * session values. | |
074fe897 | 3382 | */ |
3ca2eb98 | 3383 | static __be32 |
bf864a31 AA |
3384 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
3385 | struct nfsd4_sequence *seq) | |
074fe897 | 3386 | { |
557ce264 | 3387 | struct nfsd4_slot *slot = resp->cstate.slot; |
bddfdbcd | 3388 | struct xdr_stream *xdr = resp->xdr; |
f5236013 | 3389 | __be32 *p; |
074fe897 AA |
3390 | __be32 status; |
3391 | ||
557ce264 | 3392 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 3393 | |
abfabf8c | 3394 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
0da7b19c | 3395 | if (status) |
abfabf8c | 3396 | return status; |
074fe897 | 3397 | |
f5236013 BF |
3398 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
3399 | if (!p) { | |
3400 | WARN_ON_ONCE(1); | |
3401 | return nfserr_serverfault; | |
3402 | } | |
3403 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); | |
3404 | xdr_commit_encode(xdr); | |
074fe897 | 3405 | |
557ce264 | 3406 | resp->opcnt = slot->sl_opcnt; |
f5236013 | 3407 | return slot->sl_status; |
074fe897 AA |
3408 | } |
3409 | ||
0733d213 AA |
3410 | /* |
3411 | * Set the exchange_id flags returned by the server. | |
3412 | */ | |
3413 | static void | |
3414 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) | |
3415 | { | |
9cf514cc CH |
3416 | #ifdef CONFIG_NFSD_PNFS |
3417 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; | |
3418 | #else | |
0733d213 | 3419 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
9cf514cc | 3420 | #endif |
0733d213 AA |
3421 | |
3422 | /* Referrals are supported, Migration is not. */ | |
3423 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; | |
3424 | ||
3425 | /* set the wire flags to return to client. */ | |
3426 | clid->flags = new->cl_exchange_flags; | |
3427 | } | |
3428 | ||
4eaea134 BF |
3429 | static bool client_has_openowners(struct nfs4_client *clp) |
3430 | { | |
3431 | struct nfs4_openowner *oo; | |
3432 | ||
3433 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { | |
3434 | if (!list_empty(&oo->oo_owner.so_stateids)) | |
3435 | return true; | |
3436 | } | |
3437 | return false; | |
3438 | } | |
3439 | ||
631fc9ea BF |
3440 | static bool client_has_state(struct nfs4_client *clp) |
3441 | { | |
4eaea134 | 3442 | return client_has_openowners(clp) |
47e970be KM |
3443 | #ifdef CONFIG_NFSD_PNFS |
3444 | || !list_empty(&clp->cl_lo_states) | |
3445 | #endif | |
6eccece9 | 3446 | || !list_empty(&clp->cl_delegations) |
e0639dc5 OK |
3447 | || !list_empty(&clp->cl_sessions) |
3448 | || !list_empty(&clp->async_copies); | |
631fc9ea BF |
3449 | } |
3450 | ||
79123444 BF |
3451 | static __be32 copy_impl_id(struct nfs4_client *clp, |
3452 | struct nfsd4_exchange_id *exid) | |
3453 | { | |
3454 | if (!exid->nii_domain.data) | |
3455 | return 0; | |
3456 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); | |
3457 | if (!clp->cl_nii_domain.data) | |
3458 | return nfserr_jukebox; | |
3459 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); | |
3460 | if (!clp->cl_nii_name.data) | |
3461 | return nfserr_jukebox; | |
e29f4703 | 3462 | clp->cl_nii_time = exid->nii_time; |
79123444 BF |
3463 | return 0; |
3464 | } | |
3465 | ||
069b6ad4 | 3466 | __be32 |
eb69853d CH |
3467 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3468 | union nfsd4_op_u *u) | |
069b6ad4 | 3469 | { |
eb69853d | 3470 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
3dbacee6 TM |
3471 | struct nfs4_client *conf, *new; |
3472 | struct nfs4_client *unconf = NULL; | |
57b7b43b | 3473 | __be32 status; |
363168b4 | 3474 | char addr_str[INET6_ADDRSTRLEN]; |
0733d213 | 3475 | nfs4_verifier verf = exid->verifier; |
363168b4 | 3476 | struct sockaddr *sa = svc_addr(rqstp); |
83e08fd4 | 3477 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
c212cecf | 3478 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
0733d213 | 3479 | |
363168b4 | 3480 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
0733d213 | 3481 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
523ec6ed | 3482 | "ip_addr=%s flags %x, spa_how %u\n", |
0733d213 | 3483 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
363168b4 | 3484 | addr_str, exid->flags, exid->spa_how); |
0733d213 | 3485 | |
a084daf5 | 3486 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
0733d213 AA |
3487 | return nfserr_inval; |
3488 | ||
50c7b948 BF |
3489 | new = create_client(exid->clname, rqstp, &verf); |
3490 | if (new == NULL) | |
3491 | return nfserr_jukebox; | |
79123444 BF |
3492 | status = copy_impl_id(new, exid); |
3493 | if (status) | |
3494 | goto out_nolock; | |
50c7b948 | 3495 | |
0733d213 | 3496 | switch (exid->spa_how) { |
57266a6e | 3497 | case SP4_MACH_CRED: |
ed941643 AE |
3498 | exid->spo_must_enforce[0] = 0; |
3499 | exid->spo_must_enforce[1] = ( | |
3500 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
3501 | 1 << (OP_EXCHANGE_ID - 32) | | |
3502 | 1 << (OP_CREATE_SESSION - 32) | | |
3503 | 1 << (OP_DESTROY_SESSION - 32) | | |
3504 | 1 << (OP_DESTROY_CLIENTID - 32)); | |
3505 | ||
3506 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | | |
3507 | 1 << (OP_OPEN_DOWNGRADE) | | |
3508 | 1 << (OP_LOCKU) | | |
3509 | 1 << (OP_DELEGRETURN)); | |
3510 | ||
3511 | exid->spo_must_allow[1] &= ( | |
3512 | 1 << (OP_TEST_STATEID - 32) | | |
3513 | 1 << (OP_FREE_STATEID - 32)); | |
50c7b948 BF |
3514 | if (!svc_rqst_integrity_protected(rqstp)) { |
3515 | status = nfserr_inval; | |
3516 | goto out_nolock; | |
920dd9bb BF |
3517 | } |
3518 | /* | |
3519 | * Sometimes userspace doesn't give us a principal. | |
3520 | * Which is a bug, really. Anyway, we can't enforce | |
3521 | * MACH_CRED in that case, better to give up now: | |
3522 | */ | |
414ca017 BF |
3523 | if (!new->cl_cred.cr_principal && |
3524 | !new->cl_cred.cr_raw_principal) { | |
920dd9bb BF |
3525 | status = nfserr_serverfault; |
3526 | goto out_nolock; | |
50c7b948 BF |
3527 | } |
3528 | new->cl_mach_cred = true; | |
76c50eb7 | 3529 | break; |
0733d213 AA |
3530 | case SP4_NONE: |
3531 | break; | |
063b0fb9 BF |
3532 | default: /* checked by xdr code */ |
3533 | WARN_ON_ONCE(1); | |
df561f66 | 3534 | fallthrough; |
0733d213 | 3535 | case SP4_SSV: |
8edf4b02 KM |
3536 | status = nfserr_encr_alg_unsupp; |
3537 | goto out_nolock; | |
0733d213 AA |
3538 | } |
3539 | ||
2dbb269d | 3540 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
3dbacee6 | 3541 | spin_lock(&nn->client_lock); |
382a62e7 | 3542 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
0733d213 | 3543 | if (conf) { |
83e08fd4 BF |
3544 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
3545 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); | |
3546 | ||
136e658d BF |
3547 | if (update) { |
3548 | if (!clp_used_exchangeid(conf)) { /* buggy client */ | |
2dbb269d | 3549 | status = nfserr_inval; |
1a308118 BF |
3550 | goto out; |
3551 | } | |
dedeb13f | 3552 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
57266a6e BF |
3553 | status = nfserr_wrong_cred; |
3554 | goto out; | |
3555 | } | |
136e658d | 3556 | if (!creds_match) { /* case 9 */ |
ea236d07 | 3557 | status = nfserr_perm; |
136e658d BF |
3558 | goto out; |
3559 | } | |
3560 | if (!verfs_match) { /* case 8 */ | |
0733d213 AA |
3561 | status = nfserr_not_same; |
3562 | goto out; | |
3563 | } | |
136e658d BF |
3564 | /* case 6 */ |
3565 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
e8f80c55 | 3566 | trace_nfsd_clid_confirmed_r(conf); |
136e658d | 3567 | goto out_copy; |
0733d213 | 3568 | } |
136e658d | 3569 | if (!creds_match) { /* case 3 */ |
631fc9ea BF |
3570 | if (client_has_state(conf)) { |
3571 | status = nfserr_clid_inuse; | |
27787733 | 3572 | trace_nfsd_clid_cred_mismatch(conf, rqstp); |
0733d213 AA |
3573 | goto out; |
3574 | } | |
0733d213 AA |
3575 | goto out_new; |
3576 | } | |
136e658d | 3577 | if (verfs_match) { /* case 2 */ |
0f1ba0ef | 3578 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
e8f80c55 | 3579 | trace_nfsd_clid_confirmed_r(conf); |
136e658d BF |
3580 | goto out_copy; |
3581 | } | |
3582 | /* case 5, client reboot */ | |
744ea54c | 3583 | trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf); |
3dbacee6 | 3584 | conf = NULL; |
136e658d | 3585 | goto out_new; |
6ddbbbfe MS |
3586 | } |
3587 | ||
2dbb269d | 3588 | if (update) { /* case 7 */ |
6ddbbbfe MS |
3589 | status = nfserr_noent; |
3590 | goto out; | |
0733d213 AA |
3591 | } |
3592 | ||
e8f80c55 | 3593 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
2dbb269d | 3594 | if (unconf) /* case 4, possible retry or client restart */ |
3dbacee6 | 3595 | unhash_client_locked(unconf); |
0733d213 | 3596 | |
e8f80c55 CL |
3597 | /* case 1, new owner ID */ |
3598 | trace_nfsd_clid_fresh(new); | |
3599 | ||
0733d213 | 3600 | out_new: |
fd699b8a JL |
3601 | if (conf) { |
3602 | status = mark_client_expired_locked(conf); | |
3603 | if (status) | |
3604 | goto out; | |
2958d2ee | 3605 | trace_nfsd_clid_replaced(&conf->cl_clientid); |
fd699b8a | 3606 | } |
4f540e29 | 3607 | new->cl_minorversion = cstate->minorversion; |
ed941643 AE |
3608 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
3609 | new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; | |
0733d213 | 3610 | |
e4469c6c CL |
3611 | /* Contrived initial CREATE_SESSION response */ |
3612 | new->cl_cs_slot.sl_status = nfserr_seq_misordered; | |
3613 | ||
ac55fdc4 | 3614 | add_to_unconfirmed(new); |
3dbacee6 | 3615 | swap(new, conf); |
0733d213 | 3616 | out_copy: |
5cc40fd7 TM |
3617 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
3618 | exid->clientid.cl_id = conf->cl_clientid.cl_id; | |
0733d213 | 3619 | |
5cc40fd7 TM |
3620 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
3621 | nfsd4_set_ex_flags(conf, exid); | |
0733d213 AA |
3622 | |
3623 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", | |
5cc40fd7 | 3624 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
0733d213 AA |
3625 | status = nfs_ok; |
3626 | ||
3627 | out: | |
3dbacee6 | 3628 | spin_unlock(&nn->client_lock); |
50c7b948 | 3629 | out_nolock: |
5cc40fd7 | 3630 | if (new) |
3dbacee6 | 3631 | expire_client(new); |
e8f80c55 CL |
3632 | if (unconf) { |
3633 | trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); | |
3dbacee6 | 3634 | expire_client(unconf); |
e8f80c55 | 3635 | } |
0733d213 | 3636 | return status; |
069b6ad4 AA |
3637 | } |
3638 | ||
57b7b43b | 3639 | static __be32 |
88e588d5 | 3640 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
b85d4c01 | 3641 | { |
88e588d5 AA |
3642 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
3643 | slot_seqid); | |
b85d4c01 BH |
3644 | |
3645 | /* The slot is in use, and no response has been sent. */ | |
88e588d5 AA |
3646 | if (slot_inuse) { |
3647 | if (seqid == slot_seqid) | |
b85d4c01 BH |
3648 | return nfserr_jukebox; |
3649 | else | |
3650 | return nfserr_seq_misordered; | |
3651 | } | |
f6d82485 | 3652 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
88e588d5 | 3653 | if (likely(seqid == slot_seqid + 1)) |
b85d4c01 | 3654 | return nfs_ok; |
88e588d5 | 3655 | if (seqid == slot_seqid) |
b85d4c01 | 3656 | return nfserr_replay_cache; |
b85d4c01 BH |
3657 | return nfserr_seq_misordered; |
3658 | } | |
3659 | ||
49557cc7 AA |
3660 | /* |
3661 | * Cache the create session result into the create session single DRC | |
3662 | * slot cache by saving the xdr structure. sl_seqid has been set. | |
3663 | * Do this for solo or embedded create session operations. | |
3664 | */ | |
3665 | static void | |
3666 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, | |
57b7b43b | 3667 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
49557cc7 AA |
3668 | { |
3669 | slot->sl_status = nfserr; | |
3670 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); | |
3671 | } | |
3672 | ||
3673 | static __be32 | |
3674 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, | |
3675 | struct nfsd4_clid_slot *slot) | |
3676 | { | |
3677 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); | |
3678 | return slot->sl_status; | |
3679 | } | |
3680 | ||
1b74c25b MJ |
3681 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
3682 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ | |
3683 | 1 + /* MIN tag is length with zero, only length */ \ | |
3684 | 3 + /* version, opcount, opcode */ \ | |
3685 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
3686 | /* seqid, slotID, slotID, cache */ \ | |
3687 | 4 ) * sizeof(__be32)) | |
3688 | ||
3689 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ | |
3690 | 2 + /* verifier: AUTH_NULL, length 0 */\ | |
3691 | 1 + /* status */ \ | |
3692 | 1 + /* MIN tag is length with zero, only length */ \ | |
3693 | 3 + /* opcount, opcode, opstatus*/ \ | |
3694 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
3695 | /* seqid, slotID, slotID, slotID, status */ \ | |
3696 | 5 ) * sizeof(__be32)) | |
3697 | ||
55c760cf | 3698 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
1b74c25b | 3699 | { |
55c760cf BF |
3700 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
3701 | ||
373cd409 BF |
3702 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
3703 | return nfserr_toosmall; | |
3704 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) | |
3705 | return nfserr_toosmall; | |
55c760cf BF |
3706 | ca->headerpadsz = 0; |
3707 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); | |
3708 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); | |
3709 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); | |
3710 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, | |
3711 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); | |
3712 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); | |
3713 | /* | |
3714 | * Note decreasing slot size below client's request may make it | |
3715 | * difficult for client to function correctly, whereas | |
3716 | * decreasing the number of slots will (just?) affect | |
3717 | * performance. When short on memory we therefore prefer to | |
3718 | * decrease number of slots instead of their size. Clients that | |
3719 | * request larger slots than they need will get poor results: | |
7f49fd5d N |
3720 | * Note that we always allow at least one slot, because our |
3721 | * accounting is soft and provides no guarantees either way. | |
55c760cf | 3722 | */ |
2030ca56 | 3723 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
55c760cf | 3724 | |
373cd409 | 3725 | return nfs_ok; |
1b74c25b MJ |
3726 | } |
3727 | ||
4500632f CL |
3728 | /* |
3729 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. | |
3730 | * These are based on similar macros in linux/sunrpc/msg_prot.h . | |
3731 | */ | |
3732 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ | |
3733 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) | |
3734 | ||
3735 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ | |
3736 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) | |
3737 | ||
8a891633 | 3738 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
4500632f | 3739 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
8a891633 | 3740 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
4500632f CL |
3741 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
3742 | sizeof(__be32)) | |
8a891633 | 3743 | |
06b332a5 | 3744 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
1b74c25b | 3745 | { |
06b332a5 BF |
3746 | ca->headerpadsz = 0; |
3747 | ||
8a891633 | 3748 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
06b332a5 | 3749 | return nfserr_toosmall; |
8a891633 | 3750 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
06b332a5 BF |
3751 | return nfserr_toosmall; |
3752 | ca->maxresp_cached = 0; | |
3753 | if (ca->maxops < 2) | |
3754 | return nfserr_toosmall; | |
3755 | ||
3756 | return nfs_ok; | |
1b74c25b MJ |
3757 | } |
3758 | ||
b78724b7 BF |
3759 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
3760 | { | |
3761 | switch (cbs->flavor) { | |
3762 | case RPC_AUTH_NULL: | |
3763 | case RPC_AUTH_UNIX: | |
3764 | return nfs_ok; | |
3765 | default: | |
3766 | /* | |
3767 | * GSS case: the spec doesn't allow us to return this | |
3768 | * error. But it also doesn't allow us not to support | |
3769 | * GSS. | |
3770 | * I'd rather this fail hard than return some error the | |
3771 | * client might think it can already handle: | |
3772 | */ | |
3773 | return nfserr_encr_alg_unsupp; | |
3774 | } | |
3775 | } | |
3776 | ||
069b6ad4 AA |
3777 | __be32 |
3778 | nfsd4_create_session(struct svc_rqst *rqstp, | |
eb69853d | 3779 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
069b6ad4 | 3780 | { |
eb69853d | 3781 | struct nfsd4_create_session *cr_ses = &u->create_session; |
363168b4 | 3782 | struct sockaddr *sa = svc_addr(rqstp); |
ec6b5d7b | 3783 | struct nfs4_client *conf, *unconf; |
e4469c6c | 3784 | struct nfsd4_clid_slot *cs_slot; |
d20c11d8 | 3785 | struct nfs4_client *old = NULL; |
ac7c46f2 | 3786 | struct nfsd4_session *new; |
81f0b2a4 | 3787 | struct nfsd4_conn *conn; |
57b7b43b | 3788 | __be32 status = 0; |
8daae4dc | 3789 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
ec6b5d7b | 3790 | |
a62573dc MJ |
3791 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
3792 | return nfserr_inval; | |
b78724b7 BF |
3793 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
3794 | if (status) | |
3795 | return status; | |
55c760cf | 3796 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
06b332a5 BF |
3797 | if (status) |
3798 | return status; | |
3799 | status = check_backchannel_attrs(&cr_ses->back_channel); | |
373cd409 | 3800 | if (status) |
f403e450 | 3801 | goto out_release_drc_mem; |
81f0b2a4 | 3802 | status = nfserr_jukebox; |
60810e54 | 3803 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
55c760cf BF |
3804 | if (!new) |
3805 | goto out_release_drc_mem; | |
81f0b2a4 BF |
3806 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
3807 | if (!conn) | |
3808 | goto out_free_session; | |
a62573dc | 3809 | |
d20c11d8 | 3810 | spin_lock(&nn->client_lock); |
b910544a CL |
3811 | |
3812 | /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */ | |
0a7ec377 | 3813 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
8daae4dc | 3814 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
e4469c6c CL |
3815 | if (!conf && !unconf) { |
3816 | status = nfserr_stale_clientid; | |
3817 | goto out_free_conn; | |
3818 | } | |
ec6b5d7b | 3819 | |
b910544a | 3820 | /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */ |
e4469c6c | 3821 | if (conf) |
49557cc7 | 3822 | cs_slot = &conf->cl_cs_slot; |
e4469c6c CL |
3823 | else |
3824 | cs_slot = &unconf->cl_cs_slot; | |
3825 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
99dc2ef0 CL |
3826 | switch (status) { |
3827 | case nfs_ok: | |
3828 | cs_slot->sl_seqid++; | |
3829 | cr_ses->seqid = cs_slot->sl_seqid; | |
3830 | break; | |
3831 | case nfserr_replay_cache: | |
3832 | status = nfsd4_replay_create_session(cr_ses, cs_slot); | |
3833 | fallthrough; | |
3834 | case nfserr_jukebox: | |
3835 | /* The server MUST NOT cache NFS4ERR_DELAY */ | |
3836 | goto out_free_conn; | |
3837 | default: | |
e4469c6c CL |
3838 | goto out_cache_error; |
3839 | } | |
e4469c6c | 3840 | |
b910544a | 3841 | /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */ |
e4469c6c CL |
3842 | if (conf) { |
3843 | status = nfserr_wrong_cred; | |
3844 | if (!nfsd4_mach_creds_match(conf, rqstp)) | |
3845 | goto out_cache_error; | |
3846 | } else { | |
27787733 | 3847 | status = nfserr_clid_inuse; |
ec6b5d7b | 3848 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
363168b4 | 3849 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
27787733 | 3850 | trace_nfsd_clid_cred_mismatch(unconf, rqstp); |
e4469c6c | 3851 | goto out_cache_error; |
ec6b5d7b | 3852 | } |
57266a6e | 3853 | status = nfserr_wrong_cred; |
dedeb13f | 3854 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
e4469c6c | 3855 | goto out_cache_error; |
382a62e7 | 3856 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
221a6876 | 3857 | if (old) { |
d20c11d8 | 3858 | status = mark_client_expired_locked(old); |
99dc2ef0 CL |
3859 | if (status) |
3860 | goto out_expired_error; | |
2958d2ee | 3861 | trace_nfsd_clid_replaced(&old->cl_clientid); |
221a6876 | 3862 | } |
8f9d3d3b | 3863 | move_to_confirmed(unconf); |
ec6b5d7b | 3864 | conf = unconf; |
ec6b5d7b | 3865 | } |
b910544a CL |
3866 | |
3867 | /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */ | |
81f0b2a4 | 3868 | status = nfs_ok; |
4ce85c8c | 3869 | /* Persistent sessions are not supported */ |
408b79bc | 3870 | cr_ses->flags &= ~SESSION4_PERSIST; |
4ce85c8c | 3871 | /* Upshifting from TCP to RDMA is not supported */ |
408b79bc BF |
3872 | cr_ses->flags &= ~SESSION4_RDMA; |
3873 | ||
81f0b2a4 | 3874 | init_session(rqstp, new, conf, cr_ses); |
d20c11d8 | 3875 | nfsd4_get_session_locked(new); |
81f0b2a4 | 3876 | |
ac7c46f2 | 3877 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
ec6b5d7b | 3878 | NFS4_MAX_SESSIONID_LEN); |
ec6b5d7b | 3879 | |
d20c11d8 | 3880 | /* cache solo and embedded create sessions under the client_lock */ |
49557cc7 | 3881 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
d20c11d8 | 3882 | spin_unlock(&nn->client_lock); |
934bd07f BF |
3883 | if (conf == unconf) |
3884 | fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); | |
d20c11d8 JL |
3885 | /* init connection and backchannel */ |
3886 | nfsd4_init_conn(rqstp, conn, new); | |
3887 | nfsd4_put_session(new); | |
d20c11d8 JL |
3888 | if (old) |
3889 | expire_client(old); | |
ec6b5d7b | 3890 | return status; |
e4469c6c | 3891 | |
99dc2ef0 CL |
3892 | out_expired_error: |
3893 | old = NULL; | |
3894 | /* | |
3895 | * Revert the slot seq_nr change so the server will process | |
3896 | * the client's resend instead of returning a cached response. | |
3897 | */ | |
3898 | if (status == nfserr_jukebox) { | |
3899 | cs_slot->sl_seqid--; | |
3900 | cr_ses->seqid = cs_slot->sl_seqid; | |
3901 | goto out_free_conn; | |
3902 | } | |
e4469c6c CL |
3903 | out_cache_error: |
3904 | nfsd4_cache_create_session(cr_ses, cs_slot, status); | |
81f0b2a4 | 3905 | out_free_conn: |
d20c11d8 | 3906 | spin_unlock(&nn->client_lock); |
81f0b2a4 | 3907 | free_conn(conn); |
d20c11d8 JL |
3908 | if (old) |
3909 | expire_client(old); | |
81f0b2a4 BF |
3910 | out_free_session: |
3911 | __free_session(new); | |
55c760cf BF |
3912 | out_release_drc_mem: |
3913 | nfsd4_put_drc_mem(&cr_ses->fore_channel); | |
1ca50792 | 3914 | return status; |
069b6ad4 AA |
3915 | } |
3916 | ||
1d1bc8f2 BF |
3917 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
3918 | { | |
3919 | switch (*dir) { | |
3920 | case NFS4_CDFC4_FORE: | |
3921 | case NFS4_CDFC4_BACK: | |
3922 | return nfs_ok; | |
3923 | case NFS4_CDFC4_FORE_OR_BOTH: | |
3924 | case NFS4_CDFC4_BACK_OR_BOTH: | |
3925 | *dir = NFS4_CDFC4_BOTH; | |
3926 | return nfs_ok; | |
fc5fc5d7 | 3927 | } |
1d1bc8f2 BF |
3928 | return nfserr_inval; |
3929 | } | |
3930 | ||
eb69853d CH |
3931 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
3932 | struct nfsd4_compound_state *cstate, | |
3933 | union nfsd4_op_u *u) | |
cb73a9f4 | 3934 | { |
eb69853d | 3935 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
cb73a9f4 | 3936 | struct nfsd4_session *session = cstate->session; |
c9a49628 | 3937 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
b78724b7 | 3938 | __be32 status; |
cb73a9f4 | 3939 | |
b78724b7 BF |
3940 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
3941 | if (status) | |
3942 | return status; | |
c9a49628 | 3943 | spin_lock(&nn->client_lock); |
cb73a9f4 BF |
3944 | session->se_cb_prog = bc->bc_cb_program; |
3945 | session->se_cb_sec = bc->bc_cb_sec; | |
c9a49628 | 3946 | spin_unlock(&nn->client_lock); |
cb73a9f4 BF |
3947 | |
3948 | nfsd4_probe_callback(session->se_client); | |
3949 | ||
3950 | return nfs_ok; | |
3951 | } | |
3952 | ||
c2d715a1 BF |
3953 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
3954 | { | |
3955 | struct nfsd4_conn *c; | |
3956 | ||
3957 | list_for_each_entry(c, &s->se_conns, cn_persession) { | |
3958 | if (c->cn_xprt == xpt) { | |
3959 | return c; | |
3960 | } | |
3961 | } | |
3962 | return NULL; | |
3963 | } | |
3964 | ||
3965 | static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, | |
02579b2f | 3966 | struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn) |
c2d715a1 BF |
3967 | { |
3968 | struct nfs4_client *clp = session->se_client; | |
3969 | struct svc_xprt *xpt = rqst->rq_xprt; | |
3970 | struct nfsd4_conn *c; | |
3971 | __be32 status; | |
3972 | ||
3973 | /* Following the last paragraph of RFC 5661 Section 18.34.3: */ | |
3974 | spin_lock(&clp->cl_lock); | |
3975 | c = __nfsd4_find_conn(xpt, session); | |
3976 | if (!c) | |
3977 | status = nfserr_noent; | |
3978 | else if (req == c->cn_flags) | |
3979 | status = nfs_ok; | |
3980 | else if (req == NFS4_CDFC4_FORE_OR_BOTH && | |
3981 | c->cn_flags != NFS4_CDFC4_BACK) | |
3982 | status = nfs_ok; | |
3983 | else if (req == NFS4_CDFC4_BACK_OR_BOTH && | |
3984 | c->cn_flags != NFS4_CDFC4_FORE) | |
3985 | status = nfs_ok; | |
3986 | else | |
3987 | status = nfserr_inval; | |
3988 | spin_unlock(&clp->cl_lock); | |
02579b2f DN |
3989 | if (status == nfs_ok && conn) |
3990 | *conn = c; | |
c2d715a1 BF |
3991 | return status; |
3992 | } | |
3993 | ||
1d1bc8f2 BF |
3994 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
3995 | struct nfsd4_compound_state *cstate, | |
eb69853d | 3996 | union nfsd4_op_u *u) |
1d1bc8f2 | 3997 | { |
eb69853d | 3998 | struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; |
1d1bc8f2 | 3999 | __be32 status; |
3ba63671 | 4000 | struct nfsd4_conn *conn; |
4f6e6c17 | 4001 | struct nfsd4_session *session; |
d4e19e70 TM |
4002 | struct net *net = SVC_NET(rqstp); |
4003 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1d1bc8f2 BF |
4004 | |
4005 | if (!nfsd4_last_compound_op(rqstp)) | |
4006 | return nfserr_not_only_op; | |
c9a49628 | 4007 | spin_lock(&nn->client_lock); |
d4e19e70 | 4008 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
c9a49628 | 4009 | spin_unlock(&nn->client_lock); |
4f6e6c17 | 4010 | if (!session) |
d4e19e70 | 4011 | goto out_no_session; |
57266a6e | 4012 | status = nfserr_wrong_cred; |
dedeb13f | 4013 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
57266a6e | 4014 | goto out; |
02579b2f DN |
4015 | status = nfsd4_match_existing_connection(rqstp, session, |
4016 | bcts->dir, &conn); | |
4017 | if (status == nfs_ok) { | |
4018 | if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH || | |
4019 | bcts->dir == NFS4_CDFC4_BACK) | |
4020 | conn->cn_flags |= NFS4_CDFC4_BACK; | |
4021 | nfsd4_probe_callback(session->se_client); | |
4022 | goto out; | |
4023 | } | |
4024 | if (status == nfserr_inval) | |
c2d715a1 | 4025 | goto out; |
1d1bc8f2 | 4026 | status = nfsd4_map_bcts_dir(&bcts->dir); |
3ba63671 | 4027 | if (status) |
4f6e6c17 | 4028 | goto out; |
3ba63671 | 4029 | conn = alloc_conn(rqstp, bcts->dir); |
4f6e6c17 | 4030 | status = nfserr_jukebox; |
3ba63671 | 4031 | if (!conn) |
4f6e6c17 BF |
4032 | goto out; |
4033 | nfsd4_init_conn(rqstp, conn, session); | |
4034 | status = nfs_ok; | |
4035 | out: | |
d4e19e70 TM |
4036 | nfsd4_put_session(session); |
4037 | out_no_session: | |
4f6e6c17 | 4038 | return status; |
1d1bc8f2 BF |
4039 | } |
4040 | ||
665d5072 | 4041 | static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) |
5d4cec2f | 4042 | { |
665d5072 | 4043 | if (!cstate->session) |
51d87bc2 | 4044 | return false; |
665d5072 | 4045 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
5d4cec2f BF |
4046 | } |
4047 | ||
069b6ad4 | 4048 | __be32 |
eb69853d CH |
4049 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
4050 | union nfsd4_op_u *u) | |
069b6ad4 | 4051 | { |
ca0552f4 | 4052 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
e10e0cfc | 4053 | struct nfsd4_session *ses; |
abcdff09 | 4054 | __be32 status; |
f0f51f5c | 4055 | int ref_held_by_me = 0; |
d4e19e70 TM |
4056 | struct net *net = SVC_NET(r); |
4057 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
e10e0cfc | 4058 | |
abcdff09 | 4059 | status = nfserr_not_only_op; |
ca0552f4 | 4060 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
57716355 | 4061 | if (!nfsd4_last_compound_op(r)) |
abcdff09 | 4062 | goto out; |
f0f51f5c | 4063 | ref_held_by_me++; |
57716355 | 4064 | } |
ca0552f4 | 4065 | dump_sessionid(__func__, sessionid); |
c9a49628 | 4066 | spin_lock(&nn->client_lock); |
ca0552f4 | 4067 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
abcdff09 BF |
4068 | if (!ses) |
4069 | goto out_client_lock; | |
57266a6e | 4070 | status = nfserr_wrong_cred; |
dedeb13f | 4071 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
d4e19e70 | 4072 | goto out_put_session; |
f0f51f5c | 4073 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
66b2b9b2 | 4074 | if (status) |
f0f51f5c | 4075 | goto out_put_session; |
e10e0cfc | 4076 | unhash_session(ses); |
c9a49628 | 4077 | spin_unlock(&nn->client_lock); |
e10e0cfc | 4078 | |
84f5f7cc | 4079 | nfsd4_probe_callback_sync(ses->se_client); |
19cf5c02 | 4080 | |
c9a49628 | 4081 | spin_lock(&nn->client_lock); |
e10e0cfc | 4082 | status = nfs_ok; |
f0f51f5c | 4083 | out_put_session: |
d4e19e70 | 4084 | nfsd4_put_session_locked(ses); |
abcdff09 BF |
4085 | out_client_lock: |
4086 | spin_unlock(&nn->client_lock); | |
e10e0cfc | 4087 | out: |
e10e0cfc | 4088 | return status; |
069b6ad4 AA |
4089 | } |
4090 | ||
57266a6e | 4091 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
328ead28 BF |
4092 | { |
4093 | struct nfs4_client *clp = ses->se_client; | |
a663bdd8 | 4094 | struct nfsd4_conn *c; |
57266a6e | 4095 | __be32 status = nfs_ok; |
21b75b01 | 4096 | int ret; |
328ead28 BF |
4097 | |
4098 | spin_lock(&clp->cl_lock); | |
a663bdd8 | 4099 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
57266a6e BF |
4100 | if (c) |
4101 | goto out_free; | |
4102 | status = nfserr_conn_not_bound_to_session; | |
4103 | if (clp->cl_mach_cred) | |
4104 | goto out_free; | |
328ead28 BF |
4105 | __nfsd4_hash_conn(new, ses); |
4106 | spin_unlock(&clp->cl_lock); | |
21b75b01 BF |
4107 | ret = nfsd4_register_conn(new); |
4108 | if (ret) | |
4109 | /* oops; xprt is already down: */ | |
4110 | nfsd4_conn_lost(&new->cn_xpt_user); | |
57266a6e BF |
4111 | return nfs_ok; |
4112 | out_free: | |
4113 | spin_unlock(&clp->cl_lock); | |
4114 | free_conn(new); | |
4115 | return status; | |
328ead28 BF |
4116 | } |
4117 | ||
868b89c3 MJ |
4118 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
4119 | { | |
4120 | struct nfsd4_compoundargs *args = rqstp->rq_argp; | |
4121 | ||
4122 | return args->opcnt > session->se_fchannel.maxops; | |
4123 | } | |
4124 | ||
ae82a8d0 MJ |
4125 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
4126 | struct nfsd4_session *session) | |
4127 | { | |
4128 | struct xdr_buf *xb = &rqstp->rq_arg; | |
4129 | ||
4130 | return xb->len > session->se_fchannel.maxreq_sz; | |
4131 | } | |
4132 | ||
53da6a53 BF |
4133 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
4134 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) | |
4135 | { | |
4136 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; | |
4137 | ||
4138 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != | |
4139 | (bool)seq->cachethis) | |
4140 | return false; | |
4141 | /* | |
6e73e92b SM |
4142 | * If there's an error then the reply can have fewer ops than |
4143 | * the call. | |
53da6a53 | 4144 | */ |
6e73e92b SM |
4145 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
4146 | return false; | |
4147 | /* | |
4148 | * But if we cached a reply with *more* ops than the call you're | |
4149 | * sending us now, then this new call is clearly not really a | |
4150 | * replay of the old one: | |
4151 | */ | |
4152 | if (slot->sl_opcnt > argp->opcnt) | |
53da6a53 BF |
4153 | return false; |
4154 | /* This is the only check explicitly called by spec: */ | |
4155 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) | |
4156 | return false; | |
4157 | /* | |
4158 | * There may be more comparisons we could actually do, but the | |
4159 | * spec doesn't require us to catch every case where the calls | |
4160 | * don't match (that would require caching the call as well as | |
4161 | * the reply), so we don't bother. | |
4162 | */ | |
4163 | return true; | |
4164 | } | |
4165 | ||
069b6ad4 | 4166 | __be32 |
eb69853d CH |
4167 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
4168 | union nfsd4_op_u *u) | |
069b6ad4 | 4169 | { |
eb69853d | 4170 | struct nfsd4_sequence *seq = &u->sequence; |
f9bb94c4 | 4171 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
bddfdbcd | 4172 | struct xdr_stream *xdr = resp->xdr; |
b85d4c01 | 4173 | struct nfsd4_session *session; |
221a6876 | 4174 | struct nfs4_client *clp; |
b85d4c01 | 4175 | struct nfsd4_slot *slot; |
a663bdd8 | 4176 | struct nfsd4_conn *conn; |
57b7b43b | 4177 | __be32 status; |
47ee5298 | 4178 | int buflen; |
d4e19e70 TM |
4179 | struct net *net = SVC_NET(rqstp); |
4180 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
b85d4c01 | 4181 | |
f9bb94c4 AA |
4182 | if (resp->opcnt != 1) |
4183 | return nfserr_sequence_pos; | |
4184 | ||
a663bdd8 BF |
4185 | /* |
4186 | * Will be either used or freed by nfsd4_sequence_check_conn | |
4187 | * below. | |
4188 | */ | |
4189 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); | |
4190 | if (!conn) | |
4191 | return nfserr_jukebox; | |
4192 | ||
c9a49628 | 4193 | spin_lock(&nn->client_lock); |
d4e19e70 | 4194 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
b85d4c01 | 4195 | if (!session) |
221a6876 BF |
4196 | goto out_no_session; |
4197 | clp = session->se_client; | |
b85d4c01 | 4198 | |
868b89c3 MJ |
4199 | status = nfserr_too_many_ops; |
4200 | if (nfsd4_session_too_many_ops(rqstp, session)) | |
66b2b9b2 | 4201 | goto out_put_session; |
868b89c3 | 4202 | |
ae82a8d0 MJ |
4203 | status = nfserr_req_too_big; |
4204 | if (nfsd4_request_too_big(rqstp, session)) | |
66b2b9b2 | 4205 | goto out_put_session; |
ae82a8d0 | 4206 | |
b85d4c01 | 4207 | status = nfserr_badslot; |
6c18ba9f | 4208 | if (seq->slotid >= session->se_fchannel.maxreqs) |
66b2b9b2 | 4209 | goto out_put_session; |
b85d4c01 | 4210 | |
557ce264 | 4211 | slot = session->se_slots[seq->slotid]; |
b85d4c01 BH |
4212 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
4213 | ||
a8dfdaeb AA |
4214 | /* We do not negotiate the number of slots yet, so set the |
4215 | * maxslots to the session maxreqs which is used to encode | |
4216 | * sr_highest_slotid and the sr_target_slot id to maxslots */ | |
4217 | seq->maxslots = session->se_fchannel.maxreqs; | |
4218 | ||
73e79482 BF |
4219 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
4220 | slot->sl_flags & NFSD4_SLOT_INUSE); | |
b85d4c01 | 4221 | if (status == nfserr_replay_cache) { |
bf5c43c8 BF |
4222 | status = nfserr_seq_misordered; |
4223 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) | |
66b2b9b2 | 4224 | goto out_put_session; |
53da6a53 BF |
4225 | status = nfserr_seq_false_retry; |
4226 | if (!replay_matches_cache(rqstp, seq, slot)) | |
4227 | goto out_put_session; | |
b85d4c01 BH |
4228 | cstate->slot = slot; |
4229 | cstate->session = session; | |
4b24ca7d | 4230 | cstate->clp = clp; |
da3846a2 | 4231 | /* Return the cached reply status and set cstate->status |
557ce264 | 4232 | * for nfsd4_proc_compound processing */ |
bf864a31 | 4233 | status = nfsd4_replay_cache_entry(resp, seq); |
da3846a2 | 4234 | cstate->status = nfserr_replay_cache; |
aaf84eb9 | 4235 | goto out; |
b85d4c01 BH |
4236 | } |
4237 | if (status) | |
66b2b9b2 | 4238 | goto out_put_session; |
b85d4c01 | 4239 | |
57266a6e | 4240 | status = nfsd4_sequence_check_conn(conn, session); |
a663bdd8 | 4241 | conn = NULL; |
57266a6e BF |
4242 | if (status) |
4243 | goto out_put_session; | |
328ead28 | 4244 | |
47ee5298 BF |
4245 | buflen = (seq->cachethis) ? |
4246 | session->se_fchannel.maxresp_cached : | |
4247 | session->se_fchannel.maxresp_sz; | |
4248 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : | |
4249 | nfserr_rep_too_big; | |
a5cddc88 | 4250 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
47ee5298 | 4251 | goto out_put_session; |
32aaa62e | 4252 | svc_reserve(rqstp, buflen); |
47ee5298 BF |
4253 | |
4254 | status = nfs_ok; | |
b85d4c01 | 4255 | /* Success! bump slot seqid */ |
b85d4c01 | 4256 | slot->sl_seqid = seq->seqid; |
bf5c43c8 | 4257 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
73e79482 BF |
4258 | if (seq->cachethis) |
4259 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; | |
bf5c43c8 BF |
4260 | else |
4261 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; | |
b85d4c01 BH |
4262 | |
4263 | cstate->slot = slot; | |
4264 | cstate->session = session; | |
4b24ca7d | 4265 | cstate->clp = clp; |
b85d4c01 | 4266 | |
b85d4c01 | 4267 | out: |
221a6876 BF |
4268 | switch (clp->cl_cb_state) { |
4269 | case NFSD4_CB_DOWN: | |
4270 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; | |
4271 | break; | |
4272 | case NFSD4_CB_FAULT: | |
4273 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; | |
4274 | break; | |
4275 | default: | |
4276 | seq->status_flags = 0; | |
aaf84eb9 | 4277 | } |
3bd64a5b BF |
4278 | if (!list_empty(&clp->cl_revoked)) |
4279 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; | |
1ac3629b N |
4280 | if (atomic_read(&clp->cl_admin_revoked)) |
4281 | seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED; | |
f52f1975 | 4282 | trace_nfsd_seq4_status(rqstp, seq); |
221a6876 | 4283 | out_no_session: |
3f42d2c4 KM |
4284 | if (conn) |
4285 | free_conn(conn); | |
c9a49628 | 4286 | spin_unlock(&nn->client_lock); |
b85d4c01 | 4287 | return status; |
66b2b9b2 | 4288 | out_put_session: |
d4e19e70 | 4289 | nfsd4_put_session_locked(session); |
221a6876 | 4290 | goto out_no_session; |
069b6ad4 AA |
4291 | } |
4292 | ||
b607664e TM |
4293 | void |
4294 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) | |
4295 | { | |
4296 | struct nfsd4_compound_state *cs = &resp->cstate; | |
4297 | ||
4298 | if (nfsd4_has_session(cs)) { | |
b607664e TM |
4299 | if (cs->status != nfserr_replay_cache) { |
4300 | nfsd4_store_cache_entry(resp); | |
4301 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; | |
4302 | } | |
d4e19e70 | 4303 | /* Drop session reference that was taken in nfsd4_sequence() */ |
b607664e | 4304 | nfsd4_put_session(cs->session); |
4b24ca7d JL |
4305 | } else if (cs->clp) |
4306 | put_client_renew(cs->clp); | |
b607664e TM |
4307 | } |
4308 | ||
345c2842 | 4309 | __be32 |
eb69853d CH |
4310 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
4311 | struct nfsd4_compound_state *cstate, | |
4312 | union nfsd4_op_u *u) | |
345c2842 | 4313 | { |
eb69853d | 4314 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
6b10ad19 TM |
4315 | struct nfs4_client *conf, *unconf; |
4316 | struct nfs4_client *clp = NULL; | |
57b7b43b | 4317 | __be32 status = 0; |
8daae4dc | 4318 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
345c2842 | 4319 | |
6b10ad19 | 4320 | spin_lock(&nn->client_lock); |
0a7ec377 | 4321 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
8daae4dc | 4322 | conf = find_confirmed_client(&dc->clientid, true, nn); |
78389046 | 4323 | WARN_ON_ONCE(conf && unconf); |
345c2842 MJ |
4324 | |
4325 | if (conf) { | |
c0293b01 | 4326 | if (client_has_state(conf)) { |
345c2842 MJ |
4327 | status = nfserr_clientid_busy; |
4328 | goto out; | |
4329 | } | |
fd699b8a JL |
4330 | status = mark_client_expired_locked(conf); |
4331 | if (status) | |
4332 | goto out; | |
6b10ad19 | 4333 | clp = conf; |
345c2842 MJ |
4334 | } else if (unconf) |
4335 | clp = unconf; | |
4336 | else { | |
4337 | status = nfserr_stale_clientid; | |
4338 | goto out; | |
4339 | } | |
dedeb13f | 4340 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
6b10ad19 | 4341 | clp = NULL; |
57266a6e BF |
4342 | status = nfserr_wrong_cred; |
4343 | goto out; | |
4344 | } | |
c41a9b7a | 4345 | trace_nfsd_clid_destroyed(&clp->cl_clientid); |
6b10ad19 | 4346 | unhash_client_locked(clp); |
345c2842 | 4347 | out: |
6b10ad19 | 4348 | spin_unlock(&nn->client_lock); |
6b10ad19 TM |
4349 | if (clp) |
4350 | expire_client(clp); | |
345c2842 MJ |
4351 | return status; |
4352 | } | |
4353 | ||
4dc6ec00 | 4354 | __be32 |
eb69853d CH |
4355 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
4356 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) | |
4dc6ec00 | 4357 | { |
eb69853d | 4358 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
ec59659b | 4359 | struct nfs4_client *clp = cstate->clp; |
57b7b43b | 4360 | __be32 status = 0; |
bcecf1cc | 4361 | |
4dc6ec00 BF |
4362 | if (rc->rca_one_fs) { |
4363 | if (!cstate->current_fh.fh_dentry) | |
4364 | return nfserr_nofilehandle; | |
4365 | /* | |
4366 | * We don't take advantage of the rca_one_fs case. | |
4367 | * That's OK, it's optional, we can safely ignore it. | |
4368 | */ | |
d28c442f | 4369 | return nfs_ok; |
4dc6ec00 | 4370 | } |
bcecf1cc | 4371 | |
bcecf1cc | 4372 | status = nfserr_complete_already; |
ec59659b | 4373 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
bcecf1cc MJ |
4374 | goto out; |
4375 | ||
4376 | status = nfserr_stale_clientid; | |
ec59659b | 4377 | if (is_client_expired(clp)) |
4dc6ec00 BF |
4378 | /* |
4379 | * The following error isn't really legal. | |
4380 | * But we only get here if the client just explicitly | |
4381 | * destroyed the client. Surely it no longer cares what | |
4382 | * error it gets back on an operation for the dead | |
4383 | * client. | |
4384 | */ | |
bcecf1cc MJ |
4385 | goto out; |
4386 | ||
4387 | status = nfs_ok; | |
cee8aa07 | 4388 | trace_nfsd_clid_reclaim_complete(&clp->cl_clientid); |
ec59659b BF |
4389 | nfsd4_client_record_create(clp); |
4390 | inc_reclaim_complete(clp); | |
bcecf1cc | 4391 | out: |
bcecf1cc | 4392 | return status; |
4dc6ec00 BF |
4393 | } |
4394 | ||
b37ad28b | 4395 | __be32 |
b591480b | 4396 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 4397 | union nfsd4_op_u *u) |
1da177e4 | 4398 | { |
eb69853d | 4399 | struct nfsd4_setclientid *setclid = &u->setclientid; |
a084daf5 | 4400 | struct xdr_netobj clname = setclid->se_name; |
1da177e4 | 4401 | nfs4_verifier clverifier = setclid->se_verf; |
3dbacee6 TM |
4402 | struct nfs4_client *conf, *new; |
4403 | struct nfs4_client *unconf = NULL; | |
b37ad28b | 4404 | __be32 status; |
c212cecf SK |
4405 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
4406 | ||
5cc40fd7 TM |
4407 | new = create_client(clname, rqstp, &clverifier); |
4408 | if (new == NULL) | |
4409 | return nfserr_jukebox; | |
3dbacee6 | 4410 | spin_lock(&nn->client_lock); |
382a62e7 | 4411 | conf = find_confirmed_client_by_name(&clname, nn); |
2b634821 | 4412 | if (conf && client_has_state(conf)) { |
1da177e4 | 4413 | status = nfserr_clid_inuse; |
e203d506 BF |
4414 | if (clp_used_exchangeid(conf)) |
4415 | goto out; | |
026722c2 | 4416 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
27787733 | 4417 | trace_nfsd_clid_cred_mismatch(conf, rqstp); |
1da177e4 LT |
4418 | goto out; |
4419 | } | |
1da177e4 | 4420 | } |
a99454aa | 4421 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
8f930711 | 4422 | if (unconf) |
3dbacee6 | 4423 | unhash_client_locked(unconf); |
744ea54c CL |
4424 | if (conf) { |
4425 | if (same_verf(&conf->cl_verifier, &clverifier)) { | |
4426 | copy_clid(new, conf); | |
4427 | gen_confirm(new, nn); | |
4428 | } else | |
4429 | trace_nfsd_clid_verf_mismatch(conf, rqstp, | |
4430 | &clverifier); | |
237f91c8 CL |
4431 | } else |
4432 | trace_nfsd_clid_fresh(new); | |
8323c3b2 | 4433 | new->cl_minorversion = 0; |
6f3d772f | 4434 | gen_callback(new, setclid, rqstp); |
ac55fdc4 | 4435 | add_to_unconfirmed(new); |
1da177e4 LT |
4436 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
4437 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
4438 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
5cc40fd7 | 4439 | new = NULL; |
1da177e4 LT |
4440 | status = nfs_ok; |
4441 | out: | |
3dbacee6 | 4442 | spin_unlock(&nn->client_lock); |
5cc40fd7 TM |
4443 | if (new) |
4444 | free_client(new); | |
237f91c8 CL |
4445 | if (unconf) { |
4446 | trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); | |
3dbacee6 | 4447 | expire_client(unconf); |
237f91c8 | 4448 | } |
1da177e4 LT |
4449 | return status; |
4450 | } | |
4451 | ||
b37ad28b | 4452 | __be32 |
b591480b | 4453 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
eb69853d CH |
4454 | struct nfsd4_compound_state *cstate, |
4455 | union nfsd4_op_u *u) | |
1da177e4 | 4456 | { |
eb69853d CH |
4457 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
4458 | &u->setclientid_confirm; | |
21ab45a4 | 4459 | struct nfs4_client *conf, *unconf; |
d20c11d8 | 4460 | struct nfs4_client *old = NULL; |
1da177e4 LT |
4461 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
4462 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
b37ad28b | 4463 | __be32 status; |
7f2210fa | 4464 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4465 | |
2c142baa | 4466 | if (STALE_CLIENTID(clid, nn)) |
1da177e4 | 4467 | return nfserr_stale_clientid; |
21ab45a4 | 4468 | |
d20c11d8 | 4469 | spin_lock(&nn->client_lock); |
8daae4dc | 4470 | conf = find_confirmed_client(clid, false, nn); |
0a7ec377 | 4471 | unconf = find_unconfirmed_client(clid, false, nn); |
a186e767 | 4472 | /* |
8695b90a BF |
4473 | * We try hard to give out unique clientid's, so if we get an |
4474 | * attempt to confirm the same clientid with a different cred, | |
f984a7ce BF |
4475 | * the client may be buggy; this should never happen. |
4476 | * | |
4477 | * Nevertheless, RFC 7530 recommends INUSE for this case: | |
a186e767 | 4478 | */ |
f984a7ce | 4479 | status = nfserr_clid_inuse; |
27787733 CL |
4480 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
4481 | trace_nfsd_clid_cred_mismatch(unconf, rqstp); | |
8695b90a | 4482 | goto out; |
27787733 CL |
4483 | } |
4484 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) { | |
4485 | trace_nfsd_clid_cred_mismatch(conf, rqstp); | |
8695b90a | 4486 | goto out; |
27787733 | 4487 | } |
90d700b7 | 4488 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
7d22fc11 | 4489 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
1da177e4 | 4490 | status = nfs_ok; |
237f91c8 | 4491 | } else |
90d700b7 BF |
4492 | status = nfserr_stale_clientid; |
4493 | goto out; | |
4494 | } | |
4495 | status = nfs_ok; | |
237f91c8 | 4496 | if (conf) { |
d20c11d8 JL |
4497 | old = unconf; |
4498 | unhash_client_locked(old); | |
8695b90a | 4499 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
237f91c8 | 4500 | } else { |
d20c11d8 JL |
4501 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
4502 | if (old) { | |
2b634821 BF |
4503 | status = nfserr_clid_inuse; |
4504 | if (client_has_state(old) | |
4505 | && !same_creds(&unconf->cl_cred, | |
ab451ea9 DN |
4506 | &old->cl_cred)) { |
4507 | old = NULL; | |
2b634821 | 4508 | goto out; |
ab451ea9 | 4509 | } |
d20c11d8 | 4510 | status = mark_client_expired_locked(old); |
7abea1e8 JL |
4511 | if (status) { |
4512 | old = NULL; | |
221a6876 | 4513 | goto out; |
7abea1e8 | 4514 | } |
2958d2ee | 4515 | trace_nfsd_clid_replaced(&old->cl_clientid); |
221a6876 | 4516 | } |
8695b90a | 4517 | move_to_confirmed(unconf); |
d20c11d8 | 4518 | conf = unconf; |
08e8987c | 4519 | } |
d20c11d8 JL |
4520 | get_client_locked(conf); |
4521 | spin_unlock(&nn->client_lock); | |
934bd07f BF |
4522 | if (conf == unconf) |
4523 | fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); | |
d20c11d8 JL |
4524 | nfsd4_probe_callback(conf); |
4525 | spin_lock(&nn->client_lock); | |
4526 | put_client_renew_locked(conf); | |
1da177e4 | 4527 | out: |
d20c11d8 JL |
4528 | spin_unlock(&nn->client_lock); |
4529 | if (old) | |
4530 | expire_client(old); | |
1da177e4 LT |
4531 | return status; |
4532 | } | |
4533 | ||
32513b40 BF |
4534 | static struct nfs4_file *nfsd4_alloc_file(void) |
4535 | { | |
4536 | return kmem_cache_alloc(file_slab, GFP_KERNEL); | |
4537 | } | |
4538 | ||
1da177e4 | 4539 | /* OPEN Share state helper functions */ |
950e0118 | 4540 | |
81a21fa3 CL |
4541 | static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp) |
4542 | { | |
818a34eb | 4543 | refcount_set(&fp->fi_ref, 1); |
1d31a253 | 4544 | spin_lock_init(&fp->fi_lock); |
32513b40 BF |
4545 | INIT_LIST_HEAD(&fp->fi_stateids); |
4546 | INIT_LIST_HEAD(&fp->fi_delegations); | |
8287f009 | 4547 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
f9b60e22 | 4548 | fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); |
0c637be8 | 4549 | fp->fi_deleg_file = NULL; |
32513b40 | 4550 | fp->fi_had_conflict = false; |
baeb4ff0 | 4551 | fp->fi_share_deny = 0; |
32513b40 BF |
4552 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
4553 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); | |
a0ce4837 BF |
4554 | fp->fi_aliased = false; |
4555 | fp->fi_inode = d_inode(fh->fh_dentry); | |
9cf514cc CH |
4556 | #ifdef CONFIG_NFSD_PNFS |
4557 | INIT_LIST_HEAD(&fp->fi_lo_states); | |
c5c707f9 | 4558 | atomic_set(&fp->fi_lo_recalls, 0); |
9cf514cc | 4559 | #endif |
1da177e4 LT |
4560 | } |
4561 | ||
e8ff2a84 | 4562 | void |
1da177e4 LT |
4563 | nfsd4_free_slabs(void) |
4564 | { | |
9258a2d5 | 4565 | kmem_cache_destroy(client_slab); |
abf1135b CH |
4566 | kmem_cache_destroy(openowner_slab); |
4567 | kmem_cache_destroy(lockowner_slab); | |
4568 | kmem_cache_destroy(file_slab); | |
4569 | kmem_cache_destroy(stateid_slab); | |
4570 | kmem_cache_destroy(deleg_slab); | |
9258a2d5 | 4571 | kmem_cache_destroy(odstate_slab); |
e60d4398 | 4572 | } |
1da177e4 | 4573 | |
72083396 | 4574 | int |
e60d4398 N |
4575 | nfsd4_init_slabs(void) |
4576 | { | |
649e58d5 | 4577 | client_slab = KMEM_CACHE(nfs4_client, 0); |
9258a2d5 JL |
4578 | if (client_slab == NULL) |
4579 | goto out; | |
649e58d5 | 4580 | openowner_slab = KMEM_CACHE(nfs4_openowner, 0); |
fe0750e5 | 4581 | if (openowner_slab == NULL) |
9258a2d5 | 4582 | goto out_free_client_slab; |
649e58d5 | 4583 | lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0); |
fe0750e5 | 4584 | if (lockowner_slab == NULL) |
abf1135b | 4585 | goto out_free_openowner_slab; |
649e58d5 | 4586 | file_slab = KMEM_CACHE(nfs4_file, 0); |
e60d4398 | 4587 | if (file_slab == NULL) |
abf1135b | 4588 | goto out_free_lockowner_slab; |
649e58d5 | 4589 | stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0); |
5ac049ac | 4590 | if (stateid_slab == NULL) |
abf1135b | 4591 | goto out_free_file_slab; |
649e58d5 | 4592 | deleg_slab = KMEM_CACHE(nfs4_delegation, 0); |
5b2d21c1 | 4593 | if (deleg_slab == NULL) |
abf1135b | 4594 | goto out_free_stateid_slab; |
649e58d5 | 4595 | odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0); |
8287f009 SB |
4596 | if (odstate_slab == NULL) |
4597 | goto out_free_deleg_slab; | |
e60d4398 | 4598 | return 0; |
abf1135b | 4599 | |
8287f009 SB |
4600 | out_free_deleg_slab: |
4601 | kmem_cache_destroy(deleg_slab); | |
abf1135b CH |
4602 | out_free_stateid_slab: |
4603 | kmem_cache_destroy(stateid_slab); | |
4604 | out_free_file_slab: | |
4605 | kmem_cache_destroy(file_slab); | |
4606 | out_free_lockowner_slab: | |
4607 | kmem_cache_destroy(lockowner_slab); | |
4608 | out_free_openowner_slab: | |
4609 | kmem_cache_destroy(openowner_slab); | |
9258a2d5 JL |
4610 | out_free_client_slab: |
4611 | kmem_cache_destroy(client_slab); | |
abf1135b | 4612 | out: |
e60d4398 | 4613 | return -ENOMEM; |
1da177e4 LT |
4614 | } |
4615 | ||
7746b32f | 4616 | static unsigned long |
a1049eb4 | 4617 | nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) |
7746b32f | 4618 | { |
44df6f43 | 4619 | int count; |
d17452aa | 4620 | struct nfsd_net *nn = shrink->private_data; |
7746b32f | 4621 | |
44df6f43 DN |
4622 | count = atomic_read(&nn->nfsd_courtesy_clients); |
4623 | if (!count) | |
4624 | count = atomic_long_read(&num_delegations); | |
4625 | if (count) | |
7c24fa22 | 4626 | queue_work(laundry_wq, &nn->nfsd_shrinker_work); |
44df6f43 | 4627 | return (unsigned long)count; |
7746b32f DN |
4628 | } |
4629 | ||
4630 | static unsigned long | |
a1049eb4 | 4631 | nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc) |
7746b32f DN |
4632 | { |
4633 | return SHRINK_STOP; | |
4634 | } | |
4635 | ||
f385f7d2 | 4636 | void |
7746b32f | 4637 | nfsd4_init_leases_net(struct nfsd_net *nn) |
6867137e | 4638 | { |
4271c2c0 DN |
4639 | struct sysinfo si; |
4640 | u64 max_clients; | |
4641 | ||
6867137e DN |
4642 | nn->nfsd4_lease = 90; /* default lease time */ |
4643 | nn->nfsd4_grace = 90; | |
4644 | nn->somebody_reclaimed = false; | |
4645 | nn->track_reclaim_completes = false; | |
a251c17a JD |
4646 | nn->clverifier_counter = get_random_u32(); |
4647 | nn->clientid_base = get_random_u32(); | |
6867137e DN |
4648 | nn->clientid_counter = nn->clientid_base + 1; |
4649 | nn->s2s_cp_cl_id = nn->clientid_counter++; | |
0926c395 DN |
4650 | |
4651 | atomic_set(&nn->nfs4_client_count, 0); | |
4271c2c0 DN |
4652 | si_meminfo(&si); |
4653 | max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024); | |
4654 | max_clients *= NFS4_CLIENTS_PER_GB; | |
4655 | nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB); | |
3a4ea23d DN |
4656 | |
4657 | atomic_set(&nn->nfsd_courtesy_clients, 0); | |
6867137e DN |
4658 | } |
4659 | ||
eec76208 N |
4660 | enum rp_lock { |
4661 | RP_UNLOCKED, | |
4662 | RP_LOCKED, | |
4663 | RP_UNHASHED, | |
4664 | }; | |
4665 | ||
ff194bd9 | 4666 | static void init_nfs4_replay(struct nfs4_replay *rp) |
1da177e4 | 4667 | { |
ff194bd9 BF |
4668 | rp->rp_status = nfserr_serverfault; |
4669 | rp->rp_buflen = 0; | |
4670 | rp->rp_buf = rp->rp_ibuf; | |
eec76208 | 4671 | atomic_set(&rp->rp_locked, RP_UNLOCKED); |
58fb12e6 JL |
4672 | } |
4673 | ||
eec76208 N |
4674 | static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
4675 | struct nfs4_stateowner *so) | |
58fb12e6 JL |
4676 | { |
4677 | if (!nfsd4_has_session(cstate)) { | |
eec76208 N |
4678 | wait_var_event(&so->so_replay.rp_locked, |
4679 | atomic_cmpxchg(&so->so_replay.rp_locked, | |
4680 | RP_UNLOCKED, RP_LOCKED) != RP_LOCKED); | |
4681 | if (atomic_read(&so->so_replay.rp_locked) == RP_UNHASHED) | |
4682 | return -EAGAIN; | |
b5971afa | 4683 | cstate->replay_owner = nfs4_get_stateowner(so); |
58fb12e6 | 4684 | } |
eec76208 | 4685 | return 0; |
58fb12e6 JL |
4686 | } |
4687 | ||
4688 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) | |
4689 | { | |
4690 | struct nfs4_stateowner *so = cstate->replay_owner; | |
4691 | ||
4692 | if (so != NULL) { | |
4693 | cstate->replay_owner = NULL; | |
eec76208 N |
4694 | atomic_set(&so->so_replay.rp_locked, RP_UNLOCKED); |
4695 | wake_up_var(&so->so_replay.rp_locked); | |
58fb12e6 JL |
4696 | nfs4_put_stateowner(so); |
4697 | } | |
1da177e4 LT |
4698 | } |
4699 | ||
fe0750e5 | 4700 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
ff194bd9 | 4701 | { |
1da177e4 | 4702 | struct nfs4_stateowner *sop; |
1da177e4 | 4703 | |
fe0750e5 | 4704 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
ff194bd9 BF |
4705 | if (!sop) |
4706 | return NULL; | |
4707 | ||
6f4859b8 | 4708 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
ff194bd9 | 4709 | if (!sop->so_owner.data) { |
fe0750e5 | 4710 | kmem_cache_free(slab, sop); |
1da177e4 | 4711 | return NULL; |
ff194bd9 | 4712 | } |
ff194bd9 | 4713 | |
ea1da636 | 4714 | INIT_LIST_HEAD(&sop->so_stateids); |
ff194bd9 BF |
4715 | sop->so_client = clp; |
4716 | init_nfs4_replay(&sop->so_replay); | |
6b180f0b | 4717 | atomic_set(&sop->so_count, 1); |
ff194bd9 BF |
4718 | return sop; |
4719 | } | |
4720 | ||
fe0750e5 | 4721 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
ff194bd9 | 4722 | { |
d4f0489f | 4723 | lockdep_assert_held(&clp->cl_lock); |
9b531137 | 4724 | |
d4f0489f TM |
4725 | list_add(&oo->oo_owner.so_strhash, |
4726 | &clp->cl_ownerstr_hashtbl[strhashval]); | |
fe0750e5 | 4727 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
ff194bd9 BF |
4728 | } |
4729 | ||
8f4b54c5 JL |
4730 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
4731 | { | |
d4f0489f | 4732 | unhash_openowner_locked(openowner(so)); |
8f4b54c5 JL |
4733 | } |
4734 | ||
6b180f0b JL |
4735 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
4736 | { | |
4737 | struct nfs4_openowner *oo = openowner(so); | |
4738 | ||
4739 | kmem_cache_free(openowner_slab, oo); | |
4740 | } | |
4741 | ||
4742 | static const struct nfs4_stateowner_operations openowner_ops = { | |
8f4b54c5 JL |
4743 | .so_unhash = nfs4_unhash_openowner, |
4744 | .so_free = nfs4_free_openowner, | |
6b180f0b JL |
4745 | }; |
4746 | ||
7fc0564e AE |
4747 | static struct nfs4_ol_stateid * |
4748 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) | |
4749 | { | |
4750 | struct nfs4_ol_stateid *local, *ret = NULL; | |
4751 | struct nfs4_openowner *oo = open->op_openowner; | |
4752 | ||
4753 | lockdep_assert_held(&fp->fi_lock); | |
4754 | ||
4755 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { | |
4756 | /* ignore lock owners */ | |
4757 | if (local->st_stateowner->so_is_open_owner == 0) | |
4758 | continue; | |
15ca08d3 TM |
4759 | if (local->st_stateowner != &oo->oo_owner) |
4760 | continue; | |
3f29cc82 N |
4761 | if (local->st_stid.sc_type == SC_TYPE_OPEN && |
4762 | !local->st_stid.sc_status) { | |
7fc0564e | 4763 | ret = local; |
a15dfcd5 | 4764 | refcount_inc(&ret->st_stid.sc_count); |
7fc0564e AE |
4765 | break; |
4766 | } | |
4767 | } | |
4768 | return ret; | |
4769 | } | |
4770 | ||
d688d858 N |
4771 | static void nfsd4_drop_revoked_stid(struct nfs4_stid *s) |
4772 | __releases(&s->sc_client->cl_lock) | |
15ca08d3 | 4773 | { |
d688d858 | 4774 | struct nfs4_client *cl = s->sc_client; |
1c13bf9f N |
4775 | LIST_HEAD(reaplist); |
4776 | struct nfs4_ol_stateid *stp; | |
06efa667 | 4777 | struct nfs4_delegation *dp; |
1c13bf9f | 4778 | bool unhashed; |
15ca08d3 TM |
4779 | |
4780 | switch (s->sc_type) { | |
39657c74 N |
4781 | case SC_TYPE_OPEN: |
4782 | stp = openlockstateid(s); | |
4783 | if (unhash_open_stateid(stp, &reaplist)) | |
4784 | put_ol_stateid_locked(stp, &reaplist); | |
4785 | spin_unlock(&cl->cl_lock); | |
4786 | free_ol_stateid_reaplist(&reaplist); | |
15ca08d3 | 4787 | break; |
1c13bf9f N |
4788 | case SC_TYPE_LOCK: |
4789 | stp = openlockstateid(s); | |
4790 | unhashed = unhash_lock_stateid(stp); | |
4791 | spin_unlock(&cl->cl_lock); | |
4792 | if (unhashed) | |
4793 | nfs4_put_stid(s); | |
15ca08d3 | 4794 | break; |
06efa667 N |
4795 | case SC_TYPE_DELEG: |
4796 | dp = delegstateid(s); | |
4797 | list_del_init(&dp->dl_recall_lru); | |
4798 | spin_unlock(&cl->cl_lock); | |
4799 | nfs4_put_stid(s); | |
4800 | break; | |
d688d858 N |
4801 | default: |
4802 | spin_unlock(&cl->cl_lock); | |
15ca08d3 | 4803 | } |
d688d858 N |
4804 | } |
4805 | ||
4806 | static void nfsd40_drop_revoked_stid(struct nfs4_client *cl, | |
4807 | stateid_t *stid) | |
4808 | { | |
4809 | /* NFSv4.0 has no way for the client to tell the server | |
4810 | * that it can forget an admin-revoked stateid. | |
4811 | * So we keep it around until the first time that the | |
4812 | * client uses it, and drop it the first time | |
4813 | * nfserr_admin_revoked is returned. | |
4814 | * For v4.1 and later we wait until explicitly told | |
4815 | * to free the stateid. | |
4816 | */ | |
4817 | if (cl->cl_minorversion == 0) { | |
4818 | struct nfs4_stid *st; | |
4819 | ||
4820 | spin_lock(&cl->cl_lock); | |
4821 | st = find_stateid_locked(cl, stid); | |
4822 | if (st) | |
4823 | nfsd4_drop_revoked_stid(st); | |
4824 | else | |
4825 | spin_unlock(&cl->cl_lock); | |
4826 | } | |
4827 | } | |
4828 | ||
15ca08d3 TM |
4829 | static __be32 |
4830 | nfsd4_verify_open_stid(struct nfs4_stid *s) | |
4831 | { | |
4832 | __be32 ret = nfs_ok; | |
4833 | ||
1ac3629b N |
4834 | if (s->sc_status & SC_STATUS_ADMIN_REVOKED) |
4835 | ret = nfserr_admin_revoked; | |
4836 | else if (s->sc_status & SC_STATUS_REVOKED) | |
15ca08d3 | 4837 | ret = nfserr_deleg_revoked; |
3f29cc82 N |
4838 | else if (s->sc_status & SC_STATUS_CLOSED) |
4839 | ret = nfserr_bad_stateid; | |
15ca08d3 TM |
4840 | return ret; |
4841 | } | |
4842 | ||
4843 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ | |
4844 | static __be32 | |
4845 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) | |
4846 | { | |
4847 | __be32 ret; | |
4848 | ||
4f34bd05 | 4849 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
15ca08d3 | 4850 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
d688d858 N |
4851 | if (ret == nfserr_admin_revoked) |
4852 | nfsd40_drop_revoked_stid(stp->st_stid.sc_client, | |
4853 | &stp->st_stid.sc_stateid); | |
4854 | ||
15ca08d3 TM |
4855 | if (ret != nfs_ok) |
4856 | mutex_unlock(&stp->st_mutex); | |
4857 | return ret; | |
4858 | } | |
4859 | ||
4860 | static struct nfs4_ol_stateid * | |
4861 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) | |
4862 | { | |
4863 | struct nfs4_ol_stateid *stp; | |
4864 | for (;;) { | |
4865 | spin_lock(&fp->fi_lock); | |
4866 | stp = nfsd4_find_existing_open(fp, open); | |
4867 | spin_unlock(&fp->fi_lock); | |
4868 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) | |
4869 | break; | |
4870 | nfs4_put_stid(&stp->st_stid); | |
4871 | } | |
4872 | return stp; | |
4873 | } | |
4874 | ||
fe0750e5 | 4875 | static struct nfs4_openowner * |
23df1778 N |
4876 | find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
4877 | struct nfsd4_compound_state *cstate) | |
db24b3b4 | 4878 | { |
13d6f66b | 4879 | struct nfs4_client *clp = cstate->clp; |
23df1778 | 4880 | struct nfs4_openowner *oo, *new = NULL; |
ff194bd9 | 4881 | |
23df1778 | 4882 | retry: |
d4f0489f | 4883 | spin_lock(&clp->cl_lock); |
23df1778 N |
4884 | oo = find_openstateowner_str(strhashval, open, clp); |
4885 | if (!oo && new) { | |
4886 | hash_openowner(new, clp, strhashval); | |
4887 | spin_unlock(&clp->cl_lock); | |
4888 | return new; | |
4889 | } | |
d4f0489f | 4890 | spin_unlock(&clp->cl_lock); |
23df1778 N |
4891 | |
4892 | if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) { | |
4893 | /* Replace unconfirmed owners without checking for replay. */ | |
4894 | release_openowner(oo); | |
4895 | oo = NULL; | |
4896 | } | |
4897 | if (oo) { | |
4898 | if (new) | |
4899 | nfs4_free_stateowner(&new->oo_owner); | |
4900 | return oo; | |
4901 | } | |
4902 | ||
4903 | new = alloc_stateowner(openowner_slab, &open->op_owner, clp); | |
4904 | if (!new) | |
4905 | return NULL; | |
4906 | new->oo_owner.so_ops = &openowner_ops; | |
4907 | new->oo_owner.so_is_open_owner = 1; | |
4908 | new->oo_owner.so_seqid = open->op_seqid; | |
4909 | new->oo_flags = 0; | |
4910 | if (nfsd4_has_session(cstate)) | |
4911 | new->oo_flags |= NFS4_OO_CONFIRMED; | |
4912 | new->oo_time = 0; | |
4913 | new->oo_last_closed_stid = NULL; | |
4914 | INIT_LIST_HEAD(&new->oo_close_lru); | |
4915 | goto retry; | |
1da177e4 LT |
4916 | } |
4917 | ||
7fc0564e | 4918 | static struct nfs4_ol_stateid * |
8c7245ab | 4919 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
7fc0564e AE |
4920 | { |
4921 | ||
fe0750e5 | 4922 | struct nfs4_openowner *oo = open->op_openowner; |
7fc0564e | 4923 | struct nfs4_ol_stateid *retstp = NULL; |
8c7245ab | 4924 | struct nfs4_ol_stateid *stp; |
1da177e4 | 4925 | |
8c7245ab | 4926 | stp = open->op_stp; |
5cc1fb2a OD |
4927 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
4928 | mutex_init(&stp->st_mutex); | |
4f34bd05 | 4929 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
5cc1fb2a | 4930 | |
15ca08d3 | 4931 | retry: |
7fc0564e AE |
4932 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
4933 | spin_lock(&fp->fi_lock); | |
4934 | ||
4935 | retstp = nfsd4_find_existing_open(fp, open); | |
4936 | if (retstp) | |
4937 | goto out_unlock; | |
8c7245ab OD |
4938 | |
4939 | open->op_stp = NULL; | |
a15dfcd5 | 4940 | refcount_inc(&stp->st_stid.sc_count); |
3f29cc82 | 4941 | stp->st_stid.sc_type = SC_TYPE_OPEN; |
3c87b9b7 | 4942 | INIT_LIST_HEAD(&stp->st_locks); |
b5971afa | 4943 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
13cd2184 | 4944 | get_nfs4_file(fp); |
11b9164a | 4945 | stp->st_stid.sc_file = fp; |
1da177e4 LT |
4946 | stp->st_access_bmap = 0; |
4947 | stp->st_deny_bmap = 0; | |
4c4cd222 | 4948 | stp->st_openstp = NULL; |
1c755dc1 | 4949 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
1d31a253 | 4950 | list_add(&stp->st_perfile, &fp->fi_stateids); |
7fc0564e AE |
4951 | |
4952 | out_unlock: | |
1d31a253 | 4953 | spin_unlock(&fp->fi_lock); |
1c755dc1 | 4954 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
5cc1fb2a | 4955 | if (retstp) { |
15ca08d3 TM |
4956 | /* Handle races with CLOSE */ |
4957 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { | |
4958 | nfs4_put_stid(&retstp->st_stid); | |
4959 | goto retry; | |
4960 | } | |
8c7245ab | 4961 | /* To keep mutex tracking happy */ |
5cc1fb2a | 4962 | mutex_unlock(&stp->st_mutex); |
8c7245ab | 4963 | stp = retstp; |
5cc1fb2a | 4964 | } |
8c7245ab | 4965 | return stp; |
1da177e4 LT |
4966 | } |
4967 | ||
d3134b10 JL |
4968 | /* |
4969 | * In the 4.0 case we need to keep the owners around a little while to handle | |
4970 | * CLOSE replay. We still do need to release any file access that is held by | |
4971 | * them before returning however. | |
4972 | */ | |
fd39ca9a | 4973 | static void |
d3134b10 | 4974 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
1da177e4 | 4975 | { |
217526e7 | 4976 | struct nfs4_ol_stateid *last; |
d3134b10 JL |
4977 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
4978 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, | |
4979 | nfsd_net_id); | |
73758fed | 4980 | |
fe0750e5 | 4981 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
1da177e4 | 4982 | |
b401be22 JL |
4983 | /* |
4984 | * We know that we hold one reference via nfsd4_close, and another | |
4985 | * "persistent" reference for the client. If the refcount is higher | |
4986 | * than 2, then there are still calls in progress that are using this | |
4987 | * stateid. We can't put the sc_file reference until they are finished. | |
4988 | * Wait for the refcount to drop to 2. Since it has been unhashed, | |
4989 | * there should be no danger of the refcount going back up again at | |
4990 | * this point. | |
eec76208 N |
4991 | * Some threads with a reference might be waiting for rp_locked, |
4992 | * so tell them to stop waiting. | |
b401be22 | 4993 | */ |
eec76208 N |
4994 | atomic_set(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED); |
4995 | wake_up_var(&oo->oo_owner.so_replay.rp_locked); | |
a15dfcd5 | 4996 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
b401be22 | 4997 | |
d3134b10 JL |
4998 | release_all_access(s); |
4999 | if (s->st_stid.sc_file) { | |
5000 | put_nfs4_file(s->st_stid.sc_file); | |
5001 | s->st_stid.sc_file = NULL; | |
5002 | } | |
217526e7 JL |
5003 | |
5004 | spin_lock(&nn->client_lock); | |
5005 | last = oo->oo_last_closed_stid; | |
d3134b10 | 5006 | oo->oo_last_closed_stid = s; |
73758fed | 5007 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
20b7d86f | 5008 | oo->oo_time = ktime_get_boottime_seconds(); |
217526e7 JL |
5009 | spin_unlock(&nn->client_lock); |
5010 | if (last) | |
5011 | nfs4_put_stid(&last->st_stid); | |
1da177e4 LT |
5012 | } |
5013 | ||
15424748 CL |
5014 | static noinline_for_stack struct nfs4_file * |
5015 | nfsd4_file_hash_lookup(const struct svc_fh *fhp) | |
1da177e4 | 5016 | { |
d47b295e CL |
5017 | struct inode *inode = d_inode(fhp->fh_dentry); |
5018 | struct rhlist_head *tmp, *list; | |
15424748 | 5019 | struct nfs4_file *fi; |
1da177e4 | 5020 | |
15424748 | 5021 | rcu_read_lock(); |
d47b295e CL |
5022 | list = rhltable_lookup(&nfs4_file_rhltable, &inode, |
5023 | nfs4_file_rhash_params); | |
5024 | rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { | |
15424748 CL |
5025 | if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { |
5026 | if (refcount_inc_not_zero(&fi->fi_ref)) { | |
5027 | rcu_read_unlock(); | |
5028 | return fi; | |
5029 | } | |
13cd2184 | 5030 | } |
1da177e4 | 5031 | } |
9270fc51 | 5032 | rcu_read_unlock(); |
1da177e4 LT |
5033 | return NULL; |
5034 | } | |
5035 | ||
9270fc51 CL |
5036 | /* |
5037 | * On hash insertion, identify entries with the same inode but | |
d47b295e CL |
5038 | * distinct filehandles. They will all be on the list returned |
5039 | * by rhltable_lookup(). | |
5040 | * | |
5041 | * inode->i_lock prevents racing insertions from adding an entry | |
5042 | * for the same inode/fhp pair twice. | |
9270fc51 CL |
5043 | */ |
5044 | static noinline_for_stack struct nfs4_file * | |
5045 | nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp) | |
a0ce4837 | 5046 | { |
d47b295e CL |
5047 | struct inode *inode = d_inode(fhp->fh_dentry); |
5048 | struct rhlist_head *tmp, *list; | |
a0ce4837 BF |
5049 | struct nfs4_file *ret = NULL; |
5050 | bool alias_found = false; | |
9270fc51 | 5051 | struct nfs4_file *fi; |
d47b295e | 5052 | int err; |
a0ce4837 | 5053 | |
d47b295e CL |
5054 | rcu_read_lock(); |
5055 | spin_lock(&inode->i_lock); | |
5056 | ||
5057 | list = rhltable_lookup(&nfs4_file_rhltable, &inode, | |
5058 | nfs4_file_rhash_params); | |
5059 | rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { | |
9270fc51 CL |
5060 | if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { |
5061 | if (refcount_inc_not_zero(&fi->fi_ref)) | |
5062 | ret = fi; | |
d47b295e | 5063 | } else |
9270fc51 | 5064 | fi->fi_aliased = alias_found = true; |
a0ce4837 | 5065 | } |
d47b295e CL |
5066 | if (ret) |
5067 | goto out_unlock; | |
a0ce4837 | 5068 | |
d47b295e CL |
5069 | nfsd4_file_init(fhp, new); |
5070 | err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist, | |
5071 | nfs4_file_rhash_params); | |
5072 | if (err) | |
5073 | goto out_unlock; | |
950e0118 | 5074 | |
d47b295e CL |
5075 | new->fi_aliased = alias_found; |
5076 | ret = new; | |
5077 | ||
5078 | out_unlock: | |
5079 | spin_unlock(&inode->i_lock); | |
5b095e99 | 5080 | rcu_read_unlock(); |
a0ce4837 | 5081 | return ret; |
950e0118 TM |
5082 | } |
5083 | ||
3341678f | 5084 | static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi) |
950e0118 | 5085 | { |
d47b295e CL |
5086 | rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist, |
5087 | nfs4_file_rhash_params); | |
950e0118 TM |
5088 | } |
5089 | ||
1da177e4 LT |
5090 | /* |
5091 | * Called to check deny when READ with all zero stateid or | |
5092 | * WRITE with all zero or all one stateid | |
5093 | */ | |
b37ad28b | 5094 | static __be32 |
1da177e4 LT |
5095 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
5096 | { | |
1da177e4 | 5097 | struct nfs4_file *fp; |
baeb4ff0 | 5098 | __be32 ret = nfs_ok; |
1da177e4 | 5099 | |
15424748 | 5100 | fp = nfsd4_file_hash_lookup(current_fh); |
13cd2184 | 5101 | if (!fp) |
baeb4ff0 | 5102 | return ret; |
15424748 | 5103 | |
baeb4ff0 | 5104 | /* Check for conflicting share reservations */ |
1d31a253 | 5105 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
5106 | if (fp->fi_share_deny & deny_type) |
5107 | ret = nfserr_locked; | |
1d31a253 | 5108 | spin_unlock(&fp->fi_lock); |
13cd2184 N |
5109 | put_nfs4_file(fp); |
5110 | return ret; | |
1da177e4 LT |
5111 | } |
5112 | ||
c035362e CL |
5113 | static bool nfsd4_deleg_present(const struct inode *inode) |
5114 | { | |
77c67530 | 5115 | struct file_lock_context *ctx = locks_inode_context(inode); |
c035362e CL |
5116 | |
5117 | return ctx && !list_empty_careful(&ctx->flc_lease); | |
5118 | } | |
5119 | ||
5120 | /** | |
5121 | * nfsd_wait_for_delegreturn - wait for delegations to be returned | |
5122 | * @rqstp: the RPC transaction being executed | |
5123 | * @inode: in-core inode of the file being waited for | |
5124 | * | |
5125 | * The timeout prevents deadlock if all nfsd threads happen to be | |
5126 | * tied up waiting for returning delegations. | |
5127 | * | |
5128 | * Return values: | |
5129 | * %true: delegation was returned | |
5130 | * %false: timed out waiting for delegreturn | |
5131 | */ | |
5132 | bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode) | |
5133 | { | |
5134 | long __maybe_unused timeo; | |
5135 | ||
5136 | timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode), | |
5137 | NFSD_DELEGRETURN_TIMEOUT); | |
5138 | trace_nfsd_delegret_wakeup(rqstp, inode, timeo); | |
5139 | return timeo > 0; | |
5140 | } | |
5141 | ||
0162ac2b | 5142 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
1da177e4 | 5143 | { |
0162ac2b | 5144 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
11b9164a TM |
5145 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
5146 | nfsd_net_id); | |
e8c69d17 | 5147 | |
11b9164a | 5148 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
f54fe962 | 5149 | |
dff1399f | 5150 | /* |
f54fe962 JL |
5151 | * We can't do this in nfsd_break_deleg_cb because it is |
5152 | * already holding inode->i_lock. | |
5153 | * | |
dff1399f JL |
5154 | * If the dl_time != 0, then we know that it has already been |
5155 | * queued for a lease break. Don't queue it again. | |
5156 | */ | |
f54fe962 | 5157 | spin_lock(&state_lock); |
548ec080 | 5158 | if (delegation_hashed(dp) && dp->dl_time == 0) { |
20b7d86f | 5159 | dp->dl_time = ktime_get_boottime_seconds(); |
02e1215f | 5160 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
dff1399f | 5161 | } |
02e1215f JL |
5162 | spin_unlock(&state_lock); |
5163 | } | |
1da177e4 | 5164 | |
0162ac2b CH |
5165 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
5166 | struct rpc_task *task) | |
5167 | { | |
5168 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
5169 | ||
1035d654 CL |
5170 | trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task); |
5171 | ||
3f29cc82 N |
5172 | if (dp->dl_stid.sc_status) |
5173 | /* CLOSED or REVOKED */ | |
5174 | return 1; | |
a457974f | 5175 | |
0162ac2b CH |
5176 | switch (task->tk_status) { |
5177 | case 0: | |
5178 | return 1; | |
1c73b9d2 SM |
5179 | case -NFS4ERR_DELAY: |
5180 | rpc_delay(task, 2 * HZ); | |
5181 | return 0; | |
0162ac2b CH |
5182 | case -EBADHANDLE: |
5183 | case -NFS4ERR_BAD_STATEID: | |
5184 | /* | |
5185 | * Race: client probably got cb_recall before open reply | |
5186 | * granting delegation. | |
5187 | */ | |
5188 | if (dp->dl_retries--) { | |
5189 | rpc_delay(task, 2 * HZ); | |
5190 | return 0; | |
5191 | } | |
df561f66 | 5192 | fallthrough; |
0162ac2b | 5193 | default: |
1c73b9d2 | 5194 | return 1; |
0162ac2b CH |
5195 | } |
5196 | } | |
5197 | ||
5198 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) | |
5199 | { | |
5200 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
5201 | ||
5202 | nfs4_put_stid(&dp->dl_stid); | |
5203 | } | |
5204 | ||
c4cb8974 | 5205 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
0162ac2b CH |
5206 | .prepare = nfsd4_cb_recall_prepare, |
5207 | .done = nfsd4_cb_recall_done, | |
5208 | .release = nfsd4_cb_recall_release, | |
5209 | }; | |
5210 | ||
02e1215f JL |
5211 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
5212 | { | |
5213 | /* | |
5214 | * We're assuming the state code never drops its reference | |
5215 | * without first removing the lease. Since we're in this lease | |
4a269efb | 5216 | * callback (and since the lease code is serialized by the |
25fbe1fc | 5217 | * flc_lock) we know the server hasn't removed the lease yet, and |
4a269efb | 5218 | * we know it's safe to take a reference. |
02e1215f | 5219 | */ |
a15dfcd5 | 5220 | refcount_inc(&dp->dl_stid.sc_count); |
b95239ca | 5221 | WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall)); |
6b57d9c8 BF |
5222 | } |
5223 | ||
25fbe1fc | 5224 | /* Called from break_lease() with flc_lock held. */ |
4d01b7f5 | 5225 | static bool |
c69ff407 | 5226 | nfsd_break_deleg_cb(struct file_lease *fl) |
6b57d9c8 | 5227 | { |
05580bbf | 5228 | struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner; |
653e514e | 5229 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
66af2579 DN |
5230 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
5231 | struct nfsd_net *nn; | |
6b57d9c8 | 5232 | |
17d76ddf | 5233 | trace_nfsd_cb_recall(&dp->dl_stid); |
dd5e3fbc | 5234 | |
66af2579 DN |
5235 | dp->dl_recalled = true; |
5236 | atomic_inc(&clp->cl_delegs_in_recall); | |
5237 | if (try_to_expire_client(clp)) { | |
5238 | nn = net_generic(clp->net, nfsd_net_id); | |
5239 | mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); | |
5240 | } | |
5241 | ||
0272e1fd BF |
5242 | /* |
5243 | * We don't want the locks code to timeout the lease for us; | |
acfdf5c3 | 5244 | * we'll remove it ourself if a delegation isn't returned |
6b57d9c8 | 5245 | * in time: |
0272e1fd BF |
5246 | */ |
5247 | fl->fl_break_time = 0; | |
1da177e4 | 5248 | |
417c6629 | 5249 | fp->fi_had_conflict = true; |
353601e7 | 5250 | nfsd_break_one_deleg(dp); |
b95239ca | 5251 | return false; |
1da177e4 LT |
5252 | } |
5253 | ||
50719bf3 CL |
5254 | /** |
5255 | * nfsd_breaker_owns_lease - Check if lease conflict was resolved | |
5256 | * @fl: Lock state to check | |
5257 | * | |
5258 | * Return values: | |
5259 | * %true: Lease conflict was resolved | |
5260 | * %false: Lease conflict was not resolved. | |
5261 | */ | |
c69ff407 | 5262 | static bool nfsd_breaker_owns_lease(struct file_lease *fl) |
28df3d15 | 5263 | { |
05580bbf | 5264 | struct nfs4_delegation *dl = fl->c.flc_owner; |
28df3d15 BF |
5265 | struct svc_rqst *rqst; |
5266 | struct nfs4_client *clp; | |
5267 | ||
5268 | if (!i_am_nfsd()) | |
50719bf3 | 5269 | return false; |
28df3d15 | 5270 | rqst = kthread_data(current); |
13956160 BF |
5271 | /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */ |
5272 | if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4) | |
50719bf3 | 5273 | return false; |
28df3d15 BF |
5274 | clp = *(rqst->rq_lease_breaker); |
5275 | return dl->dl_stid.sc_client == clp; | |
5276 | } | |
5277 | ||
c45198ed | 5278 | static int |
c69ff407 | 5279 | nfsd_change_deleg_cb(struct file_lease *onlist, int arg, |
7448cc37 | 5280 | struct list_head *dispose) |
1da177e4 | 5281 | { |
05580bbf | 5282 | struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner; |
66af2579 DN |
5283 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
5284 | ||
5285 | if (arg & F_UNLCK) { | |
5286 | if (dp->dl_recalled) | |
5287 | atomic_dec(&clp->cl_delegs_in_recall); | |
c45198ed | 5288 | return lease_modify(onlist, arg, dispose); |
66af2579 | 5289 | } else |
1da177e4 LT |
5290 | return -EAGAIN; |
5291 | } | |
5292 | ||
c69ff407 | 5293 | static const struct lease_manager_operations nfsd_lease_mng_ops = { |
28df3d15 | 5294 | .lm_breaker_owns_lease = nfsd_breaker_owns_lease, |
8fb47a4f BF |
5295 | .lm_break = nfsd_break_deleg_cb, |
5296 | .lm_change = nfsd_change_deleg_cb, | |
1da177e4 LT |
5297 | }; |
5298 | ||
7a8711c9 BF |
5299 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
5300 | { | |
5301 | if (nfsd4_has_session(cstate)) | |
5302 | return nfs_ok; | |
5303 | if (seqid == so->so_seqid - 1) | |
5304 | return nfserr_replay_me; | |
5305 | if (seqid == so->so_seqid) | |
5306 | return nfs_ok; | |
5307 | return nfserr_bad_seqid; | |
5308 | } | |
1da177e4 | 5309 | |
7950b531 BF |
5310 | static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, |
5311 | struct nfsd_net *nn) | |
5312 | { | |
5313 | struct nfs4_client *found; | |
5314 | ||
5315 | spin_lock(&nn->client_lock); | |
5316 | found = find_confirmed_client(clid, sessions, nn); | |
5317 | if (found) | |
5318 | atomic_inc(&found->cl_rpc_users); | |
5319 | spin_unlock(&nn->client_lock); | |
5320 | return found; | |
5321 | } | |
5322 | ||
460d2709 | 5323 | static __be32 set_client(clientid_t *clid, |
4b24ca7d | 5324 | struct nfsd4_compound_state *cstate, |
f71475ba | 5325 | struct nfsd_net *nn) |
4b24ca7d | 5326 | { |
4b24ca7d | 5327 | if (cstate->clp) { |
7950b531 | 5328 | if (!same_clid(&cstate->clp->cl_clientid, clid)) |
4b24ca7d JL |
5329 | return nfserr_stale_clientid; |
5330 | return nfs_ok; | |
5331 | } | |
4b24ca7d JL |
5332 | if (STALE_CLIENTID(clid, nn)) |
5333 | return nfserr_stale_clientid; | |
4b24ca7d | 5334 | /* |
f71475ba BF |
5335 | * We're in the 4.0 case (otherwise the SEQUENCE op would have |
5336 | * set cstate->clp), so session = false: | |
4b24ca7d | 5337 | */ |
f71475ba | 5338 | cstate->clp = lookup_clientid(clid, false, nn); |
7950b531 | 5339 | if (!cstate->clp) |
4b24ca7d | 5340 | return nfserr_expired; |
4b24ca7d JL |
5341 | return nfs_ok; |
5342 | } | |
5343 | ||
b37ad28b | 5344 | __be32 |
6668958f | 5345 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
3320fef1 | 5346 | struct nfsd4_open *open, struct nfsd_net *nn) |
1da177e4 | 5347 | { |
1da177e4 LT |
5348 | clientid_t *clientid = &open->op_clientid; |
5349 | struct nfs4_client *clp = NULL; | |
5350 | unsigned int strhashval; | |
fe0750e5 | 5351 | struct nfs4_openowner *oo = NULL; |
4cdc951b | 5352 | __be32 status; |
1da177e4 | 5353 | |
32513b40 BF |
5354 | /* |
5355 | * In case we need it later, after we've already created the | |
5356 | * file and don't want to risk a further failure: | |
5357 | */ | |
5358 | open->op_file = nfsd4_alloc_file(); | |
5359 | if (open->op_file == NULL) | |
5360 | return nfserr_jukebox; | |
1da177e4 | 5361 | |
f71475ba | 5362 | status = set_client(clientid, cstate, nn); |
2d91e895 TM |
5363 | if (status) |
5364 | return status; | |
5365 | clp = cstate->clp; | |
5366 | ||
d4f0489f | 5367 | strhashval = ownerstr_hashval(&open->op_owner); |
eec76208 | 5368 | retry: |
23df1778 | 5369 | oo = find_or_alloc_open_stateowner(strhashval, open, cstate); |
fe0750e5 | 5370 | open->op_openowner = oo; |
23df1778 N |
5371 | if (!oo) |
5372 | return nfserr_jukebox; | |
eec76208 N |
5373 | if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { |
5374 | nfs4_put_stateowner(&oo->oo_owner); | |
5375 | goto retry; | |
5376 | } | |
4cdc951b BF |
5377 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
5378 | if (status) | |
5379 | return status; | |
23df1778 | 5380 | |
b49e084d | 5381 | open->op_stp = nfs4_alloc_open_stateid(clp); |
4cdc951b BF |
5382 | if (!open->op_stp) |
5383 | return nfserr_jukebox; | |
8287f009 SB |
5384 | |
5385 | if (nfsd4_has_session(cstate) && | |
5386 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { | |
5387 | open->op_odstate = alloc_clnt_odstate(clp); | |
5388 | if (!open->op_odstate) | |
5389 | return nfserr_jukebox; | |
5390 | } | |
5391 | ||
0f442aa2 | 5392 | return nfs_ok; |
1da177e4 LT |
5393 | } |
5394 | ||
b37ad28b | 5395 | static inline __be32 |
4a6e43e6 N |
5396 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
5397 | { | |
5398 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
5399 | return nfserr_openmode; | |
5400 | else | |
5401 | return nfs_ok; | |
5402 | } | |
5403 | ||
f459e453 | 5404 | static int share_access_to_flags(u32 share_access) |
52f4fb43 | 5405 | { |
f459e453 | 5406 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
52f4fb43 N |
5407 | } |
5408 | ||
3f29cc82 N |
5409 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, |
5410 | stateid_t *s) | |
24a0111e | 5411 | { |
f459e453 | 5412 | struct nfs4_stid *ret; |
24a0111e | 5413 | |
3f29cc82 | 5414 | ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED); |
f459e453 BF |
5415 | if (!ret) |
5416 | return NULL; | |
5417 | return delegstateid(ret); | |
24a0111e BF |
5418 | } |
5419 | ||
8b289b2c BF |
5420 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
5421 | { | |
5422 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || | |
5423 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; | |
5424 | } | |
5425 | ||
b37ad28b | 5426 | static __be32 |
41d22663 | 5427 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
567d9829 N |
5428 | struct nfs4_delegation **dp) |
5429 | { | |
5430 | int flags; | |
b37ad28b | 5431 | __be32 status = nfserr_bad_stateid; |
dcd94cc2 | 5432 | struct nfs4_delegation *deleg; |
567d9829 | 5433 | |
dcd94cc2 TM |
5434 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
5435 | if (deleg == NULL) | |
c44c5eeb | 5436 | goto out; |
1ac3629b | 5437 | if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) { |
95da1b3a | 5438 | nfs4_put_stid(&deleg->dl_stid); |
1ac3629b N |
5439 | status = nfserr_admin_revoked; |
5440 | goto out; | |
5441 | } | |
3f29cc82 | 5442 | if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) { |
95da1b3a | 5443 | nfs4_put_stid(&deleg->dl_stid); |
d688d858 | 5444 | nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid); |
83e73316 | 5445 | status = nfserr_deleg_revoked; |
95da1b3a AE |
5446 | goto out; |
5447 | } | |
24a0111e | 5448 | flags = share_access_to_flags(open->op_share_access); |
dcd94cc2 TM |
5449 | status = nfs4_check_delegmode(deleg, flags); |
5450 | if (status) { | |
5451 | nfs4_put_stid(&deleg->dl_stid); | |
5452 | goto out; | |
5453 | } | |
5454 | *dp = deleg; | |
c44c5eeb | 5455 | out: |
8b289b2c | 5456 | if (!nfsd4_is_deleg_cur(open)) |
c44c5eeb N |
5457 | return nfs_ok; |
5458 | if (status) | |
5459 | return status; | |
dad1c067 | 5460 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
c44c5eeb | 5461 | return nfs_ok; |
567d9829 N |
5462 | } |
5463 | ||
21fb4016 BF |
5464 | static inline int nfs4_access_to_access(u32 nfs4_access) |
5465 | { | |
5466 | int flags = 0; | |
5467 | ||
5468 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) | |
5469 | flags |= NFSD_MAY_READ; | |
5470 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) | |
5471 | flags |= NFSD_MAY_WRITE; | |
5472 | return flags; | |
5473 | } | |
5474 | ||
7e6a72e5 CH |
5475 | static inline __be32 |
5476 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, | |
5477 | struct nfsd4_open *open) | |
5478 | { | |
5479 | struct iattr iattr = { | |
5480 | .ia_valid = ATTR_SIZE, | |
5481 | .ia_size = 0, | |
5482 | }; | |
7fe2a71d N |
5483 | struct nfsd_attrs attrs = { |
5484 | .na_iattr = &iattr, | |
5485 | }; | |
7e6a72e5 CH |
5486 | if (!open->op_truncate) |
5487 | return 0; | |
5488 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
5489 | return nfserr_inval; | |
24d92de9 | 5490 | return nfsd_setattr(rqstp, fh, &attrs, NULL); |
7e6a72e5 CH |
5491 | } |
5492 | ||
0c12eaff | 5493 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
6eb3a1d0 | 5494 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
3d694271 | 5495 | struct nfsd4_open *open, bool new_stp) |
f9d7562f | 5496 | { |
fd4f83fd | 5497 | struct nfsd_file *nf = NULL; |
f9d7562f | 5498 | __be32 status; |
0c12eaff CB |
5499 | int oflag = nfs4_access_to_omode(open->op_share_access); |
5500 | int access = nfs4_access_to_access(open->op_share_access); | |
baeb4ff0 | 5501 | unsigned char old_access_bmap, old_deny_bmap; |
0c12eaff | 5502 | |
de18643d | 5503 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
5504 | |
5505 | /* | |
5506 | * Are we trying to set a deny mode that would conflict with | |
5507 | * current access? | |
5508 | */ | |
5509 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
5510 | if (status != nfs_ok) { | |
3d694271 DN |
5511 | if (status != nfserr_share_denied) { |
5512 | spin_unlock(&fp->fi_lock); | |
5513 | goto out; | |
5514 | } | |
5515 | if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, | |
5516 | stp, open->op_share_deny, false)) | |
5517 | status = nfserr_jukebox; | |
baeb4ff0 JL |
5518 | spin_unlock(&fp->fi_lock); |
5519 | goto out; | |
5520 | } | |
5521 | ||
5522 | /* set access to the file */ | |
5523 | status = nfs4_file_get_access(fp, open->op_share_access); | |
5524 | if (status != nfs_ok) { | |
3d694271 DN |
5525 | if (status != nfserr_share_denied) { |
5526 | spin_unlock(&fp->fi_lock); | |
5527 | goto out; | |
5528 | } | |
5529 | if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, | |
5530 | stp, open->op_share_access, true)) | |
5531 | status = nfserr_jukebox; | |
baeb4ff0 JL |
5532 | spin_unlock(&fp->fi_lock); |
5533 | goto out; | |
5534 | } | |
5535 | ||
5536 | /* Set access bits in stateid */ | |
5537 | old_access_bmap = stp->st_access_bmap; | |
5538 | set_access(open->op_share_access, stp); | |
5539 | ||
5540 | /* Set new deny mask */ | |
5541 | old_deny_bmap = stp->st_deny_bmap; | |
5542 | set_deny(open->op_share_deny, stp); | |
5543 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
5544 | ||
f9d7562f | 5545 | if (!fp->fi_fds[oflag]) { |
de18643d | 5546 | spin_unlock(&fp->fi_lock); |
fb70bf12 | 5547 | |
0b3a551f JL |
5548 | status = nfsd_file_acquire_opened(rqstp, cur_fh, access, |
5549 | open->op_filp, &nf); | |
5550 | if (status != nfs_ok) | |
5551 | goto out_put_access; | |
fb70bf12 | 5552 | |
de18643d TM |
5553 | spin_lock(&fp->fi_lock); |
5554 | if (!fp->fi_fds[oflag]) { | |
fd4f83fd JL |
5555 | fp->fi_fds[oflag] = nf; |
5556 | nf = NULL; | |
de18643d | 5557 | } |
f9d7562f | 5558 | } |
de18643d | 5559 | spin_unlock(&fp->fi_lock); |
fd4f83fd JL |
5560 | if (nf) |
5561 | nfsd_file_put(nf); | |
f9d7562f | 5562 | |
217fd6f6 BF |
5563 | status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, |
5564 | access)); | |
5565 | if (status) | |
5566 | goto out_put_access; | |
5567 | ||
7e6a72e5 CH |
5568 | status = nfsd4_truncate(rqstp, cur_fh, open); |
5569 | if (status) | |
5570 | goto out_put_access; | |
7e6a72e5 CH |
5571 | out: |
5572 | return status; | |
baeb4ff0 JL |
5573 | out_put_access: |
5574 | stp->st_access_bmap = old_access_bmap; | |
5575 | nfs4_file_put_access(fp, open->op_share_access); | |
5576 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); | |
5577 | goto out; | |
1da177e4 LT |
5578 | } |
5579 | ||
b37ad28b | 5580 | static __be32 |
3d694271 DN |
5581 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, |
5582 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, | |
5583 | struct nfsd4_open *open) | |
1da177e4 | 5584 | { |
b37ad28b | 5585 | __be32 status; |
6ac75368 | 5586 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
1da177e4 | 5587 | |
6eb3a1d0 | 5588 | if (!test_access(open->op_share_access, stp)) |
3d694271 | 5589 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false); |
7e6a72e5 | 5590 | |
baeb4ff0 JL |
5591 | /* test and set deny mode */ |
5592 | spin_lock(&fp->fi_lock); | |
5593 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
dcd779dc JL |
5594 | switch (status) { |
5595 | case nfs_ok: | |
5596 | set_deny(open->op_share_deny, stp); | |
5597 | fp->fi_share_deny |= | |
5598 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
5599 | break; | |
5600 | case nfserr_share_denied: | |
5601 | if (nfs4_resolve_deny_conflicts_locked(fp, false, | |
5602 | stp, open->op_share_deny, false)) | |
5603 | status = nfserr_jukebox; | |
5604 | break; | |
baeb4ff0 JL |
5605 | } |
5606 | spin_unlock(&fp->fi_lock); | |
5607 | ||
5608 | if (status != nfs_ok) | |
1da177e4 | 5609 | return status; |
1da177e4 | 5610 | |
baeb4ff0 JL |
5611 | status = nfsd4_truncate(rqstp, cur_fh, open); |
5612 | if (status != nfs_ok) | |
5613 | reset_union_bmap_deny(old_deny_bmap, stp); | |
5614 | return status; | |
5615 | } | |
1da177e4 | 5616 | |
14a24e99 BF |
5617 | /* Should we give out recallable state?: */ |
5618 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) | |
5619 | { | |
5620 | if (clp->cl_cb_state == NFSD4_CB_UP) | |
5621 | return true; | |
5622 | /* | |
5623 | * In the sessions case, since we don't have to establish a | |
5624 | * separate connection for callbacks, we assume it's OK | |
5625 | * until we hear otherwise: | |
5626 | */ | |
5627 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; | |
5628 | } | |
5629 | ||
c69ff407 | 5630 | static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
653e514e | 5631 | int flag) |
22d38c4c | 5632 | { |
c69ff407 | 5633 | struct file_lease *fl; |
22d38c4c | 5634 | |
c69ff407 | 5635 | fl = locks_alloc_lease(); |
22d38c4c BF |
5636 | if (!fl) |
5637 | return NULL; | |
22d38c4c | 5638 | fl->fl_lmops = &nfsd_lease_mng_ops; |
05580bbf JL |
5639 | fl->c.flc_flags = FL_DELEG; |
5640 | fl->c.flc_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; | |
05580bbf JL |
5641 | fl->c.flc_owner = (fl_owner_t)dp; |
5642 | fl->c.flc_pid = current->tgid; | |
5643 | fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file; | |
22d38c4c BF |
5644 | return fl; |
5645 | } | |
5646 | ||
aba2072f BF |
5647 | static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, |
5648 | struct nfs4_file *fp) | |
5649 | { | |
5650 | struct nfs4_ol_stateid *st; | |
5651 | struct file *f = fp->fi_deleg_file->nf_file; | |
c65454a9 | 5652 | struct inode *ino = file_inode(f); |
aba2072f BF |
5653 | int writes; |
5654 | ||
5655 | writes = atomic_read(&ino->i_writecount); | |
5656 | if (!writes) | |
5657 | return 0; | |
5658 | /* | |
5659 | * There could be multiple filehandles (hence multiple | |
5660 | * nfs4_files) referencing this file, but that's not too | |
5661 | * common; let's just give up in that case rather than | |
5662 | * trying to go look up all the clients using that other | |
5663 | * nfs4_file as well: | |
5664 | */ | |
5665 | if (fp->fi_aliased) | |
5666 | return -EAGAIN; | |
5667 | /* | |
5668 | * If there's a close in progress, make sure that we see it | |
5669 | * clear any fi_fds[] entries before we see it decrement | |
5670 | * i_writecount: | |
5671 | */ | |
5672 | smp_mb__after_atomic(); | |
5673 | ||
5674 | if (fp->fi_fds[O_WRONLY]) | |
5675 | writes--; | |
5676 | if (fp->fi_fds[O_RDWR]) | |
5677 | writes--; | |
5678 | if (writes > 0) | |
5679 | return -EAGAIN; /* There may be non-NFSv4 writers */ | |
5680 | /* | |
5681 | * It's possible there are non-NFSv4 write opens in progress, | |
5682 | * but if they haven't incremented i_writecount yet then they | |
5683 | * also haven't called break lease yet; so, they'll break this | |
5684 | * lease soon enough. So, all that's left to check for is NFSv4 | |
5685 | * opens: | |
5686 | */ | |
5687 | spin_lock(&fp->fi_lock); | |
5688 | list_for_each_entry(st, &fp->fi_stateids, st_perfile) { | |
5689 | if (st->st_openstp == NULL /* it's an open */ && | |
5690 | access_permit_write(st) && | |
5691 | st->st_stid.sc_client != clp) { | |
5692 | spin_unlock(&fp->fi_lock); | |
5693 | return -EAGAIN; | |
5694 | } | |
5695 | } | |
5696 | spin_unlock(&fp->fi_lock); | |
5697 | /* | |
5698 | * There's a small chance that we could be racing with another | |
5699 | * NFSv4 open. However, any open that hasn't added itself to | |
5700 | * the fi_stateids list also hasn't called break_lease yet; so, | |
5701 | * they'll break this lease soon enough. | |
5702 | */ | |
5703 | return 0; | |
5704 | } | |
5705 | ||
876c553c JL |
5706 | /* |
5707 | * It's possible that between opening the dentry and setting the delegation, | |
5708 | * that it has been renamed or unlinked. Redo the lookup to verify that this | |
5709 | * hasn't happened. | |
5710 | */ | |
5711 | static int | |
5712 | nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp, | |
5713 | struct svc_fh *parent) | |
5714 | { | |
5715 | struct svc_export *exp; | |
5716 | struct dentry *child; | |
5717 | __be32 err; | |
5718 | ||
876c553c JL |
5719 | err = nfsd_lookup_dentry(open->op_rqstp, parent, |
5720 | open->op_fname, open->op_fnamelen, | |
5721 | &exp, &child); | |
5722 | ||
5723 | if (err) | |
5724 | return -EAGAIN; | |
5725 | ||
50256e47 | 5726 | exp_put(exp); |
876c553c JL |
5727 | dput(child); |
5728 | if (child != file_dentry(fp->fi_deleg_file->nf_file)) | |
5729 | return -EAGAIN; | |
5730 | ||
5731 | return 0; | |
5732 | } | |
5733 | ||
826b67e6 JL |
5734 | /* |
5735 | * We avoid breaking delegations held by a client due to its own activity, but | |
5736 | * clearing setuid/setgid bits on a write is an implicit activity and the client | |
5737 | * may not notice and continue using the old mode. Avoid giving out a delegation | |
5738 | * on setuid/setgid files when the client is requesting an open for write. | |
5739 | */ | |
5740 | static int | |
5741 | nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf) | |
5742 | { | |
5743 | struct inode *inode = file_inode(nf->nf_file); | |
5744 | ||
5745 | if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) && | |
5746 | (inode->i_mode & (S_ISUID|S_ISGID))) | |
5747 | return -EAGAIN; | |
5748 | return 0; | |
5749 | } | |
5750 | ||
0b26693c | 5751 | static struct nfs4_delegation * |
876c553c JL |
5752 | nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, |
5753 | struct svc_fh *parent) | |
acfdf5c3 | 5754 | { |
68b18f52 | 5755 | int status = 0; |
876c553c JL |
5756 | struct nfs4_client *clp = stp->st_stid.sc_client; |
5757 | struct nfs4_file *fp = stp->st_stid.sc_file; | |
5758 | struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; | |
0b26693c | 5759 | struct nfs4_delegation *dp; |
1d3dd1d5 | 5760 | struct nfsd_file *nf = NULL; |
c69ff407 | 5761 | struct file_lease *fl; |
1d3dd1d5 | 5762 | u32 dl_type; |
417c6629 | 5763 | |
353601e7 BF |
5764 | /* |
5765 | * The fi_had_conflict and nfs_get_existing_delegation checks | |
5766 | * here are just optimizations; we'll need to recheck them at | |
5767 | * the end: | |
5768 | */ | |
bf7bd3e9 | 5769 | if (fp->fi_had_conflict) |
0b26693c JL |
5770 | return ERR_PTR(-EAGAIN); |
5771 | ||
1d3dd1d5 DN |
5772 | /* |
5773 | * Try for a write delegation first. RFC8881 section 10.4 says: | |
5774 | * | |
5775 | * "An OPEN_DELEGATE_WRITE delegation allows the client to handle, | |
5776 | * on its own, all opens." | |
5777 | * | |
5778 | * Furthermore the client can use a write delegation for most READ | |
5779 | * operations as well, so we require a O_RDWR file here. | |
5780 | * | |
5781 | * Offer a write delegation in the case of a BOTH open, and ensure | |
5782 | * we get the O_RDWR descriptor. | |
5783 | */ | |
5784 | if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) { | |
5785 | nf = find_rw_file(fp); | |
5786 | dl_type = NFS4_OPEN_DELEGATE_WRITE; | |
353601e7 | 5787 | } |
1d3dd1d5 DN |
5788 | |
5789 | /* | |
5790 | * If the file is being opened O_RDONLY or we couldn't get a O_RDWR | |
5791 | * file for some reason, then try for a read delegation instead. | |
5792 | */ | |
5793 | if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) { | |
5794 | nf = find_readable_file(fp); | |
5795 | dl_type = NFS4_OPEN_DELEGATE_READ; | |
5796 | } | |
5797 | ||
5798 | if (!nf) | |
5799 | return ERR_PTR(-EAGAIN); | |
5800 | ||
34ed9872 AE |
5801 | spin_lock(&state_lock); |
5802 | spin_lock(&fp->fi_lock); | |
68b18f52 BF |
5803 | if (nfs4_delegation_exists(clp, fp)) |
5804 | status = -EAGAIN; | |
826b67e6 JL |
5805 | else if (nfsd4_verify_setuid_write(open, nf)) |
5806 | status = -EAGAIN; | |
353601e7 | 5807 | else if (!fp->fi_deleg_file) { |
eb82dd39 | 5808 | fp->fi_deleg_file = nf; |
353601e7 BF |
5809 | /* increment early to prevent fi_deleg_file from being |
5810 | * cleared */ | |
5811 | fp->fi_delegees = 1; | |
eb82dd39 | 5812 | nf = NULL; |
353601e7 BF |
5813 | } else |
5814 | fp->fi_delegees++; | |
34ed9872 AE |
5815 | spin_unlock(&fp->fi_lock); |
5816 | spin_unlock(&state_lock); | |
eb82dd39 JL |
5817 | if (nf) |
5818 | nfsd_file_put(nf); | |
34ed9872 AE |
5819 | if (status) |
5820 | return ERR_PTR(status); | |
5821 | ||
353601e7 | 5822 | status = -ENOMEM; |
1d3dd1d5 | 5823 | dp = alloc_init_deleg(clp, fp, odstate, dl_type); |
0b26693c | 5824 | if (!dp) |
353601e7 BF |
5825 | goto out_delegees; |
5826 | ||
1d3dd1d5 | 5827 | fl = nfs4_alloc_init_lease(dp, dl_type); |
353601e7 | 5828 | if (!fl) |
bd8d7250 | 5829 | goto out_clnt_odstate; |
353601e7 | 5830 | |
7b800101 JL |
5831 | status = kernel_setlease(fp->fi_deleg_file->nf_file, |
5832 | fl->c.flc_type, &fl, NULL); | |
353601e7 | 5833 | if (fl) |
c69ff407 | 5834 | locks_free_lease(fl); |
353601e7 BF |
5835 | if (status) |
5836 | goto out_clnt_odstate; | |
876c553c JL |
5837 | |
5838 | if (parent) { | |
5839 | status = nfsd4_verify_deleg_dentry(open, fp, parent); | |
5840 | if (status) | |
5841 | goto out_unlock; | |
5842 | } | |
5843 | ||
aba2072f BF |
5844 | status = nfsd4_check_conflicting_opens(clp, fp); |
5845 | if (status) | |
5846 | goto out_unlock; | |
0b26693c | 5847 | |
826b67e6 JL |
5848 | /* |
5849 | * Now that the deleg is set, check again to ensure that nothing | |
5850 | * raced in and changed the mode while we weren't lookng. | |
5851 | */ | |
5852 | status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file); | |
5853 | if (status) | |
5854 | goto out_unlock; | |
5855 | ||
5ea9a7c5 N |
5856 | status = -EAGAIN; |
5857 | if (fp->fi_had_conflict) | |
5858 | goto out_unlock; | |
5859 | ||
417c6629 | 5860 | spin_lock(&state_lock); |
77945728 | 5861 | spin_lock(&clp->cl_lock); |
417c6629 | 5862 | spin_lock(&fp->fi_lock); |
5ea9a7c5 | 5863 | status = hash_delegation_locked(dp, fp); |
417c6629 | 5864 | spin_unlock(&fp->fi_lock); |
77945728 | 5865 | spin_unlock(&clp->cl_lock); |
cdc97505 | 5866 | spin_unlock(&state_lock); |
353601e7 BF |
5867 | |
5868 | if (status) | |
692ad280 AE |
5869 | goto out_unlock; |
5870 | ||
0b26693c | 5871 | return dp; |
692ad280 | 5872 | out_unlock: |
7b800101 | 5873 | kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp); |
353601e7 BF |
5874 | out_clnt_odstate: |
5875 | put_clnt_odstate(dp->dl_clnt_odstate); | |
353601e7 BF |
5876 | nfs4_put_stid(&dp->dl_stid); |
5877 | out_delegees: | |
5878 | put_deleg_file(fp); | |
5879 | return ERR_PTR(status); | |
edab9782 BF |
5880 | } |
5881 | ||
4aa8913c BH |
5882 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
5883 | { | |
5884 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
5885 | if (status == -EAGAIN) | |
5886 | open->op_why_no_deleg = WND4_CONTENTION; | |
5887 | else { | |
5888 | open->op_why_no_deleg = WND4_RESOURCE; | |
5889 | switch (open->op_deleg_want) { | |
5890 | case NFS4_SHARE_WANT_READ_DELEG: | |
5891 | case NFS4_SHARE_WANT_WRITE_DELEG: | |
5892 | case NFS4_SHARE_WANT_ANY_DELEG: | |
5893 | break; | |
5894 | case NFS4_SHARE_WANT_CANCEL: | |
5895 | open->op_why_no_deleg = WND4_CANCELLED; | |
5896 | break; | |
5897 | case NFS4_SHARE_WANT_NO_DELEG: | |
063b0fb9 | 5898 | WARN_ON_ONCE(1); |
4aa8913c BH |
5899 | } |
5900 | } | |
5901 | } | |
5902 | ||
1da177e4 | 5903 | /* |
1d3dd1d5 DN |
5904 | * The Linux NFS server does not offer write delegations to NFSv4.0 |
5905 | * clients in order to avoid conflicts between write delegations and | |
5906 | * GETATTRs requesting CHANGE or SIZE attributes. | |
5907 | * | |
5908 | * With NFSv4.1 and later minorversions, the SEQUENCE operation that | |
5909 | * begins each COMPOUND contains a client ID. Delegation recall can | |
5910 | * be avoided when the server recognizes the client sending a | |
5911 | * GETATTR also holds write delegation it conflicts with. | |
5912 | * | |
5913 | * However, the NFSv4.0 protocol does not enable a server to | |
5914 | * determine that a GETATTR originated from the client holding the | |
5915 | * conflicting delegation versus coming from some other client. Per | |
5916 | * RFC 7530 Section 16.7.5, the server must recall or send a | |
5917 | * CB_GETATTR even when the GETATTR originates from the client that | |
5918 | * holds the conflicting delegation. | |
99c41515 | 5919 | * |
1d3dd1d5 DN |
5920 | * An NFSv4.0 client can trigger a pathological situation if it |
5921 | * always sends a DELEGRETURN preceded by a conflicting GETATTR in | |
5922 | * the same COMPOUND. COMPOUND execution will always stop at the | |
5923 | * GETATTR and the DELEGRETURN will never get executed. The server | |
5924 | * eventually revokes the delegation, which can result in loss of | |
5925 | * open or lock state. | |
1da177e4 LT |
5926 | */ |
5927 | static void | |
876c553c JL |
5928 | nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, |
5929 | struct svc_fh *currentfh) | |
1da177e4 LT |
5930 | { |
5931 | struct nfs4_delegation *dp; | |
4cf59221 JL |
5932 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
5933 | struct nfs4_client *clp = stp->st_stid.sc_client; | |
876c553c | 5934 | struct svc_fh *parent = NULL; |
14a24e99 | 5935 | int cb_up; |
99c41515 | 5936 | int status = 0; |
c5967721 DN |
5937 | struct kstat stat; |
5938 | struct path path; | |
1da177e4 | 5939 | |
fe0750e5 | 5940 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
e4ad7ce7 | 5941 | open->op_recall = false; |
7b190fec N |
5942 | switch (open->op_claim_type) { |
5943 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
2bf23875 | 5944 | if (!cb_up) |
e4ad7ce7 | 5945 | open->op_recall = true; |
7b190fec N |
5946 | break; |
5947 | case NFS4_OPEN_CLAIM_NULL: | |
876c553c JL |
5948 | parent = currentfh; |
5949 | fallthrough; | |
ed47b062 | 5950 | case NFS4_OPEN_CLAIM_FH: |
99c41515 BF |
5951 | /* |
5952 | * Let's not give out any delegations till everyone's | |
c87fb4a3 BF |
5953 | * had the chance to reclaim theirs, *and* until |
5954 | * NLM locks have all been reclaimed: | |
99c41515 | 5955 | */ |
4cf59221 | 5956 | if (locks_in_grace(clp->net)) |
99c41515 | 5957 | goto out_no_deleg; |
dad1c067 | 5958 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
99c41515 | 5959 | goto out_no_deleg; |
1d3dd1d5 DN |
5960 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE && |
5961 | !clp->cl_minorversion) | |
5962 | goto out_no_deleg; | |
7b190fec N |
5963 | break; |
5964 | default: | |
99c41515 | 5965 | goto out_no_deleg; |
7b190fec | 5966 | } |
876c553c | 5967 | dp = nfs4_set_delegation(open, stp, parent); |
0b26693c | 5968 | if (IS_ERR(dp)) |
dd239cc0 | 5969 | goto out_no_deleg; |
1da177e4 | 5970 | |
d5477a8d | 5971 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
1da177e4 | 5972 | |
1d3dd1d5 DN |
5973 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { |
5974 | open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE; | |
5975 | trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid); | |
c5967721 DN |
5976 | path.mnt = currentfh->fh_export->ex_path.mnt; |
5977 | path.dentry = currentfh->fh_dentry; | |
5978 | if (vfs_getattr(&path, &stat, | |
5979 | (STATX_SIZE | STATX_CTIME | STATX_CHANGE_COOKIE), | |
5980 | AT_STATX_SYNC_AS_STAT)) { | |
5981 | nfs4_put_stid(&dp->dl_stid); | |
5982 | destroy_delegation(dp); | |
5983 | goto out_no_deleg; | |
5984 | } | |
5985 | dp->dl_cb_fattr.ncf_cur_fsize = stat.size; | |
5986 | dp->dl_cb_fattr.ncf_initial_cinfo = | |
5987 | nfsd4_change_attribute(&stat, d_inode(currentfh->fh_dentry)); | |
1d3dd1d5 DN |
5988 | } else { |
5989 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; | |
5990 | trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); | |
5991 | } | |
67cb1279 | 5992 | nfs4_put_stid(&dp->dl_stid); |
dd239cc0 | 5993 | return; |
dd239cc0 | 5994 | out_no_deleg: |
99c41515 BF |
5995 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
5996 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && | |
d08d32e6 | 5997 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
99c41515 | 5998 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
e4ad7ce7 | 5999 | open->op_recall = true; |
d08d32e6 | 6000 | } |
99c41515 BF |
6001 | |
6002 | /* 4.1 client asking for a delegation? */ | |
6003 | if (open->op_deleg_want) | |
6004 | nfsd4_open_deleg_none_ext(open, status); | |
6005 | return; | |
1da177e4 LT |
6006 | } |
6007 | ||
e27f49c3 BH |
6008 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
6009 | struct nfs4_delegation *dp) | |
6010 | { | |
6011 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && | |
6012 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
6013 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
6014 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; | |
6015 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && | |
6016 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
6017 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
6018 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; | |
6019 | } | |
6020 | /* Otherwise the client must be confused wanting a delegation | |
6021 | * it already has, therefore we don't return | |
6022 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. | |
6023 | */ | |
6024 | } | |
6025 | ||
7e2ce0cc CL |
6026 | /** |
6027 | * nfsd4_process_open2 - finish open processing | |
6028 | * @rqstp: the RPC transaction being executed | |
6029 | * @current_fh: NFSv4 COMPOUND's current filehandle | |
6030 | * @open: OPEN arguments | |
6031 | * | |
6032 | * If successful, (1) truncate the file if open->op_truncate was | |
6033 | * set, (2) set open->op_stateid, (3) set open->op_delegation. | |
6034 | * | |
6035 | * Returns %nfs_ok on success; otherwise an nfs4stat value in | |
6036 | * network byte order is returned. | |
6037 | */ | |
b37ad28b | 6038 | __be32 |
1da177e4 LT |
6039 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
6040 | { | |
6668958f | 6041 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
38c2f4b1 | 6042 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
1da177e4 | 6043 | struct nfs4_file *fp = NULL; |
dcef0413 | 6044 | struct nfs4_ol_stateid *stp = NULL; |
567d9829 | 6045 | struct nfs4_delegation *dp = NULL; |
b37ad28b | 6046 | __be32 status; |
d8a1a000 | 6047 | bool new_stp = false; |
1da177e4 | 6048 | |
1da177e4 LT |
6049 | /* |
6050 | * Lookup file; if found, lookup stateid and check open request, | |
6051 | * and check for delegations in the process of being recalled. | |
6052 | * If not found, create the nfs4_file struct | |
6053 | */ | |
9270fc51 | 6054 | fp = nfsd4_file_hash_insert(open->op_file, current_fh); |
d47b295e CL |
6055 | if (unlikely(!fp)) |
6056 | return nfserr_jukebox; | |
950e0118 | 6057 | if (fp != open->op_file) { |
41d22663 | 6058 | status = nfs4_check_deleg(cl, open, &dp); |
c44c5eeb N |
6059 | if (status) |
6060 | goto out; | |
15ca08d3 | 6061 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
1da177e4 | 6062 | } else { |
950e0118 | 6063 | open->op_file = NULL; |
c44c5eeb | 6064 | status = nfserr_bad_stateid; |
8b289b2c | 6065 | if (nfsd4_is_deleg_cur(open)) |
c44c5eeb | 6066 | goto out; |
1da177e4 LT |
6067 | } |
6068 | ||
d8a1a000 TM |
6069 | if (!stp) { |
6070 | stp = init_open_stateid(fp, open); | |
6071 | if (!open->op_stp) | |
6072 | new_stp = true; | |
6073 | } | |
6074 | ||
1da177e4 LT |
6075 | /* |
6076 | * OPEN the file, or upgrade an existing OPEN. | |
6077 | * If truncate fails, the OPEN fails. | |
d8a1a000 TM |
6078 | * |
6079 | * stp is already locked. | |
1da177e4 | 6080 | */ |
d8a1a000 | 6081 | if (!new_stp) { |
1da177e4 | 6082 | /* Stateid was found, this is an OPEN upgrade */ |
f9d7562f | 6083 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
35a92fe8 | 6084 | if (status) { |
feb9dad5 | 6085 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 6086 | goto out; |
35a92fe8 | 6087 | } |
1da177e4 | 6088 | } else { |
3d694271 | 6089 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true); |
6eb3a1d0 JL |
6090 | if (status) { |
6091 | release_open_stateid(stp); | |
d8a1a000 | 6092 | mutex_unlock(&stp->st_mutex); |
6eb3a1d0 JL |
6093 | goto out; |
6094 | } | |
8287f009 SB |
6095 | |
6096 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, | |
6097 | open->op_odstate); | |
6098 | if (stp->st_clnt_odstate == open->op_odstate) | |
6099 | open->op_odstate = NULL; | |
1da177e4 | 6100 | } |
d8a1a000 | 6101 | |
9767feb2 | 6102 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
feb9dad5 | 6103 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 6104 | |
d24433cd | 6105 | if (nfsd4_has_session(&resp->cstate)) { |
d24433cd BH |
6106 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
6107 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
6108 | open->op_why_no_deleg = WND4_NOT_WANTED; | |
6109 | goto nodeleg; | |
6110 | } | |
6111 | } | |
6112 | ||
1da177e4 LT |
6113 | /* |
6114 | * Attempt to hand out a delegation. No error return, because the | |
6115 | * OPEN succeeds even if we fail. | |
6116 | */ | |
876c553c | 6117 | nfs4_open_delegation(open, stp, &resp->cstate.current_fh); |
d24433cd | 6118 | nodeleg: |
1da177e4 | 6119 | status = nfs_ok; |
3caf9175 | 6120 | trace_nfsd_open(&stp->st_stid.sc_stateid); |
1da177e4 | 6121 | out: |
d24433cd BH |
6122 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
6123 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && | |
e27f49c3 BH |
6124 | open->op_deleg_want) |
6125 | nfsd4_deleg_xgrade_none_ext(open, dp); | |
d24433cd | 6126 | |
13cd2184 N |
6127 | if (fp) |
6128 | put_nfs4_file(fp); | |
37515177 | 6129 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
87186022 | 6130 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
1da177e4 LT |
6131 | /* |
6132 | * To finish the open response, we just need to set the rflags. | |
6133 | */ | |
6134 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
19e4c347 JL |
6135 | if (nfsd4_has_session(&resp->cstate)) |
6136 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; | |
6137 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) | |
1da177e4 | 6138 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
19e4c347 | 6139 | |
dcd94cc2 TM |
6140 | if (dp) |
6141 | nfs4_put_stid(&dp->dl_stid); | |
d6f2bc5d TM |
6142 | if (stp) |
6143 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 LT |
6144 | |
6145 | return status; | |
6146 | } | |
6147 | ||
58fb12e6 | 6148 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
42297899 | 6149 | struct nfsd4_open *open) |
d29b20cd | 6150 | { |
b3f03739 N |
6151 | if (open->op_openowner) |
6152 | nfs4_put_stateowner(&open->op_openowner->oo_owner); | |
32513b40 | 6153 | if (open->op_file) |
5b095e99 | 6154 | kmem_cache_free(file_slab, open->op_file); |
4cdc951b | 6155 | if (open->op_stp) |
6011695d | 6156 | nfs4_put_stid(&open->op_stp->st_stid); |
8287f009 SB |
6157 | if (open->op_odstate) |
6158 | kmem_cache_free(odstate_slab, open->op_odstate); | |
d29b20cd BF |
6159 | } |
6160 | ||
b37ad28b | 6161 | __be32 |
b591480b | 6162 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6163 | union nfsd4_op_u *u) |
1da177e4 | 6164 | { |
eb69853d | 6165 | clientid_t *clid = &u->renew; |
1da177e4 | 6166 | struct nfs4_client *clp; |
b37ad28b | 6167 | __be32 status; |
7f2210fa | 6168 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 6169 | |
dd5e3fbc | 6170 | trace_nfsd_clid_renew(clid); |
f71475ba | 6171 | status = set_client(clid, cstate, nn); |
9b2ef62b | 6172 | if (status) |
b4587eb2 | 6173 | return status; |
4b24ca7d | 6174 | clp = cstate->clp; |
ea1da636 | 6175 | if (!list_empty(&clp->cl_delegations) |
77a3569d | 6176 | && clp->cl_cb_state != NFSD4_CB_UP) |
b4587eb2 BF |
6177 | return nfserr_cb_path_down; |
6178 | return nfs_ok; | |
1da177e4 LT |
6179 | } |
6180 | ||
7f5ef2e9 | 6181 | void |
12760c66 | 6182 | nfsd4_end_grace(struct nfsd_net *nn) |
a76b4319 | 6183 | { |
33dcc481 | 6184 | /* do nothing if grace period already ended */ |
a51c84ed | 6185 | if (nn->grace_ended) |
33dcc481 JL |
6186 | return; |
6187 | ||
dd5e3fbc | 6188 | trace_nfsd_grace_complete(nn); |
a51c84ed | 6189 | nn->grace_ended = true; |
70b28235 BF |
6190 | /* |
6191 | * If the server goes down again right now, an NFSv4 | |
6192 | * client will still be allowed to reclaim after it comes back up, | |
6193 | * even if it hasn't yet had a chance to reclaim state this time. | |
6194 | * | |
6195 | */ | |
919b8049 | 6196 | nfsd4_record_grace_done(nn); |
70b28235 BF |
6197 | /* |
6198 | * At this point, NFSv4 clients can still reclaim. But if the | |
6199 | * server crashes, any that have not yet reclaimed will be out | |
6200 | * of luck on the next boot. | |
6201 | * | |
6202 | * (NFSv4.1+ clients are considered to have reclaimed once they | |
6203 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to | |
6204 | * have reclaimed after their first OPEN.) | |
6205 | */ | |
5e1533c7 | 6206 | locks_end_grace(&nn->nfsd4_manager); |
70b28235 BF |
6207 | /* |
6208 | * At this point, and once lockd and/or any other containers | |
6209 | * exit their grace period, further reclaims will fail and | |
6210 | * regular locking can resume. | |
6211 | */ | |
a76b4319 N |
6212 | } |
6213 | ||
03f318ca BF |
6214 | /* |
6215 | * If we've waited a lease period but there are still clients trying to | |
6216 | * reclaim, wait a little longer to give them a chance to finish. | |
6217 | */ | |
6218 | static bool clients_still_reclaiming(struct nfsd_net *nn) | |
6219 | { | |
20b7d86f AB |
6220 | time64_t double_grace_period_end = nn->boot_time + |
6221 | 2 * nn->nfsd4_lease; | |
03f318ca | 6222 | |
362063a5 SM |
6223 | if (nn->track_reclaim_completes && |
6224 | atomic_read(&nn->nr_reclaim_complete) == | |
6225 | nn->reclaim_str_hashtbl_size) | |
6226 | return false; | |
03f318ca BF |
6227 | if (!nn->somebody_reclaimed) |
6228 | return false; | |
6229 | nn->somebody_reclaimed = false; | |
6230 | /* | |
6231 | * If we've given them *two* lease times to reclaim, and they're | |
6232 | * still not done, give up: | |
6233 | */ | |
20b7d86f | 6234 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
03f318ca BF |
6235 | return false; |
6236 | return true; | |
6237 | } | |
6238 | ||
7f7e7a40 BF |
6239 | struct laundry_time { |
6240 | time64_t cutoff; | |
6241 | time64_t new_timeo; | |
6242 | }; | |
6243 | ||
6244 | static bool state_expired(struct laundry_time *lt, time64_t last_refresh) | |
6245 | { | |
6246 | time64_t time_remaining; | |
6247 | ||
6248 | if (last_refresh < lt->cutoff) | |
6249 | return true; | |
6250 | time_remaining = last_refresh - lt->cutoff; | |
6251 | lt->new_timeo = min(lt->new_timeo, time_remaining); | |
6252 | return false; | |
6253 | } | |
6254 | ||
f4e44b39 DN |
6255 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
6256 | void nfsd4_ssc_init_umount_work(struct nfsd_net *nn) | |
6257 | { | |
6258 | spin_lock_init(&nn->nfsd_ssc_lock); | |
6259 | INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list); | |
6260 | init_waitqueue_head(&nn->nfsd_ssc_waitq); | |
6261 | } | |
6262 | EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work); | |
6263 | ||
6264 | /* | |
6265 | * This is called when nfsd is being shutdown, after all inter_ssc | |
6266 | * cleanup were done, to destroy the ssc delayed unmount list. | |
6267 | */ | |
6268 | static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn) | |
6269 | { | |
f47dc2d3 | 6270 | struct nfsd4_ssc_umount_item *ni = NULL; |
f4e44b39 DN |
6271 | struct nfsd4_ssc_umount_item *tmp; |
6272 | ||
6273 | spin_lock(&nn->nfsd_ssc_lock); | |
6274 | list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { | |
6275 | list_del(&ni->nsui_list); | |
6276 | spin_unlock(&nn->nfsd_ssc_lock); | |
6277 | mntput(ni->nsui_vfsmount); | |
6278 | kfree(ni); | |
6279 | spin_lock(&nn->nfsd_ssc_lock); | |
6280 | } | |
6281 | spin_unlock(&nn->nfsd_ssc_lock); | |
6282 | } | |
6283 | ||
6284 | static void nfsd4_ssc_expire_umount(struct nfsd_net *nn) | |
6285 | { | |
6286 | bool do_wakeup = false; | |
8e70bf27 | 6287 | struct nfsd4_ssc_umount_item *ni = NULL; |
f4e44b39 DN |
6288 | struct nfsd4_ssc_umount_item *tmp; |
6289 | ||
6290 | spin_lock(&nn->nfsd_ssc_lock); | |
6291 | list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { | |
6292 | if (time_after(jiffies, ni->nsui_expire)) { | |
6293 | if (refcount_read(&ni->nsui_refcnt) > 1) | |
6294 | continue; | |
6295 | ||
6296 | /* mark being unmount */ | |
6297 | ni->nsui_busy = true; | |
6298 | spin_unlock(&nn->nfsd_ssc_lock); | |
6299 | mntput(ni->nsui_vfsmount); | |
6300 | spin_lock(&nn->nfsd_ssc_lock); | |
6301 | ||
6302 | /* waiters need to start from begin of list */ | |
6303 | list_del(&ni->nsui_list); | |
6304 | kfree(ni); | |
6305 | ||
6306 | /* wakeup ssc_connect waiters */ | |
6307 | do_wakeup = true; | |
6308 | continue; | |
6309 | } | |
6310 | break; | |
6311 | } | |
6312 | if (do_wakeup) | |
6313 | wake_up_all(&nn->nfsd_ssc_waitq); | |
6314 | spin_unlock(&nn->nfsd_ssc_lock); | |
6315 | } | |
6316 | #endif | |
6317 | ||
27431aff | 6318 | /* Check if any lock belonging to this lockowner has any blockers */ |
3d694271 | 6319 | static bool |
27431aff DN |
6320 | nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo) |
6321 | { | |
6322 | struct file_lock_context *ctx; | |
6323 | struct nfs4_ol_stateid *stp; | |
6324 | struct nfs4_file *nf; | |
6325 | ||
6326 | list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { | |
6327 | nf = stp->st_stid.sc_file; | |
77c67530 | 6328 | ctx = locks_inode_context(nf->fi_inode); |
27431aff DN |
6329 | if (!ctx) |
6330 | continue; | |
6331 | if (locks_owner_has_blockers(ctx, lo)) | |
6332 | return true; | |
6333 | } | |
6334 | return false; | |
6335 | } | |
6336 | ||
6337 | static bool | |
6338 | nfs4_anylock_blockers(struct nfs4_client *clp) | |
3d694271 DN |
6339 | { |
6340 | int i; | |
6341 | struct nfs4_stateowner *so; | |
27431aff | 6342 | struct nfs4_lockowner *lo; |
3d694271 | 6343 | |
27431aff DN |
6344 | if (atomic_read(&clp->cl_delegs_in_recall)) |
6345 | return true; | |
3d694271 DN |
6346 | spin_lock(&clp->cl_lock); |
6347 | for (i = 0; i < OWNER_HASH_SIZE; i++) { | |
6348 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i], | |
6349 | so_strhash) { | |
6350 | if (so->so_is_open_owner) | |
6351 | continue; | |
27431aff DN |
6352 | lo = lockowner(so); |
6353 | if (nfs4_lockowner_has_blockers(lo)) { | |
6354 | spin_unlock(&clp->cl_lock); | |
6355 | return true; | |
6356 | } | |
3d694271 DN |
6357 | } |
6358 | } | |
6359 | spin_unlock(&clp->cl_lock); | |
6360 | return false; | |
6361 | } | |
6362 | ||
66af2579 DN |
6363 | static void |
6364 | nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist, | |
6365 | struct laundry_time *lt) | |
6366 | { | |
4271c2c0 | 6367 | unsigned int maxreap, reapcnt = 0; |
66af2579 DN |
6368 | struct list_head *pos, *next; |
6369 | struct nfs4_client *clp; | |
6370 | ||
4271c2c0 DN |
6371 | maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ? |
6372 | NFSD_CLIENT_MAX_TRIM_PER_RUN : 0; | |
66af2579 DN |
6373 | INIT_LIST_HEAD(reaplist); |
6374 | spin_lock(&nn->client_lock); | |
6375 | list_for_each_safe(pos, next, &nn->client_lru) { | |
6376 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
6377 | if (clp->cl_state == NFSD4_EXPIRABLE) | |
6378 | goto exp_client; | |
6379 | if (!state_expired(lt, clp->cl_time)) | |
6380 | break; | |
3a4ea23d DN |
6381 | if (!atomic_read(&clp->cl_rpc_users)) { |
6382 | if (clp->cl_state == NFSD4_ACTIVE) | |
6383 | atomic_inc(&nn->nfsd_courtesy_clients); | |
66af2579 | 6384 | clp->cl_state = NFSD4_COURTESY; |
3a4ea23d | 6385 | } |
4271c2c0 | 6386 | if (!client_has_state(clp)) |
66af2579 | 6387 | goto exp_client; |
4271c2c0 DN |
6388 | if (!nfs4_anylock_blockers(clp)) |
6389 | if (reapcnt >= maxreap) | |
6390 | continue; | |
66af2579 | 6391 | exp_client: |
4271c2c0 DN |
6392 | if (!mark_client_expired_locked(clp)) { |
6393 | list_add(&clp->cl_lru, reaplist); | |
6394 | reapcnt++; | |
66af2579 DN |
6395 | } |
6396 | } | |
6397 | spin_unlock(&nn->client_lock); | |
6398 | } | |
6399 | ||
7746b32f DN |
6400 | static void |
6401 | nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn, | |
6402 | struct list_head *reaplist) | |
6403 | { | |
6404 | unsigned int maxreap = 0, reapcnt = 0; | |
6405 | struct list_head *pos, *next; | |
6406 | struct nfs4_client *clp; | |
6407 | ||
6408 | maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN; | |
6409 | INIT_LIST_HEAD(reaplist); | |
6410 | ||
6411 | spin_lock(&nn->client_lock); | |
6412 | list_for_each_safe(pos, next, &nn->client_lru) { | |
6413 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
6414 | if (clp->cl_state == NFSD4_ACTIVE) | |
6415 | break; | |
6416 | if (reapcnt >= maxreap) | |
6417 | break; | |
6418 | if (!mark_client_expired_locked(clp)) { | |
6419 | list_add(&clp->cl_lru, reaplist); | |
6420 | reapcnt++; | |
6421 | } | |
6422 | } | |
6423 | spin_unlock(&nn->client_lock); | |
6424 | } | |
6425 | ||
6426 | static void | |
6427 | nfs4_process_client_reaplist(struct list_head *reaplist) | |
6428 | { | |
6429 | struct list_head *pos, *next; | |
6430 | struct nfs4_client *clp; | |
6431 | ||
6432 | list_for_each_safe(pos, next, reaplist) { | |
6433 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
6434 | trace_nfsd_clid_purged(&clp->cl_clientid); | |
6435 | list_del_init(&clp->cl_lru); | |
6436 | expire_client(clp); | |
6437 | } | |
6438 | } | |
6439 | ||
d688d858 N |
6440 | static void nfs40_clean_admin_revoked(struct nfsd_net *nn, |
6441 | struct laundry_time *lt) | |
6442 | { | |
6443 | struct nfs4_client *clp; | |
6444 | ||
6445 | spin_lock(&nn->client_lock); | |
6446 | if (nn->nfs40_last_revoke == 0 || | |
6447 | nn->nfs40_last_revoke > lt->cutoff) { | |
6448 | spin_unlock(&nn->client_lock); | |
6449 | return; | |
6450 | } | |
6451 | nn->nfs40_last_revoke = 0; | |
6452 | ||
6453 | retry: | |
6454 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6455 | unsigned long id, tmp; | |
6456 | struct nfs4_stid *stid; | |
6457 | ||
6458 | if (atomic_read(&clp->cl_admin_revoked) == 0) | |
6459 | continue; | |
6460 | ||
6461 | spin_lock(&clp->cl_lock); | |
6462 | idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) | |
6463 | if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { | |
6464 | refcount_inc(&stid->sc_count); | |
6465 | spin_unlock(&nn->client_lock); | |
6466 | /* this function drops ->cl_lock */ | |
6467 | nfsd4_drop_revoked_stid(stid); | |
6468 | nfs4_put_stid(stid); | |
6469 | spin_lock(&nn->client_lock); | |
6470 | goto retry; | |
6471 | } | |
6472 | spin_unlock(&clp->cl_lock); | |
6473 | } | |
6474 | spin_unlock(&nn->client_lock); | |
6475 | } | |
6476 | ||
20b7d86f | 6477 | static time64_t |
09121281 | 6478 | nfs4_laundromat(struct nfsd_net *nn) |
1da177e4 | 6479 | { |
fe0750e5 | 6480 | struct nfs4_openowner *oo; |
1da177e4 | 6481 | struct nfs4_delegation *dp; |
217526e7 | 6482 | struct nfs4_ol_stateid *stp; |
7919d0a2 | 6483 | struct nfsd4_blocked_lock *nbl; |
1da177e4 | 6484 | struct list_head *pos, *next, reaplist; |
7f7e7a40 BF |
6485 | struct laundry_time lt = { |
6486 | .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, | |
6487 | .new_timeo = nn->nfsd4_lease | |
6488 | }; | |
624322f1 OK |
6489 | struct nfs4_cpntf_state *cps; |
6490 | copy_stateid_t *cps_t; | |
6491 | int i; | |
1da177e4 | 6492 | |
03f318ca | 6493 | if (clients_still_reclaiming(nn)) { |
7f7e7a40 | 6494 | lt.new_timeo = 0; |
03f318ca BF |
6495 | goto out; |
6496 | } | |
12760c66 | 6497 | nfsd4_end_grace(nn); |
624322f1 OK |
6498 | |
6499 | spin_lock(&nn->s2s_cp_lock); | |
6500 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { | |
6501 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); | |
781fde1a | 6502 | if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID && |
7f7e7a40 | 6503 | state_expired(<, cps->cpntf_time)) |
624322f1 OK |
6504 | _free_cpntf_state_locked(nn, cps); |
6505 | } | |
6506 | spin_unlock(&nn->s2s_cp_lock); | |
66af2579 | 6507 | nfs4_get_client_reaplist(nn, &reaplist, <); |
7746b32f DN |
6508 | nfs4_process_client_reaplist(&reaplist); |
6509 | ||
d688d858 N |
6510 | nfs40_clean_admin_revoked(nn, <); |
6511 | ||
cdc97505 | 6512 | spin_lock(&state_lock); |
e8c69d17 | 6513 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 6514 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
7f7e7a40 | 6515 | if (!state_expired(<, dp->dl_time)) |
1da177e4 | 6516 | break; |
3f29cc82 | 6517 | unhash_delegation_locked(dp, SC_STATUS_REVOKED); |
42690676 | 6518 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 6519 | } |
cdc97505 | 6520 | spin_unlock(&state_lock); |
2d4a532d JL |
6521 | while (!list_empty(&reaplist)) { |
6522 | dp = list_first_entry(&reaplist, struct nfs4_delegation, | |
6523 | dl_recall_lru); | |
6524 | list_del_init(&dp->dl_recall_lru); | |
3bd64a5b | 6525 | revoke_delegation(dp); |
1da177e4 | 6526 | } |
217526e7 JL |
6527 | |
6528 | spin_lock(&nn->client_lock); | |
6529 | while (!list_empty(&nn->close_lru)) { | |
6530 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, | |
6531 | oo_close_lru); | |
7f7e7a40 | 6532 | if (!state_expired(<, oo->oo_time)) |
1da177e4 | 6533 | break; |
217526e7 JL |
6534 | list_del_init(&oo->oo_close_lru); |
6535 | stp = oo->oo_last_closed_stid; | |
6536 | oo->oo_last_closed_stid = NULL; | |
6537 | spin_unlock(&nn->client_lock); | |
6538 | nfs4_put_stid(&stp->st_stid); | |
6539 | spin_lock(&nn->client_lock); | |
1da177e4 | 6540 | } |
217526e7 JL |
6541 | spin_unlock(&nn->client_lock); |
6542 | ||
7919d0a2 JL |
6543 | /* |
6544 | * It's possible for a client to try and acquire an already held lock | |
6545 | * that is being held for a long time, and then lose interest in it. | |
6546 | * So, we clean out any un-revisited request after a lease period | |
6547 | * under the assumption that the client is no longer interested. | |
6548 | * | |
6549 | * RFC5661, sec. 9.6 states that the client must not rely on getting | |
6550 | * notifications and must continue to poll for locks, even when the | |
6551 | * server supports them. Thus this shouldn't lead to clients blocking | |
6552 | * indefinitely once the lock does become free. | |
6553 | */ | |
6554 | BUG_ON(!list_empty(&reaplist)); | |
0cc11a61 | 6555 | spin_lock(&nn->blocked_locks_lock); |
7919d0a2 JL |
6556 | while (!list_empty(&nn->blocked_locks_lru)) { |
6557 | nbl = list_first_entry(&nn->blocked_locks_lru, | |
6558 | struct nfsd4_blocked_lock, nbl_lru); | |
7f7e7a40 | 6559 | if (!state_expired(<, nbl->nbl_time)) |
7919d0a2 | 6560 | break; |
7919d0a2 JL |
6561 | list_move(&nbl->nbl_lru, &reaplist); |
6562 | list_del_init(&nbl->nbl_list); | |
6563 | } | |
0cc11a61 | 6564 | spin_unlock(&nn->blocked_locks_lock); |
7919d0a2 JL |
6565 | |
6566 | while (!list_empty(&reaplist)) { | |
64ebe124 | 6567 | nbl = list_first_entry(&reaplist, |
7919d0a2 JL |
6568 | struct nfsd4_blocked_lock, nbl_lru); |
6569 | list_del_init(&nbl->nbl_lru); | |
7919d0a2 JL |
6570 | free_blocked_lock(nbl); |
6571 | } | |
f4e44b39 DN |
6572 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
6573 | /* service the server-to-server copy delayed unmount list */ | |
6574 | nfsd4_ssc_expire_umount(nn); | |
6575 | #endif | |
bad4c585 DN |
6576 | if (atomic_long_read(&num_delegations) >= max_delegations) |
6577 | deleg_reaper(nn); | |
03f318ca | 6578 | out: |
7f7e7a40 | 6579 | return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
1da177e4 LT |
6580 | } |
6581 | ||
a254b246 | 6582 | static void laundromat_main(struct work_struct *); |
a254b246 HH |
6583 | |
6584 | static void | |
09121281 | 6585 | laundromat_main(struct work_struct *laundry) |
1da177e4 | 6586 | { |
20b7d86f | 6587 | time64_t t; |
2e55f3ab | 6588 | struct delayed_work *dwork = to_delayed_work(laundry); |
09121281 SK |
6589 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
6590 | laundromat_work); | |
1da177e4 | 6591 | |
09121281 | 6592 | t = nfs4_laundromat(nn); |
09121281 | 6593 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
1da177e4 LT |
6594 | } |
6595 | ||
7746b32f | 6596 | static void |
a1049eb4 | 6597 | courtesy_client_reaper(struct nfsd_net *nn) |
7746b32f DN |
6598 | { |
6599 | struct list_head reaplist; | |
7746b32f DN |
6600 | |
6601 | nfs4_get_courtesy_client_reaplist(nn, &reaplist); | |
6602 | nfs4_process_client_reaplist(&reaplist); | |
6603 | } | |
6604 | ||
44df6f43 DN |
6605 | static void |
6606 | deleg_reaper(struct nfsd_net *nn) | |
6607 | { | |
6608 | struct list_head *pos, *next; | |
6609 | struct nfs4_client *clp; | |
6610 | struct list_head cblist; | |
6611 | ||
6612 | INIT_LIST_HEAD(&cblist); | |
6613 | spin_lock(&nn->client_lock); | |
6614 | list_for_each_safe(pos, next, &nn->client_lru) { | |
6615 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
6616 | if (clp->cl_state != NFSD4_ACTIVE || | |
6617 | list_empty(&clp->cl_delegations) || | |
6618 | atomic_read(&clp->cl_delegs_in_recall) || | |
6619 | test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) || | |
6620 | (ktime_get_boottime_seconds() - | |
6621 | clp->cl_ra_time < 5)) { | |
6622 | continue; | |
6623 | } | |
6624 | list_add(&clp->cl_ra_cblist, &cblist); | |
6625 | ||
6626 | /* release in nfsd4_cb_recall_any_release */ | |
10396f4d | 6627 | kref_get(&clp->cl_nfsdfs.cl_ref); |
44df6f43 DN |
6628 | set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags); |
6629 | clp->cl_ra_time = ktime_get_boottime_seconds(); | |
6630 | } | |
6631 | spin_unlock(&nn->client_lock); | |
6632 | ||
6633 | while (!list_empty(&cblist)) { | |
6634 | clp = list_first_entry(&cblist, struct nfs4_client, | |
6635 | cl_ra_cblist); | |
6636 | list_del_init(&clp->cl_ra_cblist); | |
6637 | clp->cl_ra->ra_keep = 0; | |
6638 | clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG); | |
5826e09b DN |
6639 | clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) | |
6640 | BIT(RCA4_TYPE_MASK_WDATA_DLG); | |
638593be | 6641 | trace_nfsd_cb_recall_any(clp->cl_ra); |
44df6f43 DN |
6642 | nfsd4_run_cb(&clp->cl_ra->ra_cb); |
6643 | } | |
6644 | } | |
6645 | ||
a1049eb4 DN |
6646 | static void |
6647 | nfsd4_state_shrinker_worker(struct work_struct *work) | |
6648 | { | |
7c24fa22 | 6649 | struct nfsd_net *nn = container_of(work, struct nfsd_net, |
a1049eb4 DN |
6650 | nfsd_shrinker_work); |
6651 | ||
6652 | courtesy_client_reaper(nn); | |
44df6f43 | 6653 | deleg_reaper(nn); |
a1049eb4 DN |
6654 | } |
6655 | ||
8fcd461d | 6656 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
1da177e4 | 6657 | { |
8fcd461d | 6658 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
f7a4d872 BF |
6659 | return nfserr_bad_stateid; |
6660 | return nfs_ok; | |
1da177e4 LT |
6661 | } |
6662 | ||
1da177e4 | 6663 | static |
dcef0413 | 6664 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
1da177e4 | 6665 | { |
b37ad28b | 6666 | __be32 status = nfserr_openmode; |
1da177e4 | 6667 | |
02921914 BF |
6668 | /* For lock stateid's, we test the parent open, not the lock: */ |
6669 | if (stp->st_openstp) | |
6670 | stp = stp->st_openstp; | |
82c5ff1b | 6671 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
1da177e4 | 6672 | goto out; |
82c5ff1b | 6673 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
1da177e4 LT |
6674 | goto out; |
6675 | status = nfs_ok; | |
6676 | out: | |
6677 | return status; | |
6678 | } | |
6679 | ||
b37ad28b | 6680 | static inline __be32 |
5ccb0066 | 6681 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
1da177e4 | 6682 | { |
203a8c8e | 6683 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
1da177e4 | 6684 | return nfs_ok; |
c87fb4a3 | 6685 | else if (opens_in_grace(net)) { |
25985edc | 6686 | /* Answer in remaining cases depends on existence of |
1da177e4 LT |
6687 | * conflicting state; so we must wait out the grace period. */ |
6688 | return nfserr_grace; | |
6689 | } else if (flags & WR_STATE) | |
6690 | return nfs4_share_conflict(current_fh, | |
6691 | NFS4_SHARE_DENY_WRITE); | |
6692 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
6693 | return nfs4_share_conflict(current_fh, | |
6694 | NFS4_SHARE_DENY_READ); | |
6695 | } | |
6696 | ||
57b7b43b | 6697 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
0836f587 | 6698 | { |
6668958f AA |
6699 | /* |
6700 | * When sessions are used the stateid generation number is ignored | |
6701 | * when it is zero. | |
6702 | */ | |
28dde241 | 6703 | if (has_session && in->si_generation == 0) |
81b82965 BF |
6704 | return nfs_ok; |
6705 | ||
6706 | if (in->si_generation == ref->si_generation) | |
6707 | return nfs_ok; | |
6668958f | 6708 | |
0836f587 | 6709 | /* If the client sends us a stateid from the future, it's buggy: */ |
14b7f4a1 | 6710 | if (nfsd4_stateid_generation_after(in, ref)) |
0836f587 BF |
6711 | return nfserr_bad_stateid; |
6712 | /* | |
81b82965 BF |
6713 | * However, we could see a stateid from the past, even from a |
6714 | * non-buggy client. For example, if the client sends a lock | |
6715 | * while some IO is outstanding, the lock may bump si_generation | |
6716 | * while the IO is still in flight. The client could avoid that | |
6717 | * situation by waiting for responses on all the IO requests, | |
6718 | * but better performance may result in retrying IO that | |
6719 | * receives an old_stateid error if requests are rarely | |
6720 | * reordered in flight: | |
0836f587 | 6721 | */ |
81b82965 | 6722 | return nfserr_old_stateid; |
0836f587 BF |
6723 | } |
6724 | ||
03da3169 TM |
6725 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
6726 | { | |
6727 | __be32 ret; | |
6728 | ||
6729 | spin_lock(&s->sc_lock); | |
6730 | ret = nfsd4_verify_open_stid(s); | |
6731 | if (ret == nfs_ok) | |
6732 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); | |
6733 | spin_unlock(&s->sc_lock); | |
d688d858 N |
6734 | if (ret == nfserr_admin_revoked) |
6735 | nfsd40_drop_revoked_stid(s->sc_client, | |
6736 | &s->sc_stateid); | |
03da3169 TM |
6737 | return ret; |
6738 | } | |
6739 | ||
ebe9cb3b CH |
6740 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
6741 | { | |
6742 | if (ols->st_stateowner->so_is_open_owner && | |
6743 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) | |
6744 | return nfserr_bad_stateid; | |
6745 | return nfs_ok; | |
6746 | } | |
6747 | ||
7df302f7 | 6748 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
17456804 | 6749 | { |
97b7e3b6 | 6750 | struct nfs4_stid *s; |
1af71cc8 | 6751 | __be32 status = nfserr_bad_stateid; |
17456804 | 6752 | |
ae254dac AE |
6753 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
6754 | CLOSE_STATEID(stateid)) | |
1af71cc8 | 6755 | return status; |
1af71cc8 JL |
6756 | spin_lock(&cl->cl_lock); |
6757 | s = find_stateid_locked(cl, stateid); | |
97b7e3b6 | 6758 | if (!s) |
1af71cc8 | 6759 | goto out_unlock; |
03da3169 | 6760 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
17456804 | 6761 | if (status) |
1af71cc8 | 6762 | goto out_unlock; |
3f29cc82 N |
6763 | status = nfsd4_verify_open_stid(s); |
6764 | if (status) | |
6765 | goto out_unlock; | |
6766 | ||
23340032 | 6767 | switch (s->sc_type) { |
3f29cc82 | 6768 | case SC_TYPE_DELEG: |
1af71cc8 JL |
6769 | status = nfs_ok; |
6770 | break; | |
3f29cc82 N |
6771 | case SC_TYPE_OPEN: |
6772 | case SC_TYPE_LOCK: | |
ebe9cb3b | 6773 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
1af71cc8 | 6774 | break; |
23340032 BF |
6775 | default: |
6776 | printk("unknown stateid type %x\n", s->sc_type); | |
1af71cc8 | 6777 | status = nfserr_bad_stateid; |
23340032 | 6778 | } |
1af71cc8 JL |
6779 | out_unlock: |
6780 | spin_unlock(&cl->cl_lock); | |
d688d858 N |
6781 | if (status == nfserr_admin_revoked) |
6782 | nfsd40_drop_revoked_stid(cl, stateid); | |
1af71cc8 | 6783 | return status; |
17456804 BS |
6784 | } |
6785 | ||
cd61c522 | 6786 | __be32 |
2dd6e458 | 6787 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
3f29cc82 N |
6788 | stateid_t *stateid, |
6789 | unsigned short typemask, unsigned short statusmask, | |
2dd6e458 | 6790 | struct nfs4_stid **s, struct nfsd_net *nn) |
38c2f4b1 | 6791 | { |
0eb6f20a | 6792 | __be32 status; |
4d01416a | 6793 | struct nfs4_stid *stid; |
95da1b3a AE |
6794 | bool return_revoked = false; |
6795 | ||
6796 | /* | |
6797 | * only return revoked delegations if explicitly asked. | |
6798 | * otherwise we report revoked or bad_stateid status. | |
6799 | */ | |
3f29cc82 | 6800 | if (statusmask & SC_STATUS_REVOKED) |
95da1b3a | 6801 | return_revoked = true; |
3f29cc82 N |
6802 | if (typemask & SC_TYPE_DELEG) |
6803 | /* Always allow REVOKED for DELEG so we can | |
6804 | * retturn the appropriate error. | |
6805 | */ | |
6806 | statusmask |= SC_STATUS_REVOKED; | |
38c2f4b1 | 6807 | |
1ac3629b | 6808 | statusmask |= SC_STATUS_ADMIN_REVOKED; |
38c2f4b1 | 6809 | |
ae254dac AE |
6810 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
6811 | CLOSE_STATEID(stateid)) | |
38c2f4b1 | 6812 | return nfserr_bad_stateid; |
f71475ba | 6813 | status = set_client(&stateid->si_opaque.so_clid, cstate, nn); |
a8a7c677 | 6814 | if (status == nfserr_stale_clientid) { |
4b24ca7d | 6815 | if (cstate->session) |
a8a7c677 | 6816 | return nfserr_bad_stateid; |
38c2f4b1 | 6817 | return nfserr_stale_stateid; |
a8a7c677 | 6818 | } |
0eb6f20a BF |
6819 | if (status) |
6820 | return status; | |
3f29cc82 | 6821 | stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask); |
4d01416a | 6822 | if (!stid) |
38c2f4b1 | 6823 | return nfserr_bad_stateid; |
3f29cc82 | 6824 | if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) { |
4d01416a | 6825 | nfs4_put_stid(stid); |
3f29cc82 | 6826 | return nfserr_deleg_revoked; |
95da1b3a | 6827 | } |
1ac3629b | 6828 | if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { |
d688d858 | 6829 | nfsd40_drop_revoked_stid(cstate->clp, stateid); |
1ac3629b N |
6830 | nfs4_put_stid(stid); |
6831 | return nfserr_admin_revoked; | |
95da1b3a | 6832 | } |
4d01416a | 6833 | *s = stid; |
38c2f4b1 | 6834 | return nfs_ok; |
38c2f4b1 BF |
6835 | } |
6836 | ||
eb82dd39 | 6837 | static struct nfsd_file * |
a0649b2d CH |
6838 | nfs4_find_file(struct nfs4_stid *s, int flags) |
6839 | { | |
bd6aaf78 JL |
6840 | struct nfsd_file *ret = NULL; |
6841 | ||
3f29cc82 | 6842 | if (!s || s->sc_status) |
af90f707 CH |
6843 | return NULL; |
6844 | ||
a0649b2d | 6845 | switch (s->sc_type) { |
3f29cc82 | 6846 | case SC_TYPE_DELEG: |
bd6aaf78 JL |
6847 | spin_lock(&s->sc_file->fi_lock); |
6848 | ret = nfsd_file_get(s->sc_file->fi_deleg_file); | |
6849 | spin_unlock(&s->sc_file->fi_lock); | |
6850 | break; | |
3f29cc82 N |
6851 | case SC_TYPE_OPEN: |
6852 | case SC_TYPE_LOCK: | |
a0649b2d | 6853 | if (flags & RD_STATE) |
bd6aaf78 | 6854 | ret = find_readable_file(s->sc_file); |
a0649b2d | 6855 | else |
bd6aaf78 | 6856 | ret = find_writeable_file(s->sc_file); |
a0649b2d CH |
6857 | } |
6858 | ||
bd6aaf78 | 6859 | return ret; |
a0649b2d CH |
6860 | } |
6861 | ||
6862 | static __be32 | |
d8836f77 | 6863 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
a0649b2d CH |
6864 | { |
6865 | __be32 status; | |
6866 | ||
a0649b2d CH |
6867 | status = nfsd4_check_openowner_confirmed(ols); |
6868 | if (status) | |
6869 | return status; | |
6870 | return nfs4_check_openmode(ols, flags); | |
6871 | } | |
6872 | ||
af90f707 CH |
6873 | static __be32 |
6874 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, | |
5c4583b2 | 6875 | struct nfsd_file **nfp, int flags) |
af90f707 CH |
6876 | { |
6877 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; | |
eb82dd39 | 6878 | struct nfsd_file *nf; |
af90f707 CH |
6879 | __be32 status; |
6880 | ||
eb82dd39 JL |
6881 | nf = nfs4_find_file(s, flags); |
6882 | if (nf) { | |
af90f707 CH |
6883 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
6884 | acc | NFSD_MAY_OWNER_OVERRIDE); | |
5c4583b2 JL |
6885 | if (status) { |
6886 | nfsd_file_put(nf); | |
eb82dd39 | 6887 | goto out; |
5c4583b2 | 6888 | } |
af90f707 | 6889 | } else { |
eb82dd39 | 6890 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
af90f707 CH |
6891 | if (status) |
6892 | return status; | |
af90f707 | 6893 | } |
5c4583b2 | 6894 | *nfp = nf; |
eb82dd39 | 6895 | out: |
eb82dd39 | 6896 | return status; |
af90f707 | 6897 | } |
624322f1 OK |
6898 | static void |
6899 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) | |
6900 | { | |
781fde1a CL |
6901 | WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID); |
6902 | if (!refcount_dec_and_test(&cps->cp_stateid.cs_count)) | |
624322f1 OK |
6903 | return; |
6904 | list_del(&cps->cp_list); | |
6905 | idr_remove(&nn->s2s_cp_stateids, | |
781fde1a | 6906 | cps->cp_stateid.cs_stid.si_opaque.so_id); |
624322f1 OK |
6907 | kfree(cps); |
6908 | } | |
b7342204 OK |
6909 | /* |
6910 | * A READ from an inter server to server COPY will have a | |
6911 | * copy stateid. Look up the copy notify stateid from the | |
6912 | * idr structure and take a reference on it. | |
6913 | */ | |
ce0887ac OK |
6914 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
6915 | struct nfs4_client *clp, | |
6916 | struct nfs4_cpntf_state **cps) | |
b7342204 OK |
6917 | { |
6918 | copy_stateid_t *cps_t; | |
6919 | struct nfs4_cpntf_state *state = NULL; | |
6920 | ||
6921 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) | |
6922 | return nfserr_bad_stateid; | |
6923 | spin_lock(&nn->s2s_cp_lock); | |
6924 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); | |
6925 | if (cps_t) { | |
6926 | state = container_of(cps_t, struct nfs4_cpntf_state, | |
6927 | cp_stateid); | |
781fde1a | 6928 | if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) { |
5277a79e DC |
6929 | state = NULL; |
6930 | goto unlock; | |
6931 | } | |
ce0887ac | 6932 | if (!clp) |
781fde1a | 6933 | refcount_inc(&state->cp_stateid.cs_count); |
ce0887ac OK |
6934 | else |
6935 | _free_cpntf_state_locked(nn, state); | |
b7342204 | 6936 | } |
5277a79e | 6937 | unlock: |
b7342204 OK |
6938 | spin_unlock(&nn->s2s_cp_lock); |
6939 | if (!state) | |
6940 | return nfserr_bad_stateid; | |
3c86e615 | 6941 | if (!clp) |
ce0887ac | 6942 | *cps = state; |
b7342204 OK |
6943 | return 0; |
6944 | } | |
6945 | ||
6946 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, | |
6947 | struct nfs4_stid **stid) | |
6948 | { | |
6949 | __be32 status; | |
6950 | struct nfs4_cpntf_state *cps = NULL; | |
47fdb22d | 6951 | struct nfs4_client *found; |
b7342204 | 6952 | |
ce0887ac | 6953 | status = manage_cpntf_state(nn, st, NULL, &cps); |
b7342204 OK |
6954 | if (status) |
6955 | return status; | |
6956 | ||
20b7d86f | 6957 | cps->cpntf_time = ktime_get_boottime_seconds(); |
47fdb22d BF |
6958 | |
6959 | status = nfserr_expired; | |
6960 | found = lookup_clientid(&cps->cp_p_clid, true, nn); | |
6961 | if (!found) | |
b7342204 | 6962 | goto out; |
47fdb22d BF |
6963 | |
6964 | *stid = find_stateid_by_type(found, &cps->cp_p_stateid, | |
3f29cc82 N |
6965 | SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, |
6966 | 0); | |
47fdb22d BF |
6967 | if (*stid) |
6968 | status = nfs_ok; | |
6969 | else | |
6970 | status = nfserr_bad_stateid; | |
6971 | ||
6972 | put_client_renew(found); | |
b7342204 OK |
6973 | out: |
6974 | nfs4_put_cpntf_state(nn, cps); | |
6975 | return status; | |
6976 | } | |
624322f1 OK |
6977 | |
6978 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) | |
6979 | { | |
6980 | spin_lock(&nn->s2s_cp_lock); | |
6981 | _free_cpntf_state_locked(nn, cps); | |
6982 | spin_unlock(&nn->s2s_cp_lock); | |
6983 | } | |
af90f707 | 6984 | |
ee97e730 JL |
6985 | /** |
6986 | * nfs4_preprocess_stateid_op - find and prep stateid for an operation | |
6987 | * @rqstp: incoming request from client | |
6988 | * @cstate: current compound state | |
6989 | * @fhp: filehandle associated with requested stateid | |
6990 | * @stateid: stateid (provided by client) | |
6991 | * @flags: flags describing type of operation to be done | |
6992 | * @nfp: optional nfsd_file return pointer (may be NULL) | |
6993 | * @cstid: optional returned nfs4_stid pointer (may be NULL) | |
6994 | * | |
6995 | * Given info from the client, look up a nfs4_stid for the operation. On | |
6996 | * success, it returns a reference to the nfs4_stid and/or the nfsd_file | |
6997 | * associated with it. | |
a0649b2d | 6998 | */ |
b37ad28b | 6999 | __be32 |
af90f707 | 7000 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
aa0d6aed | 7001 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
624322f1 OK |
7002 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
7003 | struct nfs4_stid **cstid) | |
1da177e4 | 7004 | { |
af90f707 | 7005 | struct net *net = SVC_NET(rqstp); |
3320fef1 | 7006 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
af90f707 | 7007 | struct nfs4_stid *s = NULL; |
b37ad28b | 7008 | __be32 status; |
1da177e4 | 7009 | |
5c4583b2 JL |
7010 | if (nfp) |
7011 | *nfp = NULL; | |
1da177e4 | 7012 | |
af90f707 | 7013 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
074b07d9 BF |
7014 | if (cstid) |
7015 | status = nfserr_bad_stateid; | |
7016 | else | |
7017 | status = check_special_stateids(net, fhp, stateid, | |
7018 | flags); | |
af90f707 CH |
7019 | goto done; |
7020 | } | |
1da177e4 | 7021 | |
2dd6e458 | 7022 | status = nfsd4_lookup_stateid(cstate, stateid, |
3f29cc82 N |
7023 | SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, |
7024 | 0, &s, nn); | |
b7342204 OK |
7025 | if (status == nfserr_bad_stateid) |
7026 | status = find_cpntf_state(nn, stateid, &s); | |
38c2f4b1 | 7027 | if (status) |
c2d1d6a8 | 7028 | return status; |
03da3169 | 7029 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
a0649b2d | 7030 | nfsd4_has_session(cstate)); |
69064a27 BF |
7031 | if (status) |
7032 | goto out; | |
a0649b2d | 7033 | |
f7a4d872 | 7034 | switch (s->sc_type) { |
3f29cc82 | 7035 | case SC_TYPE_DELEG: |
a0649b2d | 7036 | status = nfs4_check_delegmode(delegstateid(s), flags); |
f7a4d872 | 7037 | break; |
3f29cc82 N |
7038 | case SC_TYPE_OPEN: |
7039 | case SC_TYPE_LOCK: | |
d8836f77 | 7040 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
f7a4d872 | 7041 | break; |
a0649b2d | 7042 | } |
8fcd461d JL |
7043 | if (status) |
7044 | goto out; | |
7045 | status = nfs4_check_fh(fhp, s); | |
a0649b2d | 7046 | |
af90f707 | 7047 | done: |
5c4583b2 JL |
7048 | if (status == nfs_ok && nfp) |
7049 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); | |
1da177e4 | 7050 | out: |
624322f1 OK |
7051 | if (s) { |
7052 | if (!status && cstid) | |
7053 | *cstid = s; | |
7054 | else | |
7055 | nfs4_put_stid(s); | |
7056 | } | |
1da177e4 LT |
7057 | return status; |
7058 | } | |
7059 | ||
17456804 BS |
7060 | /* |
7061 | * Test if the stateid is valid | |
7062 | */ | |
7063 | __be32 | |
7064 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
eb69853d | 7065 | union nfsd4_op_u *u) |
17456804 | 7066 | { |
eb69853d | 7067 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
03cfb420 | 7068 | struct nfsd4_test_stateid_id *stateid; |
ec59659b | 7069 | struct nfs4_client *cl = cstate->clp; |
03cfb420 | 7070 | |
03cfb420 | 7071 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) |
7df302f7 CL |
7072 | stateid->ts_id_status = |
7073 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); | |
03cfb420 | 7074 | |
17456804 BS |
7075 | return nfs_ok; |
7076 | } | |
7077 | ||
42691398 CL |
7078 | static __be32 |
7079 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) | |
7080 | { | |
7081 | struct nfs4_ol_stateid *stp = openlockstateid(s); | |
7082 | __be32 ret; | |
7083 | ||
659aefb6 TM |
7084 | ret = nfsd4_lock_ol_stateid(stp); |
7085 | if (ret) | |
7086 | goto out_put_stid; | |
42691398 CL |
7087 | |
7088 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); | |
7089 | if (ret) | |
7090 | goto out; | |
7091 | ||
7092 | ret = nfserr_locks_held; | |
7093 | if (check_for_locks(stp->st_stid.sc_file, | |
7094 | lockowner(stp->st_stateowner))) | |
7095 | goto out; | |
7096 | ||
7097 | release_lock_stateid(stp); | |
7098 | ret = nfs_ok; | |
7099 | ||
7100 | out: | |
7101 | mutex_unlock(&stp->st_mutex); | |
659aefb6 | 7102 | out_put_stid: |
42691398 CL |
7103 | nfs4_put_stid(s); |
7104 | return ret; | |
7105 | } | |
7106 | ||
e1ca12df BS |
7107 | __be32 |
7108 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
eb69853d | 7109 | union nfsd4_op_u *u) |
e1ca12df | 7110 | { |
eb69853d | 7111 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
e1ca12df | 7112 | stateid_t *stateid = &free_stateid->fr_stateid; |
2da1cec7 | 7113 | struct nfs4_stid *s; |
3bd64a5b | 7114 | struct nfs4_delegation *dp; |
ec59659b | 7115 | struct nfs4_client *cl = cstate->clp; |
2da1cec7 | 7116 | __be32 ret = nfserr_bad_stateid; |
e1ca12df | 7117 | |
1af71cc8 JL |
7118 | spin_lock(&cl->cl_lock); |
7119 | s = find_stateid_locked(cl, stateid); | |
3f29cc82 | 7120 | if (!s || s->sc_status & SC_STATUS_CLOSED) |
1af71cc8 | 7121 | goto out_unlock; |
d688d858 N |
7122 | if (s->sc_status & SC_STATUS_ADMIN_REVOKED) { |
7123 | nfsd4_drop_revoked_stid(s); | |
7124 | ret = nfs_ok; | |
7125 | goto out; | |
7126 | } | |
03da3169 | 7127 | spin_lock(&s->sc_lock); |
2da1cec7 | 7128 | switch (s->sc_type) { |
3f29cc82 N |
7129 | case SC_TYPE_DELEG: |
7130 | if (s->sc_status & SC_STATUS_REVOKED) { | |
7131 | spin_unlock(&s->sc_lock); | |
7132 | dp = delegstateid(s); | |
7133 | list_del_init(&dp->dl_recall_lru); | |
7134 | spin_unlock(&cl->cl_lock); | |
7135 | nfs4_put_stid(s); | |
7136 | ret = nfs_ok; | |
7137 | goto out; | |
7138 | } | |
e1ca12df | 7139 | ret = nfserr_locks_held; |
1af71cc8 | 7140 | break; |
3f29cc82 | 7141 | case SC_TYPE_OPEN: |
2da1cec7 BF |
7142 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
7143 | if (ret) | |
1af71cc8 JL |
7144 | break; |
7145 | ret = nfserr_locks_held; | |
f7a4d872 | 7146 | break; |
3f29cc82 | 7147 | case SC_TYPE_LOCK: |
03da3169 | 7148 | spin_unlock(&s->sc_lock); |
a15dfcd5 | 7149 | refcount_inc(&s->sc_count); |
1af71cc8 | 7150 | spin_unlock(&cl->cl_lock); |
42691398 | 7151 | ret = nfsd4_free_lock_stateid(stateid, s); |
1af71cc8 | 7152 | goto out; |
e1ca12df | 7153 | } |
03da3169 | 7154 | spin_unlock(&s->sc_lock); |
1af71cc8 JL |
7155 | out_unlock: |
7156 | spin_unlock(&cl->cl_lock); | |
e1ca12df | 7157 | out: |
e1ca12df BS |
7158 | return ret; |
7159 | } | |
7160 | ||
4c4cd222 N |
7161 | static inline int |
7162 | setlkflg (int type) | |
7163 | { | |
7164 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
7165 | RD_STATE : WR_STATE; | |
7166 | } | |
1da177e4 | 7167 | |
dcef0413 | 7168 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
c0a5d93e BF |
7169 | { |
7170 | struct svc_fh *current_fh = &cstate->current_fh; | |
7171 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
7172 | __be32 status; | |
7173 | ||
c0a5d93e BF |
7174 | status = nfsd4_check_seqid(cstate, sop, seqid); |
7175 | if (status) | |
7176 | return status; | |
9271d7e5 TM |
7177 | status = nfsd4_lock_ol_stateid(stp); |
7178 | if (status != nfs_ok) | |
7179 | return status; | |
f7a4d872 | 7180 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
35a92fe8 JL |
7181 | if (status == nfs_ok) |
7182 | status = nfs4_check_fh(current_fh, &stp->st_stid); | |
7183 | if (status != nfs_ok) | |
feb9dad5 | 7184 | mutex_unlock(&stp->st_mutex); |
35a92fe8 | 7185 | return status; |
c0a5d93e BF |
7186 | } |
7187 | ||
ee97e730 JL |
7188 | /** |
7189 | * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op | |
7190 | * @cstate: compund state | |
7191 | * @seqid: seqid (provided by client) | |
7192 | * @stateid: stateid (provided by client) | |
7193 | * @typemask: mask of allowable types for this operation | |
3f29cc82 | 7194 | * @statusmask: mask of allowed states: 0 or STID_CLOSED |
ee97e730 JL |
7195 | * @stpp: return pointer for the stateid found |
7196 | * @nn: net namespace for request | |
7197 | * | |
7198 | * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and | |
7199 | * return it in @stpp. On a nfs_ok return, the returned stateid will | |
7200 | * have its st_mutex locked. | |
1da177e4 | 7201 | */ |
b37ad28b | 7202 | static __be32 |
dd453dfd | 7203 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
3f29cc82 N |
7204 | stateid_t *stateid, |
7205 | unsigned short typemask, unsigned short statusmask, | |
3320fef1 SK |
7206 | struct nfs4_ol_stateid **stpp, |
7207 | struct nfsd_net *nn) | |
1da177e4 | 7208 | { |
0836f587 | 7209 | __be32 status; |
38c2f4b1 | 7210 | struct nfs4_stid *s; |
e17f99b7 | 7211 | struct nfs4_ol_stateid *stp = NULL; |
1da177e4 | 7212 | |
dd5e3fbc | 7213 | trace_nfsd_preprocess(seqid, stateid); |
3a4f98bb | 7214 | |
1da177e4 | 7215 | *stpp = NULL; |
eec76208 | 7216 | retry: |
3f29cc82 N |
7217 | status = nfsd4_lookup_stateid(cstate, stateid, |
7218 | typemask, statusmask, &s, nn); | |
c0a5d93e BF |
7219 | if (status) |
7220 | return status; | |
e17f99b7 | 7221 | stp = openlockstateid(s); |
eec76208 N |
7222 | if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) { |
7223 | nfs4_put_stateowner(stp->st_stateowner); | |
7224 | goto retry; | |
7225 | } | |
1da177e4 | 7226 | |
e17f99b7 | 7227 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
fd911011 | 7228 | if (!status) |
e17f99b7 | 7229 | *stpp = stp; |
fd911011 TM |
7230 | else |
7231 | nfs4_put_stid(&stp->st_stid); | |
e17f99b7 | 7232 | return status; |
c0a5d93e | 7233 | } |
39325bd0 | 7234 | |
3320fef1 SK |
7235 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
7236 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) | |
c0a5d93e BF |
7237 | { |
7238 | __be32 status; | |
7239 | struct nfs4_openowner *oo; | |
4cbfc9f7 | 7240 | struct nfs4_ol_stateid *stp; |
1da177e4 | 7241 | |
c0a5d93e | 7242 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
3f29cc82 | 7243 | SC_TYPE_OPEN, 0, &stp, nn); |
7a8711c9 BF |
7244 | if (status) |
7245 | return status; | |
4cbfc9f7 TM |
7246 | oo = openowner(stp->st_stateowner); |
7247 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { | |
feb9dad5 | 7248 | mutex_unlock(&stp->st_mutex); |
4cbfc9f7 | 7249 | nfs4_put_stid(&stp->st_stid); |
3a4f98bb | 7250 | return nfserr_bad_stateid; |
4cbfc9f7 TM |
7251 | } |
7252 | *stpp = stp; | |
3a4f98bb | 7253 | return nfs_ok; |
1da177e4 LT |
7254 | } |
7255 | ||
b37ad28b | 7256 | __be32 |
ca364317 | 7257 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 7258 | union nfsd4_op_u *u) |
1da177e4 | 7259 | { |
eb69853d | 7260 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
b37ad28b | 7261 | __be32 status; |
fe0750e5 | 7262 | struct nfs4_openowner *oo; |
dcef0413 | 7263 | struct nfs4_ol_stateid *stp; |
3320fef1 | 7264 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 7265 | |
a6a9f18f AV |
7266 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
7267 | cstate->current_fh.fh_dentry); | |
1da177e4 | 7268 | |
ca364317 | 7269 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
a8cddc5d BF |
7270 | if (status) |
7271 | return status; | |
1da177e4 | 7272 | |
9072d5c6 | 7273 | status = nfs4_preprocess_seqid_op(cstate, |
3f29cc82 N |
7274 | oc->oc_seqid, &oc->oc_req_stateid, |
7275 | SC_TYPE_OPEN, 0, &stp, nn); | |
9072d5c6 | 7276 | if (status) |
68b66e82 | 7277 | goto out; |
fe0750e5 | 7278 | oo = openowner(stp->st_stateowner); |
68b66e82 | 7279 | status = nfserr_bad_stateid; |
35a92fe8 | 7280 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
feb9dad5 | 7281 | mutex_unlock(&stp->st_mutex); |
2585fc79 | 7282 | goto put_stateid; |
35a92fe8 | 7283 | } |
dad1c067 | 7284 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
9767feb2 | 7285 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
feb9dad5 | 7286 | mutex_unlock(&stp->st_mutex); |
dd5e3fbc | 7287 | trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); |
2a4317c5 | 7288 | nfsd4_client_record_create(oo->oo_owner.so_client); |
68b66e82 | 7289 | status = nfs_ok; |
2585fc79 TM |
7290 | put_stateid: |
7291 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 7292 | out: |
9411b1d4 | 7293 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
7294 | return status; |
7295 | } | |
7296 | ||
6409a5a6 | 7297 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
1da177e4 | 7298 | { |
82c5ff1b | 7299 | if (!test_access(access, stp)) |
6409a5a6 | 7300 | return; |
11b9164a | 7301 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
82c5ff1b | 7302 | clear_access(access, stp); |
6409a5a6 | 7303 | } |
f197c271 | 7304 | |
6409a5a6 BF |
7305 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
7306 | { | |
7307 | switch (to_access) { | |
7308 | case NFS4_SHARE_ACCESS_READ: | |
7309 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); | |
7310 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
7311 | break; | |
7312 | case NFS4_SHARE_ACCESS_WRITE: | |
7313 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); | |
7314 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
7315 | break; | |
7316 | case NFS4_SHARE_ACCESS_BOTH: | |
7317 | break; | |
7318 | default: | |
063b0fb9 | 7319 | WARN_ON_ONCE(1); |
1da177e4 LT |
7320 | } |
7321 | } | |
7322 | ||
b37ad28b | 7323 | __be32 |
ca364317 | 7324 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
eb69853d | 7325 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
1da177e4 | 7326 | { |
eb69853d | 7327 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
b37ad28b | 7328 | __be32 status; |
dcef0413 | 7329 | struct nfs4_ol_stateid *stp; |
3320fef1 | 7330 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 7331 | |
a6a9f18f AV |
7332 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
7333 | cstate->current_fh.fh_dentry); | |
1da177e4 | 7334 | |
c30e92df | 7335 | /* We don't yet support WANT bits: */ |
2c8bd7e0 BH |
7336 | if (od->od_deleg_want) |
7337 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, | |
7338 | od->od_deleg_want); | |
1da177e4 | 7339 | |
c0a5d93e | 7340 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
3320fef1 | 7341 | &od->od_stateid, &stp, nn); |
9072d5c6 | 7342 | if (status) |
1da177e4 | 7343 | goto out; |
1da177e4 | 7344 | status = nfserr_inval; |
82c5ff1b | 7345 | if (!test_access(od->od_share_access, stp)) { |
c11c591f | 7346 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
1da177e4 | 7347 | stp->st_access_bmap, od->od_share_access); |
0667b1e9 | 7348 | goto put_stateid; |
1da177e4 | 7349 | } |
ce0fc43c | 7350 | if (!test_deny(od->od_share_deny, stp)) { |
c11c591f | 7351 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
1da177e4 | 7352 | stp->st_deny_bmap, od->od_share_deny); |
0667b1e9 | 7353 | goto put_stateid; |
1da177e4 | 7354 | } |
6409a5a6 | 7355 | nfs4_stateid_downgrade(stp, od->od_share_access); |
ce0fc43c | 7356 | reset_union_bmap_deny(od->od_share_deny, stp); |
9767feb2 | 7357 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
1da177e4 | 7358 | status = nfs_ok; |
0667b1e9 | 7359 | put_stateid: |
feb9dad5 | 7360 | mutex_unlock(&stp->st_mutex); |
0667b1e9 | 7361 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 7362 | out: |
9411b1d4 | 7363 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
7364 | return status; |
7365 | } | |
7366 | ||
56c35f43 | 7367 | static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
f7a4d872 | 7368 | { |
acf9295b | 7369 | struct nfs4_client *clp = s->st_stid.sc_client; |
e8568739 | 7370 | bool unhashed; |
d83017f9 | 7371 | LIST_HEAD(reaplist); |
019805fe | 7372 | struct nfs4_ol_stateid *stp; |
acf9295b | 7373 | |
2c41beb0 | 7374 | spin_lock(&clp->cl_lock); |
e8568739 | 7375 | unhashed = unhash_open_stateid(s, &reaplist); |
acf9295b | 7376 | |
d83017f9 | 7377 | if (clp->cl_minorversion) { |
e8568739 JL |
7378 | if (unhashed) |
7379 | put_ol_stateid_locked(s, &reaplist); | |
d83017f9 | 7380 | spin_unlock(&clp->cl_lock); |
019805fe DN |
7381 | list_for_each_entry(stp, &reaplist, st_locks) |
7382 | nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); | |
d83017f9 | 7383 | free_ol_stateid_reaplist(&reaplist); |
56c35f43 | 7384 | return false; |
d83017f9 JL |
7385 | } else { |
7386 | spin_unlock(&clp->cl_lock); | |
7387 | free_ol_stateid_reaplist(&reaplist); | |
56c35f43 | 7388 | return unhashed; |
d83017f9 | 7389 | } |
38c387b5 BF |
7390 | } |
7391 | ||
1da177e4 LT |
7392 | /* |
7393 | * nfs4_unlock_state() called after encode | |
7394 | */ | |
b37ad28b | 7395 | __be32 |
ca364317 | 7396 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 7397 | union nfsd4_op_u *u) |
1da177e4 | 7398 | { |
eb69853d | 7399 | struct nfsd4_close *close = &u->close; |
b37ad28b | 7400 | __be32 status; |
dcef0413 | 7401 | struct nfs4_ol_stateid *stp; |
3320fef1 SK |
7402 | struct net *net = SVC_NET(rqstp); |
7403 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
56c35f43 | 7404 | bool need_move_to_close_list; |
1da177e4 | 7405 | |
3f29cc82 | 7406 | dprintk("NFSD: nfsd4_close on file %pd\n", |
a6a9f18f | 7407 | cstate->current_fh.fh_dentry); |
1da177e4 | 7408 | |
f7a4d872 | 7409 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
3f29cc82 N |
7410 | &close->cl_stateid, |
7411 | SC_TYPE_OPEN, SC_STATUS_CLOSED, | |
7412 | &stp, nn); | |
9411b1d4 | 7413 | nfsd4_bump_seqid(cstate, status); |
9072d5c6 | 7414 | if (status) |
3f29cc82 | 7415 | goto out; |
15ca08d3 | 7416 | |
3f29cc82 N |
7417 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
7418 | stp->st_stid.sc_status |= SC_STATUS_CLOSED; | |
7419 | spin_unlock(&stp->st_stid.sc_client->cl_lock); | |
bd2decac JL |
7420 | |
7421 | /* | |
7422 | * Technically we don't _really_ have to increment or copy it, since | |
7423 | * it should just be gone after this operation and we clobber the | |
7424 | * copied value below, but we continue to do so here just to ensure | |
7425 | * that racing ops see that there was a state change. | |
7426 | */ | |
9767feb2 | 7427 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
1da177e4 | 7428 | |
56c35f43 | 7429 | need_move_to_close_list = nfsd4_close_open_stateid(stp); |
15ca08d3 | 7430 | mutex_unlock(&stp->st_mutex); |
56c35f43 N |
7431 | if (need_move_to_close_list) |
7432 | move_to_close_lru(stp, net); | |
8a0b589d | 7433 | |
bd2decac JL |
7434 | /* v4.1+ suggests that we send a special stateid in here, since the |
7435 | * clients should just ignore this anyway. Since this is not useful | |
7436 | * for v4.0 clients either, we set it to the special close_stateid | |
7437 | * universally. | |
7438 | * | |
7439 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 | |
7440 | */ | |
7441 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); | |
fb500a7c | 7442 | |
8a0b589d TM |
7443 | /* put reference from nfs4_preprocess_seqid_op */ |
7444 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 7445 | out: |
1da177e4 LT |
7446 | return status; |
7447 | } | |
7448 | ||
b37ad28b | 7449 | __be32 |
ca364317 | 7450 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 7451 | union nfsd4_op_u *u) |
1da177e4 | 7452 | { |
eb69853d | 7453 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
203a8c8e BF |
7454 | struct nfs4_delegation *dp; |
7455 | stateid_t *stateid = &dr->dr_stateid; | |
38c2f4b1 | 7456 | struct nfs4_stid *s; |
b37ad28b | 7457 | __be32 status; |
3320fef1 | 7458 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 7459 | |
ca364317 | 7460 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
203a8c8e | 7461 | return status; |
1da177e4 | 7462 | |
3f29cc82 | 7463 | status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, 0, &s, nn); |
38c2f4b1 | 7464 | if (status) |
203a8c8e | 7465 | goto out; |
38c2f4b1 | 7466 | dp = delegstateid(s); |
03da3169 | 7467 | status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); |
203a8c8e | 7468 | if (status) |
fd911011 | 7469 | goto put_stateid; |
203a8c8e | 7470 | |
20eee313 | 7471 | trace_nfsd_deleg_return(stateid); |
c035362e | 7472 | wake_up_var(d_inode(cstate->current_fh.fh_dentry)); |
3bd64a5b | 7473 | destroy_delegation(dp); |
fd911011 TM |
7474 | put_stateid: |
7475 | nfs4_put_stid(&dp->dl_stid); | |
1da177e4 LT |
7476 | out: |
7477 | return status; | |
7478 | } | |
7479 | ||
87df4de8 BH |
7480 | /* last octet in a range */ |
7481 | static inline u64 | |
7482 | last_byte_offset(u64 start, u64 len) | |
7483 | { | |
7484 | u64 end; | |
7485 | ||
063b0fb9 | 7486 | WARN_ON_ONCE(!len); |
87df4de8 BH |
7487 | end = start + len; |
7488 | return end > start ? end - 1: NFS4_MAX_UINT64; | |
7489 | } | |
7490 | ||
1da177e4 LT |
7491 | /* |
7492 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
7493 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
7494 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
7495 | * locking, this prevents us from being completely protocol-compliant. The | |
7496 | * real solution to this problem is to start using unsigned file offsets in | |
7497 | * the VFS, but this is a very deep change! | |
7498 | */ | |
7499 | static inline void | |
7500 | nfs4_transform_lock_offset(struct file_lock *lock) | |
7501 | { | |
7502 | if (lock->fl_start < 0) | |
7503 | lock->fl_start = OFFSET_MAX; | |
7504 | if (lock->fl_end < 0) | |
7505 | lock->fl_end = OFFSET_MAX; | |
7506 | } | |
7507 | ||
cae80b30 | 7508 | static fl_owner_t |
35aff067 | 7509 | nfsd4_lm_get_owner(fl_owner_t owner) |
aef9583b | 7510 | { |
cae80b30 JL |
7511 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
7512 | ||
7513 | nfs4_get_stateowner(&lo->lo_owner); | |
7514 | return owner; | |
aef9583b KM |
7515 | } |
7516 | ||
cae80b30 | 7517 | static void |
35aff067 | 7518 | nfsd4_lm_put_owner(fl_owner_t owner) |
aef9583b | 7519 | { |
cae80b30 | 7520 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
aef9583b | 7521 | |
cae80b30 | 7522 | if (lo) |
aef9583b | 7523 | nfs4_put_stateowner(&lo->lo_owner); |
aef9583b KM |
7524 | } |
7525 | ||
27431aff DN |
7526 | /* return pointer to struct nfs4_client if client is expirable */ |
7527 | static bool | |
7528 | nfsd4_lm_lock_expirable(struct file_lock *cfl) | |
7529 | { | |
05580bbf | 7530 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner; |
27431aff DN |
7531 | struct nfs4_client *clp = lo->lo_owner.so_client; |
7532 | struct nfsd_net *nn; | |
7533 | ||
7534 | if (try_to_expire_client(clp)) { | |
7535 | nn = net_generic(clp->net, nfsd_net_id); | |
7536 | mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); | |
7537 | return true; | |
7538 | } | |
7539 | return false; | |
7540 | } | |
7541 | ||
7542 | /* schedule laundromat to run immediately and wait for it to complete */ | |
7543 | static void | |
7544 | nfsd4_lm_expire_lock(void) | |
7545 | { | |
7546 | flush_workqueue(laundry_wq); | |
7547 | } | |
7548 | ||
76d348fa JL |
7549 | static void |
7550 | nfsd4_lm_notify(struct file_lock *fl) | |
7551 | { | |
05580bbf | 7552 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner; |
76d348fa JL |
7553 | struct net *net = lo->lo_owner.so_client->net; |
7554 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
7555 | struct nfsd4_blocked_lock *nbl = container_of(fl, | |
7556 | struct nfsd4_blocked_lock, nbl_lock); | |
7557 | bool queue = false; | |
7558 | ||
7919d0a2 | 7559 | /* An empty list means that something else is going to be using it */ |
0cc11a61 | 7560 | spin_lock(&nn->blocked_locks_lock); |
76d348fa JL |
7561 | if (!list_empty(&nbl->nbl_list)) { |
7562 | list_del_init(&nbl->nbl_list); | |
7919d0a2 | 7563 | list_del_init(&nbl->nbl_lru); |
76d348fa JL |
7564 | queue = true; |
7565 | } | |
0cc11a61 | 7566 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa | 7567 | |
2cde7f81 CL |
7568 | if (queue) { |
7569 | trace_nfsd_cb_notify_lock(lo, nbl); | |
76d348fa | 7570 | nfsd4_run_cb(&nbl->nbl_cb); |
2cde7f81 | 7571 | } |
76d348fa JL |
7572 | } |
7573 | ||
7b021967 | 7574 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
27431aff | 7575 | .lm_mod_owner = THIS_MODULE, |
76d348fa | 7576 | .lm_notify = nfsd4_lm_notify, |
35aff067 CL |
7577 | .lm_get_owner = nfsd4_lm_get_owner, |
7578 | .lm_put_owner = nfsd4_lm_put_owner, | |
27431aff DN |
7579 | .lm_lock_expirable = nfsd4_lm_lock_expirable, |
7580 | .lm_expire_lock = nfsd4_lm_expire_lock, | |
d5b9026a | 7581 | }; |
1da177e4 LT |
7582 | |
7583 | static inline void | |
7584 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
7585 | { | |
fe0750e5 | 7586 | struct nfs4_lockowner *lo; |
1da177e4 | 7587 | |
d5b9026a | 7588 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
05580bbf | 7589 | lo = (struct nfs4_lockowner *) fl->c.flc_owner; |
6f4859b8 BF |
7590 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
7591 | GFP_KERNEL); | |
7c13f344 BF |
7592 | if (!deny->ld_owner.data) |
7593 | /* We just don't care that much */ | |
7594 | goto nevermind; | |
fe0750e5 | 7595 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
d5b9026a | 7596 | } else { |
7c13f344 BF |
7597 | nevermind: |
7598 | deny->ld_owner.len = 0; | |
7599 | deny->ld_owner.data = NULL; | |
d5b9026a N |
7600 | deny->ld_clientid.cl_boot = 0; |
7601 | deny->ld_clientid.cl_id = 0; | |
1da177e4 LT |
7602 | } |
7603 | deny->ld_start = fl->fl_start; | |
87df4de8 BH |
7604 | deny->ld_length = NFS4_MAX_UINT64; |
7605 | if (fl->fl_end != NFS4_MAX_UINT64) | |
1da177e4 LT |
7606 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
7607 | deny->ld_type = NFS4_READ_LT; | |
05580bbf | 7608 | if (fl->c.flc_type != F_RDLCK) |
1da177e4 LT |
7609 | deny->ld_type = NFS4_WRITE_LT; |
7610 | } | |
7611 | ||
fe0750e5 | 7612 | static struct nfs4_lockowner * |
c8623999 | 7613 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
1da177e4 | 7614 | { |
d4f0489f | 7615 | unsigned int strhashval = ownerstr_hashval(owner); |
b3c32bcd | 7616 | struct nfs4_stateowner *so; |
1da177e4 | 7617 | |
0a880a28 TM |
7618 | lockdep_assert_held(&clp->cl_lock); |
7619 | ||
d4f0489f TM |
7620 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
7621 | so_strhash) { | |
b3c32bcd TM |
7622 | if (so->so_is_open_owner) |
7623 | continue; | |
b5971afa KM |
7624 | if (same_owner_str(so, owner)) |
7625 | return lockowner(nfs4_get_stateowner(so)); | |
1da177e4 LT |
7626 | } |
7627 | return NULL; | |
7628 | } | |
7629 | ||
c58c6610 | 7630 | static struct nfs4_lockowner * |
c8623999 | 7631 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
c58c6610 TM |
7632 | { |
7633 | struct nfs4_lockowner *lo; | |
7634 | ||
d4f0489f | 7635 | spin_lock(&clp->cl_lock); |
c8623999 | 7636 | lo = find_lockowner_str_locked(clp, owner); |
d4f0489f | 7637 | spin_unlock(&clp->cl_lock); |
c58c6610 TM |
7638 | return lo; |
7639 | } | |
7640 | ||
8f4b54c5 JL |
7641 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
7642 | { | |
c58c6610 | 7643 | unhash_lockowner_locked(lockowner(sop)); |
8f4b54c5 JL |
7644 | } |
7645 | ||
6b180f0b JL |
7646 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
7647 | { | |
7648 | struct nfs4_lockowner *lo = lockowner(sop); | |
7649 | ||
7650 | kmem_cache_free(lockowner_slab, lo); | |
7651 | } | |
7652 | ||
7653 | static const struct nfs4_stateowner_operations lockowner_ops = { | |
8f4b54c5 JL |
7654 | .so_unhash = nfs4_unhash_lockowner, |
7655 | .so_free = nfs4_free_lockowner, | |
6b180f0b JL |
7656 | }; |
7657 | ||
1da177e4 LT |
7658 | /* |
7659 | * Alloc a lock owner structure. | |
7660 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
25985edc | 7661 | * occurred. |
1da177e4 | 7662 | * |
16bfdaaf | 7663 | * strhashval = ownerstr_hashval |
1da177e4 | 7664 | */ |
fe0750e5 | 7665 | static struct nfs4_lockowner * |
c58c6610 TM |
7666 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
7667 | struct nfs4_ol_stateid *open_stp, | |
7668 | struct nfsd4_lock *lock) | |
7669 | { | |
c58c6610 | 7670 | struct nfs4_lockowner *lo, *ret; |
1da177e4 | 7671 | |
fe0750e5 BF |
7672 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
7673 | if (!lo) | |
1da177e4 | 7674 | return NULL; |
76d348fa | 7675 | INIT_LIST_HEAD(&lo->lo_blocked); |
fe0750e5 BF |
7676 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
7677 | lo->lo_owner.so_is_open_owner = 0; | |
5db1c03f | 7678 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
6b180f0b | 7679 | lo->lo_owner.so_ops = &lockowner_ops; |
d4f0489f | 7680 | spin_lock(&clp->cl_lock); |
c8623999 | 7681 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
c58c6610 TM |
7682 | if (ret == NULL) { |
7683 | list_add(&lo->lo_owner.so_strhash, | |
d4f0489f | 7684 | &clp->cl_ownerstr_hashtbl[strhashval]); |
c58c6610 TM |
7685 | ret = lo; |
7686 | } else | |
d50ffded KM |
7687 | nfs4_free_stateowner(&lo->lo_owner); |
7688 | ||
d4f0489f | 7689 | spin_unlock(&clp->cl_lock); |
340f0ba1 | 7690 | return ret; |
1da177e4 LT |
7691 | } |
7692 | ||
fd1fd685 | 7693 | static struct nfs4_ol_stateid * |
a451b123 TM |
7694 | find_lock_stateid(const struct nfs4_lockowner *lo, |
7695 | const struct nfs4_ol_stateid *ost) | |
fd1fd685 TM |
7696 | { |
7697 | struct nfs4_ol_stateid *lst; | |
fd1fd685 | 7698 | |
a451b123 | 7699 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
fd1fd685 | 7700 | |
a451b123 TM |
7701 | /* If ost is not hashed, ost->st_locks will not be valid */ |
7702 | if (!nfs4_ol_stateid_unhashed(ost)) | |
7703 | list_for_each_entry(lst, &ost->st_locks, st_locks) { | |
7704 | if (lst->st_stateowner == &lo->lo_owner) { | |
7705 | refcount_inc(&lst->st_stid.sc_count); | |
7706 | return lst; | |
7707 | } | |
fd1fd685 | 7708 | } |
fd1fd685 TM |
7709 | return NULL; |
7710 | } | |
7711 | ||
beeca19c | 7712 | static struct nfs4_ol_stateid * |
356a95ec JL |
7713 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
7714 | struct nfs4_file *fp, struct inode *inode, | |
7715 | struct nfs4_ol_stateid *open_stp) | |
1da177e4 | 7716 | { |
d3b313a4 | 7717 | struct nfs4_client *clp = lo->lo_owner.so_client; |
beeca19c | 7718 | struct nfs4_ol_stateid *retstp; |
1da177e4 | 7719 | |
beeca19c | 7720 | mutex_init(&stp->st_mutex); |
4f34bd05 | 7721 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
beeca19c TM |
7722 | retry: |
7723 | spin_lock(&clp->cl_lock); | |
a451b123 TM |
7724 | if (nfs4_ol_stateid_unhashed(open_stp)) |
7725 | goto out_close; | |
7726 | retstp = find_lock_stateid(lo, open_stp); | |
beeca19c | 7727 | if (retstp) |
a451b123 | 7728 | goto out_found; |
a15dfcd5 | 7729 | refcount_inc(&stp->st_stid.sc_count); |
3f29cc82 | 7730 | stp->st_stid.sc_type = SC_TYPE_LOCK; |
b5971afa | 7731 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
13cd2184 | 7732 | get_nfs4_file(fp); |
11b9164a | 7733 | stp->st_stid.sc_file = fp; |
0997b173 | 7734 | stp->st_access_bmap = 0; |
1da177e4 | 7735 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
4c4cd222 | 7736 | stp->st_openstp = open_stp; |
a451b123 | 7737 | spin_lock(&fp->fi_lock); |
3c87b9b7 | 7738 | list_add(&stp->st_locks, &open_stp->st_locks); |
1c755dc1 | 7739 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
1d31a253 TM |
7740 | list_add(&stp->st_perfile, &fp->fi_stateids); |
7741 | spin_unlock(&fp->fi_lock); | |
beeca19c | 7742 | spin_unlock(&clp->cl_lock); |
beeca19c | 7743 | return stp; |
a451b123 TM |
7744 | out_found: |
7745 | spin_unlock(&clp->cl_lock); | |
7746 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { | |
7747 | nfs4_put_stid(&retstp->st_stid); | |
7748 | goto retry; | |
7749 | } | |
7750 | /* To keep mutex tracking happy */ | |
7751 | mutex_unlock(&stp->st_mutex); | |
7752 | return retstp; | |
7753 | out_close: | |
7754 | spin_unlock(&clp->cl_lock); | |
7755 | mutex_unlock(&stp->st_mutex); | |
7756 | return NULL; | |
1da177e4 LT |
7757 | } |
7758 | ||
356a95ec JL |
7759 | static struct nfs4_ol_stateid * |
7760 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, | |
7761 | struct inode *inode, struct nfs4_ol_stateid *ost, | |
7762 | bool *new) | |
7763 | { | |
7764 | struct nfs4_stid *ns = NULL; | |
7765 | struct nfs4_ol_stateid *lst; | |
7766 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); | |
7767 | struct nfs4_client *clp = oo->oo_owner.so_client; | |
7768 | ||
beeca19c | 7769 | *new = false; |
356a95ec | 7770 | spin_lock(&clp->cl_lock); |
a451b123 | 7771 | lst = find_lock_stateid(lo, ost); |
356a95ec | 7772 | spin_unlock(&clp->cl_lock); |
beeca19c TM |
7773 | if (lst != NULL) { |
7774 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) | |
7775 | goto out; | |
7776 | nfs4_put_stid(&lst->st_stid); | |
7777 | } | |
7778 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); | |
7779 | if (ns == NULL) | |
7780 | return NULL; | |
7781 | ||
7782 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); | |
7783 | if (lst == openlockstateid(ns)) | |
7784 | *new = true; | |
7785 | else | |
356a95ec | 7786 | nfs4_put_stid(ns); |
beeca19c | 7787 | out: |
356a95ec JL |
7788 | return lst; |
7789 | } | |
c53530da | 7790 | |
fd39ca9a | 7791 | static int |
1da177e4 LT |
7792 | check_lock_length(u64 offset, u64 length) |
7793 | { | |
e7969315 KM |
7794 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
7795 | (length > ~offset))); | |
1da177e4 LT |
7796 | } |
7797 | ||
dcef0413 | 7798 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
0997b173 | 7799 | { |
11b9164a | 7800 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
0997b173 | 7801 | |
7214e860 JL |
7802 | lockdep_assert_held(&fp->fi_lock); |
7803 | ||
82c5ff1b | 7804 | if (test_access(access, lock_stp)) |
0997b173 | 7805 | return; |
12659651 | 7806 | __nfs4_file_get_access(fp, access); |
82c5ff1b | 7807 | set_access(access, lock_stp); |
0997b173 BF |
7808 | } |
7809 | ||
356a95ec JL |
7810 | static __be32 |
7811 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, | |
7812 | struct nfs4_ol_stateid *ost, | |
7813 | struct nfsd4_lock *lock, | |
dd257933 | 7814 | struct nfs4_ol_stateid **plst, bool *new) |
64a284d0 | 7815 | { |
5db1c03f | 7816 | __be32 status; |
11b9164a | 7817 | struct nfs4_file *fi = ost->st_stid.sc_file; |
64a284d0 BF |
7818 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
7819 | struct nfs4_client *cl = oo->oo_owner.so_client; | |
2b0143b5 | 7820 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
64a284d0 | 7821 | struct nfs4_lockowner *lo; |
dd257933 | 7822 | struct nfs4_ol_stateid *lst; |
64a284d0 BF |
7823 | unsigned int strhashval; |
7824 | ||
c8623999 | 7825 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
c53530da | 7826 | if (!lo) { |
76f6c9e1 | 7827 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
c53530da JL |
7828 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
7829 | if (lo == NULL) | |
7830 | return nfserr_jukebox; | |
7831 | } else { | |
7832 | /* with an existing lockowner, seqids must be the same */ | |
5db1c03f | 7833 | status = nfserr_bad_seqid; |
c53530da JL |
7834 | if (!cstate->minorversion && |
7835 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) | |
5db1c03f | 7836 | goto out; |
64a284d0 | 7837 | } |
c53530da | 7838 | |
dd257933 JL |
7839 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
7840 | if (lst == NULL) { | |
5db1c03f JL |
7841 | status = nfserr_jukebox; |
7842 | goto out; | |
64a284d0 | 7843 | } |
dd257933 | 7844 | |
5db1c03f | 7845 | status = nfs_ok; |
dd257933 | 7846 | *plst = lst; |
5db1c03f JL |
7847 | out: |
7848 | nfs4_put_stateowner(&lo->lo_owner); | |
7849 | return status; | |
64a284d0 BF |
7850 | } |
7851 | ||
1da177e4 LT |
7852 | /* |
7853 | * LOCK operation | |
7854 | */ | |
b37ad28b | 7855 | __be32 |
ca364317 | 7856 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 7857 | union nfsd4_op_u *u) |
1da177e4 | 7858 | { |
eb69853d | 7859 | struct nfsd4_lock *lock = &u->lock; |
fe0750e5 BF |
7860 | struct nfs4_openowner *open_sop = NULL; |
7861 | struct nfs4_lockowner *lock_sop = NULL; | |
3d0fabd5 | 7862 | struct nfs4_ol_stateid *lock_stp = NULL; |
0667b1e9 | 7863 | struct nfs4_ol_stateid *open_stp = NULL; |
7214e860 | 7864 | struct nfs4_file *fp; |
eb82dd39 | 7865 | struct nfsd_file *nf = NULL; |
76d348fa | 7866 | struct nfsd4_blocked_lock *nbl = NULL; |
21179d81 JL |
7867 | struct file_lock *file_lock = NULL; |
7868 | struct file_lock *conflock = NULL; | |
2dd10de8 | 7869 | struct super_block *sb; |
b37ad28b | 7870 | __be32 status = 0; |
b34f27aa | 7871 | int lkflg; |
b8dd7b9a | 7872 | int err; |
5db1c03f | 7873 | bool new = false; |
60f3154d JL |
7874 | unsigned char type; |
7875 | unsigned int flags = FL_POSIX; | |
3320fef1 SK |
7876 | struct net *net = SVC_NET(rqstp); |
7877 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 LT |
7878 | |
7879 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
7880 | (long long) lock->lk_offset, | |
7881 | (long long) lock->lk_length); | |
7882 | ||
1da177e4 LT |
7883 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
7884 | return nfserr_inval; | |
7885 | ||
ca364317 | 7886 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
8837abca | 7887 | S_IFREG, NFSD_MAY_LOCK))) { |
a6f6ef2f AA |
7888 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
7889 | return status; | |
7890 | } | |
2dd10de8 | 7891 | sb = cstate->current_fh.fh_dentry->d_sb; |
a6f6ef2f | 7892 | |
1da177e4 | 7893 | if (lock->lk_is_new) { |
684e5638 BF |
7894 | if (nfsd4_has_session(cstate)) |
7895 | /* See rfc 5661 18.10.3: given clientid is ignored: */ | |
76f6c9e1 | 7896 | memcpy(&lock->lk_new_clientid, |
ec59659b | 7897 | &cstate->clp->cl_clientid, |
684e5638 BF |
7898 | sizeof(clientid_t)); |
7899 | ||
1da177e4 | 7900 | /* validate and update open stateid and open seqid */ |
c0a5d93e | 7901 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
1da177e4 LT |
7902 | lock->lk_new_open_seqid, |
7903 | &lock->lk_new_open_stateid, | |
3320fef1 | 7904 | &open_stp, nn); |
37515177 | 7905 | if (status) |
1da177e4 | 7906 | goto out; |
feb9dad5 | 7907 | mutex_unlock(&open_stp->st_mutex); |
fe0750e5 | 7908 | open_sop = openowner(open_stp->st_stateowner); |
b34f27aa | 7909 | status = nfserr_bad_stateid; |
684e5638 | 7910 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
76f6c9e1 | 7911 | &lock->lk_new_clientid)) |
b34f27aa | 7912 | goto out; |
64a284d0 | 7913 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
5db1c03f | 7914 | &lock_stp, &new); |
3d0fabd5 | 7915 | } else { |
dd453dfd | 7916 | status = nfs4_preprocess_seqid_op(cstate, |
3f29cc82 N |
7917 | lock->lk_old_lock_seqid, |
7918 | &lock->lk_old_lock_stateid, | |
7919 | SC_TYPE_LOCK, 0, &lock_stp, | |
7920 | nn); | |
3d0fabd5 | 7921 | } |
e1aaa891 BF |
7922 | if (status) |
7923 | goto out; | |
64a284d0 | 7924 | lock_sop = lockowner(lock_stp->st_stateowner); |
1da177e4 | 7925 | |
b34f27aa BF |
7926 | lkflg = setlkflg(lock->lk_type); |
7927 | status = nfs4_check_openmode(lock_stp, lkflg); | |
7928 | if (status) | |
7929 | goto out; | |
7930 | ||
0dd395dc | 7931 | status = nfserr_grace; |
3320fef1 | 7932 | if (locks_in_grace(net) && !lock->lk_reclaim) |
0dd395dc N |
7933 | goto out; |
7934 | status = nfserr_no_grace; | |
3320fef1 | 7935 | if (!locks_in_grace(net) && lock->lk_reclaim) |
0dd395dc N |
7936 | goto out; |
7937 | ||
bb0a55bb | 7938 | if (lock->lk_reclaim) |
60f3154d | 7939 | flags |= FL_RECLAIM; |
bb0a55bb | 7940 | |
11b9164a | 7941 | fp = lock_stp->st_stid.sc_file; |
1da177e4 | 7942 | switch (lock->lk_type) { |
1da177e4 | 7943 | case NFS4_READW_LT: |
2dd10de8 AA |
7944 | if (nfsd4_has_session(cstate) || |
7945 | exportfs_lock_op_is_async(sb->s_export_op)) | |
60f3154d | 7946 | flags |= FL_SLEEP; |
df561f66 | 7947 | fallthrough; |
76d348fa | 7948 | case NFS4_READ_LT: |
7214e860 | 7949 | spin_lock(&fp->fi_lock); |
eb82dd39 JL |
7950 | nf = find_readable_file_locked(fp); |
7951 | if (nf) | |
0997b173 | 7952 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
7214e860 | 7953 | spin_unlock(&fp->fi_lock); |
60f3154d | 7954 | type = F_RDLCK; |
529d7b2a | 7955 | break; |
1da177e4 | 7956 | case NFS4_WRITEW_LT: |
2dd10de8 AA |
7957 | if (nfsd4_has_session(cstate) || |
7958 | exportfs_lock_op_is_async(sb->s_export_op)) | |
60f3154d | 7959 | flags |= FL_SLEEP; |
df561f66 | 7960 | fallthrough; |
76d348fa | 7961 | case NFS4_WRITE_LT: |
7214e860 | 7962 | spin_lock(&fp->fi_lock); |
eb82dd39 JL |
7963 | nf = find_writeable_file_locked(fp); |
7964 | if (nf) | |
0997b173 | 7965 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
7214e860 | 7966 | spin_unlock(&fp->fi_lock); |
60f3154d | 7967 | type = F_WRLCK; |
529d7b2a | 7968 | break; |
1da177e4 LT |
7969 | default: |
7970 | status = nfserr_inval; | |
7971 | goto out; | |
7972 | } | |
76d348fa | 7973 | |
eb82dd39 | 7974 | if (!nf) { |
f9d7562f BF |
7975 | status = nfserr_openmode; |
7976 | goto out; | |
7977 | } | |
aef9583b | 7978 | |
40595cdc BF |
7979 | /* |
7980 | * Most filesystems with their own ->lock operations will block | |
7981 | * the nfsd thread waiting to acquire the lock. That leads to | |
7982 | * deadlocks (we don't want every nfsd thread tied up waiting | |
7983 | * for file locks), so don't attempt blocking lock notifications | |
7984 | * on those filesystems: | |
7985 | */ | |
2dd10de8 | 7986 | if (!exportfs_lock_op_is_async(sb->s_export_op)) |
60f3154d | 7987 | flags &= ~FL_SLEEP; |
40595cdc | 7988 | |
76d348fa JL |
7989 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
7990 | if (!nbl) { | |
7991 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); | |
7992 | status = nfserr_jukebox; | |
7993 | goto out; | |
7994 | } | |
7995 | ||
7996 | file_lock = &nbl->nbl_lock; | |
05580bbf JL |
7997 | file_lock->c.flc_type = type; |
7998 | file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); | |
7999 | file_lock->c.flc_pid = current->tgid; | |
8000 | file_lock->c.flc_file = nf->nf_file; | |
8001 | file_lock->c.flc_flags = flags; | |
21179d81 JL |
8002 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
8003 | file_lock->fl_start = lock->lk_offset; | |
8004 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); | |
8005 | nfs4_transform_lock_offset(file_lock); | |
8006 | ||
8007 | conflock = locks_alloc_lock(); | |
8008 | if (!conflock) { | |
8009 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
8010 | status = nfserr_jukebox; | |
8011 | goto out; | |
8012 | } | |
1da177e4 | 8013 | |
60f3154d | 8014 | if (flags & FL_SLEEP) { |
20b7d86f | 8015 | nbl->nbl_time = ktime_get_boottime_seconds(); |
0cc11a61 | 8016 | spin_lock(&nn->blocked_locks_lock); |
76d348fa | 8017 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
7919d0a2 | 8018 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
47446d74 | 8019 | kref_get(&nbl->nbl_kref); |
0cc11a61 | 8020 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa JL |
8021 | } |
8022 | ||
eb82dd39 | 8023 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
76d348fa | 8024 | switch (err) { |
1da177e4 | 8025 | case 0: /* success! */ |
9767feb2 | 8026 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
b8dd7b9a | 8027 | status = 0; |
03f318ca BF |
8028 | if (lock->lk_reclaim) |
8029 | nn->somebody_reclaimed = true; | |
eb76b3fd | 8030 | break; |
76d348fa | 8031 | case FILE_LOCK_DEFERRED: |
47446d74 | 8032 | kref_put(&nbl->nbl_kref, free_nbl); |
76d348fa | 8033 | nbl = NULL; |
df561f66 | 8034 | fallthrough; |
76d348fa | 8035 | case -EAGAIN: /* conflock holds conflicting lock */ |
eb76b3fd AA |
8036 | status = nfserr_denied; |
8037 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
21179d81 | 8038 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
eb76b3fd | 8039 | break; |
76d348fa | 8040 | case -EDEADLK: |
1da177e4 | 8041 | status = nfserr_deadlock; |
eb76b3fd | 8042 | break; |
3e772463 | 8043 | default: |
fd85b817 | 8044 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
3e772463 | 8045 | status = nfserrno(err); |
eb76b3fd | 8046 | break; |
1da177e4 | 8047 | } |
1da177e4 | 8048 | out: |
76d348fa JL |
8049 | if (nbl) { |
8050 | /* dequeue it if we queued it before */ | |
60f3154d | 8051 | if (flags & FL_SLEEP) { |
0cc11a61 | 8052 | spin_lock(&nn->blocked_locks_lock); |
47446d74 VA |
8053 | if (!list_empty(&nbl->nbl_list) && |
8054 | !list_empty(&nbl->nbl_lru)) { | |
8055 | list_del_init(&nbl->nbl_list); | |
8056 | list_del_init(&nbl->nbl_lru); | |
8057 | kref_put(&nbl->nbl_kref, free_nbl); | |
8058 | } | |
8059 | /* nbl can use one of lists to be linked to reaplist */ | |
0cc11a61 | 8060 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa JL |
8061 | } |
8062 | free_blocked_lock(nbl); | |
8063 | } | |
eb82dd39 JL |
8064 | if (nf) |
8065 | nfsd_file_put(nf); | |
5db1c03f JL |
8066 | if (lock_stp) { |
8067 | /* Bump seqid manually if the 4.0 replay owner is openowner */ | |
8068 | if (cstate->replay_owner && | |
8069 | cstate->replay_owner != &lock_sop->lo_owner && | |
8070 | seqid_mutating_err(ntohl(status))) | |
8071 | lock_sop->lo_owner.so_seqid++; | |
8072 | ||
8073 | /* | |
8074 | * If this is a new, never-before-used stateid, and we are | |
8075 | * returning an error, then just go ahead and release it. | |
8076 | */ | |
25020720 | 8077 | if (status && new) |
5db1c03f | 8078 | release_lock_stateid(lock_stp); |
beeca19c TM |
8079 | |
8080 | mutex_unlock(&lock_stp->st_mutex); | |
5db1c03f | 8081 | |
3d0fabd5 | 8082 | nfs4_put_stid(&lock_stp->st_stid); |
5db1c03f | 8083 | } |
0667b1e9 TM |
8084 | if (open_stp) |
8085 | nfs4_put_stid(&open_stp->st_stid); | |
9411b1d4 | 8086 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
8087 | if (conflock) |
8088 | locks_free_lock(conflock); | |
1da177e4 LT |
8089 | return status; |
8090 | } | |
8091 | ||
92d82e99 CL |
8092 | void nfsd4_lock_release(union nfsd4_op_u *u) |
8093 | { | |
8094 | struct nfsd4_lock *lock = &u->lock; | |
8095 | struct nfsd4_lock_denied *deny = &lock->lk_denied; | |
8096 | ||
8097 | kfree(deny->ld_owner.data); | |
8098 | } | |
8099 | ||
55ef1274 BF |
8100 | /* |
8101 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, | |
8102 | * so we do a temporary open here just to get an open file to pass to | |
0bcc7ca4 | 8103 | * vfs_test_lock. |
55ef1274 | 8104 | */ |
04da6e9d | 8105 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
55ef1274 | 8106 | { |
6b556ca2 | 8107 | struct nfsd_file *nf; |
bb4d53d6 | 8108 | struct inode *inode; |
217fd6f6 BF |
8109 | __be32 err; |
8110 | ||
8111 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); | |
8112 | if (err) | |
8113 | return err; | |
bb4d53d6 N |
8114 | inode = fhp->fh_dentry->d_inode; |
8115 | inode_lock(inode); /* to block new leases till after test_lock: */ | |
8116 | err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); | |
217fd6f6 BF |
8117 | if (err) |
8118 | goto out; | |
05580bbf | 8119 | lock->c.flc_file = nf->nf_file; |
217fd6f6 | 8120 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
05580bbf | 8121 | lock->c.flc_file = NULL; |
217fd6f6 | 8122 | out: |
bb4d53d6 | 8123 | inode_unlock(inode); |
217fd6f6 | 8124 | nfsd_file_put(nf); |
55ef1274 BF |
8125 | return err; |
8126 | } | |
8127 | ||
1da177e4 LT |
8128 | /* |
8129 | * LOCKT operation | |
8130 | */ | |
b37ad28b | 8131 | __be32 |
ca364317 | 8132 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 8133 | union nfsd4_op_u *u) |
1da177e4 | 8134 | { |
eb69853d | 8135 | struct nfsd4_lockt *lockt = &u->lockt; |
21179d81 | 8136 | struct file_lock *file_lock = NULL; |
5db1c03f | 8137 | struct nfs4_lockowner *lo = NULL; |
b37ad28b | 8138 | __be32 status; |
7f2210fa | 8139 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 8140 | |
5ccb0066 | 8141 | if (locks_in_grace(SVC_NET(rqstp))) |
1da177e4 LT |
8142 | return nfserr_grace; |
8143 | ||
8144 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
8145 | return nfserr_inval; | |
8146 | ||
9b2ef62b | 8147 | if (!nfsd4_has_session(cstate)) { |
f71475ba | 8148 | status = set_client(&lockt->lt_clientid, cstate, nn); |
9b2ef62b BF |
8149 | if (status) |
8150 | goto out; | |
8151 | } | |
1da177e4 | 8152 | |
75c096f7 | 8153 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
1da177e4 | 8154 | goto out; |
1da177e4 | 8155 | |
21179d81 JL |
8156 | file_lock = locks_alloc_lock(); |
8157 | if (!file_lock) { | |
8158 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
8159 | status = nfserr_jukebox; | |
8160 | goto out; | |
8161 | } | |
6cd90662 | 8162 | |
1da177e4 LT |
8163 | switch (lockt->lt_type) { |
8164 | case NFS4_READ_LT: | |
8165 | case NFS4_READW_LT: | |
05580bbf | 8166 | file_lock->c.flc_type = F_RDLCK; |
f50c9d79 | 8167 | break; |
1da177e4 LT |
8168 | case NFS4_WRITE_LT: |
8169 | case NFS4_WRITEW_LT: | |
05580bbf | 8170 | file_lock->c.flc_type = F_WRLCK; |
f50c9d79 | 8171 | break; |
1da177e4 | 8172 | default: |
2fdada03 | 8173 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
1da177e4 | 8174 | status = nfserr_inval; |
f50c9d79 | 8175 | goto out; |
1da177e4 LT |
8176 | } |
8177 | ||
c8623999 | 8178 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
fe0750e5 | 8179 | if (lo) |
05580bbf JL |
8180 | file_lock->c.flc_owner = (fl_owner_t)lo; |
8181 | file_lock->c.flc_pid = current->tgid; | |
8182 | file_lock->c.flc_flags = FL_POSIX; | |
1da177e4 | 8183 | |
21179d81 JL |
8184 | file_lock->fl_start = lockt->lt_offset; |
8185 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); | |
1da177e4 | 8186 | |
21179d81 | 8187 | nfs4_transform_lock_offset(file_lock); |
1da177e4 | 8188 | |
21179d81 | 8189 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
04da6e9d | 8190 | if (status) |
fd85b817 | 8191 | goto out; |
04da6e9d | 8192 | |
05580bbf | 8193 | if (file_lock->c.flc_type != F_UNLCK) { |
1da177e4 | 8194 | status = nfserr_denied; |
21179d81 | 8195 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
1da177e4 LT |
8196 | } |
8197 | out: | |
5db1c03f JL |
8198 | if (lo) |
8199 | nfs4_put_stateowner(&lo->lo_owner); | |
21179d81 JL |
8200 | if (file_lock) |
8201 | locks_free_lock(file_lock); | |
1da177e4 LT |
8202 | return status; |
8203 | } | |
8204 | ||
92d82e99 CL |
8205 | void nfsd4_lockt_release(union nfsd4_op_u *u) |
8206 | { | |
8207 | struct nfsd4_lockt *lockt = &u->lockt; | |
8208 | struct nfsd4_lock_denied *deny = &lockt->lt_denied; | |
8209 | ||
8210 | kfree(deny->ld_owner.data); | |
8211 | } | |
8212 | ||
b37ad28b | 8213 | __be32 |
ca364317 | 8214 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 8215 | union nfsd4_op_u *u) |
1da177e4 | 8216 | { |
eb69853d | 8217 | struct nfsd4_locku *locku = &u->locku; |
dcef0413 | 8218 | struct nfs4_ol_stateid *stp; |
eb82dd39 | 8219 | struct nfsd_file *nf = NULL; |
21179d81 | 8220 | struct file_lock *file_lock = NULL; |
b37ad28b | 8221 | __be32 status; |
b8dd7b9a | 8222 | int err; |
3320fef1 SK |
8223 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
8224 | ||
1da177e4 LT |
8225 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
8226 | (long long) locku->lu_offset, | |
8227 | (long long) locku->lu_length); | |
8228 | ||
8229 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
8230 | return nfserr_inval; | |
8231 | ||
9072d5c6 | 8232 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
3f29cc82 N |
8233 | &locku->lu_stateid, SC_TYPE_LOCK, 0, |
8234 | &stp, nn); | |
9072d5c6 | 8235 | if (status) |
1da177e4 | 8236 | goto out; |
eb82dd39 JL |
8237 | nf = find_any_file(stp->st_stid.sc_file); |
8238 | if (!nf) { | |
f9d7562f | 8239 | status = nfserr_lock_range; |
858cc573 | 8240 | goto put_stateid; |
f9d7562f | 8241 | } |
21179d81 JL |
8242 | file_lock = locks_alloc_lock(); |
8243 | if (!file_lock) { | |
8244 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
8245 | status = nfserr_jukebox; | |
eb82dd39 | 8246 | goto put_file; |
21179d81 | 8247 | } |
6cd90662 | 8248 | |
05580bbf JL |
8249 | file_lock->c.flc_type = F_UNLCK; |
8250 | file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); | |
8251 | file_lock->c.flc_pid = current->tgid; | |
8252 | file_lock->c.flc_file = nf->nf_file; | |
8253 | file_lock->c.flc_flags = FL_POSIX; | |
21179d81 JL |
8254 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
8255 | file_lock->fl_start = locku->lu_offset; | |
8256 | ||
8257 | file_lock->fl_end = last_byte_offset(locku->lu_offset, | |
8258 | locku->lu_length); | |
8259 | nfs4_transform_lock_offset(file_lock); | |
1da177e4 | 8260 | |
eb82dd39 | 8261 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
b8dd7b9a | 8262 | if (err) { |
fd85b817 | 8263 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
1da177e4 LT |
8264 | goto out_nfserr; |
8265 | } | |
9767feb2 | 8266 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
eb82dd39 JL |
8267 | put_file: |
8268 | nfsd_file_put(nf); | |
858cc573 | 8269 | put_stateid: |
feb9dad5 | 8270 | mutex_unlock(&stp->st_mutex); |
858cc573 | 8271 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 8272 | out: |
9411b1d4 | 8273 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
8274 | if (file_lock) |
8275 | locks_free_lock(file_lock); | |
1da177e4 LT |
8276 | return status; |
8277 | ||
8278 | out_nfserr: | |
b8dd7b9a | 8279 | status = nfserrno(err); |
eb82dd39 | 8280 | goto put_file; |
1da177e4 LT |
8281 | } |
8282 | ||
8283 | /* | |
8284 | * returns | |
f9c00c3a JL |
8285 | * true: locks held by lockowner |
8286 | * false: no locks held by lockowner | |
1da177e4 | 8287 | */ |
f9c00c3a JL |
8288 | static bool |
8289 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) | |
1da177e4 | 8290 | { |
bd61e0a9 | 8291 | struct file_lock *fl; |
f9c00c3a | 8292 | int status = false; |
edcf9725 | 8293 | struct nfsd_file *nf; |
f9c00c3a | 8294 | struct inode *inode; |
bd61e0a9 | 8295 | struct file_lock_context *flctx; |
f9c00c3a | 8296 | |
edcf9725 N |
8297 | spin_lock(&fp->fi_lock); |
8298 | nf = find_any_file_locked(fp); | |
eb82dd39 | 8299 | if (!nf) { |
f9c00c3a JL |
8300 | /* Any valid lock stateid should have some sort of access */ |
8301 | WARN_ON_ONCE(1); | |
edcf9725 | 8302 | goto out; |
f9c00c3a JL |
8303 | } |
8304 | ||
c65454a9 | 8305 | inode = file_inode(nf->nf_file); |
77c67530 | 8306 | flctx = locks_inode_context(inode); |
bd61e0a9 JL |
8307 | |
8308 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { | |
6109c850 | 8309 | spin_lock(&flctx->flc_lock); |
60f3154d | 8310 | for_each_file_lock(fl, &flctx->flc_posix) { |
05580bbf | 8311 | if (fl->c.flc_owner == (fl_owner_t)lowner) { |
bd61e0a9 JL |
8312 | status = true; |
8313 | break; | |
8314 | } | |
796dadfd | 8315 | } |
6109c850 | 8316 | spin_unlock(&flctx->flc_lock); |
1da177e4 | 8317 | } |
edcf9725 N |
8318 | out: |
8319 | spin_unlock(&fp->fi_lock); | |
1da177e4 LT |
8320 | return status; |
8321 | } | |
8322 | ||
043862b0 CL |
8323 | /** |
8324 | * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations | |
8325 | * @rqstp: RPC transaction | |
8326 | * @cstate: NFSv4 COMPOUND state | |
8327 | * @u: RELEASE_LOCKOWNER arguments | |
8328 | * | |
edcf9725 N |
8329 | * Check if theree are any locks still held and if not - free the lockowner |
8330 | * and any lock state that is owned. | |
043862b0 CL |
8331 | * |
8332 | * Return values: | |
8333 | * %nfs_ok: lockowner released or not found | |
8334 | * %nfserr_locks_held: lockowner still in use | |
8335 | * %nfserr_stale_clientid: clientid no longer active | |
8336 | * %nfserr_expired: clientid not recognized | |
8337 | */ | |
b37ad28b | 8338 | __be32 |
b591480b BF |
8339 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
8340 | struct nfsd4_compound_state *cstate, | |
eb69853d | 8341 | union nfsd4_op_u *u) |
1da177e4 | 8342 | { |
eb69853d | 8343 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
bd8fdb6e | 8344 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 8345 | clientid_t *clid = &rlockowner->rl_clientid; |
dcef0413 | 8346 | struct nfs4_ol_stateid *stp; |
bd8fdb6e | 8347 | struct nfs4_lockowner *lo; |
c58c6610 | 8348 | struct nfs4_client *clp; |
bd8fdb6e CL |
8349 | LIST_HEAD(reaplist); |
8350 | __be32 status; | |
1da177e4 LT |
8351 | |
8352 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
8353 | clid->cl_boot, clid->cl_id); | |
8354 | ||
f71475ba | 8355 | status = set_client(clid, cstate, nn); |
9b2ef62b | 8356 | if (status) |
51f5e783 | 8357 | return status; |
d4f0489f | 8358 | clp = cstate->clp; |
fd44907c | 8359 | |
bd8fdb6e CL |
8360 | spin_lock(&clp->cl_lock); |
8361 | lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner); | |
88584818 CL |
8362 | if (!lo) { |
8363 | spin_unlock(&clp->cl_lock); | |
043862b0 | 8364 | return nfs_ok; |
88584818 | 8365 | } |
edcf9725 N |
8366 | |
8367 | list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { | |
8368 | if (check_for_locks(stp->st_stid.sc_file, lo)) { | |
8369 | spin_unlock(&clp->cl_lock); | |
8370 | nfs4_put_stateowner(&lo->lo_owner); | |
8371 | return nfserr_locks_held; | |
8372 | } | |
bd8fdb6e | 8373 | } |
88584818 CL |
8374 | unhash_lockowner_locked(lo); |
8375 | while (!list_empty(&lo->lo_owner.so_stateids)) { | |
8376 | stp = list_first_entry(&lo->lo_owner.so_stateids, | |
8377 | struct nfs4_ol_stateid, | |
8378 | st_perstateowner); | |
c6540026 | 8379 | unhash_lock_stateid(stp); |
88584818 CL |
8380 | put_ol_stateid_locked(stp, &reaplist); |
8381 | } | |
c58c6610 | 8382 | spin_unlock(&clp->cl_lock); |
043862b0 | 8383 | |
88584818 | 8384 | free_ol_stateid_reaplist(&reaplist); |
68ef3bc3 | 8385 | remove_blocked_locks(lo); |
88584818 | 8386 | nfs4_put_stateowner(&lo->lo_owner); |
043862b0 | 8387 | return nfs_ok; |
1da177e4 LT |
8388 | } |
8389 | ||
8390 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 8391 | alloc_reclaim(void) |
1da177e4 | 8392 | { |
a55370a3 | 8393 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
8394 | } |
8395 | ||
0ce0c2b5 | 8396 | bool |
6b189105 | 8397 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
c7b9a459 | 8398 | { |
0ce0c2b5 | 8399 | struct nfs4_client_reclaim *crp; |
c7b9a459 | 8400 | |
52e19c09 | 8401 | crp = nfsd4_find_reclaim_client(name, nn); |
0ce0c2b5 | 8402 | return (crp && crp->cr_clp); |
c7b9a459 N |
8403 | } |
8404 | ||
1da177e4 LT |
8405 | /* |
8406 | * failure => all reset bets are off, nfserr_no_grace... | |
6b189105 SM |
8407 | * |
8408 | * The caller is responsible for freeing name.data if NULL is returned (it | |
8409 | * will be freed in nfs4_remove_reclaim_record in the normal case). | |
1da177e4 | 8410 | */ |
772a9bbb | 8411 | struct nfs4_client_reclaim * |
6ee95d1c SM |
8412 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
8413 | struct nfsd_net *nn) | |
1da177e4 LT |
8414 | { |
8415 | unsigned int strhashval; | |
772a9bbb | 8416 | struct nfs4_client_reclaim *crp; |
1da177e4 | 8417 | |
a55370a3 | 8418 | crp = alloc_reclaim(); |
772a9bbb JL |
8419 | if (crp) { |
8420 | strhashval = clientstr_hashval(name); | |
8421 | INIT_LIST_HEAD(&crp->cr_strhash); | |
52e19c09 | 8422 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
6b189105 SM |
8423 | crp->cr_name.data = name.data; |
8424 | crp->cr_name.len = name.len; | |
6ee95d1c SM |
8425 | crp->cr_princhash.data = princhash.data; |
8426 | crp->cr_princhash.len = princhash.len; | |
0ce0c2b5 | 8427 | crp->cr_clp = NULL; |
52e19c09 | 8428 | nn->reclaim_str_hashtbl_size++; |
772a9bbb JL |
8429 | } |
8430 | return crp; | |
1da177e4 LT |
8431 | } |
8432 | ||
ce30e539 | 8433 | void |
52e19c09 | 8434 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
ce30e539 JL |
8435 | { |
8436 | list_del(&crp->cr_strhash); | |
6b189105 | 8437 | kfree(crp->cr_name.data); |
6ee95d1c | 8438 | kfree(crp->cr_princhash.data); |
ce30e539 | 8439 | kfree(crp); |
52e19c09 | 8440 | nn->reclaim_str_hashtbl_size--; |
ce30e539 JL |
8441 | } |
8442 | ||
2a4317c5 | 8443 | void |
52e19c09 | 8444 | nfs4_release_reclaim(struct nfsd_net *nn) |
1da177e4 LT |
8445 | { |
8446 | struct nfs4_client_reclaim *crp = NULL; | |
8447 | int i; | |
8448 | ||
1da177e4 | 8449 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
52e19c09 SK |
8450 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
8451 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, | |
1da177e4 | 8452 | struct nfs4_client_reclaim, cr_strhash); |
52e19c09 | 8453 | nfs4_remove_reclaim_record(crp, nn); |
1da177e4 LT |
8454 | } |
8455 | } | |
063b0fb9 | 8456 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
1da177e4 LT |
8457 | } |
8458 | ||
8459 | /* | |
8460 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
2a4317c5 | 8461 | struct nfs4_client_reclaim * |
6b189105 | 8462 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
1da177e4 LT |
8463 | { |
8464 | unsigned int strhashval; | |
1da177e4 LT |
8465 | struct nfs4_client_reclaim *crp = NULL; |
8466 | ||
6b189105 | 8467 | strhashval = clientstr_hashval(name); |
52e19c09 | 8468 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
6b189105 | 8469 | if (compare_blob(&crp->cr_name, &name) == 0) { |
1da177e4 LT |
8470 | return crp; |
8471 | } | |
8472 | } | |
8473 | return NULL; | |
8474 | } | |
8475 | ||
b37ad28b | 8476 | __be32 |
1722b046 | 8477 | nfs4_check_open_reclaim(struct nfs4_client *clp) |
1da177e4 | 8478 | { |
1722b046 | 8479 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
3b3e7b72 JL |
8480 | return nfserr_no_grace; |
8481 | ||
1722b046 | 8482 | if (nfsd4_client_record_check(clp)) |
0fe492db TM |
8483 | return nfserr_reclaim_bad; |
8484 | ||
8485 | return nfs_ok; | |
1da177e4 LT |
8486 | } |
8487 | ||
c2f1a551 MS |
8488 | /* |
8489 | * Since the lifetime of a delegation isn't limited to that of an open, a | |
8490 | * client may quite reasonably hang on to a delegation as long as it has | |
8491 | * the inode cached. This becomes an obvious problem the first time a | |
8492 | * client's inode cache approaches the size of the server's total memory. | |
8493 | * | |
8494 | * For now we avoid this problem by imposing a hard limit on the number | |
8495 | * of delegations, which varies according to the server's memory size. | |
8496 | */ | |
8497 | static void | |
8498 | set_max_delegations(void) | |
8499 | { | |
8500 | /* | |
8501 | * Allow at most 4 delegations per megabyte of RAM. Quick | |
8502 | * estimates suggest that in the worst case (where every delegation | |
8503 | * is for a different inode), a delegation could take about 1.5K, | |
8504 | * giving a worst case usage of about 6% of memory. | |
8505 | */ | |
8506 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); | |
8507 | } | |
8508 | ||
d85ed443 | 8509 | static int nfs4_state_create_net(struct net *net) |
8daae4dc SK |
8510 | { |
8511 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
8512 | int i; | |
8513 | ||
6da2ec56 KC |
8514 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
8515 | sizeof(struct list_head), | |
8516 | GFP_KERNEL); | |
8daae4dc | 8517 | if (!nn->conf_id_hashtbl) |
382a62e7 | 8518 | goto err; |
6da2ec56 KC |
8519 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
8520 | sizeof(struct list_head), | |
8521 | GFP_KERNEL); | |
0a7ec377 SK |
8522 | if (!nn->unconf_id_hashtbl) |
8523 | goto err_unconf_id; | |
6da2ec56 KC |
8524 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
8525 | sizeof(struct list_head), | |
8526 | GFP_KERNEL); | |
1872de0e SK |
8527 | if (!nn->sessionid_hashtbl) |
8528 | goto err_sessionid; | |
8daae4dc | 8529 | |
382a62e7 | 8530 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
8daae4dc | 8531 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
0a7ec377 | 8532 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
382a62e7 | 8533 | } |
1872de0e SK |
8534 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
8535 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); | |
382a62e7 | 8536 | nn->conf_name_tree = RB_ROOT; |
a99454aa | 8537 | nn->unconf_name_tree = RB_ROOT; |
9cc76801 | 8538 | nn->boot_time = ktime_get_real_seconds(); |
81833de1 VA |
8539 | nn->grace_ended = false; |
8540 | nn->nfsd4_manager.block_opens = true; | |
8541 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); | |
5ed58bb2 | 8542 | INIT_LIST_HEAD(&nn->client_lru); |
73758fed | 8543 | INIT_LIST_HEAD(&nn->close_lru); |
e8c69d17 | 8544 | INIT_LIST_HEAD(&nn->del_recall_lru); |
c9a49628 | 8545 | spin_lock_init(&nn->client_lock); |
e0639dc5 OK |
8546 | spin_lock_init(&nn->s2s_cp_lock); |
8547 | idr_init(&nn->s2s_cp_stateids); | |
8daae4dc | 8548 | |
0cc11a61 JL |
8549 | spin_lock_init(&nn->blocked_locks_lock); |
8550 | INIT_LIST_HEAD(&nn->blocked_locks_lru); | |
8551 | ||
09121281 | 8552 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
7c24fa22 | 8553 | INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker); |
d85ed443 | 8554 | get_net(net); |
09121281 | 8555 | |
d17452aa QZ |
8556 | nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client"); |
8557 | if (!nn->nfsd_client_shrinker) | |
f385f7d2 | 8558 | goto err_shrinker; |
d17452aa QZ |
8559 | |
8560 | nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan; | |
8561 | nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count; | |
8562 | nn->nfsd_client_shrinker->private_data = nn; | |
8563 | ||
8564 | shrinker_register(nn->nfsd_client_shrinker); | |
8565 | ||
8daae4dc | 8566 | return 0; |
382a62e7 | 8567 | |
f385f7d2 DN |
8568 | err_shrinker: |
8569 | put_net(net); | |
8570 | kfree(nn->sessionid_hashtbl); | |
1872de0e | 8571 | err_sessionid: |
9b531137 | 8572 | kfree(nn->unconf_id_hashtbl); |
0a7ec377 SK |
8573 | err_unconf_id: |
8574 | kfree(nn->conf_id_hashtbl); | |
382a62e7 SK |
8575 | err: |
8576 | return -ENOMEM; | |
8daae4dc SK |
8577 | } |
8578 | ||
8579 | static void | |
4dce0ac9 | 8580 | nfs4_state_destroy_net(struct net *net) |
8daae4dc SK |
8581 | { |
8582 | int i; | |
8583 | struct nfs4_client *clp = NULL; | |
8584 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
8585 | ||
8586 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { | |
8587 | while (!list_empty(&nn->conf_id_hashtbl[i])) { | |
8588 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
8589 | destroy_client(clp); | |
8590 | } | |
8591 | } | |
a99454aa | 8592 | |
68ef3bc3 JL |
8593 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
8594 | ||
2b905635 KM |
8595 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
8596 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { | |
8597 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
8598 | destroy_client(clp); | |
8599 | } | |
a99454aa SK |
8600 | } |
8601 | ||
1872de0e | 8602 | kfree(nn->sessionid_hashtbl); |
0a7ec377 | 8603 | kfree(nn->unconf_id_hashtbl); |
8daae4dc | 8604 | kfree(nn->conf_id_hashtbl); |
4dce0ac9 | 8605 | put_net(net); |
8daae4dc SK |
8606 | } |
8607 | ||
f252bc68 | 8608 | int |
d85ed443 | 8609 | nfs4_state_start_net(struct net *net) |
ac4d8ff2 | 8610 | { |
5e1533c7 | 8611 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b5a1a81e BF |
8612 | int ret; |
8613 | ||
681370f4 | 8614 | ret = nfs4_state_create_net(net); |
c6c7f2a8 | 8615 | if (ret) |
681370f4 | 8616 | return ret; |
d4318acd JL |
8617 | locks_start_grace(net, &nn->nfsd4_manager); |
8618 | nfsd4_client_tracking_init(net); | |
362063a5 SM |
8619 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
8620 | goto skip_grace; | |
20b7d86f | 8621 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
7e981a8a | 8622 | nn->nfsd4_grace, net->ns.inum); |
dd5e3fbc | 8623 | trace_nfsd_grace_start(nn); |
5284b44e | 8624 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
d85ed443 | 8625 | return 0; |
362063a5 SM |
8626 | |
8627 | skip_grace: | |
8628 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", | |
8629 | net->ns.inum); | |
8630 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); | |
8631 | nfsd4_end_grace(nn); | |
8632 | return 0; | |
d85ed443 SK |
8633 | } |
8634 | ||
8635 | /* initialization to perform when the nfsd service is started: */ | |
8636 | ||
8637 | int | |
8638 | nfs4_state_start(void) | |
8639 | { | |
8640 | int ret; | |
8641 | ||
d47b295e | 8642 | ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params); |
b5a1a81e | 8643 | if (ret) |
d76cc46b | 8644 | return ret; |
09121281 | 8645 | |
c2f1a551 | 8646 | set_max_delegations(); |
b5a1a81e | 8647 | return 0; |
1da177e4 LT |
8648 | } |
8649 | ||
f252bc68 | 8650 | void |
4dce0ac9 | 8651 | nfs4_state_shutdown_net(struct net *net) |
1da177e4 | 8652 | { |
1da177e4 | 8653 | struct nfs4_delegation *dp = NULL; |
1da177e4 | 8654 | struct list_head *pos, *next, reaplist; |
4dce0ac9 | 8655 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
1da177e4 | 8656 | |
d17452aa | 8657 | shrinker_free(nn->nfsd_client_shrinker); |
7c24fa22 | 8658 | cancel_work(&nn->nfsd_shrinker_work); |
4dce0ac9 SK |
8659 | cancel_delayed_work_sync(&nn->laundromat_work); |
8660 | locks_end_grace(&nn->nfsd4_manager); | |
ac55fdc4 | 8661 | |
1da177e4 | 8662 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 8663 | spin_lock(&state_lock); |
e8c69d17 | 8664 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 8665 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
3f29cc82 | 8666 | unhash_delegation_locked(dp, SC_STATUS_CLOSED); |
42690676 | 8667 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 8668 | } |
cdc97505 | 8669 | spin_unlock(&state_lock); |
1da177e4 LT |
8670 | list_for_each_safe(pos, next, &reaplist) { |
8671 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 8672 | list_del_init(&dp->dl_recall_lru); |
0af6e690 | 8673 | destroy_unhashed_deleg(dp); |
1da177e4 LT |
8674 | } |
8675 | ||
3320fef1 | 8676 | nfsd4_client_tracking_exit(net); |
4dce0ac9 | 8677 | nfs4_state_destroy_net(net); |
f4e44b39 DN |
8678 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
8679 | nfsd4_ssc_shutdown_umount(nn); | |
8680 | #endif | |
1da177e4 LT |
8681 | } |
8682 | ||
8683 | void | |
8684 | nfs4_state_shutdown(void) | |
8685 | { | |
4102db17 | 8686 | rhltable_destroy(&nfs4_file_rhltable); |
1da177e4 | 8687 | } |
8b70484c TM |
8688 | |
8689 | static void | |
8690 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
8691 | { | |
51100d2b OK |
8692 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
8693 | CURRENT_STATEID(stateid)) | |
37c593c5 | 8694 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
8b70484c TM |
8695 | } |
8696 | ||
8697 | static void | |
8698 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
8699 | { | |
37c593c5 TM |
8700 | if (cstate->minorversion) { |
8701 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); | |
51100d2b | 8702 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
37c593c5 TM |
8703 | } |
8704 | } | |
8705 | ||
8706 | void | |
8707 | clear_current_stateid(struct nfsd4_compound_state *cstate) | |
8708 | { | |
51100d2b | 8709 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
8b70484c TM |
8710 | } |
8711 | ||
62cd4a59 TM |
8712 | /* |
8713 | * functions to set current state id | |
8714 | */ | |
9428fe1a | 8715 | void |
b60e9859 CH |
8716 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
8717 | union nfsd4_op_u *u) | |
9428fe1a | 8718 | { |
b60e9859 | 8719 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
9428fe1a TM |
8720 | } |
8721 | ||
8b70484c | 8722 | void |
b60e9859 CH |
8723 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
8724 | union nfsd4_op_u *u) | |
8b70484c | 8725 | { |
b60e9859 | 8726 | put_stateid(cstate, &u->open.op_stateid); |
8b70484c TM |
8727 | } |
8728 | ||
62cd4a59 | 8729 | void |
b60e9859 CH |
8730 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
8731 | union nfsd4_op_u *u) | |
62cd4a59 | 8732 | { |
b60e9859 | 8733 | put_stateid(cstate, &u->close.cl_stateid); |
62cd4a59 TM |
8734 | } |
8735 | ||
8736 | void | |
b60e9859 CH |
8737 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
8738 | union nfsd4_op_u *u) | |
62cd4a59 | 8739 | { |
b60e9859 | 8740 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
62cd4a59 TM |
8741 | } |
8742 | ||
8743 | /* | |
8744 | * functions to consume current state id | |
8745 | */ | |
1e97b519 | 8746 | |
9428fe1a | 8747 | void |
57832e7b CH |
8748 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
8749 | union nfsd4_op_u *u) | |
9428fe1a | 8750 | { |
57832e7b | 8751 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
9428fe1a TM |
8752 | } |
8753 | ||
8754 | void | |
57832e7b CH |
8755 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
8756 | union nfsd4_op_u *u) | |
9428fe1a | 8757 | { |
57832e7b | 8758 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
9428fe1a TM |
8759 | } |
8760 | ||
1e97b519 | 8761 | void |
57832e7b CH |
8762 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
8763 | union nfsd4_op_u *u) | |
1e97b519 | 8764 | { |
57832e7b | 8765 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
1e97b519 TM |
8766 | } |
8767 | ||
8768 | void | |
57832e7b CH |
8769 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
8770 | union nfsd4_op_u *u) | |
1e97b519 | 8771 | { |
57832e7b | 8772 | get_stateid(cstate, &u->setattr.sa_stateid); |
1e97b519 TM |
8773 | } |
8774 | ||
8b70484c | 8775 | void |
57832e7b CH |
8776 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
8777 | union nfsd4_op_u *u) | |
8b70484c | 8778 | { |
57832e7b | 8779 | get_stateid(cstate, &u->close.cl_stateid); |
8b70484c TM |
8780 | } |
8781 | ||
8782 | void | |
57832e7b CH |
8783 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
8784 | union nfsd4_op_u *u) | |
8b70484c | 8785 | { |
57832e7b | 8786 | get_stateid(cstate, &u->locku.lu_stateid); |
8b70484c | 8787 | } |
30813e27 TM |
8788 | |
8789 | void | |
57832e7b CH |
8790 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
8791 | union nfsd4_op_u *u) | |
30813e27 | 8792 | { |
57832e7b | 8793 | get_stateid(cstate, &u->read.rd_stateid); |
30813e27 TM |
8794 | } |
8795 | ||
8796 | void | |
57832e7b CH |
8797 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
8798 | union nfsd4_op_u *u) | |
30813e27 | 8799 | { |
57832e7b | 8800 | get_stateid(cstate, &u->write.wr_stateid); |
30813e27 | 8801 | } |
fd19ca36 DN |
8802 | |
8803 | /** | |
8804 | * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict | |
8805 | * @rqstp: RPC transaction context | |
8806 | * @inode: file to be checked for a conflict | |
c5967721 DN |
8807 | * @modified: return true if file was modified |
8808 | * @size: new size of file if modified is true | |
fd19ca36 DN |
8809 | * |
8810 | * This function is called when there is a conflict between a write | |
8811 | * delegation and a change/size GETATTR from another client. The server | |
8812 | * must either use the CB_GETATTR to get the current values of the | |
8813 | * attributes from the client that holds the delegation or recall the | |
8814 | * delegation before replying to the GETATTR. See RFC 8881 section | |
8815 | * 18.7.4. | |
8816 | * | |
fd19ca36 DN |
8817 | * Returns 0 if there is no conflict; otherwise an nfs_stat |
8818 | * code is returned. | |
8819 | */ | |
8820 | __be32 | |
c5967721 DN |
8821 | nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode, |
8822 | bool *modified, u64 *size) | |
fd19ca36 | 8823 | { |
862bee84 | 8824 | __be32 status; |
4b148854 | 8825 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
fd19ca36 | 8826 | struct file_lock_context *ctx; |
c69ff407 | 8827 | struct file_lease *fl; |
862bee84 | 8828 | struct nfs4_delegation *dp; |
c5967721 DN |
8829 | struct iattr attrs; |
8830 | struct nfs4_cb_fattr *ncf; | |
fd19ca36 | 8831 | |
c5967721 | 8832 | *modified = false; |
fd19ca36 DN |
8833 | ctx = locks_inode_context(inode); |
8834 | if (!ctx) | |
8835 | return 0; | |
8836 | spin_lock(&ctx->flc_lock); | |
60f3154d | 8837 | for_each_file_lock(fl, &ctx->flc_lease) { |
c69ff407 JL |
8838 | unsigned char type = fl->c.flc_type; |
8839 | ||
05580bbf | 8840 | if (fl->c.flc_flags == FL_LAYOUT) |
fd19ca36 DN |
8841 | continue; |
8842 | if (fl->fl_lmops != &nfsd_lease_mng_ops) { | |
8843 | /* | |
8844 | * non-nfs lease, if it's a lease with F_RDLCK then | |
8845 | * we are done; there isn't any write delegation | |
8846 | * on this inode | |
8847 | */ | |
c69ff407 | 8848 | if (type == F_RDLCK) |
fd19ca36 DN |
8849 | break; |
8850 | goto break_lease; | |
8851 | } | |
c69ff407 | 8852 | if (type == F_WRLCK) { |
05580bbf | 8853 | dp = fl->c.flc_owner; |
fd19ca36 DN |
8854 | if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) { |
8855 | spin_unlock(&ctx->flc_lock); | |
8856 | return 0; | |
8857 | } | |
8858 | break_lease: | |
4b148854 | 8859 | nfsd_stats_wdeleg_getattr_inc(nn); |
a01c9fe3 | 8860 | dp = fl->c.flc_owner; |
c5967721 DN |
8861 | ncf = &dp->dl_cb_fattr; |
8862 | nfs4_cb_getattr(&dp->dl_cb_fattr); | |
fd19ca36 | 8863 | spin_unlock(&ctx->flc_lock); |
c5967721 DN |
8864 | wait_on_bit_timeout(&ncf->ncf_cb_flags, CB_GETATTR_BUSY, |
8865 | TASK_INTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT); | |
8866 | if (ncf->ncf_cb_status) { | |
8867 | /* Recall delegation only if client didn't respond */ | |
8868 | status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); | |
8869 | if (status != nfserr_jukebox || | |
8870 | !nfsd_wait_for_delegreturn(rqstp, inode)) | |
8871 | return status; | |
8872 | } | |
8873 | if (!ncf->ncf_file_modified && | |
8874 | (ncf->ncf_initial_cinfo != ncf->ncf_cb_change || | |
8875 | ncf->ncf_cur_fsize != ncf->ncf_cb_fsize)) | |
8876 | ncf->ncf_file_modified = true; | |
8877 | if (ncf->ncf_file_modified) { | |
8878 | /* | |
8879 | * Per section 10.4.3 of RFC 8881, the server would | |
8880 | * not update the file's metadata with the client's | |
8881 | * modified size | |
8882 | */ | |
8883 | attrs.ia_mtime = attrs.ia_ctime = current_time(inode); | |
8884 | attrs.ia_valid = ATTR_MTIME | ATTR_CTIME; | |
8885 | setattr_copy(&nop_mnt_idmap, inode, &attrs); | |
8886 | mark_inode_dirty(inode); | |
8887 | ncf->ncf_cur_fsize = ncf->ncf_cb_fsize; | |
8888 | *size = ncf->ncf_cur_fsize; | |
8889 | *modified = true; | |
8890 | } | |
fd19ca36 DN |
8891 | return 0; |
8892 | } | |
8893 | break; | |
8894 | } | |
8895 | spin_unlock(&ctx->flc_lock); | |
8896 | return 0; | |
8897 | } |