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(filp->f_path.dentry->d_inode);
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)
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
148 .iterate = zpl_root_iterate,
150 .readdir = zpl_root_readdir,
154 const struct inode_operations zpl_ops_root = {
155 .lookup = zpl_root_lookup,
156 .getattr = zpl_root_getattr,
159 #ifdef HAVE_AUTOMOUNT
160 static struct vfsmount *
161 zpl_snapdir_automount(struct path *path)
165 error = -zfsctl_snapshot_mount(path, 0);
167 return (ERR_PTR(error));
170 * Rather than returning the new vfsmount for the snapshot we must
171 * return NULL to indicate a mount collision. This is done because
172 * the user space mount calls do_add_mount() which adds the vfsmount
173 * to the name space. If we returned the new mount here it would be
174 * added again to the vfsmount list resulting in list corruption.
178 #endif /* HAVE_AUTOMOUNT */
181 * Negative dentries must always be revalidated so newly created snapshots
182 * can be detected and automounted. Normal dentries should be kept because
183 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
184 * the snapshot being immediately unmounted.
187 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
188 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i)
190 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
193 return (!!dentry->d_inode);
196 dentry_operations_t zpl_dops_snapdirs = {
198 * Auto mounting of snapshots is only supported for 2.6.37 and
199 * newer kernels. Prior to this kernel the ops->follow_link()
200 * callback was used as a hack to trigger the mount. The
201 * resulting vfsmount was then explicitly grafted in to the
202 * name space. While it might be possible to add compatibility
203 * code to accomplish this it would require considerable care.
205 #ifdef HAVE_AUTOMOUNT
206 .d_automount = zpl_snapdir_automount,
207 #endif /* HAVE_AUTOMOUNT */
208 .d_revalidate = zpl_snapdir_revalidate,
211 static struct dentry *
212 #ifdef HAVE_LOOKUP_NAMEIDATA
213 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
214 struct nameidata *nd)
216 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 #ifdef HAVE_AUTOMOUNT
241 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
244 return (d_splice_alias(ip, dentry));
248 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
250 zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
251 fstrans_cookie_t cookie;
252 char snapname[MAXNAMELEN];
253 boolean_t case_conflict;
258 cookie = spl_fstrans_mark();
260 if (!dir_emit_dots(filp, ctx))
265 dsl_pool_config_enter(dmu_objset_pool(zsb->z_os), FTAG);
266 error = -dmu_snapshot_list_next(zsb->z_os, MAXNAMELEN,
267 snapname, &id, &pos, &case_conflict);
268 dsl_pool_config_exit(dmu_objset_pool(zsb->z_os), FTAG);
272 if (!dir_emit(ctx, snapname, strlen(snapname),
273 ZFSCTL_INO_SHARES - id, DT_DIR))
279 spl_fstrans_unmark(cookie);
282 if (error == -ENOENT)
288 #if !defined(HAVE_VFS_ITERATE)
290 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
292 struct dir_context ctx = 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 */
303 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
304 struct inode *tdip, struct dentry *tdentry)
310 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
311 tdip, dname(tdentry), cr, 0);
312 ASSERT3S(error, <=, 0);
319 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
325 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
326 ASSERT3S(error, <=, 0);
333 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, zpl_umode_t mode)
341 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
342 zpl_vap_init(vap, dip, mode | S_IFDIR, cr);
344 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
346 d_clear_d_op(dentry);
347 d_set_d_op(dentry, &zpl_dops_snapdirs);
348 d_instantiate(dentry, ip);
351 kmem_free(vap, sizeof (vattr_t));
352 ASSERT3S(error, <=, 0);
359 * Get snapshot directory attributes.
363 zpl_snapdir_getattr(struct vfsmount *mnt, struct dentry *dentry,
366 zfs_sb_t *zsb = ITOZSB(dentry->d_inode);
370 error = simple_getattr(mnt, dentry, stat);
371 stat->nlink = stat->size = 2;
372 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zsb->z_os);
373 stat->atime = CURRENT_TIME;
380 * The '.zfs/snapshot' directory file operations. These mainly control
381 * generating the list of available snapshots when doing an 'ls' in the
382 * directory. See zpl_snapdir_readdir().
384 const struct file_operations zpl_fops_snapdir = {
385 .open = zpl_common_open,
386 .llseek = generic_file_llseek,
387 .read = generic_read_dir,
388 #ifdef HAVE_VFS_ITERATE
389 .iterate = zpl_snapdir_iterate,
391 .readdir = zpl_snapdir_readdir,
397 * The '.zfs/snapshot' directory inode operations. These mainly control
398 * creating an inode for a snapshot directory and initializing the needed
399 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
401 const struct inode_operations zpl_ops_snapdir = {
402 .lookup = zpl_snapdir_lookup,
403 .getattr = zpl_snapdir_getattr,
404 .rename = zpl_snapdir_rename,
405 .rmdir = zpl_snapdir_rmdir,
406 .mkdir = zpl_snapdir_mkdir,
409 static struct dentry *
410 #ifdef HAVE_LOOKUP_NAMEIDATA
411 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
412 struct nameidata *nd)
414 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
418 fstrans_cookie_t cookie;
420 struct inode *ip = NULL;
424 cookie = spl_fstrans_mark();
425 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
427 ASSERT3S(error, <=, 0);
428 spl_fstrans_unmark(cookie);
432 if (error == -ENOENT)
433 return (d_splice_alias(NULL, dentry));
435 return (ERR_PTR(error));
438 return (d_splice_alias(ip, dentry));
442 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
444 fstrans_cookie_t cookie;
446 zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
451 cookie = spl_fstrans_mark();
453 if (zsb->z_shares_dir == 0) {
454 dir_emit_dots(filp, ctx);
458 error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
463 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
468 spl_fstrans_unmark(cookie);
470 ASSERT3S(error, <=, 0);
475 #if !defined(HAVE_VFS_ITERATE)
477 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
479 struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
482 error = zpl_shares_iterate(filp, &ctx);
483 filp->f_pos = ctx.pos;
487 #endif /* HAVE_VFS_ITERATE */
491 zpl_shares_getattr(struct vfsmount *mnt, struct dentry *dentry,
494 struct inode *ip = dentry->d_inode;
495 zfs_sb_t *zsb = ITOZSB(ip);
501 if (zsb->z_shares_dir == 0) {
502 error = simple_getattr(mnt, dentry, stat);
503 stat->nlink = stat->size = 2;
504 stat->atime = CURRENT_TIME;
509 error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
511 error = -zfs_getattr_fast(ZTOI(dzp), stat);
516 ASSERT3S(error, <=, 0);
522 * The '.zfs/shares' directory file operations.
524 const struct file_operations zpl_fops_shares = {
525 .open = zpl_common_open,
526 .llseek = generic_file_llseek,
527 .read = generic_read_dir,
528 #ifdef HAVE_VFS_ITERATE
529 .iterate = zpl_shares_iterate,
531 .readdir = zpl_shares_readdir,
537 * The '.zfs/shares' directory inode operations.
539 const struct inode_operations zpl_ops_shares = {
540 .lookup = zpl_shares_lookup,
541 .getattr = zpl_shares_getattr,