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
, 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 zpl_root_getattr_impl(const struct path
*path
, struct kstat
*stat
,
105 u32 request_mask
, unsigned int query_flags
)
107 struct inode
*ip
= path
->dentry
->d_inode
;
109 generic_fillattr(ip
, stat
);
110 stat
->atime
= current_time(ip
);
114 ZPL_GETATTR_WRAPPER(zpl_root_getattr
);
116 static struct dentry
*
117 #ifdef HAVE_LOOKUP_NAMEIDATA
118 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
120 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
128 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
129 ASSERT3S(error
, <=, 0);
133 if (error
== -ENOENT
)
134 return (d_splice_alias(NULL
, dentry
));
136 return (ERR_PTR(error
));
139 return (d_splice_alias(ip
, dentry
));
143 * The '.zfs' control directory file and inode operations.
145 const struct file_operations zpl_fops_root
= {
146 .open
= zpl_common_open
,
147 .llseek
= generic_file_llseek
,
148 .read
= generic_read_dir
,
149 #ifdef HAVE_VFS_ITERATE_SHARED
150 .iterate_shared
= zpl_root_iterate
,
151 #elif defined(HAVE_VFS_ITERATE)
152 .iterate
= zpl_root_iterate
,
154 .readdir
= zpl_root_readdir
,
158 const struct inode_operations zpl_ops_root
= {
159 .lookup
= zpl_root_lookup
,
160 .getattr
= zpl_root_getattr
,
163 #ifdef HAVE_AUTOMOUNT
164 static struct vfsmount
*
165 zpl_snapdir_automount(struct path
*path
)
169 error
= -zfsctl_snapshot_mount(path
, 0);
171 return (ERR_PTR(error
));
174 * Rather than returning the new vfsmount for the snapshot we must
175 * return NULL to indicate a mount collision. This is done because
176 * the user space mount calls do_add_mount() which adds the vfsmount
177 * to the name space. If we returned the new mount here it would be
178 * added again to the vfsmount list resulting in list corruption.
182 #endif /* HAVE_AUTOMOUNT */
185 * Negative dentries must always be revalidated so newly created snapshots
186 * can be detected and automounted. Normal dentries should be kept because
187 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
188 * the snapshot being immediately unmounted.
191 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
192 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
194 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
197 return (!!dentry
->d_inode
);
200 dentry_operations_t zpl_dops_snapdirs
= {
202 * Auto mounting of snapshots is only supported for 2.6.37 and
203 * newer kernels. Prior to this kernel the ops->follow_link()
204 * callback was used as a hack to trigger the mount. The
205 * resulting vfsmount was then explicitly grafted in to the
206 * name space. While it might be possible to add compatibility
207 * code to accomplish this it would require considerable care.
209 #ifdef HAVE_AUTOMOUNT
210 .d_automount
= zpl_snapdir_automount
,
211 #endif /* HAVE_AUTOMOUNT */
212 .d_revalidate
= zpl_snapdir_revalidate
,
215 static struct dentry
*
216 #ifdef HAVE_LOOKUP_NAMEIDATA
217 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
218 struct nameidata
*nd
)
220 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
225 fstrans_cookie_t cookie
;
227 struct inode
*ip
= NULL
;
231 cookie
= spl_fstrans_mark();
232 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
234 ASSERT3S(error
, <=, 0);
235 spl_fstrans_unmark(cookie
);
238 if (error
&& error
!= -ENOENT
)
239 return (ERR_PTR(error
));
241 ASSERT(error
== 0 || ip
== NULL
);
242 d_clear_d_op(dentry
);
243 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
244 #ifdef HAVE_AUTOMOUNT
245 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
248 return (d_splice_alias(ip
, dentry
));
252 zpl_snapdir_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
254 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
255 fstrans_cookie_t cookie
;
256 char snapname
[MAXNAMELEN
];
257 boolean_t case_conflict
;
262 cookie
= spl_fstrans_mark();
264 if (!zpl_dir_emit_dots(filp
, ctx
))
269 dsl_pool_config_enter(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
270 error
= -dmu_snapshot_list_next(zfsvfs
->z_os
, MAXNAMELEN
,
271 snapname
, &id
, &pos
, &case_conflict
);
272 dsl_pool_config_exit(dmu_objset_pool(zfsvfs
->z_os
), FTAG
);
276 if (!zpl_dir_emit(ctx
, snapname
, strlen(snapname
),
277 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
283 spl_fstrans_unmark(cookie
);
286 if (error
== -ENOENT
)
292 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
294 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
296 zpl_dir_context_t ctx
=
297 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
300 error
= zpl_snapdir_iterate(filp
, &ctx
);
301 filp
->f_pos
= ctx
.pos
;
305 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
308 zpl_snapdir_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
309 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
314 /* We probably don't want to support renameat2(2) in ctldir */
319 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
320 tdip
, dname(tdentry
), cr
, 0);
321 ASSERT3S(error
, <=, 0);
327 #ifndef HAVE_RENAME_WANTS_FLAGS
329 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
330 struct inode
*tdip
, struct dentry
*tdentry
)
332 return (zpl_snapdir_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
337 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
343 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
344 ASSERT3S(error
, <=, 0);
351 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, zpl_umode_t mode
)
359 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
360 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
362 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
364 d_clear_d_op(dentry
);
365 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
366 d_instantiate(dentry
, ip
);
369 kmem_free(vap
, sizeof (vattr_t
));
370 ASSERT3S(error
, <=, 0);
377 * Get snapshot directory attributes.
381 zpl_snapdir_getattr_impl(const struct path
*path
, struct kstat
*stat
,
382 u32 request_mask
, unsigned int query_flags
)
384 struct inode
*ip
= path
->dentry
->d_inode
;
385 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
388 generic_fillattr(ip
, stat
);
390 stat
->nlink
= stat
->size
= 2;
391 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zfsvfs
->z_os
);
392 stat
->atime
= current_time(ip
);
397 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr
);
400 * The '.zfs/snapshot' directory file operations. These mainly control
401 * generating the list of available snapshots when doing an 'ls' in the
402 * directory. See zpl_snapdir_readdir().
404 const struct file_operations zpl_fops_snapdir
= {
405 .open
= zpl_common_open
,
406 .llseek
= generic_file_llseek
,
407 .read
= generic_read_dir
,
408 #ifdef HAVE_VFS_ITERATE_SHARED
409 .iterate_shared
= zpl_snapdir_iterate
,
410 #elif defined(HAVE_VFS_ITERATE)
411 .iterate
= zpl_snapdir_iterate
,
413 .readdir
= zpl_snapdir_readdir
,
419 * The '.zfs/snapshot' directory inode operations. These mainly control
420 * creating an inode for a snapshot directory and initializing the needed
421 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
423 const struct inode_operations zpl_ops_snapdir
= {
424 .lookup
= zpl_snapdir_lookup
,
425 .getattr
= zpl_snapdir_getattr
,
426 #ifdef HAVE_RENAME_WANTS_FLAGS
427 .rename
= zpl_snapdir_rename2
,
429 .rename
= zpl_snapdir_rename
,
431 .rmdir
= zpl_snapdir_rmdir
,
432 .mkdir
= zpl_snapdir_mkdir
,
435 static struct dentry
*
436 #ifdef HAVE_LOOKUP_NAMEIDATA
437 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
438 struct nameidata
*nd
)
440 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
444 fstrans_cookie_t cookie
;
446 struct inode
*ip
= NULL
;
450 cookie
= spl_fstrans_mark();
451 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
453 ASSERT3S(error
, <=, 0);
454 spl_fstrans_unmark(cookie
);
458 if (error
== -ENOENT
)
459 return (d_splice_alias(NULL
, dentry
));
461 return (ERR_PTR(error
));
464 return (d_splice_alias(ip
, dentry
));
468 zpl_shares_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
470 fstrans_cookie_t cookie
;
472 zfsvfs_t
*zfsvfs
= ITOZSB(file_inode(filp
));
477 cookie
= spl_fstrans_mark();
479 if (zfsvfs
->z_shares_dir
== 0) {
480 zpl_dir_emit_dots(filp
, ctx
);
484 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
489 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
494 spl_fstrans_unmark(cookie
);
496 ASSERT3S(error
, <=, 0);
501 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
503 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
505 zpl_dir_context_t ctx
=
506 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
509 error
= zpl_shares_iterate(filp
, &ctx
);
510 filp
->f_pos
= ctx
.pos
;
514 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
518 zpl_shares_getattr_impl(const struct path
*path
, struct kstat
*stat
,
519 u32 request_mask
, unsigned int query_flags
)
521 struct inode
*ip
= path
->dentry
->d_inode
;
522 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
528 if (zfsvfs
->z_shares_dir
== 0) {
529 generic_fillattr(path
->dentry
->d_inode
, stat
);
530 stat
->nlink
= stat
->size
= 2;
531 stat
->atime
= current_time(ip
);
536 error
= -zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &dzp
);
538 error
= -zfs_getattr_fast(ZTOI(dzp
), stat
);
543 ASSERT3S(error
, <=, 0);
547 ZPL_GETATTR_WRAPPER(zpl_shares_getattr
);
550 * The '.zfs/shares' directory file operations.
552 const struct file_operations zpl_fops_shares
= {
553 .open
= zpl_common_open
,
554 .llseek
= generic_file_llseek
,
555 .read
= generic_read_dir
,
556 #ifdef HAVE_VFS_ITERATE_SHARED
557 .iterate_shared
= zpl_shares_iterate
,
558 #elif defined(HAVE_VFS_ITERATE)
559 .iterate
= zpl_shares_iterate
,
561 .readdir
= zpl_shares_readdir
,
567 * The '.zfs/shares' directory inode operations.
569 const struct inode_operations zpl_ops_shares
= {
570 .lookup
= zpl_shares_lookup
,
571 .getattr
= zpl_shares_getattr
,