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.
40 zpl_common_open(struct inode
*ip
, struct file
*filp
)
42 if (filp
->f_mode
& FMODE_WRITE
)
45 return (generic_file_open(ip
, filp
));
49 * Get root directory contents.
52 zpl_root_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
54 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
59 if (!zpl_dir_emit_dots(filp
, ctx
))
63 if (!zpl_dir_emit(ctx
, ZFS_SNAPDIR_NAME
,
64 strlen(ZFS_SNAPDIR_NAME
), ZFSCTL_INO_SNAPDIR
, DT_DIR
))
71 if (!zpl_dir_emit(ctx
, ZFS_SHAREDIR_NAME
,
72 strlen(ZFS_SHAREDIR_NAME
), ZFSCTL_INO_SHARES
, DT_DIR
))
83 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
85 zpl_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
87 zpl_dir_context_t ctx
=
88 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
91 error
= zpl_root_iterate(filp
, &ctx
);
92 filp
->f_pos
= ctx
.pos
;
96 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
99 * Get root directory attributes.
102 #ifdef HAVE_USERNS_IOPS_GETATTR
103 zpl_root_getattr_impl(struct user_namespace
*user_ns
,
104 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
105 unsigned int query_flags
)
107 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
108 u32 request_mask
, unsigned int query_flags
)
111 (void) request_mask
, (void) query_flags
;
112 struct inode
*ip
= path
->dentry
->d_inode
;
114 #ifdef HAVE_USERNS_IOPS_GETATTR
115 #ifdef HAVE_GENERIC_FILLATTR_USERNS
116 generic_fillattr(user_ns
, ip
, stat
);
121 generic_fillattr(ip
, stat
);
123 stat
->atime
= current_time(ip
);
127 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
129 static struct dentry
*
130 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
137 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
138 ASSERT3S(error
, <=, 0);
142 if (error
== -ENOENT
)
143 return (d_splice_alias(NULL
, dentry
));
145 return (ERR_PTR(error
));
148 return (d_splice_alias(ip
, dentry
));
152 * The '.zfs' control directory file and inode operations.
154 const struct file_operations zpl_fops_root
= {
155 .open
= zpl_common_open
,
156 .llseek
= generic_file_llseek
,
157 .read
= generic_read_dir
,
158 #ifdef HAVE_VFS_ITERATE_SHARED
159 .iterate_shared
= zpl_root_iterate
,
160 #elif defined(HAVE_VFS_ITERATE)
161 .iterate
= zpl_root_iterate
,
163 .readdir
= zpl_root_readdir
,
167 const struct inode_operations zpl_ops_root
= {
168 .lookup
= zpl_root_lookup
,
169 .getattr
= zpl_root_getattr
,
172 static struct vfsmount
*
173 zpl_snapdir_automount(struct path
*path
)
177 error
= -zfsctl_snapshot_mount(path
, 0);
179 return (ERR_PTR(error
));
182 * Rather than returning the new vfsmount for the snapshot we must
183 * return NULL to indicate a mount collision. This is done because
184 * the user space mount calls do_add_mount() which adds the vfsmount
185 * to the name space. If we returned the new mount here it would be
186 * added again to the vfsmount list resulting in list corruption.
192 * Negative dentries must always be revalidated so newly created snapshots
193 * can be detected and automounted. Normal dentries should be kept because
194 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
195 * the snapshot being immediately unmounted.
198 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
199 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
201 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
204 return (!!dentry
->d_inode
);
207 static const dentry_operations_t zpl_dops_snapdirs
= {
209 * Auto mounting of snapshots is only supported for 2.6.37 and
210 * newer kernels. Prior to this kernel the ops->follow_link()
211 * callback was used as a hack to trigger the mount. The
212 * resulting vfsmount was then explicitly grafted in to the
213 * name space. While it might be possible to add compatibility
214 * code to accomplish this it would require considerable care.
216 .d_automount
= zpl_snapdir_automount
,
217 .d_revalidate
= zpl_snapdir_revalidate
,
220 static struct dentry
*
221 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
224 fstrans_cookie_t cookie
;
226 struct inode
*ip
= NULL
;
230 cookie
= spl_fstrans_mark();
231 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
233 ASSERT3S(error
, <=, 0);
234 spl_fstrans_unmark(cookie
);
237 if (error
&& error
!= -ENOENT
)
238 return (ERR_PTR(error
));
240 ASSERT(error
== 0 || ip
== NULL
);
241 d_clear_d_op(dentry
);
242 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
243 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
245 return (d_splice_alias(ip
, dentry
));
249 zpl_snapdir_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
251 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
252 fstrans_cookie_t cookie
;
253 char snapname
[MAXNAMELEN
];
254 boolean_t case_conflict
;
259 cookie
= spl_fstrans_mark();
261 if (!zpl_dir_emit_dots(filp
, ctx
))
264 /* Start the position at 0 if it already emitted . and .. */
265 pos
= (ctx
->pos
== 2 ? 0 : ctx
->pos
);
267 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
268 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
269 snapname
, &id
, &pos
, &case_conflict
);
270 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
274 if (!zpl_dir_emit(ctx
, snapname
, strlen(snapname
),
275 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
281 spl_fstrans_unmark(cookie
);
284 if (error
== -ENOENT
)
290 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
292 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
294 zpl_dir_context_t ctx
=
295 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
298 error
= zpl_snapdir_iterate(filp
, &ctx
);
299 filp
->f_pos
= ctx
.pos
;
303 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
306 #ifdef HAVE_IOPS_RENAME_USERNS
307 zpl_snapdir_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
308 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
311 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
312 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
318 /* We probably don't want to support renameat2(2) in ctldir */
323 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
324 tdip
, dname(tdentry
), cr
, 0);
325 ASSERT3S(error
, <=, 0);
331 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
333 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
334 struct inode
*tdip
, struct dentry
*tdentry
)
336 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
341 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
347 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
348 ASSERT3S(error
, <=, 0);
355 #ifdef HAVE_IOPS_MKDIR_USERNS
356 zpl_snapdir_mkdir(struct user_namespace
*user_ns
, struct inode
*dip
,
357 struct dentry
*dentry
, umode_t mode
)
359 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
368 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
369 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
371 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
373 d_clear_d_op(dentry
);
374 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
375 d_instantiate(dentry
, ip
);
378 kmem_free(vap
, sizeof (vattr_t
));
379 ASSERT3S(error
, <=, 0);
386 * Get snapshot directory attributes.
389 #ifdef HAVE_USERNS_IOPS_GETATTR
390 zpl_snapdir_getattr_impl(struct user_namespace
*user_ns
,
391 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
392 unsigned int query_flags
)
394 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
395 u32 request_mask
, unsigned int query_flags
)
398 (void) request_mask
, (void) query_flags
;
399 struct inode
*ip
= path
->dentry
->d_inode
;
400 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
403 #ifdef HAVE_USERNS_IOPS_GETATTR
404 #ifdef HAVE_GENERIC_FILLATTR_USERNS
405 generic_fillattr(user_ns
, ip
, stat
);
410 generic_fillattr(ip
, stat
);
413 stat
->nlink
= stat
->size
= 2;
414 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
415 stat
->atime
= current_time(ip
);
420 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
423 * The '.zfs/snapshot' directory file operations. These mainly control
424 * generating the list of available snapshots when doing an 'ls' in the
425 * directory. See zpl_snapdir_readdir().
427 const struct file_operations zpl_fops_snapdir
= {
428 .open
= zpl_common_open
,
429 .llseek
= generic_file_llseek
,
430 .read
= generic_read_dir
,
431 #ifdef HAVE_VFS_ITERATE_SHARED
432 .iterate_shared
= zpl_snapdir_iterate
,
433 #elif defined(HAVE_VFS_ITERATE)
434 .iterate
= zpl_snapdir_iterate
,
436 .readdir
= zpl_snapdir_readdir
,
442 * The '.zfs/snapshot' directory inode operations. These mainly control
443 * creating an inode for a snapshot directory and initializing the needed
444 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
446 const struct inode_operations zpl_ops_snapdir
= {
447 .lookup
= zpl_snapdir_lookup
,
448 .getattr
= zpl_snapdir_getattr
,
449 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
450 .rename
= zpl_snapdir_rename2
,
452 .rename
= zpl_snapdir_rename
,
454 .rmdir
= zpl_snapdir_rmdir
,
455 .mkdir
= zpl_snapdir_mkdir
,
458 static struct dentry
*
459 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
462 fstrans_cookie_t cookie
;
464 struct inode
*ip
= NULL
;
468 cookie
= spl_fstrans_mark();
469 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
471 ASSERT3S(error
, <=, 0);
472 spl_fstrans_unmark(cookie
);
476 if (error
== -ENOENT
)
477 return (d_splice_alias(NULL
, dentry
));
479 return (ERR_PTR(error
));
482 return (d_splice_alias(ip
, dentry
));
486 zpl_shares_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
488 fstrans_cookie_t cookie
;
490 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
495 cookie
= spl_fstrans_mark();
497 if (zfsvfs
->z_shares_dir
== 0) {
498 zpl_dir_emit_dots(filp
, ctx
);
502 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
507 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
512 spl_fstrans_unmark(cookie
);
514 ASSERT3S(error
, <=, 0);
519 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
521 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
523 zpl_dir_context_t ctx
=
524 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
527 error
= zpl_shares_iterate(filp
, &ctx
);
528 filp
->f_pos
= ctx
.pos
;
532 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
535 #ifdef HAVE_USERNS_IOPS_GETATTR
536 zpl_shares_getattr_impl(struct user_namespace
*user_ns
,
537 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
538 unsigned int query_flags
)
540 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
541 u32 request_mask
, unsigned int query_flags
)
544 (void) request_mask
, (void) query_flags
;
545 struct inode
*ip
= path
->dentry
->d_inode
;
546 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
552 if (zfsvfs
->z_shares_dir
== 0) {
553 #ifdef HAVE_USERNS_IOPS_GETATTR
554 #ifdef HAVE_GENERIC_FILLATTR_USERNS
555 generic_fillattr(user_ns
, path
->dentry
->d_inode
, stat
);
560 generic_fillattr(path
->dentry
->d_inode
, stat
);
562 stat
->nlink
= stat
->size
= 2;
563 stat
->atime
= current_time(ip
);
568 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
570 #ifdef HAVE_USERNS_IOPS_GETATTR
571 #ifdef HAVE_GENERIC_FILLATTR_USERNS
572 error
= -zfs_getattr_fast(user_ns
, ZTOI(dzp
), stat
);
577 error
= -zfs_getattr_fast(kcred
->user_ns
, ZTOI(dzp
), stat
);
583 ASSERT3S(error
, <=, 0);
587 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
590 * The '.zfs/shares' directory file operations.
592 const struct file_operations zpl_fops_shares
= {
593 .open
= zpl_common_open
,
594 .llseek
= generic_file_llseek
,
595 .read
= generic_read_dir
,
596 #ifdef HAVE_VFS_ITERATE_SHARED
597 .iterate_shared
= zpl_shares_iterate
,
598 #elif defined(HAVE_VFS_ITERATE)
599 .iterate
= zpl_shares_iterate
,
601 .readdir
= zpl_shares_readdir
,
607 * The '.zfs/shares' directory inode operations.
609 const struct inode_operations zpl_ops_shares
= {
610 .lookup
= zpl_shares_lookup
,
611 .getattr
= zpl_shares_getattr
,