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 http://www.opensolaris.org/os/licensing.
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>
37 * Common open routine. Disallow any write access.
41 zpl_common_open(struct inode
*ip
, struct file
*filp
)
43 if (filp
->f_mode
& FMODE_WRITE
)
46 return (generic_file_open(ip
, filp
));
50 * Get root directory contents.
53 zpl_root_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
55 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
60 if (!zpl_dir_emit_dots(filp
, ctx
))
64 if (!zpl_dir_emit(ctx
, ZFS_SNAPDIR_NAME
,
65 strlen(ZFS_SNAPDIR_NAME
), ZFSCTL_INO_SNAPDIR
, DT_DIR
))
72 if (!zpl_dir_emit(ctx
, ZFS_SHAREDIR_NAME
,
73 strlen(ZFS_SHAREDIR_NAME
), ZFSCTL_INO_SHARES
, DT_DIR
))
84 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
86 zpl_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
88 zpl_dir_context_t ctx
=
89 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
92 error
= zpl_root_iterate(filp
, &ctx
);
93 filp
->f_pos
= ctx
.pos
;
97 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
100 * Get root directory attributes.
104 #ifdef HAVE_USERNS_IOPS_GETATTR
105 zpl_root_getattr_impl(struct user_namespace
*user_ns
,
106 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
107 unsigned int query_flags
)
109 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
110 u32 request_mask
, unsigned int query_flags
)
113 struct inode
*ip
= path
->dentry
->d_inode
;
115 #if defined(HAVE_GENERIC_FILLATTR_USERNS) && defined(HAVE_USERNS_IOPS_GETATTR)
116 generic_fillattr(user_ns
, ip
, stat
);
118 generic_fillattr(ip
, stat
);
120 stat
->atime
= current_time(ip
);
124 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
126 static struct dentry
*
127 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
134 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
135 ASSERT3S(error
, <=, 0);
139 if (error
== -ENOENT
)
140 return (d_splice_alias(NULL
, dentry
));
142 return (ERR_PTR(error
));
145 return (d_splice_alias(ip
, dentry
));
149 * The '.zfs' control directory file and inode operations.
151 const struct file_operations zpl_fops_root
= {
152 .open
= zpl_common_open
,
153 .llseek
= generic_file_llseek
,
154 .read
= generic_read_dir
,
155 #ifdef HAVE_VFS_ITERATE_SHARED
156 .iterate_shared
= zpl_root_iterate
,
157 #elif defined(HAVE_VFS_ITERATE)
158 .iterate
= zpl_root_iterate
,
160 .readdir
= zpl_root_readdir
,
164 const struct inode_operations zpl_ops_root
= {
165 .lookup
= zpl_root_lookup
,
166 .getattr
= zpl_root_getattr
,
169 static struct vfsmount
*
170 zpl_snapdir_automount(struct path
*path
)
174 error
= -zfsctl_snapshot_mount(path
, 0);
176 return (ERR_PTR(error
));
179 * Rather than returning the new vfsmount for the snapshot we must
180 * return NULL to indicate a mount collision. This is done because
181 * the user space mount calls do_add_mount() which adds the vfsmount
182 * to the name space. If we returned the new mount here it would be
183 * added again to the vfsmount list resulting in list corruption.
189 * Negative dentries must always be revalidated so newly created snapshots
190 * can be detected and automounted. Normal dentries should be kept because
191 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
192 * the snapshot being immediately unmounted.
195 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
196 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
198 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
201 return (!!dentry
->d_inode
);
204 static const dentry_operations_t zpl_dops_snapdirs
= {
206 * Auto mounting of snapshots is only supported for 2.6.37 and
207 * newer kernels. Prior to this kernel the ops->follow_link()
208 * callback was used as a hack to trigger the mount. The
209 * resulting vfsmount was then explicitly grafted in to the
210 * name space. While it might be possible to add compatibility
211 * code to accomplish this it would require considerable care.
213 .d_automount
= zpl_snapdir_automount
,
214 .d_revalidate
= zpl_snapdir_revalidate
,
217 static struct dentry
*
218 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
221 fstrans_cookie_t cookie
;
223 struct inode
*ip
= NULL
;
227 cookie
= spl_fstrans_mark();
228 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
230 ASSERT3S(error
, <=, 0);
231 spl_fstrans_unmark(cookie
);
234 if (error
&& error
!= -ENOENT
)
235 return (ERR_PTR(error
));
237 ASSERT(error
== 0 || ip
== NULL
);
238 d_clear_d_op(dentry
);
239 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
240 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
242 return (d_splice_alias(ip
, dentry
));
246 zpl_snapdir_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
248 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
249 fstrans_cookie_t cookie
;
250 char snapname
[MAXNAMELEN
];
251 boolean_t case_conflict
;
256 cookie
= spl_fstrans_mark();
258 if (!zpl_dir_emit_dots(filp
, ctx
))
261 /* Start the position at 0 if it already emitted . and .. */
262 pos
= (ctx
->pos
== 2 ? 0 : ctx
->pos
);
264 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
265 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
266 snapname
, &id
, &pos
, &case_conflict
);
267 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
271 if (!zpl_dir_emit(ctx
, snapname
, strlen(snapname
),
272 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
278 spl_fstrans_unmark(cookie
);
281 if (error
== -ENOENT
)
287 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
289 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
291 zpl_dir_context_t ctx
=
292 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
295 error
= zpl_snapdir_iterate(filp
, &ctx
);
296 filp
->f_pos
= ctx
.pos
;
300 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
303 #ifdef HAVE_IOPS_RENAME_USERNS
304 zpl_snapdir_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
305 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
308 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
309 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
315 /* We probably don't want to support renameat2(2) in ctldir */
320 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
321 tdip
, dname(tdentry
), cr
, 0);
322 ASSERT3S(error
, <=, 0);
328 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
330 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
331 struct inode
*tdip
, struct dentry
*tdentry
)
333 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
338 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
344 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
345 ASSERT3S(error
, <=, 0);
352 #ifdef HAVE_IOPS_MKDIR_USERNS
353 zpl_snapdir_mkdir(struct user_namespace
*user_ns
, struct inode
*dip
,
354 struct dentry
*dentry
, umode_t mode
)
356 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
365 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
366 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
368 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
370 d_clear_d_op(dentry
);
371 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
372 d_instantiate(dentry
, ip
);
375 kmem_free(vap
, sizeof (vattr_t
));
376 ASSERT3S(error
, <=, 0);
383 * Get snapshot directory attributes.
387 #ifdef HAVE_USERNS_IOPS_GETATTR
388 zpl_snapdir_getattr_impl(struct user_namespace
*user_ns
,
389 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
390 unsigned int query_flags
)
392 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
393 u32 request_mask
, unsigned int query_flags
)
396 struct inode
*ip
= path
->dentry
->d_inode
;
397 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
400 #if defined(HAVE_GENERIC_FILLATTR_USERNS) && defined(HAVE_USERNS_IOPS_GETATTR)
401 generic_fillattr(user_ns
, ip
, stat
);
403 generic_fillattr(ip
, stat
);
406 stat
->nlink
= stat
->size
= 2;
407 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
408 stat
->atime
= current_time(ip
);
413 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
416 * The '.zfs/snapshot' directory file operations. These mainly control
417 * generating the list of available snapshots when doing an 'ls' in the
418 * directory. See zpl_snapdir_readdir().
420 const struct file_operations zpl_fops_snapdir
= {
421 .open
= zpl_common_open
,
422 .llseek
= generic_file_llseek
,
423 .read
= generic_read_dir
,
424 #ifdef HAVE_VFS_ITERATE_SHARED
425 .iterate_shared
= zpl_snapdir_iterate
,
426 #elif defined(HAVE_VFS_ITERATE)
427 .iterate
= zpl_snapdir_iterate
,
429 .readdir
= zpl_snapdir_readdir
,
435 * The '.zfs/snapshot' directory inode operations. These mainly control
436 * creating an inode for a snapshot directory and initializing the needed
437 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
439 const struct inode_operations zpl_ops_snapdir
= {
440 .lookup
= zpl_snapdir_lookup
,
441 .getattr
= zpl_snapdir_getattr
,
442 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
443 .rename
= zpl_snapdir_rename2
,
445 .rename
= zpl_snapdir_rename
,
447 .rmdir
= zpl_snapdir_rmdir
,
448 .mkdir
= zpl_snapdir_mkdir
,
451 static struct dentry
*
452 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
455 fstrans_cookie_t cookie
;
457 struct inode
*ip
= NULL
;
461 cookie
= spl_fstrans_mark();
462 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
464 ASSERT3S(error
, <=, 0);
465 spl_fstrans_unmark(cookie
);
469 if (error
== -ENOENT
)
470 return (d_splice_alias(NULL
, dentry
));
472 return (ERR_PTR(error
));
475 return (d_splice_alias(ip
, dentry
));
479 zpl_shares_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
481 fstrans_cookie_t cookie
;
483 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
488 cookie
= spl_fstrans_mark();
490 if (zfsvfs
->z_shares_dir
== 0) {
491 zpl_dir_emit_dots(filp
, ctx
);
495 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
500 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
505 spl_fstrans_unmark(cookie
);
507 ASSERT3S(error
, <=, 0);
512 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
514 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
516 zpl_dir_context_t ctx
=
517 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
520 error
= zpl_shares_iterate(filp
, &ctx
);
521 filp
->f_pos
= ctx
.pos
;
525 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
529 #ifdef HAVE_USERNS_IOPS_GETATTR
530 zpl_shares_getattr_impl(struct user_namespace
*user_ns
,
531 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
532 unsigned int query_flags
)
534 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
535 u32 request_mask
, unsigned int query_flags
)
538 struct inode
*ip
= path
->dentry
->d_inode
;
539 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
545 if (zfsvfs
->z_shares_dir
== 0) {
546 #if defined(HAVE_GENERIC_FILLATTR_USERNS) && defined(HAVE_USERNS_IOPS_GETATTR)
547 generic_fillattr(user_ns
, path
->dentry
->d_inode
, stat
);
549 generic_fillattr(path
->dentry
->d_inode
, stat
);
551 stat
->nlink
= stat
->size
= 2;
552 stat
->atime
= current_time(ip
);
557 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
559 #if defined(HAVE_GENERIC_FILLATTR_USERNS) && defined(HAVE_USERNS_IOPS_GETATTR)
560 error
= -zfs_getattr_fast(user_ns
, ZTOI(dzp
), stat
);
562 error
= -zfs_getattr_fast(kcred
->user_ns
, ZTOI(dzp
), stat
);
568 ASSERT3S(error
, <=, 0);
572 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
575 * The '.zfs/shares' directory file operations.
577 const struct file_operations zpl_fops_shares
= {
578 .open
= zpl_common_open
,
579 .llseek
= generic_file_llseek
,
580 .read
= generic_read_dir
,
581 #ifdef HAVE_VFS_ITERATE_SHARED
582 .iterate_shared
= zpl_shares_iterate
,
583 #elif defined(HAVE_VFS_ITERATE)
584 .iterate
= zpl_shares_iterate
,
586 .readdir
= zpl_shares_readdir
,
592 * The '.zfs/shares' directory inode operations.
594 const struct inode_operations zpl_ops_shares
= {
595 .lookup
= zpl_shares_lookup
,
596 .getattr
= zpl_shares_getattr
,