1 /* 2 * fs/nfs/nfs4proc.c 3 * 4 * Client-side procedure declarations for NFSv4. 5 * 6 * Copyright (c) 2002 The Regents of the University of Michigan. 7 * All rights reserved. 8 * 9 * Kendrick Smith <kmsmith@umich.edu> 10 * Andy Adamson <andros@umich.edu> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <linux/mm.h> 39 #include <linux/delay.h> 40 #include <linux/errno.h> 41 #include <linux/file.h> 42 #include <linux/string.h> 43 #include <linux/ratelimit.h> 44 #include <linux/printk.h> 45 #include <linux/slab.h> 46 #include <linux/sunrpc/clnt.h> 47 #include <linux/nfs.h> 48 #include <linux/nfs4.h> 49 #include <linux/nfs_fs.h> 50 #include <linux/nfs_page.h> 51 #include <linux/nfs_mount.h> 52 #include <linux/namei.h> 53 #include <linux/mount.h> 54 #include <linux/module.h> 55 #include <linux/xattr.h> 56 #include <linux/utsname.h> 57 #include <linux/freezer.h> 58 59 #include "nfs4_fs.h" 60 #include "delegation.h" 61 #include "internal.h" 62 #include "iostat.h" 63 #include "callback.h" 64 #include "pnfs.h" 65 #include "netns.h" 66 #include "nfs4idmap.h" 67 #include "nfs4session.h" 68 #include "fscache.h" 69 70 #include "nfs4trace.h" 71 72 #define NFSDBG_FACILITY NFSDBG_PROC 73 74 #define NFS4_POLL_RETRY_MIN (HZ/10) 75 #define NFS4_POLL_RETRY_MAX (15*HZ) 76 77 /* file attributes which can be mapped to nfs attributes */ 78 #define NFS4_VALID_ATTRS (ATTR_MODE \ 79 | ATTR_UID \ 80 | ATTR_GID \ 81 | ATTR_SIZE \ 82 | ATTR_ATIME \ 83 | ATTR_MTIME \ 84 | ATTR_CTIME \ 85 | ATTR_ATIME_SET \ 86 | ATTR_MTIME_SET) 87 88 struct nfs4_opendata; 89 static int _nfs4_proc_open(struct nfs4_opendata *data); 90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data); 91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); 92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); 93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label); 94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label); 95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, 96 struct nfs_fattr *fattr, struct iattr *sattr, 97 struct nfs4_state *state, struct nfs4_label *ilabel, 98 struct nfs4_label *olabel); 99 #ifdef CONFIG_NFS_V4_1 100 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, 101 struct rpc_cred *); 102 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *, 103 struct rpc_cred *); 104 #endif 105 106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 107 static inline struct nfs4_label * 108 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 109 struct iattr *sattr, struct nfs4_label *label) 110 { 111 int err; 112 113 if (label == NULL) 114 return NULL; 115 116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) 117 return NULL; 118 119 err = security_dentry_init_security(dentry, sattr->ia_mode, 120 &dentry->d_name, (void **)&label->label, &label->len); 121 if (err == 0) 122 return label; 123 124 return NULL; 125 } 126 static inline void 127 nfs4_label_release_security(struct nfs4_label *label) 128 { 129 if (label) 130 security_release_secctx(label->label, label->len); 131 } 132 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 133 { 134 if (label) 135 return server->attr_bitmask; 136 137 return server->attr_bitmask_nl; 138 } 139 #else 140 static inline struct nfs4_label * 141 nfs4_label_init_security(struct inode *dir, struct dentry *dentry, 142 struct iattr *sattr, struct nfs4_label *l) 143 { return NULL; } 144 static inline void 145 nfs4_label_release_security(struct nfs4_label *label) 146 { return; } 147 static inline u32 * 148 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) 149 { return server->attr_bitmask; } 150 #endif 151 152 /* Prevent leaks of NFSv4 errors into userland */ 153 static int nfs4_map_errors(int err) 154 { 155 if (err >= -1000) 156 return err; 157 switch (err) { 158 case -NFS4ERR_RESOURCE: 159 case -NFS4ERR_LAYOUTTRYLATER: 160 case -NFS4ERR_RECALLCONFLICT: 161 return -EREMOTEIO; 162 case -NFS4ERR_WRONGSEC: 163 case -NFS4ERR_WRONG_CRED: 164 return -EPERM; 165 case -NFS4ERR_BADOWNER: 166 case -NFS4ERR_BADNAME: 167 return -EINVAL; 168 case -NFS4ERR_SHARE_DENIED: 169 return -EACCES; 170 case -NFS4ERR_MINOR_VERS_MISMATCH: 171 return -EPROTONOSUPPORT; 172 case -NFS4ERR_FILE_OPEN: 173 return -EBUSY; 174 default: 175 dprintk("%s could not handle NFSv4 error %d\n", 176 __func__, -err); 177 break; 178 } 179 return -EIO; 180 } 181 182 /* 183 * This is our standard bitmap for GETATTR requests. 184 */ 185 const u32 nfs4_fattr_bitmap[3] = { 186 FATTR4_WORD0_TYPE 187 | FATTR4_WORD0_CHANGE 188 | FATTR4_WORD0_SIZE 189 | FATTR4_WORD0_FSID 190 | FATTR4_WORD0_FILEID, 191 FATTR4_WORD1_MODE 192 | FATTR4_WORD1_NUMLINKS 193 | FATTR4_WORD1_OWNER 194 | FATTR4_WORD1_OWNER_GROUP 195 | FATTR4_WORD1_RAWDEV 196 | FATTR4_WORD1_SPACE_USED 197 | FATTR4_WORD1_TIME_ACCESS 198 | FATTR4_WORD1_TIME_METADATA 199 | FATTR4_WORD1_TIME_MODIFY 200 | FATTR4_WORD1_MOUNTED_ON_FILEID, 201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 202 FATTR4_WORD2_SECURITY_LABEL 203 #endif 204 }; 205 206 static const u32 nfs4_pnfs_open_bitmap[3] = { 207 FATTR4_WORD0_TYPE 208 | FATTR4_WORD0_CHANGE 209 | FATTR4_WORD0_SIZE 210 | FATTR4_WORD0_FSID 211 | FATTR4_WORD0_FILEID, 212 FATTR4_WORD1_MODE 213 | FATTR4_WORD1_NUMLINKS 214 | FATTR4_WORD1_OWNER 215 | FATTR4_WORD1_OWNER_GROUP 216 | FATTR4_WORD1_RAWDEV 217 | FATTR4_WORD1_SPACE_USED 218 | FATTR4_WORD1_TIME_ACCESS 219 | FATTR4_WORD1_TIME_METADATA 220 | FATTR4_WORD1_TIME_MODIFY, 221 FATTR4_WORD2_MDSTHRESHOLD 222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 223 | FATTR4_WORD2_SECURITY_LABEL 224 #endif 225 }; 226 227 static const u32 nfs4_open_noattr_bitmap[3] = { 228 FATTR4_WORD0_TYPE 229 | FATTR4_WORD0_CHANGE 230 | FATTR4_WORD0_FILEID, 231 }; 232 233 const u32 nfs4_statfs_bitmap[3] = { 234 FATTR4_WORD0_FILES_AVAIL 235 | FATTR4_WORD0_FILES_FREE 236 | FATTR4_WORD0_FILES_TOTAL, 237 FATTR4_WORD1_SPACE_AVAIL 238 | FATTR4_WORD1_SPACE_FREE 239 | FATTR4_WORD1_SPACE_TOTAL 240 }; 241 242 const u32 nfs4_pathconf_bitmap[3] = { 243 FATTR4_WORD0_MAXLINK 244 | FATTR4_WORD0_MAXNAME, 245 0 246 }; 247 248 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE 249 | FATTR4_WORD0_MAXREAD 250 | FATTR4_WORD0_MAXWRITE 251 | FATTR4_WORD0_LEASE_TIME, 252 FATTR4_WORD1_TIME_DELTA 253 | FATTR4_WORD1_FS_LAYOUT_TYPES, 254 FATTR4_WORD2_LAYOUT_BLKSIZE 255 | FATTR4_WORD2_CLONE_BLKSIZE 256 }; 257 258 const u32 nfs4_fs_locations_bitmap[3] = { 259 FATTR4_WORD0_TYPE 260 | FATTR4_WORD0_CHANGE 261 | FATTR4_WORD0_SIZE 262 | FATTR4_WORD0_FSID 263 | FATTR4_WORD0_FILEID 264 | FATTR4_WORD0_FS_LOCATIONS, 265 FATTR4_WORD1_MODE 266 | FATTR4_WORD1_NUMLINKS 267 | FATTR4_WORD1_OWNER 268 | FATTR4_WORD1_OWNER_GROUP 269 | FATTR4_WORD1_RAWDEV 270 | FATTR4_WORD1_SPACE_USED 271 | FATTR4_WORD1_TIME_ACCESS 272 | FATTR4_WORD1_TIME_METADATA 273 | FATTR4_WORD1_TIME_MODIFY 274 | FATTR4_WORD1_MOUNTED_ON_FILEID, 275 }; 276 277 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, 278 struct nfs4_readdir_arg *readdir) 279 { 280 __be32 *start, *p; 281 282 if (cookie > 2) { 283 readdir->cookie = cookie; 284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); 285 return; 286 } 287 288 readdir->cookie = 0; 289 memset(&readdir->verifier, 0, sizeof(readdir->verifier)); 290 if (cookie == 2) 291 return; 292 293 /* 294 * NFSv4 servers do not return entries for '.' and '..' 295 * Therefore, we fake these entries here. We let '.' 296 * have cookie 0 and '..' have cookie 1. Note that 297 * when talking to the server, we always send cookie 0 298 * instead of 1 or 2. 299 */ 300 start = p = kmap_atomic(*readdir->pages); 301 302 if (cookie == 0) { 303 *p++ = xdr_one; /* next */ 304 *p++ = xdr_zero; /* cookie, first word */ 305 *p++ = xdr_one; /* cookie, second word */ 306 *p++ = xdr_one; /* entry len */ 307 memcpy(p, ".\0\0\0", 4); /* entry */ 308 p++; 309 *p++ = xdr_one; /* bitmap length */ 310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ 311 *p++ = htonl(8); /* attribute buffer length */ 312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry))); 313 } 314 315 *p++ = xdr_one; /* next */ 316 *p++ = xdr_zero; /* cookie, first word */ 317 *p++ = xdr_two; /* cookie, second word */ 318 *p++ = xdr_two; /* entry len */ 319 memcpy(p, "..\0\0", 4); /* entry */ 320 p++; 321 *p++ = xdr_one; /* bitmap length */ 322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ 323 *p++ = htonl(8); /* attribute buffer length */ 324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent))); 325 326 readdir->pgbase = (char *)p - (char *)start; 327 readdir->count -= readdir->pgbase; 328 kunmap_atomic(start); 329 } 330 331 static long nfs4_update_delay(long *timeout) 332 { 333 long ret; 334 if (!timeout) 335 return NFS4_POLL_RETRY_MAX; 336 if (*timeout <= 0) 337 *timeout = NFS4_POLL_RETRY_MIN; 338 if (*timeout > NFS4_POLL_RETRY_MAX) 339 *timeout = NFS4_POLL_RETRY_MAX; 340 ret = *timeout; 341 *timeout <<= 1; 342 return ret; 343 } 344 345 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) 346 { 347 int res = 0; 348 349 might_sleep(); 350 351 freezable_schedule_timeout_killable_unsafe( 352 nfs4_update_delay(timeout)); 353 if (fatal_signal_pending(current)) 354 res = -ERESTARTSYS; 355 return res; 356 } 357 358 /* This is the error handling routine for processes that are allowed 359 * to sleep. 360 */ 361 static int nfs4_do_handle_exception(struct nfs_server *server, 362 int errorcode, struct nfs4_exception *exception) 363 { 364 struct nfs_client *clp = server->nfs_client; 365 struct nfs4_state *state = exception->state; 366 const nfs4_stateid *stateid = exception->stateid; 367 struct inode *inode = exception->inode; 368 int ret = errorcode; 369 370 exception->delay = 0; 371 exception->recovering = 0; 372 exception->retry = 0; 373 switch(errorcode) { 374 case 0: 375 return 0; 376 case -NFS4ERR_OPENMODE: 377 case -NFS4ERR_DELEG_REVOKED: 378 case -NFS4ERR_ADMIN_REVOKED: 379 case -NFS4ERR_BAD_STATEID: 380 if (inode) { 381 int err; 382 383 err = nfs_async_inode_return_delegation(inode, 384 stateid); 385 if (err == 0) 386 goto wait_on_recovery; 387 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) { 388 exception->retry = 1; 389 break; 390 } 391 } 392 if (state == NULL) 393 break; 394 ret = nfs4_schedule_stateid_recovery(server, state); 395 if (ret < 0) 396 break; 397 goto wait_on_recovery; 398 case -NFS4ERR_EXPIRED: 399 if (state != NULL) { 400 ret = nfs4_schedule_stateid_recovery(server, state); 401 if (ret < 0) 402 break; 403 } 404 case -NFS4ERR_STALE_STATEID: 405 case -NFS4ERR_STALE_CLIENTID: 406 nfs4_schedule_lease_recovery(clp); 407 goto wait_on_recovery; 408 case -NFS4ERR_MOVED: 409 ret = nfs4_schedule_migration_recovery(server); 410 if (ret < 0) 411 break; 412 goto wait_on_recovery; 413 case -NFS4ERR_LEASE_MOVED: 414 nfs4_schedule_lease_moved_recovery(clp); 415 goto wait_on_recovery; 416 #if defined(CONFIG_NFS_V4_1) 417 case -NFS4ERR_BADSESSION: 418 case -NFS4ERR_BADSLOT: 419 case -NFS4ERR_BAD_HIGH_SLOT: 420 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 421 case -NFS4ERR_DEADSESSION: 422 case -NFS4ERR_SEQ_FALSE_RETRY: 423 case -NFS4ERR_SEQ_MISORDERED: 424 dprintk("%s ERROR: %d Reset session\n", __func__, 425 errorcode); 426 nfs4_schedule_session_recovery(clp->cl_session, errorcode); 427 goto wait_on_recovery; 428 #endif /* defined(CONFIG_NFS_V4_1) */ 429 case -NFS4ERR_FILE_OPEN: 430 if (exception->timeout > HZ) { 431 /* We have retried a decent amount, time to 432 * fail 433 */ 434 ret = -EBUSY; 435 break; 436 } 437 case -NFS4ERR_DELAY: 438 nfs_inc_server_stats(server, NFSIOS_DELAY); 439 case -NFS4ERR_GRACE: 440 case -NFS4ERR_LAYOUTTRYLATER: 441 case -NFS4ERR_RECALLCONFLICT: 442 exception->delay = 1; 443 return 0; 444 445 case -NFS4ERR_RETRY_UNCACHED_REP: 446 case -NFS4ERR_OLD_STATEID: 447 exception->retry = 1; 448 break; 449 case -NFS4ERR_BADOWNER: 450 /* The following works around a Linux server bug! */ 451 case -NFS4ERR_BADNAME: 452 if (server->caps & NFS_CAP_UIDGID_NOMAP) { 453 server->caps &= ~NFS_CAP_UIDGID_NOMAP; 454 exception->retry = 1; 455 printk(KERN_WARNING "NFS: v4 server %s " 456 "does not accept raw " 457 "uid/gids. " 458 "Reenabling the idmapper.\n", 459 server->nfs_client->cl_hostname); 460 } 461 } 462 /* We failed to handle the error */ 463 return nfs4_map_errors(ret); 464 wait_on_recovery: 465 exception->recovering = 1; 466 return 0; 467 } 468 469 /* This is the error handling routine for processes that are allowed 470 * to sleep. 471 */ 472 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) 473 { 474 struct nfs_client *clp = server->nfs_client; 475 int ret; 476 477 ret = nfs4_do_handle_exception(server, errorcode, exception); 478 if (exception->delay) { 479 ret = nfs4_delay(server->client, &exception->timeout); 480 goto out_retry; 481 } 482 if (exception->recovering) { 483 ret = nfs4_wait_clnt_recover(clp); 484 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 485 return -EIO; 486 goto out_retry; 487 } 488 return ret; 489 out_retry: 490 if (ret == 0) 491 exception->retry = 1; 492 return ret; 493 } 494 495 static int 496 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server, 497 int errorcode, struct nfs4_exception *exception) 498 { 499 struct nfs_client *clp = server->nfs_client; 500 int ret; 501 502 ret = nfs4_do_handle_exception(server, errorcode, exception); 503 if (exception->delay) { 504 rpc_delay(task, nfs4_update_delay(&exception->timeout)); 505 goto out_retry; 506 } 507 if (exception->recovering) { 508 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); 509 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) 510 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); 511 goto out_retry; 512 } 513 if (test_bit(NFS_MIG_FAILED, &server->mig_status)) 514 ret = -EIO; 515 return ret; 516 out_retry: 517 if (ret == 0) 518 exception->retry = 1; 519 return ret; 520 } 521 522 static int 523 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server, 524 struct nfs4_state *state, long *timeout) 525 { 526 struct nfs4_exception exception = { 527 .state = state, 528 }; 529 530 if (task->tk_status >= 0) 531 return 0; 532 if (timeout) 533 exception.timeout = *timeout; 534 task->tk_status = nfs4_async_handle_exception(task, server, 535 task->tk_status, 536 &exception); 537 if (exception.delay && timeout) 538 *timeout = exception.timeout; 539 if (exception.retry) 540 return -EAGAIN; 541 return 0; 542 } 543 544 /* 545 * Return 'true' if 'clp' is using an rpc_client that is integrity protected 546 * or 'false' otherwise. 547 */ 548 static bool _nfs4_is_integrity_protected(struct nfs_client *clp) 549 { 550 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; 551 552 if (flavor == RPC_AUTH_GSS_KRB5I || 553 flavor == RPC_AUTH_GSS_KRB5P) 554 return true; 555 556 return false; 557 } 558 559 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) 560 { 561 spin_lock(&clp->cl_lock); 562 if (time_before(clp->cl_last_renewal,timestamp)) 563 clp->cl_last_renewal = timestamp; 564 spin_unlock(&clp->cl_lock); 565 } 566 567 static void renew_lease(const struct nfs_server *server, unsigned long timestamp) 568 { 569 struct nfs_client *clp = server->nfs_client; 570 571 if (!nfs4_has_session(clp)) 572 do_renew_lease(clp, timestamp); 573 } 574 575 struct nfs4_call_sync_data { 576 const struct nfs_server *seq_server; 577 struct nfs4_sequence_args *seq_args; 578 struct nfs4_sequence_res *seq_res; 579 }; 580 581 void nfs4_init_sequence(struct nfs4_sequence_args *args, 582 struct nfs4_sequence_res *res, int cache_reply) 583 { 584 args->sa_slot = NULL; 585 args->sa_cache_this = cache_reply; 586 args->sa_privileged = 0; 587 588 res->sr_slot = NULL; 589 } 590 591 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args) 592 { 593 args->sa_privileged = 1; 594 } 595 596 int nfs40_setup_sequence(struct nfs4_slot_table *tbl, 597 struct nfs4_sequence_args *args, 598 struct nfs4_sequence_res *res, 599 struct rpc_task *task) 600 { 601 struct nfs4_slot *slot; 602 603 /* slot already allocated? */ 604 if (res->sr_slot != NULL) 605 goto out_start; 606 607 spin_lock(&tbl->slot_tbl_lock); 608 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) 609 goto out_sleep; 610 611 slot = nfs4_alloc_slot(tbl); 612 if (IS_ERR(slot)) { 613 if (slot == ERR_PTR(-ENOMEM)) 614 task->tk_timeout = HZ >> 2; 615 goto out_sleep; 616 } 617 spin_unlock(&tbl->slot_tbl_lock); 618 619 slot->privileged = args->sa_privileged ? 1 : 0; 620 args->sa_slot = slot; 621 res->sr_slot = slot; 622 623 out_start: 624 rpc_call_start(task); 625 return 0; 626 627 out_sleep: 628 if (args->sa_privileged) 629 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, 630 NULL, RPC_PRIORITY_PRIVILEGED); 631 else 632 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); 633 spin_unlock(&tbl->slot_tbl_lock); 634 return -EAGAIN; 635 } 636 EXPORT_SYMBOL_GPL(nfs40_setup_sequence); 637 638 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res) 639 { 640 struct nfs4_slot *slot = res->sr_slot; 641 struct nfs4_slot_table *tbl; 642 643 tbl = slot->table; 644 spin_lock(&tbl->slot_tbl_lock); 645 if (!nfs41_wake_and_assign_slot(tbl, slot)) 646 nfs4_free_slot(tbl, slot); 647 spin_unlock(&tbl->slot_tbl_lock); 648 649 res->sr_slot = NULL; 650 } 651 652 static int nfs40_sequence_done(struct rpc_task *task, 653 struct nfs4_sequence_res *res) 654 { 655 if (res->sr_slot != NULL) 656 nfs40_sequence_free_slot(res); 657 return 1; 658 } 659 660 #if defined(CONFIG_NFS_V4_1) 661 662 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) 663 { 664 struct nfs4_session *session; 665 struct nfs4_slot_table *tbl; 666 struct nfs4_slot *slot = res->sr_slot; 667 bool send_new_highest_used_slotid = false; 668 669 tbl = slot->table; 670 session = tbl->session; 671 672 /* Bump the slot sequence number */ 673 if (slot->seq_done) 674 slot->seq_nr++; 675 slot->seq_done = 0; 676 677 spin_lock(&tbl->slot_tbl_lock); 678 /* Be nice to the server: try to ensure that the last transmitted 679 * value for highest_user_slotid <= target_highest_slotid 680 */ 681 if (tbl->highest_used_slotid > tbl->target_highest_slotid) 682 send_new_highest_used_slotid = true; 683 684 if (nfs41_wake_and_assign_slot(tbl, slot)) { 685 send_new_highest_used_slotid = false; 686 goto out_unlock; 687 } 688 nfs4_free_slot(tbl, slot); 689 690 if (tbl->highest_used_slotid != NFS4_NO_SLOT) 691 send_new_highest_used_slotid = false; 692 out_unlock: 693 spin_unlock(&tbl->slot_tbl_lock); 694 res->sr_slot = NULL; 695 if (send_new_highest_used_slotid) 696 nfs41_notify_server(session->clp); 697 if (waitqueue_active(&tbl->slot_waitq)) 698 wake_up_all(&tbl->slot_waitq); 699 } 700 701 static int nfs41_sequence_process(struct rpc_task *task, 702 struct nfs4_sequence_res *res) 703 { 704 struct nfs4_session *session; 705 struct nfs4_slot *slot = res->sr_slot; 706 struct nfs_client *clp; 707 bool interrupted = false; 708 int ret = 1; 709 710 if (slot == NULL) 711 goto out_noaction; 712 /* don't increment the sequence number if the task wasn't sent */ 713 if (!RPC_WAS_SENT(task)) 714 goto out; 715 716 session = slot->table->session; 717 718 if (slot->interrupted) { 719 slot->interrupted = 0; 720 interrupted = true; 721 } 722 723 trace_nfs4_sequence_done(session, res); 724 /* Check the SEQUENCE operation status */ 725 switch (res->sr_status) { 726 case 0: 727 /* Update the slot's sequence and clientid lease timer */ 728 slot->seq_done = 1; 729 clp = session->clp; 730 do_renew_lease(clp, res->sr_timestamp); 731 /* Check sequence flags */ 732 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags, 733 !!slot->privileged); 734 nfs41_update_target_slotid(slot->table, slot, res); 735 break; 736 case 1: 737 /* 738 * sr_status remains 1 if an RPC level error occurred. 739 * The server may or may not have processed the sequence 740 * operation.. 741 * Mark the slot as having hosted an interrupted RPC call. 742 */ 743 slot->interrupted = 1; 744 goto out; 745 case -NFS4ERR_DELAY: 746 /* The server detected a resend of the RPC call and 747 * returned NFS4ERR_DELAY as per Section 2.10.6.2 748 * of RFC5661. 749 */ 750 dprintk("%s: slot=%u seq=%u: Operation in progress\n", 751 __func__, 752 slot->slot_nr, 753 slot->seq_nr); 754 goto out_retry; 755 case -NFS4ERR_BADSLOT: 756 /* 757 * The slot id we used was probably retired. Try again 758 * using a different slot id. 759 */ 760 goto retry_nowait; 761 case -NFS4ERR_SEQ_MISORDERED: 762 /* 763 * Was the last operation on this sequence interrupted? 764 * If so, retry after bumping the sequence number. 765 */ 766 if (interrupted) { 767 ++slot->seq_nr; 768 goto retry_nowait; 769 } 770 /* 771 * Could this slot have been previously retired? 772 * If so, then the server may be expecting seq_nr = 1! 773 */ 774 if (slot->seq_nr != 1) { 775 slot->seq_nr = 1; 776 goto retry_nowait; 777 } 778 break; 779 case -NFS4ERR_SEQ_FALSE_RETRY: 780 ++slot->seq_nr; 781 goto retry_nowait; 782 default: 783 /* Just update the slot sequence no. */ 784 slot->seq_done = 1; 785 } 786 out: 787 /* The session may be reset by one of the error handlers. */ 788 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); 789 out_noaction: 790 return ret; 791 retry_nowait: 792 if (rpc_restart_call_prepare(task)) { 793 nfs41_sequence_free_slot(res); 794 task->tk_status = 0; 795 ret = 0; 796 } 797 goto out; 798 out_retry: 799 if (!rpc_restart_call(task)) 800 goto out; 801 rpc_delay(task, NFS4_POLL_RETRY_MAX); 802 return 0; 803 } 804 805 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 806 { 807 if (!nfs41_sequence_process(task, res)) 808 return 0; 809 if (res->sr_slot != NULL) 810 nfs41_sequence_free_slot(res); 811 return 1; 812 813 } 814 EXPORT_SYMBOL_GPL(nfs41_sequence_done); 815 816 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) 817 { 818 if (res->sr_slot == NULL) 819 return 1; 820 if (res->sr_slot->table->session != NULL) 821 return nfs41_sequence_process(task, res); 822 return nfs40_sequence_done(task, res); 823 } 824 825 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) 826 { 827 if (res->sr_slot != NULL) { 828 if (res->sr_slot->table->session != NULL) 829 nfs41_sequence_free_slot(res); 830 else 831 nfs40_sequence_free_slot(res); 832 } 833 } 834 835 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) 836 { 837 if (res->sr_slot == NULL) 838 return 1; 839 if (!res->sr_slot->table->session) 840 return nfs40_sequence_done(task, res); 841 return nfs41_sequence_done(task, res); 842 } 843 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 844 845 int nfs41_setup_sequence(struct nfs4_session *session, 846 struct nfs4_sequence_args *args, 847 struct nfs4_sequence_res *res, 848 struct rpc_task *task) 849 { 850 struct nfs4_slot *slot; 851 struct nfs4_slot_table *tbl; 852 853 dprintk("--> %s\n", __func__); 854 /* slot already allocated? */ 855 if (res->sr_slot != NULL) 856 goto out_success; 857 858 tbl = &session->fc_slot_table; 859 860 task->tk_timeout = 0; 861 862 spin_lock(&tbl->slot_tbl_lock); 863 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) && 864 !args->sa_privileged) { 865 /* The state manager will wait until the slot table is empty */ 866 dprintk("%s session is draining\n", __func__); 867 goto out_sleep; 868 } 869 870 slot = nfs4_alloc_slot(tbl); 871 if (IS_ERR(slot)) { 872 /* If out of memory, try again in 1/4 second */ 873 if (slot == ERR_PTR(-ENOMEM)) 874 task->tk_timeout = HZ >> 2; 875 dprintk("<-- %s: no free slots\n", __func__); 876 goto out_sleep; 877 } 878 spin_unlock(&tbl->slot_tbl_lock); 879 880 slot->privileged = args->sa_privileged ? 1 : 0; 881 args->sa_slot = slot; 882 883 dprintk("<-- %s slotid=%u seqid=%u\n", __func__, 884 slot->slot_nr, slot->seq_nr); 885 886 res->sr_slot = slot; 887 res->sr_timestamp = jiffies; 888 res->sr_status_flags = 0; 889 /* 890 * sr_status is only set in decode_sequence, and so will remain 891 * set to 1 if an rpc level failure occurs. 892 */ 893 res->sr_status = 1; 894 trace_nfs4_setup_sequence(session, args); 895 out_success: 896 rpc_call_start(task); 897 return 0; 898 out_sleep: 899 /* Privileged tasks are queued with top priority */ 900 if (args->sa_privileged) 901 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, 902 NULL, RPC_PRIORITY_PRIVILEGED); 903 else 904 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); 905 spin_unlock(&tbl->slot_tbl_lock); 906 return -EAGAIN; 907 } 908 EXPORT_SYMBOL_GPL(nfs41_setup_sequence); 909 910 static int nfs4_setup_sequence(const struct nfs_server *server, 911 struct nfs4_sequence_args *args, 912 struct nfs4_sequence_res *res, 913 struct rpc_task *task) 914 { 915 struct nfs4_session *session = nfs4_get_session(server); 916 int ret = 0; 917 918 if (!session) 919 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, 920 args, res, task); 921 922 dprintk("--> %s clp %p session %p sr_slot %u\n", 923 __func__, session->clp, session, res->sr_slot ? 924 res->sr_slot->slot_nr : NFS4_NO_SLOT); 925 926 ret = nfs41_setup_sequence(session, args, res, task); 927 928 dprintk("<-- %s status=%d\n", __func__, ret); 929 return ret; 930 } 931 932 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) 933 { 934 struct nfs4_call_sync_data *data = calldata; 935 struct nfs4_session *session = nfs4_get_session(data->seq_server); 936 937 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); 938 939 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task); 940 } 941 942 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) 943 { 944 struct nfs4_call_sync_data *data = calldata; 945 946 nfs41_sequence_done(task, data->seq_res); 947 } 948 949 static const struct rpc_call_ops nfs41_call_sync_ops = { 950 .rpc_call_prepare = nfs41_call_sync_prepare, 951 .rpc_call_done = nfs41_call_sync_done, 952 }; 953 954 #else /* !CONFIG_NFS_V4_1 */ 955 956 static int nfs4_setup_sequence(const struct nfs_server *server, 957 struct nfs4_sequence_args *args, 958 struct nfs4_sequence_res *res, 959 struct rpc_task *task) 960 { 961 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, 962 args, res, task); 963 } 964 965 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) 966 { 967 return nfs40_sequence_done(task, res); 968 } 969 970 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) 971 { 972 if (res->sr_slot != NULL) 973 nfs40_sequence_free_slot(res); 974 } 975 976 int nfs4_sequence_done(struct rpc_task *task, 977 struct nfs4_sequence_res *res) 978 { 979 return nfs40_sequence_done(task, res); 980 } 981 EXPORT_SYMBOL_GPL(nfs4_sequence_done); 982 983 #endif /* !CONFIG_NFS_V4_1 */ 984 985 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) 986 { 987 struct nfs4_call_sync_data *data = calldata; 988 nfs4_setup_sequence(data->seq_server, 989 data->seq_args, data->seq_res, task); 990 } 991 992 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) 993 { 994 struct nfs4_call_sync_data *data = calldata; 995 nfs4_sequence_done(task, data->seq_res); 996 } 997 998 static const struct rpc_call_ops nfs40_call_sync_ops = { 999 .rpc_call_prepare = nfs40_call_sync_prepare, 1000 .rpc_call_done = nfs40_call_sync_done, 1001 }; 1002 1003 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, 1004 struct nfs_server *server, 1005 struct rpc_message *msg, 1006 struct nfs4_sequence_args *args, 1007 struct nfs4_sequence_res *res) 1008 { 1009 int ret; 1010 struct rpc_task *task; 1011 struct nfs_client *clp = server->nfs_client; 1012 struct nfs4_call_sync_data data = { 1013 .seq_server = server, 1014 .seq_args = args, 1015 .seq_res = res, 1016 }; 1017 struct rpc_task_setup task_setup = { 1018 .rpc_client = clnt, 1019 .rpc_message = msg, 1020 .callback_ops = clp->cl_mvops->call_sync_ops, 1021 .callback_data = &data 1022 }; 1023 1024 task = rpc_run_task(&task_setup); 1025 if (IS_ERR(task)) 1026 ret = PTR_ERR(task); 1027 else { 1028 ret = task->tk_status; 1029 rpc_put_task(task); 1030 } 1031 return ret; 1032 } 1033 1034 int nfs4_call_sync(struct rpc_clnt *clnt, 1035 struct nfs_server *server, 1036 struct rpc_message *msg, 1037 struct nfs4_sequence_args *args, 1038 struct nfs4_sequence_res *res, 1039 int cache_reply) 1040 { 1041 nfs4_init_sequence(args, res, cache_reply); 1042 return nfs4_call_sync_sequence(clnt, server, msg, args, res); 1043 } 1044 1045 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) 1046 { 1047 struct nfs_inode *nfsi = NFS_I(dir); 1048 1049 spin_lock(&dir->i_lock); 1050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; 1051 if (!cinfo->atomic || cinfo->before != dir->i_version) 1052 nfs_force_lookup_revalidate(dir); 1053 dir->i_version = cinfo->after; 1054 nfsi->attr_gencount = nfs_inc_attr_generation_counter(); 1055 nfs_fscache_invalidate(dir); 1056 spin_unlock(&dir->i_lock); 1057 } 1058 1059 struct nfs4_opendata { 1060 struct kref kref; 1061 struct nfs_openargs o_arg; 1062 struct nfs_openres o_res; 1063 struct nfs_open_confirmargs c_arg; 1064 struct nfs_open_confirmres c_res; 1065 struct nfs4_string owner_name; 1066 struct nfs4_string group_name; 1067 struct nfs4_label *a_label; 1068 struct nfs_fattr f_attr; 1069 struct nfs4_label *f_label; 1070 struct dentry *dir; 1071 struct dentry *dentry; 1072 struct nfs4_state_owner *owner; 1073 struct nfs4_state *state; 1074 struct iattr attrs; 1075 unsigned long timestamp; 1076 unsigned int rpc_done : 1; 1077 unsigned int file_created : 1; 1078 unsigned int is_recover : 1; 1079 int rpc_status; 1080 int cancelled; 1081 }; 1082 1083 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, 1084 int err, struct nfs4_exception *exception) 1085 { 1086 if (err != -EINVAL) 1087 return false; 1088 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 1089 return false; 1090 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; 1091 exception->retry = 1; 1092 return true; 1093 } 1094 1095 static u32 1096 nfs4_map_atomic_open_share(struct nfs_server *server, 1097 fmode_t fmode, int openflags) 1098 { 1099 u32 res = 0; 1100 1101 switch (fmode & (FMODE_READ | FMODE_WRITE)) { 1102 case FMODE_READ: 1103 res = NFS4_SHARE_ACCESS_READ; 1104 break; 1105 case FMODE_WRITE: 1106 res = NFS4_SHARE_ACCESS_WRITE; 1107 break; 1108 case FMODE_READ|FMODE_WRITE: 1109 res = NFS4_SHARE_ACCESS_BOTH; 1110 } 1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) 1112 goto out; 1113 /* Want no delegation if we're using O_DIRECT */ 1114 if (openflags & O_DIRECT) 1115 res |= NFS4_SHARE_WANT_NO_DELEG; 1116 out: 1117 return res; 1118 } 1119 1120 static enum open_claim_type4 1121 nfs4_map_atomic_open_claim(struct nfs_server *server, 1122 enum open_claim_type4 claim) 1123 { 1124 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) 1125 return claim; 1126 switch (claim) { 1127 default: 1128 return claim; 1129 case NFS4_OPEN_CLAIM_FH: 1130 return NFS4_OPEN_CLAIM_NULL; 1131 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1132 return NFS4_OPEN_CLAIM_DELEGATE_CUR; 1133 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1134 return NFS4_OPEN_CLAIM_DELEGATE_PREV; 1135 } 1136 } 1137 1138 static void nfs4_init_opendata_res(struct nfs4_opendata *p) 1139 { 1140 p->o_res.f_attr = &p->f_attr; 1141 p->o_res.f_label = p->f_label; 1142 p->o_res.seqid = p->o_arg.seqid; 1143 p->c_res.seqid = p->c_arg.seqid; 1144 p->o_res.server = p->o_arg.server; 1145 p->o_res.access_request = p->o_arg.access; 1146 nfs_fattr_init(&p->f_attr); 1147 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); 1148 } 1149 1150 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, 1151 struct nfs4_state_owner *sp, fmode_t fmode, int flags, 1152 const struct iattr *attrs, 1153 struct nfs4_label *label, 1154 enum open_claim_type4 claim, 1155 gfp_t gfp_mask) 1156 { 1157 struct dentry *parent = dget_parent(dentry); 1158 struct inode *dir = d_inode(parent); 1159 struct nfs_server *server = NFS_SERVER(dir); 1160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 1161 struct nfs4_opendata *p; 1162 1163 p = kzalloc(sizeof(*p), gfp_mask); 1164 if (p == NULL) 1165 goto err; 1166 1167 p->f_label = nfs4_label_alloc(server, gfp_mask); 1168 if (IS_ERR(p->f_label)) 1169 goto err_free_p; 1170 1171 p->a_label = nfs4_label_alloc(server, gfp_mask); 1172 if (IS_ERR(p->a_label)) 1173 goto err_free_f; 1174 1175 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 1176 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask); 1177 if (IS_ERR(p->o_arg.seqid)) 1178 goto err_free_label; 1179 nfs_sb_active(dentry->d_sb); 1180 p->dentry = dget(dentry); 1181 p->dir = parent; 1182 p->owner = sp; 1183 atomic_inc(&sp->so_count); 1184 p->o_arg.open_flags = flags; 1185 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); 1186 p->o_arg.share_access = nfs4_map_atomic_open_share(server, 1187 fmode, flags); 1188 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS 1189 * will return permission denied for all bits until close */ 1190 if (!(flags & O_EXCL)) { 1191 /* ask server to check for all possible rights as results 1192 * are cached */ 1193 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY | 1194 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE; 1195 } 1196 p->o_arg.clientid = server->nfs_client->cl_clientid; 1197 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); 1198 p->o_arg.id.uniquifier = sp->so_seqid.owner_id; 1199 p->o_arg.name = &dentry->d_name; 1200 p->o_arg.server = server; 1201 p->o_arg.bitmask = nfs4_bitmask(server, label); 1202 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; 1203 p->o_arg.label = nfs4_label_copy(p->a_label, label); 1204 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); 1205 switch (p->o_arg.claim) { 1206 case NFS4_OPEN_CLAIM_NULL: 1207 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1208 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 1209 p->o_arg.fh = NFS_FH(dir); 1210 break; 1211 case NFS4_OPEN_CLAIM_PREVIOUS: 1212 case NFS4_OPEN_CLAIM_FH: 1213 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1214 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 1215 p->o_arg.fh = NFS_FH(d_inode(dentry)); 1216 } 1217 if (attrs != NULL && attrs->ia_valid != 0) { 1218 __u32 verf[2]; 1219 1220 p->o_arg.u.attrs = &p->attrs; 1221 memcpy(&p->attrs, attrs, sizeof(p->attrs)); 1222 1223 verf[0] = jiffies; 1224 verf[1] = current->pid; 1225 memcpy(p->o_arg.u.verifier.data, verf, 1226 sizeof(p->o_arg.u.verifier.data)); 1227 } 1228 p->c_arg.fh = &p->o_res.fh; 1229 p->c_arg.stateid = &p->o_res.stateid; 1230 p->c_arg.seqid = p->o_arg.seqid; 1231 nfs4_init_opendata_res(p); 1232 kref_init(&p->kref); 1233 return p; 1234 1235 err_free_label: 1236 nfs4_label_free(p->a_label); 1237 err_free_f: 1238 nfs4_label_free(p->f_label); 1239 err_free_p: 1240 kfree(p); 1241 err: 1242 dput(parent); 1243 return NULL; 1244 } 1245 1246 static void nfs4_opendata_free(struct kref *kref) 1247 { 1248 struct nfs4_opendata *p = container_of(kref, 1249 struct nfs4_opendata, kref); 1250 struct super_block *sb = p->dentry->d_sb; 1251 1252 nfs_free_seqid(p->o_arg.seqid); 1253 nfs4_sequence_free_slot(&p->o_res.seq_res); 1254 if (p->state != NULL) 1255 nfs4_put_open_state(p->state); 1256 nfs4_put_state_owner(p->owner); 1257 1258 nfs4_label_free(p->a_label); 1259 nfs4_label_free(p->f_label); 1260 1261 dput(p->dir); 1262 dput(p->dentry); 1263 nfs_sb_deactive(sb); 1264 nfs_fattr_free_names(&p->f_attr); 1265 kfree(p->f_attr.mdsthreshold); 1266 kfree(p); 1267 } 1268 1269 static void nfs4_opendata_put(struct nfs4_opendata *p) 1270 { 1271 if (p != NULL) 1272 kref_put(&p->kref, nfs4_opendata_free); 1273 } 1274 1275 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) 1276 { 1277 int ret; 1278 1279 ret = rpc_wait_for_completion_task(task); 1280 return ret; 1281 } 1282 1283 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state, 1284 fmode_t fmode) 1285 { 1286 switch(fmode & (FMODE_READ|FMODE_WRITE)) { 1287 case FMODE_READ|FMODE_WRITE: 1288 return state->n_rdwr != 0; 1289 case FMODE_WRITE: 1290 return state->n_wronly != 0; 1291 case FMODE_READ: 1292 return state->n_rdonly != 0; 1293 } 1294 WARN_ON_ONCE(1); 1295 return false; 1296 } 1297 1298 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode) 1299 { 1300 int ret = 0; 1301 1302 if (open_mode & (O_EXCL|O_TRUNC)) 1303 goto out; 1304 switch (mode & (FMODE_READ|FMODE_WRITE)) { 1305 case FMODE_READ: 1306 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 1307 && state->n_rdonly != 0; 1308 break; 1309 case FMODE_WRITE: 1310 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 1311 && state->n_wronly != 0; 1312 break; 1313 case FMODE_READ|FMODE_WRITE: 1314 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 1315 && state->n_rdwr != 0; 1316 } 1317 out: 1318 return ret; 1319 } 1320 1321 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode, 1322 enum open_claim_type4 claim) 1323 { 1324 if (delegation == NULL) 1325 return 0; 1326 if ((delegation->type & fmode) != fmode) 1327 return 0; 1328 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 1329 return 0; 1330 switch (claim) { 1331 case NFS4_OPEN_CLAIM_NULL: 1332 case NFS4_OPEN_CLAIM_FH: 1333 break; 1334 case NFS4_OPEN_CLAIM_PREVIOUS: 1335 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) 1336 break; 1337 default: 1338 return 0; 1339 } 1340 nfs_mark_delegation_referenced(delegation); 1341 return 1; 1342 } 1343 1344 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) 1345 { 1346 switch (fmode) { 1347 case FMODE_WRITE: 1348 state->n_wronly++; 1349 break; 1350 case FMODE_READ: 1351 state->n_rdonly++; 1352 break; 1353 case FMODE_READ|FMODE_WRITE: 1354 state->n_rdwr++; 1355 } 1356 nfs4_state_set_mode_locked(state, state->state | fmode); 1357 } 1358 1359 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) 1360 { 1361 struct nfs_client *clp = state->owner->so_server->nfs_client; 1362 bool need_recover = false; 1363 1364 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) 1365 need_recover = true; 1366 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) 1367 need_recover = true; 1368 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) 1369 need_recover = true; 1370 if (need_recover) 1371 nfs4_state_mark_reclaim_nograce(clp, state); 1372 } 1373 1374 static bool nfs_need_update_open_stateid(struct nfs4_state *state, 1375 nfs4_stateid *stateid) 1376 { 1377 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0) 1378 return true; 1379 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) { 1380 nfs_test_and_clear_all_open_stateid(state); 1381 return true; 1382 } 1383 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) 1384 return true; 1385 return false; 1386 } 1387 1388 static void nfs_resync_open_stateid_locked(struct nfs4_state *state) 1389 { 1390 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr)) 1391 return; 1392 if (state->n_wronly) 1393 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1394 if (state->n_rdonly) 1395 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1396 if (state->n_rdwr) 1397 set_bit(NFS_O_RDWR_STATE, &state->flags); 1398 set_bit(NFS_OPEN_STATE, &state->flags); 1399 } 1400 1401 static void nfs_clear_open_stateid_locked(struct nfs4_state *state, 1402 nfs4_stateid *arg_stateid, 1403 nfs4_stateid *stateid, fmode_t fmode) 1404 { 1405 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1406 switch (fmode & (FMODE_READ|FMODE_WRITE)) { 1407 case FMODE_WRITE: 1408 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1409 break; 1410 case FMODE_READ: 1411 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1412 break; 1413 case 0: 1414 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1415 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1416 clear_bit(NFS_OPEN_STATE, &state->flags); 1417 } 1418 if (stateid == NULL) 1419 return; 1420 /* Handle races with OPEN */ 1421 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) || 1422 (nfs4_stateid_match_other(stateid, &state->open_stateid) && 1423 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) { 1424 nfs_resync_open_stateid_locked(state); 1425 return; 1426 } 1427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1428 nfs4_stateid_copy(&state->stateid, stateid); 1429 nfs4_stateid_copy(&state->open_stateid, stateid); 1430 } 1431 1432 static void nfs_clear_open_stateid(struct nfs4_state *state, 1433 nfs4_stateid *arg_stateid, 1434 nfs4_stateid *stateid, fmode_t fmode) 1435 { 1436 write_seqlock(&state->seqlock); 1437 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode); 1438 write_sequnlock(&state->seqlock); 1439 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1440 nfs4_schedule_state_manager(state->owner->so_server->nfs_client); 1441 } 1442 1443 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) 1444 { 1445 switch (fmode) { 1446 case FMODE_READ: 1447 set_bit(NFS_O_RDONLY_STATE, &state->flags); 1448 break; 1449 case FMODE_WRITE: 1450 set_bit(NFS_O_WRONLY_STATE, &state->flags); 1451 break; 1452 case FMODE_READ|FMODE_WRITE: 1453 set_bit(NFS_O_RDWR_STATE, &state->flags); 1454 } 1455 if (!nfs_need_update_open_stateid(state, stateid)) 1456 return; 1457 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1458 nfs4_stateid_copy(&state->stateid, stateid); 1459 nfs4_stateid_copy(&state->open_stateid, stateid); 1460 } 1461 1462 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode) 1463 { 1464 /* 1465 * Protect the call to nfs4_state_set_mode_locked and 1466 * serialise the stateid update 1467 */ 1468 spin_lock(&state->owner->so_lock); 1469 write_seqlock(&state->seqlock); 1470 if (deleg_stateid != NULL) { 1471 nfs4_stateid_copy(&state->stateid, deleg_stateid); 1472 set_bit(NFS_DELEGATED_STATE, &state->flags); 1473 } 1474 if (open_stateid != NULL) 1475 nfs_set_open_stateid_locked(state, open_stateid, fmode); 1476 write_sequnlock(&state->seqlock); 1477 update_open_stateflags(state, fmode); 1478 spin_unlock(&state->owner->so_lock); 1479 } 1480 1481 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode) 1482 { 1483 struct nfs_inode *nfsi = NFS_I(state->inode); 1484 struct nfs_delegation *deleg_cur; 1485 int ret = 0; 1486 1487 fmode &= (FMODE_READ|FMODE_WRITE); 1488 1489 rcu_read_lock(); 1490 deleg_cur = rcu_dereference(nfsi->delegation); 1491 if (deleg_cur == NULL) 1492 goto no_delegation; 1493 1494 spin_lock(&deleg_cur->lock); 1495 if (rcu_dereference(nfsi->delegation) != deleg_cur || 1496 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || 1497 (deleg_cur->type & fmode) != fmode) 1498 goto no_delegation_unlock; 1499 1500 if (delegation == NULL) 1501 delegation = &deleg_cur->stateid; 1502 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation)) 1503 goto no_delegation_unlock; 1504 1505 nfs_mark_delegation_referenced(deleg_cur); 1506 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode); 1507 ret = 1; 1508 no_delegation_unlock: 1509 spin_unlock(&deleg_cur->lock); 1510 no_delegation: 1511 rcu_read_unlock(); 1512 1513 if (!ret && open_stateid != NULL) { 1514 __update_open_stateid(state, open_stateid, NULL, fmode); 1515 ret = 1; 1516 } 1517 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) 1518 nfs4_schedule_state_manager(state->owner->so_server->nfs_client); 1519 1520 return ret; 1521 } 1522 1523 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp, 1524 const nfs4_stateid *stateid) 1525 { 1526 struct nfs4_state *state = lsp->ls_state; 1527 bool ret = false; 1528 1529 spin_lock(&state->state_lock); 1530 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid)) 1531 goto out_noupdate; 1532 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid)) 1533 goto out_noupdate; 1534 nfs4_stateid_copy(&lsp->ls_stateid, stateid); 1535 ret = true; 1536 out_noupdate: 1537 spin_unlock(&state->state_lock); 1538 return ret; 1539 } 1540 1541 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) 1542 { 1543 struct nfs_delegation *delegation; 1544 1545 rcu_read_lock(); 1546 delegation = rcu_dereference(NFS_I(inode)->delegation); 1547 if (delegation == NULL || (delegation->type & fmode) == fmode) { 1548 rcu_read_unlock(); 1549 return; 1550 } 1551 rcu_read_unlock(); 1552 nfs4_inode_return_delegation(inode); 1553 } 1554 1555 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) 1556 { 1557 struct nfs4_state *state = opendata->state; 1558 struct nfs_inode *nfsi = NFS_I(state->inode); 1559 struct nfs_delegation *delegation; 1560 int open_mode = opendata->o_arg.open_flags; 1561 fmode_t fmode = opendata->o_arg.fmode; 1562 enum open_claim_type4 claim = opendata->o_arg.claim; 1563 nfs4_stateid stateid; 1564 int ret = -EAGAIN; 1565 1566 for (;;) { 1567 spin_lock(&state->owner->so_lock); 1568 if (can_open_cached(state, fmode, open_mode)) { 1569 update_open_stateflags(state, fmode); 1570 spin_unlock(&state->owner->so_lock); 1571 goto out_return_state; 1572 } 1573 spin_unlock(&state->owner->so_lock); 1574 rcu_read_lock(); 1575 delegation = rcu_dereference(nfsi->delegation); 1576 if (!can_open_delegated(delegation, fmode, claim)) { 1577 rcu_read_unlock(); 1578 break; 1579 } 1580 /* Save the delegation */ 1581 nfs4_stateid_copy(&stateid, &delegation->stateid); 1582 rcu_read_unlock(); 1583 nfs_release_seqid(opendata->o_arg.seqid); 1584 if (!opendata->is_recover) { 1585 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); 1586 if (ret != 0) 1587 goto out; 1588 } 1589 ret = -EAGAIN; 1590 1591 /* Try to update the stateid using the delegation */ 1592 if (update_open_stateid(state, NULL, &stateid, fmode)) 1593 goto out_return_state; 1594 } 1595 out: 1596 return ERR_PTR(ret); 1597 out_return_state: 1598 atomic_inc(&state->count); 1599 return state; 1600 } 1601 1602 static void 1603 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) 1604 { 1605 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; 1606 struct nfs_delegation *delegation; 1607 int delegation_flags = 0; 1608 1609 rcu_read_lock(); 1610 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 1611 if (delegation) 1612 delegation_flags = delegation->flags; 1613 rcu_read_unlock(); 1614 switch (data->o_arg.claim) { 1615 default: 1616 break; 1617 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 1618 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 1619 pr_err_ratelimited("NFS: Broken NFSv4 server %s is " 1620 "returning a delegation for " 1621 "OPEN(CLAIM_DELEGATE_CUR)\n", 1622 clp->cl_hostname); 1623 return; 1624 } 1625 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) 1626 nfs_inode_set_delegation(state->inode, 1627 data->owner->so_cred, 1628 &data->o_res); 1629 else 1630 nfs_inode_reclaim_delegation(state->inode, 1631 data->owner->so_cred, 1632 &data->o_res); 1633 } 1634 1635 /* 1636 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes 1637 * and update the nfs4_state. 1638 */ 1639 static struct nfs4_state * 1640 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) 1641 { 1642 struct inode *inode = data->state->inode; 1643 struct nfs4_state *state = data->state; 1644 int ret; 1645 1646 if (!data->rpc_done) { 1647 if (data->rpc_status) { 1648 ret = data->rpc_status; 1649 goto err; 1650 } 1651 /* cached opens have already been processed */ 1652 goto update; 1653 } 1654 1655 ret = nfs_refresh_inode(inode, &data->f_attr); 1656 if (ret) 1657 goto err; 1658 1659 if (data->o_res.delegation_type != 0) 1660 nfs4_opendata_check_deleg(data, state); 1661 update: 1662 update_open_stateid(state, &data->o_res.stateid, NULL, 1663 data->o_arg.fmode); 1664 atomic_inc(&state->count); 1665 1666 return state; 1667 err: 1668 return ERR_PTR(ret); 1669 1670 } 1671 1672 static struct nfs4_state * 1673 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 1674 { 1675 struct inode *inode; 1676 struct nfs4_state *state = NULL; 1677 int ret; 1678 1679 if (!data->rpc_done) { 1680 state = nfs4_try_open_cached(data); 1681 trace_nfs4_cached_open(data->state); 1682 goto out; 1683 } 1684 1685 ret = -EAGAIN; 1686 if (!(data->f_attr.valid & NFS_ATTR_FATTR)) 1687 goto err; 1688 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label); 1689 ret = PTR_ERR(inode); 1690 if (IS_ERR(inode)) 1691 goto err; 1692 ret = -ENOMEM; 1693 state = nfs4_get_open_state(inode, data->owner); 1694 if (state == NULL) 1695 goto err_put_inode; 1696 if (data->o_res.delegation_type != 0) 1697 nfs4_opendata_check_deleg(data, state); 1698 update_open_stateid(state, &data->o_res.stateid, NULL, 1699 data->o_arg.fmode); 1700 iput(inode); 1701 out: 1702 nfs_release_seqid(data->o_arg.seqid); 1703 return state; 1704 err_put_inode: 1705 iput(inode); 1706 err: 1707 return ERR_PTR(ret); 1708 } 1709 1710 static struct nfs4_state * 1711 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) 1712 { 1713 struct nfs4_state *ret; 1714 1715 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) 1716 ret =_nfs4_opendata_reclaim_to_nfs4_state(data); 1717 else 1718 ret = _nfs4_opendata_to_nfs4_state(data); 1719 nfs4_sequence_free_slot(&data->o_res.seq_res); 1720 return ret; 1721 } 1722 1723 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) 1724 { 1725 struct nfs_inode *nfsi = NFS_I(state->inode); 1726 struct nfs_open_context *ctx; 1727 1728 spin_lock(&state->inode->i_lock); 1729 list_for_each_entry(ctx, &nfsi->open_files, list) { 1730 if (ctx->state != state) 1731 continue; 1732 get_nfs_open_context(ctx); 1733 spin_unlock(&state->inode->i_lock); 1734 return ctx; 1735 } 1736 spin_unlock(&state->inode->i_lock); 1737 return ERR_PTR(-ENOENT); 1738 } 1739 1740 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, 1741 struct nfs4_state *state, enum open_claim_type4 claim) 1742 { 1743 struct nfs4_opendata *opendata; 1744 1745 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, 1746 NULL, NULL, claim, GFP_NOFS); 1747 if (opendata == NULL) 1748 return ERR_PTR(-ENOMEM); 1749 opendata->state = state; 1750 atomic_inc(&state->count); 1751 return opendata; 1752 } 1753 1754 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, 1755 fmode_t fmode) 1756 { 1757 struct nfs4_state *newstate; 1758 int ret; 1759 1760 if (!nfs4_mode_match_open_stateid(opendata->state, fmode)) 1761 return 0; 1762 opendata->o_arg.open_flags = 0; 1763 opendata->o_arg.fmode = fmode; 1764 opendata->o_arg.share_access = nfs4_map_atomic_open_share( 1765 NFS_SB(opendata->dentry->d_sb), 1766 fmode, 0); 1767 memset(&opendata->o_res, 0, sizeof(opendata->o_res)); 1768 memset(&opendata->c_res, 0, sizeof(opendata->c_res)); 1769 nfs4_init_opendata_res(opendata); 1770 ret = _nfs4_recover_proc_open(opendata); 1771 if (ret != 0) 1772 return ret; 1773 newstate = nfs4_opendata_to_nfs4_state(opendata); 1774 if (IS_ERR(newstate)) 1775 return PTR_ERR(newstate); 1776 if (newstate != opendata->state) 1777 ret = -ESTALE; 1778 nfs4_close_state(newstate, fmode); 1779 return ret; 1780 } 1781 1782 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) 1783 { 1784 int ret; 1785 1786 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */ 1787 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1788 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1789 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1790 /* memory barrier prior to reading state->n_* */ 1791 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1792 clear_bit(NFS_OPEN_STATE, &state->flags); 1793 smp_rmb(); 1794 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 1795 if (ret != 0) 1796 return ret; 1797 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE); 1798 if (ret != 0) 1799 return ret; 1800 ret = nfs4_open_recover_helper(opendata, FMODE_READ); 1801 if (ret != 0) 1802 return ret; 1803 /* 1804 * We may have performed cached opens for all three recoveries. 1805 * Check if we need to update the current stateid. 1806 */ 1807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && 1808 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { 1809 write_seqlock(&state->seqlock); 1810 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) 1811 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 1812 write_sequnlock(&state->seqlock); 1813 } 1814 return 0; 1815 } 1816 1817 /* 1818 * OPEN_RECLAIM: 1819 * reclaim state on the server after a reboot. 1820 */ 1821 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 1822 { 1823 struct nfs_delegation *delegation; 1824 struct nfs4_opendata *opendata; 1825 fmode_t delegation_type = 0; 1826 int status; 1827 1828 opendata = nfs4_open_recoverdata_alloc(ctx, state, 1829 NFS4_OPEN_CLAIM_PREVIOUS); 1830 if (IS_ERR(opendata)) 1831 return PTR_ERR(opendata); 1832 rcu_read_lock(); 1833 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 1834 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) 1835 delegation_type = delegation->type; 1836 rcu_read_unlock(); 1837 opendata->o_arg.u.delegation_type = delegation_type; 1838 status = nfs4_open_recover(opendata, state); 1839 nfs4_opendata_put(opendata); 1840 return status; 1841 } 1842 1843 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) 1844 { 1845 struct nfs_server *server = NFS_SERVER(state->inode); 1846 struct nfs4_exception exception = { }; 1847 int err; 1848 do { 1849 err = _nfs4_do_open_reclaim(ctx, state); 1850 trace_nfs4_open_reclaim(ctx, 0, err); 1851 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 1852 continue; 1853 if (err != -NFS4ERR_DELAY) 1854 break; 1855 nfs4_handle_exception(server, err, &exception); 1856 } while (exception.retry); 1857 return err; 1858 } 1859 1860 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) 1861 { 1862 struct nfs_open_context *ctx; 1863 int ret; 1864 1865 ctx = nfs4_state_find_open_context(state); 1866 if (IS_ERR(ctx)) 1867 return -EAGAIN; 1868 ret = nfs4_do_open_reclaim(ctx, state); 1869 put_nfs_open_context(ctx); 1870 return ret; 1871 } 1872 1873 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err) 1874 { 1875 switch (err) { 1876 default: 1877 printk(KERN_ERR "NFS: %s: unhandled error " 1878 "%d.\n", __func__, err); 1879 case 0: 1880 case -ENOENT: 1881 case -EAGAIN: 1882 case -ESTALE: 1883 break; 1884 case -NFS4ERR_BADSESSION: 1885 case -NFS4ERR_BADSLOT: 1886 case -NFS4ERR_BAD_HIGH_SLOT: 1887 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1888 case -NFS4ERR_DEADSESSION: 1889 set_bit(NFS_DELEGATED_STATE, &state->flags); 1890 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err); 1891 return -EAGAIN; 1892 case -NFS4ERR_STALE_CLIENTID: 1893 case -NFS4ERR_STALE_STATEID: 1894 set_bit(NFS_DELEGATED_STATE, &state->flags); 1895 case -NFS4ERR_EXPIRED: 1896 /* Don't recall a delegation if it was lost */ 1897 nfs4_schedule_lease_recovery(server->nfs_client); 1898 return -EAGAIN; 1899 case -NFS4ERR_MOVED: 1900 nfs4_schedule_migration_recovery(server); 1901 return -EAGAIN; 1902 case -NFS4ERR_LEASE_MOVED: 1903 nfs4_schedule_lease_moved_recovery(server->nfs_client); 1904 return -EAGAIN; 1905 case -NFS4ERR_DELEG_REVOKED: 1906 case -NFS4ERR_ADMIN_REVOKED: 1907 case -NFS4ERR_BAD_STATEID: 1908 case -NFS4ERR_OPENMODE: 1909 nfs_inode_find_state_and_recover(state->inode, 1910 stateid); 1911 nfs4_schedule_stateid_recovery(server, state); 1912 return -EAGAIN; 1913 case -NFS4ERR_DELAY: 1914 case -NFS4ERR_GRACE: 1915 set_bit(NFS_DELEGATED_STATE, &state->flags); 1916 ssleep(1); 1917 return -EAGAIN; 1918 case -ENOMEM: 1919 case -NFS4ERR_DENIED: 1920 /* kill_proc(fl->fl_pid, SIGLOST, 1); */ 1921 return 0; 1922 } 1923 return err; 1924 } 1925 1926 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, 1927 struct nfs4_state *state, const nfs4_stateid *stateid, 1928 fmode_t type) 1929 { 1930 struct nfs_server *server = NFS_SERVER(state->inode); 1931 struct nfs4_opendata *opendata; 1932 int err = 0; 1933 1934 opendata = nfs4_open_recoverdata_alloc(ctx, state, 1935 NFS4_OPEN_CLAIM_DELEG_CUR_FH); 1936 if (IS_ERR(opendata)) 1937 return PTR_ERR(opendata); 1938 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); 1939 write_seqlock(&state->seqlock); 1940 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 1941 write_sequnlock(&state->seqlock); 1942 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1943 switch (type & (FMODE_READ|FMODE_WRITE)) { 1944 case FMODE_READ|FMODE_WRITE: 1945 case FMODE_WRITE: 1946 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); 1947 if (err) 1948 break; 1949 err = nfs4_open_recover_helper(opendata, FMODE_WRITE); 1950 if (err) 1951 break; 1952 case FMODE_READ: 1953 err = nfs4_open_recover_helper(opendata, FMODE_READ); 1954 } 1955 nfs4_opendata_put(opendata); 1956 return nfs4_handle_delegation_recall_error(server, state, stateid, err); 1957 } 1958 1959 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) 1960 { 1961 struct nfs4_opendata *data = calldata; 1962 1963 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl, 1964 &data->c_arg.seq_args, &data->c_res.seq_res, task); 1965 } 1966 1967 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) 1968 { 1969 struct nfs4_opendata *data = calldata; 1970 1971 nfs40_sequence_done(task, &data->c_res.seq_res); 1972 1973 data->rpc_status = task->tk_status; 1974 if (data->rpc_status == 0) { 1975 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); 1976 nfs_confirm_seqid(&data->owner->so_seqid, 0); 1977 renew_lease(data->o_res.server, data->timestamp); 1978 data->rpc_done = 1; 1979 } 1980 } 1981 1982 static void nfs4_open_confirm_release(void *calldata) 1983 { 1984 struct nfs4_opendata *data = calldata; 1985 struct nfs4_state *state = NULL; 1986 1987 /* If this request hasn't been cancelled, do nothing */ 1988 if (data->cancelled == 0) 1989 goto out_free; 1990 /* In case of error, no cleanup! */ 1991 if (!data->rpc_done) 1992 goto out_free; 1993 state = nfs4_opendata_to_nfs4_state(data); 1994 if (!IS_ERR(state)) 1995 nfs4_close_state(state, data->o_arg.fmode); 1996 out_free: 1997 nfs4_opendata_put(data); 1998 } 1999 2000 static const struct rpc_call_ops nfs4_open_confirm_ops = { 2001 .rpc_call_prepare = nfs4_open_confirm_prepare, 2002 .rpc_call_done = nfs4_open_confirm_done, 2003 .rpc_release = nfs4_open_confirm_release, 2004 }; 2005 2006 /* 2007 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata 2008 */ 2009 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) 2010 { 2011 struct nfs_server *server = NFS_SERVER(d_inode(data->dir)); 2012 struct rpc_task *task; 2013 struct rpc_message msg = { 2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], 2015 .rpc_argp = &data->c_arg, 2016 .rpc_resp = &data->c_res, 2017 .rpc_cred = data->owner->so_cred, 2018 }; 2019 struct rpc_task_setup task_setup_data = { 2020 .rpc_client = server->client, 2021 .rpc_message = &msg, 2022 .callback_ops = &nfs4_open_confirm_ops, 2023 .callback_data = data, 2024 .workqueue = nfsiod_workqueue, 2025 .flags = RPC_TASK_ASYNC, 2026 }; 2027 int status; 2028 2029 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1); 2030 kref_get(&data->kref); 2031 data->rpc_done = 0; 2032 data->rpc_status = 0; 2033 data->timestamp = jiffies; 2034 if (data->is_recover) 2035 nfs4_set_sequence_privileged(&data->c_arg.seq_args); 2036 task = rpc_run_task(&task_setup_data); 2037 if (IS_ERR(task)) 2038 return PTR_ERR(task); 2039 status = nfs4_wait_for_completion_rpc_task(task); 2040 if (status != 0) { 2041 data->cancelled = 1; 2042 smp_wmb(); 2043 } else 2044 status = data->rpc_status; 2045 rpc_put_task(task); 2046 return status; 2047 } 2048 2049 static void nfs4_open_prepare(struct rpc_task *task, void *calldata) 2050 { 2051 struct nfs4_opendata *data = calldata; 2052 struct nfs4_state_owner *sp = data->owner; 2053 struct nfs_client *clp = sp->so_server->nfs_client; 2054 enum open_claim_type4 claim = data->o_arg.claim; 2055 2056 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) 2057 goto out_wait; 2058 /* 2059 * Check if we still need to send an OPEN call, or if we can use 2060 * a delegation instead. 2061 */ 2062 if (data->state != NULL) { 2063 struct nfs_delegation *delegation; 2064 2065 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags)) 2066 goto out_no_action; 2067 rcu_read_lock(); 2068 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation); 2069 if (can_open_delegated(delegation, data->o_arg.fmode, claim)) 2070 goto unlock_no_action; 2071 rcu_read_unlock(); 2072 } 2073 /* Update client id. */ 2074 data->o_arg.clientid = clp->cl_clientid; 2075 switch (claim) { 2076 default: 2077 break; 2078 case NFS4_OPEN_CLAIM_PREVIOUS: 2079 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 2080 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 2081 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; 2082 case NFS4_OPEN_CLAIM_FH: 2083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; 2084 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh); 2085 } 2086 data->timestamp = jiffies; 2087 if (nfs4_setup_sequence(data->o_arg.server, 2088 &data->o_arg.seq_args, 2089 &data->o_res.seq_res, 2090 task) != 0) 2091 nfs_release_seqid(data->o_arg.seqid); 2092 2093 /* Set the create mode (note dependency on the session type) */ 2094 data->o_arg.createmode = NFS4_CREATE_UNCHECKED; 2095 if (data->o_arg.open_flags & O_EXCL) { 2096 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; 2097 if (nfs4_has_persistent_session(clp)) 2098 data->o_arg.createmode = NFS4_CREATE_GUARDED; 2099 else if (clp->cl_mvops->minor_version > 0) 2100 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; 2101 } 2102 return; 2103 unlock_no_action: 2104 trace_nfs4_cached_open(data->state); 2105 rcu_read_unlock(); 2106 out_no_action: 2107 task->tk_action = NULL; 2108 out_wait: 2109 nfs4_sequence_done(task, &data->o_res.seq_res); 2110 } 2111 2112 static void nfs4_open_done(struct rpc_task *task, void *calldata) 2113 { 2114 struct nfs4_opendata *data = calldata; 2115 2116 data->rpc_status = task->tk_status; 2117 2118 if (!nfs4_sequence_process(task, &data->o_res.seq_res)) 2119 return; 2120 2121 if (task->tk_status == 0) { 2122 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { 2123 switch (data->o_res.f_attr->mode & S_IFMT) { 2124 case S_IFREG: 2125 break; 2126 case S_IFLNK: 2127 data->rpc_status = -ELOOP; 2128 break; 2129 case S_IFDIR: 2130 data->rpc_status = -EISDIR; 2131 break; 2132 default: 2133 data->rpc_status = -ENOTDIR; 2134 } 2135 } 2136 renew_lease(data->o_res.server, data->timestamp); 2137 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) 2138 nfs_confirm_seqid(&data->owner->so_seqid, 0); 2139 } 2140 data->rpc_done = 1; 2141 } 2142 2143 static void nfs4_open_release(void *calldata) 2144 { 2145 struct nfs4_opendata *data = calldata; 2146 struct nfs4_state *state = NULL; 2147 2148 /* If this request hasn't been cancelled, do nothing */ 2149 if (data->cancelled == 0) 2150 goto out_free; 2151 /* In case of error, no cleanup! */ 2152 if (data->rpc_status != 0 || !data->rpc_done) 2153 goto out_free; 2154 /* In case we need an open_confirm, no cleanup! */ 2155 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) 2156 goto out_free; 2157 state = nfs4_opendata_to_nfs4_state(data); 2158 if (!IS_ERR(state)) 2159 nfs4_close_state(state, data->o_arg.fmode); 2160 out_free: 2161 nfs4_opendata_put(data); 2162 } 2163 2164 static const struct rpc_call_ops nfs4_open_ops = { 2165 .rpc_call_prepare = nfs4_open_prepare, 2166 .rpc_call_done = nfs4_open_done, 2167 .rpc_release = nfs4_open_release, 2168 }; 2169 2170 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover) 2171 { 2172 struct inode *dir = d_inode(data->dir); 2173 struct nfs_server *server = NFS_SERVER(dir); 2174 struct nfs_openargs *o_arg = &data->o_arg; 2175 struct nfs_openres *o_res = &data->o_res; 2176 struct rpc_task *task; 2177 struct rpc_message msg = { 2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], 2179 .rpc_argp = o_arg, 2180 .rpc_resp = o_res, 2181 .rpc_cred = data->owner->so_cred, 2182 }; 2183 struct rpc_task_setup task_setup_data = { 2184 .rpc_client = server->client, 2185 .rpc_message = &msg, 2186 .callback_ops = &nfs4_open_ops, 2187 .callback_data = data, 2188 .workqueue = nfsiod_workqueue, 2189 .flags = RPC_TASK_ASYNC, 2190 }; 2191 int status; 2192 2193 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1); 2194 kref_get(&data->kref); 2195 data->rpc_done = 0; 2196 data->rpc_status = 0; 2197 data->cancelled = 0; 2198 data->is_recover = 0; 2199 if (isrecover) { 2200 nfs4_set_sequence_privileged(&o_arg->seq_args); 2201 data->is_recover = 1; 2202 } 2203 task = rpc_run_task(&task_setup_data); 2204 if (IS_ERR(task)) 2205 return PTR_ERR(task); 2206 status = nfs4_wait_for_completion_rpc_task(task); 2207 if (status != 0) { 2208 data->cancelled = 1; 2209 smp_wmb(); 2210 } else 2211 status = data->rpc_status; 2212 rpc_put_task(task); 2213 2214 return status; 2215 } 2216 2217 static int _nfs4_recover_proc_open(struct nfs4_opendata *data) 2218 { 2219 struct inode *dir = d_inode(data->dir); 2220 struct nfs_openres *o_res = &data->o_res; 2221 int status; 2222 2223 status = nfs4_run_open_task(data, 1); 2224 if (status != 0 || !data->rpc_done) 2225 return status; 2226 2227 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); 2228 2229 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 2230 status = _nfs4_proc_open_confirm(data); 2231 if (status != 0) 2232 return status; 2233 } 2234 2235 return status; 2236 } 2237 2238 /* 2239 * Additional permission checks in order to distinguish between an 2240 * open for read, and an open for execute. This works around the 2241 * fact that NFSv4 OPEN treats read and execute permissions as being 2242 * the same. 2243 * Note that in the non-execute case, we want to turn off permission 2244 * checking if we just created a new file (POSIX open() semantics). 2245 */ 2246 static int nfs4_opendata_access(struct rpc_cred *cred, 2247 struct nfs4_opendata *opendata, 2248 struct nfs4_state *state, fmode_t fmode, 2249 int openflags) 2250 { 2251 struct nfs_access_entry cache; 2252 u32 mask; 2253 2254 /* access call failed or for some reason the server doesn't 2255 * support any access modes -- defer access call until later */ 2256 if (opendata->o_res.access_supported == 0) 2257 return 0; 2258 2259 mask = 0; 2260 /* 2261 * Use openflags to check for exec, because fmode won't 2262 * always have FMODE_EXEC set when file open for exec. 2263 */ 2264 if (openflags & __FMODE_EXEC) { 2265 /* ONLY check for exec rights */ 2266 mask = MAY_EXEC; 2267 } else if ((fmode & FMODE_READ) && !opendata->file_created) 2268 mask = MAY_READ; 2269 2270 cache.cred = cred; 2271 cache.jiffies = jiffies; 2272 nfs_access_set_mask(&cache, opendata->o_res.access_result); 2273 nfs_access_add_cache(state->inode, &cache); 2274 2275 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0) 2276 return 0; 2277 2278 /* even though OPEN succeeded, access is denied. Close the file */ 2279 nfs4_close_state(state, fmode); 2280 return -EACCES; 2281 } 2282 2283 /* 2284 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata 2285 */ 2286 static int _nfs4_proc_open(struct nfs4_opendata *data) 2287 { 2288 struct inode *dir = d_inode(data->dir); 2289 struct nfs_server *server = NFS_SERVER(dir); 2290 struct nfs_openargs *o_arg = &data->o_arg; 2291 struct nfs_openres *o_res = &data->o_res; 2292 int status; 2293 2294 status = nfs4_run_open_task(data, 0); 2295 if (!data->rpc_done) 2296 return status; 2297 if (status != 0) { 2298 if (status == -NFS4ERR_BADNAME && 2299 !(o_arg->open_flags & O_CREAT)) 2300 return -ENOENT; 2301 return status; 2302 } 2303 2304 nfs_fattr_map_and_free_names(server, &data->f_attr); 2305 2306 if (o_arg->open_flags & O_CREAT) { 2307 update_changeattr(dir, &o_res->cinfo); 2308 if (o_arg->open_flags & O_EXCL) 2309 data->file_created = 1; 2310 else if (o_res->cinfo.before != o_res->cinfo.after) 2311 data->file_created = 1; 2312 } 2313 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) 2314 server->caps &= ~NFS_CAP_POSIX_LOCK; 2315 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 2316 status = _nfs4_proc_open_confirm(data); 2317 if (status != 0) 2318 return status; 2319 } 2320 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) 2321 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label); 2322 return 0; 2323 } 2324 2325 static int nfs4_recover_expired_lease(struct nfs_server *server) 2326 { 2327 return nfs4_client_recover_expired_lease(server->nfs_client); 2328 } 2329 2330 /* 2331 * OPEN_EXPIRED: 2332 * reclaim state on the server after a network partition. 2333 * Assumes caller holds the appropriate lock 2334 */ 2335 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2336 { 2337 struct nfs4_opendata *opendata; 2338 int ret; 2339 2340 opendata = nfs4_open_recoverdata_alloc(ctx, state, 2341 NFS4_OPEN_CLAIM_FH); 2342 if (IS_ERR(opendata)) 2343 return PTR_ERR(opendata); 2344 ret = nfs4_open_recover(opendata, state); 2345 if (ret == -ESTALE) 2346 d_drop(ctx->dentry); 2347 nfs4_opendata_put(opendata); 2348 return ret; 2349 } 2350 2351 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) 2352 { 2353 struct nfs_server *server = NFS_SERVER(state->inode); 2354 struct nfs4_exception exception = { }; 2355 int err; 2356 2357 do { 2358 err = _nfs4_open_expired(ctx, state); 2359 trace_nfs4_open_expired(ctx, 0, err); 2360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) 2361 continue; 2362 switch (err) { 2363 default: 2364 goto out; 2365 case -NFS4ERR_GRACE: 2366 case -NFS4ERR_DELAY: 2367 nfs4_handle_exception(server, err, &exception); 2368 err = 0; 2369 } 2370 } while (exception.retry); 2371 out: 2372 return err; 2373 } 2374 2375 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2376 { 2377 struct nfs_open_context *ctx; 2378 int ret; 2379 2380 ctx = nfs4_state_find_open_context(state); 2381 if (IS_ERR(ctx)) 2382 return -EAGAIN; 2383 ret = nfs4_do_open_expired(ctx, state); 2384 put_nfs_open_context(ctx); 2385 return ret; 2386 } 2387 2388 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state, 2389 const nfs4_stateid *stateid) 2390 { 2391 nfs_remove_bad_delegation(state->inode, stateid); 2392 write_seqlock(&state->seqlock); 2393 nfs4_stateid_copy(&state->stateid, &state->open_stateid); 2394 write_sequnlock(&state->seqlock); 2395 clear_bit(NFS_DELEGATED_STATE, &state->flags); 2396 } 2397 2398 static void nfs40_clear_delegation_stateid(struct nfs4_state *state) 2399 { 2400 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) 2401 nfs_finish_clear_delegation_stateid(state, NULL); 2402 } 2403 2404 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2405 { 2406 /* NFSv4.0 doesn't allow for delegation recovery on open expire */ 2407 nfs40_clear_delegation_stateid(state); 2408 return nfs4_open_expired(sp, state); 2409 } 2410 2411 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server, 2412 nfs4_stateid *stateid, 2413 struct rpc_cred *cred) 2414 { 2415 return -NFS4ERR_BAD_STATEID; 2416 } 2417 2418 #if defined(CONFIG_NFS_V4_1) 2419 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server, 2420 nfs4_stateid *stateid, 2421 struct rpc_cred *cred) 2422 { 2423 int status; 2424 2425 status = nfs41_test_stateid(server, stateid, cred); 2426 2427 switch (status) { 2428 case -NFS4ERR_EXPIRED: 2429 case -NFS4ERR_ADMIN_REVOKED: 2430 case -NFS4ERR_DELEG_REVOKED: 2431 /* Ack the revoked state to the server */ 2432 nfs41_free_stateid(server, stateid, cred); 2433 case -NFS4ERR_BAD_STATEID: 2434 return status; 2435 } 2436 return NFS_OK; 2437 } 2438 2439 static void nfs41_check_delegation_stateid(struct nfs4_state *state) 2440 { 2441 struct nfs_server *server = NFS_SERVER(state->inode); 2442 nfs4_stateid stateid; 2443 struct nfs_delegation *delegation; 2444 struct rpc_cred *cred; 2445 int status; 2446 2447 /* Get the delegation credential for use by test/free_stateid */ 2448 rcu_read_lock(); 2449 delegation = rcu_dereference(NFS_I(state->inode)->delegation); 2450 if (delegation == NULL) { 2451 rcu_read_unlock(); 2452 return; 2453 } 2454 2455 nfs4_stateid_copy(&stateid, &delegation->stateid); 2456 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 2457 rcu_read_unlock(); 2458 nfs_finish_clear_delegation_stateid(state, &stateid); 2459 return; 2460 } 2461 2462 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) { 2463 rcu_read_unlock(); 2464 return; 2465 } 2466 2467 cred = get_rpccred(delegation->cred); 2468 rcu_read_unlock(); 2469 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred); 2470 trace_nfs4_test_delegation_stateid(state, NULL, status); 2471 if (status != NFS_OK) 2472 nfs_finish_clear_delegation_stateid(state, &stateid); 2473 2474 put_rpccred(cred); 2475 } 2476 2477 /** 2478 * nfs41_check_open_stateid - possibly free an open stateid 2479 * 2480 * @state: NFSv4 state for an inode 2481 * 2482 * Returns NFS_OK if recovery for this stateid is now finished. 2483 * Otherwise a negative NFS4ERR value is returned. 2484 */ 2485 static int nfs41_check_open_stateid(struct nfs4_state *state) 2486 { 2487 struct nfs_server *server = NFS_SERVER(state->inode); 2488 nfs4_stateid *stateid = &state->open_stateid; 2489 struct rpc_cred *cred = state->owner->so_cred; 2490 int status; 2491 2492 /* If a state reset has been done, test_stateid is unneeded */ 2493 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) && 2494 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) && 2495 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0)) 2496 return -NFS4ERR_BAD_STATEID; 2497 2498 status = nfs41_test_and_free_expired_stateid(server, stateid, cred); 2499 trace_nfs4_test_open_stateid(state, NULL, status); 2500 if (status != NFS_OK) { 2501 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 2502 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 2503 clear_bit(NFS_O_RDWR_STATE, &state->flags); 2504 clear_bit(NFS_OPEN_STATE, &state->flags); 2505 } 2506 return status; 2507 } 2508 2509 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) 2510 { 2511 int status; 2512 2513 nfs41_check_delegation_stateid(state); 2514 status = nfs41_check_open_stateid(state); 2515 if (status != NFS_OK) 2516 status = nfs4_open_expired(sp, state); 2517 return status; 2518 } 2519 #endif 2520 2521 /* 2522 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* 2523 * fields corresponding to attributes that were used to store the verifier. 2524 * Make sure we clobber those fields in the later setattr call 2525 */ 2526 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, 2527 struct iattr *sattr, struct nfs4_label **label) 2528 { 2529 const u32 *attrset = opendata->o_res.attrset; 2530 2531 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) && 2532 !(sattr->ia_valid & ATTR_ATIME_SET)) 2533 sattr->ia_valid |= ATTR_ATIME; 2534 2535 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) && 2536 !(sattr->ia_valid & ATTR_MTIME_SET)) 2537 sattr->ia_valid |= ATTR_MTIME; 2538 2539 /* Except MODE, it seems harmless of setting twice. */ 2540 if ((attrset[1] & FATTR4_WORD1_MODE)) 2541 sattr->ia_valid &= ~ATTR_MODE; 2542 2543 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL) 2544 *label = NULL; 2545 } 2546 2547 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, 2548 fmode_t fmode, 2549 int flags, 2550 struct nfs_open_context *ctx) 2551 { 2552 struct nfs4_state_owner *sp = opendata->owner; 2553 struct nfs_server *server = sp->so_server; 2554 struct dentry *dentry; 2555 struct nfs4_state *state; 2556 unsigned int seq; 2557 int ret; 2558 2559 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); 2560 2561 ret = _nfs4_proc_open(opendata); 2562 if (ret != 0) 2563 goto out; 2564 2565 state = nfs4_opendata_to_nfs4_state(opendata); 2566 ret = PTR_ERR(state); 2567 if (IS_ERR(state)) 2568 goto out; 2569 if (server->caps & NFS_CAP_POSIX_LOCK) 2570 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); 2571 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK) 2572 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags); 2573 2574 dentry = opendata->dentry; 2575 if (d_really_is_negative(dentry)) { 2576 struct dentry *alias; 2577 d_drop(dentry); 2578 alias = d_exact_alias(dentry, state->inode); 2579 if (!alias) 2580 alias = d_splice_alias(igrab(state->inode), dentry); 2581 /* d_splice_alias() can't fail here - it's a non-directory */ 2582 if (alias) { 2583 dput(ctx->dentry); 2584 ctx->dentry = dentry = alias; 2585 } 2586 nfs_set_verifier(dentry, 2587 nfs_save_change_attribute(d_inode(opendata->dir))); 2588 } 2589 2590 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags); 2591 if (ret != 0) 2592 goto out; 2593 2594 ctx->state = state; 2595 if (d_inode(dentry) == state->inode) { 2596 nfs_inode_attach_open_context(ctx); 2597 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) 2598 nfs4_schedule_stateid_recovery(server, state); 2599 } 2600 out: 2601 return ret; 2602 } 2603 2604 /* 2605 * Returns a referenced nfs4_state 2606 */ 2607 static int _nfs4_do_open(struct inode *dir, 2608 struct nfs_open_context *ctx, 2609 int flags, 2610 struct iattr *sattr, 2611 struct nfs4_label *label, 2612 int *opened) 2613 { 2614 struct nfs4_state_owner *sp; 2615 struct nfs4_state *state = NULL; 2616 struct nfs_server *server = NFS_SERVER(dir); 2617 struct nfs4_opendata *opendata; 2618 struct dentry *dentry = ctx->dentry; 2619 struct rpc_cred *cred = ctx->cred; 2620 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; 2621 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); 2622 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; 2623 struct nfs4_label *olabel = NULL; 2624 int status; 2625 2626 /* Protect against reboot recovery conflicts */ 2627 status = -ENOMEM; 2628 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); 2629 if (sp == NULL) { 2630 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); 2631 goto out_err; 2632 } 2633 status = nfs4_recover_expired_lease(server); 2634 if (status != 0) 2635 goto err_put_state_owner; 2636 if (d_really_is_positive(dentry)) 2637 nfs4_return_incompatible_delegation(d_inode(dentry), fmode); 2638 status = -ENOMEM; 2639 if (d_really_is_positive(dentry)) 2640 claim = NFS4_OPEN_CLAIM_FH; 2641 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, 2642 label, claim, GFP_KERNEL); 2643 if (opendata == NULL) 2644 goto err_put_state_owner; 2645 2646 if (label) { 2647 olabel = nfs4_label_alloc(server, GFP_KERNEL); 2648 if (IS_ERR(olabel)) { 2649 status = PTR_ERR(olabel); 2650 goto err_opendata_put; 2651 } 2652 } 2653 2654 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { 2655 if (!opendata->f_attr.mdsthreshold) { 2656 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); 2657 if (!opendata->f_attr.mdsthreshold) 2658 goto err_free_label; 2659 } 2660 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; 2661 } 2662 if (d_really_is_positive(dentry)) 2663 opendata->state = nfs4_get_open_state(d_inode(dentry), sp); 2664 2665 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx); 2666 if (status != 0) 2667 goto err_free_label; 2668 state = ctx->state; 2669 2670 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) && 2671 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { 2672 nfs4_exclusive_attrset(opendata, sattr, &label); 2673 /* 2674 * send create attributes which was not set by open 2675 * with an extra setattr. 2676 */ 2677 if (sattr->ia_valid & NFS4_VALID_ATTRS) { 2678 nfs_fattr_init(opendata->o_res.f_attr); 2679 status = nfs4_do_setattr(state->inode, cred, 2680 opendata->o_res.f_attr, sattr, 2681 state, label, olabel); 2682 if (status == 0) { 2683 nfs_setattr_update_inode(state->inode, sattr, 2684 opendata->o_res.f_attr); 2685 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel); 2686 } 2687 } 2688 } 2689 if (opened && opendata->file_created) 2690 *opened |= FILE_CREATED; 2691 2692 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { 2693 *ctx_th = opendata->f_attr.mdsthreshold; 2694 opendata->f_attr.mdsthreshold = NULL; 2695 } 2696 2697 nfs4_label_free(olabel); 2698 2699 nfs4_opendata_put(opendata); 2700 nfs4_put_state_owner(sp); 2701 return 0; 2702 err_free_label: 2703 nfs4_label_free(olabel); 2704 err_opendata_put: 2705 nfs4_opendata_put(opendata); 2706 err_put_state_owner: 2707 nfs4_put_state_owner(sp); 2708 out_err: 2709 return status; 2710 } 2711 2712 2713 static struct nfs4_state *nfs4_do_open(struct inode *dir, 2714 struct nfs_open_context *ctx, 2715 int flags, 2716 struct iattr *sattr, 2717 struct nfs4_label *label, 2718 int *opened) 2719 { 2720 struct nfs_server *server = NFS_SERVER(dir); 2721 struct nfs4_exception exception = { }; 2722 struct nfs4_state *res; 2723 int status; 2724 2725 do { 2726 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened); 2727 res = ctx->state; 2728 trace_nfs4_open_file(ctx, flags, status); 2729 if (status == 0) 2730 break; 2731 /* NOTE: BAD_SEQID means the server and client disagree about the 2732 * book-keeping w.r.t. state-changing operations 2733 * (OPEN/CLOSE/LOCK/LOCKU...) 2734 * It is actually a sign of a bug on the client or on the server. 2735 * 2736 * If we receive a BAD_SEQID error in the particular case of 2737 * doing an OPEN, we assume that nfs_increment_open_seqid() will 2738 * have unhashed the old state_owner for us, and that we can 2739 * therefore safely retry using a new one. We should still warn 2740 * the user though... 2741 */ 2742 if (status == -NFS4ERR_BAD_SEQID) { 2743 pr_warn_ratelimited("NFS: v4 server %s " 2744 " returned a bad sequence-id error!\n", 2745 NFS_SERVER(dir)->nfs_client->cl_hostname); 2746 exception.retry = 1; 2747 continue; 2748 } 2749 /* 2750 * BAD_STATEID on OPEN means that the server cancelled our 2751 * state before it received the OPEN_CONFIRM. 2752 * Recover by retrying the request as per the discussion 2753 * on Page 181 of RFC3530. 2754 */ 2755 if (status == -NFS4ERR_BAD_STATEID) { 2756 exception.retry = 1; 2757 continue; 2758 } 2759 if (status == -EAGAIN) { 2760 /* We must have found a delegation */ 2761 exception.retry = 1; 2762 continue; 2763 } 2764 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) 2765 continue; 2766 res = ERR_PTR(nfs4_handle_exception(server, 2767 status, &exception)); 2768 } while (exception.retry); 2769 return res; 2770 } 2771 2772 static int _nfs4_do_setattr(struct inode *inode, 2773 struct nfs_setattrargs *arg, 2774 struct nfs_setattrres *res, 2775 struct rpc_cred *cred, 2776 struct nfs4_state *state) 2777 { 2778 struct nfs_server *server = NFS_SERVER(inode); 2779 struct rpc_message msg = { 2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 2781 .rpc_argp = arg, 2782 .rpc_resp = res, 2783 .rpc_cred = cred, 2784 }; 2785 struct rpc_cred *delegation_cred = NULL; 2786 unsigned long timestamp = jiffies; 2787 fmode_t fmode; 2788 bool truncate; 2789 int status; 2790 2791 nfs_fattr_init(res->fattr); 2792 2793 /* Servers should only apply open mode checks for file size changes */ 2794 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false; 2795 fmode = truncate ? FMODE_WRITE : FMODE_READ; 2796 2797 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) { 2798 /* Use that stateid */ 2799 } else if (truncate && state != NULL) { 2800 struct nfs_lockowner lockowner = { 2801 .l_owner = current->files, 2802 .l_pid = current->tgid, 2803 }; 2804 if (!nfs4_valid_open_stateid(state)) 2805 return -EBADF; 2806 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner, 2807 &arg->stateid, &delegation_cred) == -EIO) 2808 return -EBADF; 2809 } else 2810 nfs4_stateid_copy(&arg->stateid, &zero_stateid); 2811 if (delegation_cred) 2812 msg.rpc_cred = delegation_cred; 2813 2814 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1); 2815 2816 put_rpccred(delegation_cred); 2817 if (status == 0 && state != NULL) 2818 renew_lease(server, timestamp); 2819 trace_nfs4_setattr(inode, &arg->stateid, status); 2820 return status; 2821 } 2822 2823 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, 2824 struct nfs_fattr *fattr, struct iattr *sattr, 2825 struct nfs4_state *state, struct nfs4_label *ilabel, 2826 struct nfs4_label *olabel) 2827 { 2828 struct nfs_server *server = NFS_SERVER(inode); 2829 struct nfs_setattrargs arg = { 2830 .fh = NFS_FH(inode), 2831 .iap = sattr, 2832 .server = server, 2833 .bitmask = server->attr_bitmask, 2834 .label = ilabel, 2835 }; 2836 struct nfs_setattrres res = { 2837 .fattr = fattr, 2838 .label = olabel, 2839 .server = server, 2840 }; 2841 struct nfs4_exception exception = { 2842 .state = state, 2843 .inode = inode, 2844 .stateid = &arg.stateid, 2845 }; 2846 int err; 2847 2848 arg.bitmask = nfs4_bitmask(server, ilabel); 2849 if (ilabel) 2850 arg.bitmask = nfs4_bitmask(server, olabel); 2851 2852 do { 2853 err = _nfs4_do_setattr(inode, &arg, &res, cred, state); 2854 switch (err) { 2855 case -NFS4ERR_OPENMODE: 2856 if (!(sattr->ia_valid & ATTR_SIZE)) { 2857 pr_warn_once("NFSv4: server %s is incorrectly " 2858 "applying open mode checks to " 2859 "a SETATTR that is not " 2860 "changing file size.\n", 2861 server->nfs_client->cl_hostname); 2862 } 2863 if (state && !(state->state & FMODE_WRITE)) { 2864 err = -EBADF; 2865 if (sattr->ia_valid & ATTR_OPEN) 2866 err = -EACCES; 2867 goto out; 2868 } 2869 } 2870 err = nfs4_handle_exception(server, err, &exception); 2871 } while (exception.retry); 2872 out: 2873 return err; 2874 } 2875 2876 static bool 2877 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task) 2878 { 2879 if (inode == NULL || !nfs_have_layout(inode)) 2880 return false; 2881 2882 return pnfs_wait_on_layoutreturn(inode, task); 2883 } 2884 2885 struct nfs4_closedata { 2886 struct inode *inode; 2887 struct nfs4_state *state; 2888 struct nfs_closeargs arg; 2889 struct nfs_closeres res; 2890 struct nfs_fattr fattr; 2891 unsigned long timestamp; 2892 bool roc; 2893 u32 roc_barrier; 2894 }; 2895 2896 static void nfs4_free_closedata(void *data) 2897 { 2898 struct nfs4_closedata *calldata = data; 2899 struct nfs4_state_owner *sp = calldata->state->owner; 2900 struct super_block *sb = calldata->state->inode->i_sb; 2901 2902 if (calldata->roc) 2903 pnfs_roc_release(calldata->state->inode); 2904 nfs4_put_open_state(calldata->state); 2905 nfs_free_seqid(calldata->arg.seqid); 2906 nfs4_put_state_owner(sp); 2907 nfs_sb_deactive(sb); 2908 kfree(calldata); 2909 } 2910 2911 static void nfs4_close_done(struct rpc_task *task, void *data) 2912 { 2913 struct nfs4_closedata *calldata = data; 2914 struct nfs4_state *state = calldata->state; 2915 struct nfs_server *server = NFS_SERVER(calldata->inode); 2916 nfs4_stateid *res_stateid = NULL; 2917 2918 dprintk("%s: begin!\n", __func__); 2919 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 2920 return; 2921 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); 2922 /* hmm. we are done with the inode, and in the process of freeing 2923 * the state_owner. we keep this around to process errors 2924 */ 2925 switch (task->tk_status) { 2926 case 0: 2927 res_stateid = &calldata->res.stateid; 2928 if (calldata->roc) 2929 pnfs_roc_set_barrier(state->inode, 2930 calldata->roc_barrier); 2931 renew_lease(server, calldata->timestamp); 2932 break; 2933 case -NFS4ERR_ADMIN_REVOKED: 2934 case -NFS4ERR_STALE_STATEID: 2935 case -NFS4ERR_OLD_STATEID: 2936 case -NFS4ERR_BAD_STATEID: 2937 case -NFS4ERR_EXPIRED: 2938 if (!nfs4_stateid_match(&calldata->arg.stateid, 2939 &state->open_stateid)) { 2940 rpc_restart_call_prepare(task); 2941 goto out_release; 2942 } 2943 if (calldata->arg.fmode == 0) 2944 break; 2945 default: 2946 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) { 2947 rpc_restart_call_prepare(task); 2948 goto out_release; 2949 } 2950 } 2951 nfs_clear_open_stateid(state, &calldata->arg.stateid, 2952 res_stateid, calldata->arg.fmode); 2953 out_release: 2954 nfs_release_seqid(calldata->arg.seqid); 2955 nfs_refresh_inode(calldata->inode, calldata->res.fattr); 2956 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); 2957 } 2958 2959 static void nfs4_close_prepare(struct rpc_task *task, void *data) 2960 { 2961 struct nfs4_closedata *calldata = data; 2962 struct nfs4_state *state = calldata->state; 2963 struct inode *inode = calldata->inode; 2964 bool is_rdonly, is_wronly, is_rdwr; 2965 int call_close = 0; 2966 2967 dprintk("%s: begin!\n", __func__); 2968 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 2969 goto out_wait; 2970 2971 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; 2972 spin_lock(&state->owner->so_lock); 2973 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); 2974 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); 2975 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); 2976 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid); 2977 /* Calculate the change in open mode */ 2978 calldata->arg.fmode = 0; 2979 if (state->n_rdwr == 0) { 2980 if (state->n_rdonly == 0) 2981 call_close |= is_rdonly; 2982 else if (is_rdonly) 2983 calldata->arg.fmode |= FMODE_READ; 2984 if (state->n_wronly == 0) 2985 call_close |= is_wronly; 2986 else if (is_wronly) 2987 calldata->arg.fmode |= FMODE_WRITE; 2988 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE)) 2989 call_close |= is_rdwr; 2990 } else if (is_rdwr) 2991 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; 2992 2993 if (!nfs4_valid_open_stateid(state)) 2994 call_close = 0; 2995 spin_unlock(&state->owner->so_lock); 2996 2997 if (!call_close) { 2998 /* Note: exit _without_ calling nfs4_close_done */ 2999 goto out_no_action; 3000 } 3001 3002 if (nfs4_wait_on_layoutreturn(inode, task)) { 3003 nfs_release_seqid(calldata->arg.seqid); 3004 goto out_wait; 3005 } 3006 3007 if (calldata->arg.fmode == 0) 3008 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; 3009 if (calldata->roc) 3010 pnfs_roc_get_barrier(inode, &calldata->roc_barrier); 3011 3012 calldata->arg.share_access = 3013 nfs4_map_atomic_open_share(NFS_SERVER(inode), 3014 calldata->arg.fmode, 0); 3015 3016 nfs_fattr_init(calldata->res.fattr); 3017 calldata->timestamp = jiffies; 3018 if (nfs4_setup_sequence(NFS_SERVER(inode), 3019 &calldata->arg.seq_args, 3020 &calldata->res.seq_res, 3021 task) != 0) 3022 nfs_release_seqid(calldata->arg.seqid); 3023 dprintk("%s: done!\n", __func__); 3024 return; 3025 out_no_action: 3026 task->tk_action = NULL; 3027 out_wait: 3028 nfs4_sequence_done(task, &calldata->res.seq_res); 3029 } 3030 3031 static const struct rpc_call_ops nfs4_close_ops = { 3032 .rpc_call_prepare = nfs4_close_prepare, 3033 .rpc_call_done = nfs4_close_done, 3034 .rpc_release = nfs4_free_closedata, 3035 }; 3036 3037 static bool nfs4_roc(struct inode *inode) 3038 { 3039 if (!nfs_have_layout(inode)) 3040 return false; 3041 return pnfs_roc(inode); 3042 } 3043 3044 /* 3045 * It is possible for data to be read/written from a mem-mapped file 3046 * after the sys_close call (which hits the vfs layer as a flush). 3047 * This means that we can't safely call nfsv4 close on a file until 3048 * the inode is cleared. This in turn means that we are not good 3049 * NFSv4 citizens - we do not indicate to the server to update the file's 3050 * share state even when we are done with one of the three share 3051 * stateid's in the inode. 3052 * 3053 * NOTE: Caller must be holding the sp->so_owner semaphore! 3054 */ 3055 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) 3056 { 3057 struct nfs_server *server = NFS_SERVER(state->inode); 3058 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 3059 struct nfs4_closedata *calldata; 3060 struct nfs4_state_owner *sp = state->owner; 3061 struct rpc_task *task; 3062 struct rpc_message msg = { 3063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], 3064 .rpc_cred = state->owner->so_cred, 3065 }; 3066 struct rpc_task_setup task_setup_data = { 3067 .rpc_client = server->client, 3068 .rpc_message = &msg, 3069 .callback_ops = &nfs4_close_ops, 3070 .workqueue = nfsiod_workqueue, 3071 .flags = RPC_TASK_ASYNC, 3072 }; 3073 int status = -ENOMEM; 3074 3075 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, 3076 &task_setup_data.rpc_client, &msg); 3077 3078 calldata = kzalloc(sizeof(*calldata), gfp_mask); 3079 if (calldata == NULL) 3080 goto out; 3081 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1); 3082 calldata->inode = state->inode; 3083 calldata->state = state; 3084 calldata->arg.fh = NFS_FH(state->inode); 3085 /* Serialization for the sequence id */ 3086 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 3087 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask); 3088 if (IS_ERR(calldata->arg.seqid)) 3089 goto out_free_calldata; 3090 calldata->arg.fmode = 0; 3091 calldata->arg.bitmask = server->cache_consistency_bitmask; 3092 calldata->res.fattr = &calldata->fattr; 3093 calldata->res.seqid = calldata->arg.seqid; 3094 calldata->res.server = server; 3095 calldata->roc = nfs4_roc(state->inode); 3096 nfs_sb_active(calldata->inode->i_sb); 3097 3098 msg.rpc_argp = &calldata->arg; 3099 msg.rpc_resp = &calldata->res; 3100 task_setup_data.callback_data = calldata; 3101 task = rpc_run_task(&task_setup_data); 3102 if (IS_ERR(task)) 3103 return PTR_ERR(task); 3104 status = 0; 3105 if (wait) 3106 status = rpc_wait_for_completion_task(task); 3107 rpc_put_task(task); 3108 return status; 3109 out_free_calldata: 3110 kfree(calldata); 3111 out: 3112 nfs4_put_open_state(state); 3113 nfs4_put_state_owner(sp); 3114 return status; 3115 } 3116 3117 static struct inode * 3118 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, 3119 int open_flags, struct iattr *attr, int *opened) 3120 { 3121 struct nfs4_state *state; 3122 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL; 3123 3124 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); 3125 3126 /* Protect against concurrent sillydeletes */ 3127 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); 3128 3129 nfs4_label_release_security(label); 3130 3131 if (IS_ERR(state)) 3132 return ERR_CAST(state); 3133 return state->inode; 3134 } 3135 3136 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) 3137 { 3138 if (ctx->state == NULL) 3139 return; 3140 if (is_sync) 3141 nfs4_close_sync(ctx->state, ctx->mode); 3142 else 3143 nfs4_close_state(ctx->state, ctx->mode); 3144 } 3145 3146 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) 3147 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) 3148 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL) 3149 3150 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 3151 { 3152 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion; 3153 struct nfs4_server_caps_arg args = { 3154 .fhandle = fhandle, 3155 .bitmask = bitmask, 3156 }; 3157 struct nfs4_server_caps_res res = {}; 3158 struct rpc_message msg = { 3159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], 3160 .rpc_argp = &args, 3161 .rpc_resp = &res, 3162 }; 3163 int status; 3164 3165 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS | 3166 FATTR4_WORD0_FH_EXPIRE_TYPE | 3167 FATTR4_WORD0_LINK_SUPPORT | 3168 FATTR4_WORD0_SYMLINK_SUPPORT | 3169 FATTR4_WORD0_ACLSUPPORT; 3170 if (minorversion) 3171 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT; 3172 3173 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3174 if (status == 0) { 3175 /* Sanity check the server answers */ 3176 switch (minorversion) { 3177 case 0: 3178 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; 3179 res.attr_bitmask[2] = 0; 3180 break; 3181 case 1: 3182 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; 3183 break; 3184 case 2: 3185 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; 3186 } 3187 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); 3188 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| 3189 NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 3190 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| 3191 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| 3192 NFS_CAP_CTIME|NFS_CAP_MTIME| 3193 NFS_CAP_SECURITY_LABEL); 3194 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && 3195 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) 3196 server->caps |= NFS_CAP_ACLS; 3197 if (res.has_links != 0) 3198 server->caps |= NFS_CAP_HARDLINKS; 3199 if (res.has_symlinks != 0) 3200 server->caps |= NFS_CAP_SYMLINKS; 3201 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) 3202 server->caps |= NFS_CAP_FILEID; 3203 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) 3204 server->caps |= NFS_CAP_MODE; 3205 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) 3206 server->caps |= NFS_CAP_NLINK; 3207 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) 3208 server->caps |= NFS_CAP_OWNER; 3209 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) 3210 server->caps |= NFS_CAP_OWNER_GROUP; 3211 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) 3212 server->caps |= NFS_CAP_ATIME; 3213 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) 3214 server->caps |= NFS_CAP_CTIME; 3215 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) 3216 server->caps |= NFS_CAP_MTIME; 3217 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 3218 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) 3219 server->caps |= NFS_CAP_SECURITY_LABEL; 3220 #endif 3221 memcpy(server->attr_bitmask_nl, res.attr_bitmask, 3222 sizeof(server->attr_bitmask)); 3223 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; 3224 3225 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); 3226 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; 3227 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; 3228 server->cache_consistency_bitmask[2] = 0; 3229 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask, 3230 sizeof(server->exclcreat_bitmask)); 3231 server->acl_bitmask = res.acl_bitmask; 3232 server->fh_expire_type = res.fh_expire_type; 3233 } 3234 3235 return status; 3236 } 3237 3238 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) 3239 { 3240 struct nfs4_exception exception = { }; 3241 int err; 3242 do { 3243 err = nfs4_handle_exception(server, 3244 _nfs4_server_capabilities(server, fhandle), 3245 &exception); 3246 } while (exception.retry); 3247 return err; 3248 } 3249 3250 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 3251 struct nfs_fsinfo *info) 3252 { 3253 u32 bitmask[3]; 3254 struct nfs4_lookup_root_arg args = { 3255 .bitmask = bitmask, 3256 }; 3257 struct nfs4_lookup_res res = { 3258 .server = server, 3259 .fattr = info->fattr, 3260 .fh = fhandle, 3261 }; 3262 struct rpc_message msg = { 3263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], 3264 .rpc_argp = &args, 3265 .rpc_resp = &res, 3266 }; 3267 3268 bitmask[0] = nfs4_fattr_bitmap[0]; 3269 bitmask[1] = nfs4_fattr_bitmap[1]; 3270 /* 3271 * Process the label in the upcoming getfattr 3272 */ 3273 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; 3274 3275 nfs_fattr_init(info->fattr); 3276 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3277 } 3278 3279 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, 3280 struct nfs_fsinfo *info) 3281 { 3282 struct nfs4_exception exception = { }; 3283 int err; 3284 do { 3285 err = _nfs4_lookup_root(server, fhandle, info); 3286 trace_nfs4_lookup_root(server, fhandle, info->fattr, err); 3287 switch (err) { 3288 case 0: 3289 case -NFS4ERR_WRONGSEC: 3290 goto out; 3291 default: 3292 err = nfs4_handle_exception(server, err, &exception); 3293 } 3294 } while (exception.retry); 3295 out: 3296 return err; 3297 } 3298 3299 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3300 struct nfs_fsinfo *info, rpc_authflavor_t flavor) 3301 { 3302 struct rpc_auth_create_args auth_args = { 3303 .pseudoflavor = flavor, 3304 }; 3305 struct rpc_auth *auth; 3306 int ret; 3307 3308 auth = rpcauth_create(&auth_args, server->client); 3309 if (IS_ERR(auth)) { 3310 ret = -EACCES; 3311 goto out; 3312 } 3313 ret = nfs4_lookup_root(server, fhandle, info); 3314 out: 3315 return ret; 3316 } 3317 3318 /* 3319 * Retry pseudoroot lookup with various security flavors. We do this when: 3320 * 3321 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC 3322 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation 3323 * 3324 * Returns zero on success, or a negative NFS4ERR value, or a 3325 * negative errno value. 3326 */ 3327 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 3328 struct nfs_fsinfo *info) 3329 { 3330 /* Per 3530bis 15.33.5 */ 3331 static const rpc_authflavor_t flav_array[] = { 3332 RPC_AUTH_GSS_KRB5P, 3333 RPC_AUTH_GSS_KRB5I, 3334 RPC_AUTH_GSS_KRB5, 3335 RPC_AUTH_UNIX, /* courtesy */ 3336 RPC_AUTH_NULL, 3337 }; 3338 int status = -EPERM; 3339 size_t i; 3340 3341 if (server->auth_info.flavor_len > 0) { 3342 /* try each flavor specified by user */ 3343 for (i = 0; i < server->auth_info.flavor_len; i++) { 3344 status = nfs4_lookup_root_sec(server, fhandle, info, 3345 server->auth_info.flavors[i]); 3346 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3347 continue; 3348 break; 3349 } 3350 } else { 3351 /* no flavors specified by user, try default list */ 3352 for (i = 0; i < ARRAY_SIZE(flav_array); i++) { 3353 status = nfs4_lookup_root_sec(server, fhandle, info, 3354 flav_array[i]); 3355 if (status == -NFS4ERR_WRONGSEC || status == -EACCES) 3356 continue; 3357 break; 3358 } 3359 } 3360 3361 /* 3362 * -EACCESS could mean that the user doesn't have correct permissions 3363 * to access the mount. It could also mean that we tried to mount 3364 * with a gss auth flavor, but rpc.gssd isn't running. Either way, 3365 * existing mount programs don't handle -EACCES very well so it should 3366 * be mapped to -EPERM instead. 3367 */ 3368 if (status == -EACCES) 3369 status = -EPERM; 3370 return status; 3371 } 3372 3373 /** 3374 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot 3375 * @server: initialized nfs_server handle 3376 * @fhandle: we fill in the pseudo-fs root file handle 3377 * @info: we fill in an FSINFO struct 3378 * @auth_probe: probe the auth flavours 3379 * 3380 * Returns zero on success, or a negative errno. 3381 */ 3382 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, 3383 struct nfs_fsinfo *info, 3384 bool auth_probe) 3385 { 3386 int status = 0; 3387 3388 if (!auth_probe) 3389 status = nfs4_lookup_root(server, fhandle, info); 3390 3391 if (auth_probe || status == NFS4ERR_WRONGSEC) 3392 status = server->nfs_client->cl_mvops->find_root_sec(server, 3393 fhandle, info); 3394 3395 if (status == 0) 3396 status = nfs4_server_capabilities(server, fhandle); 3397 if (status == 0) 3398 status = nfs4_do_fsinfo(server, fhandle, info); 3399 3400 return nfs4_map_errors(status); 3401 } 3402 3403 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, 3404 struct nfs_fsinfo *info) 3405 { 3406 int error; 3407 struct nfs_fattr *fattr = info->fattr; 3408 struct nfs4_label *label = NULL; 3409 3410 error = nfs4_server_capabilities(server, mntfh); 3411 if (error < 0) { 3412 dprintk("nfs4_get_root: getcaps error = %d\n", -error); 3413 return error; 3414 } 3415 3416 label = nfs4_label_alloc(server, GFP_KERNEL); 3417 if (IS_ERR(label)) 3418 return PTR_ERR(label); 3419 3420 error = nfs4_proc_getattr(server, mntfh, fattr, label); 3421 if (error < 0) { 3422 dprintk("nfs4_get_root: getattr error = %d\n", -error); 3423 goto err_free_label; 3424 } 3425 3426 if (fattr->valid & NFS_ATTR_FATTR_FSID && 3427 !nfs_fsid_equal(&server->fsid, &fattr->fsid)) 3428 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 3429 3430 err_free_label: 3431 nfs4_label_free(label); 3432 3433 return error; 3434 } 3435 3436 /* 3437 * Get locations and (maybe) other attributes of a referral. 3438 * Note that we'll actually follow the referral later when 3439 * we detect fsid mismatch in inode revalidation 3440 */ 3441 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, 3442 const struct qstr *name, struct nfs_fattr *fattr, 3443 struct nfs_fh *fhandle) 3444 { 3445 int status = -ENOMEM; 3446 struct page *page = NULL; 3447 struct nfs4_fs_locations *locations = NULL; 3448 3449 page = alloc_page(GFP_KERNEL); 3450 if (page == NULL) 3451 goto out; 3452 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); 3453 if (locations == NULL) 3454 goto out; 3455 3456 status = nfs4_proc_fs_locations(client, dir, name, locations, page); 3457 if (status != 0) 3458 goto out; 3459 3460 /* 3461 * If the fsid didn't change, this is a migration event, not a 3462 * referral. Cause us to drop into the exception handler, which 3463 * will kick off migration recovery. 3464 */ 3465 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { 3466 dprintk("%s: server did not return a different fsid for" 3467 " a referral at %s\n", __func__, name->name); 3468 status = -NFS4ERR_MOVED; 3469 goto out; 3470 } 3471 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ 3472 nfs_fixup_referral_attributes(&locations->fattr); 3473 3474 /* replace the lookup nfs_fattr with the locations nfs_fattr */ 3475 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); 3476 memset(fhandle, 0, sizeof(struct nfs_fh)); 3477 out: 3478 if (page) 3479 __free_page(page); 3480 kfree(locations); 3481 return status; 3482 } 3483 3484 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 3485 struct nfs_fattr *fattr, struct nfs4_label *label) 3486 { 3487 struct nfs4_getattr_arg args = { 3488 .fh = fhandle, 3489 .bitmask = server->attr_bitmask, 3490 }; 3491 struct nfs4_getattr_res res = { 3492 .fattr = fattr, 3493 .label = label, 3494 .server = server, 3495 }; 3496 struct rpc_message msg = { 3497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 3498 .rpc_argp = &args, 3499 .rpc_resp = &res, 3500 }; 3501 3502 args.bitmask = nfs4_bitmask(server, label); 3503 3504 nfs_fattr_init(fattr); 3505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3506 } 3507 3508 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 3509 struct nfs_fattr *fattr, struct nfs4_label *label) 3510 { 3511 struct nfs4_exception exception = { }; 3512 int err; 3513 do { 3514 err = _nfs4_proc_getattr(server, fhandle, fattr, label); 3515 trace_nfs4_getattr(server, fhandle, fattr, err); 3516 err = nfs4_handle_exception(server, err, 3517 &exception); 3518 } while (exception.retry); 3519 return err; 3520 } 3521 3522 /* 3523 * The file is not closed if it is opened due to the a request to change 3524 * the size of the file. The open call will not be needed once the 3525 * VFS layer lookup-intents are implemented. 3526 * 3527 * Close is called when the inode is destroyed. 3528 * If we haven't opened the file for O_WRONLY, we 3529 * need to in the size_change case to obtain a stateid. 3530 * 3531 * Got race? 3532 * Because OPEN is always done by name in nfsv4, it is 3533 * possible that we opened a different file by the same 3534 * name. We can recognize this race condition, but we 3535 * can't do anything about it besides returning an error. 3536 * 3537 * This will be fixed with VFS changes (lookup-intent). 3538 */ 3539 static int 3540 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, 3541 struct iattr *sattr) 3542 { 3543 struct inode *inode = d_inode(dentry); 3544 struct rpc_cred *cred = NULL; 3545 struct nfs4_state *state = NULL; 3546 struct nfs4_label *label = NULL; 3547 int status; 3548 3549 if (pnfs_ld_layoutret_on_setattr(inode) && 3550 sattr->ia_valid & ATTR_SIZE && 3551 sattr->ia_size < i_size_read(inode)) 3552 pnfs_commit_and_return_layout(inode); 3553 3554 nfs_fattr_init(fattr); 3555 3556 /* Deal with open(O_TRUNC) */ 3557 if (sattr->ia_valid & ATTR_OPEN) 3558 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); 3559 3560 /* Optimization: if the end result is no change, don't RPC */ 3561 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) 3562 return 0; 3563 3564 /* Search for an existing open(O_WRITE) file */ 3565 if (sattr->ia_valid & ATTR_FILE) { 3566 struct nfs_open_context *ctx; 3567 3568 ctx = nfs_file_open_context(sattr->ia_file); 3569 if (ctx) { 3570 cred = ctx->cred; 3571 state = ctx->state; 3572 } 3573 } 3574 3575 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 3576 if (IS_ERR(label)) 3577 return PTR_ERR(label); 3578 3579 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label); 3580 if (status == 0) { 3581 nfs_setattr_update_inode(inode, sattr, fattr); 3582 nfs_setsecurity(inode, fattr, label); 3583 } 3584 nfs4_label_free(label); 3585 return status; 3586 } 3587 3588 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, 3589 const struct qstr *name, struct nfs_fh *fhandle, 3590 struct nfs_fattr *fattr, struct nfs4_label *label) 3591 { 3592 struct nfs_server *server = NFS_SERVER(dir); 3593 int status; 3594 struct nfs4_lookup_arg args = { 3595 .bitmask = server->attr_bitmask, 3596 .dir_fh = NFS_FH(dir), 3597 .name = name, 3598 }; 3599 struct nfs4_lookup_res res = { 3600 .server = server, 3601 .fattr = fattr, 3602 .label = label, 3603 .fh = fhandle, 3604 }; 3605 struct rpc_message msg = { 3606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], 3607 .rpc_argp = &args, 3608 .rpc_resp = &res, 3609 }; 3610 3611 args.bitmask = nfs4_bitmask(server, label); 3612 3613 nfs_fattr_init(fattr); 3614 3615 dprintk("NFS call lookup %s\n", name->name); 3616 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0); 3617 dprintk("NFS reply lookup: %d\n", status); 3618 return status; 3619 } 3620 3621 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) 3622 { 3623 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 3624 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; 3625 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 3626 fattr->nlink = 2; 3627 } 3628 3629 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, 3630 const struct qstr *name, struct nfs_fh *fhandle, 3631 struct nfs_fattr *fattr, struct nfs4_label *label) 3632 { 3633 struct nfs4_exception exception = { }; 3634 struct rpc_clnt *client = *clnt; 3635 int err; 3636 do { 3637 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label); 3638 trace_nfs4_lookup(dir, name, err); 3639 switch (err) { 3640 case -NFS4ERR_BADNAME: 3641 err = -ENOENT; 3642 goto out; 3643 case -NFS4ERR_MOVED: 3644 err = nfs4_get_referral(client, dir, name, fattr, fhandle); 3645 if (err == -NFS4ERR_MOVED) 3646 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 3647 goto out; 3648 case -NFS4ERR_WRONGSEC: 3649 err = -EPERM; 3650 if (client != *clnt) 3651 goto out; 3652 client = nfs4_negotiate_security(client, dir, name); 3653 if (IS_ERR(client)) 3654 return PTR_ERR(client); 3655 3656 exception.retry = 1; 3657 break; 3658 default: 3659 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); 3660 } 3661 } while (exception.retry); 3662 3663 out: 3664 if (err == 0) 3665 *clnt = client; 3666 else if (client != *clnt) 3667 rpc_shutdown_client(client); 3668 3669 return err; 3670 } 3671 3672 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name, 3673 struct nfs_fh *fhandle, struct nfs_fattr *fattr, 3674 struct nfs4_label *label) 3675 { 3676 int status; 3677 struct rpc_clnt *client = NFS_CLIENT(dir); 3678 3679 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label); 3680 if (client != NFS_CLIENT(dir)) { 3681 rpc_shutdown_client(client); 3682 nfs_fixup_secinfo_attributes(fattr); 3683 } 3684 return status; 3685 } 3686 3687 struct rpc_clnt * 3688 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name, 3689 struct nfs_fh *fhandle, struct nfs_fattr *fattr) 3690 { 3691 struct rpc_clnt *client = NFS_CLIENT(dir); 3692 int status; 3693 3694 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL); 3695 if (status < 0) 3696 return ERR_PTR(status); 3697 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; 3698 } 3699 3700 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 3701 { 3702 struct nfs_server *server = NFS_SERVER(inode); 3703 struct nfs4_accessargs args = { 3704 .fh = NFS_FH(inode), 3705 .bitmask = server->cache_consistency_bitmask, 3706 }; 3707 struct nfs4_accessres res = { 3708 .server = server, 3709 }; 3710 struct rpc_message msg = { 3711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], 3712 .rpc_argp = &args, 3713 .rpc_resp = &res, 3714 .rpc_cred = entry->cred, 3715 }; 3716 int mode = entry->mask; 3717 int status = 0; 3718 3719 /* 3720 * Determine which access bits we want to ask for... 3721 */ 3722 if (mode & MAY_READ) 3723 args.access |= NFS4_ACCESS_READ; 3724 if (S_ISDIR(inode->i_mode)) { 3725 if (mode & MAY_WRITE) 3726 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; 3727 if (mode & MAY_EXEC) 3728 args.access |= NFS4_ACCESS_LOOKUP; 3729 } else { 3730 if (mode & MAY_WRITE) 3731 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; 3732 if (mode & MAY_EXEC) 3733 args.access |= NFS4_ACCESS_EXECUTE; 3734 } 3735 3736 res.fattr = nfs_alloc_fattr(); 3737 if (res.fattr == NULL) 3738 return -ENOMEM; 3739 3740 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 3741 if (!status) { 3742 nfs_access_set_mask(entry, res.access); 3743 nfs_refresh_inode(inode, res.fattr); 3744 } 3745 nfs_free_fattr(res.fattr); 3746 return status; 3747 } 3748 3749 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) 3750 { 3751 struct nfs4_exception exception = { }; 3752 int err; 3753 do { 3754 err = _nfs4_proc_access(inode, entry); 3755 trace_nfs4_access(inode, err); 3756 err = nfs4_handle_exception(NFS_SERVER(inode), err, 3757 &exception); 3758 } while (exception.retry); 3759 return err; 3760 } 3761 3762 /* 3763 * TODO: For the time being, we don't try to get any attributes 3764 * along with any of the zero-copy operations READ, READDIR, 3765 * READLINK, WRITE. 3766 * 3767 * In the case of the first three, we want to put the GETATTR 3768 * after the read-type operation -- this is because it is hard 3769 * to predict the length of a GETATTR response in v4, and thus 3770 * align the READ data correctly. This means that the GETATTR 3771 * may end up partially falling into the page cache, and we should 3772 * shift it into the 'tail' of the xdr_buf before processing. 3773 * To do this efficiently, we need to know the total length 3774 * of data received, which doesn't seem to be available outside 3775 * of the RPC layer. 3776 * 3777 * In the case of WRITE, we also want to put the GETATTR after 3778 * the operation -- in this case because we want to make sure 3779 * we get the post-operation mtime and size. 3780 * 3781 * Both of these changes to the XDR layer would in fact be quite 3782 * minor, but I decided to leave them for a subsequent patch. 3783 */ 3784 static int _nfs4_proc_readlink(struct inode *inode, struct page *page, 3785 unsigned int pgbase, unsigned int pglen) 3786 { 3787 struct nfs4_readlink args = { 3788 .fh = NFS_FH(inode), 3789 .pgbase = pgbase, 3790 .pglen = pglen, 3791 .pages = &page, 3792 }; 3793 struct nfs4_readlink_res res; 3794 struct rpc_message msg = { 3795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], 3796 .rpc_argp = &args, 3797 .rpc_resp = &res, 3798 }; 3799 3800 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); 3801 } 3802 3803 static int nfs4_proc_readlink(struct inode *inode, struct page *page, 3804 unsigned int pgbase, unsigned int pglen) 3805 { 3806 struct nfs4_exception exception = { }; 3807 int err; 3808 do { 3809 err = _nfs4_proc_readlink(inode, page, pgbase, pglen); 3810 trace_nfs4_readlink(inode, err); 3811 err = nfs4_handle_exception(NFS_SERVER(inode), err, 3812 &exception); 3813 } while (exception.retry); 3814 return err; 3815 } 3816 3817 /* 3818 * This is just for mknod. open(O_CREAT) will always do ->open_context(). 3819 */ 3820 static int 3821 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 3822 int flags) 3823 { 3824 struct nfs4_label l, *ilabel = NULL; 3825 struct nfs_open_context *ctx; 3826 struct nfs4_state *state; 3827 int status = 0; 3828 3829 ctx = alloc_nfs_open_context(dentry, FMODE_READ); 3830 if (IS_ERR(ctx)) 3831 return PTR_ERR(ctx); 3832 3833 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); 3834 3835 sattr->ia_mode &= ~current_umask(); 3836 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL); 3837 if (IS_ERR(state)) { 3838 status = PTR_ERR(state); 3839 goto out; 3840 } 3841 out: 3842 nfs4_label_release_security(ilabel); 3843 put_nfs_open_context(ctx); 3844 return status; 3845 } 3846 3847 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name) 3848 { 3849 struct nfs_server *server = NFS_SERVER(dir); 3850 struct nfs_removeargs args = { 3851 .fh = NFS_FH(dir), 3852 .name = *name, 3853 }; 3854 struct nfs_removeres res = { 3855 .server = server, 3856 }; 3857 struct rpc_message msg = { 3858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], 3859 .rpc_argp = &args, 3860 .rpc_resp = &res, 3861 }; 3862 int status; 3863 3864 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); 3865 if (status == 0) 3866 update_changeattr(dir, &res.cinfo); 3867 return status; 3868 } 3869 3870 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name) 3871 { 3872 struct nfs4_exception exception = { }; 3873 int err; 3874 do { 3875 err = _nfs4_proc_remove(dir, name); 3876 trace_nfs4_remove(dir, name, err); 3877 err = nfs4_handle_exception(NFS_SERVER(dir), err, 3878 &exception); 3879 } while (exception.retry); 3880 return err; 3881 } 3882 3883 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir) 3884 { 3885 struct nfs_server *server = NFS_SERVER(dir); 3886 struct nfs_removeargs *args = msg->rpc_argp; 3887 struct nfs_removeres *res = msg->rpc_resp; 3888 3889 res->server = server; 3890 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; 3891 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1); 3892 3893 nfs_fattr_init(res->dir_attr); 3894 } 3895 3896 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) 3897 { 3898 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb), 3899 &data->args.seq_args, 3900 &data->res.seq_res, 3901 task); 3902 } 3903 3904 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) 3905 { 3906 struct nfs_unlinkdata *data = task->tk_calldata; 3907 struct nfs_removeres *res = &data->res; 3908 3909 if (!nfs4_sequence_done(task, &res->seq_res)) 3910 return 0; 3911 if (nfs4_async_handle_error(task, res->server, NULL, 3912 &data->timeout) == -EAGAIN) 3913 return 0; 3914 update_changeattr(dir, &res->cinfo); 3915 return 1; 3916 } 3917 3918 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir) 3919 { 3920 struct nfs_server *server = NFS_SERVER(dir); 3921 struct nfs_renameargs *arg = msg->rpc_argp; 3922 struct nfs_renameres *res = msg->rpc_resp; 3923 3924 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; 3925 res->server = server; 3926 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1); 3927 } 3928 3929 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) 3930 { 3931 nfs4_setup_sequence(NFS_SERVER(data->old_dir), 3932 &data->args.seq_args, 3933 &data->res.seq_res, 3934 task); 3935 } 3936 3937 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, 3938 struct inode *new_dir) 3939 { 3940 struct nfs_renamedata *data = task->tk_calldata; 3941 struct nfs_renameres *res = &data->res; 3942 3943 if (!nfs4_sequence_done(task, &res->seq_res)) 3944 return 0; 3945 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) 3946 return 0; 3947 3948 update_changeattr(old_dir, &res->old_cinfo); 3949 update_changeattr(new_dir, &res->new_cinfo); 3950 return 1; 3951 } 3952 3953 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) 3954 { 3955 struct nfs_server *server = NFS_SERVER(inode); 3956 struct nfs4_link_arg arg = { 3957 .fh = NFS_FH(inode), 3958 .dir_fh = NFS_FH(dir), 3959 .name = name, 3960 .bitmask = server->attr_bitmask, 3961 }; 3962 struct nfs4_link_res res = { 3963 .server = server, 3964 .label = NULL, 3965 }; 3966 struct rpc_message msg = { 3967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], 3968 .rpc_argp = &arg, 3969 .rpc_resp = &res, 3970 }; 3971 int status = -ENOMEM; 3972 3973 res.fattr = nfs_alloc_fattr(); 3974 if (res.fattr == NULL) 3975 goto out; 3976 3977 res.label = nfs4_label_alloc(server, GFP_KERNEL); 3978 if (IS_ERR(res.label)) { 3979 status = PTR_ERR(res.label); 3980 goto out; 3981 } 3982 arg.bitmask = nfs4_bitmask(server, res.label); 3983 3984 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 3985 if (!status) { 3986 update_changeattr(dir, &res.cinfo); 3987 status = nfs_post_op_update_inode(inode, res.fattr); 3988 if (!status) 3989 nfs_setsecurity(inode, res.fattr, res.label); 3990 } 3991 3992 3993 nfs4_label_free(res.label); 3994 3995 out: 3996 nfs_free_fattr(res.fattr); 3997 return status; 3998 } 3999 4000 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) 4001 { 4002 struct nfs4_exception exception = { }; 4003 int err; 4004 do { 4005 err = nfs4_handle_exception(NFS_SERVER(inode), 4006 _nfs4_proc_link(inode, dir, name), 4007 &exception); 4008 } while (exception.retry); 4009 return err; 4010 } 4011 4012 struct nfs4_createdata { 4013 struct rpc_message msg; 4014 struct nfs4_create_arg arg; 4015 struct nfs4_create_res res; 4016 struct nfs_fh fh; 4017 struct nfs_fattr fattr; 4018 struct nfs4_label *label; 4019 }; 4020 4021 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, 4022 const struct qstr *name, struct iattr *sattr, u32 ftype) 4023 { 4024 struct nfs4_createdata *data; 4025 4026 data = kzalloc(sizeof(*data), GFP_KERNEL); 4027 if (data != NULL) { 4028 struct nfs_server *server = NFS_SERVER(dir); 4029 4030 data->label = nfs4_label_alloc(server, GFP_KERNEL); 4031 if (IS_ERR(data->label)) 4032 goto out_free; 4033 4034 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; 4035 data->msg.rpc_argp = &data->arg; 4036 data->msg.rpc_resp = &data->res; 4037 data->arg.dir_fh = NFS_FH(dir); 4038 data->arg.server = server; 4039 data->arg.name = name; 4040 data->arg.attrs = sattr; 4041 data->arg.ftype = ftype; 4042 data->arg.bitmask = nfs4_bitmask(server, data->label); 4043 data->res.server = server; 4044 data->res.fh = &data->fh; 4045 data->res.fattr = &data->fattr; 4046 data->res.label = data->label; 4047 nfs_fattr_init(data->res.fattr); 4048 } 4049 return data; 4050 out_free: 4051 kfree(data); 4052 return NULL; 4053 } 4054 4055 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) 4056 { 4057 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, 4058 &data->arg.seq_args, &data->res.seq_res, 1); 4059 if (status == 0) { 4060 update_changeattr(dir, &data->res.dir_cinfo); 4061 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label); 4062 } 4063 return status; 4064 } 4065 4066 static void nfs4_free_createdata(struct nfs4_createdata *data) 4067 { 4068 nfs4_label_free(data->label); 4069 kfree(data); 4070 } 4071 4072 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 4073 struct page *page, unsigned int len, struct iattr *sattr, 4074 struct nfs4_label *label) 4075 { 4076 struct nfs4_createdata *data; 4077 int status = -ENAMETOOLONG; 4078 4079 if (len > NFS4_MAXPATHLEN) 4080 goto out; 4081 4082 status = -ENOMEM; 4083 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); 4084 if (data == NULL) 4085 goto out; 4086 4087 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; 4088 data->arg.u.symlink.pages = &page; 4089 data->arg.u.symlink.len = len; 4090 data->arg.label = label; 4091 4092 status = nfs4_do_create(dir, dentry, data); 4093 4094 nfs4_free_createdata(data); 4095 out: 4096 return status; 4097 } 4098 4099 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 4100 struct page *page, unsigned int len, struct iattr *sattr) 4101 { 4102 struct nfs4_exception exception = { }; 4103 struct nfs4_label l, *label = NULL; 4104 int err; 4105 4106 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4107 4108 do { 4109 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label); 4110 trace_nfs4_symlink(dir, &dentry->d_name, err); 4111 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4112 &exception); 4113 } while (exception.retry); 4114 4115 nfs4_label_release_security(label); 4116 return err; 4117 } 4118 4119 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 4120 struct iattr *sattr, struct nfs4_label *label) 4121 { 4122 struct nfs4_createdata *data; 4123 int status = -ENOMEM; 4124 4125 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); 4126 if (data == NULL) 4127 goto out; 4128 4129 data->arg.label = label; 4130 status = nfs4_do_create(dir, dentry, data); 4131 4132 nfs4_free_createdata(data); 4133 out: 4134 return status; 4135 } 4136 4137 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 4138 struct iattr *sattr) 4139 { 4140 struct nfs4_exception exception = { }; 4141 struct nfs4_label l, *label = NULL; 4142 int err; 4143 4144 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4145 4146 sattr->ia_mode &= ~current_umask(); 4147 do { 4148 err = _nfs4_proc_mkdir(dir, dentry, sattr, label); 4149 trace_nfs4_mkdir(dir, &dentry->d_name, err); 4150 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4151 &exception); 4152 } while (exception.retry); 4153 nfs4_label_release_security(label); 4154 4155 return err; 4156 } 4157 4158 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, 4159 u64 cookie, struct page **pages, unsigned int count, int plus) 4160 { 4161 struct inode *dir = d_inode(dentry); 4162 struct nfs4_readdir_arg args = { 4163 .fh = NFS_FH(dir), 4164 .pages = pages, 4165 .pgbase = 0, 4166 .count = count, 4167 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask, 4168 .plus = plus, 4169 }; 4170 struct nfs4_readdir_res res; 4171 struct rpc_message msg = { 4172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], 4173 .rpc_argp = &args, 4174 .rpc_resp = &res, 4175 .rpc_cred = cred, 4176 }; 4177 int status; 4178 4179 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__, 4180 dentry, 4181 (unsigned long long)cookie); 4182 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); 4183 res.pgbase = args.pgbase; 4184 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); 4185 if (status >= 0) { 4186 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); 4187 status += args.pgbase; 4188 } 4189 4190 nfs_invalidate_atime(dir); 4191 4192 dprintk("%s: returns %d\n", __func__, status); 4193 return status; 4194 } 4195 4196 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, 4197 u64 cookie, struct page **pages, unsigned int count, int plus) 4198 { 4199 struct nfs4_exception exception = { }; 4200 int err; 4201 do { 4202 err = _nfs4_proc_readdir(dentry, cred, cookie, 4203 pages, count, plus); 4204 trace_nfs4_readdir(d_inode(dentry), err); 4205 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err, 4206 &exception); 4207 } while (exception.retry); 4208 return err; 4209 } 4210 4211 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 4212 struct iattr *sattr, struct nfs4_label *label, dev_t rdev) 4213 { 4214 struct nfs4_createdata *data; 4215 int mode = sattr->ia_mode; 4216 int status = -ENOMEM; 4217 4218 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); 4219 if (data == NULL) 4220 goto out; 4221 4222 if (S_ISFIFO(mode)) 4223 data->arg.ftype = NF4FIFO; 4224 else if (S_ISBLK(mode)) { 4225 data->arg.ftype = NF4BLK; 4226 data->arg.u.device.specdata1 = MAJOR(rdev); 4227 data->arg.u.device.specdata2 = MINOR(rdev); 4228 } 4229 else if (S_ISCHR(mode)) { 4230 data->arg.ftype = NF4CHR; 4231 data->arg.u.device.specdata1 = MAJOR(rdev); 4232 data->arg.u.device.specdata2 = MINOR(rdev); 4233 } else if (!S_ISSOCK(mode)) { 4234 status = -EINVAL; 4235 goto out_free; 4236 } 4237 4238 data->arg.label = label; 4239 status = nfs4_do_create(dir, dentry, data); 4240 out_free: 4241 nfs4_free_createdata(data); 4242 out: 4243 return status; 4244 } 4245 4246 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 4247 struct iattr *sattr, dev_t rdev) 4248 { 4249 struct nfs4_exception exception = { }; 4250 struct nfs4_label l, *label = NULL; 4251 int err; 4252 4253 label = nfs4_label_init_security(dir, dentry, sattr, &l); 4254 4255 sattr->ia_mode &= ~current_umask(); 4256 do { 4257 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); 4258 trace_nfs4_mknod(dir, &dentry->d_name, err); 4259 err = nfs4_handle_exception(NFS_SERVER(dir), err, 4260 &exception); 4261 } while (exception.retry); 4262 4263 nfs4_label_release_security(label); 4264 4265 return err; 4266 } 4267 4268 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, 4269 struct nfs_fsstat *fsstat) 4270 { 4271 struct nfs4_statfs_arg args = { 4272 .fh = fhandle, 4273 .bitmask = server->attr_bitmask, 4274 }; 4275 struct nfs4_statfs_res res = { 4276 .fsstat = fsstat, 4277 }; 4278 struct rpc_message msg = { 4279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], 4280 .rpc_argp = &args, 4281 .rpc_resp = &res, 4282 }; 4283 4284 nfs_fattr_init(fsstat->fattr); 4285 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4286 } 4287 4288 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) 4289 { 4290 struct nfs4_exception exception = { }; 4291 int err; 4292 do { 4293 err = nfs4_handle_exception(server, 4294 _nfs4_proc_statfs(server, fhandle, fsstat), 4295 &exception); 4296 } while (exception.retry); 4297 return err; 4298 } 4299 4300 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, 4301 struct nfs_fsinfo *fsinfo) 4302 { 4303 struct nfs4_fsinfo_arg args = { 4304 .fh = fhandle, 4305 .bitmask = server->attr_bitmask, 4306 }; 4307 struct nfs4_fsinfo_res res = { 4308 .fsinfo = fsinfo, 4309 }; 4310 struct rpc_message msg = { 4311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], 4312 .rpc_argp = &args, 4313 .rpc_resp = &res, 4314 }; 4315 4316 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4317 } 4318 4319 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 4320 { 4321 struct nfs4_exception exception = { }; 4322 unsigned long now = jiffies; 4323 int err; 4324 4325 do { 4326 err = _nfs4_do_fsinfo(server, fhandle, fsinfo); 4327 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); 4328 if (err == 0) { 4329 nfs4_set_lease_period(server->nfs_client, 4330 fsinfo->lease_time * HZ, 4331 now); 4332 break; 4333 } 4334 err = nfs4_handle_exception(server, err, &exception); 4335 } while (exception.retry); 4336 return err; 4337 } 4338 4339 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) 4340 { 4341 int error; 4342 4343 nfs_fattr_init(fsinfo->fattr); 4344 error = nfs4_do_fsinfo(server, fhandle, fsinfo); 4345 if (error == 0) { 4346 /* block layout checks this! */ 4347 server->pnfs_blksize = fsinfo->blksize; 4348 set_pnfs_layoutdriver(server, fhandle, fsinfo); 4349 } 4350 4351 return error; 4352 } 4353 4354 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 4355 struct nfs_pathconf *pathconf) 4356 { 4357 struct nfs4_pathconf_arg args = { 4358 .fh = fhandle, 4359 .bitmask = server->attr_bitmask, 4360 }; 4361 struct nfs4_pathconf_res res = { 4362 .pathconf = pathconf, 4363 }; 4364 struct rpc_message msg = { 4365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], 4366 .rpc_argp = &args, 4367 .rpc_resp = &res, 4368 }; 4369 4370 /* None of the pathconf attributes are mandatory to implement */ 4371 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { 4372 memset(pathconf, 0, sizeof(*pathconf)); 4373 return 0; 4374 } 4375 4376 nfs_fattr_init(pathconf->fattr); 4377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 4378 } 4379 4380 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 4381 struct nfs_pathconf *pathconf) 4382 { 4383 struct nfs4_exception exception = { }; 4384 int err; 4385 4386 do { 4387 err = nfs4_handle_exception(server, 4388 _nfs4_proc_pathconf(server, fhandle, pathconf), 4389 &exception); 4390 } while (exception.retry); 4391 return err; 4392 } 4393 4394 int nfs4_set_rw_stateid(nfs4_stateid *stateid, 4395 const struct nfs_open_context *ctx, 4396 const struct nfs_lock_context *l_ctx, 4397 fmode_t fmode) 4398 { 4399 const struct nfs_lockowner *lockowner = NULL; 4400 4401 if (l_ctx != NULL) 4402 lockowner = &l_ctx->lockowner; 4403 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL); 4404 } 4405 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); 4406 4407 static bool nfs4_stateid_is_current(nfs4_stateid *stateid, 4408 const struct nfs_open_context *ctx, 4409 const struct nfs_lock_context *l_ctx, 4410 fmode_t fmode) 4411 { 4412 nfs4_stateid current_stateid; 4413 4414 /* If the current stateid represents a lost lock, then exit */ 4415 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO) 4416 return true; 4417 return nfs4_stateid_match(stateid, ¤t_stateid); 4418 } 4419 4420 static bool nfs4_error_stateid_expired(int err) 4421 { 4422 switch (err) { 4423 case -NFS4ERR_DELEG_REVOKED: 4424 case -NFS4ERR_ADMIN_REVOKED: 4425 case -NFS4ERR_BAD_STATEID: 4426 case -NFS4ERR_STALE_STATEID: 4427 case -NFS4ERR_OLD_STATEID: 4428 case -NFS4ERR_OPENMODE: 4429 case -NFS4ERR_EXPIRED: 4430 return true; 4431 } 4432 return false; 4433 } 4434 4435 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr) 4436 { 4437 nfs_invalidate_atime(hdr->inode); 4438 } 4439 4440 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) 4441 { 4442 struct nfs_server *server = NFS_SERVER(hdr->inode); 4443 4444 trace_nfs4_read(hdr, task->tk_status); 4445 if (nfs4_async_handle_error(task, server, 4446 hdr->args.context->state, 4447 NULL) == -EAGAIN) { 4448 rpc_restart_call_prepare(task); 4449 return -EAGAIN; 4450 } 4451 4452 __nfs4_read_done_cb(hdr); 4453 if (task->tk_status > 0) 4454 renew_lease(server, hdr->timestamp); 4455 return 0; 4456 } 4457 4458 static bool nfs4_read_stateid_changed(struct rpc_task *task, 4459 struct nfs_pgio_args *args) 4460 { 4461 4462 if (!nfs4_error_stateid_expired(task->tk_status) || 4463 nfs4_stateid_is_current(&args->stateid, 4464 args->context, 4465 args->lock_context, 4466 FMODE_READ)) 4467 return false; 4468 rpc_restart_call_prepare(task); 4469 return true; 4470 } 4471 4472 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 4473 { 4474 4475 dprintk("--> %s\n", __func__); 4476 4477 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 4478 return -EAGAIN; 4479 if (nfs4_read_stateid_changed(task, &hdr->args)) 4480 return -EAGAIN; 4481 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 4482 nfs4_read_done_cb(task, hdr); 4483 } 4484 4485 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, 4486 struct rpc_message *msg) 4487 { 4488 hdr->timestamp = jiffies; 4489 if (!hdr->pgio_done_cb) 4490 hdr->pgio_done_cb = nfs4_read_done_cb; 4491 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; 4492 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0); 4493 } 4494 4495 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, 4496 struct nfs_pgio_header *hdr) 4497 { 4498 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode), 4499 &hdr->args.seq_args, 4500 &hdr->res.seq_res, 4501 task)) 4502 return 0; 4503 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, 4504 hdr->args.lock_context, 4505 hdr->rw_ops->rw_mode) == -EIO) 4506 return -EIO; 4507 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) 4508 return -EIO; 4509 return 0; 4510 } 4511 4512 static int nfs4_write_done_cb(struct rpc_task *task, 4513 struct nfs_pgio_header *hdr) 4514 { 4515 struct inode *inode = hdr->inode; 4516 4517 trace_nfs4_write(hdr, task->tk_status); 4518 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 4519 hdr->args.context->state, 4520 NULL) == -EAGAIN) { 4521 rpc_restart_call_prepare(task); 4522 return -EAGAIN; 4523 } 4524 if (task->tk_status >= 0) { 4525 renew_lease(NFS_SERVER(inode), hdr->timestamp); 4526 nfs_writeback_update_inode(hdr); 4527 } 4528 return 0; 4529 } 4530 4531 static bool nfs4_write_stateid_changed(struct rpc_task *task, 4532 struct nfs_pgio_args *args) 4533 { 4534 4535 if (!nfs4_error_stateid_expired(task->tk_status) || 4536 nfs4_stateid_is_current(&args->stateid, 4537 args->context, 4538 args->lock_context, 4539 FMODE_WRITE)) 4540 return false; 4541 rpc_restart_call_prepare(task); 4542 return true; 4543 } 4544 4545 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) 4546 { 4547 if (!nfs4_sequence_done(task, &hdr->res.seq_res)) 4548 return -EAGAIN; 4549 if (nfs4_write_stateid_changed(task, &hdr->args)) 4550 return -EAGAIN; 4551 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : 4552 nfs4_write_done_cb(task, hdr); 4553 } 4554 4555 static 4556 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) 4557 { 4558 /* Don't request attributes for pNFS or O_DIRECT writes */ 4559 if (hdr->ds_clp != NULL || hdr->dreq != NULL) 4560 return false; 4561 /* Otherwise, request attributes if and only if we don't hold 4562 * a delegation 4563 */ 4564 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; 4565 } 4566 4567 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, 4568 struct rpc_message *msg) 4569 { 4570 struct nfs_server *server = NFS_SERVER(hdr->inode); 4571 4572 if (!nfs4_write_need_cache_consistency_data(hdr)) { 4573 hdr->args.bitmask = NULL; 4574 hdr->res.fattr = NULL; 4575 } else 4576 hdr->args.bitmask = server->cache_consistency_bitmask; 4577 4578 if (!hdr->pgio_done_cb) 4579 hdr->pgio_done_cb = nfs4_write_done_cb; 4580 hdr->res.server = server; 4581 hdr->timestamp = jiffies; 4582 4583 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; 4584 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1); 4585 } 4586 4587 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) 4588 { 4589 nfs4_setup_sequence(NFS_SERVER(data->inode), 4590 &data->args.seq_args, 4591 &data->res.seq_res, 4592 task); 4593 } 4594 4595 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) 4596 { 4597 struct inode *inode = data->inode; 4598 4599 trace_nfs4_commit(data, task->tk_status); 4600 if (nfs4_async_handle_error(task, NFS_SERVER(inode), 4601 NULL, NULL) == -EAGAIN) { 4602 rpc_restart_call_prepare(task); 4603 return -EAGAIN; 4604 } 4605 return 0; 4606 } 4607 4608 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) 4609 { 4610 if (!nfs4_sequence_done(task, &data->res.seq_res)) 4611 return -EAGAIN; 4612 return data->commit_done_cb(task, data); 4613 } 4614 4615 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg) 4616 { 4617 struct nfs_server *server = NFS_SERVER(data->inode); 4618 4619 if (data->commit_done_cb == NULL) 4620 data->commit_done_cb = nfs4_commit_done_cb; 4621 data->res.server = server; 4622 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; 4623 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); 4624 } 4625 4626 struct nfs4_renewdata { 4627 struct nfs_client *client; 4628 unsigned long timestamp; 4629 }; 4630 4631 /* 4632 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special 4633 * standalone procedure for queueing an asynchronous RENEW. 4634 */ 4635 static void nfs4_renew_release(void *calldata) 4636 { 4637 struct nfs4_renewdata *data = calldata; 4638 struct nfs_client *clp = data->client; 4639 4640 if (atomic_read(&clp->cl_count) > 1) 4641 nfs4_schedule_state_renewal(clp); 4642 nfs_put_client(clp); 4643 kfree(data); 4644 } 4645 4646 static void nfs4_renew_done(struct rpc_task *task, void *calldata) 4647 { 4648 struct nfs4_renewdata *data = calldata; 4649 struct nfs_client *clp = data->client; 4650 unsigned long timestamp = data->timestamp; 4651 4652 trace_nfs4_renew_async(clp, task->tk_status); 4653 switch (task->tk_status) { 4654 case 0: 4655 break; 4656 case -NFS4ERR_LEASE_MOVED: 4657 nfs4_schedule_lease_moved_recovery(clp); 4658 break; 4659 default: 4660 /* Unless we're shutting down, schedule state recovery! */ 4661 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) 4662 return; 4663 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { 4664 nfs4_schedule_lease_recovery(clp); 4665 return; 4666 } 4667 nfs4_schedule_path_down_recovery(clp); 4668 } 4669 do_renew_lease(clp, timestamp); 4670 } 4671 4672 static const struct rpc_call_ops nfs4_renew_ops = { 4673 .rpc_call_done = nfs4_renew_done, 4674 .rpc_release = nfs4_renew_release, 4675 }; 4676 4677 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) 4678 { 4679 struct rpc_message msg = { 4680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 4681 .rpc_argp = clp, 4682 .rpc_cred = cred, 4683 }; 4684 struct nfs4_renewdata *data; 4685 4686 if (renew_flags == 0) 4687 return 0; 4688 if (!atomic_inc_not_zero(&clp->cl_count)) 4689 return -EIO; 4690 data = kmalloc(sizeof(*data), GFP_NOFS); 4691 if (data == NULL) 4692 return -ENOMEM; 4693 data->client = clp; 4694 data->timestamp = jiffies; 4695 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, 4696 &nfs4_renew_ops, data); 4697 } 4698 4699 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) 4700 { 4701 struct rpc_message msg = { 4702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], 4703 .rpc_argp = clp, 4704 .rpc_cred = cred, 4705 }; 4706 unsigned long now = jiffies; 4707 int status; 4708 4709 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 4710 if (status < 0) 4711 return status; 4712 do_renew_lease(clp, now); 4713 return 0; 4714 } 4715 4716 static inline int nfs4_server_supports_acls(struct nfs_server *server) 4717 { 4718 return server->caps & NFS_CAP_ACLS; 4719 } 4720 4721 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that 4722 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on 4723 * the stack. 4724 */ 4725 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) 4726 4727 static int buf_to_pages_noslab(const void *buf, size_t buflen, 4728 struct page **pages) 4729 { 4730 struct page *newpage, **spages; 4731 int rc = 0; 4732 size_t len; 4733 spages = pages; 4734 4735 do { 4736 len = min_t(size_t, PAGE_SIZE, buflen); 4737 newpage = alloc_page(GFP_KERNEL); 4738 4739 if (newpage == NULL) 4740 goto unwind; 4741 memcpy(page_address(newpage), buf, len); 4742 buf += len; 4743 buflen -= len; 4744 *pages++ = newpage; 4745 rc++; 4746 } while (buflen != 0); 4747 4748 return rc; 4749 4750 unwind: 4751 for(; rc > 0; rc--) 4752 __free_page(spages[rc-1]); 4753 return -ENOMEM; 4754 } 4755 4756 struct nfs4_cached_acl { 4757 int cached; 4758 size_t len; 4759 char data[0]; 4760 }; 4761 4762 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) 4763 { 4764 struct nfs_inode *nfsi = NFS_I(inode); 4765 4766 spin_lock(&inode->i_lock); 4767 kfree(nfsi->nfs4_acl); 4768 nfsi->nfs4_acl = acl; 4769 spin_unlock(&inode->i_lock); 4770 } 4771 4772 static void nfs4_zap_acl_attr(struct inode *inode) 4773 { 4774 nfs4_set_cached_acl(inode, NULL); 4775 } 4776 4777 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) 4778 { 4779 struct nfs_inode *nfsi = NFS_I(inode); 4780 struct nfs4_cached_acl *acl; 4781 int ret = -ENOENT; 4782 4783 spin_lock(&inode->i_lock); 4784 acl = nfsi->nfs4_acl; 4785 if (acl == NULL) 4786 goto out; 4787 if (buf == NULL) /* user is just asking for length */ 4788 goto out_len; 4789 if (acl->cached == 0) 4790 goto out; 4791 ret = -ERANGE; /* see getxattr(2) man page */ 4792 if (acl->len > buflen) 4793 goto out; 4794 memcpy(buf, acl->data, acl->len); 4795 out_len: 4796 ret = acl->len; 4797 out: 4798 spin_unlock(&inode->i_lock); 4799 return ret; 4800 } 4801 4802 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) 4803 { 4804 struct nfs4_cached_acl *acl; 4805 size_t buflen = sizeof(*acl) + acl_len; 4806 4807 if (buflen <= PAGE_SIZE) { 4808 acl = kmalloc(buflen, GFP_KERNEL); 4809 if (acl == NULL) 4810 goto out; 4811 acl->cached = 1; 4812 _copy_from_pages(acl->data, pages, pgbase, acl_len); 4813 } else { 4814 acl = kmalloc(sizeof(*acl), GFP_KERNEL); 4815 if (acl == NULL) 4816 goto out; 4817 acl->cached = 0; 4818 } 4819 acl->len = acl_len; 4820 out: 4821 nfs4_set_cached_acl(inode, acl); 4822 } 4823 4824 /* 4825 * The getxattr API returns the required buffer length when called with a 4826 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating 4827 * the required buf. On a NULL buf, we send a page of data to the server 4828 * guessing that the ACL request can be serviced by a page. If so, we cache 4829 * up to the page of ACL data, and the 2nd call to getxattr is serviced by 4830 * the cache. If not so, we throw away the page, and cache the required 4831 * length. The next getxattr call will then produce another round trip to 4832 * the server, this time with the input buf of the required size. 4833 */ 4834 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 4835 { 4836 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, }; 4837 struct nfs_getaclargs args = { 4838 .fh = NFS_FH(inode), 4839 .acl_pages = pages, 4840 .acl_len = buflen, 4841 }; 4842 struct nfs_getaclres res = { 4843 .acl_len = buflen, 4844 }; 4845 struct rpc_message msg = { 4846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], 4847 .rpc_argp = &args, 4848 .rpc_resp = &res, 4849 }; 4850 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 4851 int ret = -ENOMEM, i; 4852 4853 /* As long as we're doing a round trip to the server anyway, 4854 * let's be prepared for a page of acl data. */ 4855 if (npages == 0) 4856 npages = 1; 4857 if (npages > ARRAY_SIZE(pages)) 4858 return -ERANGE; 4859 4860 for (i = 0; i < npages; i++) { 4861 pages[i] = alloc_page(GFP_KERNEL); 4862 if (!pages[i]) 4863 goto out_free; 4864 } 4865 4866 /* for decoding across pages */ 4867 res.acl_scratch = alloc_page(GFP_KERNEL); 4868 if (!res.acl_scratch) 4869 goto out_free; 4870 4871 args.acl_len = npages * PAGE_SIZE; 4872 4873 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", 4874 __func__, buf, buflen, npages, args.acl_len); 4875 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), 4876 &msg, &args.seq_args, &res.seq_res, 0); 4877 if (ret) 4878 goto out_free; 4879 4880 /* Handle the case where the passed-in buffer is too short */ 4881 if (res.acl_flags & NFS4_ACL_TRUNC) { 4882 /* Did the user only issue a request for the acl length? */ 4883 if (buf == NULL) 4884 goto out_ok; 4885 ret = -ERANGE; 4886 goto out_free; 4887 } 4888 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); 4889 if (buf) { 4890 if (res.acl_len > buflen) { 4891 ret = -ERANGE; 4892 goto out_free; 4893 } 4894 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); 4895 } 4896 out_ok: 4897 ret = res.acl_len; 4898 out_free: 4899 for (i = 0; i < npages; i++) 4900 if (pages[i]) 4901 __free_page(pages[i]); 4902 if (res.acl_scratch) 4903 __free_page(res.acl_scratch); 4904 return ret; 4905 } 4906 4907 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) 4908 { 4909 struct nfs4_exception exception = { }; 4910 ssize_t ret; 4911 do { 4912 ret = __nfs4_get_acl_uncached(inode, buf, buflen); 4913 trace_nfs4_get_acl(inode, ret); 4914 if (ret >= 0) 4915 break; 4916 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); 4917 } while (exception.retry); 4918 return ret; 4919 } 4920 4921 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) 4922 { 4923 struct nfs_server *server = NFS_SERVER(inode); 4924 int ret; 4925 4926 if (!nfs4_server_supports_acls(server)) 4927 return -EOPNOTSUPP; 4928 ret = nfs_revalidate_inode(server, inode); 4929 if (ret < 0) 4930 return ret; 4931 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) 4932 nfs_zap_acl_cache(inode); 4933 ret = nfs4_read_cached_acl(inode, buf, buflen); 4934 if (ret != -ENOENT) 4935 /* -ENOENT is returned if there is no ACL or if there is an ACL 4936 * but no cached acl data, just the acl length */ 4937 return ret; 4938 return nfs4_get_acl_uncached(inode, buf, buflen); 4939 } 4940 4941 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 4942 { 4943 struct nfs_server *server = NFS_SERVER(inode); 4944 struct page *pages[NFS4ACL_MAXPAGES]; 4945 struct nfs_setaclargs arg = { 4946 .fh = NFS_FH(inode), 4947 .acl_pages = pages, 4948 .acl_len = buflen, 4949 }; 4950 struct nfs_setaclres res; 4951 struct rpc_message msg = { 4952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], 4953 .rpc_argp = &arg, 4954 .rpc_resp = &res, 4955 }; 4956 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); 4957 int ret, i; 4958 4959 if (!nfs4_server_supports_acls(server)) 4960 return -EOPNOTSUPP; 4961 if (npages > ARRAY_SIZE(pages)) 4962 return -ERANGE; 4963 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages); 4964 if (i < 0) 4965 return i; 4966 nfs4_inode_return_delegation(inode); 4967 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 4968 4969 /* 4970 * Free each page after tx, so the only ref left is 4971 * held by the network stack 4972 */ 4973 for (; i > 0; i--) 4974 put_page(pages[i-1]); 4975 4976 /* 4977 * Acl update can result in inode attribute update. 4978 * so mark the attribute cache invalid. 4979 */ 4980 spin_lock(&inode->i_lock); 4981 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; 4982 spin_unlock(&inode->i_lock); 4983 nfs_access_zap_cache(inode); 4984 nfs_zap_acl_cache(inode); 4985 return ret; 4986 } 4987 4988 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) 4989 { 4990 struct nfs4_exception exception = { }; 4991 int err; 4992 do { 4993 err = __nfs4_proc_set_acl(inode, buf, buflen); 4994 trace_nfs4_set_acl(inode, err); 4995 err = nfs4_handle_exception(NFS_SERVER(inode), err, 4996 &exception); 4997 } while (exception.retry); 4998 return err; 4999 } 5000 5001 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 5002 static int _nfs4_get_security_label(struct inode *inode, void *buf, 5003 size_t buflen) 5004 { 5005 struct nfs_server *server = NFS_SERVER(inode); 5006 struct nfs_fattr fattr; 5007 struct nfs4_label label = {0, 0, buflen, buf}; 5008 5009 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 5010 struct nfs4_getattr_arg arg = { 5011 .fh = NFS_FH(inode), 5012 .bitmask = bitmask, 5013 }; 5014 struct nfs4_getattr_res res = { 5015 .fattr = &fattr, 5016 .label = &label, 5017 .server = server, 5018 }; 5019 struct rpc_message msg = { 5020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], 5021 .rpc_argp = &arg, 5022 .rpc_resp = &res, 5023 }; 5024 int ret; 5025 5026 nfs_fattr_init(&fattr); 5027 5028 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); 5029 if (ret) 5030 return ret; 5031 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) 5032 return -ENOENT; 5033 if (buflen < label.len) 5034 return -ERANGE; 5035 return 0; 5036 } 5037 5038 static int nfs4_get_security_label(struct inode *inode, void *buf, 5039 size_t buflen) 5040 { 5041 struct nfs4_exception exception = { }; 5042 int err; 5043 5044 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 5045 return -EOPNOTSUPP; 5046 5047 do { 5048 err = _nfs4_get_security_label(inode, buf, buflen); 5049 trace_nfs4_get_security_label(inode, err); 5050 err = nfs4_handle_exception(NFS_SERVER(inode), err, 5051 &exception); 5052 } while (exception.retry); 5053 return err; 5054 } 5055 5056 static int _nfs4_do_set_security_label(struct inode *inode, 5057 struct nfs4_label *ilabel, 5058 struct nfs_fattr *fattr, 5059 struct nfs4_label *olabel) 5060 { 5061 5062 struct iattr sattr = {0}; 5063 struct nfs_server *server = NFS_SERVER(inode); 5064 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; 5065 struct nfs_setattrargs arg = { 5066 .fh = NFS_FH(inode), 5067 .iap = &sattr, 5068 .server = server, 5069 .bitmask = bitmask, 5070 .label = ilabel, 5071 }; 5072 struct nfs_setattrres res = { 5073 .fattr = fattr, 5074 .label = olabel, 5075 .server = server, 5076 }; 5077 struct rpc_message msg = { 5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 5079 .rpc_argp = &arg, 5080 .rpc_resp = &res, 5081 }; 5082 int status; 5083 5084 nfs4_stateid_copy(&arg.stateid, &zero_stateid); 5085 5086 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 5087 if (status) 5088 dprintk("%s failed: %d\n", __func__, status); 5089 5090 return status; 5091 } 5092 5093 static int nfs4_do_set_security_label(struct inode *inode, 5094 struct nfs4_label *ilabel, 5095 struct nfs_fattr *fattr, 5096 struct nfs4_label *olabel) 5097 { 5098 struct nfs4_exception exception = { }; 5099 int err; 5100 5101 do { 5102 err = _nfs4_do_set_security_label(inode, ilabel, 5103 fattr, olabel); 5104 trace_nfs4_set_security_label(inode, err); 5105 err = nfs4_handle_exception(NFS_SERVER(inode), err, 5106 &exception); 5107 } while (exception.retry); 5108 return err; 5109 } 5110 5111 static int 5112 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen) 5113 { 5114 struct nfs4_label ilabel, *olabel = NULL; 5115 struct nfs_fattr fattr; 5116 struct rpc_cred *cred; 5117 int status; 5118 5119 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) 5120 return -EOPNOTSUPP; 5121 5122 nfs_fattr_init(&fattr); 5123 5124 ilabel.pi = 0; 5125 ilabel.lfs = 0; 5126 ilabel.label = (char *)buf; 5127 ilabel.len = buflen; 5128 5129 cred = rpc_lookup_cred(); 5130 if (IS_ERR(cred)) 5131 return PTR_ERR(cred); 5132 5133 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 5134 if (IS_ERR(olabel)) { 5135 status = -PTR_ERR(olabel); 5136 goto out; 5137 } 5138 5139 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel); 5140 if (status == 0) 5141 nfs_setsecurity(inode, &fattr, olabel); 5142 5143 nfs4_label_free(olabel); 5144 out: 5145 put_rpccred(cred); 5146 return status; 5147 } 5148 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ 5149 5150 5151 static void nfs4_init_boot_verifier(const struct nfs_client *clp, 5152 nfs4_verifier *bootverf) 5153 { 5154 __be32 verf[2]; 5155 5156 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { 5157 /* An impossible timestamp guarantees this value 5158 * will never match a generated boot time. */ 5159 verf[0] = 0; 5160 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1); 5161 } else { 5162 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 5163 verf[0] = cpu_to_be32(nn->boot_time.tv_sec); 5164 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec); 5165 } 5166 memcpy(bootverf->data, verf, sizeof(bootverf->data)); 5167 } 5168 5169 static int 5170 nfs4_init_nonuniform_client_string(struct nfs_client *clp) 5171 { 5172 size_t len; 5173 char *str; 5174 5175 if (clp->cl_owner_id != NULL) 5176 return 0; 5177 5178 rcu_read_lock(); 5179 len = 14 + strlen(clp->cl_ipaddr) + 1 + 5180 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + 5181 1 + 5182 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) + 5183 1; 5184 rcu_read_unlock(); 5185 5186 if (len > NFS4_OPAQUE_LIMIT + 1) 5187 return -EINVAL; 5188 5189 /* 5190 * Since this string is allocated at mount time, and held until the 5191 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5192 * about a memory-reclaim deadlock. 5193 */ 5194 str = kmalloc(len, GFP_KERNEL); 5195 if (!str) 5196 return -ENOMEM; 5197 5198 rcu_read_lock(); 5199 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s", 5200 clp->cl_ipaddr, 5201 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR), 5202 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)); 5203 rcu_read_unlock(); 5204 5205 clp->cl_owner_id = str; 5206 return 0; 5207 } 5208 5209 static int 5210 nfs4_init_uniquifier_client_string(struct nfs_client *clp) 5211 { 5212 size_t len; 5213 char *str; 5214 5215 len = 10 + 10 + 1 + 10 + 1 + 5216 strlen(nfs4_client_id_uniquifier) + 1 + 5217 strlen(clp->cl_rpcclient->cl_nodename) + 1; 5218 5219 if (len > NFS4_OPAQUE_LIMIT + 1) 5220 return -EINVAL; 5221 5222 /* 5223 * Since this string is allocated at mount time, and held until the 5224 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5225 * about a memory-reclaim deadlock. 5226 */ 5227 str = kmalloc(len, GFP_KERNEL); 5228 if (!str) 5229 return -ENOMEM; 5230 5231 scnprintf(str, len, "Linux NFSv%u.%u %s/%s", 5232 clp->rpc_ops->version, clp->cl_minorversion, 5233 nfs4_client_id_uniquifier, 5234 clp->cl_rpcclient->cl_nodename); 5235 clp->cl_owner_id = str; 5236 return 0; 5237 } 5238 5239 static int 5240 nfs4_init_uniform_client_string(struct nfs_client *clp) 5241 { 5242 size_t len; 5243 char *str; 5244 5245 if (clp->cl_owner_id != NULL) 5246 return 0; 5247 5248 if (nfs4_client_id_uniquifier[0] != '\0') 5249 return nfs4_init_uniquifier_client_string(clp); 5250 5251 len = 10 + 10 + 1 + 10 + 1 + 5252 strlen(clp->cl_rpcclient->cl_nodename) + 1; 5253 5254 if (len > NFS4_OPAQUE_LIMIT + 1) 5255 return -EINVAL; 5256 5257 /* 5258 * Since this string is allocated at mount time, and held until the 5259 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying 5260 * about a memory-reclaim deadlock. 5261 */ 5262 str = kmalloc(len, GFP_KERNEL); 5263 if (!str) 5264 return -ENOMEM; 5265 5266 scnprintf(str, len, "Linux NFSv%u.%u %s", 5267 clp->rpc_ops->version, clp->cl_minorversion, 5268 clp->cl_rpcclient->cl_nodename); 5269 clp->cl_owner_id = str; 5270 return 0; 5271 } 5272 5273 /* 5274 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback 5275 * services. Advertise one based on the address family of the 5276 * clientaddr. 5277 */ 5278 static unsigned int 5279 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) 5280 { 5281 if (strchr(clp->cl_ipaddr, ':') != NULL) 5282 return scnprintf(buf, len, "tcp6"); 5283 else 5284 return scnprintf(buf, len, "tcp"); 5285 } 5286 5287 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) 5288 { 5289 struct nfs4_setclientid *sc = calldata; 5290 5291 if (task->tk_status == 0) 5292 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); 5293 } 5294 5295 static const struct rpc_call_ops nfs4_setclientid_ops = { 5296 .rpc_call_done = nfs4_setclientid_done, 5297 }; 5298 5299 /** 5300 * nfs4_proc_setclientid - Negotiate client ID 5301 * @clp: state data structure 5302 * @program: RPC program for NFSv4 callback service 5303 * @port: IP port number for NFS4 callback service 5304 * @cred: RPC credential to use for this call 5305 * @res: where to place the result 5306 * 5307 * Returns zero, a negative errno, or a negative NFS4ERR status code. 5308 */ 5309 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, 5310 unsigned short port, struct rpc_cred *cred, 5311 struct nfs4_setclientid_res *res) 5312 { 5313 nfs4_verifier sc_verifier; 5314 struct nfs4_setclientid setclientid = { 5315 .sc_verifier = &sc_verifier, 5316 .sc_prog = program, 5317 .sc_clnt = clp, 5318 }; 5319 struct rpc_message msg = { 5320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], 5321 .rpc_argp = &setclientid, 5322 .rpc_resp = res, 5323 .rpc_cred = cred, 5324 }; 5325 struct rpc_task *task; 5326 struct rpc_task_setup task_setup_data = { 5327 .rpc_client = clp->cl_rpcclient, 5328 .rpc_message = &msg, 5329 .callback_ops = &nfs4_setclientid_ops, 5330 .callback_data = &setclientid, 5331 .flags = RPC_TASK_TIMEOUT, 5332 }; 5333 int status; 5334 5335 /* nfs_client_id4 */ 5336 nfs4_init_boot_verifier(clp, &sc_verifier); 5337 5338 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) 5339 status = nfs4_init_uniform_client_string(clp); 5340 else 5341 status = nfs4_init_nonuniform_client_string(clp); 5342 5343 if (status) 5344 goto out; 5345 5346 /* cb_client4 */ 5347 setclientid.sc_netid_len = 5348 nfs4_init_callback_netid(clp, 5349 setclientid.sc_netid, 5350 sizeof(setclientid.sc_netid)); 5351 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, 5352 sizeof(setclientid.sc_uaddr), "%s.%u.%u", 5353 clp->cl_ipaddr, port >> 8, port & 255); 5354 5355 dprintk("NFS call setclientid auth=%s, '%s'\n", 5356 clp->cl_rpcclient->cl_auth->au_ops->au_name, 5357 clp->cl_owner_id); 5358 task = rpc_run_task(&task_setup_data); 5359 if (IS_ERR(task)) { 5360 status = PTR_ERR(task); 5361 goto out; 5362 } 5363 status = task->tk_status; 5364 if (setclientid.sc_cred) { 5365 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); 5366 put_rpccred(setclientid.sc_cred); 5367 } 5368 rpc_put_task(task); 5369 out: 5370 trace_nfs4_setclientid(clp, status); 5371 dprintk("NFS reply setclientid: %d\n", status); 5372 return status; 5373 } 5374 5375 /** 5376 * nfs4_proc_setclientid_confirm - Confirm client ID 5377 * @clp: state data structure 5378 * @res: result of a previous SETCLIENTID 5379 * @cred: RPC credential to use for this call 5380 * 5381 * Returns zero, a negative errno, or a negative NFS4ERR status code. 5382 */ 5383 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, 5384 struct nfs4_setclientid_res *arg, 5385 struct rpc_cred *cred) 5386 { 5387 struct rpc_message msg = { 5388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], 5389 .rpc_argp = arg, 5390 .rpc_cred = cred, 5391 }; 5392 int status; 5393 5394 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", 5395 clp->cl_rpcclient->cl_auth->au_ops->au_name, 5396 clp->cl_clientid); 5397 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 5398 trace_nfs4_setclientid_confirm(clp, status); 5399 dprintk("NFS reply setclientid_confirm: %d\n", status); 5400 return status; 5401 } 5402 5403 struct nfs4_delegreturndata { 5404 struct nfs4_delegreturnargs args; 5405 struct nfs4_delegreturnres res; 5406 struct nfs_fh fh; 5407 nfs4_stateid stateid; 5408 unsigned long timestamp; 5409 struct nfs_fattr fattr; 5410 int rpc_status; 5411 struct inode *inode; 5412 bool roc; 5413 u32 roc_barrier; 5414 }; 5415 5416 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) 5417 { 5418 struct nfs4_delegreturndata *data = calldata; 5419 5420 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5421 return; 5422 5423 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); 5424 switch (task->tk_status) { 5425 case 0: 5426 renew_lease(data->res.server, data->timestamp); 5427 case -NFS4ERR_ADMIN_REVOKED: 5428 case -NFS4ERR_DELEG_REVOKED: 5429 case -NFS4ERR_BAD_STATEID: 5430 case -NFS4ERR_OLD_STATEID: 5431 case -NFS4ERR_STALE_STATEID: 5432 case -NFS4ERR_EXPIRED: 5433 task->tk_status = 0; 5434 if (data->roc) 5435 pnfs_roc_set_barrier(data->inode, data->roc_barrier); 5436 break; 5437 default: 5438 if (nfs4_async_handle_error(task, data->res.server, 5439 NULL, NULL) == -EAGAIN) { 5440 rpc_restart_call_prepare(task); 5441 return; 5442 } 5443 } 5444 data->rpc_status = task->tk_status; 5445 } 5446 5447 static void nfs4_delegreturn_release(void *calldata) 5448 { 5449 struct nfs4_delegreturndata *data = calldata; 5450 struct inode *inode = data->inode; 5451 5452 if (inode) { 5453 if (data->roc) 5454 pnfs_roc_release(inode); 5455 nfs_iput_and_deactive(inode); 5456 } 5457 kfree(calldata); 5458 } 5459 5460 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) 5461 { 5462 struct nfs4_delegreturndata *d_data; 5463 5464 d_data = (struct nfs4_delegreturndata *)data; 5465 5466 if (nfs4_wait_on_layoutreturn(d_data->inode, task)) 5467 return; 5468 5469 if (d_data->roc) 5470 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier); 5471 5472 nfs4_setup_sequence(d_data->res.server, 5473 &d_data->args.seq_args, 5474 &d_data->res.seq_res, 5475 task); 5476 } 5477 5478 static const struct rpc_call_ops nfs4_delegreturn_ops = { 5479 .rpc_call_prepare = nfs4_delegreturn_prepare, 5480 .rpc_call_done = nfs4_delegreturn_done, 5481 .rpc_release = nfs4_delegreturn_release, 5482 }; 5483 5484 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) 5485 { 5486 struct nfs4_delegreturndata *data; 5487 struct nfs_server *server = NFS_SERVER(inode); 5488 struct rpc_task *task; 5489 struct rpc_message msg = { 5490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], 5491 .rpc_cred = cred, 5492 }; 5493 struct rpc_task_setup task_setup_data = { 5494 .rpc_client = server->client, 5495 .rpc_message = &msg, 5496 .callback_ops = &nfs4_delegreturn_ops, 5497 .flags = RPC_TASK_ASYNC, 5498 }; 5499 int status = 0; 5500 5501 data = kzalloc(sizeof(*data), GFP_NOFS); 5502 if (data == NULL) 5503 return -ENOMEM; 5504 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); 5505 5506 nfs4_state_protect(server->nfs_client, 5507 NFS_SP4_MACH_CRED_CLEANUP, 5508 &task_setup_data.rpc_client, &msg); 5509 5510 data->args.fhandle = &data->fh; 5511 data->args.stateid = &data->stateid; 5512 data->args.bitmask = server->cache_consistency_bitmask; 5513 nfs_copy_fh(&data->fh, NFS_FH(inode)); 5514 nfs4_stateid_copy(&data->stateid, stateid); 5515 data->res.fattr = &data->fattr; 5516 data->res.server = server; 5517 nfs_fattr_init(data->res.fattr); 5518 data->timestamp = jiffies; 5519 data->rpc_status = 0; 5520 data->inode = nfs_igrab_and_active(inode); 5521 if (data->inode) 5522 data->roc = nfs4_roc(inode); 5523 5524 task_setup_data.callback_data = data; 5525 msg.rpc_argp = &data->args; 5526 msg.rpc_resp = &data->res; 5527 task = rpc_run_task(&task_setup_data); 5528 if (IS_ERR(task)) 5529 return PTR_ERR(task); 5530 if (!issync) 5531 goto out; 5532 status = nfs4_wait_for_completion_rpc_task(task); 5533 if (status != 0) 5534 goto out; 5535 status = data->rpc_status; 5536 if (status == 0) 5537 nfs_post_op_update_inode_force_wcc(inode, &data->fattr); 5538 else 5539 nfs_refresh_inode(inode, &data->fattr); 5540 out: 5541 rpc_put_task(task); 5542 return status; 5543 } 5544 5545 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) 5546 { 5547 struct nfs_server *server = NFS_SERVER(inode); 5548 struct nfs4_exception exception = { }; 5549 int err; 5550 do { 5551 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); 5552 trace_nfs4_delegreturn(inode, stateid, err); 5553 switch (err) { 5554 case -NFS4ERR_STALE_STATEID: 5555 case -NFS4ERR_EXPIRED: 5556 case 0: 5557 return 0; 5558 } 5559 err = nfs4_handle_exception(server, err, &exception); 5560 } while (exception.retry); 5561 return err; 5562 } 5563 5564 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 5565 { 5566 struct inode *inode = state->inode; 5567 struct nfs_server *server = NFS_SERVER(inode); 5568 struct nfs_client *clp = server->nfs_client; 5569 struct nfs_lockt_args arg = { 5570 .fh = NFS_FH(inode), 5571 .fl = request, 5572 }; 5573 struct nfs_lockt_res res = { 5574 .denied = request, 5575 }; 5576 struct rpc_message msg = { 5577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], 5578 .rpc_argp = &arg, 5579 .rpc_resp = &res, 5580 .rpc_cred = state->owner->so_cred, 5581 }; 5582 struct nfs4_lock_state *lsp; 5583 int status; 5584 5585 arg.lock_owner.clientid = clp->cl_clientid; 5586 status = nfs4_set_lock_state(state, request); 5587 if (status != 0) 5588 goto out; 5589 lsp = request->fl_u.nfs4_fl.owner; 5590 arg.lock_owner.id = lsp->ls_seqid.owner_id; 5591 arg.lock_owner.s_dev = server->s_dev; 5592 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); 5593 switch (status) { 5594 case 0: 5595 request->fl_type = F_UNLCK; 5596 break; 5597 case -NFS4ERR_DENIED: 5598 status = 0; 5599 } 5600 request->fl_ops->fl_release_private(request); 5601 request->fl_ops = NULL; 5602 out: 5603 return status; 5604 } 5605 5606 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) 5607 { 5608 struct nfs4_exception exception = { }; 5609 int err; 5610 5611 do { 5612 err = _nfs4_proc_getlk(state, cmd, request); 5613 trace_nfs4_get_lock(request, state, cmd, err); 5614 err = nfs4_handle_exception(NFS_SERVER(state->inode), err, 5615 &exception); 5616 } while (exception.retry); 5617 return err; 5618 } 5619 5620 struct nfs4_unlockdata { 5621 struct nfs_locku_args arg; 5622 struct nfs_locku_res res; 5623 struct nfs4_lock_state *lsp; 5624 struct nfs_open_context *ctx; 5625 struct file_lock fl; 5626 struct nfs_server *server; 5627 unsigned long timestamp; 5628 }; 5629 5630 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, 5631 struct nfs_open_context *ctx, 5632 struct nfs4_lock_state *lsp, 5633 struct nfs_seqid *seqid) 5634 { 5635 struct nfs4_unlockdata *p; 5636 struct inode *inode = lsp->ls_state->inode; 5637 5638 p = kzalloc(sizeof(*p), GFP_NOFS); 5639 if (p == NULL) 5640 return NULL; 5641 p->arg.fh = NFS_FH(inode); 5642 p->arg.fl = &p->fl; 5643 p->arg.seqid = seqid; 5644 p->res.seqid = seqid; 5645 p->lsp = lsp; 5646 atomic_inc(&lsp->ls_count); 5647 /* Ensure we don't close file until we're done freeing locks! */ 5648 p->ctx = get_nfs_open_context(ctx); 5649 memcpy(&p->fl, fl, sizeof(p->fl)); 5650 p->server = NFS_SERVER(inode); 5651 return p; 5652 } 5653 5654 static void nfs4_locku_release_calldata(void *data) 5655 { 5656 struct nfs4_unlockdata *calldata = data; 5657 nfs_free_seqid(calldata->arg.seqid); 5658 nfs4_put_lock_state(calldata->lsp); 5659 put_nfs_open_context(calldata->ctx); 5660 kfree(calldata); 5661 } 5662 5663 static void nfs4_locku_done(struct rpc_task *task, void *data) 5664 { 5665 struct nfs4_unlockdata *calldata = data; 5666 5667 if (!nfs4_sequence_done(task, &calldata->res.seq_res)) 5668 return; 5669 switch (task->tk_status) { 5670 case 0: 5671 renew_lease(calldata->server, calldata->timestamp); 5672 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl); 5673 if (nfs4_update_lock_stateid(calldata->lsp, 5674 &calldata->res.stateid)) 5675 break; 5676 case -NFS4ERR_BAD_STATEID: 5677 case -NFS4ERR_OLD_STATEID: 5678 case -NFS4ERR_STALE_STATEID: 5679 case -NFS4ERR_EXPIRED: 5680 if (!nfs4_stateid_match(&calldata->arg.stateid, 5681 &calldata->lsp->ls_stateid)) 5682 rpc_restart_call_prepare(task); 5683 break; 5684 default: 5685 if (nfs4_async_handle_error(task, calldata->server, 5686 NULL, NULL) == -EAGAIN) 5687 rpc_restart_call_prepare(task); 5688 } 5689 nfs_release_seqid(calldata->arg.seqid); 5690 } 5691 5692 static void nfs4_locku_prepare(struct rpc_task *task, void *data) 5693 { 5694 struct nfs4_unlockdata *calldata = data; 5695 5696 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) 5697 goto out_wait; 5698 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid); 5699 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { 5700 /* Note: exit _without_ running nfs4_locku_done */ 5701 goto out_no_action; 5702 } 5703 calldata->timestamp = jiffies; 5704 if (nfs4_setup_sequence(calldata->server, 5705 &calldata->arg.seq_args, 5706 &calldata->res.seq_res, 5707 task) != 0) 5708 nfs_release_seqid(calldata->arg.seqid); 5709 return; 5710 out_no_action: 5711 task->tk_action = NULL; 5712 out_wait: 5713 nfs4_sequence_done(task, &calldata->res.seq_res); 5714 } 5715 5716 static const struct rpc_call_ops nfs4_locku_ops = { 5717 .rpc_call_prepare = nfs4_locku_prepare, 5718 .rpc_call_done = nfs4_locku_done, 5719 .rpc_release = nfs4_locku_release_calldata, 5720 }; 5721 5722 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, 5723 struct nfs_open_context *ctx, 5724 struct nfs4_lock_state *lsp, 5725 struct nfs_seqid *seqid) 5726 { 5727 struct nfs4_unlockdata *data; 5728 struct rpc_message msg = { 5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], 5730 .rpc_cred = ctx->cred, 5731 }; 5732 struct rpc_task_setup task_setup_data = { 5733 .rpc_client = NFS_CLIENT(lsp->ls_state->inode), 5734 .rpc_message = &msg, 5735 .callback_ops = &nfs4_locku_ops, 5736 .workqueue = nfsiod_workqueue, 5737 .flags = RPC_TASK_ASYNC, 5738 }; 5739 5740 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, 5741 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); 5742 5743 /* Ensure this is an unlock - when canceling a lock, the 5744 * canceled lock is passed in, and it won't be an unlock. 5745 */ 5746 fl->fl_type = F_UNLCK; 5747 5748 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); 5749 if (data == NULL) { 5750 nfs_free_seqid(seqid); 5751 return ERR_PTR(-ENOMEM); 5752 } 5753 5754 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); 5755 msg.rpc_argp = &data->arg; 5756 msg.rpc_resp = &data->res; 5757 task_setup_data.callback_data = data; 5758 return rpc_run_task(&task_setup_data); 5759 } 5760 5761 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) 5762 { 5763 struct inode *inode = state->inode; 5764 struct nfs4_state_owner *sp = state->owner; 5765 struct nfs_inode *nfsi = NFS_I(inode); 5766 struct nfs_seqid *seqid; 5767 struct nfs4_lock_state *lsp; 5768 struct rpc_task *task; 5769 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 5770 int status = 0; 5771 unsigned char fl_flags = request->fl_flags; 5772 5773 status = nfs4_set_lock_state(state, request); 5774 /* Unlock _before_ we do the RPC call */ 5775 request->fl_flags |= FL_EXISTS; 5776 /* Exclude nfs_delegation_claim_locks() */ 5777 mutex_lock(&sp->so_delegreturn_mutex); 5778 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ 5779 down_read(&nfsi->rwsem); 5780 if (locks_lock_inode_wait(inode, request) == -ENOENT) { 5781 up_read(&nfsi->rwsem); 5782 mutex_unlock(&sp->so_delegreturn_mutex); 5783 goto out; 5784 } 5785 up_read(&nfsi->rwsem); 5786 mutex_unlock(&sp->so_delegreturn_mutex); 5787 if (status != 0) 5788 goto out; 5789 /* Is this a delegated lock? */ 5790 lsp = request->fl_u.nfs4_fl.owner; 5791 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) 5792 goto out; 5793 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; 5794 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); 5795 status = -ENOMEM; 5796 if (IS_ERR(seqid)) 5797 goto out; 5798 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); 5799 status = PTR_ERR(task); 5800 if (IS_ERR(task)) 5801 goto out; 5802 status = nfs4_wait_for_completion_rpc_task(task); 5803 rpc_put_task(task); 5804 out: 5805 request->fl_flags = fl_flags; 5806 trace_nfs4_unlock(request, state, F_SETLK, status); 5807 return status; 5808 } 5809 5810 struct nfs4_lockdata { 5811 struct nfs_lock_args arg; 5812 struct nfs_lock_res res; 5813 struct nfs4_lock_state *lsp; 5814 struct nfs_open_context *ctx; 5815 struct file_lock fl; 5816 unsigned long timestamp; 5817 int rpc_status; 5818 int cancelled; 5819 struct nfs_server *server; 5820 }; 5821 5822 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, 5823 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, 5824 gfp_t gfp_mask) 5825 { 5826 struct nfs4_lockdata *p; 5827 struct inode *inode = lsp->ls_state->inode; 5828 struct nfs_server *server = NFS_SERVER(inode); 5829 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); 5830 5831 p = kzalloc(sizeof(*p), gfp_mask); 5832 if (p == NULL) 5833 return NULL; 5834 5835 p->arg.fh = NFS_FH(inode); 5836 p->arg.fl = &p->fl; 5837 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); 5838 if (IS_ERR(p->arg.open_seqid)) 5839 goto out_free; 5840 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; 5841 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); 5842 if (IS_ERR(p->arg.lock_seqid)) 5843 goto out_free_seqid; 5844 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; 5845 p->arg.lock_owner.id = lsp->ls_seqid.owner_id; 5846 p->arg.lock_owner.s_dev = server->s_dev; 5847 p->res.lock_seqid = p->arg.lock_seqid; 5848 p->lsp = lsp; 5849 p->server = server; 5850 atomic_inc(&lsp->ls_count); 5851 p->ctx = get_nfs_open_context(ctx); 5852 get_file(fl->fl_file); 5853 memcpy(&p->fl, fl, sizeof(p->fl)); 5854 return p; 5855 out_free_seqid: 5856 nfs_free_seqid(p->arg.open_seqid); 5857 out_free: 5858 kfree(p); 5859 return NULL; 5860 } 5861 5862 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) 5863 { 5864 struct nfs4_lockdata *data = calldata; 5865 struct nfs4_state *state = data->lsp->ls_state; 5866 5867 dprintk("%s: begin!\n", __func__); 5868 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) 5869 goto out_wait; 5870 /* Do we need to do an open_to_lock_owner? */ 5871 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { 5872 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { 5873 goto out_release_lock_seqid; 5874 } 5875 nfs4_stateid_copy(&data->arg.open_stateid, 5876 &state->open_stateid); 5877 data->arg.new_lock_owner = 1; 5878 data->res.open_seqid = data->arg.open_seqid; 5879 } else { 5880 data->arg.new_lock_owner = 0; 5881 nfs4_stateid_copy(&data->arg.lock_stateid, 5882 &data->lsp->ls_stateid); 5883 } 5884 if (!nfs4_valid_open_stateid(state)) { 5885 data->rpc_status = -EBADF; 5886 task->tk_action = NULL; 5887 goto out_release_open_seqid; 5888 } 5889 data->timestamp = jiffies; 5890 if (nfs4_setup_sequence(data->server, 5891 &data->arg.seq_args, 5892 &data->res.seq_res, 5893 task) == 0) 5894 return; 5895 out_release_open_seqid: 5896 nfs_release_seqid(data->arg.open_seqid); 5897 out_release_lock_seqid: 5898 nfs_release_seqid(data->arg.lock_seqid); 5899 out_wait: 5900 nfs4_sequence_done(task, &data->res.seq_res); 5901 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); 5902 } 5903 5904 static void nfs4_lock_done(struct rpc_task *task, void *calldata) 5905 { 5906 struct nfs4_lockdata *data = calldata; 5907 struct nfs4_lock_state *lsp = data->lsp; 5908 5909 dprintk("%s: begin!\n", __func__); 5910 5911 if (!nfs4_sequence_done(task, &data->res.seq_res)) 5912 return; 5913 5914 data->rpc_status = task->tk_status; 5915 switch (task->tk_status) { 5916 case 0: 5917 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), 5918 data->timestamp); 5919 if (data->arg.new_lock) { 5920 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS); 5921 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) { 5922 rpc_restart_call_prepare(task); 5923 break; 5924 } 5925 } 5926 if (data->arg.new_lock_owner != 0) { 5927 nfs_confirm_seqid(&lsp->ls_seqid, 0); 5928 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); 5929 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 5930 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) 5931 rpc_restart_call_prepare(task); 5932 break; 5933 case -NFS4ERR_BAD_STATEID: 5934 case -NFS4ERR_OLD_STATEID: 5935 case -NFS4ERR_STALE_STATEID: 5936 case -NFS4ERR_EXPIRED: 5937 if (data->arg.new_lock_owner != 0) { 5938 if (!nfs4_stateid_match(&data->arg.open_stateid, 5939 &lsp->ls_state->open_stateid)) 5940 rpc_restart_call_prepare(task); 5941 } else if (!nfs4_stateid_match(&data->arg.lock_stateid, 5942 &lsp->ls_stateid)) 5943 rpc_restart_call_prepare(task); 5944 } 5945 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); 5946 } 5947 5948 static void nfs4_lock_release(void *calldata) 5949 { 5950 struct nfs4_lockdata *data = calldata; 5951 5952 dprintk("%s: begin!\n", __func__); 5953 nfs_free_seqid(data->arg.open_seqid); 5954 if (data->cancelled != 0) { 5955 struct rpc_task *task; 5956 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, 5957 data->arg.lock_seqid); 5958 if (!IS_ERR(task)) 5959 rpc_put_task_async(task); 5960 dprintk("%s: cancelling lock!\n", __func__); 5961 } else 5962 nfs_free_seqid(data->arg.lock_seqid); 5963 nfs4_put_lock_state(data->lsp); 5964 put_nfs_open_context(data->ctx); 5965 fput(data->fl.fl_file); 5966 kfree(data); 5967 dprintk("%s: done!\n", __func__); 5968 } 5969 5970 static const struct rpc_call_ops nfs4_lock_ops = { 5971 .rpc_call_prepare = nfs4_lock_prepare, 5972 .rpc_call_done = nfs4_lock_done, 5973 .rpc_release = nfs4_lock_release, 5974 }; 5975 5976 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) 5977 { 5978 switch (error) { 5979 case -NFS4ERR_ADMIN_REVOKED: 5980 case -NFS4ERR_BAD_STATEID: 5981 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 5982 if (new_lock_owner != 0 || 5983 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) 5984 nfs4_schedule_stateid_recovery(server, lsp->ls_state); 5985 break; 5986 case -NFS4ERR_STALE_STATEID: 5987 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; 5988 case -NFS4ERR_EXPIRED: 5989 nfs4_schedule_lease_recovery(server->nfs_client); 5990 }; 5991 } 5992 5993 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) 5994 { 5995 struct nfs4_lockdata *data; 5996 struct rpc_task *task; 5997 struct rpc_message msg = { 5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], 5999 .rpc_cred = state->owner->so_cred, 6000 }; 6001 struct rpc_task_setup task_setup_data = { 6002 .rpc_client = NFS_CLIENT(state->inode), 6003 .rpc_message = &msg, 6004 .callback_ops = &nfs4_lock_ops, 6005 .workqueue = nfsiod_workqueue, 6006 .flags = RPC_TASK_ASYNC, 6007 }; 6008 int ret; 6009 6010 dprintk("%s: begin!\n", __func__); 6011 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), 6012 fl->fl_u.nfs4_fl.owner, 6013 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); 6014 if (data == NULL) 6015 return -ENOMEM; 6016 if (IS_SETLKW(cmd)) 6017 data->arg.block = 1; 6018 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); 6019 msg.rpc_argp = &data->arg; 6020 msg.rpc_resp = &data->res; 6021 task_setup_data.callback_data = data; 6022 if (recovery_type > NFS_LOCK_NEW) { 6023 if (recovery_type == NFS_LOCK_RECLAIM) 6024 data->arg.reclaim = NFS_LOCK_RECLAIM; 6025 nfs4_set_sequence_privileged(&data->arg.seq_args); 6026 } else 6027 data->arg.new_lock = 1; 6028 task = rpc_run_task(&task_setup_data); 6029 if (IS_ERR(task)) 6030 return PTR_ERR(task); 6031 ret = nfs4_wait_for_completion_rpc_task(task); 6032 if (ret == 0) { 6033 ret = data->rpc_status; 6034 if (ret) 6035 nfs4_handle_setlk_error(data->server, data->lsp, 6036 data->arg.new_lock_owner, ret); 6037 } else 6038 data->cancelled = 1; 6039 rpc_put_task(task); 6040 dprintk("%s: done, ret = %d!\n", __func__, ret); 6041 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret); 6042 return ret; 6043 } 6044 6045 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) 6046 { 6047 struct nfs_server *server = NFS_SERVER(state->inode); 6048 struct nfs4_exception exception = { 6049 .inode = state->inode, 6050 }; 6051 int err; 6052 6053 do { 6054 /* Cache the lock if possible... */ 6055 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 6056 return 0; 6057 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); 6058 if (err != -NFS4ERR_DELAY) 6059 break; 6060 nfs4_handle_exception(server, err, &exception); 6061 } while (exception.retry); 6062 return err; 6063 } 6064 6065 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) 6066 { 6067 struct nfs_server *server = NFS_SERVER(state->inode); 6068 struct nfs4_exception exception = { 6069 .inode = state->inode, 6070 }; 6071 int err; 6072 6073 err = nfs4_set_lock_state(state, request); 6074 if (err != 0) 6075 return err; 6076 if (!recover_lost_locks) { 6077 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); 6078 return 0; 6079 } 6080 do { 6081 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) 6082 return 0; 6083 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); 6084 switch (err) { 6085 default: 6086 goto out; 6087 case -NFS4ERR_GRACE: 6088 case -NFS4ERR_DELAY: 6089 nfs4_handle_exception(server, err, &exception); 6090 err = 0; 6091 } 6092 } while (exception.retry); 6093 out: 6094 return err; 6095 } 6096 6097 #if defined(CONFIG_NFS_V4_1) 6098 /** 6099 * nfs41_check_expired_locks - possibly free a lock stateid 6100 * 6101 * @state: NFSv4 state for an inode 6102 * 6103 * Returns NFS_OK if recovery for this stateid is now finished. 6104 * Otherwise a negative NFS4ERR value is returned. 6105 */ 6106 static int nfs41_check_expired_locks(struct nfs4_state *state) 6107 { 6108 int status, ret = -NFS4ERR_BAD_STATEID; 6109 struct nfs4_lock_state *lsp; 6110 struct nfs_server *server = NFS_SERVER(state->inode); 6111 6112 list_for_each_entry(lsp, &state->lock_states, ls_locks) { 6113 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { 6114 struct rpc_cred *cred = lsp->ls_state->owner->so_cred; 6115 6116 status = nfs41_test_and_free_expired_stateid(server, 6117 &lsp->ls_stateid, 6118 cred); 6119 trace_nfs4_test_lock_stateid(state, lsp, status); 6120 if (status != NFS_OK) { 6121 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); 6122 ret = status; 6123 } 6124 } 6125 }; 6126 6127 return ret; 6128 } 6129 6130 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) 6131 { 6132 int status = NFS_OK; 6133 6134 if (test_bit(LK_STATE_IN_USE, &state->flags)) 6135 status = nfs41_check_expired_locks(state); 6136 if (status != NFS_OK) 6137 status = nfs4_lock_expired(state, request); 6138 return status; 6139 } 6140 #endif 6141 6142 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6143 { 6144 struct nfs_inode *nfsi = NFS_I(state->inode); 6145 struct nfs4_state_owner *sp = state->owner; 6146 unsigned char fl_flags = request->fl_flags; 6147 int status; 6148 6149 request->fl_flags |= FL_ACCESS; 6150 status = locks_lock_inode_wait(state->inode, request); 6151 if (status < 0) 6152 goto out; 6153 mutex_lock(&sp->so_delegreturn_mutex); 6154 down_read(&nfsi->rwsem); 6155 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { 6156 /* Yes: cache locks! */ 6157 /* ...but avoid races with delegation recall... */ 6158 request->fl_flags = fl_flags & ~FL_SLEEP; 6159 status = locks_lock_inode_wait(state->inode, request); 6160 up_read(&nfsi->rwsem); 6161 mutex_unlock(&sp->so_delegreturn_mutex); 6162 goto out; 6163 } 6164 up_read(&nfsi->rwsem); 6165 mutex_unlock(&sp->so_delegreturn_mutex); 6166 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); 6167 out: 6168 request->fl_flags = fl_flags; 6169 return status; 6170 } 6171 6172 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6173 { 6174 struct nfs4_exception exception = { 6175 .state = state, 6176 .inode = state->inode, 6177 }; 6178 int err; 6179 6180 do { 6181 err = _nfs4_proc_setlk(state, cmd, request); 6182 if (err == -NFS4ERR_DENIED) 6183 err = -EAGAIN; 6184 err = nfs4_handle_exception(NFS_SERVER(state->inode), 6185 err, &exception); 6186 } while (exception.retry); 6187 return err; 6188 } 6189 6190 #define NFS4_LOCK_MINTIMEOUT (1 * HZ) 6191 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) 6192 6193 static int 6194 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd, 6195 struct file_lock *request) 6196 { 6197 int status = -ERESTARTSYS; 6198 unsigned long timeout = NFS4_LOCK_MINTIMEOUT; 6199 6200 while(!signalled()) { 6201 status = nfs4_proc_setlk(state, cmd, request); 6202 if ((status != -EAGAIN) || IS_SETLK(cmd)) 6203 break; 6204 freezable_schedule_timeout_interruptible(timeout); 6205 timeout *= 2; 6206 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout); 6207 status = -ERESTARTSYS; 6208 } 6209 return status; 6210 } 6211 6212 #ifdef CONFIG_NFS_V4_1 6213 struct nfs4_lock_waiter { 6214 struct task_struct *task; 6215 struct inode *inode; 6216 struct nfs_lowner *owner; 6217 bool notified; 6218 }; 6219 6220 static int 6221 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key) 6222 { 6223 int ret; 6224 struct cb_notify_lock_args *cbnl = key; 6225 struct nfs4_lock_waiter *waiter = wait->private; 6226 struct nfs_lowner *lowner = &cbnl->cbnl_owner, 6227 *wowner = waiter->owner; 6228 6229 /* Only wake if the callback was for the same owner */ 6230 if (lowner->clientid != wowner->clientid || 6231 lowner->id != wowner->id || 6232 lowner->s_dev != wowner->s_dev) 6233 return 0; 6234 6235 /* Make sure it's for the right inode */ 6236 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh)) 6237 return 0; 6238 6239 waiter->notified = true; 6240 6241 /* override "private" so we can use default_wake_function */ 6242 wait->private = waiter->task; 6243 ret = autoremove_wake_function(wait, mode, flags, key); 6244 wait->private = waiter; 6245 return ret; 6246 } 6247 6248 static int 6249 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6250 { 6251 int status = -ERESTARTSYS; 6252 unsigned long flags; 6253 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; 6254 struct nfs_server *server = NFS_SERVER(state->inode); 6255 struct nfs_client *clp = server->nfs_client; 6256 wait_queue_head_t *q = &clp->cl_lock_waitq; 6257 struct nfs_lowner owner = { .clientid = clp->cl_clientid, 6258 .id = lsp->ls_seqid.owner_id, 6259 .s_dev = server->s_dev }; 6260 struct nfs4_lock_waiter waiter = { .task = current, 6261 .inode = state->inode, 6262 .owner = &owner, 6263 .notified = false }; 6264 wait_queue_t wait; 6265 6266 /* Don't bother with waitqueue if we don't expect a callback */ 6267 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags)) 6268 return nfs4_retry_setlk_simple(state, cmd, request); 6269 6270 init_wait(&wait); 6271 wait.private = &waiter; 6272 wait.func = nfs4_wake_lock_waiter; 6273 add_wait_queue(q, &wait); 6274 6275 while(!signalled()) { 6276 status = nfs4_proc_setlk(state, cmd, request); 6277 if ((status != -EAGAIN) || IS_SETLK(cmd)) 6278 break; 6279 6280 status = -ERESTARTSYS; 6281 spin_lock_irqsave(&q->lock, flags); 6282 if (waiter.notified) { 6283 spin_unlock_irqrestore(&q->lock, flags); 6284 continue; 6285 } 6286 set_current_state(TASK_INTERRUPTIBLE); 6287 spin_unlock_irqrestore(&q->lock, flags); 6288 6289 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT); 6290 } 6291 6292 finish_wait(q, &wait); 6293 return status; 6294 } 6295 #else /* !CONFIG_NFS_V4_1 */ 6296 static inline int 6297 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) 6298 { 6299 return nfs4_retry_setlk_simple(state, cmd, request); 6300 } 6301 #endif 6302 6303 static int 6304 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) 6305 { 6306 struct nfs_open_context *ctx; 6307 struct nfs4_state *state; 6308 int status; 6309 6310 /* verify open state */ 6311 ctx = nfs_file_open_context(filp); 6312 state = ctx->state; 6313 6314 if (request->fl_start < 0 || request->fl_end < 0) 6315 return -EINVAL; 6316 6317 if (IS_GETLK(cmd)) { 6318 if (state != NULL) 6319 return nfs4_proc_getlk(state, F_GETLK, request); 6320 return 0; 6321 } 6322 6323 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) 6324 return -EINVAL; 6325 6326 if (request->fl_type == F_UNLCK) { 6327 if (state != NULL) 6328 return nfs4_proc_unlck(state, cmd, request); 6329 return 0; 6330 } 6331 6332 if (state == NULL) 6333 return -ENOLCK; 6334 6335 if ((request->fl_flags & FL_POSIX) && 6336 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) 6337 return -ENOLCK; 6338 6339 /* 6340 * Don't rely on the VFS having checked the file open mode, 6341 * since it won't do this for flock() locks. 6342 */ 6343 switch (request->fl_type) { 6344 case F_RDLCK: 6345 if (!(filp->f_mode & FMODE_READ)) 6346 return -EBADF; 6347 break; 6348 case F_WRLCK: 6349 if (!(filp->f_mode & FMODE_WRITE)) 6350 return -EBADF; 6351 } 6352 6353 status = nfs4_set_lock_state(state, request); 6354 if (status != 0) 6355 return status; 6356 6357 return nfs4_retry_setlk(state, cmd, request); 6358 } 6359 6360 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) 6361 { 6362 struct nfs_server *server = NFS_SERVER(state->inode); 6363 int err; 6364 6365 err = nfs4_set_lock_state(state, fl); 6366 if (err != 0) 6367 return err; 6368 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); 6369 return nfs4_handle_delegation_recall_error(server, state, stateid, err); 6370 } 6371 6372 struct nfs_release_lockowner_data { 6373 struct nfs4_lock_state *lsp; 6374 struct nfs_server *server; 6375 struct nfs_release_lockowner_args args; 6376 struct nfs_release_lockowner_res res; 6377 unsigned long timestamp; 6378 }; 6379 6380 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) 6381 { 6382 struct nfs_release_lockowner_data *data = calldata; 6383 struct nfs_server *server = data->server; 6384 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, 6385 &data->args.seq_args, &data->res.seq_res, task); 6386 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 6387 data->timestamp = jiffies; 6388 } 6389 6390 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) 6391 { 6392 struct nfs_release_lockowner_data *data = calldata; 6393 struct nfs_server *server = data->server; 6394 6395 nfs40_sequence_done(task, &data->res.seq_res); 6396 6397 switch (task->tk_status) { 6398 case 0: 6399 renew_lease(server, data->timestamp); 6400 break; 6401 case -NFS4ERR_STALE_CLIENTID: 6402 case -NFS4ERR_EXPIRED: 6403 nfs4_schedule_lease_recovery(server->nfs_client); 6404 break; 6405 case -NFS4ERR_LEASE_MOVED: 6406 case -NFS4ERR_DELAY: 6407 if (nfs4_async_handle_error(task, server, 6408 NULL, NULL) == -EAGAIN) 6409 rpc_restart_call_prepare(task); 6410 } 6411 } 6412 6413 static void nfs4_release_lockowner_release(void *calldata) 6414 { 6415 struct nfs_release_lockowner_data *data = calldata; 6416 nfs4_free_lock_state(data->server, data->lsp); 6417 kfree(calldata); 6418 } 6419 6420 static const struct rpc_call_ops nfs4_release_lockowner_ops = { 6421 .rpc_call_prepare = nfs4_release_lockowner_prepare, 6422 .rpc_call_done = nfs4_release_lockowner_done, 6423 .rpc_release = nfs4_release_lockowner_release, 6424 }; 6425 6426 static void 6427 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) 6428 { 6429 struct nfs_release_lockowner_data *data; 6430 struct rpc_message msg = { 6431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], 6432 }; 6433 6434 if (server->nfs_client->cl_mvops->minor_version != 0) 6435 return; 6436 6437 data = kmalloc(sizeof(*data), GFP_NOFS); 6438 if (!data) 6439 return; 6440 data->lsp = lsp; 6441 data->server = server; 6442 data->args.lock_owner.clientid = server->nfs_client->cl_clientid; 6443 data->args.lock_owner.id = lsp->ls_seqid.owner_id; 6444 data->args.lock_owner.s_dev = server->s_dev; 6445 6446 msg.rpc_argp = &data->args; 6447 msg.rpc_resp = &data->res; 6448 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); 6449 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); 6450 } 6451 6452 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" 6453 6454 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler, 6455 struct dentry *unused, struct inode *inode, 6456 const char *key, const void *buf, 6457 size_t buflen, int flags) 6458 { 6459 return nfs4_proc_set_acl(inode, buf, buflen); 6460 } 6461 6462 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler, 6463 struct dentry *unused, struct inode *inode, 6464 const char *key, void *buf, size_t buflen) 6465 { 6466 return nfs4_proc_get_acl(inode, buf, buflen); 6467 } 6468 6469 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry) 6470 { 6471 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))); 6472 } 6473 6474 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 6475 6476 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler, 6477 struct dentry *unused, struct inode *inode, 6478 const char *key, const void *buf, 6479 size_t buflen, int flags) 6480 { 6481 if (security_ismaclabel(key)) 6482 return nfs4_set_security_label(inode, buf, buflen); 6483 6484 return -EOPNOTSUPP; 6485 } 6486 6487 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler, 6488 struct dentry *unused, struct inode *inode, 6489 const char *key, void *buf, size_t buflen) 6490 { 6491 if (security_ismaclabel(key)) 6492 return nfs4_get_security_label(inode, buf, buflen); 6493 return -EOPNOTSUPP; 6494 } 6495 6496 static ssize_t 6497 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) 6498 { 6499 int len = 0; 6500 6501 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) { 6502 len = security_inode_listsecurity(inode, list, list_len); 6503 if (list_len && len > list_len) 6504 return -ERANGE; 6505 } 6506 return len; 6507 } 6508 6509 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { 6510 .prefix = XATTR_SECURITY_PREFIX, 6511 .get = nfs4_xattr_get_nfs4_label, 6512 .set = nfs4_xattr_set_nfs4_label, 6513 }; 6514 6515 #else 6516 6517 static ssize_t 6518 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) 6519 { 6520 return 0; 6521 } 6522 6523 #endif 6524 6525 /* 6526 * nfs_fhget will use either the mounted_on_fileid or the fileid 6527 */ 6528 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) 6529 { 6530 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || 6531 (fattr->valid & NFS_ATTR_FATTR_FILEID)) && 6532 (fattr->valid & NFS_ATTR_FATTR_FSID) && 6533 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) 6534 return; 6535 6536 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | 6537 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; 6538 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; 6539 fattr->nlink = 2; 6540 } 6541 6542 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 6543 const struct qstr *name, 6544 struct nfs4_fs_locations *fs_locations, 6545 struct page *page) 6546 { 6547 struct nfs_server *server = NFS_SERVER(dir); 6548 u32 bitmask[3] = { 6549 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 6550 }; 6551 struct nfs4_fs_locations_arg args = { 6552 .dir_fh = NFS_FH(dir), 6553 .name = name, 6554 .page = page, 6555 .bitmask = bitmask, 6556 }; 6557 struct nfs4_fs_locations_res res = { 6558 .fs_locations = fs_locations, 6559 }; 6560 struct rpc_message msg = { 6561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 6562 .rpc_argp = &args, 6563 .rpc_resp = &res, 6564 }; 6565 int status; 6566 6567 dprintk("%s: start\n", __func__); 6568 6569 /* Ask for the fileid of the absent filesystem if mounted_on_fileid 6570 * is not supported */ 6571 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) 6572 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID; 6573 else 6574 bitmask[0] |= FATTR4_WORD0_FILEID; 6575 6576 nfs_fattr_init(&fs_locations->fattr); 6577 fs_locations->server = server; 6578 fs_locations->nlocations = 0; 6579 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); 6580 dprintk("%s: returned status = %d\n", __func__, status); 6581 return status; 6582 } 6583 6584 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, 6585 const struct qstr *name, 6586 struct nfs4_fs_locations *fs_locations, 6587 struct page *page) 6588 { 6589 struct nfs4_exception exception = { }; 6590 int err; 6591 do { 6592 err = _nfs4_proc_fs_locations(client, dir, name, 6593 fs_locations, page); 6594 trace_nfs4_get_fs_locations(dir, name, err); 6595 err = nfs4_handle_exception(NFS_SERVER(dir), err, 6596 &exception); 6597 } while (exception.retry); 6598 return err; 6599 } 6600 6601 /* 6602 * This operation also signals the server that this client is 6603 * performing migration recovery. The server can stop returning 6604 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is 6605 * appended to this compound to identify the client ID which is 6606 * performing recovery. 6607 */ 6608 static int _nfs40_proc_get_locations(struct inode *inode, 6609 struct nfs4_fs_locations *locations, 6610 struct page *page, struct rpc_cred *cred) 6611 { 6612 struct nfs_server *server = NFS_SERVER(inode); 6613 struct rpc_clnt *clnt = server->client; 6614 u32 bitmask[2] = { 6615 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 6616 }; 6617 struct nfs4_fs_locations_arg args = { 6618 .clientid = server->nfs_client->cl_clientid, 6619 .fh = NFS_FH(inode), 6620 .page = page, 6621 .bitmask = bitmask, 6622 .migration = 1, /* skip LOOKUP */ 6623 .renew = 1, /* append RENEW */ 6624 }; 6625 struct nfs4_fs_locations_res res = { 6626 .fs_locations = locations, 6627 .migration = 1, 6628 .renew = 1, 6629 }; 6630 struct rpc_message msg = { 6631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 6632 .rpc_argp = &args, 6633 .rpc_resp = &res, 6634 .rpc_cred = cred, 6635 }; 6636 unsigned long now = jiffies; 6637 int status; 6638 6639 nfs_fattr_init(&locations->fattr); 6640 locations->server = server; 6641 locations->nlocations = 0; 6642 6643 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); 6644 nfs4_set_sequence_privileged(&args.seq_args); 6645 status = nfs4_call_sync_sequence(clnt, server, &msg, 6646 &args.seq_args, &res.seq_res); 6647 if (status) 6648 return status; 6649 6650 renew_lease(server, now); 6651 return 0; 6652 } 6653 6654 #ifdef CONFIG_NFS_V4_1 6655 6656 /* 6657 * This operation also signals the server that this client is 6658 * performing migration recovery. The server can stop asserting 6659 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID 6660 * performing this operation is identified in the SEQUENCE 6661 * operation in this compound. 6662 * 6663 * When the client supports GETATTR(fs_locations_info), it can 6664 * be plumbed in here. 6665 */ 6666 static int _nfs41_proc_get_locations(struct inode *inode, 6667 struct nfs4_fs_locations *locations, 6668 struct page *page, struct rpc_cred *cred) 6669 { 6670 struct nfs_server *server = NFS_SERVER(inode); 6671 struct rpc_clnt *clnt = server->client; 6672 u32 bitmask[2] = { 6673 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, 6674 }; 6675 struct nfs4_fs_locations_arg args = { 6676 .fh = NFS_FH(inode), 6677 .page = page, 6678 .bitmask = bitmask, 6679 .migration = 1, /* skip LOOKUP */ 6680 }; 6681 struct nfs4_fs_locations_res res = { 6682 .fs_locations = locations, 6683 .migration = 1, 6684 }; 6685 struct rpc_message msg = { 6686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], 6687 .rpc_argp = &args, 6688 .rpc_resp = &res, 6689 .rpc_cred = cred, 6690 }; 6691 int status; 6692 6693 nfs_fattr_init(&locations->fattr); 6694 locations->server = server; 6695 locations->nlocations = 0; 6696 6697 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); 6698 nfs4_set_sequence_privileged(&args.seq_args); 6699 status = nfs4_call_sync_sequence(clnt, server, &msg, 6700 &args.seq_args, &res.seq_res); 6701 if (status == NFS4_OK && 6702 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 6703 status = -NFS4ERR_LEASE_MOVED; 6704 return status; 6705 } 6706 6707 #endif /* CONFIG_NFS_V4_1 */ 6708 6709 /** 6710 * nfs4_proc_get_locations - discover locations for a migrated FSID 6711 * @inode: inode on FSID that is migrating 6712 * @locations: result of query 6713 * @page: buffer 6714 * @cred: credential to use for this operation 6715 * 6716 * Returns NFS4_OK on success, a negative NFS4ERR status code if the 6717 * operation failed, or a negative errno if a local error occurred. 6718 * 6719 * On success, "locations" is filled in, but if the server has 6720 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not 6721 * asserted. 6722 * 6723 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases 6724 * from this client that require migration recovery. 6725 */ 6726 int nfs4_proc_get_locations(struct inode *inode, 6727 struct nfs4_fs_locations *locations, 6728 struct page *page, struct rpc_cred *cred) 6729 { 6730 struct nfs_server *server = NFS_SERVER(inode); 6731 struct nfs_client *clp = server->nfs_client; 6732 const struct nfs4_mig_recovery_ops *ops = 6733 clp->cl_mvops->mig_recovery_ops; 6734 struct nfs4_exception exception = { }; 6735 int status; 6736 6737 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 6738 (unsigned long long)server->fsid.major, 6739 (unsigned long long)server->fsid.minor, 6740 clp->cl_hostname); 6741 nfs_display_fhandle(NFS_FH(inode), __func__); 6742 6743 do { 6744 status = ops->get_locations(inode, locations, page, cred); 6745 if (status != -NFS4ERR_DELAY) 6746 break; 6747 nfs4_handle_exception(server, status, &exception); 6748 } while (exception.retry); 6749 return status; 6750 } 6751 6752 /* 6753 * This operation also signals the server that this client is 6754 * performing "lease moved" recovery. The server can stop 6755 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation 6756 * is appended to this compound to identify the client ID which is 6757 * performing recovery. 6758 */ 6759 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) 6760 { 6761 struct nfs_server *server = NFS_SERVER(inode); 6762 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 6763 struct rpc_clnt *clnt = server->client; 6764 struct nfs4_fsid_present_arg args = { 6765 .fh = NFS_FH(inode), 6766 .clientid = clp->cl_clientid, 6767 .renew = 1, /* append RENEW */ 6768 }; 6769 struct nfs4_fsid_present_res res = { 6770 .renew = 1, 6771 }; 6772 struct rpc_message msg = { 6773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], 6774 .rpc_argp = &args, 6775 .rpc_resp = &res, 6776 .rpc_cred = cred, 6777 }; 6778 unsigned long now = jiffies; 6779 int status; 6780 6781 res.fh = nfs_alloc_fhandle(); 6782 if (res.fh == NULL) 6783 return -ENOMEM; 6784 6785 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); 6786 nfs4_set_sequence_privileged(&args.seq_args); 6787 status = nfs4_call_sync_sequence(clnt, server, &msg, 6788 &args.seq_args, &res.seq_res); 6789 nfs_free_fhandle(res.fh); 6790 if (status) 6791 return status; 6792 6793 do_renew_lease(clp, now); 6794 return 0; 6795 } 6796 6797 #ifdef CONFIG_NFS_V4_1 6798 6799 /* 6800 * This operation also signals the server that this client is 6801 * performing "lease moved" recovery. The server can stop asserting 6802 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing 6803 * this operation is identified in the SEQUENCE operation in this 6804 * compound. 6805 */ 6806 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) 6807 { 6808 struct nfs_server *server = NFS_SERVER(inode); 6809 struct rpc_clnt *clnt = server->client; 6810 struct nfs4_fsid_present_arg args = { 6811 .fh = NFS_FH(inode), 6812 }; 6813 struct nfs4_fsid_present_res res = { 6814 }; 6815 struct rpc_message msg = { 6816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], 6817 .rpc_argp = &args, 6818 .rpc_resp = &res, 6819 .rpc_cred = cred, 6820 }; 6821 int status; 6822 6823 res.fh = nfs_alloc_fhandle(); 6824 if (res.fh == NULL) 6825 return -ENOMEM; 6826 6827 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); 6828 nfs4_set_sequence_privileged(&args.seq_args); 6829 status = nfs4_call_sync_sequence(clnt, server, &msg, 6830 &args.seq_args, &res.seq_res); 6831 nfs_free_fhandle(res.fh); 6832 if (status == NFS4_OK && 6833 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) 6834 status = -NFS4ERR_LEASE_MOVED; 6835 return status; 6836 } 6837 6838 #endif /* CONFIG_NFS_V4_1 */ 6839 6840 /** 6841 * nfs4_proc_fsid_present - Is this FSID present or absent on server? 6842 * @inode: inode on FSID to check 6843 * @cred: credential to use for this operation 6844 * 6845 * Server indicates whether the FSID is present, moved, or not 6846 * recognized. This operation is necessary to clear a LEASE_MOVED 6847 * condition for this client ID. 6848 * 6849 * Returns NFS4_OK if the FSID is present on this server, 6850 * -NFS4ERR_MOVED if the FSID is no longer present, a negative 6851 * NFS4ERR code if some error occurred on the server, or a 6852 * negative errno if a local failure occurred. 6853 */ 6854 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) 6855 { 6856 struct nfs_server *server = NFS_SERVER(inode); 6857 struct nfs_client *clp = server->nfs_client; 6858 const struct nfs4_mig_recovery_ops *ops = 6859 clp->cl_mvops->mig_recovery_ops; 6860 struct nfs4_exception exception = { }; 6861 int status; 6862 6863 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, 6864 (unsigned long long)server->fsid.major, 6865 (unsigned long long)server->fsid.minor, 6866 clp->cl_hostname); 6867 nfs_display_fhandle(NFS_FH(inode), __func__); 6868 6869 do { 6870 status = ops->fsid_present(inode, cred); 6871 if (status != -NFS4ERR_DELAY) 6872 break; 6873 nfs4_handle_exception(server, status, &exception); 6874 } while (exception.retry); 6875 return status; 6876 } 6877 6878 /** 6879 * If 'use_integrity' is true and the state managment nfs_client 6880 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient 6881 * and the machine credential as per RFC3530bis and RFC5661 Security 6882 * Considerations sections. Otherwise, just use the user cred with the 6883 * filesystem's rpc_client. 6884 */ 6885 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) 6886 { 6887 int status; 6888 struct nfs4_secinfo_arg args = { 6889 .dir_fh = NFS_FH(dir), 6890 .name = name, 6891 }; 6892 struct nfs4_secinfo_res res = { 6893 .flavors = flavors, 6894 }; 6895 struct rpc_message msg = { 6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], 6897 .rpc_argp = &args, 6898 .rpc_resp = &res, 6899 }; 6900 struct rpc_clnt *clnt = NFS_SERVER(dir)->client; 6901 struct rpc_cred *cred = NULL; 6902 6903 if (use_integrity) { 6904 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient; 6905 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client); 6906 msg.rpc_cred = cred; 6907 } 6908 6909 dprintk("NFS call secinfo %s\n", name->name); 6910 6911 nfs4_state_protect(NFS_SERVER(dir)->nfs_client, 6912 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); 6913 6914 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, 6915 &res.seq_res, 0); 6916 dprintk("NFS reply secinfo: %d\n", status); 6917 6918 if (cred) 6919 put_rpccred(cred); 6920 6921 return status; 6922 } 6923 6924 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, 6925 struct nfs4_secinfo_flavors *flavors) 6926 { 6927 struct nfs4_exception exception = { }; 6928 int err; 6929 do { 6930 err = -NFS4ERR_WRONGSEC; 6931 6932 /* try to use integrity protection with machine cred */ 6933 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) 6934 err = _nfs4_proc_secinfo(dir, name, flavors, true); 6935 6936 /* 6937 * if unable to use integrity protection, or SECINFO with 6938 * integrity protection returns NFS4ERR_WRONGSEC (which is 6939 * disallowed by spec, but exists in deployed servers) use 6940 * the current filesystem's rpc_client and the user cred. 6941 */ 6942 if (err == -NFS4ERR_WRONGSEC) 6943 err = _nfs4_proc_secinfo(dir, name, flavors, false); 6944 6945 trace_nfs4_secinfo(dir, name, err); 6946 err = nfs4_handle_exception(NFS_SERVER(dir), err, 6947 &exception); 6948 } while (exception.retry); 6949 return err; 6950 } 6951 6952 #ifdef CONFIG_NFS_V4_1 6953 /* 6954 * Check the exchange flags returned by the server for invalid flags, having 6955 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or 6956 * DS flags set. 6957 */ 6958 static int nfs4_check_cl_exchange_flags(u32 flags) 6959 { 6960 if (flags & ~EXCHGID4_FLAG_MASK_R) 6961 goto out_inval; 6962 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && 6963 (flags & EXCHGID4_FLAG_USE_NON_PNFS)) 6964 goto out_inval; 6965 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) 6966 goto out_inval; 6967 return NFS_OK; 6968 out_inval: 6969 return -NFS4ERR_INVAL; 6970 } 6971 6972 static bool 6973 nfs41_same_server_scope(struct nfs41_server_scope *a, 6974 struct nfs41_server_scope *b) 6975 { 6976 if (a->server_scope_sz == b->server_scope_sz && 6977 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0) 6978 return true; 6979 6980 return false; 6981 } 6982 6983 static void 6984 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata) 6985 { 6986 } 6987 6988 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = { 6989 .rpc_call_done = &nfs4_bind_one_conn_to_session_done, 6990 }; 6991 6992 /* 6993 * nfs4_proc_bind_one_conn_to_session() 6994 * 6995 * The 4.1 client currently uses the same TCP connection for the 6996 * fore and backchannel. 6997 */ 6998 static 6999 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt, 7000 struct rpc_xprt *xprt, 7001 struct nfs_client *clp, 7002 struct rpc_cred *cred) 7003 { 7004 int status; 7005 struct nfs41_bind_conn_to_session_args args = { 7006 .client = clp, 7007 .dir = NFS4_CDFC4_FORE_OR_BOTH, 7008 }; 7009 struct nfs41_bind_conn_to_session_res res; 7010 struct rpc_message msg = { 7011 .rpc_proc = 7012 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], 7013 .rpc_argp = &args, 7014 .rpc_resp = &res, 7015 .rpc_cred = cred, 7016 }; 7017 struct rpc_task_setup task_setup_data = { 7018 .rpc_client = clnt, 7019 .rpc_xprt = xprt, 7020 .callback_ops = &nfs4_bind_one_conn_to_session_ops, 7021 .rpc_message = &msg, 7022 .flags = RPC_TASK_TIMEOUT, 7023 }; 7024 struct rpc_task *task; 7025 7026 dprintk("--> %s\n", __func__); 7027 7028 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id); 7029 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN)) 7030 args.dir = NFS4_CDFC4_FORE; 7031 7032 /* Do not set the backchannel flag unless this is clnt->cl_xprt */ 7033 if (xprt != rcu_access_pointer(clnt->cl_xprt)) 7034 args.dir = NFS4_CDFC4_FORE; 7035 7036 task = rpc_run_task(&task_setup_data); 7037 if (!IS_ERR(task)) { 7038 status = task->tk_status; 7039 rpc_put_task(task); 7040 } else 7041 status = PTR_ERR(task); 7042 trace_nfs4_bind_conn_to_session(clp, status); 7043 if (status == 0) { 7044 if (memcmp(res.sessionid.data, 7045 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { 7046 dprintk("NFS: %s: Session ID mismatch\n", __func__); 7047 status = -EIO; 7048 goto out; 7049 } 7050 if ((res.dir & args.dir) != res.dir || res.dir == 0) { 7051 dprintk("NFS: %s: Unexpected direction from server\n", 7052 __func__); 7053 status = -EIO; 7054 goto out; 7055 } 7056 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) { 7057 dprintk("NFS: %s: Server returned RDMA mode = true\n", 7058 __func__); 7059 status = -EIO; 7060 goto out; 7061 } 7062 } 7063 out: 7064 dprintk("<-- %s status= %d\n", __func__, status); 7065 return status; 7066 } 7067 7068 struct rpc_bind_conn_calldata { 7069 struct nfs_client *clp; 7070 struct rpc_cred *cred; 7071 }; 7072 7073 static int 7074 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt, 7075 struct rpc_xprt *xprt, 7076 void *calldata) 7077 { 7078 struct rpc_bind_conn_calldata *p = calldata; 7079 7080 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred); 7081 } 7082 7083 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred) 7084 { 7085 struct rpc_bind_conn_calldata data = { 7086 .clp = clp, 7087 .cred = cred, 7088 }; 7089 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient, 7090 nfs4_proc_bind_conn_to_session_callback, &data); 7091 } 7092 7093 /* 7094 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map 7095 * and operations we'd like to see to enable certain features in the allow map 7096 */ 7097 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { 7098 .how = SP4_MACH_CRED, 7099 .enforce.u.words = { 7100 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 7101 1 << (OP_EXCHANGE_ID - 32) | 7102 1 << (OP_CREATE_SESSION - 32) | 7103 1 << (OP_DESTROY_SESSION - 32) | 7104 1 << (OP_DESTROY_CLIENTID - 32) 7105 }, 7106 .allow.u.words = { 7107 [0] = 1 << (OP_CLOSE) | 7108 1 << (OP_OPEN_DOWNGRADE) | 7109 1 << (OP_LOCKU) | 7110 1 << (OP_DELEGRETURN) | 7111 1 << (OP_COMMIT), 7112 [1] = 1 << (OP_SECINFO - 32) | 7113 1 << (OP_SECINFO_NO_NAME - 32) | 7114 1 << (OP_LAYOUTRETURN - 32) | 7115 1 << (OP_TEST_STATEID - 32) | 7116 1 << (OP_FREE_STATEID - 32) | 7117 1 << (OP_WRITE - 32) 7118 } 7119 }; 7120 7121 /* 7122 * Select the state protection mode for client `clp' given the server results 7123 * from exchange_id in `sp'. 7124 * 7125 * Returns 0 on success, negative errno otherwise. 7126 */ 7127 static int nfs4_sp4_select_mode(struct nfs_client *clp, 7128 struct nfs41_state_protection *sp) 7129 { 7130 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { 7131 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | 7132 1 << (OP_EXCHANGE_ID - 32) | 7133 1 << (OP_CREATE_SESSION - 32) | 7134 1 << (OP_DESTROY_SESSION - 32) | 7135 1 << (OP_DESTROY_CLIENTID - 32) 7136 }; 7137 unsigned int i; 7138 7139 if (sp->how == SP4_MACH_CRED) { 7140 /* Print state protect result */ 7141 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); 7142 for (i = 0; i <= LAST_NFS4_OP; i++) { 7143 if (test_bit(i, sp->enforce.u.longs)) 7144 dfprintk(MOUNT, " enforce op %d\n", i); 7145 if (test_bit(i, sp->allow.u.longs)) 7146 dfprintk(MOUNT, " allow op %d\n", i); 7147 } 7148 7149 /* make sure nothing is on enforce list that isn't supported */ 7150 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { 7151 if (sp->enforce.u.words[i] & ~supported_enforce[i]) { 7152 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 7153 return -EINVAL; 7154 } 7155 } 7156 7157 /* 7158 * Minimal mode - state operations are allowed to use machine 7159 * credential. Note this already happens by default, so the 7160 * client doesn't have to do anything more than the negotiation. 7161 * 7162 * NOTE: we don't care if EXCHANGE_ID is in the list - 7163 * we're already using the machine cred for exchange_id 7164 * and will never use a different cred. 7165 */ 7166 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && 7167 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && 7168 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && 7169 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { 7170 dfprintk(MOUNT, "sp4_mach_cred:\n"); 7171 dfprintk(MOUNT, " minimal mode enabled\n"); 7172 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags); 7173 } else { 7174 dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); 7175 return -EINVAL; 7176 } 7177 7178 if (test_bit(OP_CLOSE, sp->allow.u.longs) && 7179 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) && 7180 test_bit(OP_DELEGRETURN, sp->allow.u.longs) && 7181 test_bit(OP_LOCKU, sp->allow.u.longs)) { 7182 dfprintk(MOUNT, " cleanup mode enabled\n"); 7183 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags); 7184 } 7185 7186 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) { 7187 dfprintk(MOUNT, " pnfs cleanup mode enabled\n"); 7188 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, 7189 &clp->cl_sp4_flags); 7190 } 7191 7192 if (test_bit(OP_SECINFO, sp->allow.u.longs) && 7193 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { 7194 dfprintk(MOUNT, " secinfo mode enabled\n"); 7195 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags); 7196 } 7197 7198 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && 7199 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { 7200 dfprintk(MOUNT, " stateid mode enabled\n"); 7201 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags); 7202 } 7203 7204 if (test_bit(OP_WRITE, sp->allow.u.longs)) { 7205 dfprintk(MOUNT, " write mode enabled\n"); 7206 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags); 7207 } 7208 7209 if (test_bit(OP_COMMIT, sp->allow.u.longs)) { 7210 dfprintk(MOUNT, " commit mode enabled\n"); 7211 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags); 7212 } 7213 } 7214 7215 return 0; 7216 } 7217 7218 struct nfs41_exchange_id_data { 7219 struct nfs41_exchange_id_res res; 7220 struct nfs41_exchange_id_args args; 7221 struct rpc_xprt *xprt; 7222 int rpc_status; 7223 }; 7224 7225 static void nfs4_exchange_id_done(struct rpc_task *task, void *data) 7226 { 7227 struct nfs41_exchange_id_data *cdata = 7228 (struct nfs41_exchange_id_data *)data; 7229 struct nfs_client *clp = cdata->args.client; 7230 int status = task->tk_status; 7231 7232 trace_nfs4_exchange_id(clp, status); 7233 7234 if (status == 0) 7235 status = nfs4_check_cl_exchange_flags(cdata->res.flags); 7236 7237 if (cdata->xprt && status == 0) { 7238 status = nfs4_detect_session_trunking(clp, &cdata->res, 7239 cdata->xprt); 7240 goto out; 7241 } 7242 7243 if (status == 0) 7244 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect); 7245 7246 if (status == 0) { 7247 clp->cl_clientid = cdata->res.clientid; 7248 clp->cl_exchange_flags = cdata->res.flags; 7249 /* Client ID is not confirmed */ 7250 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) { 7251 clear_bit(NFS4_SESSION_ESTABLISHED, 7252 &clp->cl_session->session_state); 7253 clp->cl_seqid = cdata->res.seqid; 7254 } 7255 7256 kfree(clp->cl_serverowner); 7257 clp->cl_serverowner = cdata->res.server_owner; 7258 cdata->res.server_owner = NULL; 7259 7260 /* use the most recent implementation id */ 7261 kfree(clp->cl_implid); 7262 clp->cl_implid = cdata->res.impl_id; 7263 cdata->res.impl_id = NULL; 7264 7265 if (clp->cl_serverscope != NULL && 7266 !nfs41_same_server_scope(clp->cl_serverscope, 7267 cdata->res.server_scope)) { 7268 dprintk("%s: server_scope mismatch detected\n", 7269 __func__); 7270 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); 7271 kfree(clp->cl_serverscope); 7272 clp->cl_serverscope = NULL; 7273 } 7274 7275 if (clp->cl_serverscope == NULL) { 7276 clp->cl_serverscope = cdata->res.server_scope; 7277 cdata->res.server_scope = NULL; 7278 } 7279 /* Save the EXCHANGE_ID verifier session trunk tests */ 7280 memcpy(clp->cl_confirm.data, cdata->args.verifier->data, 7281 sizeof(clp->cl_confirm.data)); 7282 } 7283 out: 7284 cdata->rpc_status = status; 7285 return; 7286 } 7287 7288 static void nfs4_exchange_id_release(void *data) 7289 { 7290 struct nfs41_exchange_id_data *cdata = 7291 (struct nfs41_exchange_id_data *)data; 7292 7293 nfs_put_client(cdata->args.client); 7294 if (cdata->xprt) { 7295 xprt_put(cdata->xprt); 7296 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient); 7297 } 7298 kfree(cdata->res.impl_id); 7299 kfree(cdata->res.server_scope); 7300 kfree(cdata->res.server_owner); 7301 kfree(cdata); 7302 } 7303 7304 static const struct rpc_call_ops nfs4_exchange_id_call_ops = { 7305 .rpc_call_done = nfs4_exchange_id_done, 7306 .rpc_release = nfs4_exchange_id_release, 7307 }; 7308 7309 /* 7310 * _nfs4_proc_exchange_id() 7311 * 7312 * Wrapper for EXCHANGE_ID operation. 7313 */ 7314 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred, 7315 u32 sp4_how, struct rpc_xprt *xprt) 7316 { 7317 nfs4_verifier verifier; 7318 struct rpc_message msg = { 7319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], 7320 .rpc_cred = cred, 7321 }; 7322 struct rpc_task_setup task_setup_data = { 7323 .rpc_client = clp->cl_rpcclient, 7324 .callback_ops = &nfs4_exchange_id_call_ops, 7325 .rpc_message = &msg, 7326 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, 7327 }; 7328 struct nfs41_exchange_id_data *calldata; 7329 struct rpc_task *task; 7330 int status = -EIO; 7331 7332 if (!atomic_inc_not_zero(&clp->cl_count)) 7333 goto out; 7334 7335 status = -ENOMEM; 7336 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 7337 if (!calldata) 7338 goto out; 7339 7340 if (!xprt) 7341 nfs4_init_boot_verifier(clp, &verifier); 7342 7343 status = nfs4_init_uniform_client_string(clp); 7344 if (status) 7345 goto out_calldata; 7346 7347 dprintk("NFS call exchange_id auth=%s, '%s'\n", 7348 clp->cl_rpcclient->cl_auth->au_ops->au_name, 7349 clp->cl_owner_id); 7350 7351 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), 7352 GFP_NOFS); 7353 status = -ENOMEM; 7354 if (unlikely(calldata->res.server_owner == NULL)) 7355 goto out_calldata; 7356 7357 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), 7358 GFP_NOFS); 7359 if (unlikely(calldata->res.server_scope == NULL)) 7360 goto out_server_owner; 7361 7362 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); 7363 if (unlikely(calldata->res.impl_id == NULL)) 7364 goto out_server_scope; 7365 7366 switch (sp4_how) { 7367 case SP4_NONE: 7368 calldata->args.state_protect.how = SP4_NONE; 7369 break; 7370 7371 case SP4_MACH_CRED: 7372 calldata->args.state_protect = nfs4_sp4_mach_cred_request; 7373 break; 7374 7375 default: 7376 /* unsupported! */ 7377 WARN_ON_ONCE(1); 7378 status = -EINVAL; 7379 goto out_impl_id; 7380 } 7381 if (xprt) { 7382 calldata->xprt = xprt; 7383 task_setup_data.rpc_xprt = xprt; 7384 task_setup_data.flags = 7385 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC; 7386 calldata->args.verifier = &clp->cl_confirm; 7387 } else { 7388 calldata->args.verifier = &verifier; 7389 } 7390 calldata->args.client = clp; 7391 #ifdef CONFIG_NFS_V4_1_MIGRATION 7392 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | 7393 EXCHGID4_FLAG_BIND_PRINC_STATEID | 7394 EXCHGID4_FLAG_SUPP_MOVED_MIGR, 7395 #else 7396 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | 7397 EXCHGID4_FLAG_BIND_PRINC_STATEID, 7398 #endif 7399 msg.rpc_argp = &calldata->args; 7400 msg.rpc_resp = &calldata->res; 7401 task_setup_data.callback_data = calldata; 7402 7403 task = rpc_run_task(&task_setup_data); 7404 if (IS_ERR(task)) { 7405 status = PTR_ERR(task); 7406 goto out_impl_id; 7407 } 7408 7409 if (!xprt) { 7410 status = rpc_wait_for_completion_task(task); 7411 if (!status) 7412 status = calldata->rpc_status; 7413 } else /* session trunking test */ 7414 status = calldata->rpc_status; 7415 7416 rpc_put_task(task); 7417 out: 7418 if (clp->cl_implid != NULL) 7419 dprintk("NFS reply exchange_id: Server Implementation ID: " 7420 "domain: %s, name: %s, date: %llu,%u\n", 7421 clp->cl_implid->domain, clp->cl_implid->name, 7422 clp->cl_implid->date.seconds, 7423 clp->cl_implid->date.nseconds); 7424 dprintk("NFS reply exchange_id: %d\n", status); 7425 return status; 7426 7427 out_impl_id: 7428 kfree(calldata->res.impl_id); 7429 out_server_scope: 7430 kfree(calldata->res.server_scope); 7431 out_server_owner: 7432 kfree(calldata->res.server_owner); 7433 out_calldata: 7434 kfree(calldata); 7435 goto out; 7436 } 7437 7438 /* 7439 * nfs4_proc_exchange_id() 7440 * 7441 * Returns zero, a negative errno, or a negative NFS4ERR status code. 7442 * 7443 * Since the clientid has expired, all compounds using sessions 7444 * associated with the stale clientid will be returning 7445 * NFS4ERR_BADSESSION in the sequence operation, and will therefore 7446 * be in some phase of session reset. 7447 * 7448 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. 7449 */ 7450 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred) 7451 { 7452 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; 7453 int status; 7454 7455 /* try SP4_MACH_CRED if krb5i/p */ 7456 if (authflavor == RPC_AUTH_GSS_KRB5I || 7457 authflavor == RPC_AUTH_GSS_KRB5P) { 7458 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL); 7459 if (!status) 7460 return 0; 7461 } 7462 7463 /* try SP4_NONE */ 7464 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL); 7465 } 7466 7467 /** 7468 * nfs4_test_session_trunk 7469 * 7470 * This is an add_xprt_test() test function called from 7471 * rpc_clnt_setup_test_and_add_xprt. 7472 * 7473 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt 7474 * and is dereferrenced in nfs4_exchange_id_release 7475 * 7476 * Upon success, add the new transport to the rpc_clnt 7477 * 7478 * @clnt: struct rpc_clnt to get new transport 7479 * @xprt: the rpc_xprt to test 7480 * @data: call data for _nfs4_proc_exchange_id. 7481 */ 7482 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt, 7483 void *data) 7484 { 7485 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data; 7486 u32 sp4_how; 7487 7488 dprintk("--> %s try %s\n", __func__, 7489 xprt->address_strings[RPC_DISPLAY_ADDR]); 7490 7491 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED); 7492 7493 /* Test connection for session trunking. Async exchange_id call */ 7494 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt); 7495 } 7496 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk); 7497 7498 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, 7499 struct rpc_cred *cred) 7500 { 7501 struct rpc_message msg = { 7502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], 7503 .rpc_argp = clp, 7504 .rpc_cred = cred, 7505 }; 7506 int status; 7507 7508 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 7509 trace_nfs4_destroy_clientid(clp, status); 7510 if (status) 7511 dprintk("NFS: Got error %d from the server %s on " 7512 "DESTROY_CLIENTID.", status, clp->cl_hostname); 7513 return status; 7514 } 7515 7516 static int nfs4_proc_destroy_clientid(struct nfs_client *clp, 7517 struct rpc_cred *cred) 7518 { 7519 unsigned int loop; 7520 int ret; 7521 7522 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { 7523 ret = _nfs4_proc_destroy_clientid(clp, cred); 7524 switch (ret) { 7525 case -NFS4ERR_DELAY: 7526 case -NFS4ERR_CLIENTID_BUSY: 7527 ssleep(1); 7528 break; 7529 default: 7530 return ret; 7531 } 7532 } 7533 return 0; 7534 } 7535 7536 int nfs4_destroy_clientid(struct nfs_client *clp) 7537 { 7538 struct rpc_cred *cred; 7539 int ret = 0; 7540 7541 if (clp->cl_mvops->minor_version < 1) 7542 goto out; 7543 if (clp->cl_exchange_flags == 0) 7544 goto out; 7545 if (clp->cl_preserve_clid) 7546 goto out; 7547 cred = nfs4_get_clid_cred(clp); 7548 ret = nfs4_proc_destroy_clientid(clp, cred); 7549 if (cred) 7550 put_rpccred(cred); 7551 switch (ret) { 7552 case 0: 7553 case -NFS4ERR_STALE_CLIENTID: 7554 clp->cl_exchange_flags = 0; 7555 } 7556 out: 7557 return ret; 7558 } 7559 7560 struct nfs4_get_lease_time_data { 7561 struct nfs4_get_lease_time_args *args; 7562 struct nfs4_get_lease_time_res *res; 7563 struct nfs_client *clp; 7564 }; 7565 7566 static void nfs4_get_lease_time_prepare(struct rpc_task *task, 7567 void *calldata) 7568 { 7569 struct nfs4_get_lease_time_data *data = 7570 (struct nfs4_get_lease_time_data *)calldata; 7571 7572 dprintk("--> %s\n", __func__); 7573 /* just setup sequence, do not trigger session recovery 7574 since we're invoked within one */ 7575 nfs41_setup_sequence(data->clp->cl_session, 7576 &data->args->la_seq_args, 7577 &data->res->lr_seq_res, 7578 task); 7579 dprintk("<-- %s\n", __func__); 7580 } 7581 7582 /* 7583 * Called from nfs4_state_manager thread for session setup, so don't recover 7584 * from sequence operation or clientid errors. 7585 */ 7586 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) 7587 { 7588 struct nfs4_get_lease_time_data *data = 7589 (struct nfs4_get_lease_time_data *)calldata; 7590 7591 dprintk("--> %s\n", __func__); 7592 if (!nfs41_sequence_done(task, &data->res->lr_seq_res)) 7593 return; 7594 switch (task->tk_status) { 7595 case -NFS4ERR_DELAY: 7596 case -NFS4ERR_GRACE: 7597 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); 7598 rpc_delay(task, NFS4_POLL_RETRY_MIN); 7599 task->tk_status = 0; 7600 /* fall through */ 7601 case -NFS4ERR_RETRY_UNCACHED_REP: 7602 rpc_restart_call_prepare(task); 7603 return; 7604 } 7605 dprintk("<-- %s\n", __func__); 7606 } 7607 7608 static const struct rpc_call_ops nfs4_get_lease_time_ops = { 7609 .rpc_call_prepare = nfs4_get_lease_time_prepare, 7610 .rpc_call_done = nfs4_get_lease_time_done, 7611 }; 7612 7613 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) 7614 { 7615 struct rpc_task *task; 7616 struct nfs4_get_lease_time_args args; 7617 struct nfs4_get_lease_time_res res = { 7618 .lr_fsinfo = fsinfo, 7619 }; 7620 struct nfs4_get_lease_time_data data = { 7621 .args = &args, 7622 .res = &res, 7623 .clp = clp, 7624 }; 7625 struct rpc_message msg = { 7626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], 7627 .rpc_argp = &args, 7628 .rpc_resp = &res, 7629 }; 7630 struct rpc_task_setup task_setup = { 7631 .rpc_client = clp->cl_rpcclient, 7632 .rpc_message = &msg, 7633 .callback_ops = &nfs4_get_lease_time_ops, 7634 .callback_data = &data, 7635 .flags = RPC_TASK_TIMEOUT, 7636 }; 7637 int status; 7638 7639 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0); 7640 nfs4_set_sequence_privileged(&args.la_seq_args); 7641 dprintk("--> %s\n", __func__); 7642 task = rpc_run_task(&task_setup); 7643 7644 if (IS_ERR(task)) 7645 status = PTR_ERR(task); 7646 else { 7647 status = task->tk_status; 7648 rpc_put_task(task); 7649 } 7650 dprintk("<-- %s return %d\n", __func__, status); 7651 7652 return status; 7653 } 7654 7655 /* 7656 * Initialize the values to be used by the client in CREATE_SESSION 7657 * If nfs4_init_session set the fore channel request and response sizes, 7658 * use them. 7659 * 7660 * Set the back channel max_resp_sz_cached to zero to force the client to 7661 * always set csa_cachethis to FALSE because the current implementation 7662 * of the back channel DRC only supports caching the CB_SEQUENCE operation. 7663 */ 7664 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args, 7665 struct rpc_clnt *clnt) 7666 { 7667 unsigned int max_rqst_sz, max_resp_sz; 7668 unsigned int max_bc_payload = rpc_max_bc_payload(clnt); 7669 7670 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; 7671 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; 7672 7673 /* Fore channel attributes */ 7674 args->fc_attrs.max_rqst_sz = max_rqst_sz; 7675 args->fc_attrs.max_resp_sz = max_resp_sz; 7676 args->fc_attrs.max_ops = NFS4_MAX_OPS; 7677 args->fc_attrs.max_reqs = max_session_slots; 7678 7679 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " 7680 "max_ops=%u max_reqs=%u\n", 7681 __func__, 7682 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, 7683 args->fc_attrs.max_ops, args->fc_attrs.max_reqs); 7684 7685 /* Back channel attributes */ 7686 args->bc_attrs.max_rqst_sz = max_bc_payload; 7687 args->bc_attrs.max_resp_sz = max_bc_payload; 7688 args->bc_attrs.max_resp_sz_cached = 0; 7689 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; 7690 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1); 7691 7692 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " 7693 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", 7694 __func__, 7695 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, 7696 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, 7697 args->bc_attrs.max_reqs); 7698 } 7699 7700 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, 7701 struct nfs41_create_session_res *res) 7702 { 7703 struct nfs4_channel_attrs *sent = &args->fc_attrs; 7704 struct nfs4_channel_attrs *rcvd = &res->fc_attrs; 7705 7706 if (rcvd->max_resp_sz > sent->max_resp_sz) 7707 return -EINVAL; 7708 /* 7709 * Our requested max_ops is the minimum we need; we're not 7710 * prepared to break up compounds into smaller pieces than that. 7711 * So, no point even trying to continue if the server won't 7712 * cooperate: 7713 */ 7714 if (rcvd->max_ops < sent->max_ops) 7715 return -EINVAL; 7716 if (rcvd->max_reqs == 0) 7717 return -EINVAL; 7718 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) 7719 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; 7720 return 0; 7721 } 7722 7723 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, 7724 struct nfs41_create_session_res *res) 7725 { 7726 struct nfs4_channel_attrs *sent = &args->bc_attrs; 7727 struct nfs4_channel_attrs *rcvd = &res->bc_attrs; 7728 7729 if (!(res->flags & SESSION4_BACK_CHAN)) 7730 goto out; 7731 if (rcvd->max_rqst_sz > sent->max_rqst_sz) 7732 return -EINVAL; 7733 if (rcvd->max_resp_sz < sent->max_resp_sz) 7734 return -EINVAL; 7735 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) 7736 return -EINVAL; 7737 if (rcvd->max_ops > sent->max_ops) 7738 return -EINVAL; 7739 if (rcvd->max_reqs > sent->max_reqs) 7740 return -EINVAL; 7741 out: 7742 return 0; 7743 } 7744 7745 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, 7746 struct nfs41_create_session_res *res) 7747 { 7748 int ret; 7749 7750 ret = nfs4_verify_fore_channel_attrs(args, res); 7751 if (ret) 7752 return ret; 7753 return nfs4_verify_back_channel_attrs(args, res); 7754 } 7755 7756 static void nfs4_update_session(struct nfs4_session *session, 7757 struct nfs41_create_session_res *res) 7758 { 7759 nfs4_copy_sessionid(&session->sess_id, &res->sessionid); 7760 /* Mark client id and session as being confirmed */ 7761 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 7762 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state); 7763 session->flags = res->flags; 7764 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs)); 7765 if (res->flags & SESSION4_BACK_CHAN) 7766 memcpy(&session->bc_attrs, &res->bc_attrs, 7767 sizeof(session->bc_attrs)); 7768 } 7769 7770 static int _nfs4_proc_create_session(struct nfs_client *clp, 7771 struct rpc_cred *cred) 7772 { 7773 struct nfs4_session *session = clp->cl_session; 7774 struct nfs41_create_session_args args = { 7775 .client = clp, 7776 .clientid = clp->cl_clientid, 7777 .seqid = clp->cl_seqid, 7778 .cb_program = NFS4_CALLBACK, 7779 }; 7780 struct nfs41_create_session_res res; 7781 7782 struct rpc_message msg = { 7783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], 7784 .rpc_argp = &args, 7785 .rpc_resp = &res, 7786 .rpc_cred = cred, 7787 }; 7788 int status; 7789 7790 nfs4_init_channel_attrs(&args, clp->cl_rpcclient); 7791 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); 7792 7793 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 7794 trace_nfs4_create_session(clp, status); 7795 7796 switch (status) { 7797 case -NFS4ERR_STALE_CLIENTID: 7798 case -NFS4ERR_DELAY: 7799 case -ETIMEDOUT: 7800 case -EACCES: 7801 case -EAGAIN: 7802 goto out; 7803 }; 7804 7805 clp->cl_seqid++; 7806 if (!status) { 7807 /* Verify the session's negotiated channel_attrs values */ 7808 status = nfs4_verify_channel_attrs(&args, &res); 7809 /* Increment the clientid slot sequence id */ 7810 if (status) 7811 goto out; 7812 nfs4_update_session(session, &res); 7813 } 7814 out: 7815 return status; 7816 } 7817 7818 /* 7819 * Issues a CREATE_SESSION operation to the server. 7820 * It is the responsibility of the caller to verify the session is 7821 * expired before calling this routine. 7822 */ 7823 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred) 7824 { 7825 int status; 7826 unsigned *ptr; 7827 struct nfs4_session *session = clp->cl_session; 7828 7829 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); 7830 7831 status = _nfs4_proc_create_session(clp, cred); 7832 if (status) 7833 goto out; 7834 7835 /* Init or reset the session slot tables */ 7836 status = nfs4_setup_session_slot_tables(session); 7837 dprintk("slot table setup returned %d\n", status); 7838 if (status) 7839 goto out; 7840 7841 ptr = (unsigned *)&session->sess_id.data[0]; 7842 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, 7843 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); 7844 out: 7845 dprintk("<-- %s\n", __func__); 7846 return status; 7847 } 7848 7849 /* 7850 * Issue the over-the-wire RPC DESTROY_SESSION. 7851 * The caller must serialize access to this routine. 7852 */ 7853 int nfs4_proc_destroy_session(struct nfs4_session *session, 7854 struct rpc_cred *cred) 7855 { 7856 struct rpc_message msg = { 7857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], 7858 .rpc_argp = session, 7859 .rpc_cred = cred, 7860 }; 7861 int status = 0; 7862 7863 dprintk("--> nfs4_proc_destroy_session\n"); 7864 7865 /* session is still being setup */ 7866 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state)) 7867 return 0; 7868 7869 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); 7870 trace_nfs4_destroy_session(session->clp, status); 7871 7872 if (status) 7873 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " 7874 "Session has been destroyed regardless...\n", status); 7875 7876 dprintk("<-- nfs4_proc_destroy_session\n"); 7877 return status; 7878 } 7879 7880 /* 7881 * Renew the cl_session lease. 7882 */ 7883 struct nfs4_sequence_data { 7884 struct nfs_client *clp; 7885 struct nfs4_sequence_args args; 7886 struct nfs4_sequence_res res; 7887 }; 7888 7889 static void nfs41_sequence_release(void *data) 7890 { 7891 struct nfs4_sequence_data *calldata = data; 7892 struct nfs_client *clp = calldata->clp; 7893 7894 if (atomic_read(&clp->cl_count) > 1) 7895 nfs4_schedule_state_renewal(clp); 7896 nfs_put_client(clp); 7897 kfree(calldata); 7898 } 7899 7900 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) 7901 { 7902 switch(task->tk_status) { 7903 case -NFS4ERR_DELAY: 7904 rpc_delay(task, NFS4_POLL_RETRY_MAX); 7905 return -EAGAIN; 7906 default: 7907 nfs4_schedule_lease_recovery(clp); 7908 } 7909 return 0; 7910 } 7911 7912 static void nfs41_sequence_call_done(struct rpc_task *task, void *data) 7913 { 7914 struct nfs4_sequence_data *calldata = data; 7915 struct nfs_client *clp = calldata->clp; 7916 7917 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) 7918 return; 7919 7920 trace_nfs4_sequence(clp, task->tk_status); 7921 if (task->tk_status < 0) { 7922 dprintk("%s ERROR %d\n", __func__, task->tk_status); 7923 if (atomic_read(&clp->cl_count) == 1) 7924 goto out; 7925 7926 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { 7927 rpc_restart_call_prepare(task); 7928 return; 7929 } 7930 } 7931 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); 7932 out: 7933 dprintk("<-- %s\n", __func__); 7934 } 7935 7936 static void nfs41_sequence_prepare(struct rpc_task *task, void *data) 7937 { 7938 struct nfs4_sequence_data *calldata = data; 7939 struct nfs_client *clp = calldata->clp; 7940 struct nfs4_sequence_args *args; 7941 struct nfs4_sequence_res *res; 7942 7943 args = task->tk_msg.rpc_argp; 7944 res = task->tk_msg.rpc_resp; 7945 7946 nfs41_setup_sequence(clp->cl_session, args, res, task); 7947 } 7948 7949 static const struct rpc_call_ops nfs41_sequence_ops = { 7950 .rpc_call_done = nfs41_sequence_call_done, 7951 .rpc_call_prepare = nfs41_sequence_prepare, 7952 .rpc_release = nfs41_sequence_release, 7953 }; 7954 7955 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, 7956 struct rpc_cred *cred, 7957 bool is_privileged) 7958 { 7959 struct nfs4_sequence_data *calldata; 7960 struct rpc_message msg = { 7961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], 7962 .rpc_cred = cred, 7963 }; 7964 struct rpc_task_setup task_setup_data = { 7965 .rpc_client = clp->cl_rpcclient, 7966 .rpc_message = &msg, 7967 .callback_ops = &nfs41_sequence_ops, 7968 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, 7969 }; 7970 7971 if (!atomic_inc_not_zero(&clp->cl_count)) 7972 return ERR_PTR(-EIO); 7973 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 7974 if (calldata == NULL) { 7975 nfs_put_client(clp); 7976 return ERR_PTR(-ENOMEM); 7977 } 7978 nfs4_init_sequence(&calldata->args, &calldata->res, 0); 7979 if (is_privileged) 7980 nfs4_set_sequence_privileged(&calldata->args); 7981 msg.rpc_argp = &calldata->args; 7982 msg.rpc_resp = &calldata->res; 7983 calldata->clp = clp; 7984 task_setup_data.callback_data = calldata; 7985 7986 return rpc_run_task(&task_setup_data); 7987 } 7988 7989 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) 7990 { 7991 struct rpc_task *task; 7992 int ret = 0; 7993 7994 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) 7995 return -EAGAIN; 7996 task = _nfs41_proc_sequence(clp, cred, false); 7997 if (IS_ERR(task)) 7998 ret = PTR_ERR(task); 7999 else 8000 rpc_put_task_async(task); 8001 dprintk("<-- %s status=%d\n", __func__, ret); 8002 return ret; 8003 } 8004 8005 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred) 8006 { 8007 struct rpc_task *task; 8008 int ret; 8009 8010 task = _nfs41_proc_sequence(clp, cred, true); 8011 if (IS_ERR(task)) { 8012 ret = PTR_ERR(task); 8013 goto out; 8014 } 8015 ret = rpc_wait_for_completion_task(task); 8016 if (!ret) 8017 ret = task->tk_status; 8018 rpc_put_task(task); 8019 out: 8020 dprintk("<-- %s status=%d\n", __func__, ret); 8021 return ret; 8022 } 8023 8024 struct nfs4_reclaim_complete_data { 8025 struct nfs_client *clp; 8026 struct nfs41_reclaim_complete_args arg; 8027 struct nfs41_reclaim_complete_res res; 8028 }; 8029 8030 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) 8031 { 8032 struct nfs4_reclaim_complete_data *calldata = data; 8033 8034 nfs41_setup_sequence(calldata->clp->cl_session, 8035 &calldata->arg.seq_args, 8036 &calldata->res.seq_res, 8037 task); 8038 } 8039 8040 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) 8041 { 8042 switch(task->tk_status) { 8043 case 0: 8044 case -NFS4ERR_COMPLETE_ALREADY: 8045 case -NFS4ERR_WRONG_CRED: /* What to do here? */ 8046 break; 8047 case -NFS4ERR_DELAY: 8048 rpc_delay(task, NFS4_POLL_RETRY_MAX); 8049 /* fall through */ 8050 case -NFS4ERR_RETRY_UNCACHED_REP: 8051 return -EAGAIN; 8052 default: 8053 nfs4_schedule_lease_recovery(clp); 8054 } 8055 return 0; 8056 } 8057 8058 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) 8059 { 8060 struct nfs4_reclaim_complete_data *calldata = data; 8061 struct nfs_client *clp = calldata->clp; 8062 struct nfs4_sequence_res *res = &calldata->res.seq_res; 8063 8064 dprintk("--> %s\n", __func__); 8065 if (!nfs41_sequence_done(task, res)) 8066 return; 8067 8068 trace_nfs4_reclaim_complete(clp, task->tk_status); 8069 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { 8070 rpc_restart_call_prepare(task); 8071 return; 8072 } 8073 dprintk("<-- %s\n", __func__); 8074 } 8075 8076 static void nfs4_free_reclaim_complete_data(void *data) 8077 { 8078 struct nfs4_reclaim_complete_data *calldata = data; 8079 8080 kfree(calldata); 8081 } 8082 8083 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { 8084 .rpc_call_prepare = nfs4_reclaim_complete_prepare, 8085 .rpc_call_done = nfs4_reclaim_complete_done, 8086 .rpc_release = nfs4_free_reclaim_complete_data, 8087 }; 8088 8089 /* 8090 * Issue a global reclaim complete. 8091 */ 8092 static int nfs41_proc_reclaim_complete(struct nfs_client *clp, 8093 struct rpc_cred *cred) 8094 { 8095 struct nfs4_reclaim_complete_data *calldata; 8096 struct rpc_task *task; 8097 struct rpc_message msg = { 8098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], 8099 .rpc_cred = cred, 8100 }; 8101 struct rpc_task_setup task_setup_data = { 8102 .rpc_client = clp->cl_rpcclient, 8103 .rpc_message = &msg, 8104 .callback_ops = &nfs4_reclaim_complete_call_ops, 8105 .flags = RPC_TASK_ASYNC, 8106 }; 8107 int status = -ENOMEM; 8108 8109 dprintk("--> %s\n", __func__); 8110 calldata = kzalloc(sizeof(*calldata), GFP_NOFS); 8111 if (calldata == NULL) 8112 goto out; 8113 calldata->clp = clp; 8114 calldata->arg.one_fs = 0; 8115 8116 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0); 8117 nfs4_set_sequence_privileged(&calldata->arg.seq_args); 8118 msg.rpc_argp = &calldata->arg; 8119 msg.rpc_resp = &calldata->res; 8120 task_setup_data.callback_data = calldata; 8121 task = rpc_run_task(&task_setup_data); 8122 if (IS_ERR(task)) { 8123 status = PTR_ERR(task); 8124 goto out; 8125 } 8126 status = nfs4_wait_for_completion_rpc_task(task); 8127 if (status == 0) 8128 status = task->tk_status; 8129 rpc_put_task(task); 8130 return 0; 8131 out: 8132 dprintk("<-- %s status=%d\n", __func__, status); 8133 return status; 8134 } 8135 8136 static void 8137 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) 8138 { 8139 struct nfs4_layoutget *lgp = calldata; 8140 struct nfs_server *server = NFS_SERVER(lgp->args.inode); 8141 struct nfs4_session *session = nfs4_get_session(server); 8142 8143 dprintk("--> %s\n", __func__); 8144 nfs41_setup_sequence(session, &lgp->args.seq_args, 8145 &lgp->res.seq_res, task); 8146 dprintk("<-- %s\n", __func__); 8147 } 8148 8149 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) 8150 { 8151 struct nfs4_layoutget *lgp = calldata; 8152 8153 dprintk("--> %s\n", __func__); 8154 nfs41_sequence_process(task, &lgp->res.seq_res); 8155 dprintk("<-- %s\n", __func__); 8156 } 8157 8158 static int 8159 nfs4_layoutget_handle_exception(struct rpc_task *task, 8160 struct nfs4_layoutget *lgp, struct nfs4_exception *exception) 8161 { 8162 struct inode *inode = lgp->args.inode; 8163 struct nfs_server *server = NFS_SERVER(inode); 8164 struct pnfs_layout_hdr *lo; 8165 int nfs4err = task->tk_status; 8166 int err, status = 0; 8167 LIST_HEAD(head); 8168 8169 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); 8170 8171 switch (nfs4err) { 8172 case 0: 8173 goto out; 8174 8175 /* 8176 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs 8177 * on the file. set tk_status to -ENODATA to tell upper layer to 8178 * retry go inband. 8179 */ 8180 case -NFS4ERR_LAYOUTUNAVAILABLE: 8181 status = -ENODATA; 8182 goto out; 8183 /* 8184 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of 8185 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3). 8186 */ 8187 case -NFS4ERR_BADLAYOUT: 8188 status = -EOVERFLOW; 8189 goto out; 8190 /* 8191 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client 8192 * (or clients) writing to the same RAID stripe except when 8193 * the minlength argument is 0 (see RFC5661 section 18.43.3). 8194 * 8195 * Treat it like we would RECALLCONFLICT -- we retry for a little 8196 * while, and then eventually give up. 8197 */ 8198 case -NFS4ERR_LAYOUTTRYLATER: 8199 if (lgp->args.minlength == 0) { 8200 status = -EOVERFLOW; 8201 goto out; 8202 } 8203 status = -EBUSY; 8204 break; 8205 case -NFS4ERR_RECALLCONFLICT: 8206 status = -ERECALLCONFLICT; 8207 break; 8208 case -NFS4ERR_EXPIRED: 8209 case -NFS4ERR_BAD_STATEID: 8210 exception->timeout = 0; 8211 spin_lock(&inode->i_lock); 8212 lo = NFS_I(inode)->layout; 8213 /* If the open stateid was bad, then recover it. */ 8214 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || 8215 nfs4_stateid_match_other(&lgp->args.stateid, 8216 &lgp->args.ctx->state->stateid)) { 8217 spin_unlock(&inode->i_lock); 8218 exception->state = lgp->args.ctx->state; 8219 break; 8220 } 8221 8222 /* 8223 * Mark the bad layout state as invalid, then retry 8224 */ 8225 pnfs_mark_layout_stateid_invalid(lo, &head); 8226 spin_unlock(&inode->i_lock); 8227 pnfs_free_lseg_list(&head); 8228 status = -EAGAIN; 8229 goto out; 8230 } 8231 8232 err = nfs4_handle_exception(server, nfs4err, exception); 8233 if (!status) { 8234 if (exception->retry) 8235 status = -EAGAIN; 8236 else 8237 status = err; 8238 } 8239 out: 8240 dprintk("<-- %s\n", __func__); 8241 return status; 8242 } 8243 8244 static size_t max_response_pages(struct nfs_server *server) 8245 { 8246 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; 8247 return nfs_page_array_len(0, max_resp_sz); 8248 } 8249 8250 static void nfs4_free_pages(struct page **pages, size_t size) 8251 { 8252 int i; 8253 8254 if (!pages) 8255 return; 8256 8257 for (i = 0; i < size; i++) { 8258 if (!pages[i]) 8259 break; 8260 __free_page(pages[i]); 8261 } 8262 kfree(pages); 8263 } 8264 8265 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags) 8266 { 8267 struct page **pages; 8268 int i; 8269 8270 pages = kcalloc(size, sizeof(struct page *), gfp_flags); 8271 if (!pages) { 8272 dprintk("%s: can't alloc array of %zu pages\n", __func__, size); 8273 return NULL; 8274 } 8275 8276 for (i = 0; i < size; i++) { 8277 pages[i] = alloc_page(gfp_flags); 8278 if (!pages[i]) { 8279 dprintk("%s: failed to allocate page\n", __func__); 8280 nfs4_free_pages(pages, size); 8281 return NULL; 8282 } 8283 } 8284 8285 return pages; 8286 } 8287 8288 static void nfs4_layoutget_release(void *calldata) 8289 { 8290 struct nfs4_layoutget *lgp = calldata; 8291 struct inode *inode = lgp->args.inode; 8292 struct nfs_server *server = NFS_SERVER(inode); 8293 size_t max_pages = max_response_pages(server); 8294 8295 dprintk("--> %s\n", __func__); 8296 nfs4_free_pages(lgp->args.layout.pages, max_pages); 8297 pnfs_put_layout_hdr(NFS_I(inode)->layout); 8298 put_nfs_open_context(lgp->args.ctx); 8299 kfree(calldata); 8300 dprintk("<-- %s\n", __func__); 8301 } 8302 8303 static const struct rpc_call_ops nfs4_layoutget_call_ops = { 8304 .rpc_call_prepare = nfs4_layoutget_prepare, 8305 .rpc_call_done = nfs4_layoutget_done, 8306 .rpc_release = nfs4_layoutget_release, 8307 }; 8308 8309 struct pnfs_layout_segment * 8310 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags) 8311 { 8312 struct inode *inode = lgp->args.inode; 8313 struct nfs_server *server = NFS_SERVER(inode); 8314 size_t max_pages = max_response_pages(server); 8315 struct rpc_task *task; 8316 struct rpc_message msg = { 8317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], 8318 .rpc_argp = &lgp->args, 8319 .rpc_resp = &lgp->res, 8320 .rpc_cred = lgp->cred, 8321 }; 8322 struct rpc_task_setup task_setup_data = { 8323 .rpc_client = server->client, 8324 .rpc_message = &msg, 8325 .callback_ops = &nfs4_layoutget_call_ops, 8326 .callback_data = lgp, 8327 .flags = RPC_TASK_ASYNC, 8328 }; 8329 struct pnfs_layout_segment *lseg = NULL; 8330 struct nfs4_exception exception = { 8331 .inode = inode, 8332 .timeout = *timeout, 8333 }; 8334 int status = 0; 8335 8336 dprintk("--> %s\n", __func__); 8337 8338 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */ 8339 pnfs_get_layout_hdr(NFS_I(inode)->layout); 8340 8341 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags); 8342 if (!lgp->args.layout.pages) { 8343 nfs4_layoutget_release(lgp); 8344 return ERR_PTR(-ENOMEM); 8345 } 8346 lgp->args.layout.pglen = max_pages * PAGE_SIZE; 8347 8348 lgp->res.layoutp = &lgp->args.layout; 8349 lgp->res.seq_res.sr_slot = NULL; 8350 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0); 8351 8352 task = rpc_run_task(&task_setup_data); 8353 if (IS_ERR(task)) 8354 return ERR_CAST(task); 8355 status = nfs4_wait_for_completion_rpc_task(task); 8356 if (status == 0) { 8357 status = nfs4_layoutget_handle_exception(task, lgp, &exception); 8358 *timeout = exception.timeout; 8359 } 8360 8361 trace_nfs4_layoutget(lgp->args.ctx, 8362 &lgp->args.range, 8363 &lgp->res.range, 8364 &lgp->res.stateid, 8365 status); 8366 8367 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */ 8368 if (status == 0 && lgp->res.layoutp->len) 8369 lseg = pnfs_layout_process(lgp); 8370 nfs4_sequence_free_slot(&lgp->res.seq_res); 8371 rpc_put_task(task); 8372 dprintk("<-- %s status=%d\n", __func__, status); 8373 if (status) 8374 return ERR_PTR(status); 8375 return lseg; 8376 } 8377 8378 static void 8379 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) 8380 { 8381 struct nfs4_layoutreturn *lrp = calldata; 8382 8383 dprintk("--> %s\n", __func__); 8384 nfs41_setup_sequence(lrp->clp->cl_session, 8385 &lrp->args.seq_args, 8386 &lrp->res.seq_res, 8387 task); 8388 } 8389 8390 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) 8391 { 8392 struct nfs4_layoutreturn *lrp = calldata; 8393 struct nfs_server *server; 8394 8395 dprintk("--> %s\n", __func__); 8396 8397 if (!nfs41_sequence_process(task, &lrp->res.seq_res)) 8398 return; 8399 8400 server = NFS_SERVER(lrp->args.inode); 8401 switch (task->tk_status) { 8402 default: 8403 task->tk_status = 0; 8404 case 0: 8405 break; 8406 case -NFS4ERR_DELAY: 8407 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN) 8408 break; 8409 nfs4_sequence_free_slot(&lrp->res.seq_res); 8410 rpc_restart_call_prepare(task); 8411 return; 8412 } 8413 dprintk("<-- %s\n", __func__); 8414 } 8415 8416 static void nfs4_layoutreturn_release(void *calldata) 8417 { 8418 struct nfs4_layoutreturn *lrp = calldata; 8419 struct pnfs_layout_hdr *lo = lrp->args.layout; 8420 LIST_HEAD(freeme); 8421 8422 dprintk("--> %s\n", __func__); 8423 spin_lock(&lo->plh_inode->i_lock); 8424 if (lrp->res.lrs_present) { 8425 pnfs_mark_matching_lsegs_invalid(lo, &freeme, 8426 &lrp->args.range, 8427 be32_to_cpu(lrp->args.stateid.seqid)); 8428 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true); 8429 } else 8430 pnfs_mark_layout_stateid_invalid(lo, &freeme); 8431 pnfs_clear_layoutreturn_waitbit(lo); 8432 spin_unlock(&lo->plh_inode->i_lock); 8433 nfs4_sequence_free_slot(&lrp->res.seq_res); 8434 pnfs_free_lseg_list(&freeme); 8435 pnfs_put_layout_hdr(lrp->args.layout); 8436 nfs_iput_and_deactive(lrp->inode); 8437 kfree(calldata); 8438 dprintk("<-- %s\n", __func__); 8439 } 8440 8441 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { 8442 .rpc_call_prepare = nfs4_layoutreturn_prepare, 8443 .rpc_call_done = nfs4_layoutreturn_done, 8444 .rpc_release = nfs4_layoutreturn_release, 8445 }; 8446 8447 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync) 8448 { 8449 struct rpc_task *task; 8450 struct rpc_message msg = { 8451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], 8452 .rpc_argp = &lrp->args, 8453 .rpc_resp = &lrp->res, 8454 .rpc_cred = lrp->cred, 8455 }; 8456 struct rpc_task_setup task_setup_data = { 8457 .rpc_client = NFS_SERVER(lrp->args.inode)->client, 8458 .rpc_message = &msg, 8459 .callback_ops = &nfs4_layoutreturn_call_ops, 8460 .callback_data = lrp, 8461 }; 8462 int status = 0; 8463 8464 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client, 8465 NFS_SP4_MACH_CRED_PNFS_CLEANUP, 8466 &task_setup_data.rpc_client, &msg); 8467 8468 dprintk("--> %s\n", __func__); 8469 if (!sync) { 8470 lrp->inode = nfs_igrab_and_active(lrp->args.inode); 8471 if (!lrp->inode) { 8472 nfs4_layoutreturn_release(lrp); 8473 return -EAGAIN; 8474 } 8475 task_setup_data.flags |= RPC_TASK_ASYNC; 8476 } 8477 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1); 8478 task = rpc_run_task(&task_setup_data); 8479 if (IS_ERR(task)) 8480 return PTR_ERR(task); 8481 if (sync) 8482 status = task->tk_status; 8483 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status); 8484 dprintk("<-- %s status=%d\n", __func__, status); 8485 rpc_put_task(task); 8486 return status; 8487 } 8488 8489 static int 8490 _nfs4_proc_getdeviceinfo(struct nfs_server *server, 8491 struct pnfs_device *pdev, 8492 struct rpc_cred *cred) 8493 { 8494 struct nfs4_getdeviceinfo_args args = { 8495 .pdev = pdev, 8496 .notify_types = NOTIFY_DEVICEID4_CHANGE | 8497 NOTIFY_DEVICEID4_DELETE, 8498 }; 8499 struct nfs4_getdeviceinfo_res res = { 8500 .pdev = pdev, 8501 }; 8502 struct rpc_message msg = { 8503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], 8504 .rpc_argp = &args, 8505 .rpc_resp = &res, 8506 .rpc_cred = cred, 8507 }; 8508 int status; 8509 8510 dprintk("--> %s\n", __func__); 8511 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); 8512 if (res.notification & ~args.notify_types) 8513 dprintk("%s: unsupported notification\n", __func__); 8514 if (res.notification != args.notify_types) 8515 pdev->nocache = 1; 8516 8517 dprintk("<-- %s status=%d\n", __func__, status); 8518 8519 return status; 8520 } 8521 8522 int nfs4_proc_getdeviceinfo(struct nfs_server *server, 8523 struct pnfs_device *pdev, 8524 struct rpc_cred *cred) 8525 { 8526 struct nfs4_exception exception = { }; 8527 int err; 8528 8529 do { 8530 err = nfs4_handle_exception(server, 8531 _nfs4_proc_getdeviceinfo(server, pdev, cred), 8532 &exception); 8533 } while (exception.retry); 8534 return err; 8535 } 8536 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); 8537 8538 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) 8539 { 8540 struct nfs4_layoutcommit_data *data = calldata; 8541 struct nfs_server *server = NFS_SERVER(data->args.inode); 8542 struct nfs4_session *session = nfs4_get_session(server); 8543 8544 nfs41_setup_sequence(session, 8545 &data->args.seq_args, 8546 &data->res.seq_res, 8547 task); 8548 } 8549 8550 static void 8551 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) 8552 { 8553 struct nfs4_layoutcommit_data *data = calldata; 8554 struct nfs_server *server = NFS_SERVER(data->args.inode); 8555 8556 if (!nfs41_sequence_done(task, &data->res.seq_res)) 8557 return; 8558 8559 switch (task->tk_status) { /* Just ignore these failures */ 8560 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ 8561 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ 8562 case -NFS4ERR_BADLAYOUT: /* no layout */ 8563 case -NFS4ERR_GRACE: /* loca_recalim always false */ 8564 task->tk_status = 0; 8565 case 0: 8566 break; 8567 default: 8568 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { 8569 rpc_restart_call_prepare(task); 8570 return; 8571 } 8572 } 8573 } 8574 8575 static void nfs4_layoutcommit_release(void *calldata) 8576 { 8577 struct nfs4_layoutcommit_data *data = calldata; 8578 8579 pnfs_cleanup_layoutcommit(data); 8580 nfs_post_op_update_inode_force_wcc(data->args.inode, 8581 data->res.fattr); 8582 put_rpccred(data->cred); 8583 nfs_iput_and_deactive(data->inode); 8584 kfree(data); 8585 } 8586 8587 static const struct rpc_call_ops nfs4_layoutcommit_ops = { 8588 .rpc_call_prepare = nfs4_layoutcommit_prepare, 8589 .rpc_call_done = nfs4_layoutcommit_done, 8590 .rpc_release = nfs4_layoutcommit_release, 8591 }; 8592 8593 int 8594 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) 8595 { 8596 struct rpc_message msg = { 8597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], 8598 .rpc_argp = &data->args, 8599 .rpc_resp = &data->res, 8600 .rpc_cred = data->cred, 8601 }; 8602 struct rpc_task_setup task_setup_data = { 8603 .task = &data->task, 8604 .rpc_client = NFS_CLIENT(data->args.inode), 8605 .rpc_message = &msg, 8606 .callback_ops = &nfs4_layoutcommit_ops, 8607 .callback_data = data, 8608 }; 8609 struct rpc_task *task; 8610 int status = 0; 8611 8612 dprintk("NFS: initiating layoutcommit call. sync %d " 8613 "lbw: %llu inode %lu\n", sync, 8614 data->args.lastbytewritten, 8615 data->args.inode->i_ino); 8616 8617 if (!sync) { 8618 data->inode = nfs_igrab_and_active(data->args.inode); 8619 if (data->inode == NULL) { 8620 nfs4_layoutcommit_release(data); 8621 return -EAGAIN; 8622 } 8623 task_setup_data.flags = RPC_TASK_ASYNC; 8624 } 8625 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); 8626 task = rpc_run_task(&task_setup_data); 8627 if (IS_ERR(task)) 8628 return PTR_ERR(task); 8629 if (sync) 8630 status = task->tk_status; 8631 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status); 8632 dprintk("%s: status %d\n", __func__, status); 8633 rpc_put_task(task); 8634 return status; 8635 } 8636 8637 /** 8638 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if 8639 * possible) as per RFC3530bis and RFC5661 Security Considerations sections 8640 */ 8641 static int 8642 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, 8643 struct nfs_fsinfo *info, 8644 struct nfs4_secinfo_flavors *flavors, bool use_integrity) 8645 { 8646 struct nfs41_secinfo_no_name_args args = { 8647 .style = SECINFO_STYLE_CURRENT_FH, 8648 }; 8649 struct nfs4_secinfo_res res = { 8650 .flavors = flavors, 8651 }; 8652 struct rpc_message msg = { 8653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], 8654 .rpc_argp = &args, 8655 .rpc_resp = &res, 8656 }; 8657 struct rpc_clnt *clnt = server->client; 8658 struct rpc_cred *cred = NULL; 8659 int status; 8660 8661 if (use_integrity) { 8662 clnt = server->nfs_client->cl_rpcclient; 8663 cred = nfs4_get_clid_cred(server->nfs_client); 8664 msg.rpc_cred = cred; 8665 } 8666 8667 dprintk("--> %s\n", __func__); 8668 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, 8669 &res.seq_res, 0); 8670 dprintk("<-- %s status=%d\n", __func__, status); 8671 8672 if (cred) 8673 put_rpccred(cred); 8674 8675 return status; 8676 } 8677 8678 static int 8679 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, 8680 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) 8681 { 8682 struct nfs4_exception exception = { }; 8683 int err; 8684 do { 8685 /* first try using integrity protection */ 8686 err = -NFS4ERR_WRONGSEC; 8687 8688 /* try to use integrity protection with machine cred */ 8689 if (_nfs4_is_integrity_protected(server->nfs_client)) 8690 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, 8691 flavors, true); 8692 8693 /* 8694 * if unable to use integrity protection, or SECINFO with 8695 * integrity protection returns NFS4ERR_WRONGSEC (which is 8696 * disallowed by spec, but exists in deployed servers) use 8697 * the current filesystem's rpc_client and the user cred. 8698 */ 8699 if (err == -NFS4ERR_WRONGSEC) 8700 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, 8701 flavors, false); 8702 8703 switch (err) { 8704 case 0: 8705 case -NFS4ERR_WRONGSEC: 8706 case -ENOTSUPP: 8707 goto out; 8708 default: 8709 err = nfs4_handle_exception(server, err, &exception); 8710 } 8711 } while (exception.retry); 8712 out: 8713 return err; 8714 } 8715 8716 static int 8717 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, 8718 struct nfs_fsinfo *info) 8719 { 8720 int err; 8721 struct page *page; 8722 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; 8723 struct nfs4_secinfo_flavors *flavors; 8724 struct nfs4_secinfo4 *secinfo; 8725 int i; 8726 8727 page = alloc_page(GFP_KERNEL); 8728 if (!page) { 8729 err = -ENOMEM; 8730 goto out; 8731 } 8732 8733 flavors = page_address(page); 8734 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); 8735 8736 /* 8737 * Fall back on "guess and check" method if 8738 * the server doesn't support SECINFO_NO_NAME 8739 */ 8740 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { 8741 err = nfs4_find_root_sec(server, fhandle, info); 8742 goto out_freepage; 8743 } 8744 if (err) 8745 goto out_freepage; 8746 8747 for (i = 0; i < flavors->num_flavors; i++) { 8748 secinfo = &flavors->flavors[i]; 8749 8750 switch (secinfo->flavor) { 8751 case RPC_AUTH_NULL: 8752 case RPC_AUTH_UNIX: 8753 case RPC_AUTH_GSS: 8754 flavor = rpcauth_get_pseudoflavor(secinfo->flavor, 8755 &secinfo->flavor_info); 8756 break; 8757 default: 8758 flavor = RPC_AUTH_MAXFLAVOR; 8759 break; 8760 } 8761 8762 if (!nfs_auth_info_match(&server->auth_info, flavor)) 8763 flavor = RPC_AUTH_MAXFLAVOR; 8764 8765 if (flavor != RPC_AUTH_MAXFLAVOR) { 8766 err = nfs4_lookup_root_sec(server, fhandle, 8767 info, flavor); 8768 if (!err) 8769 break; 8770 } 8771 } 8772 8773 if (flavor == RPC_AUTH_MAXFLAVOR) 8774 err = -EPERM; 8775 8776 out_freepage: 8777 put_page(page); 8778 if (err == -EACCES) 8779 return -EPERM; 8780 out: 8781 return err; 8782 } 8783 8784 static int _nfs41_test_stateid(struct nfs_server *server, 8785 nfs4_stateid *stateid, 8786 struct rpc_cred *cred) 8787 { 8788 int status; 8789 struct nfs41_test_stateid_args args = { 8790 .stateid = stateid, 8791 }; 8792 struct nfs41_test_stateid_res res; 8793 struct rpc_message msg = { 8794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], 8795 .rpc_argp = &args, 8796 .rpc_resp = &res, 8797 .rpc_cred = cred, 8798 }; 8799 struct rpc_clnt *rpc_client = server->client; 8800 8801 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, 8802 &rpc_client, &msg); 8803 8804 dprintk("NFS call test_stateid %p\n", stateid); 8805 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); 8806 nfs4_set_sequence_privileged(&args.seq_args); 8807 status = nfs4_call_sync_sequence(rpc_client, server, &msg, 8808 &args.seq_args, &res.seq_res); 8809 if (status != NFS_OK) { 8810 dprintk("NFS reply test_stateid: failed, %d\n", status); 8811 return status; 8812 } 8813 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); 8814 return -res.status; 8815 } 8816 8817 static void nfs4_handle_delay_or_session_error(struct nfs_server *server, 8818 int err, struct nfs4_exception *exception) 8819 { 8820 exception->retry = 0; 8821 switch(err) { 8822 case -NFS4ERR_DELAY: 8823 nfs4_handle_exception(server, err, exception); 8824 break; 8825 case -NFS4ERR_BADSESSION: 8826 case -NFS4ERR_BADSLOT: 8827 case -NFS4ERR_BAD_HIGH_SLOT: 8828 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 8829 case -NFS4ERR_DEADSESSION: 8830 nfs4_do_handle_exception(server, err, exception); 8831 } 8832 } 8833 8834 /** 8835 * nfs41_test_stateid - perform a TEST_STATEID operation 8836 * 8837 * @server: server / transport on which to perform the operation 8838 * @stateid: state ID to test 8839 * @cred: credential 8840 * 8841 * Returns NFS_OK if the server recognizes that "stateid" is valid. 8842 * Otherwise a negative NFS4ERR value is returned if the operation 8843 * failed or the state ID is not currently valid. 8844 */ 8845 static int nfs41_test_stateid(struct nfs_server *server, 8846 nfs4_stateid *stateid, 8847 struct rpc_cred *cred) 8848 { 8849 struct nfs4_exception exception = { }; 8850 int err; 8851 do { 8852 err = _nfs41_test_stateid(server, stateid, cred); 8853 nfs4_handle_delay_or_session_error(server, err, &exception); 8854 } while (exception.retry); 8855 return err; 8856 } 8857 8858 struct nfs_free_stateid_data { 8859 struct nfs_server *server; 8860 struct nfs41_free_stateid_args args; 8861 struct nfs41_free_stateid_res res; 8862 }; 8863 8864 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) 8865 { 8866 struct nfs_free_stateid_data *data = calldata; 8867 nfs41_setup_sequence(nfs4_get_session(data->server), 8868 &data->args.seq_args, 8869 &data->res.seq_res, 8870 task); 8871 } 8872 8873 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) 8874 { 8875 struct nfs_free_stateid_data *data = calldata; 8876 8877 nfs41_sequence_done(task, &data->res.seq_res); 8878 8879 switch (task->tk_status) { 8880 case -NFS4ERR_DELAY: 8881 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) 8882 rpc_restart_call_prepare(task); 8883 } 8884 } 8885 8886 static void nfs41_free_stateid_release(void *calldata) 8887 { 8888 kfree(calldata); 8889 } 8890 8891 static const struct rpc_call_ops nfs41_free_stateid_ops = { 8892 .rpc_call_prepare = nfs41_free_stateid_prepare, 8893 .rpc_call_done = nfs41_free_stateid_done, 8894 .rpc_release = nfs41_free_stateid_release, 8895 }; 8896 8897 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server, 8898 nfs4_stateid *stateid, 8899 struct rpc_cred *cred, 8900 bool privileged) 8901 { 8902 struct rpc_message msg = { 8903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], 8904 .rpc_cred = cred, 8905 }; 8906 struct rpc_task_setup task_setup = { 8907 .rpc_client = server->client, 8908 .rpc_message = &msg, 8909 .callback_ops = &nfs41_free_stateid_ops, 8910 .flags = RPC_TASK_ASYNC, 8911 }; 8912 struct nfs_free_stateid_data *data; 8913 8914 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, 8915 &task_setup.rpc_client, &msg); 8916 8917 dprintk("NFS call free_stateid %p\n", stateid); 8918 data = kmalloc(sizeof(*data), GFP_NOFS); 8919 if (!data) 8920 return ERR_PTR(-ENOMEM); 8921 data->server = server; 8922 nfs4_stateid_copy(&data->args.stateid, stateid); 8923 8924 task_setup.callback_data = data; 8925 8926 msg.rpc_argp = &data->args; 8927 msg.rpc_resp = &data->res; 8928 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); 8929 if (privileged) 8930 nfs4_set_sequence_privileged(&data->args.seq_args); 8931 8932 return rpc_run_task(&task_setup); 8933 } 8934 8935 /** 8936 * nfs41_free_stateid - perform a FREE_STATEID operation 8937 * 8938 * @server: server / transport on which to perform the operation 8939 * @stateid: state ID to release 8940 * @cred: credential 8941 * 8942 * Returns NFS_OK if the server freed "stateid". Otherwise a 8943 * negative NFS4ERR value is returned. 8944 */ 8945 static int nfs41_free_stateid(struct nfs_server *server, 8946 nfs4_stateid *stateid, 8947 struct rpc_cred *cred) 8948 { 8949 struct rpc_task *task; 8950 int ret; 8951 8952 task = _nfs41_free_stateid(server, stateid, cred, true); 8953 if (IS_ERR(task)) 8954 return PTR_ERR(task); 8955 ret = rpc_wait_for_completion_task(task); 8956 if (!ret) 8957 ret = task->tk_status; 8958 rpc_put_task(task); 8959 return ret; 8960 } 8961 8962 static void 8963 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) 8964 { 8965 struct rpc_task *task; 8966 struct rpc_cred *cred = lsp->ls_state->owner->so_cred; 8967 8968 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); 8969 nfs4_free_lock_state(server, lsp); 8970 if (IS_ERR(task)) 8971 return; 8972 rpc_put_task(task); 8973 } 8974 8975 static bool nfs41_match_stateid(const nfs4_stateid *s1, 8976 const nfs4_stateid *s2) 8977 { 8978 if (s1->type != s2->type) 8979 return false; 8980 8981 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) 8982 return false; 8983 8984 if (s1->seqid == s2->seqid) 8985 return true; 8986 if (s1->seqid == 0 || s2->seqid == 0) 8987 return true; 8988 8989 return false; 8990 } 8991 8992 #endif /* CONFIG_NFS_V4_1 */ 8993 8994 static bool nfs4_match_stateid(const nfs4_stateid *s1, 8995 const nfs4_stateid *s2) 8996 { 8997 return nfs4_stateid_match(s1, s2); 8998 } 8999 9000 9001 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { 9002 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, 9003 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, 9004 .recover_open = nfs4_open_reclaim, 9005 .recover_lock = nfs4_lock_reclaim, 9006 .establish_clid = nfs4_init_clientid, 9007 .detect_trunking = nfs40_discover_server_trunking, 9008 }; 9009 9010 #if defined(CONFIG_NFS_V4_1) 9011 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { 9012 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, 9013 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, 9014 .recover_open = nfs4_open_reclaim, 9015 .recover_lock = nfs4_lock_reclaim, 9016 .establish_clid = nfs41_init_clientid, 9017 .reclaim_complete = nfs41_proc_reclaim_complete, 9018 .detect_trunking = nfs41_discover_server_trunking, 9019 }; 9020 #endif /* CONFIG_NFS_V4_1 */ 9021 9022 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { 9023 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, 9024 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, 9025 .recover_open = nfs40_open_expired, 9026 .recover_lock = nfs4_lock_expired, 9027 .establish_clid = nfs4_init_clientid, 9028 }; 9029 9030 #if defined(CONFIG_NFS_V4_1) 9031 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { 9032 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, 9033 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, 9034 .recover_open = nfs41_open_expired, 9035 .recover_lock = nfs41_lock_expired, 9036 .establish_clid = nfs41_init_clientid, 9037 }; 9038 #endif /* CONFIG_NFS_V4_1 */ 9039 9040 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { 9041 .sched_state_renewal = nfs4_proc_async_renew, 9042 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked, 9043 .renew_lease = nfs4_proc_renew, 9044 }; 9045 9046 #if defined(CONFIG_NFS_V4_1) 9047 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { 9048 .sched_state_renewal = nfs41_proc_async_sequence, 9049 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked, 9050 .renew_lease = nfs4_proc_sequence, 9051 }; 9052 #endif 9053 9054 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = { 9055 .get_locations = _nfs40_proc_get_locations, 9056 .fsid_present = _nfs40_proc_fsid_present, 9057 }; 9058 9059 #if defined(CONFIG_NFS_V4_1) 9060 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { 9061 .get_locations = _nfs41_proc_get_locations, 9062 .fsid_present = _nfs41_proc_fsid_present, 9063 }; 9064 #endif /* CONFIG_NFS_V4_1 */ 9065 9066 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { 9067 .minor_version = 0, 9068 .init_caps = NFS_CAP_READDIRPLUS 9069 | NFS_CAP_ATOMIC_OPEN 9070 | NFS_CAP_POSIX_LOCK, 9071 .init_client = nfs40_init_client, 9072 .shutdown_client = nfs40_shutdown_client, 9073 .match_stateid = nfs4_match_stateid, 9074 .find_root_sec = nfs4_find_root_sec, 9075 .free_lock_state = nfs4_release_lockowner, 9076 .test_and_free_expired = nfs40_test_and_free_expired_stateid, 9077 .alloc_seqid = nfs_alloc_seqid, 9078 .call_sync_ops = &nfs40_call_sync_ops, 9079 .reboot_recovery_ops = &nfs40_reboot_recovery_ops, 9080 .nograce_recovery_ops = &nfs40_nograce_recovery_ops, 9081 .state_renewal_ops = &nfs40_state_renewal_ops, 9082 .mig_recovery_ops = &nfs40_mig_recovery_ops, 9083 }; 9084 9085 #if defined(CONFIG_NFS_V4_1) 9086 static struct nfs_seqid * 9087 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2) 9088 { 9089 return NULL; 9090 } 9091 9092 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { 9093 .minor_version = 1, 9094 .init_caps = NFS_CAP_READDIRPLUS 9095 | NFS_CAP_ATOMIC_OPEN 9096 | NFS_CAP_POSIX_LOCK 9097 | NFS_CAP_STATEID_NFSV41 9098 | NFS_CAP_ATOMIC_OPEN_V1, 9099 .init_client = nfs41_init_client, 9100 .shutdown_client = nfs41_shutdown_client, 9101 .match_stateid = nfs41_match_stateid, 9102 .find_root_sec = nfs41_find_root_sec, 9103 .free_lock_state = nfs41_free_lock_state, 9104 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 9105 .alloc_seqid = nfs_alloc_no_seqid, 9106 .session_trunk = nfs4_test_session_trunk, 9107 .call_sync_ops = &nfs41_call_sync_ops, 9108 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 9109 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 9110 .state_renewal_ops = &nfs41_state_renewal_ops, 9111 .mig_recovery_ops = &nfs41_mig_recovery_ops, 9112 }; 9113 #endif 9114 9115 #if defined(CONFIG_NFS_V4_2) 9116 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { 9117 .minor_version = 2, 9118 .init_caps = NFS_CAP_READDIRPLUS 9119 | NFS_CAP_ATOMIC_OPEN 9120 | NFS_CAP_POSIX_LOCK 9121 | NFS_CAP_STATEID_NFSV41 9122 | NFS_CAP_ATOMIC_OPEN_V1 9123 | NFS_CAP_ALLOCATE 9124 | NFS_CAP_COPY 9125 | NFS_CAP_DEALLOCATE 9126 | NFS_CAP_SEEK 9127 | NFS_CAP_LAYOUTSTATS 9128 | NFS_CAP_CLONE, 9129 .init_client = nfs41_init_client, 9130 .shutdown_client = nfs41_shutdown_client, 9131 .match_stateid = nfs41_match_stateid, 9132 .find_root_sec = nfs41_find_root_sec, 9133 .free_lock_state = nfs41_free_lock_state, 9134 .call_sync_ops = &nfs41_call_sync_ops, 9135 .test_and_free_expired = nfs41_test_and_free_expired_stateid, 9136 .alloc_seqid = nfs_alloc_no_seqid, 9137 .session_trunk = nfs4_test_session_trunk, 9138 .reboot_recovery_ops = &nfs41_reboot_recovery_ops, 9139 .nograce_recovery_ops = &nfs41_nograce_recovery_ops, 9140 .state_renewal_ops = &nfs41_state_renewal_ops, 9141 .mig_recovery_ops = &nfs41_mig_recovery_ops, 9142 }; 9143 #endif 9144 9145 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { 9146 [0] = &nfs_v4_0_minor_ops, 9147 #if defined(CONFIG_NFS_V4_1) 9148 [1] = &nfs_v4_1_minor_ops, 9149 #endif 9150 #if defined(CONFIG_NFS_V4_2) 9151 [2] = &nfs_v4_2_minor_ops, 9152 #endif 9153 }; 9154 9155 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size) 9156 { 9157 ssize_t error, error2; 9158 9159 error = generic_listxattr(dentry, list, size); 9160 if (error < 0) 9161 return error; 9162 if (list) { 9163 list += error; 9164 size -= error; 9165 } 9166 9167 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size); 9168 if (error2 < 0) 9169 return error2; 9170 return error + error2; 9171 } 9172 9173 static const struct inode_operations nfs4_dir_inode_operations = { 9174 .create = nfs_create, 9175 .lookup = nfs_lookup, 9176 .atomic_open = nfs_atomic_open, 9177 .link = nfs_link, 9178 .unlink = nfs_unlink, 9179 .symlink = nfs_symlink, 9180 .mkdir = nfs_mkdir, 9181 .rmdir = nfs_rmdir, 9182 .mknod = nfs_mknod, 9183 .rename = nfs_rename, 9184 .permission = nfs_permission, 9185 .getattr = nfs_getattr, 9186 .setattr = nfs_setattr, 9187 .getxattr = generic_getxattr, 9188 .setxattr = generic_setxattr, 9189 .listxattr = nfs4_listxattr, 9190 .removexattr = generic_removexattr, 9191 }; 9192 9193 static const struct inode_operations nfs4_file_inode_operations = { 9194 .permission = nfs_permission, 9195 .getattr = nfs_getattr, 9196 .setattr = nfs_setattr, 9197 .getxattr = generic_getxattr, 9198 .setxattr = generic_setxattr, 9199 .listxattr = nfs4_listxattr, 9200 .removexattr = generic_removexattr, 9201 }; 9202 9203 const struct nfs_rpc_ops nfs_v4_clientops = { 9204 .version = 4, /* protocol version */ 9205 .dentry_ops = &nfs4_dentry_operations, 9206 .dir_inode_ops = &nfs4_dir_inode_operations, 9207 .file_inode_ops = &nfs4_file_inode_operations, 9208 .file_ops = &nfs4_file_operations, 9209 .getroot = nfs4_proc_get_root, 9210 .submount = nfs4_submount, 9211 .try_mount = nfs4_try_mount, 9212 .getattr = nfs4_proc_getattr, 9213 .setattr = nfs4_proc_setattr, 9214 .lookup = nfs4_proc_lookup, 9215 .access = nfs4_proc_access, 9216 .readlink = nfs4_proc_readlink, 9217 .create = nfs4_proc_create, 9218 .remove = nfs4_proc_remove, 9219 .unlink_setup = nfs4_proc_unlink_setup, 9220 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, 9221 .unlink_done = nfs4_proc_unlink_done, 9222 .rename_setup = nfs4_proc_rename_setup, 9223 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, 9224 .rename_done = nfs4_proc_rename_done, 9225 .link = nfs4_proc_link, 9226 .symlink = nfs4_proc_symlink, 9227 .mkdir = nfs4_proc_mkdir, 9228 .rmdir = nfs4_proc_remove, 9229 .readdir = nfs4_proc_readdir, 9230 .mknod = nfs4_proc_mknod, 9231 .statfs = nfs4_proc_statfs, 9232 .fsinfo = nfs4_proc_fsinfo, 9233 .pathconf = nfs4_proc_pathconf, 9234 .set_capabilities = nfs4_server_capabilities, 9235 .decode_dirent = nfs4_decode_dirent, 9236 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, 9237 .read_setup = nfs4_proc_read_setup, 9238 .read_done = nfs4_read_done, 9239 .write_setup = nfs4_proc_write_setup, 9240 .write_done = nfs4_write_done, 9241 .commit_setup = nfs4_proc_commit_setup, 9242 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, 9243 .commit_done = nfs4_commit_done, 9244 .lock = nfs4_proc_lock, 9245 .clear_acl_cache = nfs4_zap_acl_attr, 9246 .close_context = nfs4_close_context, 9247 .open_context = nfs4_atomic_open, 9248 .have_delegation = nfs4_have_delegation, 9249 .return_delegation = nfs4_inode_return_delegation, 9250 .alloc_client = nfs4_alloc_client, 9251 .init_client = nfs4_init_client, 9252 .free_client = nfs4_free_client, 9253 .create_server = nfs4_create_server, 9254 .clone_server = nfs_clone_server, 9255 }; 9256 9257 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { 9258 .name = XATTR_NAME_NFSV4_ACL, 9259 .list = nfs4_xattr_list_nfs4_acl, 9260 .get = nfs4_xattr_get_nfs4_acl, 9261 .set = nfs4_xattr_set_nfs4_acl, 9262 }; 9263 9264 const struct xattr_handler *nfs4_xattr_handlers[] = { 9265 &nfs4_xattr_nfs4_acl_handler, 9266 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 9267 &nfs4_xattr_nfs4_label_handler, 9268 #endif 9269 NULL 9270 }; 9271 9272 /* 9273 * Local variables: 9274 * c-basic-offset: 8 9275 * End: 9276 */ 9277