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 zpl_common_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
52 struct dentry
*dentry
= filp
->f_path
.dentry
;
53 struct inode
*ip
= dentry
->d_inode
;
56 switch (filp
->f_pos
) {
58 error
= filldir(dirent
, ".", 1, 0, ip
->i_ino
, DT_DIR
);
65 error
= filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
);
79 * Get root directory contents.
82 zpl_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
84 struct dentry
*dentry
= filp
->f_path
.dentry
;
85 struct inode
*ip
= dentry
->d_inode
;
86 zfs_sb_t
*zsb
= ITOZSB(ip
);
91 switch (filp
->f_pos
) {
93 error
= filldir(dirent
, ".", 1, 0, ip
->i_ino
, DT_DIR
);
100 error
= filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
);
107 error
= filldir(dirent
, ZFS_SNAPDIR_NAME
,
108 strlen(ZFS_SNAPDIR_NAME
), 2, ZFSCTL_INO_SNAPDIR
, DT_DIR
);
115 error
= filldir(dirent
, ZFS_SHAREDIR_NAME
,
116 strlen(ZFS_SHAREDIR_NAME
), 3, ZFSCTL_INO_SHARES
, DT_DIR
);
130 * Get root directory attributes.
134 zpl_root_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
139 error
= simple_getattr(mnt
, dentry
, stat
);
140 stat
->atime
= CURRENT_TIME
;
145 static struct dentry
*
146 #ifdef HAVE_LOOKUP_NAMEIDATA
147 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
149 zpl_root_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
157 error
= -zfsctl_root_lookup(dip
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
158 ASSERT3S(error
, <=, 0);
162 if (error
== -ENOENT
)
163 return d_splice_alias(NULL
, dentry
);
165 return ERR_PTR(error
);
168 return d_splice_alias(ip
, dentry
);
172 * The '.zfs' control directory file and inode operations.
174 const struct file_operations zpl_fops_root
= {
175 .open
= zpl_common_open
,
176 .llseek
= generic_file_llseek
,
177 .read
= generic_read_dir
,
178 .readdir
= zpl_root_readdir
,
181 const struct inode_operations zpl_ops_root
= {
182 .lookup
= zpl_root_lookup
,
183 .getattr
= zpl_root_getattr
,
186 static struct dentry
*
187 #ifdef HAVE_LOOKUP_NAMEIDATA
188 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
189 struct nameidata
*nd
)
191 zpl_snapdir_lookup(struct inode
*dip
, struct dentry
*dentry
,
201 error
= -zfsctl_snapdir_lookup(dip
, dname(dentry
), &ip
,
203 ASSERT3S(error
, <=, 0);
207 if (error
== -ENOENT
)
208 return d_splice_alias(NULL
, dentry
);
210 return ERR_PTR(error
);
214 * Auto mounting of snapshots is only supported for 2.6.37 and
215 * newer kernels. Prior to this kernel the ops->follow_link()
216 * callback was used as a hack to trigger the mount. The
217 * resulting vfsmount was then explicitly grafted in to the
218 * name space. While it might be possible to add compatibility
219 * code to accomplish this it would require considerable care.
221 #ifdef HAVE_AUTOMOUNT
222 dentry
->d_op
= &zpl_dops_snapdirs
;
223 #endif /* HAVE_AUTOMOUNT */
225 return d_splice_alias(ip
, dentry
);
230 zpl_snapdir_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
232 struct dentry
*dentry
= filp
->f_path
.dentry
;
233 struct inode
*dip
= dentry
->d_inode
;
234 zfs_sb_t
*zsb
= ITOZSB(dip
);
235 char snapname
[MAXNAMELEN
];
237 boolean_t case_conflict
;
242 cookie
= filp
->f_pos
;
243 switch (filp
->f_pos
) {
245 error
= filldir(dirent
, ".", 1, 0, dip
->i_ino
, DT_DIR
);
252 error
= filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
);
260 error
= -dmu_snapshot_list_next(zsb
->z_os
, MAXNAMELEN
,
261 snapname
, &id
, &cookie
, &case_conflict
);
265 error
= filldir(dirent
, snapname
, strlen(snapname
),
266 filp
->f_pos
, ZFSCTL_INO_SHARES
- id
, DT_DIR
);
270 filp
->f_pos
= cookie
;
276 if (error
== -ENOENT
)
283 zpl_snapdir_rename(struct inode
*sdip
, struct dentry
*sdentry
,
284 struct inode
*tdip
, struct dentry
*tdentry
)
290 error
= -zfsctl_snapdir_rename(sdip
, dname(sdentry
),
291 tdip
, dname(tdentry
), cr
, 0);
292 ASSERT3S(error
, <=, 0);
299 zpl_snapdir_rmdir(struct inode
*dip
, struct dentry
*dentry
)
305 error
= -zfsctl_snapdir_remove(dip
, dname(dentry
), cr
, 0);
306 ASSERT3S(error
, <=, 0);
313 zpl_snapdir_mkdir(struct inode
*dip
, struct dentry
*dentry
, zpl_umode_t mode
)
321 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
322 zpl_vap_init(vap
, dip
, dentry
, mode
| S_IFDIR
, cr
);
324 error
= -zfsctl_snapdir_mkdir(dip
, dname(dentry
), vap
, &ip
, cr
, 0);
326 #ifdef HAVE_AUTOMOUNT
327 dentry
->d_op
= &zpl_dops_snapdirs
;
328 #endif /* HAVE_AUTOMOUNT */
329 d_instantiate(dentry
, ip
);
332 kmem_free(vap
, sizeof(vattr_t
));
333 ASSERT3S(error
, <=, 0);
339 #ifdef HAVE_AUTOMOUNT
340 static struct vfsmount
*
341 zpl_snapdir_automount(struct path
*path
)
343 struct dentry
*dentry
= path
->dentry
;
347 * We must briefly disable automounts for this dentry because the
348 * user space mount utility will trigger another lookup on this
349 * directory. That will result in zpl_snapdir_automount() being
350 * called repeatedly. The DCACHE_NEED_AUTOMOUNT flag can be
351 * safely reset once the mount completes.
353 dentry
->d_flags
&= ~DCACHE_NEED_AUTOMOUNT
;
354 error
= -zfsctl_mount_snapshot(path
, 0);
355 dentry
->d_flags
|= DCACHE_NEED_AUTOMOUNT
;
357 return ERR_PTR(error
);
360 * Rather than returning the new vfsmount for the snapshot we must
361 * return NULL to indicate a mount collision. This is done because
362 * the user space mount calls do_add_mount() which adds the vfsmount
363 * to the name space. If we returned the new mount here it would be
364 * added again to the vfsmount list resulting in list corruption.
368 #endif /* HAVE_AUTOMOUNT */
371 * Get snapshot directory attributes.
375 zpl_snapdir_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
378 zfs_sb_t
*zsb
= ITOZSB(dentry
->d_inode
);
382 error
= simple_getattr(mnt
, dentry
, stat
);
383 stat
->nlink
= stat
->size
= avl_numnodes(&zsb
->z_ctldir_snaps
) + 2;
384 stat
->ctime
= stat
->mtime
= dmu_objset_snap_cmtime(zsb
->z_os
);
385 stat
->atime
= CURRENT_TIME
;
392 * The '.zfs/snapshot' directory file operations. These mainly control
393 * generating the list of available snapshots when doing an 'ls' in the
394 * directory. See zpl_snapdir_readdir().
396 const struct file_operations zpl_fops_snapdir
= {
397 .open
= zpl_common_open
,
398 .llseek
= generic_file_llseek
,
399 .read
= generic_read_dir
,
400 .readdir
= zpl_snapdir_readdir
,
404 * The '.zfs/snapshot' directory inode operations. These mainly control
405 * creating an inode for a snapshot directory and initializing the needed
406 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
408 const struct inode_operations zpl_ops_snapdir
= {
409 .lookup
= zpl_snapdir_lookup
,
410 .getattr
= zpl_snapdir_getattr
,
411 .rename
= zpl_snapdir_rename
,
412 .rmdir
= zpl_snapdir_rmdir
,
413 .mkdir
= zpl_snapdir_mkdir
,
416 #ifdef HAVE_AUTOMOUNT
417 const struct dentry_operations zpl_dops_snapdirs
= {
418 .d_automount
= zpl_snapdir_automount
,
420 #endif /* HAVE_AUTOMOUNT */
422 static struct dentry
*
423 #ifdef HAVE_LOOKUP_NAMEIDATA
424 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
425 struct nameidata
*nd
)
427 zpl_shares_lookup(struct inode
*dip
, struct dentry
*dentry
,
432 struct inode
*ip
= NULL
;
436 error
= -zfsctl_shares_lookup(dip
, dname(dentry
), &ip
,
438 ASSERT3S(error
, <=, 0);
442 if (error
== -ENOENT
)
443 return d_splice_alias(NULL
, dentry
);
445 return ERR_PTR(error
);
448 return d_splice_alias(ip
, dentry
);
453 zpl_shares_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
456 struct dentry
*dentry
= filp
->f_path
.dentry
;
457 struct inode
*ip
= dentry
->d_inode
;
458 zfs_sb_t
*zsb
= ITOZSB(ip
);
464 if (zsb
->z_shares_dir
== 0) {
465 error
= zpl_common_readdir(filp
, dirent
, filldir
);
470 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
477 error
= -zfs_readdir(ZTOI(dzp
), dirent
, filldir
, &filp
->f_pos
, cr
);
482 ASSERT3S(error
, <=, 0);
489 zpl_shares_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
492 struct inode
*ip
= dentry
->d_inode
;
493 zfs_sb_t
*zsb
= ITOZSB(ip
);
499 if (zsb
->z_shares_dir
== 0) {
500 error
= simple_getattr(mnt
, dentry
, stat
);
501 stat
->nlink
= stat
->size
= 2;
502 stat
->atime
= CURRENT_TIME
;
507 error
= -zfs_zget(zsb
, zsb
->z_shares_dir
, &dzp
);
509 error
= -zfs_getattr_fast(dentry
->d_inode
, stat
);
513 ASSERT3S(error
, <=, 0);
519 * The '.zfs/shares' directory file operations.
521 const struct file_operations zpl_fops_shares
= {
522 .open
= zpl_common_open
,
523 .llseek
= generic_file_llseek
,
524 .read
= generic_read_dir
,
525 .readdir
= zpl_shares_readdir
,
529 * The '.zfs/shares' directory inode operations.
531 const struct inode_operations zpl_ops_shares
= {
532 .lookup
= zpl_shares_lookup
,
533 .getattr
= zpl_shares_getattr
,