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_vfsops.h>
31 #include <sys/zfs_vnops.h>
32 #include <sys/zfs_znode.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
, struct dir_context
*ctx
)
55 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
60 if (!dir_emit_dots(filp
, ctx
))
64 if (!dir_emit(ctx
, ZFS_SNAPDIR_NAME
, strlen(ZFS_SNAPDIR_NAME
),
65 ZFSCTL_INO_SNAPDIR
, DT_DIR
))
72 if (!dir_emit(ctx
, ZFS_SHAREDIR_NAME
, strlen(ZFS_SHAREDIR_NAME
),
73 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 struct dir_context ctx
= 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 */
99 * Get root directory attributes.
103 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
104 u32 request_mask
, unsigned int query_flags
)
106 struct inode
*ip
= path
->dentry
->d_inode
;
108 generic_fillattr(ip
, stat
);
109 stat
->atime
= current_time(ip
);
113 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
115 static struct dentry
*
116 #ifdef HAVE_LOOKUP_NAMEIDATA
117 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
119 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
127 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
128 ASSERT3S(error
, <=, 0);
132 if (error
== -ENOENT
)
133 return (d_splice_alias(NULL
, dentry
));
135 return (ERR_PTR(error
));
138 return (d_splice_alias(ip
, dentry
));
142 * The '.zfs' control directory file and inode operations.
144 const struct file_operations zpl_fops_root
= {
145 .open
= zpl_common_open
,
146 .llseek
= generic_file_llseek
,
147 .read
= generic_read_dir
,
148 #ifdef HAVE_VFS_ITERATE_SHARED
149 .iterate_shared
= zpl_root_iterate
,
150 #elif defined(HAVE_VFS_ITERATE)
151 .iterate
= zpl_root_iterate
,
153 .readdir
= zpl_root_readdir
,
157 const struct inode_operations zpl_ops_root
= {
158 .lookup
= zpl_root_lookup
,
159 .getattr
= zpl_root_getattr
,
162 #ifdef HAVE_AUTOMOUNT
163 static struct vfsmount
*
164 zpl_snapdir_automount(struct path
*path
)
168 error
= -zfsctl_snapshot_mount(path
, 0);
170 return (ERR_PTR(error
));
173 * Rather than returning the new vfsmount for the snapshot we must
174 * return NULL to indicate a mount collision. This is done because
175 * the user space mount calls do_add_mount() which adds the vfsmount
176 * to the name space. If we returned the new mount here it would be
177 * added again to the vfsmount list resulting in list corruption.
181 #endif /* HAVE_AUTOMOUNT */
184 * Negative dentries must always be revalidated so newly created snapshots
185 * can be detected and automounted. Normal dentries should be kept because
186 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
187 * the snapshot being immediately unmounted.
190 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
191 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
193 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
196 return (!!dentry
->d_inode
);
199 dentry_operations_t zpl_dops_snapdirs
= {
201 * Auto mounting of snapshots is only supported for 2.6.37 and
202 * newer kernels. Prior to this kernel the ops->follow_link()
203 * callback was used as a hack to trigger the mount. The
204 * resulting vfsmount was then explicitly grafted in to the
205 * name space. While it might be possible to add compatibility
206 * code to accomplish this it would require considerable care.
208 #ifdef HAVE_AUTOMOUNT
209 .d_automount
= zpl_snapdir_automount
,
210 #endif /* HAVE_AUTOMOUNT */
211 .d_revalidate
= zpl_snapdir_revalidate
,
214 static struct dentry
*
215 #ifdef HAVE_LOOKUP_NAMEIDATA
216 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
217 struct nameidata
*nd
)
219 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 #ifdef HAVE_AUTOMOUNT
244 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
247 return (d_splice_alias(ip
, dentry
));
251 zpl_snapdir_iterate(struct file
*filp
, struct dir_context
*ctx
)
253 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
254 fstrans_cookie_t cookie
;
255 char snapname
[MAXNAMELEN
];
256 boolean_t case_conflict
;
261 cookie
= spl_fstrans_mark();
263 if (!dir_emit_dots(filp
, ctx
))
268 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
269 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
270 snapname
, &id
, &pos
, &case_conflict
);
271 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
275 if (!dir_emit(ctx
, snapname
, strlen(snapname
),
276 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
282 spl_fstrans_unmark(cookie
);
285 if (error
== -ENOENT
)
291 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
293 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
295 struct dir_context ctx
= 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 */
306 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
307 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
312 /* We probably don't want to support renameat2(2) in ctldir */
317 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
318 tdip
, dname(tdentry
), cr
, 0);
319 ASSERT3S(error
, <=, 0);
325 #ifndef HAVE_RENAME_WANTS_FLAGS
327 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
328 struct inode
*tdip
, struct dentry
*tdentry
)
330 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
335 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
341 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
342 ASSERT3S(error
, <=, 0);
349 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, zpl_umode_t mode
)
357 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
358 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
360 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
362 d_clear_d_op(dentry
);
363 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
364 d_instantiate(dentry
, ip
);
367 kmem_free(vap
, sizeof (vattr_t
));
368 ASSERT3S(error
, <=, 0);
375 * Get snapshot directory attributes.
379 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
380 u32 request_mask
, unsigned int query_flags
)
382 struct inode
*ip
= path
->dentry
->d_inode
;
383 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
386 generic_fillattr(ip
, stat
);
388 stat
->nlink
= stat
->size
= 2;
389 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
390 stat
->atime
= current_time(ip
);
395 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
398 * The '.zfs/snapshot' directory file operations. These mainly control
399 * generating the list of available snapshots when doing an 'ls' in the
400 * directory. See zpl_snapdir_readdir().
402 const struct file_operations zpl_fops_snapdir
= {
403 .open
= zpl_common_open
,
404 .llseek
= generic_file_llseek
,
405 .read
= generic_read_dir
,
406 #ifdef HAVE_VFS_ITERATE_SHARED
407 .iterate_shared
= zpl_snapdir_iterate
,
408 #elif defined(HAVE_VFS_ITERATE)
409 .iterate
= zpl_snapdir_iterate
,
411 .readdir
= zpl_snapdir_readdir
,
417 * The '.zfs/snapshot' directory inode operations. These mainly control
418 * creating an inode for a snapshot directory and initializing the needed
419 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
421 const struct inode_operations zpl_ops_snapdir
= {
422 .lookup
= zpl_snapdir_lookup
,
423 .getattr
= zpl_snapdir_getattr
,
424 #ifdef HAVE_RENAME_WANTS_FLAGS
425 .rename
= zpl_snapdir_rename2
,
427 .rename
= zpl_snapdir_rename
,
429 .rmdir
= zpl_snapdir_rmdir
,
430 .mkdir
= zpl_snapdir_mkdir
,
433 static struct dentry
*
434 #ifdef HAVE_LOOKUP_NAMEIDATA
435 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
436 struct nameidata
*nd
)
438 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
442 fstrans_cookie_t cookie
;
444 struct inode
*ip
= NULL
;
448 cookie
= spl_fstrans_mark();
449 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
451 ASSERT3S(error
, <=, 0);
452 spl_fstrans_unmark(cookie
);
456 if (error
== -ENOENT
)
457 return (d_splice_alias(NULL
, dentry
));
459 return (ERR_PTR(error
));
462 return (d_splice_alias(ip
, dentry
));
466 zpl_shares_iterate(struct file
*filp
, struct dir_context
*ctx
)
468 fstrans_cookie_t cookie
;
470 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
475 cookie
= spl_fstrans_mark();
477 if (zfsvfs
->z_shares_dir
== 0) {
478 dir_emit_dots(filp
, ctx
);
482 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
487 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
492 spl_fstrans_unmark(cookie
);
494 ASSERT3S(error
, <=, 0);
499 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
501 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
503 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
506 error
= zpl_shares_iterate(filp
, &ctx
);
507 filp
->f_pos
= ctx
.pos
;
511 #endif /* HAVE_VFS_ITERATE */
515 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
516 u32 request_mask
, unsigned int query_flags
)
518 struct inode
*ip
= path
->dentry
->d_inode
;
519 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
525 if (zfsvfs
->z_shares_dir
== 0) {
526 generic_fillattr(path
->dentry
->d_inode
, stat
);
527 stat
->nlink
= stat
->size
= 2;
528 stat
->atime
= current_time(ip
);
533 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
535 error
= -zfs_getattr_fast(ZTOI(dzp
), stat
);
540 ASSERT3S(error
, <=, 0);
544 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
547 * The '.zfs/shares' directory file operations.
549 const struct file_operations zpl_fops_shares
= {
550 .open
= zpl_common_open
,
551 .llseek
= generic_file_llseek
,
552 .read
= generic_read_dir
,
553 #ifdef HAVE_VFS_ITERATE_SHARED
554 .iterate_shared
= zpl_shares_iterate
,
555 #elif defined(HAVE_VFS_ITERATE)
556 .iterate
= zpl_shares_iterate
,
558 .readdir
= zpl_shares_readdir
,
564 * The '.zfs/shares' directory inode operations.
566 const struct inode_operations zpl_ops_shares
= {
567 .lookup
= zpl_shares_lookup
,
568 .getattr
= zpl_shares_getattr
,