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 generic_fillattr(path->dentry->d_inode, stat);
107 stat->atime = CURRENT_TIME;
111 ZPL_GETATTR_WRAPPER(zpl_root_getattr);
113 static struct dentry *
114 #ifdef HAVE_LOOKUP_NAMEIDATA
115 zpl_root_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
117 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
125 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
126 ASSERT3S(error, <=, 0);
130 if (error == -ENOENT)
131 return (d_splice_alias(NULL, dentry));
133 return (ERR_PTR(error));
136 return (d_splice_alias(ip, dentry));
140 * The '.zfs' control directory file and inode operations.
142 const struct file_operations zpl_fops_root = {
143 .open = zpl_common_open,
144 .llseek = generic_file_llseek,
145 .read = generic_read_dir,
146 #ifdef HAVE_VFS_ITERATE_SHARED
147 .iterate_shared = zpl_root_iterate,
148 #elif defined(HAVE_VFS_ITERATE)
149 .iterate = zpl_root_iterate,
151 .readdir = zpl_root_readdir,
155 const struct inode_operations zpl_ops_root = {
156 .lookup = zpl_root_lookup,
157 .getattr = zpl_root_getattr,
160 #ifdef HAVE_AUTOMOUNT
161 static struct vfsmount *
162 zpl_snapdir_automount(struct path *path)
166 error = -zfsctl_snapshot_mount(path, 0);
168 return (ERR_PTR(error));
171 * Rather than returning the new vfsmount for the snapshot we must
172 * return NULL to indicate a mount collision. This is done because
173 * the user space mount calls do_add_mount() which adds the vfsmount
174 * to the name space. If we returned the new mount here it would be
175 * added again to the vfsmount list resulting in list corruption.
179 #endif /* HAVE_AUTOMOUNT */
182 * Negative dentries must always be revalidated so newly created snapshots
183 * can be detected and automounted. Normal dentries should be kept because
184 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
185 * the snapshot being immediately unmounted.
188 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
189 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i)
191 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
194 return (!!dentry->d_inode);
197 dentry_operations_t zpl_dops_snapdirs = {
199 * Auto mounting of snapshots is only supported for 2.6.37 and
200 * newer kernels. Prior to this kernel the ops->follow_link()
201 * callback was used as a hack to trigger the mount. The
202 * resulting vfsmount was then explicitly grafted in to the
203 * name space. While it might be possible to add compatibility
204 * code to accomplish this it would require considerable care.
206 #ifdef HAVE_AUTOMOUNT
207 .d_automount = zpl_snapdir_automount,
208 #endif /* HAVE_AUTOMOUNT */
209 .d_revalidate = zpl_snapdir_revalidate,
212 static struct dentry *
213 #ifdef HAVE_LOOKUP_NAMEIDATA
214 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
215 struct nameidata *nd)
217 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
222 fstrans_cookie_t cookie;
224 struct inode *ip = NULL;
228 cookie = spl_fstrans_mark();
229 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
231 ASSERT3S(error, <=, 0);
232 spl_fstrans_unmark(cookie);
235 if (error && error != -ENOENT)
236 return (ERR_PTR(error));
238 ASSERT(error == 0 || ip == NULL);
239 d_clear_d_op(dentry);
240 d_set_d_op(dentry, &zpl_dops_snapdirs);
241 #ifdef HAVE_AUTOMOUNT
242 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
245 return (d_splice_alias(ip, dentry));
249 zpl_snapdir_iterate(struct file *filp, struct dir_context *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 (!dir_emit_dots(filp, ctx))
266 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
267 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
268 snapname, &id, &pos, &case_conflict);
269 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
273 if (!dir_emit(ctx, snapname, strlen(snapname),
274 ZFSCTL_INO_SHARES - id, DT_DIR))
280 spl_fstrans_unmark(cookie);
283 if (error == -ENOENT)
289 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
291 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
293 struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
296 error = zpl_snapdir_iterate(filp, &ctx);
297 filp->f_pos = ctx.pos;
301 #endif /* HAVE_VFS_ITERATE */
304 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,
305 struct inode *tdip, struct dentry *tdentry, unsigned int flags)
310 /* We probably don't want to support renameat2(2) in ctldir */
315 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
316 tdip, dname(tdentry), cr, 0);
317 ASSERT3S(error, <=, 0);
323 #ifndef HAVE_RENAME_WANTS_FLAGS
325 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
326 struct inode *tdip, struct dentry *tdentry)
328 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));
333 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
339 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
340 ASSERT3S(error, <=, 0);
347 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, zpl_umode_t mode)
355 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
356 zpl_vap_init(vap, dip, mode | S_IFDIR, cr);
358 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
360 d_clear_d_op(dentry);
361 d_set_d_op(dentry, &zpl_dops_snapdirs);
362 d_instantiate(dentry, ip);
365 kmem_free(vap, sizeof (vattr_t));
366 ASSERT3S(error, <=, 0);
373 * Get snapshot directory attributes.
377 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
378 u32 request_mask, unsigned int query_flags)
380 zfsvfs_t *zfsvfs = ITOZSB(path->dentry->d_inode);
383 generic_fillattr(path->dentry->d_inode, stat);
385 stat->nlink = stat->size = 2;
386 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
387 stat->atime = CURRENT_TIME;
392 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);
395 * The '.zfs/snapshot' directory file operations. These mainly control
396 * generating the list of available snapshots when doing an 'ls' in the
397 * directory. See zpl_snapdir_readdir().
399 const struct file_operations zpl_fops_snapdir = {
400 .open = zpl_common_open,
401 .llseek = generic_file_llseek,
402 .read = generic_read_dir,
403 #ifdef HAVE_VFS_ITERATE_SHARED
404 .iterate_shared = zpl_snapdir_iterate,
405 #elif defined(HAVE_VFS_ITERATE)
406 .iterate = zpl_snapdir_iterate,
408 .readdir = zpl_snapdir_readdir,
414 * The '.zfs/snapshot' directory inode operations. These mainly control
415 * creating an inode for a snapshot directory and initializing the needed
416 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
418 const struct inode_operations zpl_ops_snapdir = {
419 .lookup = zpl_snapdir_lookup,
420 .getattr = zpl_snapdir_getattr,
421 #ifdef HAVE_RENAME_WANTS_FLAGS
422 .rename = zpl_snapdir_rename2,
424 .rename = zpl_snapdir_rename,
426 .rmdir = zpl_snapdir_rmdir,
427 .mkdir = zpl_snapdir_mkdir,
430 static struct dentry *
431 #ifdef HAVE_LOOKUP_NAMEIDATA
432 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
433 struct nameidata *nd)
435 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
439 fstrans_cookie_t cookie;
441 struct inode *ip = NULL;
445 cookie = spl_fstrans_mark();
446 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
448 ASSERT3S(error, <=, 0);
449 spl_fstrans_unmark(cookie);
453 if (error == -ENOENT)
454 return (d_splice_alias(NULL, dentry));
456 return (ERR_PTR(error));
459 return (d_splice_alias(ip, dentry));
463 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
465 fstrans_cookie_t cookie;
467 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
472 cookie = spl_fstrans_mark();
474 if (zfsvfs->z_shares_dir == 0) {
475 dir_emit_dots(filp, ctx);
479 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
484 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
489 spl_fstrans_unmark(cookie);
491 ASSERT3S(error, <=, 0);
496 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
498 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
500 struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
503 error = zpl_shares_iterate(filp, &ctx);
504 filp->f_pos = ctx.pos;
508 #endif /* HAVE_VFS_ITERATE */
512 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,
513 u32 request_mask, unsigned int query_flags)
515 struct inode *ip = path->dentry->d_inode;
516 zfsvfs_t *zfsvfs = ITOZSB(ip);
522 if (zfsvfs->z_shares_dir == 0) {
523 generic_fillattr(path->dentry->d_inode, stat);
524 stat->nlink = stat->size = 2;
525 stat->atime = CURRENT_TIME;
530 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
532 error = -zfs_getattr_fast(ZTOI(dzp), stat);
537 ASSERT3S(error, <=, 0);
541 ZPL_GETATTR_WRAPPER(zpl_shares_getattr);
544 * The '.zfs/shares' directory file operations.
546 const struct file_operations zpl_fops_shares = {
547 .open = zpl_common_open,
548 .llseek = generic_file_llseek,
549 .read = generic_read_dir,
550 #ifdef HAVE_VFS_ITERATE_SHARED
551 .iterate_shared = zpl_shares_iterate,
552 #elif defined(HAVE_VFS_ITERATE)
553 .iterate = zpl_shares_iterate,
555 .readdir = zpl_shares_readdir,
561 * The '.zfs/shares' directory inode operations.
563 const struct inode_operations zpl_ops_shares = {
564 .lookup = zpl_shares_lookup,
565 .getattr = zpl_shares_getattr,