1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (C) 2011 Lawrence Livermore National Security, LLC. 23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 24 * LLNL-CODE-403049. 25 * Rewritten for Linux by: 26 * Rohan Puri <rohan.puri15@gmail.com> 27 * Brian Behlendorf <behlendorf1@llnl.gov> 28 */ 29 30 #include <sys/zfs_znode.h> 31 #include <sys/zfs_vfsops.h> 32 #include <sys/zfs_vnops.h> 33 #include <sys/zfs_ctldir.h> 34 #include <sys/zpl.h> 35 #include <sys/dmu.h> 36 #include <sys/dsl_dataset.h> 37 #include <sys/zap.h> 38 39 /* 40 * Common open routine. Disallow any write access. 41 */ 42 static int 43 zpl_common_open(struct inode *ip, struct file *filp) 44 { 45 if (filp->f_mode & FMODE_WRITE) 46 return (-EACCES); 47 48 return (generic_file_open(ip, filp)); 49 } 50 51 /* 52 * Get root directory contents. 53 */ 54 static int 55 zpl_root_iterate(struct file *filp, zpl_dir_context_t *ctx) 56 { 57 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 58 int error = 0; 59 60 ZPL_ENTER(zfsvfs); 61 62 if (!zpl_dir_emit_dots(filp, ctx)) 63 goto out; 64 65 if (ctx->pos == 2) { 66 if (!zpl_dir_emit(ctx, ZFS_SNAPDIR_NAME, 67 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR)) 68 goto out; 69 70 ctx->pos++; 71 } 72 73 if (ctx->pos == 3) { 74 if (!zpl_dir_emit(ctx, ZFS_SHAREDIR_NAME, 75 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR)) 76 goto out; 77 78 ctx->pos++; 79 } 80 out: 81 ZPL_EXIT(zfsvfs); 82 83 return (error); 84 } 85 86 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 87 static int 88 zpl_root_readdir(struct file *filp, void *dirent, filldir_t filldir) 89 { 90 zpl_dir_context_t ctx = 91 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 92 int error; 93 94 error = zpl_root_iterate(filp, &ctx); 95 filp->f_pos = ctx.pos; 96 97 return (error); 98 } 99 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 100 101 /* 102 * Get root directory attributes. 103 */ 104 static int 105 #ifdef HAVE_USERNS_IOPS_GETATTR 106 zpl_root_getattr_impl(struct user_namespace *user_ns, 107 const struct path *path, struct kstat *stat, u32 request_mask, 108 unsigned int query_flags) 109 #else 110 zpl_root_getattr_impl(const struct path *path, struct kstat *stat, 111 u32 request_mask, unsigned int query_flags) 112 #endif 113 { 114 (void) request_mask, (void) query_flags; 115 struct inode *ip = path->dentry->d_inode; 116 117 #ifdef HAVE_USERNS_IOPS_GETATTR 118 #ifdef HAVE_GENERIC_FILLATTR_USERNS 119 generic_fillattr(user_ns, ip, stat); 120 #else 121 (void) user_ns; 122 #endif 123 #else 124 generic_fillattr(ip, stat); 125 #endif 126 stat->atime = current_time(ip); 127 128 return (0); 129 } 130 ZPL_GETATTR_WRAPPER(zpl_root_getattr); 131 132 static struct dentry * 133 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags) 134 { 135 cred_t *cr = CRED(); 136 struct inode *ip; 137 int error; 138 139 crhold(cr); 140 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL); 141 ASSERT3S(error, <=, 0); 142 crfree(cr); 143 144 if (error) { 145 if (error == -ENOENT) 146 return (d_splice_alias(NULL, dentry)); 147 else 148 return (ERR_PTR(error)); 149 } 150 151 return (d_splice_alias(ip, dentry)); 152 } 153 154 /* 155 * The '.zfs' control directory file and inode operations. 156 */ 157 const struct file_operations zpl_fops_root = { 158 .open = zpl_common_open, 159 .llseek = generic_file_llseek, 160 .read = generic_read_dir, 161 #ifdef HAVE_VFS_ITERATE_SHARED 162 .iterate_shared = zpl_root_iterate, 163 #elif defined(HAVE_VFS_ITERATE) 164 .iterate = zpl_root_iterate, 165 #else 166 .readdir = zpl_root_readdir, 167 #endif 168 }; 169 170 const struct inode_operations zpl_ops_root = { 171 .lookup = zpl_root_lookup, 172 .getattr = zpl_root_getattr, 173 }; 174 175 static struct vfsmount * 176 zpl_snapdir_automount(struct path *path) 177 { 178 int error; 179 180 error = -zfsctl_snapshot_mount(path, 0); 181 if (error) 182 return (ERR_PTR(error)); 183 184 /* 185 * Rather than returning the new vfsmount for the snapshot we must 186 * return NULL to indicate a mount collision. This is done because 187 * the user space mount calls do_add_mount() which adds the vfsmount 188 * to the name space. If we returned the new mount here it would be 189 * added again to the vfsmount list resulting in list corruption. 190 */ 191 return (NULL); 192 } 193 194 /* 195 * Negative dentries must always be revalidated so newly created snapshots 196 * can be detected and automounted. Normal dentries should be kept because 197 * as of the 3.18 kernel revaliding the mountpoint dentry will result in 198 * the snapshot being immediately unmounted. 199 */ 200 static int 201 #ifdef HAVE_D_REVALIDATE_NAMEIDATA 202 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i) 203 #else 204 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags) 205 #endif 206 { 207 return (!!dentry->d_inode); 208 } 209 210 static const dentry_operations_t zpl_dops_snapdirs = { 211 /* 212 * Auto mounting of snapshots is only supported for 2.6.37 and 213 * newer kernels. Prior to this kernel the ops->follow_link() 214 * callback was used as a hack to trigger the mount. The 215 * resulting vfsmount was then explicitly grafted in to the 216 * name space. While it might be possible to add compatibility 217 * code to accomplish this it would require considerable care. 218 */ 219 .d_automount = zpl_snapdir_automount, 220 .d_revalidate = zpl_snapdir_revalidate, 221 }; 222 223 static struct dentry * 224 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry, 225 unsigned int flags) 226 { 227 fstrans_cookie_t cookie; 228 cred_t *cr = CRED(); 229 struct inode *ip = NULL; 230 int error; 231 232 crhold(cr); 233 cookie = spl_fstrans_mark(); 234 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip, 235 0, cr, NULL, NULL); 236 ASSERT3S(error, <=, 0); 237 spl_fstrans_unmark(cookie); 238 crfree(cr); 239 240 if (error && error != -ENOENT) 241 return (ERR_PTR(error)); 242 243 ASSERT(error == 0 || ip == NULL); 244 d_clear_d_op(dentry); 245 d_set_d_op(dentry, &zpl_dops_snapdirs); 246 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT; 247 248 return (d_splice_alias(ip, dentry)); 249 } 250 251 static int 252 zpl_snapdir_iterate(struct file *filp, zpl_dir_context_t *ctx) 253 { 254 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 255 fstrans_cookie_t cookie; 256 char snapname[MAXNAMELEN]; 257 boolean_t case_conflict; 258 uint64_t id, pos; 259 int error = 0; 260 261 ZPL_ENTER(zfsvfs); 262 cookie = spl_fstrans_mark(); 263 264 if (!zpl_dir_emit_dots(filp, ctx)) 265 goto out; 266 267 /* Start the position at 0 if it already emitted . and .. */ 268 pos = (ctx->pos == 2 ? 0 : ctx->pos); 269 while (error == 0) { 270 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 271 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, 272 snapname, &id, &pos, &case_conflict); 273 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 274 if (error) 275 goto out; 276 277 if (!zpl_dir_emit(ctx, snapname, strlen(snapname), 278 ZFSCTL_INO_SHARES - id, DT_DIR)) 279 goto out; 280 281 ctx->pos = pos; 282 } 283 out: 284 spl_fstrans_unmark(cookie); 285 ZPL_EXIT(zfsvfs); 286 287 if (error == -ENOENT) 288 return (0); 289 290 return (error); 291 } 292 293 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 294 static int 295 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir) 296 { 297 zpl_dir_context_t ctx = 298 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 299 int error; 300 301 error = zpl_snapdir_iterate(filp, &ctx); 302 filp->f_pos = ctx.pos; 303 304 return (error); 305 } 306 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 307 308 static int 309 #ifdef HAVE_IOPS_RENAME_USERNS 310 zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip, 311 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry, 312 unsigned int flags) 313 #else 314 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry, 315 struct inode *tdip, struct dentry *tdentry, unsigned int flags) 316 #endif 317 { 318 cred_t *cr = CRED(); 319 int error; 320 321 /* We probably don't want to support renameat2(2) in ctldir */ 322 if (flags) 323 return (-EINVAL); 324 325 crhold(cr); 326 error = -zfsctl_snapdir_rename(sdip, dname(sdentry), 327 tdip, dname(tdentry), cr, 0); 328 ASSERT3S(error, <=, 0); 329 crfree(cr); 330 331 return (error); 332 } 333 334 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS) 335 static int 336 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry, 337 struct inode *tdip, struct dentry *tdentry) 338 { 339 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0)); 340 } 341 #endif 342 343 static int 344 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry) 345 { 346 cred_t *cr = CRED(); 347 int error; 348 349 crhold(cr); 350 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0); 351 ASSERT3S(error, <=, 0); 352 crfree(cr); 353 354 return (error); 355 } 356 357 static int 358 #ifdef HAVE_IOPS_MKDIR_USERNS 359 zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip, 360 struct dentry *dentry, umode_t mode) 361 #else 362 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode) 363 #endif 364 { 365 cred_t *cr = CRED(); 366 vattr_t *vap; 367 struct inode *ip; 368 int error; 369 370 crhold(cr); 371 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 372 zpl_vap_init(vap, dip, mode | S_IFDIR, cr); 373 374 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0); 375 if (error == 0) { 376 d_clear_d_op(dentry); 377 d_set_d_op(dentry, &zpl_dops_snapdirs); 378 d_instantiate(dentry, ip); 379 } 380 381 kmem_free(vap, sizeof (vattr_t)); 382 ASSERT3S(error, <=, 0); 383 crfree(cr); 384 385 return (error); 386 } 387 388 /* 389 * Get snapshot directory attributes. 390 */ 391 static int 392 #ifdef HAVE_USERNS_IOPS_GETATTR 393 zpl_snapdir_getattr_impl(struct user_namespace *user_ns, 394 const struct path *path, struct kstat *stat, u32 request_mask, 395 unsigned int query_flags) 396 #else 397 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat, 398 u32 request_mask, unsigned int query_flags) 399 #endif 400 { 401 (void) request_mask, (void) query_flags; 402 struct inode *ip = path->dentry->d_inode; 403 zfsvfs_t *zfsvfs = ITOZSB(ip); 404 405 ZPL_ENTER(zfsvfs); 406 #ifdef HAVE_USERNS_IOPS_GETATTR 407 #ifdef HAVE_GENERIC_FILLATTR_USERNS 408 generic_fillattr(user_ns, ip, stat); 409 #else 410 (void) user_ns; 411 #endif 412 #else 413 generic_fillattr(ip, stat); 414 #endif 415 416 stat->nlink = stat->size = 2; 417 418 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os); 419 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) { 420 uint64_t snap_count; 421 int err = zap_count( 422 dmu_objset_pool(ds->ds_objset)->dp_meta_objset, 423 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count); 424 if (err != 0) { 425 ZPL_EXIT(zfsvfs); 426 return (-err); 427 } 428 stat->nlink += snap_count; 429 } 430 431 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 432 stat->atime = current_time(ip); 433 ZPL_EXIT(zfsvfs); 434 435 return (0); 436 } 437 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr); 438 439 /* 440 * The '.zfs/snapshot' directory file operations. These mainly control 441 * generating the list of available snapshots when doing an 'ls' in the 442 * directory. See zpl_snapdir_readdir(). 443 */ 444 const struct file_operations zpl_fops_snapdir = { 445 .open = zpl_common_open, 446 .llseek = generic_file_llseek, 447 .read = generic_read_dir, 448 #ifdef HAVE_VFS_ITERATE_SHARED 449 .iterate_shared = zpl_snapdir_iterate, 450 #elif defined(HAVE_VFS_ITERATE) 451 .iterate = zpl_snapdir_iterate, 452 #else 453 .readdir = zpl_snapdir_readdir, 454 #endif 455 456 }; 457 458 /* 459 * The '.zfs/snapshot' directory inode operations. These mainly control 460 * creating an inode for a snapshot directory and initializing the needed 461 * infrastructure to automount the snapshot. See zpl_snapdir_lookup(). 462 */ 463 const struct inode_operations zpl_ops_snapdir = { 464 .lookup = zpl_snapdir_lookup, 465 .getattr = zpl_snapdir_getattr, 466 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS) 467 .rename = zpl_snapdir_rename2, 468 #else 469 .rename = zpl_snapdir_rename, 470 #endif 471 .rmdir = zpl_snapdir_rmdir, 472 .mkdir = zpl_snapdir_mkdir, 473 }; 474 475 static struct dentry * 476 zpl_shares_lookup(struct inode *dip, struct dentry *dentry, 477 unsigned int flags) 478 { 479 fstrans_cookie_t cookie; 480 cred_t *cr = CRED(); 481 struct inode *ip = NULL; 482 int error; 483 484 crhold(cr); 485 cookie = spl_fstrans_mark(); 486 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip, 487 0, cr, NULL, NULL); 488 ASSERT3S(error, <=, 0); 489 spl_fstrans_unmark(cookie); 490 crfree(cr); 491 492 if (error) { 493 if (error == -ENOENT) 494 return (d_splice_alias(NULL, dentry)); 495 else 496 return (ERR_PTR(error)); 497 } 498 499 return (d_splice_alias(ip, dentry)); 500 } 501 502 static int 503 zpl_shares_iterate(struct file *filp, zpl_dir_context_t *ctx) 504 { 505 fstrans_cookie_t cookie; 506 cred_t *cr = CRED(); 507 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 508 znode_t *dzp; 509 int error = 0; 510 511 ZPL_ENTER(zfsvfs); 512 cookie = spl_fstrans_mark(); 513 514 if (zfsvfs->z_shares_dir == 0) { 515 zpl_dir_emit_dots(filp, ctx); 516 goto out; 517 } 518 519 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 520 if (error) 521 goto out; 522 523 crhold(cr); 524 error = -zfs_readdir(ZTOI(dzp), ctx, cr); 525 crfree(cr); 526 527 iput(ZTOI(dzp)); 528 out: 529 spl_fstrans_unmark(cookie); 530 ZPL_EXIT(zfsvfs); 531 ASSERT3S(error, <=, 0); 532 533 return (error); 534 } 535 536 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 537 static int 538 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir) 539 { 540 zpl_dir_context_t ctx = 541 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 542 int error; 543 544 error = zpl_shares_iterate(filp, &ctx); 545 filp->f_pos = ctx.pos; 546 547 return (error); 548 } 549 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 550 551 static int 552 #ifdef HAVE_USERNS_IOPS_GETATTR 553 zpl_shares_getattr_impl(struct user_namespace *user_ns, 554 const struct path *path, struct kstat *stat, u32 request_mask, 555 unsigned int query_flags) 556 #else 557 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat, 558 u32 request_mask, unsigned int query_flags) 559 #endif 560 { 561 (void) request_mask, (void) query_flags; 562 struct inode *ip = path->dentry->d_inode; 563 zfsvfs_t *zfsvfs = ITOZSB(ip); 564 znode_t *dzp; 565 int error; 566 567 ZPL_ENTER(zfsvfs); 568 569 if (zfsvfs->z_shares_dir == 0) { 570 #ifdef HAVE_USERNS_IOPS_GETATTR 571 #ifdef HAVE_GENERIC_FILLATTR_USERNS 572 generic_fillattr(user_ns, path->dentry->d_inode, stat); 573 #else 574 (void) user_ns; 575 #endif 576 #else 577 generic_fillattr(path->dentry->d_inode, stat); 578 #endif 579 stat->nlink = stat->size = 2; 580 stat->atime = current_time(ip); 581 ZPL_EXIT(zfsvfs); 582 return (0); 583 } 584 585 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 586 if (error == 0) { 587 #ifdef HAVE_USERNS_IOPS_GETATTR 588 #ifdef HAVE_GENERIC_FILLATTR_USERNS 589 error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat); 590 #else 591 (void) user_ns; 592 #endif 593 #else 594 error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat); 595 #endif 596 iput(ZTOI(dzp)); 597 } 598 599 ZPL_EXIT(zfsvfs); 600 ASSERT3S(error, <=, 0); 601 602 return (error); 603 } 604 ZPL_GETATTR_WRAPPER(zpl_shares_getattr); 605 606 /* 607 * The '.zfs/shares' directory file operations. 608 */ 609 const struct file_operations zpl_fops_shares = { 610 .open = zpl_common_open, 611 .llseek = generic_file_llseek, 612 .read = generic_read_dir, 613 #ifdef HAVE_VFS_ITERATE_SHARED 614 .iterate_shared = zpl_shares_iterate, 615 #elif defined(HAVE_VFS_ITERATE) 616 .iterate = zpl_shares_iterate, 617 #else 618 .readdir = zpl_shares_readdir, 619 #endif 620 621 }; 622 623 /* 624 * The '.zfs/shares' directory inode operations. 625 */ 626 const struct inode_operations zpl_ops_shares = { 627 .lookup = zpl_shares_lookup, 628 .getattr = zpl_shares_getattr, 629 }; 630