1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2011, Lawrence Livermore National Security, LLC. 24 * Copyright (c) 2015 by Chunwei Chen. All rights reserved. 25 * Copyright (c) 2025, Rob Norris <robn@despairlabs.com> 26 */ 27 28 29 #include <sys/sysmacros.h> 30 #include <sys/zfs_ctldir.h> 31 #include <sys/zfs_vfsops.h> 32 #include <sys/zfs_vnops.h> 33 #include <sys/zfs_znode.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/spa_impl.h> 36 #include <sys/vfs.h> 37 #include <sys/zpl.h> 38 #include <sys/file.h> 39 40 static struct dentry * 41 zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 42 { 43 cred_t *cr = CRED(); 44 struct inode *ip; 45 znode_t *zp; 46 int error; 47 fstrans_cookie_t cookie; 48 pathname_t *ppn = NULL; 49 pathname_t pn; 50 int zfs_flags = 0; 51 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; 52 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os); 53 size_t dlen = dlen(dentry); 54 55 /* 56 * If z_longname is disabled, disallow create or rename of names 57 * longer than ZAP_MAXNAMELEN. 58 * 59 * This is needed in cases where longname was enabled first and some 60 * files/dirs with names > ZAP_MAXNAMELEN were created. And later 61 * longname was disabled. In such a case allow access to existing 62 * longnames. But disallow creation newer longnamed entities. 63 */ 64 if (!zfsvfs->z_longname && (dlen >= ZAP_MAXNAMELEN)) { 65 /* 66 * If this is for create or rename fail it. 67 */ 68 if (!dsl_dataset_feature_is_active(ds, SPA_FEATURE_LONGNAME) || 69 (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))) 70 return (ERR_PTR(-ENAMETOOLONG)); 71 } 72 if (dlen >= ZAP_MAXNAMELEN_NEW) { 73 return (ERR_PTR(-ENAMETOOLONG)); 74 } 75 76 crhold(cr); 77 cookie = spl_fstrans_mark(); 78 79 /* If we are a case insensitive fs, we need the real name */ 80 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 81 zfs_flags = FIGNORECASE; 82 pn_alloc(&pn); 83 ppn = &pn; 84 } 85 86 error = -zfs_lookup(ITOZ(dir), dname(dentry), &zp, 87 zfs_flags, cr, NULL, ppn); 88 spl_fstrans_unmark(cookie); 89 ASSERT3S(error, <=, 0); 90 crfree(cr); 91 92 spin_lock(&dentry->d_lock); 93 dentry->d_time = jiffies; 94 spin_unlock(&dentry->d_lock); 95 96 if (error) { 97 /* 98 * If we have a case sensitive fs, we do not want to 99 * insert negative entries, so return NULL for ENOENT. 100 * Fall through if the error is not ENOENT. Also free memory. 101 */ 102 if (ppn) { 103 pn_free(ppn); 104 if (error == -ENOENT) 105 return (NULL); 106 } 107 108 if (error == -ENOENT) 109 return (d_splice_alias(NULL, dentry)); 110 else 111 return (ERR_PTR(error)); 112 } 113 ip = ZTOI(zp); 114 115 /* 116 * If we are case insensitive, call the correct function 117 * to install the name. 118 */ 119 if (ppn) { 120 struct dentry *new_dentry; 121 struct qstr ci_name; 122 123 if (strcmp(dname(dentry), pn.pn_buf) == 0) { 124 new_dentry = d_splice_alias(ip, dentry); 125 } else { 126 ci_name.name = pn.pn_buf; 127 ci_name.len = strlen(pn.pn_buf); 128 new_dentry = d_add_ci(dentry, ip, &ci_name); 129 } 130 pn_free(ppn); 131 return (new_dentry); 132 } else { 133 return (d_splice_alias(ip, dentry)); 134 } 135 } 136 137 void 138 zpl_vap_init(vattr_t *vap, struct inode *dir, umode_t mode, cred_t *cr, 139 zidmap_t *mnt_ns) 140 { 141 vap->va_mask = ATTR_MODE; 142 vap->va_mode = mode; 143 144 vap->va_uid = zfs_vfsuid_to_uid(mnt_ns, 145 zfs_i_user_ns(dir), crgetuid(cr)); 146 147 if (dir->i_mode & S_ISGID) { 148 vap->va_gid = KGID_TO_SGID(dir->i_gid); 149 if (S_ISDIR(mode)) 150 vap->va_mode |= S_ISGID; 151 } else { 152 vap->va_gid = zfs_vfsgid_to_gid(mnt_ns, 153 zfs_i_user_ns(dir), crgetgid(cr)); 154 } 155 } 156 157 static inline bool 158 is_nametoolong(struct dentry *dentry) 159 { 160 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; 161 size_t dlen = dlen(dentry); 162 163 return ((!zfsvfs->z_longname && dlen >= ZAP_MAXNAMELEN) || 164 dlen >= ZAP_MAXNAMELEN_NEW); 165 } 166 167 static int 168 #ifdef HAVE_IOPS_CREATE_USERNS 169 zpl_create(struct user_namespace *user_ns, struct inode *dir, 170 struct dentry *dentry, umode_t mode, bool flag) 171 #elif defined(HAVE_IOPS_CREATE_IDMAP) 172 zpl_create(struct mnt_idmap *user_ns, struct inode *dir, 173 struct dentry *dentry, umode_t mode, bool flag) 174 #else 175 zpl_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool flag) 176 #endif 177 { 178 cred_t *cr = CRED(); 179 znode_t *zp; 180 vattr_t *vap; 181 int error; 182 fstrans_cookie_t cookie; 183 #if !(defined(HAVE_IOPS_CREATE_USERNS) || defined(HAVE_IOPS_CREATE_IDMAP)) 184 zidmap_t *user_ns = kcred->user_ns; 185 #endif 186 187 if (is_nametoolong(dentry)) { 188 return (-ENAMETOOLONG); 189 } 190 191 crhold(cr); 192 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 193 zpl_vap_init(vap, dir, mode, cr, user_ns); 194 195 cookie = spl_fstrans_mark(); 196 error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0, 197 mode, &zp, cr, 0, NULL, user_ns); 198 if (error == 0) { 199 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name); 200 if (error == 0) 201 error = zpl_init_acl(ZTOI(zp), dir); 202 203 if (error) { 204 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0); 205 remove_inode_hash(ZTOI(zp)); 206 iput(ZTOI(zp)); 207 } else { 208 d_instantiate(dentry, ZTOI(zp)); 209 } 210 } 211 212 spl_fstrans_unmark(cookie); 213 kmem_free(vap, sizeof (vattr_t)); 214 crfree(cr); 215 ASSERT3S(error, <=, 0); 216 217 return (error); 218 } 219 220 static int 221 #ifdef HAVE_IOPS_MKNOD_USERNS 222 zpl_mknod(struct user_namespace *user_ns, struct inode *dir, 223 struct dentry *dentry, umode_t mode, 224 #elif defined(HAVE_IOPS_MKNOD_IDMAP) 225 zpl_mknod(struct mnt_idmap *user_ns, struct inode *dir, 226 struct dentry *dentry, umode_t mode, 227 #else 228 zpl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, 229 #endif 230 dev_t rdev) 231 { 232 cred_t *cr = CRED(); 233 znode_t *zp; 234 vattr_t *vap; 235 int error; 236 fstrans_cookie_t cookie; 237 #if !(defined(HAVE_IOPS_MKNOD_USERNS) || defined(HAVE_IOPS_MKNOD_IDMAP)) 238 zidmap_t *user_ns = kcred->user_ns; 239 #endif 240 241 if (is_nametoolong(dentry)) { 242 return (-ENAMETOOLONG); 243 } 244 245 /* 246 * We currently expect Linux to supply rdev=0 for all sockets 247 * and fifos, but we want to know if this behavior ever changes. 248 */ 249 if (S_ISSOCK(mode) || S_ISFIFO(mode)) 250 ASSERT(rdev == 0); 251 252 crhold(cr); 253 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 254 zpl_vap_init(vap, dir, mode, cr, user_ns); 255 vap->va_rdev = rdev; 256 257 cookie = spl_fstrans_mark(); 258 error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0, 259 mode, &zp, cr, 0, NULL, user_ns); 260 if (error == 0) { 261 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name); 262 if (error == 0) 263 error = zpl_init_acl(ZTOI(zp), dir); 264 265 if (error) { 266 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0); 267 remove_inode_hash(ZTOI(zp)); 268 iput(ZTOI(zp)); 269 } else { 270 d_instantiate(dentry, ZTOI(zp)); 271 } 272 } 273 274 spl_fstrans_unmark(cookie); 275 kmem_free(vap, sizeof (vattr_t)); 276 crfree(cr); 277 ASSERT3S(error, <=, 0); 278 279 return (error); 280 } 281 282 static int 283 #ifdef HAVE_TMPFILE_IDMAP 284 zpl_tmpfile(struct mnt_idmap *userns, struct inode *dir, 285 struct file *file, umode_t mode) 286 #elif !defined(HAVE_TMPFILE_DENTRY) 287 zpl_tmpfile(struct user_namespace *userns, struct inode *dir, 288 struct file *file, umode_t mode) 289 #else 290 #ifdef HAVE_TMPFILE_USERNS 291 zpl_tmpfile(struct user_namespace *userns, struct inode *dir, 292 struct dentry *dentry, umode_t mode) 293 #else 294 zpl_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 295 #endif 296 #endif 297 { 298 cred_t *cr = CRED(); 299 struct inode *ip; 300 vattr_t *vap; 301 int error; 302 fstrans_cookie_t cookie; 303 #if !(defined(HAVE_TMPFILE_USERNS) || defined(HAVE_TMPFILE_IDMAP)) 304 zidmap_t *userns = kcred->user_ns; 305 #endif 306 307 crhold(cr); 308 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 309 /* 310 * The VFS does not apply the umask, therefore it is applied here 311 * when POSIX ACLs are not enabled. 312 */ 313 if (!IS_POSIXACL(dir)) 314 mode &= ~current_umask(); 315 zpl_vap_init(vap, dir, mode, cr, userns); 316 317 cookie = spl_fstrans_mark(); 318 error = -zfs_tmpfile(dir, vap, 0, mode, &ip, cr, 0, NULL, userns); 319 if (error == 0) { 320 /* d_tmpfile will do drop_nlink, so we should set it first */ 321 set_nlink(ip, 1); 322 #ifndef HAVE_TMPFILE_DENTRY 323 d_tmpfile(file, ip); 324 325 error = zpl_xattr_security_init(ip, dir, 326 &file->f_path.dentry->d_name); 327 #else 328 d_tmpfile(dentry, ip); 329 330 error = zpl_xattr_security_init(ip, dir, &dentry->d_name); 331 #endif 332 if (error == 0) 333 error = zpl_init_acl(ip, dir); 334 #ifndef HAVE_TMPFILE_DENTRY 335 error = finish_open_simple(file, error); 336 #endif 337 /* 338 * don't need to handle error here, file is already in 339 * unlinked set. 340 */ 341 } 342 343 spl_fstrans_unmark(cookie); 344 kmem_free(vap, sizeof (vattr_t)); 345 crfree(cr); 346 ASSERT3S(error, <=, 0); 347 348 return (error); 349 } 350 351 static int 352 zpl_unlink(struct inode *dir, struct dentry *dentry) 353 { 354 cred_t *cr = CRED(); 355 int error; 356 fstrans_cookie_t cookie; 357 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; 358 359 crhold(cr); 360 cookie = spl_fstrans_mark(); 361 error = -zfs_remove(ITOZ(dir), dname(dentry), cr, 0); 362 363 /* 364 * For a CI FS we must invalidate the dentry to prevent the 365 * creation of negative entries. 366 */ 367 if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE) 368 d_invalidate(dentry); 369 370 spl_fstrans_unmark(cookie); 371 crfree(cr); 372 ASSERT3S(error, <=, 0); 373 374 return (error); 375 } 376 377 #if defined(HAVE_IOPS_MKDIR_USERNS) 378 static int 379 zpl_mkdir(struct user_namespace *user_ns, struct inode *dir, 380 struct dentry *dentry, umode_t mode) 381 #elif defined(HAVE_IOPS_MKDIR_IDMAP) 382 static int 383 zpl_mkdir(struct mnt_idmap *user_ns, struct inode *dir, 384 struct dentry *dentry, umode_t mode) 385 #elif defined(HAVE_IOPS_MKDIR_DENTRY) 386 static struct dentry * 387 zpl_mkdir(struct mnt_idmap *user_ns, struct inode *dir, 388 struct dentry *dentry, umode_t mode) 389 #else 390 static int 391 zpl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 392 #endif 393 { 394 cred_t *cr = CRED(); 395 vattr_t *vap; 396 znode_t *zp; 397 int error; 398 fstrans_cookie_t cookie; 399 #if !(defined(HAVE_IOPS_MKDIR_USERNS) || \ 400 defined(HAVE_IOPS_MKDIR_IDMAP) || defined(HAVE_IOPS_MKDIR_DENTRY)) 401 zidmap_t *user_ns = kcred->user_ns; 402 #endif 403 404 if (is_nametoolong(dentry)) { 405 error = -ENAMETOOLONG; 406 goto err; 407 } 408 409 crhold(cr); 410 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 411 zpl_vap_init(vap, dir, mode | S_IFDIR, cr, user_ns); 412 413 cookie = spl_fstrans_mark(); 414 error = -zfs_mkdir(ITOZ(dir), dname(dentry), vap, &zp, cr, 0, NULL, 415 user_ns); 416 if (error == 0) { 417 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name); 418 if (error == 0) 419 error = zpl_init_acl(ZTOI(zp), dir); 420 421 if (error) { 422 (void) zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0); 423 remove_inode_hash(ZTOI(zp)); 424 iput(ZTOI(zp)); 425 } else { 426 d_instantiate(dentry, ZTOI(zp)); 427 } 428 } 429 430 spl_fstrans_unmark(cookie); 431 kmem_free(vap, sizeof (vattr_t)); 432 crfree(cr); 433 434 err: 435 ASSERT3S(error, <=, 0); 436 #if defined(HAVE_IOPS_MKDIR_DENTRY) 437 return (error != 0 ? ERR_PTR(error) : NULL); 438 #else 439 return (error); 440 #endif 441 } 442 443 static int 444 zpl_rmdir(struct inode *dir, struct dentry *dentry) 445 { 446 cred_t *cr = CRED(); 447 int error; 448 fstrans_cookie_t cookie; 449 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; 450 451 crhold(cr); 452 cookie = spl_fstrans_mark(); 453 error = -zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0); 454 455 /* 456 * For a CI FS we must invalidate the dentry to prevent the 457 * creation of negative entries. 458 */ 459 if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE) 460 d_invalidate(dentry); 461 462 spl_fstrans_unmark(cookie); 463 crfree(cr); 464 ASSERT3S(error, <=, 0); 465 466 return (error); 467 } 468 469 static int 470 #ifdef HAVE_USERNS_IOPS_GETATTR 471 zpl_getattr_impl(struct user_namespace *user_ns, 472 const struct path *path, struct kstat *stat, u32 request_mask, 473 unsigned int query_flags) 474 #elif defined(HAVE_IDMAP_IOPS_GETATTR) 475 zpl_getattr_impl(struct mnt_idmap *user_ns, 476 const struct path *path, struct kstat *stat, u32 request_mask, 477 unsigned int query_flags) 478 #else 479 zpl_getattr_impl(const struct path *path, struct kstat *stat, u32 request_mask, 480 unsigned int query_flags) 481 #endif 482 { 483 int error; 484 fstrans_cookie_t cookie; 485 struct inode *ip = path->dentry->d_inode; 486 znode_t *zp __maybe_unused = ITOZ(ip); 487 488 cookie = spl_fstrans_mark(); 489 490 /* 491 * XXX query_flags currently ignored. 492 */ 493 494 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK 495 error = -zfs_getattr_fast(user_ns, request_mask, ip, stat); 496 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR)) 497 error = -zfs_getattr_fast(user_ns, ip, stat); 498 #else 499 error = -zfs_getattr_fast(kcred->user_ns, ip, stat); 500 #endif 501 502 #ifdef STATX_BTIME 503 if (request_mask & STATX_BTIME) { 504 stat->btime = zp->z_btime; 505 stat->result_mask |= STATX_BTIME; 506 } 507 #endif 508 509 #ifdef STATX_DIOALIGN 510 if (request_mask & STATX_DIOALIGN) { 511 uint64_t align; 512 if (zfs_get_direct_alignment(zp, &align) == 0) { 513 stat->dio_mem_align = PAGE_SIZE; 514 stat->dio_offset_align = align; 515 stat->result_mask |= STATX_DIOALIGN; 516 } 517 } 518 #endif 519 520 #ifdef STATX_ATTR_IMMUTABLE 521 if (zp->z_pflags & ZFS_IMMUTABLE) 522 stat->attributes |= STATX_ATTR_IMMUTABLE; 523 stat->attributes_mask |= STATX_ATTR_IMMUTABLE; 524 #endif 525 526 #ifdef STATX_ATTR_APPEND 527 if (zp->z_pflags & ZFS_APPENDONLY) 528 stat->attributes |= STATX_ATTR_APPEND; 529 stat->attributes_mask |= STATX_ATTR_APPEND; 530 #endif 531 532 #ifdef STATX_ATTR_NODUMP 533 if (zp->z_pflags & ZFS_NODUMP) 534 stat->attributes |= STATX_ATTR_NODUMP; 535 stat->attributes_mask |= STATX_ATTR_NODUMP; 536 #endif 537 538 spl_fstrans_unmark(cookie); 539 ASSERT3S(error, <=, 0); 540 541 return (error); 542 } 543 ZPL_GETATTR_WRAPPER(zpl_getattr); 544 545 static int 546 #ifdef HAVE_USERNS_IOPS_SETATTR 547 zpl_setattr(struct user_namespace *user_ns, struct dentry *dentry, 548 struct iattr *ia) 549 #elif defined(HAVE_IDMAP_IOPS_SETATTR) 550 zpl_setattr(struct mnt_idmap *user_ns, struct dentry *dentry, 551 struct iattr *ia) 552 #else 553 zpl_setattr(struct dentry *dentry, struct iattr *ia) 554 #endif 555 { 556 struct inode *ip = dentry->d_inode; 557 cred_t *cr = CRED(); 558 vattr_t *vap; 559 int error; 560 fstrans_cookie_t cookie; 561 562 #ifdef HAVE_SETATTR_PREPARE_USERNS 563 error = zpl_setattr_prepare(user_ns, dentry, ia); 564 #elif defined(HAVE_SETATTR_PREPARE_IDMAP) 565 error = zpl_setattr_prepare(user_ns, dentry, ia); 566 #else 567 error = zpl_setattr_prepare(zfs_init_idmap, dentry, ia); 568 #endif 569 if (error) 570 return (error); 571 572 crhold(cr); 573 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 574 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK; 575 vap->va_mode = ia->ia_mode; 576 if (ia->ia_valid & ATTR_UID) 577 #ifdef HAVE_IATTR_VFSID 578 vap->va_uid = zfs_vfsuid_to_uid(user_ns, zfs_i_user_ns(ip), 579 __vfsuid_val(ia->ia_vfsuid)); 580 #else 581 vap->va_uid = KUID_TO_SUID(ia->ia_uid); 582 #endif 583 if (ia->ia_valid & ATTR_GID) 584 #ifdef HAVE_IATTR_VFSID 585 vap->va_gid = zfs_vfsgid_to_gid(user_ns, zfs_i_user_ns(ip), 586 __vfsgid_val(ia->ia_vfsgid)); 587 #else 588 vap->va_gid = KGID_TO_SGID(ia->ia_gid); 589 #endif 590 vap->va_size = ia->ia_size; 591 vap->va_atime = ia->ia_atime; 592 vap->va_mtime = ia->ia_mtime; 593 vap->va_ctime = ia->ia_ctime; 594 595 if (vap->va_mask & ATTR_ATIME) 596 zpl_inode_set_atime_to_ts(ip, 597 zpl_inode_timestamp_truncate(ia->ia_atime, ip)); 598 599 cookie = spl_fstrans_mark(); 600 #ifdef HAVE_USERNS_IOPS_SETATTR 601 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns); 602 #elif defined(HAVE_IDMAP_IOPS_SETATTR) 603 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns); 604 #else 605 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, zfs_init_idmap); 606 #endif 607 if (!error && (ia->ia_valid & ATTR_MODE)) 608 error = zpl_chmod_acl(ip); 609 610 spl_fstrans_unmark(cookie); 611 kmem_free(vap, sizeof (vattr_t)); 612 crfree(cr); 613 ASSERT3S(error, <=, 0); 614 615 return (error); 616 } 617 618 static int 619 #ifdef HAVE_IOPS_RENAME_USERNS 620 zpl_rename2(struct user_namespace *user_ns, struct inode *sdip, 621 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry, 622 unsigned int rflags) 623 #elif defined(HAVE_IOPS_RENAME_IDMAP) 624 zpl_rename2(struct mnt_idmap *user_ns, struct inode *sdip, 625 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry, 626 unsigned int rflags) 627 #else 628 zpl_rename2(struct inode *sdip, struct dentry *sdentry, 629 struct inode *tdip, struct dentry *tdentry, unsigned int rflags) 630 #endif 631 { 632 cred_t *cr = CRED(); 633 vattr_t *wo_vap = NULL; 634 int error; 635 fstrans_cookie_t cookie; 636 #if !(defined(HAVE_IOPS_RENAME_USERNS) || defined(HAVE_IOPS_RENAME_IDMAP)) 637 zidmap_t *user_ns = kcred->user_ns; 638 #endif 639 640 if (is_nametoolong(tdentry)) { 641 return (-ENAMETOOLONG); 642 } 643 644 crhold(cr); 645 if (rflags & RENAME_WHITEOUT) { 646 wo_vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 647 zpl_vap_init(wo_vap, sdip, S_IFCHR, cr, user_ns); 648 wo_vap->va_rdev = makedevice(0, 0); 649 } 650 651 cookie = spl_fstrans_mark(); 652 error = -zfs_rename(ITOZ(sdip), dname(sdentry), ITOZ(tdip), 653 dname(tdentry), cr, 0, rflags, wo_vap, user_ns); 654 spl_fstrans_unmark(cookie); 655 if (wo_vap) 656 kmem_free(wo_vap, sizeof (vattr_t)); 657 crfree(cr); 658 ASSERT3S(error, <=, 0); 659 660 return (error); 661 } 662 663 #if !defined(HAVE_IOPS_RENAME_USERNS) && \ 664 !defined(HAVE_RENAME_WANTS_FLAGS) && \ 665 !defined(HAVE_IOPS_RENAME_IDMAP) 666 static int 667 zpl_rename(struct inode *sdip, struct dentry *sdentry, 668 struct inode *tdip, struct dentry *tdentry) 669 { 670 return (zpl_rename2(sdip, sdentry, tdip, tdentry, 0)); 671 } 672 #endif 673 674 static int 675 #ifdef HAVE_IOPS_SYMLINK_USERNS 676 zpl_symlink(struct user_namespace *user_ns, struct inode *dir, 677 struct dentry *dentry, const char *name) 678 #elif defined(HAVE_IOPS_SYMLINK_IDMAP) 679 zpl_symlink(struct mnt_idmap *user_ns, struct inode *dir, 680 struct dentry *dentry, const char *name) 681 #else 682 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name) 683 #endif 684 { 685 cred_t *cr = CRED(); 686 vattr_t *vap; 687 znode_t *zp; 688 int error; 689 fstrans_cookie_t cookie; 690 #if !(defined(HAVE_IOPS_SYMLINK_USERNS) || defined(HAVE_IOPS_SYMLINK_IDMAP)) 691 zidmap_t *user_ns = kcred->user_ns; 692 #endif 693 694 if (is_nametoolong(dentry)) { 695 return (-ENAMETOOLONG); 696 } 697 698 crhold(cr); 699 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 700 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr, user_ns); 701 702 cookie = spl_fstrans_mark(); 703 error = -zfs_symlink(ITOZ(dir), dname(dentry), vap, 704 (char *)name, &zp, cr, 0, user_ns); 705 if (error == 0) { 706 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name); 707 if (error) { 708 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0); 709 remove_inode_hash(ZTOI(zp)); 710 iput(ZTOI(zp)); 711 } else { 712 d_instantiate(dentry, ZTOI(zp)); 713 } 714 } 715 716 spl_fstrans_unmark(cookie); 717 kmem_free(vap, sizeof (vattr_t)); 718 crfree(cr); 719 ASSERT3S(error, <=, 0); 720 721 return (error); 722 } 723 724 static void 725 zpl_put_link(void *ptr) 726 { 727 kmem_free(ptr, MAXPATHLEN); 728 } 729 730 static int 731 zpl_get_link_common(struct dentry *dentry, struct inode *ip, char **link) 732 { 733 fstrans_cookie_t cookie; 734 cred_t *cr = CRED(); 735 int error; 736 737 crhold(cr); 738 *link = NULL; 739 740 struct iovec iov; 741 iov.iov_len = MAXPATHLEN; 742 iov.iov_base = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 743 744 zfs_uio_t uio; 745 zfs_uio_iovec_init(&uio, &iov, 1, 0, UIO_SYSSPACE, MAXPATHLEN - 1, 0); 746 747 cookie = spl_fstrans_mark(); 748 error = -zfs_readlink(ip, &uio, cr); 749 spl_fstrans_unmark(cookie); 750 crfree(cr); 751 752 if (error) 753 kmem_free(iov.iov_base, MAXPATHLEN); 754 else 755 *link = iov.iov_base; 756 757 return (error); 758 } 759 760 static const char * 761 zpl_get_link(struct dentry *dentry, struct inode *inode, 762 struct delayed_call *done) 763 { 764 char *link = NULL; 765 int error; 766 767 if (!dentry) 768 return (ERR_PTR(-ECHILD)); 769 770 error = zpl_get_link_common(dentry, inode, &link); 771 if (error) 772 return (ERR_PTR(error)); 773 774 set_delayed_call(done, zpl_put_link, link); 775 776 return (link); 777 } 778 779 static int 780 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 781 { 782 cred_t *cr = CRED(); 783 struct inode *ip = old_dentry->d_inode; 784 int error; 785 fstrans_cookie_t cookie; 786 787 if (is_nametoolong(dentry)) { 788 return (-ENAMETOOLONG); 789 } 790 791 if (ip->i_nlink >= ZFS_LINK_MAX) 792 return (-EMLINK); 793 794 crhold(cr); 795 zpl_inode_set_ctime_to_ts(ip, current_time(ip)); 796 /* Must have an existing ref, so igrab() cannot return NULL */ 797 VERIFY3P(igrab(ip), !=, NULL); 798 799 cookie = spl_fstrans_mark(); 800 error = -zfs_link(ITOZ(dir), ITOZ(ip), dname(dentry), cr, 0); 801 if (error) { 802 iput(ip); 803 goto out; 804 } 805 806 d_instantiate(dentry, ip); 807 out: 808 spl_fstrans_unmark(cookie); 809 crfree(cr); 810 ASSERT3S(error, <=, 0); 811 812 return (error); 813 } 814 815 const struct inode_operations zpl_inode_operations = { 816 .setattr = zpl_setattr, 817 .getattr = zpl_getattr, 818 .listxattr = zpl_xattr_list, 819 #if defined(CONFIG_FS_POSIX_ACL) 820 .set_acl = zpl_set_acl, 821 #if defined(HAVE_GET_INODE_ACL) 822 .get_inode_acl = zpl_get_acl, 823 #else 824 .get_acl = zpl_get_acl, 825 #endif /* HAVE_GET_INODE_ACL */ 826 #endif /* CONFIG_FS_POSIX_ACL */ 827 }; 828 829 const struct inode_operations zpl_dir_inode_operations = { 830 .create = zpl_create, 831 .lookup = zpl_lookup, 832 .link = zpl_link, 833 .unlink = zpl_unlink, 834 .symlink = zpl_symlink, 835 .mkdir = zpl_mkdir, 836 .rmdir = zpl_rmdir, 837 .mknod = zpl_mknod, 838 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS) 839 .rename = zpl_rename2, 840 #elif defined(HAVE_IOPS_RENAME_IDMAP) 841 .rename = zpl_rename2, 842 #else 843 .rename = zpl_rename, 844 #endif 845 .tmpfile = zpl_tmpfile, 846 .setattr = zpl_setattr, 847 .getattr = zpl_getattr, 848 .listxattr = zpl_xattr_list, 849 #if defined(CONFIG_FS_POSIX_ACL) 850 .set_acl = zpl_set_acl, 851 #if defined(HAVE_GET_INODE_ACL) 852 .get_inode_acl = zpl_get_acl, 853 #else 854 .get_acl = zpl_get_acl, 855 #endif /* HAVE_GET_INODE_ACL */ 856 #endif /* CONFIG_FS_POSIX_ACL */ 857 }; 858 859 const struct inode_operations zpl_symlink_inode_operations = { 860 .get_link = zpl_get_link, 861 .setattr = zpl_setattr, 862 .getattr = zpl_getattr, 863 .listxattr = zpl_xattr_list, 864 }; 865 866 const struct inode_operations zpl_special_inode_operations = { 867 .setattr = zpl_setattr, 868 .getattr = zpl_getattr, 869 .listxattr = zpl_xattr_list, 870 #if defined(CONFIG_FS_POSIX_ACL) 871 .set_acl = zpl_set_acl, 872 #if defined(HAVE_GET_INODE_ACL) 873 .get_inode_acl = zpl_get_acl, 874 #else 875 .get_acl = zpl_get_acl, 876 #endif /* HAVE_GET_INODE_ACL */ 877 #endif /* CONFIG_FS_POSIX_ACL */ 878 }; 879