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 zfs_sb_t
*zsb
= 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(struct vfsmount
*mnt
, struct dentry
*dentry
,
108 error
= simple_getattr(mnt
, dentry
, stat
);
109 stat
->atime
= CURRENT_TIME
;
114 static struct dentry
*
115 #ifdef HAVE_LOOKUP_NAMEIDATA
116 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
118 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
126 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
127 ASSERT3S(error
, <=, 0);
131 if (error
== -ENOENT
)
132 return (d_splice_alias(NULL
, dentry
));
134 return (ERR_PTR(error
));
137 return (d_splice_alias(ip
, dentry
));
141 * The '.zfs' control directory file and inode operations.
143 const struct file_operations zpl_fops_root
= {
144 .open
= zpl_common_open
,
145 .llseek
= generic_file_llseek
,
146 .read
= generic_read_dir
,
147 #ifdef HAVE_VFS_ITERATE_SHARED
148 .iterate_shared
= zpl_root_iterate
,
149 #elif defined(HAVE_VFS_ITERATE)
150 .iterate
= zpl_root_iterate
,
152 .readdir
= zpl_root_readdir
,
156 const struct inode_operations zpl_ops_root
= {
157 .lookup
= zpl_root_lookup
,
158 .getattr
= zpl_root_getattr
,
161 #ifdef HAVE_AUTOMOUNT
162 static struct vfsmount
*
163 zpl_snapdir_automount(struct path
*path
)
167 error
= -zfsctl_snapshot_mount(path
, 0);
169 return (ERR_PTR(error
));
172 * Rather than returning the new vfsmount for the snapshot we must
173 * return NULL to indicate a mount collision. This is done because
174 * the user space mount calls do_add_mount() which adds the vfsmount
175 * to the name space. If we returned the new mount here it would be
176 * added again to the vfsmount list resulting in list corruption.
180 #endif /* HAVE_AUTOMOUNT */
183 * Negative dentries must always be revalidated so newly created snapshots
184 * can be detected and automounted. Normal dentries should be kept because
185 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
186 * the snapshot being immediately unmounted.
189 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
190 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
192 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
195 return (!!dentry
->d_inode
);
198 dentry_operations_t zpl_dops_snapdirs
= {
200 * Auto mounting of snapshots is only supported for 2.6.37 and
201 * newer kernels. Prior to this kernel the ops->follow_link()
202 * callback was used as a hack to trigger the mount. The
203 * resulting vfsmount was then explicitly grafted in to the
204 * name space. While it might be possible to add compatibility
205 * code to accomplish this it would require considerable care.
207 #ifdef HAVE_AUTOMOUNT
208 .d_automount
= zpl_snapdir_automount
,
209 #endif /* HAVE_AUTOMOUNT */
210 .d_revalidate
= zpl_snapdir_revalidate
,
213 static struct dentry
*
214 #ifdef HAVE_LOOKUP_NAMEIDATA
215 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
216 struct nameidata
*nd
)
218 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
223 fstrans_cookie_t cookie
;
225 struct inode
*ip
= NULL
;
229 cookie
= spl_fstrans_mark();
230 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
232 ASSERT3S(error
, <=, 0);
233 spl_fstrans_unmark(cookie
);
236 if (error
&& error
!= -ENOENT
)
237 return (ERR_PTR(error
));
239 ASSERT(error
== 0 || ip
== NULL
);
240 d_clear_d_op(dentry
);
241 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
242 #ifdef HAVE_AUTOMOUNT
243 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
246 return (d_splice_alias(ip
, dentry
));
250 zpl_snapdir_iterate(struct file
*filp
, struct dir_context
*ctx
)
252 zfs_sb_t
*zsb
= ITOZSB(file_inode(filp
));
253 fstrans_cookie_t cookie
;
254 char snapname
[MAXNAMELEN
];
255 boolean_t case_conflict
;
260 cookie
= spl_fstrans_mark();
262 if (!dir_emit_dots(filp
, ctx
))
267 dsl_pool_config_enter(dmu_objset_pool(zsb
->z_os
), FTAG
);
268 error
= -dmu_snapshot_list_next(zsb
->z_os
, MAXNAMELEN
,
269 snapname
, &id
, &pos
, &case_conflict
);
270 dsl_pool_config_exit(dmu_objset_pool(zsb
->z_os
), FTAG
);
274 if (!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 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
297 error
= zpl_snapdir_iterate(filp
, &ctx
);
298 filp
->f_pos
= ctx
.pos
;
302 #endif /* HAVE_VFS_ITERATE */
305 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
306 struct inode
*tdip
, struct dentry
*tdentry
)
312 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
313 tdip
, dname(tdentry
), cr
, 0);
314 ASSERT3S(error
, <=, 0);
321 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
327 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
328 ASSERT3S(error
, <=, 0);
335 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, zpl_umode_t mode
)
343 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
344 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
346 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
348 d_clear_d_op(dentry
);
349 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
350 d_instantiate(dentry
, ip
);
353 kmem_free(vap
, sizeof (vattr_t
));
354 ASSERT3S(error
, <=, 0);
361 * Get snapshot directory attributes.
365 zpl_snapdir_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
368 zfs_sb_t
*zsb
= ITOZSB(dentry
->d_inode
);
372 error
= simple_getattr(mnt
, dentry
, stat
);
373 stat
->nlink
= stat
->size
= 2;
374 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zsb
->z_os
);
375 stat
->atime
= CURRENT_TIME
;
382 * The '.zfs/snapshot' directory file operations. These mainly control
383 * generating the list of available snapshots when doing an 'ls' in the
384 * directory. See zpl_snapdir_readdir().
386 const struct file_operations zpl_fops_snapdir
= {
387 .open
= zpl_common_open
,
388 .llseek
= generic_file_llseek
,
389 .read
= generic_read_dir
,
390 #ifdef HAVE_VFS_ITERATE_SHARED
391 .iterate_shared
= zpl_snapdir_iterate
,
392 #elif defined(HAVE_VFS_ITERATE)
393 .iterate
= zpl_snapdir_iterate
,
395 .readdir
= zpl_snapdir_readdir
,
401 * The '.zfs/snapshot' directory inode operations. These mainly control
402 * creating an inode for a snapshot directory and initializing the needed
403 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
405 const struct inode_operations zpl_ops_snapdir
= {
406 .lookup
= zpl_snapdir_lookup
,
407 .getattr
= zpl_snapdir_getattr
,
408 .rename
= zpl_snapdir_rename
,
409 .rmdir
= zpl_snapdir_rmdir
,
410 .mkdir
= zpl_snapdir_mkdir
,
413 static struct dentry
*
414 #ifdef HAVE_LOOKUP_NAMEIDATA
415 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
416 struct nameidata
*nd
)
418 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
422 fstrans_cookie_t cookie
;
424 struct inode
*ip
= NULL
;
428 cookie
= spl_fstrans_mark();
429 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
431 ASSERT3S(error
, <=, 0);
432 spl_fstrans_unmark(cookie
);
436 if (error
== -ENOENT
)
437 return (d_splice_alias(NULL
, dentry
));
439 return (ERR_PTR(error
));
442 return (d_splice_alias(ip
, dentry
));
446 zpl_shares_iterate(struct file
*filp
, struct dir_context
*ctx
)
448 fstrans_cookie_t cookie
;
450 zfs_sb_t
*zsb
= ITOZSB(file_inode(filp
));
455 cookie
= spl_fstrans_mark();
457 if (zsb
->z_shares_dir
== 0) {
458 dir_emit_dots(filp
, ctx
);
462 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
467 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
472 spl_fstrans_unmark(cookie
);
474 ASSERT3S(error
, <=, 0);
479 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
481 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
483 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
486 error
= zpl_shares_iterate(filp
, &ctx
);
487 filp
->f_pos
= ctx
.pos
;
491 #endif /* HAVE_VFS_ITERATE */
495 zpl_shares_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
498 struct inode
*ip
= dentry
->d_inode
;
499 zfs_sb_t
*zsb
= ITOZSB(ip
);
505 if (zsb
->z_shares_dir
== 0) {
506 error
= simple_getattr(mnt
, dentry
, stat
);
507 stat
->nlink
= stat
->size
= 2;
508 stat
->atime
= CURRENT_TIME
;
513 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
515 error
= -zfs_getattr_fast(ZTOI(dzp
), stat
);
520 ASSERT3S(error
, <=, 0);
526 * The '.zfs/shares' directory file operations.
528 const struct file_operations zpl_fops_shares
= {
529 .open
= zpl_common_open
,
530 .llseek
= generic_file_llseek
,
531 .read
= generic_read_dir
,
532 #ifdef HAVE_VFS_ITERATE_SHARED
533 .iterate_shared
= zpl_shares_iterate
,
534 #elif defined(HAVE_VFS_ITERATE)
535 .iterate
= zpl_shares_iterate
,
537 .readdir
= zpl_shares_readdir
,
543 * The '.zfs/shares' directory inode operations.
545 const struct inode_operations zpl_ops_shares
= {
546 .lookup
= zpl_shares_lookup
,
547 .getattr
= zpl_shares_getattr
,