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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
25 * Rewritten for Linux by:
26 * Rohan Puri <rohan.puri15@gmail.com>
27 * Brian Behlendorf <behlendorf1@llnl.gov>
30 #include <sys/zfs_znode.h>
31 #include <sys/zfs_vfsops.h>
32 #include <sys/zfs_vnops.h>
33 #include <sys/zfs_ctldir.h>
36 #include <sys/dsl_dataset.h>
40 * Common open routine. Disallow any write access.
43 zpl_common_open(struct inode
*ip
, struct file
*filp
)
45 if (blk_mode_is_open_write(filp
->f_mode
))
48 return (generic_file_open(ip
, filp
));
52 * Get root directory contents.
55 zpl_root_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
57 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
60 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
63 if (!zpl_dir_emit_dots(filp
, ctx
))
67 if (!zpl_dir_emit(ctx
, ZFS_SNAPDIR_NAME
,
68 strlen(ZFS_SNAPDIR_NAME
), ZFSCTL_INO_SNAPDIR
, DT_DIR
))
75 if (!zpl_dir_emit(ctx
, ZFS_SHAREDIR_NAME
,
76 strlen(ZFS_SHAREDIR_NAME
), ZFSCTL_INO_SHARES
, DT_DIR
))
82 zpl_exit(zfsvfs
, FTAG
);
87 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
89 zpl_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
91 zpl_dir_context_t ctx
=
92 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
95 error
= zpl_root_iterate(filp
, &ctx
);
96 filp
->f_pos
= ctx
.pos
;
100 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
103 * Get root directory attributes.
106 #ifdef HAVE_IDMAP_IOPS_GETATTR
107 zpl_root_getattr_impl(struct mnt_idmap
*user_ns
,
108 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
109 unsigned int query_flags
)
110 #elif defined(HAVE_USERNS_IOPS_GETATTR)
111 zpl_root_getattr_impl(struct user_namespace
*user_ns
,
112 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
113 unsigned int query_flags
)
115 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
116 u32 request_mask
, unsigned int query_flags
)
119 (void) request_mask
, (void) query_flags
;
120 struct inode
*ip
= path
->dentry
->d_inode
;
122 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
123 #ifdef HAVE_GENERIC_FILLATTR_USERNS
124 generic_fillattr(user_ns
, ip
, stat
);
125 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
126 generic_fillattr(user_ns
, ip
, stat
);
127 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
128 generic_fillattr(user_ns
, request_mask
, ip
, stat
);
133 generic_fillattr(ip
, stat
);
135 stat
->atime
= current_time(ip
);
139 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
141 static struct dentry
*
142 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
149 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
150 ASSERT3S(error
, <=, 0);
154 if (error
== -ENOENT
)
155 return (d_splice_alias(NULL
, dentry
));
157 return (ERR_PTR(error
));
160 return (d_splice_alias(ip
, dentry
));
164 * The '.zfs' control directory file and inode operations.
166 const struct file_operations zpl_fops_root
= {
167 .open
= zpl_common_open
,
168 .llseek
= generic_file_llseek
,
169 .read
= generic_read_dir
,
170 #ifdef HAVE_VFS_ITERATE_SHARED
171 .iterate_shared
= zpl_root_iterate
,
172 #elif defined(HAVE_VFS_ITERATE)
173 .iterate
= zpl_root_iterate
,
175 .readdir
= zpl_root_readdir
,
179 const struct inode_operations zpl_ops_root
= {
180 .lookup
= zpl_root_lookup
,
181 .getattr
= zpl_root_getattr
,
184 static struct vfsmount
*
185 zpl_snapdir_automount(struct path
*path
)
189 error
= -zfsctl_snapshot_mount(path
, 0);
191 return (ERR_PTR(error
));
194 * Rather than returning the new vfsmount for the snapshot we must
195 * return NULL to indicate a mount collision. This is done because
196 * the user space mount calls do_add_mount() which adds the vfsmount
197 * to the name space. If we returned the new mount here it would be
198 * added again to the vfsmount list resulting in list corruption.
204 * Negative dentries must always be revalidated so newly created snapshots
205 * can be detected and automounted. Normal dentries should be kept because
206 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
207 * the snapshot being immediately unmounted.
210 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
211 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
213 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
216 return (!!dentry
->d_inode
);
219 static dentry_operations_t zpl_dops_snapdirs
= {
221 * Auto mounting of snapshots is only supported for 2.6.37 and
222 * newer kernels. Prior to this kernel the ops->follow_link()
223 * callback was used as a hack to trigger the mount. The
224 * resulting vfsmount was then explicitly grafted in to the
225 * name space. While it might be possible to add compatibility
226 * code to accomplish this it would require considerable care.
228 .d_automount
= zpl_snapdir_automount
,
229 .d_revalidate
= zpl_snapdir_revalidate
,
232 static struct dentry
*
233 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
236 fstrans_cookie_t cookie
;
238 struct inode
*ip
= NULL
;
242 cookie
= spl_fstrans_mark();
243 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
245 ASSERT3S(error
, <=, 0);
246 spl_fstrans_unmark(cookie
);
249 if (error
&& error
!= -ENOENT
)
250 return (ERR_PTR(error
));
252 ASSERT(error
== 0 || ip
== NULL
);
253 d_clear_d_op(dentry
);
254 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
255 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
257 return (d_splice_alias(ip
, dentry
));
261 zpl_snapdir_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
263 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
264 fstrans_cookie_t cookie
;
265 char snapname
[MAXNAMELEN
];
266 boolean_t case_conflict
;
270 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
272 cookie
= spl_fstrans_mark();
274 if (!zpl_dir_emit_dots(filp
, ctx
))
277 /* Start the position at 0 if it already emitted . and .. */
278 pos
= (ctx
->pos
== 2 ? 0 : ctx
->pos
);
280 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
281 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
282 snapname
, &id
, &pos
, &case_conflict
);
283 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
287 if (!zpl_dir_emit(ctx
, snapname
, strlen(snapname
),
288 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
294 spl_fstrans_unmark(cookie
);
295 zpl_exit(zfsvfs
, FTAG
);
297 if (error
== -ENOENT
)
303 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
305 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
307 zpl_dir_context_t ctx
=
308 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
311 error
= zpl_snapdir_iterate(filp
, &ctx
);
312 filp
->f_pos
= ctx
.pos
;
316 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
319 #ifdef HAVE_IOPS_RENAME_USERNS
320 zpl_snapdir_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
321 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
323 #elif defined(HAVE_IOPS_RENAME_IDMAP)
324 zpl_snapdir_rename2(struct mnt_idmap
*user_ns
, struct inode
*sdip
,
325 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
328 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
329 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
335 /* We probably don't want to support renameat2(2) in ctldir */
340 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
341 tdip
, dname(tdentry
), cr
, 0);
342 ASSERT3S(error
, <=, 0);
348 #if (!defined(HAVE_RENAME_WANTS_FLAGS) && \
349 !defined(HAVE_IOPS_RENAME_USERNS) && \
350 !defined(HAVE_IOPS_RENAME_IDMAP))
352 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
353 struct inode
*tdip
, struct dentry
*tdentry
)
355 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
360 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
366 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
367 ASSERT3S(error
, <=, 0);
374 #ifdef HAVE_IOPS_MKDIR_USERNS
375 zpl_snapdir_mkdir(struct user_namespace
*user_ns
, struct inode
*dip
,
376 struct dentry
*dentry
, umode_t mode
)
377 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
378 zpl_snapdir_mkdir(struct mnt_idmap
*user_ns
, struct inode
*dip
,
379 struct dentry
*dentry
, umode_t mode
)
381 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
390 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
391 #if (defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
392 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
, user_ns
);
394 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
, zfs_init_idmap
);
397 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
399 d_clear_d_op(dentry
);
400 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
401 d_instantiate(dentry
, ip
);
404 kmem_free(vap
, sizeof (vattr_t
));
405 ASSERT3S(error
, <=, 0);
412 * Get snapshot directory attributes.
415 #ifdef HAVE_IDMAP_IOPS_GETATTR
416 zpl_snapdir_getattr_impl(struct mnt_idmap
*user_ns
,
417 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
418 unsigned int query_flags
)
419 #elif defined(HAVE_USERNS_IOPS_GETATTR)
420 zpl_snapdir_getattr_impl(struct user_namespace
*user_ns
,
421 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
422 unsigned int query_flags
)
424 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
425 u32 request_mask
, unsigned int query_flags
)
428 (void) request_mask
, (void) query_flags
;
429 struct inode
*ip
= path
->dentry
->d_inode
;
430 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
433 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
435 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
436 #ifdef HAVE_GENERIC_FILLATTR_USERNS
437 generic_fillattr(user_ns
, ip
, stat
);
438 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
439 generic_fillattr(user_ns
, ip
, stat
);
440 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
441 generic_fillattr(user_ns
, request_mask
, ip
, stat
);
446 generic_fillattr(ip
, stat
);
449 stat
->nlink
= stat
->size
= 2;
451 dsl_dataset_t
*ds
= dmu_objset_ds(zfsvfs
->z_os
);
452 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0) {
455 dmu_objset_pool(ds
->ds_objset
)->dp_meta_objset
,
456 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, &snap_count
);
458 zpl_exit(zfsvfs
, FTAG
);
461 stat
->nlink
+= snap_count
;
464 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
465 stat
->atime
= current_time(ip
);
466 zpl_exit(zfsvfs
, FTAG
);
470 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
473 * The '.zfs/snapshot' directory file operations. These mainly control
474 * generating the list of available snapshots when doing an 'ls' in the
475 * directory. See zpl_snapdir_readdir().
477 const struct file_operations zpl_fops_snapdir
= {
478 .open
= zpl_common_open
,
479 .llseek
= generic_file_llseek
,
480 .read
= generic_read_dir
,
481 #ifdef HAVE_VFS_ITERATE_SHARED
482 .iterate_shared
= zpl_snapdir_iterate
,
483 #elif defined(HAVE_VFS_ITERATE)
484 .iterate
= zpl_snapdir_iterate
,
486 .readdir
= zpl_snapdir_readdir
,
492 * The '.zfs/snapshot' directory inode operations. These mainly control
493 * creating an inode for a snapshot directory and initializing the needed
494 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
496 const struct inode_operations zpl_ops_snapdir
= {
497 .lookup
= zpl_snapdir_lookup
,
498 .getattr
= zpl_snapdir_getattr
,
499 #if (defined(HAVE_RENAME_WANTS_FLAGS) || \
500 defined(HAVE_IOPS_RENAME_USERNS) || \
501 defined(HAVE_IOPS_RENAME_IDMAP))
502 .rename
= zpl_snapdir_rename2
,
504 .rename
= zpl_snapdir_rename
,
506 .rmdir
= zpl_snapdir_rmdir
,
507 .mkdir
= zpl_snapdir_mkdir
,
510 static struct dentry
*
511 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
514 fstrans_cookie_t cookie
;
516 struct inode
*ip
= NULL
;
520 cookie
= spl_fstrans_mark();
521 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
523 ASSERT3S(error
, <=, 0);
524 spl_fstrans_unmark(cookie
);
528 if (error
== -ENOENT
)
529 return (d_splice_alias(NULL
, dentry
));
531 return (ERR_PTR(error
));
534 return (d_splice_alias(ip
, dentry
));
538 zpl_shares_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
540 fstrans_cookie_t cookie
;
542 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
546 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
548 cookie
= spl_fstrans_mark();
550 if (zfsvfs
->z_shares_dir
== 0) {
551 zpl_dir_emit_dots(filp
, ctx
);
555 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
560 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
565 spl_fstrans_unmark(cookie
);
566 zpl_exit(zfsvfs
, FTAG
);
567 ASSERT3S(error
, <=, 0);
572 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
574 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
576 zpl_dir_context_t ctx
=
577 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
580 error
= zpl_shares_iterate(filp
, &ctx
);
581 filp
->f_pos
= ctx
.pos
;
585 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
588 #ifdef HAVE_USERNS_IOPS_GETATTR
589 zpl_shares_getattr_impl(struct user_namespace
*user_ns
,
590 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
591 unsigned int query_flags
)
592 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
593 zpl_shares_getattr_impl(struct mnt_idmap
*user_ns
,
594 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
595 unsigned int query_flags
)
597 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
598 u32 request_mask
, unsigned int query_flags
)
601 (void) request_mask
, (void) query_flags
;
602 struct inode
*ip
= path
->dentry
->d_inode
;
603 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
607 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
610 if (zfsvfs
->z_shares_dir
== 0) {
611 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
612 #ifdef HAVE_GENERIC_FILLATTR_USERNS
613 generic_fillattr(user_ns
, path
->dentry
->d_inode
, stat
);
614 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
615 generic_fillattr(user_ns
, path
->dentry
->d_inode
, stat
);
616 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
617 generic_fillattr(user_ns
, request_mask
, ip
, stat
);
622 generic_fillattr(path
->dentry
->d_inode
, stat
);
624 stat
->nlink
= stat
->size
= 2;
625 stat
->atime
= current_time(ip
);
626 zpl_exit(zfsvfs
, FTAG
);
630 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
632 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
633 error
= -zfs_getattr_fast(user_ns
, request_mask
, ZTOI(dzp
),
635 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
636 error
= -zfs_getattr_fast(user_ns
, ZTOI(dzp
), stat
);
638 error
= -zfs_getattr_fast(kcred
->user_ns
, ZTOI(dzp
), stat
);
643 zpl_exit(zfsvfs
, FTAG
);
644 ASSERT3S(error
, <=, 0);
648 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
651 * The '.zfs/shares' directory file operations.
653 const struct file_operations zpl_fops_shares
= {
654 .open
= zpl_common_open
,
655 .llseek
= generic_file_llseek
,
656 .read
= generic_read_dir
,
657 #ifdef HAVE_VFS_ITERATE_SHARED
658 .iterate_shared
= zpl_shares_iterate
,
659 #elif defined(HAVE_VFS_ITERATE)
660 .iterate
= zpl_shares_iterate
,
662 .readdir
= zpl_shares_readdir
,
668 * The '.zfs/shares' directory inode operations.
670 const struct inode_operations zpl_ops_shares
= {
671 .lookup
= zpl_shares_lookup
,
672 .getattr
= zpl_shares_getattr
,