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 36 /* 37 * Common open routine. Disallow any write access. 38 */ 39 /* ARGSUSED */ 40 static int 41 zpl_common_open(struct inode *ip, struct file *filp) 42 { 43 if (filp->f_mode & FMODE_WRITE) 44 return (-EACCES); 45 46 return (generic_file_open(ip, filp)); 47 } 48 49 /* 50 * Get root directory contents. 51 */ 52 static int 53 zpl_root_iterate(struct file *filp, zpl_dir_context_t *ctx) 54 { 55 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 56 int error = 0; 57 58 ZPL_ENTER(zfsvfs); 59 60 if (!zpl_dir_emit_dots(filp, ctx)) 61 goto out; 62 63 if (ctx->pos == 2) { 64 if (!zpl_dir_emit(ctx, ZFS_SNAPDIR_NAME, 65 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR)) 66 goto out; 67 68 ctx->pos++; 69 } 70 71 if (ctx->pos == 3) { 72 if (!zpl_dir_emit(ctx, ZFS_SHAREDIR_NAME, 73 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR)) 74 goto out; 75 76 ctx->pos++; 77 } 78 out: 79 ZPL_EXIT(zfsvfs); 80 81 return (error); 82 } 83 84 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 85 static int 86 zpl_root_readdir(struct file *filp, void *dirent, filldir_t filldir) 87 { 88 zpl_dir_context_t ctx = 89 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 90 int error; 91 92 error = zpl_root_iterate(filp, &ctx); 93 filp->f_pos = ctx.pos; 94 95 return (error); 96 } 97 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 98 99 /* 100 * Get root directory attributes. 101 */ 102 /* ARGSUSED */ 103 static int 104 zpl_root_getattr_impl(const struct path *path, struct kstat *stat, 105 u32 request_mask, unsigned int query_flags) 106 { 107 struct inode *ip = path->dentry->d_inode; 108 109 generic_fillattr(ip, stat); 110 stat->atime = current_time(ip); 111 112 return (0); 113 } 114 ZPL_GETATTR_WRAPPER(zpl_root_getattr); 115 116 static struct dentry * 117 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags) 118 { 119 cred_t *cr = CRED(); 120 struct inode *ip; 121 int error; 122 123 crhold(cr); 124 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL); 125 ASSERT3S(error, <=, 0); 126 crfree(cr); 127 128 if (error) { 129 if (error == -ENOENT) 130 return (d_splice_alias(NULL, dentry)); 131 else 132 return (ERR_PTR(error)); 133 } 134 135 return (d_splice_alias(ip, dentry)); 136 } 137 138 /* 139 * The '.zfs' control directory file and inode operations. 140 */ 141 const struct file_operations zpl_fops_root = { 142 .open = zpl_common_open, 143 .llseek = generic_file_llseek, 144 .read = generic_read_dir, 145 #ifdef HAVE_VFS_ITERATE_SHARED 146 .iterate_shared = zpl_root_iterate, 147 #elif defined(HAVE_VFS_ITERATE) 148 .iterate = zpl_root_iterate, 149 #else 150 .readdir = zpl_root_readdir, 151 #endif 152 }; 153 154 const struct inode_operations zpl_ops_root = { 155 .lookup = zpl_root_lookup, 156 .getattr = zpl_root_getattr, 157 }; 158 159 static struct vfsmount * 160 zpl_snapdir_automount(struct path *path) 161 { 162 int error; 163 164 error = -zfsctl_snapshot_mount(path, 0); 165 if (error) 166 return (ERR_PTR(error)); 167 168 /* 169 * Rather than returning the new vfsmount for the snapshot we must 170 * return NULL to indicate a mount collision. This is done because 171 * the user space mount calls do_add_mount() which adds the vfsmount 172 * to the name space. If we returned the new mount here it would be 173 * added again to the vfsmount list resulting in list corruption. 174 */ 175 return (NULL); 176 } 177 178 /* 179 * Negative dentries must always be revalidated so newly created snapshots 180 * can be detected and automounted. Normal dentries should be kept because 181 * as of the 3.18 kernel revaliding the mountpoint dentry will result in 182 * the snapshot being immediately unmounted. 183 */ 184 static int 185 #ifdef HAVE_D_REVALIDATE_NAMEIDATA 186 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i) 187 #else 188 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags) 189 #endif 190 { 191 return (!!dentry->d_inode); 192 } 193 194 dentry_operations_t zpl_dops_snapdirs = { 195 /* 196 * Auto mounting of snapshots is only supported for 2.6.37 and 197 * newer kernels. Prior to this kernel the ops->follow_link() 198 * callback was used as a hack to trigger the mount. The 199 * resulting vfsmount was then explicitly grafted in to the 200 * name space. While it might be possible to add compatibility 201 * code to accomplish this it would require considerable care. 202 */ 203 .d_automount = zpl_snapdir_automount, 204 .d_revalidate = zpl_snapdir_revalidate, 205 }; 206 207 static struct dentry * 208 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry, 209 unsigned int flags) 210 { 211 fstrans_cookie_t cookie; 212 cred_t *cr = CRED(); 213 struct inode *ip = NULL; 214 int error; 215 216 crhold(cr); 217 cookie = spl_fstrans_mark(); 218 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip, 219 0, cr, NULL, NULL); 220 ASSERT3S(error, <=, 0); 221 spl_fstrans_unmark(cookie); 222 crfree(cr); 223 224 if (error && error != -ENOENT) 225 return (ERR_PTR(error)); 226 227 ASSERT(error == 0 || ip == NULL); 228 d_clear_d_op(dentry); 229 d_set_d_op(dentry, &zpl_dops_snapdirs); 230 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT; 231 232 return (d_splice_alias(ip, dentry)); 233 } 234 235 static int 236 zpl_snapdir_iterate(struct file *filp, zpl_dir_context_t *ctx) 237 { 238 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 239 fstrans_cookie_t cookie; 240 char snapname[MAXNAMELEN]; 241 boolean_t case_conflict; 242 uint64_t id, pos; 243 int error = 0; 244 245 ZPL_ENTER(zfsvfs); 246 cookie = spl_fstrans_mark(); 247 248 if (!zpl_dir_emit_dots(filp, ctx)) 249 goto out; 250 251 /* Start the position at 0 if it already emitted . and .. */ 252 pos = (ctx->pos == 2 ? 0 : ctx->pos); 253 while (error == 0) { 254 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 255 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, 256 snapname, &id, &pos, &case_conflict); 257 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 258 if (error) 259 goto out; 260 261 if (!zpl_dir_emit(ctx, snapname, strlen(snapname), 262 ZFSCTL_INO_SHARES - id, DT_DIR)) 263 goto out; 264 265 ctx->pos = pos; 266 } 267 out: 268 spl_fstrans_unmark(cookie); 269 ZPL_EXIT(zfsvfs); 270 271 if (error == -ENOENT) 272 return (0); 273 274 return (error); 275 } 276 277 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 278 static int 279 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir) 280 { 281 zpl_dir_context_t ctx = 282 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 283 int error; 284 285 error = zpl_snapdir_iterate(filp, &ctx); 286 filp->f_pos = ctx.pos; 287 288 return (error); 289 } 290 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 291 292 static int 293 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry, 294 struct inode *tdip, struct dentry *tdentry, unsigned int flags) 295 { 296 cred_t *cr = CRED(); 297 int error; 298 299 /* We probably don't want to support renameat2(2) in ctldir */ 300 if (flags) 301 return (-EINVAL); 302 303 crhold(cr); 304 error = -zfsctl_snapdir_rename(sdip, dname(sdentry), 305 tdip, dname(tdentry), cr, 0); 306 ASSERT3S(error, <=, 0); 307 crfree(cr); 308 309 return (error); 310 } 311 312 #ifndef HAVE_RENAME_WANTS_FLAGS 313 static int 314 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry, 315 struct inode *tdip, struct dentry *tdentry) 316 { 317 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0)); 318 } 319 #endif 320 321 static int 322 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry) 323 { 324 cred_t *cr = CRED(); 325 int error; 326 327 crhold(cr); 328 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0); 329 ASSERT3S(error, <=, 0); 330 crfree(cr); 331 332 return (error); 333 } 334 335 static int 336 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode) 337 { 338 cred_t *cr = CRED(); 339 vattr_t *vap; 340 struct inode *ip; 341 int error; 342 343 crhold(cr); 344 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 345 zpl_vap_init(vap, dip, mode | S_IFDIR, cr); 346 347 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0); 348 if (error == 0) { 349 d_clear_d_op(dentry); 350 d_set_d_op(dentry, &zpl_dops_snapdirs); 351 d_instantiate(dentry, ip); 352 } 353 354 kmem_free(vap, sizeof (vattr_t)); 355 ASSERT3S(error, <=, 0); 356 crfree(cr); 357 358 return (error); 359 } 360 361 /* 362 * Get snapshot directory attributes. 363 */ 364 /* ARGSUSED */ 365 static int 366 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat, 367 u32 request_mask, unsigned int query_flags) 368 { 369 struct inode *ip = path->dentry->d_inode; 370 zfsvfs_t *zfsvfs = ITOZSB(ip); 371 372 ZPL_ENTER(zfsvfs); 373 generic_fillattr(ip, stat); 374 375 stat->nlink = stat->size = 2; 376 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 377 stat->atime = current_time(ip); 378 ZPL_EXIT(zfsvfs); 379 380 return (0); 381 } 382 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr); 383 384 /* 385 * The '.zfs/snapshot' directory file operations. These mainly control 386 * generating the list of available snapshots when doing an 'ls' in the 387 * directory. See zpl_snapdir_readdir(). 388 */ 389 const struct file_operations zpl_fops_snapdir = { 390 .open = zpl_common_open, 391 .llseek = generic_file_llseek, 392 .read = generic_read_dir, 393 #ifdef HAVE_VFS_ITERATE_SHARED 394 .iterate_shared = zpl_snapdir_iterate, 395 #elif defined(HAVE_VFS_ITERATE) 396 .iterate = zpl_snapdir_iterate, 397 #else 398 .readdir = zpl_snapdir_readdir, 399 #endif 400 401 }; 402 403 /* 404 * The '.zfs/snapshot' directory inode operations. These mainly control 405 * creating an inode for a snapshot directory and initializing the needed 406 * infrastructure to automount the snapshot. See zpl_snapdir_lookup(). 407 */ 408 const struct inode_operations zpl_ops_snapdir = { 409 .lookup = zpl_snapdir_lookup, 410 .getattr = zpl_snapdir_getattr, 411 #ifdef HAVE_RENAME_WANTS_FLAGS 412 .rename = zpl_snapdir_rename2, 413 #else 414 .rename = zpl_snapdir_rename, 415 #endif 416 .rmdir = zpl_snapdir_rmdir, 417 .mkdir = zpl_snapdir_mkdir, 418 }; 419 420 static struct dentry * 421 zpl_shares_lookup(struct inode *dip, struct dentry *dentry, 422 unsigned int flags) 423 { 424 fstrans_cookie_t cookie; 425 cred_t *cr = CRED(); 426 struct inode *ip = NULL; 427 int error; 428 429 crhold(cr); 430 cookie = spl_fstrans_mark(); 431 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip, 432 0, cr, NULL, NULL); 433 ASSERT3S(error, <=, 0); 434 spl_fstrans_unmark(cookie); 435 crfree(cr); 436 437 if (error) { 438 if (error == -ENOENT) 439 return (d_splice_alias(NULL, dentry)); 440 else 441 return (ERR_PTR(error)); 442 } 443 444 return (d_splice_alias(ip, dentry)); 445 } 446 447 static int 448 zpl_shares_iterate(struct file *filp, zpl_dir_context_t *ctx) 449 { 450 fstrans_cookie_t cookie; 451 cred_t *cr = CRED(); 452 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 453 znode_t *dzp; 454 int error = 0; 455 456 ZPL_ENTER(zfsvfs); 457 cookie = spl_fstrans_mark(); 458 459 if (zfsvfs->z_shares_dir == 0) { 460 zpl_dir_emit_dots(filp, ctx); 461 goto out; 462 } 463 464 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 465 if (error) 466 goto out; 467 468 crhold(cr); 469 error = -zfs_readdir(ZTOI(dzp), ctx, cr); 470 crfree(cr); 471 472 iput(ZTOI(dzp)); 473 out: 474 spl_fstrans_unmark(cookie); 475 ZPL_EXIT(zfsvfs); 476 ASSERT3S(error, <=, 0); 477 478 return (error); 479 } 480 481 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 482 static int 483 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir) 484 { 485 zpl_dir_context_t ctx = 486 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 487 int error; 488 489 error = zpl_shares_iterate(filp, &ctx); 490 filp->f_pos = ctx.pos; 491 492 return (error); 493 } 494 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 495 496 /* ARGSUSED */ 497 static int 498 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat, 499 u32 request_mask, unsigned int query_flags) 500 { 501 struct inode *ip = path->dentry->d_inode; 502 zfsvfs_t *zfsvfs = ITOZSB(ip); 503 znode_t *dzp; 504 int error; 505 506 ZPL_ENTER(zfsvfs); 507 508 if (zfsvfs->z_shares_dir == 0) { 509 generic_fillattr(path->dentry->d_inode, stat); 510 stat->nlink = stat->size = 2; 511 stat->atime = current_time(ip); 512 ZPL_EXIT(zfsvfs); 513 return (0); 514 } 515 516 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 517 if (error == 0) { 518 error = -zfs_getattr_fast(ZTOI(dzp), stat); 519 iput(ZTOI(dzp)); 520 } 521 522 ZPL_EXIT(zfsvfs); 523 ASSERT3S(error, <=, 0); 524 525 return (error); 526 } 527 ZPL_GETATTR_WRAPPER(zpl_shares_getattr); 528 529 /* 530 * The '.zfs/shares' directory file operations. 531 */ 532 const struct file_operations zpl_fops_shares = { 533 .open = zpl_common_open, 534 .llseek = generic_file_llseek, 535 .read = generic_read_dir, 536 #ifdef HAVE_VFS_ITERATE_SHARED 537 .iterate_shared = zpl_shares_iterate, 538 #elif defined(HAVE_VFS_ITERATE) 539 .iterate = zpl_shares_iterate, 540 #else 541 .readdir = zpl_shares_readdir, 542 #endif 543 544 }; 545 546 /* 547 * The '.zfs/shares' directory inode operations. 548 */ 549 const struct inode_operations zpl_ops_shares = { 550 .lookup = zpl_shares_lookup, 551 .getattr = zpl_shares_getattr, 552 }; 553