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 ZFS_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 ZFS_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 ZFS_ENTER(zfsvfs); 246 cookie = spl_fstrans_mark(); 247 248 if (!zpl_dir_emit_dots(filp, ctx)) 249 goto out; 250 251 pos = ctx->pos; 252 while (error == 0) { 253 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 254 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, 255 snapname, &id, &pos, &case_conflict); 256 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 257 if (error) 258 goto out; 259 260 if (!zpl_dir_emit(ctx, snapname, strlen(snapname), 261 ZFSCTL_INO_SHARES - id, DT_DIR)) 262 goto out; 263 264 ctx->pos = pos; 265 } 266 out: 267 spl_fstrans_unmark(cookie); 268 ZFS_EXIT(zfsvfs); 269 270 if (error == -ENOENT) 271 return (0); 272 273 return (error); 274 } 275 276 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 277 static int 278 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir) 279 { 280 zpl_dir_context_t ctx = 281 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 282 int error; 283 284 error = zpl_snapdir_iterate(filp, &ctx); 285 filp->f_pos = ctx.pos; 286 287 return (error); 288 } 289 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 290 291 static int 292 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry, 293 struct inode *tdip, struct dentry *tdentry, unsigned int flags) 294 { 295 cred_t *cr = CRED(); 296 int error; 297 298 /* We probably don't want to support renameat2(2) in ctldir */ 299 if (flags) 300 return (-EINVAL); 301 302 crhold(cr); 303 error = -zfsctl_snapdir_rename(sdip, dname(sdentry), 304 tdip, dname(tdentry), cr, 0); 305 ASSERT3S(error, <=, 0); 306 crfree(cr); 307 308 return (error); 309 } 310 311 #ifndef HAVE_RENAME_WANTS_FLAGS 312 static int 313 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry, 314 struct inode *tdip, struct dentry *tdentry) 315 { 316 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0)); 317 } 318 #endif 319 320 static int 321 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry) 322 { 323 cred_t *cr = CRED(); 324 int error; 325 326 crhold(cr); 327 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0); 328 ASSERT3S(error, <=, 0); 329 crfree(cr); 330 331 return (error); 332 } 333 334 static int 335 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode) 336 { 337 cred_t *cr = CRED(); 338 vattr_t *vap; 339 struct inode *ip; 340 int error; 341 342 crhold(cr); 343 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); 344 zpl_vap_init(vap, dip, mode | S_IFDIR, cr); 345 346 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0); 347 if (error == 0) { 348 d_clear_d_op(dentry); 349 d_set_d_op(dentry, &zpl_dops_snapdirs); 350 d_instantiate(dentry, ip); 351 } 352 353 kmem_free(vap, sizeof (vattr_t)); 354 ASSERT3S(error, <=, 0); 355 crfree(cr); 356 357 return (error); 358 } 359 360 /* 361 * Get snapshot directory attributes. 362 */ 363 /* ARGSUSED */ 364 static int 365 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat, 366 u32 request_mask, unsigned int query_flags) 367 { 368 struct inode *ip = path->dentry->d_inode; 369 zfsvfs_t *zfsvfs = ITOZSB(ip); 370 371 ZFS_ENTER(zfsvfs); 372 generic_fillattr(ip, stat); 373 374 stat->nlink = stat->size = 2; 375 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 376 stat->atime = current_time(ip); 377 ZFS_EXIT(zfsvfs); 378 379 return (0); 380 } 381 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr); 382 383 /* 384 * The '.zfs/snapshot' directory file operations. These mainly control 385 * generating the list of available snapshots when doing an 'ls' in the 386 * directory. See zpl_snapdir_readdir(). 387 */ 388 const struct file_operations zpl_fops_snapdir = { 389 .open = zpl_common_open, 390 .llseek = generic_file_llseek, 391 .read = generic_read_dir, 392 #ifdef HAVE_VFS_ITERATE_SHARED 393 .iterate_shared = zpl_snapdir_iterate, 394 #elif defined(HAVE_VFS_ITERATE) 395 .iterate = zpl_snapdir_iterate, 396 #else 397 .readdir = zpl_snapdir_readdir, 398 #endif 399 400 }; 401 402 /* 403 * The '.zfs/snapshot' directory inode operations. These mainly control 404 * creating an inode for a snapshot directory and initializing the needed 405 * infrastructure to automount the snapshot. See zpl_snapdir_lookup(). 406 */ 407 const struct inode_operations zpl_ops_snapdir = { 408 .lookup = zpl_snapdir_lookup, 409 .getattr = zpl_snapdir_getattr, 410 #ifdef HAVE_RENAME_WANTS_FLAGS 411 .rename = zpl_snapdir_rename2, 412 #else 413 .rename = zpl_snapdir_rename, 414 #endif 415 .rmdir = zpl_snapdir_rmdir, 416 .mkdir = zpl_snapdir_mkdir, 417 }; 418 419 static struct dentry * 420 zpl_shares_lookup(struct inode *dip, struct dentry *dentry, 421 unsigned int flags) 422 { 423 fstrans_cookie_t cookie; 424 cred_t *cr = CRED(); 425 struct inode *ip = NULL; 426 int error; 427 428 crhold(cr); 429 cookie = spl_fstrans_mark(); 430 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip, 431 0, cr, NULL, NULL); 432 ASSERT3S(error, <=, 0); 433 spl_fstrans_unmark(cookie); 434 crfree(cr); 435 436 if (error) { 437 if (error == -ENOENT) 438 return (d_splice_alias(NULL, dentry)); 439 else 440 return (ERR_PTR(error)); 441 } 442 443 return (d_splice_alias(ip, dentry)); 444 } 445 446 static int 447 zpl_shares_iterate(struct file *filp, zpl_dir_context_t *ctx) 448 { 449 fstrans_cookie_t cookie; 450 cred_t *cr = CRED(); 451 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp)); 452 znode_t *dzp; 453 int error = 0; 454 455 ZFS_ENTER(zfsvfs); 456 cookie = spl_fstrans_mark(); 457 458 if (zfsvfs->z_shares_dir == 0) { 459 zpl_dir_emit_dots(filp, ctx); 460 goto out; 461 } 462 463 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 464 if (error) 465 goto out; 466 467 crhold(cr); 468 error = -zfs_readdir(ZTOI(dzp), ctx, cr); 469 crfree(cr); 470 471 iput(ZTOI(dzp)); 472 out: 473 spl_fstrans_unmark(cookie); 474 ZFS_EXIT(zfsvfs); 475 ASSERT3S(error, <=, 0); 476 477 return (error); 478 } 479 480 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) 481 static int 482 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir) 483 { 484 zpl_dir_context_t ctx = 485 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); 486 int error; 487 488 error = zpl_shares_iterate(filp, &ctx); 489 filp->f_pos = ctx.pos; 490 491 return (error); 492 } 493 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ 494 495 /* ARGSUSED */ 496 static int 497 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat, 498 u32 request_mask, unsigned int query_flags) 499 { 500 struct inode *ip = path->dentry->d_inode; 501 zfsvfs_t *zfsvfs = ITOZSB(ip); 502 znode_t *dzp; 503 int error; 504 505 ZFS_ENTER(zfsvfs); 506 507 if (zfsvfs->z_shares_dir == 0) { 508 generic_fillattr(path->dentry->d_inode, stat); 509 stat->nlink = stat->size = 2; 510 stat->atime = current_time(ip); 511 ZFS_EXIT(zfsvfs); 512 return (0); 513 } 514 515 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp); 516 if (error == 0) { 517 error = -zfs_getattr_fast(ZTOI(dzp), stat); 518 iput(ZTOI(dzp)); 519 } 520 521 ZFS_EXIT(zfsvfs); 522 ASSERT3S(error, <=, 0); 523 524 return (error); 525 } 526 ZPL_GETATTR_WRAPPER(zpl_shares_getattr); 527 528 /* 529 * The '.zfs/shares' directory file operations. 530 */ 531 const struct file_operations zpl_fops_shares = { 532 .open = zpl_common_open, 533 .llseek = generic_file_llseek, 534 .read = generic_read_dir, 535 #ifdef HAVE_VFS_ITERATE_SHARED 536 .iterate_shared = zpl_shares_iterate, 537 #elif defined(HAVE_VFS_ITERATE) 538 .iterate = zpl_shares_iterate, 539 #else 540 .readdir = zpl_shares_readdir, 541 #endif 542 543 }; 544 545 /* 546 * The '.zfs/shares' directory inode operations. 547 */ 548 const struct inode_operations zpl_ops_shares = { 549 .lookup = zpl_shares_lookup, 550 .getattr = zpl_shares_getattr, 551 }; 552