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 (filp
->f_mode
& FMODE_WRITE
)
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_USERNS_IOPS_GETATTR
107 zpl_root_getattr_impl(struct user_namespace
*user_ns
,
108 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
109 unsigned int query_flags
)
111 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
112 u32 request_mask
, unsigned int query_flags
)
115 (void) request_mask
, (void) query_flags
;
116 struct inode
*ip
= path
->dentry
->d_inode
;
118 #ifdef HAVE_USERNS_IOPS_GETATTR
119 #ifdef HAVE_GENERIC_FILLATTR_USERNS
120 generic_fillattr(user_ns
, ip
, stat
);
125 generic_fillattr(ip
, stat
);
127 stat
->atime
= current_time(ip
);
131 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
133 static struct dentry
*
134 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
141 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
142 ASSERT3S(error
, <=, 0);
146 if (error
== -ENOENT
)
147 return (d_splice_alias(NULL
, dentry
));
149 return (ERR_PTR(error
));
152 return (d_splice_alias(ip
, dentry
));
156 * The '.zfs' control directory file and inode operations.
158 const struct file_operations zpl_fops_root
= {
159 .open
= zpl_common_open
,
160 .llseek
= generic_file_llseek
,
161 .read
= generic_read_dir
,
162 #ifdef HAVE_VFS_ITERATE_SHARED
163 .iterate_shared
= zpl_root_iterate
,
164 #elif defined(HAVE_VFS_ITERATE)
165 .iterate
= zpl_root_iterate
,
167 .readdir
= zpl_root_readdir
,
171 const struct inode_operations zpl_ops_root
= {
172 .lookup
= zpl_root_lookup
,
173 .getattr
= zpl_root_getattr
,
176 static struct vfsmount
*
177 zpl_snapdir_automount(struct path
*path
)
181 error
= -zfsctl_snapshot_mount(path
, 0);
183 return (ERR_PTR(error
));
186 * Rather than returning the new vfsmount for the snapshot we must
187 * return NULL to indicate a mount collision. This is done because
188 * the user space mount calls do_add_mount() which adds the vfsmount
189 * to the name space. If we returned the new mount here it would be
190 * added again to the vfsmount list resulting in list corruption.
196 * Negative dentries must always be revalidated so newly created snapshots
197 * can be detected and automounted. Normal dentries should be kept because
198 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
199 * the snapshot being immediately unmounted.
202 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
203 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
205 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
208 return (!!dentry
->d_inode
);
211 static dentry_operations_t zpl_dops_snapdirs
= {
213 * Auto mounting of snapshots is only supported for 2.6.37 and
214 * newer kernels. Prior to this kernel the ops->follow_link()
215 * callback was used as a hack to trigger the mount. The
216 * resulting vfsmount was then explicitly grafted in to the
217 * name space. While it might be possible to add compatibility
218 * code to accomplish this it would require considerable care.
220 .d_automount
= zpl_snapdir_automount
,
221 .d_revalidate
= zpl_snapdir_revalidate
,
224 static struct dentry
*
225 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
228 fstrans_cookie_t cookie
;
230 struct inode
*ip
= NULL
;
234 cookie
= spl_fstrans_mark();
235 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
237 ASSERT3S(error
, <=, 0);
238 spl_fstrans_unmark(cookie
);
241 if (error
&& error
!= -ENOENT
)
242 return (ERR_PTR(error
));
244 ASSERT(error
== 0 || ip
== NULL
);
245 d_clear_d_op(dentry
);
246 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
247 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
249 return (d_splice_alias(ip
, dentry
));
253 zpl_snapdir_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
255 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
256 fstrans_cookie_t cookie
;
257 char snapname
[MAXNAMELEN
];
258 boolean_t case_conflict
;
262 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
264 cookie
= spl_fstrans_mark();
266 if (!zpl_dir_emit_dots(filp
, ctx
))
269 /* Start the position at 0 if it already emitted . and .. */
270 pos
= (ctx
->pos
== 2 ? 0 : ctx
->pos
);
272 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
273 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
274 snapname
, &id
, &pos
, &case_conflict
);
275 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
279 if (!zpl_dir_emit(ctx
, snapname
, strlen(snapname
),
280 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
286 spl_fstrans_unmark(cookie
);
287 zpl_exit(zfsvfs
, FTAG
);
289 if (error
== -ENOENT
)
295 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
297 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
299 zpl_dir_context_t ctx
=
300 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
303 error
= zpl_snapdir_iterate(filp
, &ctx
);
304 filp
->f_pos
= ctx
.pos
;
308 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
311 #ifdef HAVE_IOPS_RENAME_USERNS
312 zpl_snapdir_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
313 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
316 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
317 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
323 /* We probably don't want to support renameat2(2) in ctldir */
328 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
329 tdip
, dname(tdentry
), cr
, 0);
330 ASSERT3S(error
, <=, 0);
336 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
338 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
339 struct inode
*tdip
, struct dentry
*tdentry
)
341 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
346 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
352 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
353 ASSERT3S(error
, <=, 0);
360 #ifdef HAVE_IOPS_MKDIR_USERNS
361 zpl_snapdir_mkdir(struct user_namespace
*user_ns
, struct inode
*dip
,
362 struct dentry
*dentry
, umode_t mode
)
364 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
373 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
374 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
376 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
378 d_clear_d_op(dentry
);
379 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
380 d_instantiate(dentry
, ip
);
383 kmem_free(vap
, sizeof (vattr_t
));
384 ASSERT3S(error
, <=, 0);
391 * Get snapshot directory attributes.
394 #ifdef HAVE_USERNS_IOPS_GETATTR
395 zpl_snapdir_getattr_impl(struct user_namespace
*user_ns
,
396 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
397 unsigned int query_flags
)
399 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
400 u32 request_mask
, unsigned int query_flags
)
403 (void) request_mask
, (void) query_flags
;
404 struct inode
*ip
= path
->dentry
->d_inode
;
405 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
408 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
410 #ifdef HAVE_USERNS_IOPS_GETATTR
411 #ifdef HAVE_GENERIC_FILLATTR_USERNS
412 generic_fillattr(user_ns
, ip
, stat
);
417 generic_fillattr(ip
, stat
);
420 stat
->nlink
= stat
->size
= 2;
422 dsl_dataset_t
*ds
= dmu_objset_ds(zfsvfs
->z_os
);
423 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0) {
426 dmu_objset_pool(ds
->ds_objset
)->dp_meta_objset
,
427 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, &snap_count
);
429 zpl_exit(zfsvfs
, FTAG
);
432 stat
->nlink
+= snap_count
;
435 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
436 stat
->atime
= current_time(ip
);
437 zpl_exit(zfsvfs
, FTAG
);
441 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
444 * The '.zfs/snapshot' directory file operations. These mainly control
445 * generating the list of available snapshots when doing an 'ls' in the
446 * directory. See zpl_snapdir_readdir().
448 const struct file_operations zpl_fops_snapdir
= {
449 .open
= zpl_common_open
,
450 .llseek
= generic_file_llseek
,
451 .read
= generic_read_dir
,
452 #ifdef HAVE_VFS_ITERATE_SHARED
453 .iterate_shared
= zpl_snapdir_iterate
,
454 #elif defined(HAVE_VFS_ITERATE)
455 .iterate
= zpl_snapdir_iterate
,
457 .readdir
= zpl_snapdir_readdir
,
463 * The '.zfs/snapshot' directory inode operations. These mainly control
464 * creating an inode for a snapshot directory and initializing the needed
465 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
467 const struct inode_operations zpl_ops_snapdir
= {
468 .lookup
= zpl_snapdir_lookup
,
469 .getattr
= zpl_snapdir_getattr
,
470 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
471 .rename
= zpl_snapdir_rename2
,
473 .rename
= zpl_snapdir_rename
,
475 .rmdir
= zpl_snapdir_rmdir
,
476 .mkdir
= zpl_snapdir_mkdir
,
479 static struct dentry
*
480 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
483 fstrans_cookie_t cookie
;
485 struct inode
*ip
= NULL
;
489 cookie
= spl_fstrans_mark();
490 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
492 ASSERT3S(error
, <=, 0);
493 spl_fstrans_unmark(cookie
);
497 if (error
== -ENOENT
)
498 return (d_splice_alias(NULL
, dentry
));
500 return (ERR_PTR(error
));
503 return (d_splice_alias(ip
, dentry
));
507 zpl_shares_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
509 fstrans_cookie_t cookie
;
511 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
515 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
517 cookie
= spl_fstrans_mark();
519 if (zfsvfs
->z_shares_dir
== 0) {
520 zpl_dir_emit_dots(filp
, ctx
);
524 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
529 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
534 spl_fstrans_unmark(cookie
);
535 zpl_exit(zfsvfs
, FTAG
);
536 ASSERT3S(error
, <=, 0);
541 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
543 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
545 zpl_dir_context_t ctx
=
546 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
549 error
= zpl_shares_iterate(filp
, &ctx
);
550 filp
->f_pos
= ctx
.pos
;
554 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
557 #ifdef HAVE_USERNS_IOPS_GETATTR
558 zpl_shares_getattr_impl(struct user_namespace
*user_ns
,
559 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
560 unsigned int query_flags
)
562 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
563 u32 request_mask
, unsigned int query_flags
)
566 (void) request_mask
, (void) query_flags
;
567 struct inode
*ip
= path
->dentry
->d_inode
;
568 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
572 if ((error
= zpl_enter(zfsvfs
, FTAG
)) != 0)
575 if (zfsvfs
->z_shares_dir
== 0) {
576 #ifdef HAVE_USERNS_IOPS_GETATTR
577 #ifdef HAVE_GENERIC_FILLATTR_USERNS
578 generic_fillattr(user_ns
, path
->dentry
->d_inode
, stat
);
583 generic_fillattr(path
->dentry
->d_inode
, stat
);
585 stat
->nlink
= stat
->size
= 2;
586 stat
->atime
= current_time(ip
);
587 zpl_exit(zfsvfs
, FTAG
);
591 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
593 #ifdef HAVE_USERNS_IOPS_GETATTR
594 #ifdef HAVE_GENERIC_FILLATTR_USERNS
595 error
= -zfs_getattr_fast(user_ns
, ZTOI(dzp
), stat
);
600 error
= -zfs_getattr_fast(kcred
->user_ns
, ZTOI(dzp
), stat
);
605 zpl_exit(zfsvfs
, FTAG
);
606 ASSERT3S(error
, <=, 0);
610 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
613 * The '.zfs/shares' directory file operations.
615 const struct file_operations zpl_fops_shares
= {
616 .open
= zpl_common_open
,
617 .llseek
= generic_file_llseek
,
618 .read
= generic_read_dir
,
619 #ifdef HAVE_VFS_ITERATE_SHARED
620 .iterate_shared
= zpl_shares_iterate
,
621 #elif defined(HAVE_VFS_ITERATE)
622 .iterate
= zpl_shares_iterate
,
624 .readdir
= zpl_shares_readdir
,
630 * The '.zfs/shares' directory inode operations.
632 const struct inode_operations zpl_ops_shares
= {
633 .lookup
= zpl_shares_lookup
,
634 .getattr
= zpl_shares_getattr
,