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