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