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
)
163 struct dentry
*dentry
= path
->dentry
;
167 * We must briefly disable automounts for this dentry because the
168 * user space mount utility will trigger another lookup on this
169 * directory. That will result in zpl_snapdir_automount() being
170 * called repeatedly. The DCACHE_NEED_AUTOMOUNT flag can be
171 * safely reset once the mount completes.
173 dentry
->d_flags
&= ~DCACHE_NEED_AUTOMOUNT
;
174 error
= -zfsctl_mount_snapshot(path
, 0);
175 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
177 return ERR_PTR(error
);
180 * Rather than returning the new vfsmount for the snapshot we must
181 * return NULL to indicate a mount collision. This is done because
182 * the user space mount calls do_add_mount() which adds the vfsmount
183 * to the name space. If we returned the new mount here it would be
184 * added again to the vfsmount list resulting in list corruption.
188 #endif /* HAVE_AUTOMOUNT */
191 * Revalidate any dentry in the snapshot directory on lookup, since a snapshot
192 * having the same name have been created or destroyed since it was cached.
195 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
196 zpl_snapdir_revalidate(struct dentry
*dentry
, struct nameidata
*i
)
198 zpl_snapdir_revalidate(struct dentry
*dentry
, unsigned int flags
)
204 dentry_operations_t zpl_dops_snapdirs
= {
206 * Auto mounting of snapshots is only supported for 2.6.37 and
207 * newer kernels. Prior to this kernel the ops->follow_link()
208 * callback was used as a hack to trigger the mount. The
209 * resulting vfsmount was then explicitly grafted in to the
210 * name space. While it might be possible to add compatibility
211 * code to accomplish this it would require considerable care.
213 #ifdef HAVE_AUTOMOUNT
214 .d_automount
= zpl_snapdir_automount
,
215 #endif /* HAVE_AUTOMOUNT */
216 .d_revalidate
= zpl_snapdir_revalidate
,
219 static struct dentry
*
220 #ifdef HAVE_LOOKUP_NAMEIDATA
221 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
222 struct nameidata
*nd
)
224 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
230 struct inode
*ip
= NULL
;
234 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
236 ASSERT3S(error
, <=, 0);
239 if (error
&& error
!= -ENOENT
)
240 return ERR_PTR(error
);
242 ASSERT(error
== 0 || ip
== NULL
);
243 d_clear_d_op(dentry
);
244 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
246 return d_splice_alias(ip
, dentry
);
250 zpl_snapdir_iterate(struct file
*filp
, struct dir_context
*ctx
)
252 zfs_sb_t
*zsb
= ITOZSB(filp
->f_path
.dentry
->d_inode
);
253 char snapname
[MAXNAMELEN
];
254 boolean_t case_conflict
;
260 if (!dir_emit_dots(filp
, ctx
))
264 dsl_pool_config_enter(dmu_objset_pool(zsb
->z_os
), FTAG
);
265 error
= -dmu_snapshot_list_next(zsb
->z_os
, MAXNAMELEN
,
266 snapname
, &id
, &ctx
->pos
, &case_conflict
);
267 dsl_pool_config_exit(dmu_objset_pool(zsb
->z_os
), FTAG
);
271 if (!dir_emit(ctx
, snapname
, strlen(snapname
),
272 ZFSCTL_INO_SHARES
- id
, DT_DIR
))
278 if (error
== -ENOENT
)
284 #if !defined(HAVE_VFS_ITERATE)
286 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
288 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
291 error
= zpl_snapdir_iterate(filp
, &ctx
);
292 filp
->f_pos
= ctx
.pos
;
296 #endif /* HAVE_VFS_ITERATE */
299 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
300 struct inode
*tdip
, struct dentry
*tdentry
)
306 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
307 tdip
, dname(tdentry
), cr
, 0);
308 ASSERT3S(error
, <=, 0);
315 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
321 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
322 ASSERT3S(error
, <=, 0);
329 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, zpl_umode_t mode
)
337 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
338 zpl_vap_init(vap
, dip
, mode
| S_IFDIR
, cr
);
340 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
342 d_clear_d_op(dentry
);
343 d_set_d_op(dentry
, &zpl_dops_snapdirs
);
344 d_instantiate(dentry
, ip
);
347 kmem_free(vap
, sizeof(vattr_t
));
348 ASSERT3S(error
, <=, 0);
355 * Get snapshot directory attributes.
359 zpl_snapdir_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
362 zfs_sb_t
*zsb
= ITOZSB(dentry
->d_inode
);
366 error
= simple_getattr(mnt
, dentry
, stat
);
367 stat
->nlink
= stat
->size
= avl_numnodes(&zsb
->z_ctldir_snaps
) + 2;
368 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zsb
->z_os
);
369 stat
->atime
= CURRENT_TIME
;
376 * The '.zfs/snapshot' directory file operations. These mainly control
377 * generating the list of available snapshots when doing an 'ls' in the
378 * directory. See zpl_snapdir_readdir().
380 const struct file_operations zpl_fops_snapdir
= {
381 .open
= zpl_common_open
,
382 .llseek
= generic_file_llseek
,
383 .read
= generic_read_dir
,
384 #ifdef HAVE_VFS_ITERATE
385 .iterate
= zpl_snapdir_iterate
,
387 .readdir
= zpl_snapdir_readdir
,
393 * The '.zfs/snapshot' directory inode operations. These mainly control
394 * creating an inode for a snapshot directory and initializing the needed
395 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
397 const struct inode_operations zpl_ops_snapdir
= {
398 .lookup
= zpl_snapdir_lookup
,
399 .getattr
= zpl_snapdir_getattr
,
400 .rename
= zpl_snapdir_rename
,
401 .rmdir
= zpl_snapdir_rmdir
,
402 .mkdir
= zpl_snapdir_mkdir
,
405 static struct dentry
*
406 #ifdef HAVE_LOOKUP_NAMEIDATA
407 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
408 struct nameidata
*nd
)
410 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
415 struct inode
*ip
= NULL
;
419 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
421 ASSERT3S(error
, <=, 0);
425 if (error
== -ENOENT
)
426 return d_splice_alias(NULL
, dentry
);
428 return ERR_PTR(error
);
431 return d_splice_alias(ip
, dentry
);
435 zpl_shares_iterate(struct file
*filp
, struct dir_context
*ctx
)
438 zfs_sb_t
*zsb
= ITOZSB(filp
->f_path
.dentry
->d_inode
);
444 if (zsb
->z_shares_dir
== 0) {
445 dir_emit_dots(filp
, ctx
);
449 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
454 error
= -zfs_readdir(ZTOI(dzp
), ctx
, cr
);
460 ASSERT3S(error
, <=, 0);
465 #if !defined(HAVE_VFS_ITERATE)
467 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
469 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
472 error
= zpl_shares_iterate(filp
, &ctx
);
473 filp
->f_pos
= ctx
.pos
;
477 #endif /* HAVE_VFS_ITERATE */
481 zpl_shares_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
484 struct inode
*ip
= dentry
->d_inode
;
485 zfs_sb_t
*zsb
= ITOZSB(ip
);
491 if (zsb
->z_shares_dir
== 0) {
492 error
= simple_getattr(mnt
, dentry
, stat
);
493 stat
->nlink
= stat
->size
= 2;
494 stat
->atime
= CURRENT_TIME
;
499 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
501 error
= -zfs_getattr_fast(ZTOI(dzp
), stat
);
506 ASSERT3S(error
, <=, 0);
512 * The '.zfs/shares' directory file operations.
514 const struct file_operations zpl_fops_shares
= {
515 .open
= zpl_common_open
,
516 .llseek
= generic_file_llseek
,
517 .read
= generic_read_dir
,
518 #ifdef HAVE_VFS_ITERATE
519 .iterate
= zpl_shares_iterate
,
521 .readdir
= zpl_shares_readdir
,
527 * The '.zfs/shares' directory inode operations.
529 const struct inode_operations zpl_ops_shares
= {
530 .lookup
= zpl_shares_lookup
,
531 .getattr
= zpl_shares_getattr
,