1 /* 2 * Server-side procedures for NFSv4. 3 * 4 * Copyright (c) 2002 The Regents of the University of Michigan. 5 * All rights reserved. 6 * 7 * Kendrick Smith <kmsmith@umich.edu> 8 * Andy Adamson <andros@umich.edu> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 #include <linux/fs_struct.h> 36 #include <linux/file.h> 37 #include <linux/falloc.h> 38 #include <linux/slab.h> 39 #include <linux/kthread.h> 40 #include <linux/namei.h> 41 42 #include <linux/sunrpc/addr.h> 43 #include <linux/nfs_ssc.h> 44 45 #include "idmap.h" 46 #include "cache.h" 47 #include "xdr4.h" 48 #include "vfs.h" 49 #include "current_stateid.h" 50 #include "netns.h" 51 #include "acl.h" 52 #include "pnfs.h" 53 #include "trace.h" 54 55 static bool inter_copy_offload_enable; 56 module_param(inter_copy_offload_enable, bool, 0644); 57 MODULE_PARM_DESC(inter_copy_offload_enable, 58 "Enable inter server to server copy offload. Default: false"); 59 60 static void cleanup_async_copy(struct nfsd4_copy *copy); 61 62 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 63 static int nfsd4_ssc_umount_timeout = 900000; /* default to 15 mins */ 64 module_param(nfsd4_ssc_umount_timeout, int, 0644); 65 MODULE_PARM_DESC(nfsd4_ssc_umount_timeout, 66 "idle msecs before unmount export from source server"); 67 #endif 68 69 #define NFSDDBG_FACILITY NFSDDBG_PROC 70 71 static u32 nfsd_attrmask[] = { 72 NFSD_WRITEABLE_ATTRS_WORD0, 73 NFSD_WRITEABLE_ATTRS_WORD1, 74 NFSD_WRITEABLE_ATTRS_WORD2 75 }; 76 77 static u32 nfsd41_ex_attrmask[] = { 78 NFSD_SUPPATTR_EXCLCREAT_WORD0, 79 NFSD_SUPPATTR_EXCLCREAT_WORD1, 80 NFSD_SUPPATTR_EXCLCREAT_WORD2 81 }; 82 83 static __be32 84 check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 85 u32 *bmval, u32 *writable) 86 { 87 struct dentry *dentry = cstate->current_fh.fh_dentry; 88 struct svc_export *exp = cstate->current_fh.fh_export; 89 90 if (!nfsd_attrs_supported(cstate->minorversion, bmval)) 91 return nfserr_attrnotsupp; 92 if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry))) 93 return nfserr_attrnotsupp; 94 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) && 95 !(exp->ex_flags & NFSEXP_SECURITY_LABEL)) 96 return nfserr_attrnotsupp; 97 if (writable && !bmval_is_subset(bmval, writable)) 98 return nfserr_inval; 99 if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) && 100 (bmval[1] & FATTR4_WORD1_MODE)) 101 return nfserr_inval; 102 return nfs_ok; 103 } 104 105 static __be32 106 nfsd4_check_open_attributes(struct svc_rqst *rqstp, 107 struct nfsd4_compound_state *cstate, struct nfsd4_open *open) 108 { 109 __be32 status = nfs_ok; 110 111 if (open->op_create == NFS4_OPEN_CREATE) { 112 if (open->op_createmode == NFS4_CREATE_UNCHECKED 113 || open->op_createmode == NFS4_CREATE_GUARDED) 114 status = check_attr_support(rqstp, cstate, 115 open->op_bmval, nfsd_attrmask); 116 else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1) 117 status = check_attr_support(rqstp, cstate, 118 open->op_bmval, nfsd41_ex_attrmask); 119 } 120 121 return status; 122 } 123 124 static int 125 is_create_with_attrs(struct nfsd4_open *open) 126 { 127 return open->op_create == NFS4_OPEN_CREATE 128 && (open->op_createmode == NFS4_CREATE_UNCHECKED 129 || open->op_createmode == NFS4_CREATE_GUARDED 130 || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1); 131 } 132 133 static inline void 134 fh_dup2(struct svc_fh *dst, struct svc_fh *src) 135 { 136 fh_put(dst); 137 dget(src->fh_dentry); 138 if (src->fh_export) 139 exp_get(src->fh_export); 140 *dst = *src; 141 } 142 143 static __be32 144 do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode) 145 { 146 147 if (open->op_truncate && 148 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 149 return nfserr_inval; 150 151 accmode |= NFSD_MAY_READ_IF_EXEC; 152 153 if (open->op_share_access & NFS4_SHARE_ACCESS_READ) 154 accmode |= NFSD_MAY_READ; 155 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) 156 accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC); 157 if (open->op_share_deny & NFS4_SHARE_DENY_READ) 158 accmode |= NFSD_MAY_WRITE; 159 160 return fh_verify(rqstp, current_fh, S_IFREG, accmode); 161 } 162 163 static __be32 nfsd_check_obj_isreg(struct svc_fh *fh, u32 minor_version) 164 { 165 umode_t mode = d_inode(fh->fh_dentry)->i_mode; 166 167 if (S_ISREG(mode)) 168 return nfs_ok; 169 if (S_ISDIR(mode)) 170 return nfserr_isdir; 171 if (S_ISLNK(mode)) 172 return nfserr_symlink; 173 174 /* RFC 7530 - 16.16.6 */ 175 if (minor_version == 0) 176 return nfserr_symlink; 177 else 178 return nfserr_wrong_type; 179 180 } 181 182 static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh) 183 { 184 if (nfsd4_has_session(cstate)) 185 return; 186 fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh, 187 &resfh->fh_handle); 188 } 189 190 static inline bool nfsd4_create_is_exclusive(int createmode) 191 { 192 return createmode == NFS4_CREATE_EXCLUSIVE || 193 createmode == NFS4_CREATE_EXCLUSIVE4_1; 194 } 195 196 static __be32 197 nfsd4_vfs_create(struct svc_fh *fhp, struct dentry **child, 198 struct nfsd4_open *open) 199 { 200 struct file *filp; 201 struct path path; 202 int oflags; 203 204 oflags = O_CREAT | O_LARGEFILE; 205 if (nfsd4_create_is_exclusive(open->op_createmode)) 206 oflags |= O_EXCL; 207 208 switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) { 209 case NFS4_SHARE_ACCESS_WRITE: 210 oflags |= O_WRONLY; 211 break; 212 case NFS4_SHARE_ACCESS_BOTH: 213 oflags |= O_RDWR; 214 break; 215 default: 216 oflags |= O_RDONLY; 217 } 218 219 path.mnt = fhp->fh_export->ex_path.mnt; 220 path.dentry = *child; 221 filp = dentry_create(&path, oflags, open->op_iattr.ia_mode, 222 current_cred()); 223 *child = path.dentry; 224 225 if (IS_ERR(filp)) 226 return nfserrno(PTR_ERR(filp)); 227 228 open->op_filp = filp; 229 return nfs_ok; 230 } 231 232 /* 233 * Implement NFSv4's unchecked, guarded, and exclusive create 234 * semantics for regular files. Open state for this new file is 235 * subsequently fabricated in nfsd4_process_open2(). 236 * 237 * Upon return, caller must release @fhp and @resfhp. 238 */ 239 static __be32 240 nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, 241 struct svc_fh *resfhp, struct nfsd4_open *open) 242 { 243 struct iattr *iap = &open->op_iattr; 244 struct nfsd_attrs attrs = { 245 .na_iattr = iap, 246 .na_seclabel = &open->op_label, 247 }; 248 struct dentry *parent, *child; 249 __u32 v_mtime, v_atime; 250 struct inode *inode; 251 __be32 status; 252 int host_err; 253 254 if (isdotent(open->op_fname, open->op_fnamelen)) 255 return nfserr_exist; 256 if (!(iap->ia_valid & ATTR_MODE)) 257 iap->ia_mode = 0; 258 259 status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); 260 if (status != nfs_ok) 261 return status; 262 parent = fhp->fh_dentry; 263 inode = d_inode(parent); 264 265 host_err = fh_want_write(fhp); 266 if (host_err) 267 return nfserrno(host_err); 268 269 if (is_create_with_attrs(open)) 270 nfsd4_acl_to_attr(NF4REG, open->op_acl, &attrs); 271 272 child = start_creating(&nop_mnt_idmap, parent, 273 &QSTR_LEN(open->op_fname, open->op_fnamelen)); 274 if (IS_ERR(child)) { 275 status = nfserrno(PTR_ERR(child)); 276 goto out_write; 277 } 278 279 if (d_really_is_negative(child)) { 280 status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); 281 if (status != nfs_ok) 282 goto out; 283 } 284 285 status = fh_compose(resfhp, fhp->fh_export, child, fhp); 286 if (status != nfs_ok) 287 goto out; 288 289 v_mtime = 0; 290 v_atime = 0; 291 if (nfsd4_create_is_exclusive(open->op_createmode)) { 292 u32 *verifier = (u32 *)open->op_verf.data; 293 294 /* 295 * Solaris 7 gets confused (bugid 4218508) if these have 296 * the high bit set, as do xfs filesystems without the 297 * "bigtime" feature. So just clear the high bits. If this 298 * is ever changed to use different attrs for storing the 299 * verifier, then do_open_lookup() will also need to be 300 * fixed accordingly. 301 */ 302 v_mtime = verifier[0] & 0x7fffffff; 303 v_atime = verifier[1] & 0x7fffffff; 304 } 305 306 if (d_really_is_positive(child)) { 307 /* NFSv4 protocol requires change attributes even though 308 * no change happened. 309 */ 310 status = fh_fill_both_attrs(fhp); 311 if (status != nfs_ok) 312 goto out; 313 314 switch (open->op_createmode) { 315 case NFS4_CREATE_UNCHECKED: 316 if (!d_is_reg(child)) 317 break; 318 319 /* 320 * In NFSv4, we don't want to truncate the file 321 * now. This would be wrong if the OPEN fails for 322 * some other reason. Furthermore, if the size is 323 * nonzero, we should ignore it according to spec! 324 */ 325 open->op_truncate = (iap->ia_valid & ATTR_SIZE) && 326 !iap->ia_size; 327 break; 328 case NFS4_CREATE_GUARDED: 329 status = nfserr_exist; 330 break; 331 case NFS4_CREATE_EXCLUSIVE: 332 if (inode_get_mtime_sec(d_inode(child)) == v_mtime && 333 inode_get_atime_sec(d_inode(child)) == v_atime && 334 d_inode(child)->i_size == 0) { 335 open->op_created = true; 336 break; /* subtle */ 337 } 338 status = nfserr_exist; 339 break; 340 case NFS4_CREATE_EXCLUSIVE4_1: 341 if (inode_get_mtime_sec(d_inode(child)) == v_mtime && 342 inode_get_atime_sec(d_inode(child)) == v_atime && 343 d_inode(child)->i_size == 0) { 344 open->op_created = true; 345 goto set_attr; /* subtle */ 346 } 347 status = nfserr_exist; 348 } 349 goto out; 350 } 351 352 if (!IS_POSIXACL(inode)) 353 iap->ia_mode &= ~current_umask(); 354 355 status = fh_fill_pre_attrs(fhp); 356 if (status != nfs_ok) 357 goto out; 358 status = nfsd4_vfs_create(fhp, &child, open); 359 if (status != nfs_ok) 360 goto out; 361 open->op_created = true; 362 fh_fill_post_attrs(fhp); 363 364 /* A newly created file already has a file size of zero. */ 365 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) 366 iap->ia_valid &= ~ATTR_SIZE; 367 if (nfsd4_create_is_exclusive(open->op_createmode)) { 368 iap->ia_valid = ATTR_MTIME | ATTR_ATIME | 369 ATTR_MTIME_SET|ATTR_ATIME_SET; 370 iap->ia_mtime.tv_sec = v_mtime; 371 iap->ia_atime.tv_sec = v_atime; 372 iap->ia_mtime.tv_nsec = 0; 373 iap->ia_atime.tv_nsec = 0; 374 } 375 376 set_attr: 377 status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs); 378 379 if (attrs.na_labelerr) 380 open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL; 381 if (attrs.na_aclerr) 382 open->op_bmval[0] &= ~FATTR4_WORD0_ACL; 383 out: 384 end_creating(child); 385 nfsd_attrs_free(&attrs); 386 out_write: 387 fh_drop_write(fhp); 388 return status; 389 } 390 391 /** 392 * set_change_info - set up the change_info4 for a reply 393 * @cinfo: pointer to nfsd4_change_info to be populated 394 * @fhp: pointer to svc_fh to use as source 395 * 396 * Many operations in NFSv4 require change_info4 in the reply. This function 397 * populates that from the info that we (should!) have already collected. In 398 * the event that we didn't get any pre-attrs, just zero out both. 399 */ 400 static void 401 set_change_info(struct nfsd4_change_info *cinfo, struct svc_fh *fhp) 402 { 403 cinfo->atomic = (u32)(fhp->fh_pre_saved && fhp->fh_post_saved && !fhp->fh_no_atomic_attr); 404 cinfo->before_change = fhp->fh_pre_change; 405 cinfo->after_change = fhp->fh_post_change; 406 407 /* 408 * If fetching the pre-change attributes failed, then we should 409 * have already failed the whole operation. We could have still 410 * failed to fetch post-change attributes however. 411 * 412 * If we didn't get post-op attrs, just zero-out the after 413 * field since we don't know what it should be. If the pre_saved 414 * field isn't set for some reason, throw warning and just copy 415 * whatever is in the after field. 416 */ 417 if (WARN_ON_ONCE(!fhp->fh_pre_saved)) 418 cinfo->before_change = 0; 419 if (!fhp->fh_post_saved) 420 cinfo->after_change = cinfo->before_change + 1; 421 } 422 423 static __be32 424 do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh) 425 { 426 struct svc_fh *current_fh = &cstate->current_fh; 427 int accmode; 428 __be32 status; 429 430 *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL); 431 if (!*resfh) 432 return nfserr_jukebox; 433 fh_init(*resfh, NFS4_FHSIZE); 434 open->op_truncate = false; 435 436 if (open->op_create) { 437 /* FIXME: check session persistence and pnfs flags. 438 * The nfsv4.1 spec requires the following semantics: 439 * 440 * Persistent | pNFS | Server REQUIRED | Client Allowed 441 * Reply Cache | server | | 442 * -------------+--------+-----------------+-------------------- 443 * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1 444 * | | | (SHOULD) 445 * | | and EXCLUSIVE4 | or EXCLUSIVE4 446 * | | | (SHOULD NOT) 447 * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1 448 * yes | no | GUARDED4 | GUARDED4 449 * yes | yes | GUARDED4 | GUARDED4 450 */ 451 452 current->fs->umask = open->op_umask; 453 status = nfsd4_create_file(rqstp, current_fh, *resfh, open); 454 current->fs->umask = 0; 455 456 /* 457 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4 458 * use the returned bitmask to indicate which attributes 459 * we used to store the verifier: 460 */ 461 if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0) 462 open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS | 463 FATTR4_WORD1_TIME_MODIFY); 464 } else { 465 status = nfsd_lookup(rqstp, current_fh, 466 open->op_fname, open->op_fnamelen, *resfh); 467 if (status == nfs_ok) 468 /* NFSv4 protocol requires change attributes even though 469 * no change happened. 470 */ 471 status = fh_fill_both_attrs(current_fh); 472 } 473 if (status) 474 goto out; 475 status = nfsd_check_obj_isreg(*resfh, cstate->minorversion); 476 if (status) 477 goto out; 478 479 nfsd4_set_open_owner_reply_cache(cstate, open, *resfh); 480 accmode = NFSD_MAY_NOP; 481 if (open->op_created || 482 open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) 483 accmode |= NFSD_MAY_OWNER_OVERRIDE; 484 status = do_open_permission(rqstp, *resfh, open, accmode); 485 set_change_info(&open->op_cinfo, current_fh); 486 out: 487 return status; 488 } 489 490 static __be32 491 do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open) 492 { 493 struct svc_fh *current_fh = &cstate->current_fh; 494 int accmode = 0; 495 496 /* We don't know the target directory, and therefore can not 497 * set the change info 498 */ 499 500 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info)); 501 502 nfsd4_set_open_owner_reply_cache(cstate, open, current_fh); 503 504 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) && 505 (open->op_iattr.ia_size == 0); 506 /* 507 * In the delegation case, the client is telling us about an 508 * open that it *already* performed locally, some time ago. We 509 * should let it succeed now if possible. 510 * 511 * In the case of a CLAIM_FH open, on the other hand, the client 512 * may be counting on us to enforce permissions (the Linux 4.1 513 * client uses this for normal opens, for example). 514 */ 515 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH) 516 accmode = NFSD_MAY_OWNER_OVERRIDE; 517 518 return do_open_permission(rqstp, current_fh, open, accmode); 519 } 520 521 static void 522 copy_clientid(clientid_t *clid, struct nfsd4_session *session) 523 { 524 struct nfsd4_sessionid *sid = 525 (struct nfsd4_sessionid *)session->se_sessionid.data; 526 527 clid->cl_boot = sid->clientid.cl_boot; 528 clid->cl_id = sid->clientid.cl_id; 529 } 530 531 static __be32 532 nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 533 union nfsd4_op_u *u) 534 { 535 struct nfsd4_open *open = &u->open; 536 __be32 status; 537 struct svc_fh *resfh = NULL; 538 struct net *net = SVC_NET(rqstp); 539 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 540 bool reclaim = false; 541 542 dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n", 543 (int)open->op_fnamelen, open->op_fname, 544 open->op_openowner); 545 546 open->op_filp = NULL; 547 open->op_rqstp = rqstp; 548 549 /* This check required by spec. */ 550 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL) 551 return nfserr_inval; 552 553 open->op_created = false; 554 /* 555 * RFC5661 18.51.3 556 * Before RECLAIM_COMPLETE done, server should deny new lock 557 */ 558 if (nfsd4_has_session(cstate) && 559 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) && 560 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS) 561 return nfserr_grace; 562 563 if (nfsd4_has_session(cstate)) 564 copy_clientid(&open->op_clientid, cstate->session); 565 566 /* check seqid for replay. set nfs4_owner */ 567 status = nfsd4_process_open1(cstate, open, nn); 568 if (status == nfserr_replay_me) { 569 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay; 570 fh_put(&cstate->current_fh); 571 fh_copy_shallow(&cstate->current_fh.fh_handle, 572 &rp->rp_openfh); 573 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP); 574 if (status) 575 dprintk("nfsd4_open: replay failed" 576 " restoring previous filehandle\n"); 577 else 578 status = nfserr_replay_me; 579 } 580 if (status) 581 goto out; 582 if (open->op_xdr_error) { 583 status = open->op_xdr_error; 584 goto out; 585 } 586 587 status = nfsd4_check_open_attributes(rqstp, cstate, open); 588 if (status) 589 goto out; 590 591 /* Openowner is now set, so sequence id will get bumped. Now we need 592 * these checks before we do any creates: */ 593 status = nfserr_grace; 594 if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS) 595 goto out; 596 status = nfserr_no_grace; 597 if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 598 goto out; 599 600 switch (open->op_claim_type) { 601 case NFS4_OPEN_CLAIM_DELEGATE_CUR: 602 case NFS4_OPEN_CLAIM_NULL: 603 status = do_open_lookup(rqstp, cstate, open, &resfh); 604 if (status) 605 goto out; 606 break; 607 case NFS4_OPEN_CLAIM_PREVIOUS: 608 status = nfs4_check_open_reclaim(cstate->clp); 609 if (status) 610 goto out; 611 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 612 reclaim = true; 613 fallthrough; 614 case NFS4_OPEN_CLAIM_FH: 615 case NFS4_OPEN_CLAIM_DELEG_CUR_FH: 616 status = do_open_fhandle(rqstp, cstate, open); 617 if (status) 618 goto out; 619 resfh = &cstate->current_fh; 620 break; 621 case NFS4_OPEN_CLAIM_DELEG_PREV_FH: 622 case NFS4_OPEN_CLAIM_DELEGATE_PREV: 623 status = nfserr_notsupp; 624 goto out; 625 default: 626 status = nfserr_inval; 627 goto out; 628 } 629 630 status = nfsd4_process_open2(rqstp, resfh, open); 631 if (status && open->op_created) 632 pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n", 633 be32_to_cpu(status)); 634 if (reclaim && !status) 635 nn->somebody_reclaimed = true; 636 out: 637 if (open->op_filp) { 638 fput(open->op_filp); 639 open->op_filp = NULL; 640 } 641 if (resfh && resfh != &cstate->current_fh) { 642 fh_dup2(&cstate->current_fh, resfh); 643 fh_put(resfh); 644 kfree(resfh); 645 } 646 nfsd4_cleanup_open_state(cstate, open); 647 nfsd4_bump_seqid(cstate, status); 648 return status; 649 } 650 651 /* 652 * OPEN is the only seqid-mutating operation whose decoding can fail 653 * with a seqid-mutating error (specifically, decoding of user names in 654 * the attributes). Therefore we have to do some processing to look up 655 * the stateowner so that we can bump the seqid. 656 */ 657 static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op) 658 { 659 struct nfsd4_open *open = &op->u.open; 660 661 if (!seqid_mutating_err(ntohl(op->status))) 662 return op->status; 663 if (nfsd4_has_session(cstate)) 664 return op->status; 665 open->op_xdr_error = op->status; 666 return nfsd4_open(rqstp, cstate, &op->u); 667 } 668 669 /* 670 * filehandle-manipulating ops. 671 */ 672 static __be32 673 nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 674 union nfsd4_op_u *u) 675 { 676 u->getfh = &cstate->current_fh; 677 return nfs_ok; 678 } 679 680 static __be32 681 nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 682 union nfsd4_op_u *u) 683 { 684 struct nfsd4_putfh *putfh = &u->putfh; 685 __be32 ret; 686 687 fh_put(&cstate->current_fh); 688 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen; 689 memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval, 690 putfh->pf_fhlen); 691 ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS); 692 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 693 if (ret == nfserr_stale && putfh->no_verify) { 694 SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN); 695 ret = 0; 696 } 697 #endif 698 return ret; 699 } 700 701 static __be32 702 nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 703 union nfsd4_op_u *u) 704 { 705 fh_put(&cstate->current_fh); 706 707 return exp_pseudoroot(rqstp, &cstate->current_fh); 708 } 709 710 static __be32 711 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 712 union nfsd4_op_u *u) 713 { 714 if (!cstate->save_fh.fh_dentry) 715 return nfserr_restorefh; 716 717 fh_dup2(&cstate->current_fh, &cstate->save_fh); 718 if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) { 719 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t)); 720 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 721 } 722 return nfs_ok; 723 } 724 725 static __be32 726 nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 727 union nfsd4_op_u *u) 728 { 729 fh_dup2(&cstate->save_fh, &cstate->current_fh); 730 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) { 731 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t)); 732 SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG); 733 } 734 return nfs_ok; 735 } 736 737 /* 738 * misc nfsv4 ops 739 */ 740 static __be32 741 nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 742 union nfsd4_op_u *u) 743 { 744 struct nfsd4_access *access = &u->access; 745 u32 access_full; 746 747 access_full = NFS3_ACCESS_FULL; 748 if (cstate->minorversion >= 2) 749 access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD | 750 NFS4_ACCESS_XAWRITE; 751 752 if (access->ac_req_access & ~access_full) 753 return nfserr_inval; 754 755 access->ac_resp_access = access->ac_req_access; 756 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access, 757 &access->ac_supported); 758 } 759 760 static __be32 761 nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 762 union nfsd4_op_u *u) 763 { 764 struct nfsd4_commit *commit = &u->commit; 765 struct nfsd_file *nf; 766 __be32 status; 767 768 status = nfsd_file_acquire(rqstp, &cstate->current_fh, NFSD_MAY_WRITE | 769 NFSD_MAY_NOT_BREAK_LEASE, &nf); 770 if (status != nfs_ok) 771 return status; 772 773 status = nfsd_commit(rqstp, &cstate->current_fh, nf, commit->co_offset, 774 commit->co_count, 775 (__be32 *)commit->co_verf.data); 776 nfsd_file_put(nf); 777 return status; 778 } 779 780 static __be32 781 nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 782 union nfsd4_op_u *u) 783 { 784 struct nfsd4_create *create = &u->create; 785 struct nfsd_attrs attrs = { 786 .na_iattr = &create->cr_iattr, 787 .na_seclabel = &create->cr_label, 788 }; 789 struct svc_fh resfh; 790 __be32 status; 791 dev_t rdev; 792 793 fh_init(&resfh, NFS4_FHSIZE); 794 795 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP); 796 if (status) 797 return status; 798 799 status = check_attr_support(rqstp, cstate, create->cr_bmval, 800 nfsd_attrmask); 801 if (status) 802 return status; 803 804 status = nfsd4_acl_to_attr(create->cr_type, create->cr_acl, &attrs); 805 current->fs->umask = create->cr_umask; 806 switch (create->cr_type) { 807 case NF4LNK: 808 status = nfsd_symlink(rqstp, &cstate->current_fh, 809 create->cr_name, create->cr_namelen, 810 create->cr_data, &attrs, &resfh); 811 break; 812 813 case NF4BLK: 814 status = nfserr_inval; 815 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2); 816 if (MAJOR(rdev) != create->cr_specdata1 || 817 MINOR(rdev) != create->cr_specdata2) 818 goto out_umask; 819 status = nfsd_create(rqstp, &cstate->current_fh, 820 create->cr_name, create->cr_namelen, 821 &attrs, S_IFBLK, rdev, &resfh); 822 break; 823 824 case NF4CHR: 825 status = nfserr_inval; 826 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2); 827 if (MAJOR(rdev) != create->cr_specdata1 || 828 MINOR(rdev) != create->cr_specdata2) 829 goto out_umask; 830 status = nfsd_create(rqstp, &cstate->current_fh, 831 create->cr_name, create->cr_namelen, 832 &attrs, S_IFCHR, rdev, &resfh); 833 break; 834 835 case NF4SOCK: 836 status = nfsd_create(rqstp, &cstate->current_fh, 837 create->cr_name, create->cr_namelen, 838 &attrs, S_IFSOCK, 0, &resfh); 839 break; 840 841 case NF4FIFO: 842 status = nfsd_create(rqstp, &cstate->current_fh, 843 create->cr_name, create->cr_namelen, 844 &attrs, S_IFIFO, 0, &resfh); 845 break; 846 847 case NF4DIR: 848 create->cr_iattr.ia_valid &= ~ATTR_SIZE; 849 status = nfsd_create(rqstp, &cstate->current_fh, 850 create->cr_name, create->cr_namelen, 851 &attrs, S_IFDIR, 0, &resfh); 852 break; 853 854 default: 855 status = nfserr_badtype; 856 } 857 858 if (status) 859 goto out; 860 861 if (attrs.na_labelerr) 862 create->cr_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL; 863 if (attrs.na_aclerr) 864 create->cr_bmval[0] &= ~FATTR4_WORD0_ACL; 865 set_change_info(&create->cr_cinfo, &cstate->current_fh); 866 fh_dup2(&cstate->current_fh, &resfh); 867 out: 868 fh_put(&resfh); 869 out_umask: 870 current->fs->umask = 0; 871 nfsd_attrs_free(&attrs); 872 return status; 873 } 874 875 static __be32 876 nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 877 union nfsd4_op_u *u) 878 { 879 struct nfsd4_getattr *getattr = &u->getattr; 880 __be32 status; 881 882 trace_nfsd_vfs_getattr(rqstp, &cstate->current_fh); 883 884 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP); 885 if (status) 886 return status; 887 888 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1) 889 return nfserr_inval; 890 891 getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0]; 892 getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1]; 893 getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2]; 894 895 getattr->ga_fhp = &cstate->current_fh; 896 return nfs_ok; 897 } 898 899 static __be32 900 nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 901 union nfsd4_op_u *u) 902 { 903 struct nfsd4_link *link = &u->link; 904 __be32 status; 905 906 status = nfsd_link(rqstp, &cstate->current_fh, 907 link->li_name, link->li_namelen, &cstate->save_fh); 908 if (!status) 909 set_change_info(&link->li_cinfo, &cstate->current_fh); 910 return status; 911 } 912 913 static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh) 914 { 915 struct svc_fh tmp_fh; 916 __be32 ret; 917 918 fh_init(&tmp_fh, NFS4_FHSIZE); 919 ret = exp_pseudoroot(rqstp, &tmp_fh); 920 if (ret) 921 return ret; 922 if (tmp_fh.fh_dentry == fh->fh_dentry) { 923 fh_put(&tmp_fh); 924 return nfserr_noent; 925 } 926 fh_put(&tmp_fh); 927 return nfsd_lookup(rqstp, fh, "..", 2, fh); 928 } 929 930 static __be32 931 nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 932 union nfsd4_op_u *u) 933 { 934 return nfsd4_do_lookupp(rqstp, &cstate->current_fh); 935 } 936 937 static __be32 938 nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 939 union nfsd4_op_u *u) 940 { 941 return nfsd_lookup(rqstp, &cstate->current_fh, 942 u->lookup.lo_name, u->lookup.lo_len, 943 &cstate->current_fh); 944 } 945 946 static __be32 947 nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 948 union nfsd4_op_u *u) 949 { 950 struct nfsd4_read *read = &u->read; 951 __be32 status; 952 953 read->rd_nf = NULL; 954 955 trace_nfsd_read_start(rqstp, &cstate->current_fh, 956 read->rd_offset, read->rd_length); 957 958 read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp)); 959 if (read->rd_offset > (u64)OFFSET_MAX) 960 read->rd_offset = (u64)OFFSET_MAX; 961 if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX) 962 read->rd_length = (u64)OFFSET_MAX - read->rd_offset; 963 964 /* 965 * If we do a zero copy read, then a client will see read data 966 * that reflects the state of the file *after* performing the 967 * following compound. 968 * 969 * To ensure proper ordering, we therefore turn off zero copy if 970 * the client wants us to do more in this compound: 971 */ 972 if (!nfsd4_last_compound_op(rqstp)) { 973 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 974 975 argp->splice_ok = false; 976 } 977 978 /* check stateid */ 979 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 980 &read->rd_stateid, RD_STATE, 981 &read->rd_nf, NULL); 982 983 read->rd_rqstp = rqstp; 984 read->rd_fhp = &cstate->current_fh; 985 return status; 986 } 987 988 989 static void 990 nfsd4_read_release(union nfsd4_op_u *u) 991 { 992 if (u->read.rd_nf) { 993 trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp, 994 u->read.rd_offset, u->read.rd_length); 995 nfsd_file_put(u->read.rd_nf); 996 } 997 } 998 999 static __be32 1000 nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1001 union nfsd4_op_u *u) 1002 { 1003 struct nfsd4_readdir *readdir = &u->readdir; 1004 u64 cookie = readdir->rd_cookie; 1005 static const nfs4_verifier zeroverf; 1006 1007 trace_nfsd_vfs_readdir(rqstp, &cstate->current_fh, 1008 readdir->rd_maxcount, readdir->rd_cookie); 1009 1010 /* no need to check permission - this will be done in nfsd_readdir() */ 1011 1012 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1) 1013 return nfserr_inval; 1014 1015 readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0]; 1016 readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1]; 1017 readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2]; 1018 1019 if ((cookie == 1) || (cookie == 2) || 1020 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE))) 1021 return nfserr_bad_cookie; 1022 1023 readdir->rd_rqstp = rqstp; 1024 readdir->rd_fhp = &cstate->current_fh; 1025 return nfs_ok; 1026 } 1027 1028 static __be32 1029 nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1030 union nfsd4_op_u *u) 1031 { 1032 u->readlink.rl_rqstp = rqstp; 1033 u->readlink.rl_fhp = &cstate->current_fh; 1034 return nfs_ok; 1035 } 1036 1037 static __be32 1038 nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1039 union nfsd4_op_u *u) 1040 { 1041 struct nfsd4_remove *remove = &u->remove; 1042 __be32 status; 1043 1044 if (opens_in_grace(SVC_NET(rqstp))) 1045 return nfserr_grace; 1046 status = nfsd_unlink(rqstp, &cstate->current_fh, 0, 1047 remove->rm_name, remove->rm_namelen); 1048 if (!status) 1049 set_change_info(&remove->rm_cinfo, &cstate->current_fh); 1050 return status; 1051 } 1052 1053 static __be32 1054 nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1055 union nfsd4_op_u *u) 1056 { 1057 struct nfsd4_rename *rename = &u->rename; 1058 __be32 status; 1059 1060 if (opens_in_grace(SVC_NET(rqstp))) 1061 return nfserr_grace; 1062 status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname, 1063 rename->rn_snamelen, &cstate->current_fh, 1064 rename->rn_tname, rename->rn_tnamelen); 1065 if (status) 1066 return status; 1067 set_change_info(&rename->rn_sinfo, &cstate->save_fh); 1068 set_change_info(&rename->rn_tinfo, &cstate->current_fh); 1069 return nfs_ok; 1070 } 1071 1072 static __be32 1073 nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1074 union nfsd4_op_u *u) 1075 { 1076 struct nfsd4_secinfo *secinfo = &u->secinfo; 1077 struct svc_export *exp; 1078 struct dentry *dentry; 1079 __be32 err; 1080 1081 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC); 1082 if (err) 1083 return err; 1084 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh, 1085 secinfo->si_name, secinfo->si_namelen, 1086 &exp, &dentry); 1087 if (err) 1088 return err; 1089 if (d_really_is_negative(dentry)) { 1090 exp_put(exp); 1091 err = nfserr_noent; 1092 } else 1093 secinfo->si_exp = exp; 1094 dput(dentry); 1095 if (cstate->minorversion) 1096 /* See rfc 5661 section 2.6.3.1.1.8 */ 1097 fh_put(&cstate->current_fh); 1098 return err; 1099 } 1100 1101 static __be32 1102 nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1103 union nfsd4_op_u *u) 1104 { 1105 __be32 err; 1106 1107 switch (u->secinfo_no_name.sin_style) { 1108 case NFS4_SECINFO_STYLE4_CURRENT_FH: 1109 break; 1110 case NFS4_SECINFO_STYLE4_PARENT: 1111 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh); 1112 if (err) 1113 return err; 1114 break; 1115 default: 1116 return nfserr_inval; 1117 } 1118 1119 u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export); 1120 fh_put(&cstate->current_fh); 1121 return nfs_ok; 1122 } 1123 1124 static void 1125 nfsd4_secinfo_release(union nfsd4_op_u *u) 1126 { 1127 if (u->secinfo.si_exp) 1128 exp_put(u->secinfo.si_exp); 1129 } 1130 1131 static void 1132 nfsd4_secinfo_no_name_release(union nfsd4_op_u *u) 1133 { 1134 if (u->secinfo_no_name.sin_exp) 1135 exp_put(u->secinfo_no_name.sin_exp); 1136 } 1137 1138 /* 1139 * Validate that the requested timestamps are within the acceptable range. If 1140 * timestamp appears to be in the future, then it will be clamped to 1141 * current_time(). 1142 */ 1143 static void 1144 vet_deleg_attrs(struct nfsd4_setattr *setattr, struct nfs4_delegation *dp) 1145 { 1146 struct timespec64 now = current_time(dp->dl_stid.sc_file->fi_inode); 1147 struct iattr *iattr = &setattr->sa_iattr; 1148 1149 if ((setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) && 1150 !nfsd4_vet_deleg_time(&iattr->ia_atime, &dp->dl_atime, &now)) 1151 iattr->ia_valid &= ~(ATTR_ATIME | ATTR_ATIME_SET); 1152 1153 if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_MODIFY) { 1154 if (nfsd4_vet_deleg_time(&iattr->ia_mtime, &dp->dl_mtime, &now)) { 1155 iattr->ia_ctime = iattr->ia_mtime; 1156 if (nfsd4_vet_deleg_time(&iattr->ia_ctime, &dp->dl_ctime, &now)) 1157 dp->dl_setattr = true; 1158 else 1159 iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET); 1160 } else { 1161 iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET | 1162 ATTR_MTIME | ATTR_MTIME_SET); 1163 } 1164 } 1165 } 1166 1167 static __be32 1168 nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1169 union nfsd4_op_u *u) 1170 { 1171 struct nfsd4_setattr *setattr = &u->setattr; 1172 struct nfsd_attrs attrs = { 1173 .na_iattr = &setattr->sa_iattr, 1174 .na_seclabel = &setattr->sa_label, 1175 }; 1176 bool save_no_wcc, deleg_attrs; 1177 struct nfs4_stid *st = NULL; 1178 struct inode *inode; 1179 __be32 status = nfs_ok; 1180 int err; 1181 1182 deleg_attrs = setattr->sa_bmval[2] & (FATTR4_WORD2_TIME_DELEG_ACCESS | 1183 FATTR4_WORD2_TIME_DELEG_MODIFY); 1184 1185 if (deleg_attrs || (setattr->sa_iattr.ia_valid & ATTR_SIZE)) { 1186 int flags = WR_STATE; 1187 1188 if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) 1189 flags |= RD_STATE; 1190 1191 status = nfs4_preprocess_stateid_op(rqstp, cstate, 1192 &cstate->current_fh, &setattr->sa_stateid, 1193 flags, NULL, &st); 1194 if (status) 1195 return status; 1196 } 1197 1198 if (deleg_attrs) { 1199 status = nfserr_bad_stateid; 1200 if (st->sc_type & SC_TYPE_DELEG) { 1201 struct nfs4_delegation *dp = delegstateid(st); 1202 1203 /* Only for *_ATTRS_DELEG flavors */ 1204 if (deleg_attrs_deleg(dp->dl_type)) { 1205 vet_deleg_attrs(setattr, dp); 1206 status = nfs_ok; 1207 } 1208 } 1209 } 1210 if (st) 1211 nfs4_put_stid(st); 1212 if (status) 1213 return status; 1214 1215 err = fh_want_write(&cstate->current_fh); 1216 if (err) 1217 return nfserrno(err); 1218 status = nfs_ok; 1219 1220 status = check_attr_support(rqstp, cstate, setattr->sa_bmval, 1221 nfsd_attrmask); 1222 if (status) 1223 goto out; 1224 1225 inode = cstate->current_fh.fh_dentry->d_inode; 1226 status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG, 1227 setattr->sa_acl, &attrs); 1228 1229 if (status) 1230 goto out; 1231 save_no_wcc = cstate->current_fh.fh_no_wcc; 1232 cstate->current_fh.fh_no_wcc = true; 1233 status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs, NULL); 1234 cstate->current_fh.fh_no_wcc = save_no_wcc; 1235 if (!status) 1236 status = nfserrno(attrs.na_labelerr); 1237 if (!status) 1238 status = nfserrno(attrs.na_aclerr); 1239 out: 1240 nfsd_attrs_free(&attrs); 1241 fh_drop_write(&cstate->current_fh); 1242 return status; 1243 } 1244 1245 static void nfsd4_file_mark_deleg_written(struct nfs4_file *fi) 1246 { 1247 spin_lock(&fi->fi_lock); 1248 if (!list_empty(&fi->fi_delegations)) { 1249 struct nfs4_delegation *dp = list_first_entry(&fi->fi_delegations, 1250 struct nfs4_delegation, dl_perfile); 1251 1252 if (dp->dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG) 1253 dp->dl_written = true; 1254 } 1255 spin_unlock(&fi->fi_lock); 1256 } 1257 1258 static __be32 1259 nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1260 union nfsd4_op_u *u) 1261 { 1262 struct nfsd4_write *write = &u->write; 1263 stateid_t *stateid = &write->wr_stateid; 1264 struct nfs4_stid *stid = NULL; 1265 struct nfsd_file *nf = NULL; 1266 __be32 status = nfs_ok; 1267 unsigned long cnt; 1268 1269 if (write->wr_offset > (u64)OFFSET_MAX || 1270 write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX) 1271 return nfserr_fbig; 1272 1273 cnt = write->wr_buflen; 1274 trace_nfsd_write_start(rqstp, &cstate->current_fh, 1275 write->wr_offset, cnt); 1276 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 1277 stateid, WR_STATE, &nf, &stid); 1278 if (status) 1279 return status; 1280 1281 if (stid) { 1282 nfsd4_file_mark_deleg_written(stid->sc_file); 1283 nfs4_put_stid(stid); 1284 } 1285 1286 write->wr_how_written = write->wr_stable_how; 1287 status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf, 1288 write->wr_offset, &write->wr_payload, 1289 &cnt, write->wr_how_written, 1290 (__be32 *)write->wr_verifier.data); 1291 nfsd_file_put(nf); 1292 1293 write->wr_bytes_written = cnt; 1294 trace_nfsd_write_done(rqstp, &cstate->current_fh, 1295 write->wr_offset, cnt); 1296 return status; 1297 } 1298 1299 static __be32 1300 nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1301 stateid_t *src_stateid, struct nfsd_file **src, 1302 stateid_t *dst_stateid, struct nfsd_file **dst) 1303 { 1304 __be32 status; 1305 1306 if (!cstate->save_fh.fh_dentry) 1307 return nfserr_nofilehandle; 1308 1309 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh, 1310 src_stateid, RD_STATE, src, NULL); 1311 if (status) 1312 goto out; 1313 1314 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 1315 dst_stateid, WR_STATE, dst, NULL); 1316 if (status) 1317 goto out_put_src; 1318 1319 /* fix up for NFS-specific error code */ 1320 if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) || 1321 !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) { 1322 status = nfserr_wrong_type; 1323 goto out_put_dst; 1324 } 1325 1326 out: 1327 return status; 1328 out_put_dst: 1329 nfsd_file_put(*dst); 1330 *dst = NULL; 1331 out_put_src: 1332 nfsd_file_put(*src); 1333 *src = NULL; 1334 goto out; 1335 } 1336 1337 static __be32 1338 nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1339 union nfsd4_op_u *u) 1340 { 1341 struct nfsd4_clone *clone = &u->clone; 1342 struct nfsd_file *src, *dst; 1343 __be32 status; 1344 1345 status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src, 1346 &clone->cl_dst_stateid, &dst); 1347 if (status) 1348 goto out; 1349 1350 status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos, 1351 dst, clone->cl_dst_pos, clone->cl_count, 1352 EX_ISSYNC(cstate->current_fh.fh_export)); 1353 1354 nfsd_file_put(dst); 1355 nfsd_file_put(src); 1356 out: 1357 return status; 1358 } 1359 1360 /** 1361 * nfsd4_has_active_async_copies - Check for ongoing copy operations 1362 * @clp: Client to be checked 1363 * 1364 * NFSD maintains state for async COPY operations after they complete, 1365 * and this state remains in the nfs4_client's async_copies list. 1366 * Ongoing copies should block the destruction of the nfs4_client, but 1367 * completed copies should not. 1368 * 1369 * Return values: 1370 * %true: At least one active async COPY is ongoing 1371 * %false: No active async COPY operations were found 1372 */ 1373 bool nfsd4_has_active_async_copies(struct nfs4_client *clp) 1374 { 1375 struct nfsd4_copy *copy; 1376 bool result = false; 1377 1378 spin_lock(&clp->async_lock); 1379 list_for_each_entry(copy, &clp->async_copies, copies) { 1380 if (!test_bit(NFSD4_COPY_F_COMPLETED, ©->cp_flags) && 1381 !test_bit(NFSD4_COPY_F_STOPPED, ©->cp_flags)) { 1382 result = true; 1383 break; 1384 } 1385 } 1386 spin_unlock(&clp->async_lock); 1387 return result; 1388 } 1389 1390 /** 1391 * nfsd4_async_copy_reaper - Purge completed copies 1392 * @nn: Network namespace with possible active copy information 1393 */ 1394 void nfsd4_async_copy_reaper(struct nfsd_net *nn) 1395 { 1396 struct nfs4_client *clp; 1397 struct nfsd4_copy *copy; 1398 LIST_HEAD(reaplist); 1399 1400 spin_lock(&nn->client_lock); 1401 list_for_each_entry(clp, &nn->client_lru, cl_lru) { 1402 struct list_head *pos, *next; 1403 1404 spin_lock(&clp->async_lock); 1405 list_for_each_safe(pos, next, &clp->async_copies) { 1406 copy = list_entry(pos, struct nfsd4_copy, copies); 1407 if (test_bit(NFSD4_COPY_F_OFFLOAD_DONE, ©->cp_flags)) { 1408 if (--copy->cp_ttl) { 1409 list_del_init(©->copies); 1410 list_add(©->copies, &reaplist); 1411 } 1412 } 1413 } 1414 spin_unlock(&clp->async_lock); 1415 } 1416 spin_unlock(&nn->client_lock); 1417 1418 while (!list_empty(&reaplist)) { 1419 copy = list_first_entry(&reaplist, struct nfsd4_copy, copies); 1420 list_del_init(©->copies); 1421 cleanup_async_copy(copy); 1422 } 1423 } 1424 1425 static void nfs4_put_copy(struct nfsd4_copy *copy) 1426 { 1427 if (!refcount_dec_and_test(©->refcount)) 1428 return; 1429 kfree(copy->cp_src); 1430 kfree(copy); 1431 } 1432 1433 static void nfsd4_stop_copy(struct nfsd4_copy *copy) 1434 { 1435 trace_nfsd_copy_async_cancel(copy); 1436 if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, ©->cp_flags)) { 1437 kthread_stop(copy->copy_task); 1438 copy->nfserr = nfs_ok; 1439 set_bit(NFSD4_COPY_F_COMPLETED, ©->cp_flags); 1440 } 1441 nfs4_put_copy(copy); 1442 } 1443 1444 static struct nfsd4_copy *nfsd4_unhash_copy(struct nfs4_client *clp) 1445 { 1446 struct nfsd4_copy *copy = NULL; 1447 1448 spin_lock(&clp->async_lock); 1449 if (!list_empty(&clp->async_copies)) { 1450 copy = list_first_entry(&clp->async_copies, struct nfsd4_copy, 1451 copies); 1452 refcount_inc(©->refcount); 1453 copy->cp_clp = NULL; 1454 if (!list_empty(©->copies)) 1455 list_del_init(©->copies); 1456 } 1457 spin_unlock(&clp->async_lock); 1458 return copy; 1459 } 1460 1461 void nfsd4_shutdown_copy(struct nfs4_client *clp) 1462 { 1463 struct nfsd4_copy *copy; 1464 1465 while ((copy = nfsd4_unhash_copy(clp)) != NULL) 1466 nfsd4_stop_copy(copy); 1467 } 1468 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 1469 1470 extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt, 1471 struct nfs_fh *src_fh, 1472 nfs4_stateid *stateid); 1473 extern void nfs42_ssc_close(struct file *filep); 1474 1475 extern void nfs_sb_deactive(struct super_block *sb); 1476 1477 #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys" 1478 1479 /* 1480 * setup a work entry in the ssc delayed unmount list. 1481 */ 1482 static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr, 1483 struct nfsd4_ssc_umount_item **nsui, 1484 struct svc_rqst *rqstp) 1485 { 1486 struct nfsd4_ssc_umount_item *ni = NULL; 1487 struct nfsd4_ssc_umount_item *work = NULL; 1488 struct nfsd4_ssc_umount_item *tmp; 1489 DEFINE_WAIT(wait); 1490 __be32 status = 0; 1491 1492 *nsui = NULL; 1493 work = kzalloc(sizeof(*work), GFP_KERNEL); 1494 try_again: 1495 spin_lock(&nn->nfsd_ssc_lock); 1496 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 1497 if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr))) 1498 continue; 1499 /* found a match */ 1500 if (ni->nsui_busy) { 1501 /* wait - and try again */ 1502 prepare_to_wait(&nn->nfsd_ssc_waitq, &wait, TASK_IDLE); 1503 spin_unlock(&nn->nfsd_ssc_lock); 1504 1505 /* allow 20secs for mount/unmount for now - revisit */ 1506 if (svc_thread_should_stop(rqstp) || 1507 (schedule_timeout(20*HZ) == 0)) { 1508 finish_wait(&nn->nfsd_ssc_waitq, &wait); 1509 kfree(work); 1510 return nfserr_jukebox; 1511 } 1512 finish_wait(&nn->nfsd_ssc_waitq, &wait); 1513 goto try_again; 1514 } 1515 *nsui = ni; 1516 refcount_inc(&ni->nsui_refcnt); 1517 spin_unlock(&nn->nfsd_ssc_lock); 1518 kfree(work); 1519 1520 /* return vfsmount in (*nsui)->nsui_vfsmount */ 1521 return 0; 1522 } 1523 if (work) { 1524 strscpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr)); 1525 refcount_set(&work->nsui_refcnt, 2); 1526 work->nsui_busy = true; 1527 list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list); 1528 *nsui = work; 1529 } else 1530 status = nfserr_resource; 1531 spin_unlock(&nn->nfsd_ssc_lock); 1532 return status; 1533 } 1534 1535 static void nfsd4_ssc_update_dul(struct nfsd_net *nn, 1536 struct nfsd4_ssc_umount_item *nsui, 1537 struct vfsmount *ss_mnt) 1538 { 1539 spin_lock(&nn->nfsd_ssc_lock); 1540 nsui->nsui_vfsmount = ss_mnt; 1541 nsui->nsui_busy = false; 1542 wake_up_all(&nn->nfsd_ssc_waitq); 1543 spin_unlock(&nn->nfsd_ssc_lock); 1544 } 1545 1546 static void nfsd4_ssc_cancel_dul(struct nfsd_net *nn, 1547 struct nfsd4_ssc_umount_item *nsui) 1548 { 1549 spin_lock(&nn->nfsd_ssc_lock); 1550 list_del(&nsui->nsui_list); 1551 wake_up_all(&nn->nfsd_ssc_waitq); 1552 spin_unlock(&nn->nfsd_ssc_lock); 1553 kfree(nsui); 1554 } 1555 1556 /* 1557 * Support one copy source server for now. 1558 */ 1559 static __be32 1560 nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp, 1561 struct nfsd4_ssc_umount_item **nsui) 1562 { 1563 struct file_system_type *type; 1564 struct vfsmount *ss_mnt; 1565 struct nfs42_netaddr *naddr; 1566 struct sockaddr_storage tmp_addr; 1567 size_t tmp_addrlen, match_netid_len = 3; 1568 char *startsep = "", *endsep = "", *match_netid = "tcp"; 1569 char *ipaddr, *dev_name, *raw_data; 1570 int len, raw_len; 1571 __be32 status = nfserr_inval; 1572 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 1573 1574 naddr = &nss->u.nl4_addr; 1575 tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr, 1576 naddr->addr_len, 1577 (struct sockaddr *)&tmp_addr, 1578 sizeof(tmp_addr)); 1579 *nsui = NULL; 1580 if (tmp_addrlen == 0) 1581 goto out_err; 1582 1583 if (tmp_addr.ss_family == AF_INET6) { 1584 startsep = "["; 1585 endsep = "]"; 1586 match_netid = "tcp6"; 1587 match_netid_len = 4; 1588 } 1589 1590 if (naddr->netid_len != match_netid_len || 1591 strncmp(naddr->netid, match_netid, naddr->netid_len)) 1592 goto out_err; 1593 1594 /* Construct the raw data for the vfs_kern_mount call */ 1595 len = RPC_MAX_ADDRBUFLEN + 1; 1596 ipaddr = kzalloc(len, GFP_KERNEL); 1597 if (!ipaddr) 1598 goto out_err; 1599 1600 rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len); 1601 1602 /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/ 1603 1604 raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr); 1605 raw_data = kzalloc(raw_len, GFP_KERNEL); 1606 if (!raw_data) 1607 goto out_free_ipaddr; 1608 1609 snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr); 1610 1611 status = nfserr_nodev; 1612 type = get_fs_type("nfs"); 1613 if (!type) 1614 goto out_free_rawdata; 1615 1616 /* Set the server:<export> for the vfs_kern_mount call */ 1617 dev_name = kzalloc(len + 5, GFP_KERNEL); 1618 if (!dev_name) 1619 goto out_free_rawdata; 1620 snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep); 1621 1622 status = nfsd4_ssc_setup_dul(nn, ipaddr, nsui, rqstp); 1623 if (status) 1624 goto out_free_devname; 1625 if ((*nsui)->nsui_vfsmount) 1626 goto out_done; 1627 1628 /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */ 1629 ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data); 1630 module_put(type->owner); 1631 if (IS_ERR(ss_mnt)) { 1632 status = nfserr_nodev; 1633 nfsd4_ssc_cancel_dul(nn, *nsui); 1634 goto out_free_devname; 1635 } 1636 nfsd4_ssc_update_dul(nn, *nsui, ss_mnt); 1637 out_done: 1638 status = 0; 1639 1640 out_free_devname: 1641 kfree(dev_name); 1642 out_free_rawdata: 1643 kfree(raw_data); 1644 out_free_ipaddr: 1645 kfree(ipaddr); 1646 out_err: 1647 return status; 1648 } 1649 1650 /* 1651 * Verify COPY destination stateid. 1652 * 1653 * Connect to the source server with NFSv4.1. 1654 * Create the source struct file for nfsd_copy_range. 1655 * Called with COPY cstate: 1656 * SAVED_FH: source filehandle 1657 * CURRENT_FH: destination filehandle 1658 */ 1659 static __be32 1660 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp, 1661 struct nfsd4_compound_state *cstate, 1662 struct nfsd4_copy *copy) 1663 { 1664 struct svc_fh *s_fh = NULL; 1665 stateid_t *s_stid = ©->cp_src_stateid; 1666 __be32 status = nfserr_inval; 1667 1668 /* Verify the destination stateid and set dst struct file*/ 1669 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 1670 ©->cp_dst_stateid, 1671 WR_STATE, ©->nf_dst, NULL); 1672 if (status) 1673 goto out; 1674 1675 status = nfsd4_interssc_connect(copy->cp_src, rqstp, ©->ss_nsui); 1676 if (status) 1677 goto out; 1678 1679 s_fh = &cstate->save_fh; 1680 1681 copy->c_fh.size = s_fh->fh_handle.fh_size; 1682 memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size); 1683 copy->stateid.seqid = cpu_to_be32(s_stid->si_generation); 1684 memcpy(copy->stateid.other, (void *)&s_stid->si_opaque, 1685 sizeof(stateid_opaque_t)); 1686 1687 status = 0; 1688 out: 1689 return status; 1690 } 1691 1692 static void 1693 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp, 1694 struct nfsd_file *dst) 1695 { 1696 struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id); 1697 long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout); 1698 1699 nfs42_ssc_close(filp); 1700 fput(filp); 1701 1702 spin_lock(&nn->nfsd_ssc_lock); 1703 list_del(&nsui->nsui_list); 1704 /* 1705 * vfsmount can be shared by multiple exports, 1706 * decrement refcnt. If the count drops to 1 it 1707 * will be unmounted when nsui_expire expires. 1708 */ 1709 refcount_dec(&nsui->nsui_refcnt); 1710 nsui->nsui_expire = jiffies + timeout; 1711 list_add_tail(&nsui->nsui_list, &nn->nfsd_ssc_mount_list); 1712 spin_unlock(&nn->nfsd_ssc_lock); 1713 } 1714 1715 #else /* CONFIG_NFSD_V4_2_INTER_SSC */ 1716 1717 static __be32 1718 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp, 1719 struct nfsd4_compound_state *cstate, 1720 struct nfsd4_copy *copy) 1721 { 1722 return nfserr_inval; 1723 } 1724 1725 static void 1726 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp, 1727 struct nfsd_file *dst) 1728 { 1729 } 1730 1731 static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt, 1732 struct nfs_fh *src_fh, 1733 nfs4_stateid *stateid) 1734 { 1735 return NULL; 1736 } 1737 #endif /* CONFIG_NFSD_V4_2_INTER_SSC */ 1738 1739 static __be32 1740 nfsd4_setup_intra_ssc(struct svc_rqst *rqstp, 1741 struct nfsd4_compound_state *cstate, 1742 struct nfsd4_copy *copy) 1743 { 1744 return nfsd4_verify_copy(rqstp, cstate, ©->cp_src_stateid, 1745 ©->nf_src, ©->cp_dst_stateid, 1746 ©->nf_dst); 1747 } 1748 1749 static void nfsd4_cb_offload_release(struct nfsd4_callback *cb) 1750 { 1751 struct nfsd4_cb_offload *cbo = 1752 container_of(cb, struct nfsd4_cb_offload, co_cb); 1753 struct nfsd4_copy *copy = 1754 container_of(cbo, struct nfsd4_copy, cp_cb_offload); 1755 1756 set_bit(NFSD4_COPY_F_OFFLOAD_DONE, ©->cp_flags); 1757 } 1758 1759 static int nfsd4_cb_offload_done(struct nfsd4_callback *cb, 1760 struct rpc_task *task) 1761 { 1762 struct nfsd4_cb_offload *cbo = 1763 container_of(cb, struct nfsd4_cb_offload, co_cb); 1764 1765 trace_nfsd_cb_offload_done(&cbo->co_res.cb_stateid, task); 1766 switch (task->tk_status) { 1767 case -NFS4ERR_DELAY: 1768 if (cbo->co_retries--) { 1769 rpc_delay(task, HZ / 5); 1770 return 0; 1771 } 1772 } 1773 nfsd41_cb_destroy_referring_call_list(cb); 1774 return 1; 1775 } 1776 1777 static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = { 1778 .release = nfsd4_cb_offload_release, 1779 .done = nfsd4_cb_offload_done, 1780 .opcode = OP_CB_OFFLOAD, 1781 }; 1782 1783 static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync) 1784 { 1785 copy->cp_res.wr_stable_how = 1786 test_bit(NFSD4_COPY_F_COMMITTED, ©->cp_flags) ? 1787 NFS_FILE_SYNC : NFS_UNSTABLE; 1788 nfsd4_copy_set_sync(copy, sync); 1789 } 1790 1791 static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy, 1792 struct file *dst, 1793 struct file *src) 1794 { 1795 errseq_t since; 1796 ssize_t bytes_copied = 0; 1797 u64 bytes_total = copy->cp_count; 1798 u64 src_pos = copy->cp_src_pos; 1799 u64 dst_pos = copy->cp_dst_pos; 1800 int status; 1801 loff_t end; 1802 1803 /* See RFC 7862 p.67: */ 1804 if (bytes_total == 0) 1805 bytes_total = ULLONG_MAX; 1806 do { 1807 /* Only async copies can be stopped here */ 1808 if (kthread_should_stop()) 1809 break; 1810 bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos, 1811 bytes_total); 1812 if (bytes_copied <= 0) 1813 break; 1814 bytes_total -= bytes_copied; 1815 copy->cp_res.wr_bytes_written += bytes_copied; 1816 src_pos += bytes_copied; 1817 dst_pos += bytes_copied; 1818 } while (bytes_total > 0 && nfsd4_copy_is_async(copy)); 1819 /* for a non-zero asynchronous copy do a commit of data */ 1820 if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) { 1821 since = READ_ONCE(dst->f_wb_err); 1822 end = copy->cp_dst_pos + copy->cp_res.wr_bytes_written - 1; 1823 status = vfs_fsync_range(dst, copy->cp_dst_pos, end, 0); 1824 if (!status) 1825 status = filemap_check_wb_err(dst->f_mapping, since); 1826 if (!status) 1827 set_bit(NFSD4_COPY_F_COMMITTED, ©->cp_flags); 1828 } 1829 return bytes_copied; 1830 } 1831 1832 static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, 1833 struct file *src, struct file *dst, 1834 bool sync) 1835 { 1836 __be32 status; 1837 ssize_t bytes; 1838 1839 bytes = _nfsd_copy_file_range(copy, dst, src); 1840 1841 /* for async copy, we ignore the error, client can always retry 1842 * to get the error 1843 */ 1844 if (bytes < 0 && !copy->cp_res.wr_bytes_written) 1845 status = nfserrno(bytes); 1846 else { 1847 nfsd4_init_copy_res(copy, sync); 1848 status = nfs_ok; 1849 } 1850 return status; 1851 } 1852 1853 static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst) 1854 { 1855 dst->cp_src_pos = src->cp_src_pos; 1856 dst->cp_dst_pos = src->cp_dst_pos; 1857 dst->cp_count = src->cp_count; 1858 dst->cp_flags = src->cp_flags; 1859 memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res)); 1860 memcpy(&dst->fh, &src->fh, sizeof(src->fh)); 1861 dst->cp_clp = src->cp_clp; 1862 dst->nf_dst = nfsd_file_get(src->nf_dst); 1863 /* for inter, nf_src doesn't exist yet */ 1864 if (!nfsd4_ssc_is_inter(src)) 1865 dst->nf_src = nfsd_file_get(src->nf_src); 1866 1867 memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid)); 1868 memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server)); 1869 memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid)); 1870 memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh)); 1871 dst->ss_nsui = src->ss_nsui; 1872 } 1873 1874 static void release_copy_files(struct nfsd4_copy *copy) 1875 { 1876 if (copy->nf_src) 1877 nfsd_file_put(copy->nf_src); 1878 if (copy->nf_dst) 1879 nfsd_file_put(copy->nf_dst); 1880 } 1881 1882 static void cleanup_async_copy(struct nfsd4_copy *copy) 1883 { 1884 nfs4_free_copy_state(copy); 1885 release_copy_files(copy); 1886 if (copy->cp_clp) { 1887 spin_lock(©->cp_clp->async_lock); 1888 if (!list_empty(©->copies)) 1889 list_del_init(©->copies); 1890 spin_unlock(©->cp_clp->async_lock); 1891 } 1892 nfs4_put_copy(copy); 1893 } 1894 1895 static void nfsd4_send_cb_offload(struct nfsd4_copy *copy) 1896 { 1897 struct nfsd4_cb_offload *cbo = ©->cp_cb_offload; 1898 1899 memcpy(&cbo->co_res, ©->cp_res, sizeof(copy->cp_res)); 1900 memcpy(&cbo->co_fh, ©->fh, sizeof(copy->fh)); 1901 cbo->co_nfserr = copy->nfserr; 1902 cbo->co_retries = 5; 1903 1904 nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops, 1905 NFSPROC4_CLNT_CB_OFFLOAD); 1906 nfsd41_cb_referring_call(&cbo->co_cb, &cbo->co_referring_sessionid, 1907 cbo->co_referring_slotid, 1908 cbo->co_referring_seqno); 1909 trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid, 1910 &cbo->co_fh, copy->cp_count, copy->nfserr); 1911 nfsd4_try_run_cb(&cbo->co_cb); 1912 } 1913 1914 /** 1915 * nfsd4_do_async_copy - kthread function for background server-side COPY 1916 * @data: arguments for COPY operation 1917 * 1918 * Return values: 1919 * %0: Copy operation is done. 1920 */ 1921 static int nfsd4_do_async_copy(void *data) 1922 { 1923 struct nfsd4_copy *copy = (struct nfsd4_copy *)data; 1924 1925 trace_nfsd_copy_async(copy); 1926 if (nfsd4_ssc_is_inter(copy)) { 1927 struct file *filp; 1928 1929 filp = nfs42_ssc_open(copy->ss_nsui->nsui_vfsmount, 1930 ©->c_fh, ©->stateid); 1931 if (IS_ERR(filp)) { 1932 switch (PTR_ERR(filp)) { 1933 case -EBADF: 1934 copy->nfserr = nfserr_wrong_type; 1935 break; 1936 default: 1937 copy->nfserr = nfserr_offload_denied; 1938 } 1939 /* ss_mnt will be unmounted by the laundromat */ 1940 goto do_callback; 1941 } 1942 copy->nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file, 1943 false); 1944 nfsd4_cleanup_inter_ssc(copy->ss_nsui, filp, copy->nf_dst); 1945 } else { 1946 copy->nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file, 1947 copy->nf_dst->nf_file, false); 1948 } 1949 1950 do_callback: 1951 /* The kthread exits forthwith. Ensure that a subsequent 1952 * OFFLOAD_CANCEL won't try to kill it again. */ 1953 set_bit(NFSD4_COPY_F_STOPPED, ©->cp_flags); 1954 1955 set_bit(NFSD4_COPY_F_COMPLETED, ©->cp_flags); 1956 trace_nfsd_copy_async_done(copy); 1957 nfsd4_send_cb_offload(copy); 1958 atomic_dec(©->cp_nn->pending_async_copies); 1959 return 0; 1960 } 1961 1962 static __be32 1963 nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 1964 union nfsd4_op_u *u) 1965 { 1966 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 1967 struct nfsd4_copy *async_copy = NULL; 1968 struct nfsd4_copy *copy = &u->copy; 1969 struct nfsd42_write_res *result; 1970 __be32 status; 1971 1972 result = ©->cp_res; 1973 nfsd_copy_write_verifier((__be32 *)&result->wr_verifier.data, nn); 1974 1975 copy->cp_clp = cstate->clp; 1976 if (nfsd4_ssc_is_inter(copy)) { 1977 trace_nfsd_copy_inter(copy); 1978 if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) { 1979 status = nfserr_notsupp; 1980 goto out; 1981 } 1982 status = nfsd4_setup_inter_ssc(rqstp, cstate, copy); 1983 if (status) { 1984 trace_nfsd_copy_done(copy, status); 1985 return nfserr_offload_denied; 1986 } 1987 } else { 1988 trace_nfsd_copy_intra(copy); 1989 status = nfsd4_setup_intra_ssc(rqstp, cstate, copy); 1990 if (status) { 1991 trace_nfsd_copy_done(copy, status); 1992 return status; 1993 } 1994 } 1995 1996 memcpy(©->fh, &cstate->current_fh.fh_handle, 1997 sizeof(struct knfsd_fh)); 1998 if (nfsd4_copy_is_async(copy)) { 1999 async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL); 2000 if (!async_copy) 2001 goto out_err; 2002 async_copy->cp_nn = nn; 2003 INIT_LIST_HEAD(&async_copy->copies); 2004 refcount_set(&async_copy->refcount, 1); 2005 async_copy->cp_ttl = NFSD_COPY_INITIAL_TTL; 2006 /* Arbitrary cap on number of pending async copy operations */ 2007 if (atomic_inc_return(&nn->pending_async_copies) > 2008 (int)rqstp->rq_pool->sp_nrthreads) 2009 goto out_dec_async_copy_err; 2010 async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL); 2011 if (!async_copy->cp_src) 2012 goto out_dec_async_copy_err; 2013 if (!nfs4_init_copy_state(nn, copy)) 2014 goto out_dec_async_copy_err; 2015 memcpy(&result->cb_stateid, ©->cp_stateid.cs_stid, 2016 sizeof(result->cb_stateid)); 2017 dup_copy_fields(copy, async_copy); 2018 memcpy(async_copy->cp_cb_offload.co_referring_sessionid.data, 2019 cstate->session->se_sessionid.data, 2020 NFS4_MAX_SESSIONID_LEN); 2021 async_copy->cp_cb_offload.co_referring_slotid = cstate->slot->sl_index; 2022 async_copy->cp_cb_offload.co_referring_seqno = cstate->slot->sl_seqid; 2023 async_copy->copy_task = kthread_create(nfsd4_do_async_copy, 2024 async_copy, "%s", "copy thread"); 2025 if (IS_ERR(async_copy->copy_task)) 2026 goto out_dec_async_copy_err; 2027 spin_lock(&async_copy->cp_clp->async_lock); 2028 list_add(&async_copy->copies, 2029 &async_copy->cp_clp->async_copies); 2030 spin_unlock(&async_copy->cp_clp->async_lock); 2031 wake_up_process(async_copy->copy_task); 2032 status = nfs_ok; 2033 } else { 2034 status = nfsd4_do_copy(copy, copy->nf_src->nf_file, 2035 copy->nf_dst->nf_file, true); 2036 } 2037 out: 2038 trace_nfsd_copy_done(copy, status); 2039 release_copy_files(copy); 2040 return status; 2041 out_dec_async_copy_err: 2042 if (async_copy) 2043 atomic_dec(&nn->pending_async_copies); 2044 out_err: 2045 if (nfsd4_ssc_is_inter(copy)) { 2046 /* 2047 * Source's vfsmount of inter-copy will be unmounted 2048 * by the laundromat. Use copy instead of async_copy 2049 * since async_copy->ss_nsui might not be set yet. 2050 */ 2051 refcount_dec(©->ss_nsui->nsui_refcnt); 2052 } 2053 if (async_copy) 2054 cleanup_async_copy(async_copy); 2055 status = nfserr_jukebox; 2056 goto out; 2057 } 2058 2059 static struct nfsd4_copy * 2060 find_async_copy_locked(struct nfs4_client *clp, stateid_t *stateid) 2061 { 2062 struct nfsd4_copy *copy; 2063 2064 lockdep_assert_held(&clp->async_lock); 2065 2066 list_for_each_entry(copy, &clp->async_copies, copies) { 2067 if (memcmp(©->cp_stateid.cs_stid, stateid, NFS4_STATEID_SIZE)) 2068 continue; 2069 return copy; 2070 } 2071 return NULL; 2072 } 2073 2074 static struct nfsd4_copy * 2075 find_async_copy(struct nfs4_client *clp, stateid_t *stateid) 2076 { 2077 struct nfsd4_copy *copy; 2078 2079 spin_lock(&clp->async_lock); 2080 copy = find_async_copy_locked(clp, stateid); 2081 if (copy) 2082 refcount_inc(©->refcount); 2083 spin_unlock(&clp->async_lock); 2084 return copy; 2085 } 2086 2087 static __be32 2088 nfsd4_offload_cancel(struct svc_rqst *rqstp, 2089 struct nfsd4_compound_state *cstate, 2090 union nfsd4_op_u *u) 2091 { 2092 struct nfsd4_offload_status *os = &u->offload_status; 2093 struct nfsd4_copy *copy; 2094 struct nfs4_client *clp = cstate->clp; 2095 2096 copy = find_async_copy(clp, &os->stateid); 2097 if (!copy) { 2098 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2099 2100 return manage_cpntf_state(nn, &os->stateid, clp, NULL); 2101 } else 2102 nfsd4_stop_copy(copy); 2103 2104 return nfs_ok; 2105 } 2106 2107 static __be32 2108 nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2109 union nfsd4_op_u *u) 2110 { 2111 struct nfsd4_copy_notify *cn = &u->copy_notify; 2112 __be32 status; 2113 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2114 struct nfs4_stid *stid = NULL; 2115 struct nfs4_cpntf_state *cps; 2116 struct nfs4_client *clp = cstate->clp; 2117 2118 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 2119 &cn->cpn_src_stateid, RD_STATE, NULL, 2120 &stid); 2121 if (status) 2122 return status; 2123 if (!stid) 2124 return nfserr_bad_stateid; 2125 2126 cn->cpn_lease_time.tv_sec = nn->nfsd4_lease; 2127 cn->cpn_lease_time.tv_nsec = 0; 2128 2129 status = nfserrno(-ENOMEM); 2130 cps = nfs4_alloc_init_cpntf_state(nn, stid); 2131 if (!cps) 2132 goto out; 2133 memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.cs_stid, sizeof(stateid_t)); 2134 memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t)); 2135 memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t)); 2136 2137 /* For now, only return one server address in cpn_src, the 2138 * address used by the client to connect to this server. 2139 */ 2140 cn->cpn_src->nl4_type = NL4_NETADDR; 2141 status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr, 2142 &cn->cpn_src->u.nl4_addr); 2143 WARN_ON_ONCE(status); 2144 if (status) { 2145 nfs4_put_cpntf_state(nn, cps); 2146 goto out; 2147 } 2148 out: 2149 nfs4_put_stid(stid); 2150 return status; 2151 } 2152 2153 static __be32 2154 nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2155 struct nfsd4_fallocate *fallocate, int flags) 2156 { 2157 __be32 status; 2158 struct nfsd_file *nf; 2159 2160 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 2161 &fallocate->falloc_stateid, 2162 WR_STATE, &nf, NULL); 2163 if (status != nfs_ok) 2164 return status; 2165 2166 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file, 2167 fallocate->falloc_offset, 2168 fallocate->falloc_length, 2169 flags); 2170 nfsd_file_put(nf); 2171 return status; 2172 } 2173 2174 static __be32 2175 nfsd4_offload_status(struct svc_rqst *rqstp, 2176 struct nfsd4_compound_state *cstate, 2177 union nfsd4_op_u *u) 2178 { 2179 struct nfsd4_offload_status *os = &u->offload_status; 2180 __be32 status = nfs_ok; 2181 struct nfsd4_copy *copy; 2182 struct nfs4_client *clp = cstate->clp; 2183 2184 os->completed = false; 2185 spin_lock(&clp->async_lock); 2186 copy = find_async_copy_locked(clp, &os->stateid); 2187 if (copy) { 2188 os->count = copy->cp_res.wr_bytes_written; 2189 if (test_bit(NFSD4_COPY_F_COMPLETED, ©->cp_flags)) { 2190 os->completed = true; 2191 os->status = copy->nfserr; 2192 } 2193 } else 2194 status = nfserr_bad_stateid; 2195 spin_unlock(&clp->async_lock); 2196 2197 return status; 2198 } 2199 2200 static __be32 2201 nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2202 union nfsd4_op_u *u) 2203 { 2204 return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0); 2205 } 2206 2207 static __be32 2208 nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2209 union nfsd4_op_u *u) 2210 { 2211 return nfsd4_fallocate(rqstp, cstate, &u->deallocate, 2212 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE); 2213 } 2214 2215 static __be32 2216 nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2217 union nfsd4_op_u *u) 2218 { 2219 struct nfsd4_seek *seek = &u->seek; 2220 int whence; 2221 __be32 status; 2222 struct nfsd_file *nf; 2223 2224 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh, 2225 &seek->seek_stateid, 2226 RD_STATE, &nf, NULL); 2227 if (status) 2228 return status; 2229 2230 switch (seek->seek_whence) { 2231 case NFS4_CONTENT_DATA: 2232 whence = SEEK_DATA; 2233 break; 2234 case NFS4_CONTENT_HOLE: 2235 whence = SEEK_HOLE; 2236 break; 2237 default: 2238 status = nfserr_union_notsupp; 2239 goto out; 2240 } 2241 2242 /* 2243 * Note: This call does change file->f_pos, but nothing in NFSD 2244 * should ever file->f_pos. 2245 */ 2246 seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence); 2247 if (seek->seek_pos < 0) 2248 status = nfserrno(seek->seek_pos); 2249 else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file))) 2250 seek->seek_eof = true; 2251 2252 out: 2253 nfsd_file_put(nf); 2254 return status; 2255 } 2256 2257 /* This routine never returns NFS_OK! If there are no other errors, it 2258 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the 2259 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME 2260 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK. 2261 */ 2262 static __be32 2263 _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2264 struct nfsd4_verify *verify) 2265 { 2266 __be32 *buf, *p; 2267 int count; 2268 __be32 status; 2269 2270 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP); 2271 if (status) 2272 return status; 2273 2274 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL); 2275 if (status) 2276 return status; 2277 2278 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR) 2279 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)) 2280 return nfserr_inval; 2281 if (verify->ve_attrlen & 3) 2282 return nfserr_inval; 2283 2284 /* count in words: 2285 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4 2286 */ 2287 count = 4 + (verify->ve_attrlen >> 2); 2288 buf = kmalloc(count << 2, GFP_KERNEL); 2289 if (!buf) 2290 return nfserr_jukebox; 2291 2292 p = buf; 2293 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh, 2294 cstate->current_fh.fh_export, 2295 cstate->current_fh.fh_dentry, 2296 verify->ve_bmval, 2297 rqstp, 0); 2298 /* 2299 * If nfsd4_encode_fattr() ran out of space, assume that's because 2300 * the attributes are longer (hence different) than those given: 2301 */ 2302 if (status == nfserr_resource) 2303 status = nfserr_not_same; 2304 if (status) 2305 goto out_kfree; 2306 2307 /* skip bitmap */ 2308 p = buf + 1 + ntohl(buf[0]); 2309 status = nfserr_not_same; 2310 if (ntohl(*p++) != verify->ve_attrlen) 2311 goto out_kfree; 2312 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen)) 2313 status = nfserr_same; 2314 2315 out_kfree: 2316 kfree(buf); 2317 return status; 2318 } 2319 2320 static __be32 2321 nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2322 union nfsd4_op_u *u) 2323 { 2324 __be32 status; 2325 2326 status = _nfsd4_verify(rqstp, cstate, &u->verify); 2327 return status == nfserr_not_same ? nfs_ok : status; 2328 } 2329 2330 static __be32 2331 nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2332 union nfsd4_op_u *u) 2333 { 2334 __be32 status; 2335 2336 status = _nfsd4_verify(rqstp, cstate, &u->nverify); 2337 return status == nfserr_same ? nfs_ok : status; 2338 } 2339 2340 static __be32 2341 nfsd4_get_dir_delegation(struct svc_rqst *rqstp, 2342 struct nfsd4_compound_state *cstate, 2343 union nfsd4_op_u *u) 2344 { 2345 struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation; 2346 struct nfs4_delegation *dd; 2347 struct nfsd_file *nf; 2348 __be32 status; 2349 2350 status = nfsd_file_acquire_dir(rqstp, &cstate->current_fh, &nf); 2351 if (status != nfs_ok) 2352 return status; 2353 2354 /* 2355 * RFC 8881, section 18.39.3 says: 2356 * 2357 * "The server may refuse to grant the delegation. In that case, the 2358 * server will return NFS4ERR_DIRDELEG_UNAVAIL." 2359 * 2360 * This is sub-optimal, since it means that the server would need to 2361 * abort compound processing just because the delegation wasn't 2362 * available. RFC8881bis should change this to allow the server to 2363 * return NFS4_OK with a non-fatal status of GDD4_UNAVAIL in this 2364 * situation. 2365 */ 2366 dd = nfsd_get_dir_deleg(cstate, gdd, nf); 2367 nfsd_file_put(nf); 2368 if (IS_ERR(dd)) { 2369 int err = PTR_ERR(dd); 2370 2371 if (err != -EAGAIN) 2372 return nfserrno(err); 2373 gdd->gddrnf_status = GDD4_UNAVAIL; 2374 return nfs_ok; 2375 } 2376 2377 gdd->gddrnf_status = GDD4_OK; 2378 memcpy(&gdd->gddr_stateid, &dd->dl_stid.sc_stateid, sizeof(gdd->gddr_stateid)); 2379 nfs4_put_stid(&dd->dl_stid); 2380 return nfs_ok; 2381 } 2382 2383 #ifdef CONFIG_NFSD_PNFS 2384 static const struct nfsd4_layout_ops * 2385 nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type) 2386 { 2387 if (!exp->ex_layout_types) { 2388 dprintk("%s: export does not support pNFS\n", __func__); 2389 return NULL; 2390 } 2391 2392 if (layout_type >= LAYOUT_TYPE_MAX || 2393 !(exp->ex_layout_types & (1 << layout_type))) { 2394 dprintk("%s: layout type %d not supported\n", 2395 __func__, layout_type); 2396 return NULL; 2397 } 2398 2399 return nfsd4_layout_ops[layout_type]; 2400 } 2401 2402 static __be32 2403 nfsd4_getdeviceinfo(struct svc_rqst *rqstp, 2404 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 2405 { 2406 struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo; 2407 const struct nfsd4_layout_ops *ops; 2408 struct nfsd4_deviceid_map *map; 2409 struct svc_export *exp; 2410 __be32 nfserr; 2411 2412 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n", 2413 __func__, 2414 gdp->gd_layout_type, 2415 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation, 2416 gdp->gd_maxcount); 2417 2418 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx); 2419 if (!map) { 2420 dprintk("%s: couldn't find device ID to export mapping!\n", 2421 __func__); 2422 return nfserr_noent; 2423 } 2424 2425 exp = rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp), 2426 rqstp->rq_client, rqstp->rq_gssclient, 2427 map->fsid_type, map->fsid); 2428 if (IS_ERR(exp)) { 2429 dprintk("%s: could not find device id\n", __func__); 2430 return nfserr_noent; 2431 } 2432 2433 nfserr = nfserr_layoutunavailable; 2434 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type); 2435 if (!ops) 2436 goto out; 2437 2438 nfserr = nfs_ok; 2439 if (gdp->gd_maxcount != 0) { 2440 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb, 2441 rqstp, cstate->clp, gdp); 2442 } 2443 2444 gdp->gd_notify_types &= ops->notify_types; 2445 out: 2446 exp_put(exp); 2447 return nfserr; 2448 } 2449 2450 static void 2451 nfsd4_getdeviceinfo_release(union nfsd4_op_u *u) 2452 { 2453 kfree(u->getdeviceinfo.gd_device); 2454 } 2455 2456 static __be32 2457 nfsd4_layoutget(struct svc_rqst *rqstp, 2458 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 2459 { 2460 struct nfsd4_layoutget *lgp = &u->layoutget; 2461 struct svc_fh *current_fh = &cstate->current_fh; 2462 const struct nfsd4_layout_ops *ops; 2463 struct nfs4_layout_stateid *ls; 2464 __be32 nfserr; 2465 int accmode = NFSD_MAY_READ_IF_EXEC | NFSD_MAY_OWNER_OVERRIDE; 2466 2467 switch (lgp->lg_seg.iomode) { 2468 case IOMODE_READ: 2469 accmode |= NFSD_MAY_READ; 2470 break; 2471 case IOMODE_RW: 2472 accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE; 2473 break; 2474 default: 2475 dprintk("%s: invalid iomode %d\n", 2476 __func__, lgp->lg_seg.iomode); 2477 nfserr = nfserr_badiomode; 2478 goto out; 2479 } 2480 2481 nfserr = fh_verify(rqstp, current_fh, 0, accmode); 2482 if (nfserr) 2483 goto out; 2484 2485 nfserr = nfserr_layoutunavailable; 2486 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type); 2487 if (!ops) 2488 goto out; 2489 2490 /* 2491 * Verify minlength and range as per RFC5661: 2492 * o If loga_length is less than loga_minlength, 2493 * the metadata server MUST return NFS4ERR_INVAL. 2494 * o If the sum of loga_offset and loga_minlength exceeds 2495 * NFS4_UINT64_MAX, and loga_minlength is not 2496 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result. 2497 * o If the sum of loga_offset and loga_length exceeds 2498 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX, 2499 * the error NFS4ERR_INVAL MUST result. 2500 */ 2501 nfserr = nfserr_inval; 2502 if (lgp->lg_seg.length < lgp->lg_minlength || 2503 (lgp->lg_minlength != NFS4_MAX_UINT64 && 2504 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) || 2505 (lgp->lg_seg.length != NFS4_MAX_UINT64 && 2506 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset)) 2507 goto out; 2508 if (lgp->lg_seg.length == 0) 2509 goto out; 2510 2511 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid, 2512 true, lgp->lg_layout_type, &ls); 2513 if (nfserr) { 2514 trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid); 2515 goto out; 2516 } 2517 2518 nfserr = nfserr_recallconflict; 2519 if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls)) 2520 goto out_put_stid; 2521 2522 nfserr = ops->proc_layoutget(rqstp, d_inode(current_fh->fh_dentry), 2523 current_fh, lgp); 2524 if (nfserr) 2525 goto out_put_stid; 2526 2527 nfserr = nfsd4_insert_layout(lgp, ls); 2528 2529 out_put_stid: 2530 mutex_unlock(&ls->ls_mutex); 2531 nfs4_put_stid(&ls->ls_stid); 2532 out: 2533 return nfserr; 2534 } 2535 2536 static void 2537 nfsd4_layoutget_release(union nfsd4_op_u *u) 2538 { 2539 kfree(u->layoutget.lg_content); 2540 } 2541 2542 static __be32 2543 nfsd4_layoutcommit(struct svc_rqst *rqstp, 2544 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 2545 { 2546 struct net *net = SVC_NET(rqstp); 2547 struct nfsd4_layoutcommit *lcp = &u->layoutcommit; 2548 const struct nfsd4_layout_seg *seg = &lcp->lc_seg; 2549 struct svc_fh *current_fh = &cstate->current_fh; 2550 const struct nfsd4_layout_ops *ops; 2551 struct inode *inode; 2552 struct nfs4_layout_stateid *ls; 2553 __be32 nfserr; 2554 2555 nfserr = fh_verify(rqstp, current_fh, 0, 2556 NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE); 2557 if (nfserr) 2558 goto out; 2559 2560 nfserr = nfserr_layoutunavailable; 2561 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type); 2562 if (!ops) 2563 goto out; 2564 inode = d_inode(current_fh->fh_dentry); 2565 2566 lcp->lc_size_chg = false; 2567 if (lcp->lc_newoffset) { 2568 loff_t new_size = lcp->lc_last_wr + 1; 2569 2570 nfserr = nfserr_inval; 2571 if (new_size <= seg->offset) 2572 goto out; 2573 if (new_size > seg->offset + seg->length) 2574 goto out; 2575 2576 if (new_size > i_size_read(inode)) { 2577 lcp->lc_size_chg = true; 2578 lcp->lc_newsize = new_size; 2579 } 2580 } 2581 2582 nfserr = nfserr_grace; 2583 if (locks_in_grace(net) && !lcp->lc_reclaim) 2584 goto out; 2585 nfserr = nfserr_no_grace; 2586 if (!locks_in_grace(net) && lcp->lc_reclaim) 2587 goto out; 2588 2589 if (!lcp->lc_reclaim) { 2590 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, 2591 &lcp->lc_sid, false, lcp->lc_layout_type, &ls); 2592 if (nfserr) { 2593 trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid); 2594 /* fixup error code as per RFC5661 */ 2595 if (nfserr == nfserr_bad_stateid) 2596 nfserr = nfserr_badlayout; 2597 goto out; 2598 } 2599 2600 /* LAYOUTCOMMIT does not require any serialization */ 2601 mutex_unlock(&ls->ls_mutex); 2602 } 2603 2604 nfserr = ops->proc_layoutcommit(inode, rqstp, lcp); 2605 2606 if (!lcp->lc_reclaim) { 2607 nfsd4_file_mark_deleg_written(ls->ls_stid.sc_file); 2608 nfs4_put_stid(&ls->ls_stid); 2609 } 2610 out: 2611 return nfserr; 2612 } 2613 2614 static __be32 2615 nfsd4_layoutreturn(struct svc_rqst *rqstp, 2616 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 2617 { 2618 struct nfsd4_layoutreturn *lrp = &u->layoutreturn; 2619 struct svc_fh *current_fh = &cstate->current_fh; 2620 __be32 nfserr; 2621 2622 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP); 2623 if (nfserr) 2624 goto out; 2625 2626 nfserr = nfserr_layoutunavailable; 2627 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type)) 2628 goto out; 2629 2630 switch (lrp->lr_seg.iomode) { 2631 case IOMODE_READ: 2632 case IOMODE_RW: 2633 case IOMODE_ANY: 2634 break; 2635 default: 2636 dprintk("%s: invalid iomode %d\n", __func__, 2637 lrp->lr_seg.iomode); 2638 nfserr = nfserr_inval; 2639 goto out; 2640 } 2641 2642 switch (lrp->lr_return_type) { 2643 case RETURN_FILE: 2644 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp); 2645 break; 2646 case RETURN_FSID: 2647 case RETURN_ALL: 2648 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp); 2649 break; 2650 default: 2651 dprintk("%s: invalid return_type %d\n", __func__, 2652 lrp->lr_return_type); 2653 nfserr = nfserr_inval; 2654 break; 2655 } 2656 out: 2657 return nfserr; 2658 } 2659 #endif /* CONFIG_NFSD_PNFS */ 2660 2661 static __be32 2662 nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2663 union nfsd4_op_u *u) 2664 { 2665 struct nfsd4_getxattr *getxattr = &u->getxattr; 2666 2667 return nfsd_getxattr(rqstp, &cstate->current_fh, 2668 getxattr->getxa_name, &getxattr->getxa_buf, 2669 &getxattr->getxa_len); 2670 } 2671 2672 static __be32 2673 nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2674 union nfsd4_op_u *u) 2675 { 2676 struct nfsd4_setxattr *setxattr = &u->setxattr; 2677 __be32 ret; 2678 2679 if (opens_in_grace(SVC_NET(rqstp))) 2680 return nfserr_grace; 2681 2682 ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name, 2683 setxattr->setxa_buf, setxattr->setxa_len, 2684 setxattr->setxa_flags); 2685 2686 if (!ret) 2687 set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh); 2688 2689 return ret; 2690 } 2691 2692 static __be32 2693 nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2694 union nfsd4_op_u *u) 2695 { 2696 /* 2697 * Get the entire list, then copy out only the user attributes 2698 * in the encode function. 2699 */ 2700 return nfsd_listxattr(rqstp, &cstate->current_fh, 2701 &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len); 2702 } 2703 2704 static __be32 2705 nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2706 union nfsd4_op_u *u) 2707 { 2708 struct nfsd4_removexattr *removexattr = &u->removexattr; 2709 __be32 ret; 2710 2711 if (opens_in_grace(SVC_NET(rqstp))) 2712 return nfserr_grace; 2713 2714 ret = nfsd_removexattr(rqstp, &cstate->current_fh, 2715 removexattr->rmxa_name); 2716 2717 if (!ret) 2718 set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh); 2719 2720 return ret; 2721 } 2722 2723 /* 2724 * NULL call. 2725 */ 2726 static __be32 2727 nfsd4_proc_null(struct svc_rqst *rqstp) 2728 { 2729 return rpc_success; 2730 } 2731 2732 static inline void nfsd4_increment_op_stats(struct nfsd_net *nn, u32 opnum) 2733 { 2734 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP) 2735 percpu_counter_inc(&nn->counter[NFSD_STATS_NFS4_OP(opnum)]); 2736 } 2737 2738 static const struct nfsd4_operation nfsd4_ops[]; 2739 2740 static const char *nfsd4_op_name(unsigned opnum); 2741 2742 /* 2743 * Enforce NFSv4.1 COMPOUND ordering rules: 2744 * 2745 * Also note, enforced elsewhere: 2746 * - SEQUENCE other than as first op results in 2747 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().) 2748 * - BIND_CONN_TO_SESSION must be the only op in its compound. 2749 * (Enforced in nfsd4_bind_conn_to_session().) 2750 * - DESTROY_SESSION must be the final operation in a compound, if 2751 * sessionid's in SEQUENCE and DESTROY_SESSION are the same. 2752 * (Enforced in nfsd4_destroy_session().) 2753 */ 2754 static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args) 2755 { 2756 struct nfsd4_op *first_op = &args->ops[0]; 2757 2758 /* These ordering requirements don't apply to NFSv4.0: */ 2759 if (args->minorversion == 0) 2760 return nfs_ok; 2761 /* This is weird, but OK, not our problem: */ 2762 if (args->opcnt == 0) 2763 return nfs_ok; 2764 if (first_op->status == nfserr_op_illegal) 2765 return nfs_ok; 2766 if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP)) 2767 return nfserr_op_not_in_session; 2768 if (first_op->opnum == OP_SEQUENCE) 2769 return nfs_ok; 2770 /* 2771 * So first_op is something allowed outside a session, like 2772 * EXCHANGE_ID; but then it has to be the only op in the 2773 * compound: 2774 */ 2775 if (args->opcnt != 1) 2776 return nfserr_not_only_op; 2777 return nfs_ok; 2778 } 2779 2780 const struct nfsd4_operation *OPDESC(struct nfsd4_op *op) 2781 { 2782 return &nfsd4_ops[op->opnum]; 2783 } 2784 2785 bool nfsd4_cache_this_op(struct nfsd4_op *op) 2786 { 2787 if (op->opnum == OP_ILLEGAL) 2788 return false; 2789 return OPDESC(op)->op_flags & OP_CACHEME; 2790 } 2791 2792 static bool need_wrongsec_check(struct svc_rqst *rqstp) 2793 { 2794 struct nfsd4_compoundres *resp = rqstp->rq_resp; 2795 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 2796 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1]; 2797 struct nfsd4_op *next = &argp->ops[resp->opcnt]; 2798 const struct nfsd4_operation *thisd = OPDESC(this); 2799 const struct nfsd4_operation *nextd; 2800 2801 /* 2802 * Most ops check wronsec on our own; only the putfh-like ops 2803 * have special rules. 2804 */ 2805 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE)) 2806 return false; 2807 /* 2808 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a 2809 * put-filehandle operation if we're not going to use the 2810 * result: 2811 */ 2812 if (argp->opcnt == resp->opcnt) 2813 return false; 2814 if (next->opnum == OP_ILLEGAL) 2815 return false; 2816 nextd = OPDESC(next); 2817 /* 2818 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC 2819 * errors themselves as necessary; others should check for them 2820 * now: 2821 */ 2822 return !(nextd->op_flags & OP_HANDLES_WRONGSEC); 2823 } 2824 2825 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 2826 static void 2827 check_if_stalefh_allowed(struct nfsd4_compoundargs *args) 2828 { 2829 struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL; 2830 struct nfsd4_copy *copy; 2831 struct nfsd4_putfh *putfh; 2832 int i; 2833 2834 /* traverse all operation and if it's a COPY compound, mark the 2835 * source filehandle to skip verification 2836 */ 2837 for (i = 0; i < args->opcnt; i++) { 2838 op = &args->ops[i]; 2839 if (op->opnum == OP_PUTFH) 2840 current_op = op; 2841 else if (op->opnum == OP_SAVEFH) 2842 saved_op = current_op; 2843 else if (op->opnum == OP_RESTOREFH) 2844 current_op = saved_op; 2845 else if (op->opnum == OP_COPY) { 2846 copy = (struct nfsd4_copy *)&op->u; 2847 if (!saved_op) { 2848 op->status = nfserr_nofilehandle; 2849 return; 2850 } 2851 putfh = (struct nfsd4_putfh *)&saved_op->u; 2852 if (nfsd4_ssc_is_inter(copy)) 2853 putfh->no_verify = true; 2854 } 2855 } 2856 } 2857 #else 2858 static void 2859 check_if_stalefh_allowed(struct nfsd4_compoundargs *args) 2860 { 2861 } 2862 #endif 2863 2864 /* 2865 * COMPOUND call. 2866 */ 2867 static __be32 2868 nfsd4_proc_compound(struct svc_rqst *rqstp) 2869 { 2870 struct nfsd4_compoundargs *args = rqstp->rq_argp; 2871 struct nfsd4_compoundres *resp = rqstp->rq_resp; 2872 struct nfsd4_op *op; 2873 struct nfsd4_compound_state *cstate = &resp->cstate; 2874 struct svc_fh *current_fh = &cstate->current_fh; 2875 struct svc_fh *save_fh = &cstate->save_fh; 2876 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2877 __be32 status; 2878 2879 resp->xdr = &rqstp->rq_res_stream; 2880 resp->statusp = resp->xdr->p; 2881 2882 /* reserve space for: NFS status code */ 2883 xdr_reserve_space(resp->xdr, XDR_UNIT); 2884 2885 /* reserve space for: taglen, tag, and opcnt */ 2886 xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen); 2887 resp->taglen = args->taglen; 2888 resp->tag = args->tag; 2889 resp->rqstp = rqstp; 2890 cstate->minorversion = args->minorversion; 2891 fh_init(current_fh, NFS4_FHSIZE); 2892 fh_init(save_fh, NFS4_FHSIZE); 2893 /* 2894 * Don't use the deferral mechanism for NFSv4; compounds make it 2895 * too hard to avoid non-idempotency problems. 2896 */ 2897 clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags); 2898 2899 /* 2900 * According to RFC3010, this takes precedence over all other errors. 2901 */ 2902 status = nfserr_minor_vers_mismatch; 2903 if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0) 2904 goto out; 2905 2906 status = nfs41_check_op_ordering(args); 2907 if (status) { 2908 op = &args->ops[0]; 2909 op->status = status; 2910 resp->opcnt = 1; 2911 goto encode_op; 2912 } 2913 check_if_stalefh_allowed(args); 2914 2915 rqstp->rq_lease_breaker = (void **)&cstate->clp; 2916 2917 trace_nfsd_compound(rqstp, args->tag, args->taglen, args->client_opcnt); 2918 while (!status && resp->opcnt < args->opcnt) { 2919 op = &args->ops[resp->opcnt++]; 2920 2921 if (unlikely(resp->opcnt == NFSD_MAX_OPS_PER_COMPOUND)) { 2922 /* If there are still more operations to process, 2923 * stop here and report NFS4ERR_RESOURCE. */ 2924 if (cstate->minorversion == 0 && 2925 args->client_opcnt > resp->opcnt) { 2926 op->status = nfserr_resource; 2927 goto encode_op; 2928 } 2929 } 2930 2931 /* 2932 * The XDR decode routines may have pre-set op->status; 2933 * for example, if there is a miscellaneous XDR error 2934 * it will be set to nfserr_bad_xdr. 2935 */ 2936 if (op->status) { 2937 if (op->opnum == OP_OPEN) 2938 op->status = nfsd4_open_omfg(rqstp, cstate, op); 2939 goto encode_op; 2940 } 2941 if (!current_fh->fh_dentry && 2942 !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) { 2943 if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) { 2944 op->status = nfserr_nofilehandle; 2945 goto encode_op; 2946 } 2947 } else if (current_fh->fh_export && 2948 current_fh->fh_export->ex_fslocs.migrated && 2949 !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) { 2950 op->status = nfserr_moved; 2951 goto encode_op; 2952 } 2953 2954 fh_clear_pre_post_attrs(current_fh); 2955 2956 /* If op is non-idempotent */ 2957 if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) { 2958 /* 2959 * Don't execute this op if we couldn't encode a 2960 * successful reply: 2961 */ 2962 u32 plen = op->opdesc->op_rsize_bop(rqstp, op); 2963 /* 2964 * Plus if there's another operation, make sure 2965 * we'll have space to at least encode an error: 2966 */ 2967 if (resp->opcnt < args->opcnt) 2968 plen += COMPOUND_ERR_SLACK_SPACE; 2969 op->status = nfsd4_check_resp_size(resp, plen); 2970 } 2971 2972 if (op->status) 2973 goto encode_op; 2974 2975 if (op->opdesc->op_get_currentstateid) 2976 op->opdesc->op_get_currentstateid(cstate, &op->u); 2977 op->status = op->opdesc->op_func(rqstp, cstate, &op->u); 2978 trace_nfsd_compound_op_err(rqstp, op->opnum, op->status); 2979 2980 /* Only from SEQUENCE */ 2981 if (cstate->status == nfserr_replay_cache) { 2982 dprintk("%s NFS4.1 replay from cache\n", __func__); 2983 status = op->status; 2984 goto out; 2985 } 2986 if (!op->status) { 2987 if (op->opdesc->op_set_currentstateid) 2988 op->opdesc->op_set_currentstateid(cstate, &op->u); 2989 2990 if (op->opdesc->op_flags & OP_CLEAR_STATEID) 2991 clear_current_stateid(cstate); 2992 2993 if (current_fh->fh_export && 2994 need_wrongsec_check(rqstp)) 2995 op->status = check_nfsd_access(current_fh->fh_export, rqstp, false); 2996 } 2997 encode_op: 2998 if (op->status == nfserr_replay_me) { 2999 op->replay = &cstate->replay_owner->so_replay; 3000 nfsd4_encode_replay(resp->xdr, op); 3001 status = op->status = op->replay->rp_status; 3002 } else { 3003 nfsd4_encode_operation(resp, op); 3004 status = op->status; 3005 } 3006 3007 trace_nfsd_compound_status(args->client_opcnt, resp->opcnt, 3008 status, nfsd4_op_name(op->opnum)); 3009 3010 nfsd4_cstate_clear_replay(cstate); 3011 nfsd4_increment_op_stats(nn, op->opnum); 3012 } 3013 3014 fh_put(current_fh); 3015 fh_put(save_fh); 3016 BUG_ON(cstate->replay_owner); 3017 out: 3018 cstate->status = status; 3019 /* Reset deferral mechanism for RPC deferrals */ 3020 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags); 3021 return rpc_success; 3022 } 3023 3024 #define op_encode_hdr_size (2) 3025 #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE)) 3026 #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE)) 3027 #define op_encode_change_info_maxsz (5) 3028 #define nfs4_fattr_bitmap_maxsz (4) 3029 3030 /* We'll fall back on returning no lockowner if run out of space: */ 3031 #define op_encode_lockowner_maxsz (0) 3032 #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz) 3033 3034 #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ)) 3035 3036 #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz) 3037 #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \ 3038 op_encode_ace_maxsz) 3039 3040 #define op_encode_channel_attrs_maxsz (6 + 1 + 1) 3041 3042 /* 3043 * The _rsize() helpers are invoked by the NFSv4 COMPOUND decoder, which 3044 * is called before sunrpc sets rq_res.buflen. Thus we have to compute 3045 * the maximum payload size here, based on transport limits and the size 3046 * of the remaining space in the rq_pages array. 3047 */ 3048 static u32 nfsd4_max_payload(const struct svc_rqst *rqstp) 3049 { 3050 u32 buflen; 3051 3052 buflen = (rqstp->rq_page_end - rqstp->rq_next_page) * PAGE_SIZE; 3053 buflen -= rqstp->rq_auth_slack; 3054 buflen -= rqstp->rq_res.head[0].iov_len; 3055 return min_t(u32, buflen, svc_max_payload(rqstp)); 3056 } 3057 3058 static u32 nfsd4_only_status_rsize(const struct svc_rqst *rqstp, 3059 const struct nfsd4_op *op) 3060 { 3061 return (op_encode_hdr_size) * sizeof(__be32); 3062 } 3063 3064 static u32 nfsd4_status_stateid_rsize(const struct svc_rqst *rqstp, 3065 const struct nfsd4_op *op) 3066 { 3067 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32); 3068 } 3069 3070 static u32 nfsd4_access_rsize(const struct svc_rqst *rqstp, 3071 const struct nfsd4_op *op) 3072 { 3073 /* ac_supported, ac_resp_access */ 3074 return (op_encode_hdr_size + 2)* sizeof(__be32); 3075 } 3076 3077 static u32 nfsd4_commit_rsize(const struct svc_rqst *rqstp, 3078 const struct nfsd4_op *op) 3079 { 3080 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32); 3081 } 3082 3083 static u32 nfsd4_create_rsize(const struct svc_rqst *rqstp, 3084 const struct nfsd4_op *op) 3085 { 3086 return (op_encode_hdr_size + op_encode_change_info_maxsz 3087 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32); 3088 } 3089 3090 /* 3091 * Note since this is an idempotent operation we won't insist on failing 3092 * the op prematurely if the estimate is too large. We may turn off splice 3093 * reads unnecessarily. 3094 */ 3095 static u32 nfsd4_getattr_rsize(const struct svc_rqst *rqstp, 3096 const struct nfsd4_op *op) 3097 { 3098 const u32 *bmap = op->u.getattr.ga_bmval; 3099 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2]; 3100 u32 ret = 0; 3101 3102 if (bmap0 & FATTR4_WORD0_ACL) 3103 return nfsd4_max_payload(rqstp); 3104 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS) 3105 return nfsd4_max_payload(rqstp); 3106 3107 if (bmap1 & FATTR4_WORD1_OWNER) { 3108 ret += IDMAP_NAMESZ + 4; 3109 bmap1 &= ~FATTR4_WORD1_OWNER; 3110 } 3111 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) { 3112 ret += IDMAP_NAMESZ + 4; 3113 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP; 3114 } 3115 if (bmap0 & FATTR4_WORD0_FILEHANDLE) { 3116 ret += NFS4_FHSIZE + 4; 3117 bmap0 &= ~FATTR4_WORD0_FILEHANDLE; 3118 } 3119 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) { 3120 ret += NFS4_MAXLABELLEN + 12; 3121 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL; 3122 } 3123 /* 3124 * Largest of remaining attributes are 16 bytes (e.g., 3125 * supported_attributes) 3126 */ 3127 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2)); 3128 /* bitmask, length */ 3129 ret += 20; 3130 return ret; 3131 } 3132 3133 static u32 nfsd4_getfh_rsize(const struct svc_rqst *rqstp, 3134 const struct nfsd4_op *op) 3135 { 3136 return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE; 3137 } 3138 3139 static u32 nfsd4_link_rsize(const struct svc_rqst *rqstp, 3140 const struct nfsd4_op *op) 3141 { 3142 return (op_encode_hdr_size + op_encode_change_info_maxsz) 3143 * sizeof(__be32); 3144 } 3145 3146 static u32 nfsd4_lock_rsize(const struct svc_rqst *rqstp, 3147 const struct nfsd4_op *op) 3148 { 3149 return (op_encode_hdr_size + op_encode_lock_denied_maxsz) 3150 * sizeof(__be32); 3151 } 3152 3153 static u32 nfsd4_open_rsize(const struct svc_rqst *rqstp, 3154 const struct nfsd4_op *op) 3155 { 3156 return (op_encode_hdr_size + op_encode_stateid_maxsz 3157 + op_encode_change_info_maxsz + 1 3158 + nfs4_fattr_bitmap_maxsz 3159 + op_encode_delegation_maxsz) * sizeof(__be32); 3160 } 3161 3162 static u32 nfsd4_read_rsize(const struct svc_rqst *rqstp, 3163 const struct nfsd4_op *op) 3164 { 3165 u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp)); 3166 3167 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32); 3168 } 3169 3170 static u32 nfsd4_read_plus_rsize(const struct svc_rqst *rqstp, 3171 const struct nfsd4_op *op) 3172 { 3173 u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp)); 3174 /* 3175 * If we detect that the file changed during hole encoding, then we 3176 * recover by encoding the remaining reply as data. This means we need 3177 * to set aside enough room to encode two data segments. 3178 */ 3179 u32 seg_len = 2 * (1 + 2 + 1); 3180 3181 return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32); 3182 } 3183 3184 static u32 nfsd4_readdir_rsize(const struct svc_rqst *rqstp, 3185 const struct nfsd4_op *op) 3186 { 3187 u32 rlen = min(op->u.readdir.rd_maxcount, nfsd4_max_payload(rqstp)); 3188 3189 return (op_encode_hdr_size + op_encode_verifier_maxsz + 3190 XDR_QUADLEN(rlen)) * sizeof(__be32); 3191 } 3192 3193 static u32 nfsd4_readlink_rsize(const struct svc_rqst *rqstp, 3194 const struct nfsd4_op *op) 3195 { 3196 return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE; 3197 } 3198 3199 static u32 nfsd4_remove_rsize(const struct svc_rqst *rqstp, 3200 const struct nfsd4_op *op) 3201 { 3202 return (op_encode_hdr_size + op_encode_change_info_maxsz) 3203 * sizeof(__be32); 3204 } 3205 3206 static u32 nfsd4_rename_rsize(const struct svc_rqst *rqstp, 3207 const struct nfsd4_op *op) 3208 { 3209 return (op_encode_hdr_size + op_encode_change_info_maxsz 3210 + op_encode_change_info_maxsz) * sizeof(__be32); 3211 } 3212 3213 static u32 nfsd4_sequence_rsize(const struct svc_rqst *rqstp, 3214 const struct nfsd4_op *op) 3215 { 3216 return (op_encode_hdr_size 3217 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32); 3218 } 3219 3220 static u32 nfsd4_test_stateid_rsize(const struct svc_rqst *rqstp, 3221 const struct nfsd4_op *op) 3222 { 3223 return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids) 3224 * sizeof(__be32); 3225 } 3226 3227 static u32 nfsd4_setattr_rsize(const struct svc_rqst *rqstp, 3228 const struct nfsd4_op *op) 3229 { 3230 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32); 3231 } 3232 3233 static u32 nfsd4_secinfo_rsize(const struct svc_rqst *rqstp, 3234 const struct nfsd4_op *op) 3235 { 3236 return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR * 3237 (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32); 3238 } 3239 3240 static u32 nfsd4_setclientid_rsize(const struct svc_rqst *rqstp, 3241 const struct nfsd4_op *op) 3242 { 3243 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) * 3244 sizeof(__be32); 3245 } 3246 3247 static u32 nfsd4_write_rsize(const struct svc_rqst *rqstp, 3248 const struct nfsd4_op *op) 3249 { 3250 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32); 3251 } 3252 3253 static u32 nfsd4_exchange_id_rsize(const struct svc_rqst *rqstp, 3254 const struct nfsd4_op *op) 3255 { 3256 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\ 3257 1 + 1 + /* eir_flags, spr_how */\ 3258 4 + /* spo_must_enforce & _allow with bitmap */\ 3259 2 + /*eir_server_owner.so_minor_id */\ 3260 /* eir_server_owner.so_major_id<> */\ 3261 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\ 3262 /* eir_server_scope<> */\ 3263 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\ 3264 1 + /* eir_server_impl_id array length */\ 3265 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32); 3266 } 3267 3268 static u32 nfsd4_bind_conn_to_session_rsize(const struct svc_rqst *rqstp, 3269 const struct nfsd4_op *op) 3270 { 3271 return (op_encode_hdr_size + \ 3272 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\ 3273 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32); 3274 } 3275 3276 static u32 nfsd4_create_session_rsize(const struct svc_rqst *rqstp, 3277 const struct nfsd4_op *op) 3278 { 3279 return (op_encode_hdr_size + \ 3280 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\ 3281 2 + /* csr_sequence, csr_flags */\ 3282 op_encode_channel_attrs_maxsz + \ 3283 op_encode_channel_attrs_maxsz) * sizeof(__be32); 3284 } 3285 3286 static u32 nfsd4_copy_rsize(const struct svc_rqst *rqstp, 3287 const struct nfsd4_op *op) 3288 { 3289 return (op_encode_hdr_size + 3290 1 /* wr_callback */ + 3291 op_encode_stateid_maxsz /* wr_callback */ + 3292 2 /* wr_count */ + 3293 1 /* wr_committed */ + 3294 op_encode_verifier_maxsz + 3295 1 /* cr_consecutive */ + 3296 1 /* cr_synchronous */) * sizeof(__be32); 3297 } 3298 3299 static u32 nfsd4_offload_status_rsize(const struct svc_rqst *rqstp, 3300 const struct nfsd4_op *op) 3301 { 3302 return (op_encode_hdr_size + 3303 2 /* osr_count */ + 3304 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32); 3305 } 3306 3307 static u32 nfsd4_copy_notify_rsize(const struct svc_rqst *rqstp, 3308 const struct nfsd4_op *op) 3309 { 3310 return (op_encode_hdr_size + 3311 3 /* cnr_lease_time */ + 3312 1 /* We support one cnr_source_server */ + 3313 1 /* cnr_stateid seq */ + 3314 op_encode_stateid_maxsz /* cnr_stateid */ + 3315 1 /* num cnr_source_server*/ + 3316 1 /* nl4_type */ + 3317 1 /* nl4 size */ + 3318 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */) 3319 * sizeof(__be32); 3320 } 3321 3322 static u32 nfsd4_get_dir_delegation_rsize(const struct svc_rqst *rqstp, 3323 const struct nfsd4_op *op) 3324 { 3325 return (op_encode_hdr_size + 3326 1 /* gddr_status */ + 3327 op_encode_verifier_maxsz + 3328 op_encode_stateid_maxsz + 3329 2 /* gddr_notification */ + 3330 2 /* gddr_child_attributes */ + 3331 2 /* gddr_dir_attributes */); 3332 } 3333 3334 #ifdef CONFIG_NFSD_PNFS 3335 static u32 nfsd4_getdeviceinfo_rsize(const struct svc_rqst *rqstp, 3336 const struct nfsd4_op *op) 3337 { 3338 u32 rlen = min(op->u.getdeviceinfo.gd_maxcount, nfsd4_max_payload(rqstp)); 3339 3340 return (op_encode_hdr_size + 3341 1 /* gd_layout_type*/ + 3342 XDR_QUADLEN(rlen) + 3343 2 /* gd_notify_types */) * sizeof(__be32); 3344 } 3345 3346 /* 3347 * At this stage we don't really know what layout driver will handle the request, 3348 * so we need to define an arbitrary upper bound here. 3349 */ 3350 #define MAX_LAYOUT_SIZE 128 3351 static u32 nfsd4_layoutget_rsize(const struct svc_rqst *rqstp, 3352 const struct nfsd4_op *op) 3353 { 3354 return (op_encode_hdr_size + 3355 1 /* logr_return_on_close */ + 3356 op_encode_stateid_maxsz + 3357 1 /* nr of layouts */ + 3358 MAX_LAYOUT_SIZE) * sizeof(__be32); 3359 } 3360 3361 static u32 nfsd4_layoutcommit_rsize(const struct svc_rqst *rqstp, 3362 const struct nfsd4_op *op) 3363 { 3364 return (op_encode_hdr_size + 3365 1 /* locr_newsize */ + 3366 2 /* ns_size */) * sizeof(__be32); 3367 } 3368 3369 static u32 nfsd4_layoutreturn_rsize(const struct svc_rqst *rqstp, 3370 const struct nfsd4_op *op) 3371 { 3372 return (op_encode_hdr_size + 3373 1 /* lrs_stateid */ + 3374 op_encode_stateid_maxsz) * sizeof(__be32); 3375 } 3376 #endif /* CONFIG_NFSD_PNFS */ 3377 3378 3379 static u32 nfsd4_seek_rsize(const struct svc_rqst *rqstp, 3380 const struct nfsd4_op *op) 3381 { 3382 return (op_encode_hdr_size + 3) * sizeof(__be32); 3383 } 3384 3385 static u32 nfsd4_getxattr_rsize(const struct svc_rqst *rqstp, 3386 const struct nfsd4_op *op) 3387 { 3388 u32 rlen = min_t(u32, XATTR_SIZE_MAX, nfsd4_max_payload(rqstp)); 3389 3390 return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32); 3391 } 3392 3393 static u32 nfsd4_setxattr_rsize(const struct svc_rqst *rqstp, 3394 const struct nfsd4_op *op) 3395 { 3396 return (op_encode_hdr_size + op_encode_change_info_maxsz) 3397 * sizeof(__be32); 3398 } 3399 static u32 nfsd4_listxattrs_rsize(const struct svc_rqst *rqstp, 3400 const struct nfsd4_op *op) 3401 { 3402 u32 rlen = min(op->u.listxattrs.lsxa_maxcount, nfsd4_max_payload(rqstp)); 3403 3404 return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32); 3405 } 3406 3407 static u32 nfsd4_removexattr_rsize(const struct svc_rqst *rqstp, 3408 const struct nfsd4_op *op) 3409 { 3410 return (op_encode_hdr_size + op_encode_change_info_maxsz) 3411 * sizeof(__be32); 3412 } 3413 3414 3415 static const struct nfsd4_operation nfsd4_ops[] = { 3416 [OP_ACCESS] = { 3417 .op_func = nfsd4_access, 3418 .op_name = "OP_ACCESS", 3419 .op_rsize_bop = nfsd4_access_rsize, 3420 }, 3421 [OP_CLOSE] = { 3422 .op_func = nfsd4_close, 3423 .op_flags = OP_MODIFIES_SOMETHING, 3424 .op_name = "OP_CLOSE", 3425 .op_rsize_bop = nfsd4_status_stateid_rsize, 3426 .op_get_currentstateid = nfsd4_get_closestateid, 3427 .op_set_currentstateid = nfsd4_set_closestateid, 3428 }, 3429 [OP_COMMIT] = { 3430 .op_func = nfsd4_commit, 3431 .op_flags = OP_MODIFIES_SOMETHING, 3432 .op_name = "OP_COMMIT", 3433 .op_rsize_bop = nfsd4_commit_rsize, 3434 }, 3435 [OP_CREATE] = { 3436 .op_func = nfsd4_create, 3437 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID, 3438 .op_name = "OP_CREATE", 3439 .op_rsize_bop = nfsd4_create_rsize, 3440 }, 3441 [OP_DELEGRETURN] = { 3442 .op_func = nfsd4_delegreturn, 3443 .op_flags = OP_MODIFIES_SOMETHING, 3444 .op_name = "OP_DELEGRETURN", 3445 .op_rsize_bop = nfsd4_only_status_rsize, 3446 .op_get_currentstateid = nfsd4_get_delegreturnstateid, 3447 }, 3448 [OP_GETATTR] = { 3449 .op_func = nfsd4_getattr, 3450 .op_flags = ALLOWED_ON_ABSENT_FS, 3451 .op_rsize_bop = nfsd4_getattr_rsize, 3452 .op_name = "OP_GETATTR", 3453 }, 3454 [OP_GETFH] = { 3455 .op_func = nfsd4_getfh, 3456 .op_name = "OP_GETFH", 3457 .op_rsize_bop = nfsd4_getfh_rsize, 3458 }, 3459 [OP_LINK] = { 3460 .op_func = nfsd4_link, 3461 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING 3462 | OP_CACHEME, 3463 .op_name = "OP_LINK", 3464 .op_rsize_bop = nfsd4_link_rsize, 3465 }, 3466 [OP_LOCK] = { 3467 .op_func = nfsd4_lock, 3468 .op_release = nfsd4_lock_release, 3469 .op_flags = OP_MODIFIES_SOMETHING | 3470 OP_NONTRIVIAL_ERROR_ENCODE, 3471 .op_name = "OP_LOCK", 3472 .op_rsize_bop = nfsd4_lock_rsize, 3473 .op_set_currentstateid = nfsd4_set_lockstateid, 3474 }, 3475 [OP_LOCKT] = { 3476 .op_func = nfsd4_lockt, 3477 .op_release = nfsd4_lockt_release, 3478 .op_flags = OP_NONTRIVIAL_ERROR_ENCODE, 3479 .op_name = "OP_LOCKT", 3480 .op_rsize_bop = nfsd4_lock_rsize, 3481 }, 3482 [OP_LOCKU] = { 3483 .op_func = nfsd4_locku, 3484 .op_flags = OP_MODIFIES_SOMETHING, 3485 .op_name = "OP_LOCKU", 3486 .op_rsize_bop = nfsd4_status_stateid_rsize, 3487 .op_get_currentstateid = nfsd4_get_lockustateid, 3488 }, 3489 [OP_LOOKUP] = { 3490 .op_func = nfsd4_lookup, 3491 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID, 3492 .op_name = "OP_LOOKUP", 3493 .op_rsize_bop = nfsd4_only_status_rsize, 3494 }, 3495 [OP_LOOKUPP] = { 3496 .op_func = nfsd4_lookupp, 3497 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID, 3498 .op_name = "OP_LOOKUPP", 3499 .op_rsize_bop = nfsd4_only_status_rsize, 3500 }, 3501 [OP_NVERIFY] = { 3502 .op_func = nfsd4_nverify, 3503 .op_name = "OP_NVERIFY", 3504 .op_rsize_bop = nfsd4_only_status_rsize, 3505 }, 3506 [OP_OPEN] = { 3507 .op_func = nfsd4_open, 3508 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING, 3509 .op_name = "OP_OPEN", 3510 .op_rsize_bop = nfsd4_open_rsize, 3511 .op_set_currentstateid = nfsd4_set_openstateid, 3512 }, 3513 [OP_OPEN_CONFIRM] = { 3514 .op_func = nfsd4_open_confirm, 3515 .op_flags = OP_MODIFIES_SOMETHING, 3516 .op_name = "OP_OPEN_CONFIRM", 3517 .op_rsize_bop = nfsd4_status_stateid_rsize, 3518 }, 3519 [OP_OPEN_DOWNGRADE] = { 3520 .op_func = nfsd4_open_downgrade, 3521 .op_flags = OP_MODIFIES_SOMETHING, 3522 .op_name = "OP_OPEN_DOWNGRADE", 3523 .op_rsize_bop = nfsd4_status_stateid_rsize, 3524 .op_get_currentstateid = nfsd4_get_opendowngradestateid, 3525 .op_set_currentstateid = nfsd4_set_opendowngradestateid, 3526 }, 3527 [OP_PUTFH] = { 3528 .op_func = nfsd4_putfh, 3529 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3530 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID, 3531 .op_name = "OP_PUTFH", 3532 .op_rsize_bop = nfsd4_only_status_rsize, 3533 }, 3534 [OP_PUTPUBFH] = { 3535 .op_func = nfsd4_putrootfh, 3536 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3537 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID, 3538 .op_name = "OP_PUTPUBFH", 3539 .op_rsize_bop = nfsd4_only_status_rsize, 3540 }, 3541 [OP_PUTROOTFH] = { 3542 .op_func = nfsd4_putrootfh, 3543 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3544 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID, 3545 .op_name = "OP_PUTROOTFH", 3546 .op_rsize_bop = nfsd4_only_status_rsize, 3547 }, 3548 [OP_READ] = { 3549 .op_func = nfsd4_read, 3550 .op_release = nfsd4_read_release, 3551 .op_name = "OP_READ", 3552 .op_rsize_bop = nfsd4_read_rsize, 3553 .op_get_currentstateid = nfsd4_get_readstateid, 3554 }, 3555 [OP_READDIR] = { 3556 .op_func = nfsd4_readdir, 3557 .op_name = "OP_READDIR", 3558 .op_rsize_bop = nfsd4_readdir_rsize, 3559 }, 3560 [OP_READLINK] = { 3561 .op_func = nfsd4_readlink, 3562 .op_name = "OP_READLINK", 3563 .op_rsize_bop = nfsd4_readlink_rsize, 3564 }, 3565 [OP_REMOVE] = { 3566 .op_func = nfsd4_remove, 3567 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME, 3568 .op_name = "OP_REMOVE", 3569 .op_rsize_bop = nfsd4_remove_rsize, 3570 }, 3571 [OP_RENAME] = { 3572 .op_func = nfsd4_rename, 3573 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME, 3574 .op_name = "OP_RENAME", 3575 .op_rsize_bop = nfsd4_rename_rsize, 3576 }, 3577 [OP_RENEW] = { 3578 .op_func = nfsd4_renew, 3579 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3580 | OP_MODIFIES_SOMETHING, 3581 .op_name = "OP_RENEW", 3582 .op_rsize_bop = nfsd4_only_status_rsize, 3583 3584 }, 3585 [OP_RESTOREFH] = { 3586 .op_func = nfsd4_restorefh, 3587 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3588 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING, 3589 .op_name = "OP_RESTOREFH", 3590 .op_rsize_bop = nfsd4_only_status_rsize, 3591 }, 3592 [OP_SAVEFH] = { 3593 .op_func = nfsd4_savefh, 3594 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING, 3595 .op_name = "OP_SAVEFH", 3596 .op_rsize_bop = nfsd4_only_status_rsize, 3597 }, 3598 [OP_SECINFO] = { 3599 .op_func = nfsd4_secinfo, 3600 .op_release = nfsd4_secinfo_release, 3601 .op_flags = OP_HANDLES_WRONGSEC, 3602 .op_name = "OP_SECINFO", 3603 .op_rsize_bop = nfsd4_secinfo_rsize, 3604 }, 3605 [OP_SETATTR] = { 3606 .op_func = nfsd4_setattr, 3607 .op_name = "OP_SETATTR", 3608 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME 3609 | OP_NONTRIVIAL_ERROR_ENCODE, 3610 .op_rsize_bop = nfsd4_setattr_rsize, 3611 .op_get_currentstateid = nfsd4_get_setattrstateid, 3612 }, 3613 [OP_SETCLIENTID] = { 3614 .op_func = nfsd4_setclientid, 3615 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3616 | OP_MODIFIES_SOMETHING | OP_CACHEME 3617 | OP_NONTRIVIAL_ERROR_ENCODE, 3618 .op_name = "OP_SETCLIENTID", 3619 .op_rsize_bop = nfsd4_setclientid_rsize, 3620 }, 3621 [OP_SETCLIENTID_CONFIRM] = { 3622 .op_func = nfsd4_setclientid_confirm, 3623 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3624 | OP_MODIFIES_SOMETHING | OP_CACHEME, 3625 .op_name = "OP_SETCLIENTID_CONFIRM", 3626 .op_rsize_bop = nfsd4_only_status_rsize, 3627 }, 3628 [OP_VERIFY] = { 3629 .op_func = nfsd4_verify, 3630 .op_name = "OP_VERIFY", 3631 .op_rsize_bop = nfsd4_only_status_rsize, 3632 }, 3633 [OP_WRITE] = { 3634 .op_func = nfsd4_write, 3635 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME, 3636 .op_name = "OP_WRITE", 3637 .op_rsize_bop = nfsd4_write_rsize, 3638 .op_get_currentstateid = nfsd4_get_writestateid, 3639 }, 3640 [OP_RELEASE_LOCKOWNER] = { 3641 .op_func = nfsd4_release_lockowner, 3642 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS 3643 | OP_MODIFIES_SOMETHING, 3644 .op_name = "OP_RELEASE_LOCKOWNER", 3645 .op_rsize_bop = nfsd4_only_status_rsize, 3646 }, 3647 3648 /* NFSv4.1 operations */ 3649 [OP_EXCHANGE_ID] = { 3650 .op_func = nfsd4_exchange_id, 3651 .op_release = nfsd4_exchange_id_release, 3652 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP 3653 | OP_MODIFIES_SOMETHING, 3654 .op_name = "OP_EXCHANGE_ID", 3655 .op_rsize_bop = nfsd4_exchange_id_rsize, 3656 }, 3657 [OP_BACKCHANNEL_CTL] = { 3658 .op_func = nfsd4_backchannel_ctl, 3659 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING, 3660 .op_name = "OP_BACKCHANNEL_CTL", 3661 .op_rsize_bop = nfsd4_only_status_rsize, 3662 }, 3663 [OP_BIND_CONN_TO_SESSION] = { 3664 .op_func = nfsd4_bind_conn_to_session, 3665 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP 3666 | OP_MODIFIES_SOMETHING, 3667 .op_name = "OP_BIND_CONN_TO_SESSION", 3668 .op_rsize_bop = nfsd4_bind_conn_to_session_rsize, 3669 }, 3670 [OP_CREATE_SESSION] = { 3671 .op_func = nfsd4_create_session, 3672 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP 3673 | OP_MODIFIES_SOMETHING, 3674 .op_name = "OP_CREATE_SESSION", 3675 .op_rsize_bop = nfsd4_create_session_rsize, 3676 }, 3677 [OP_DESTROY_SESSION] = { 3678 .op_func = nfsd4_destroy_session, 3679 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP 3680 | OP_MODIFIES_SOMETHING, 3681 .op_name = "OP_DESTROY_SESSION", 3682 .op_rsize_bop = nfsd4_only_status_rsize, 3683 }, 3684 [OP_SEQUENCE] = { 3685 .op_func = nfsd4_sequence, 3686 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP, 3687 .op_name = "OP_SEQUENCE", 3688 .op_rsize_bop = nfsd4_sequence_rsize, 3689 }, 3690 [OP_DESTROY_CLIENTID] = { 3691 .op_func = nfsd4_destroy_clientid, 3692 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP 3693 | OP_MODIFIES_SOMETHING, 3694 .op_name = "OP_DESTROY_CLIENTID", 3695 .op_rsize_bop = nfsd4_only_status_rsize, 3696 }, 3697 [OP_RECLAIM_COMPLETE] = { 3698 .op_func = nfsd4_reclaim_complete, 3699 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING, 3700 .op_name = "OP_RECLAIM_COMPLETE", 3701 .op_rsize_bop = nfsd4_only_status_rsize, 3702 }, 3703 [OP_SECINFO_NO_NAME] = { 3704 .op_func = nfsd4_secinfo_no_name, 3705 .op_release = nfsd4_secinfo_no_name_release, 3706 .op_flags = OP_HANDLES_WRONGSEC, 3707 .op_name = "OP_SECINFO_NO_NAME", 3708 .op_rsize_bop = nfsd4_secinfo_rsize, 3709 }, 3710 [OP_TEST_STATEID] = { 3711 .op_func = nfsd4_test_stateid, 3712 .op_flags = ALLOWED_WITHOUT_FH, 3713 .op_name = "OP_TEST_STATEID", 3714 .op_rsize_bop = nfsd4_test_stateid_rsize, 3715 }, 3716 [OP_FREE_STATEID] = { 3717 .op_func = nfsd4_free_stateid, 3718 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING, 3719 .op_name = "OP_FREE_STATEID", 3720 .op_get_currentstateid = nfsd4_get_freestateid, 3721 .op_rsize_bop = nfsd4_only_status_rsize, 3722 }, 3723 [OP_GET_DIR_DELEGATION] = { 3724 .op_func = nfsd4_get_dir_delegation, 3725 .op_flags = OP_MODIFIES_SOMETHING, 3726 .op_name = "OP_GET_DIR_DELEGATION", 3727 .op_rsize_bop = nfsd4_get_dir_delegation_rsize, 3728 }, 3729 #ifdef CONFIG_NFSD_PNFS 3730 [OP_GETDEVICEINFO] = { 3731 .op_func = nfsd4_getdeviceinfo, 3732 .op_release = nfsd4_getdeviceinfo_release, 3733 .op_flags = ALLOWED_WITHOUT_FH, 3734 .op_name = "OP_GETDEVICEINFO", 3735 .op_rsize_bop = nfsd4_getdeviceinfo_rsize, 3736 }, 3737 [OP_LAYOUTGET] = { 3738 .op_func = nfsd4_layoutget, 3739 .op_release = nfsd4_layoutget_release, 3740 .op_flags = OP_MODIFIES_SOMETHING, 3741 .op_name = "OP_LAYOUTGET", 3742 .op_rsize_bop = nfsd4_layoutget_rsize, 3743 }, 3744 [OP_LAYOUTCOMMIT] = { 3745 .op_func = nfsd4_layoutcommit, 3746 .op_flags = OP_MODIFIES_SOMETHING, 3747 .op_name = "OP_LAYOUTCOMMIT", 3748 .op_rsize_bop = nfsd4_layoutcommit_rsize, 3749 }, 3750 [OP_LAYOUTRETURN] = { 3751 .op_func = nfsd4_layoutreturn, 3752 .op_flags = OP_MODIFIES_SOMETHING, 3753 .op_name = "OP_LAYOUTRETURN", 3754 .op_rsize_bop = nfsd4_layoutreturn_rsize, 3755 }, 3756 #endif /* CONFIG_NFSD_PNFS */ 3757 3758 /* NFSv4.2 operations */ 3759 [OP_ALLOCATE] = { 3760 .op_func = nfsd4_allocate, 3761 .op_flags = OP_MODIFIES_SOMETHING, 3762 .op_name = "OP_ALLOCATE", 3763 .op_rsize_bop = nfsd4_only_status_rsize, 3764 }, 3765 [OP_DEALLOCATE] = { 3766 .op_func = nfsd4_deallocate, 3767 .op_flags = OP_MODIFIES_SOMETHING, 3768 .op_name = "OP_DEALLOCATE", 3769 .op_rsize_bop = nfsd4_only_status_rsize, 3770 }, 3771 [OP_CLONE] = { 3772 .op_func = nfsd4_clone, 3773 .op_flags = OP_MODIFIES_SOMETHING, 3774 .op_name = "OP_CLONE", 3775 .op_rsize_bop = nfsd4_only_status_rsize, 3776 }, 3777 [OP_COPY] = { 3778 .op_func = nfsd4_copy, 3779 .op_flags = OP_MODIFIES_SOMETHING, 3780 .op_name = "OP_COPY", 3781 .op_rsize_bop = nfsd4_copy_rsize, 3782 }, 3783 [OP_READ_PLUS] = { 3784 .op_func = nfsd4_read, 3785 .op_release = nfsd4_read_release, 3786 .op_name = "OP_READ_PLUS", 3787 .op_rsize_bop = nfsd4_read_plus_rsize, 3788 .op_get_currentstateid = nfsd4_get_readstateid, 3789 }, 3790 [OP_SEEK] = { 3791 .op_func = nfsd4_seek, 3792 .op_name = "OP_SEEK", 3793 .op_rsize_bop = nfsd4_seek_rsize, 3794 }, 3795 [OP_OFFLOAD_STATUS] = { 3796 .op_func = nfsd4_offload_status, 3797 .op_name = "OP_OFFLOAD_STATUS", 3798 .op_rsize_bop = nfsd4_offload_status_rsize, 3799 }, 3800 [OP_OFFLOAD_CANCEL] = { 3801 .op_func = nfsd4_offload_cancel, 3802 .op_flags = OP_MODIFIES_SOMETHING, 3803 .op_name = "OP_OFFLOAD_CANCEL", 3804 .op_rsize_bop = nfsd4_only_status_rsize, 3805 }, 3806 [OP_COPY_NOTIFY] = { 3807 .op_func = nfsd4_copy_notify, 3808 .op_flags = OP_MODIFIES_SOMETHING, 3809 .op_name = "OP_COPY_NOTIFY", 3810 .op_rsize_bop = nfsd4_copy_notify_rsize, 3811 }, 3812 [OP_GETXATTR] = { 3813 .op_func = nfsd4_getxattr, 3814 .op_name = "OP_GETXATTR", 3815 .op_rsize_bop = nfsd4_getxattr_rsize, 3816 }, 3817 [OP_SETXATTR] = { 3818 .op_func = nfsd4_setxattr, 3819 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME, 3820 .op_name = "OP_SETXATTR", 3821 .op_rsize_bop = nfsd4_setxattr_rsize, 3822 }, 3823 [OP_LISTXATTRS] = { 3824 .op_func = nfsd4_listxattrs, 3825 .op_name = "OP_LISTXATTRS", 3826 .op_rsize_bop = nfsd4_listxattrs_rsize, 3827 }, 3828 [OP_REMOVEXATTR] = { 3829 .op_func = nfsd4_removexattr, 3830 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME, 3831 .op_name = "OP_REMOVEXATTR", 3832 .op_rsize_bop = nfsd4_removexattr_rsize, 3833 }, 3834 }; 3835 3836 /** 3837 * nfsd4_spo_must_allow - Determine if the compound op contains an 3838 * operation that is allowed to be sent with machine credentials 3839 * 3840 * @rqstp: a pointer to the struct svc_rqst 3841 * 3842 * Checks to see if the compound contains a spo_must_allow op 3843 * and confirms that it was sent with the proper machine creds. 3844 */ 3845 3846 bool nfsd4_spo_must_allow(struct svc_rqst *rqstp) 3847 { 3848 struct nfsd4_compoundres *resp = rqstp->rq_resp; 3849 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 3850 struct nfsd4_op *this; 3851 struct nfsd4_compound_state *cstate = &resp->cstate; 3852 struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow; 3853 u32 opiter; 3854 3855 if (rqstp->rq_procinfo != &nfsd_version4.vs_proc[NFSPROC4_COMPOUND] || 3856 cstate->minorversion == 0) 3857 return false; 3858 3859 if (cstate->spo_must_allowed) 3860 return true; 3861 3862 opiter = resp->opcnt; 3863 while (opiter < argp->opcnt) { 3864 this = &argp->ops[opiter++]; 3865 if (test_bit(this->opnum, allow->u.longs) && 3866 cstate->clp->cl_mach_cred && 3867 nfsd4_mach_creds_match(cstate->clp, rqstp)) { 3868 cstate->spo_must_allowed = true; 3869 return true; 3870 } 3871 } 3872 cstate->spo_must_allowed = false; 3873 return false; 3874 } 3875 3876 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op) 3877 { 3878 if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp) 3879 return op_encode_hdr_size * sizeof(__be32); 3880 3881 BUG_ON(OPDESC(op)->op_rsize_bop == NULL); 3882 return OPDESC(op)->op_rsize_bop(rqstp, op); 3883 } 3884 3885 void warn_on_nonidempotent_op(struct nfsd4_op *op) 3886 { 3887 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) { 3888 pr_err("unable to encode reply to nonidempotent op %u (%s)\n", 3889 op->opnum, nfsd4_op_name(op->opnum)); 3890 WARN_ON_ONCE(1); 3891 } 3892 } 3893 3894 static const char *nfsd4_op_name(unsigned opnum) 3895 { 3896 if (opnum < ARRAY_SIZE(nfsd4_ops)) 3897 return nfsd4_ops[opnum].op_name; 3898 return "unknown_operation"; 3899 } 3900 3901 static const struct svc_procedure nfsd_procedures4[2] = { 3902 [NFSPROC4_NULL] = { 3903 .pc_func = nfsd4_proc_null, 3904 .pc_decode = nfssvc_decode_voidarg, 3905 .pc_encode = nfssvc_encode_voidres, 3906 .pc_argsize = sizeof(struct nfsd_voidargs), 3907 .pc_argzero = sizeof(struct nfsd_voidargs), 3908 .pc_ressize = sizeof(struct nfsd_voidres), 3909 .pc_cachetype = RC_NOCACHE, 3910 .pc_xdrressize = 1, 3911 .pc_name = "NULL", 3912 }, 3913 [NFSPROC4_COMPOUND] = { 3914 .pc_func = nfsd4_proc_compound, 3915 .pc_decode = nfs4svc_decode_compoundargs, 3916 .pc_encode = nfs4svc_encode_compoundres, 3917 .pc_argsize = sizeof(struct nfsd4_compoundargs), 3918 .pc_argzero = offsetof(struct nfsd4_compoundargs, iops), 3919 .pc_ressize = sizeof(struct nfsd4_compoundres), 3920 .pc_release = nfsd4_release_compoundargs, 3921 .pc_cachetype = RC_NOCACHE, 3922 .pc_xdrressize = 3+NFSSVC_MAXBLKSIZE/4, 3923 .pc_name = "COMPOUND", 3924 }, 3925 }; 3926 3927 static DEFINE_PER_CPU_ALIGNED(unsigned long, 3928 nfsd_count4[ARRAY_SIZE(nfsd_procedures4)]); 3929 const struct svc_version nfsd_version4 = { 3930 .vs_vers = 4, 3931 .vs_nproc = ARRAY_SIZE(nfsd_procedures4), 3932 .vs_proc = nfsd_procedures4, 3933 .vs_count = nfsd_count4, 3934 .vs_dispatch = nfsd_dispatch, 3935 .vs_xdrsize = NFS4_SVC_XDRSIZE, 3936 .vs_rpcb_optnl = true, 3937 .vs_need_cong_ctrl = true, 3938 }; 3939