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 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
27 #include <sys/zfs_ctldir.h>
28 #include <sys/zfs_vfsops.h>
29 #include <sys/zfs_vnops.h>
30 #include <sys/zfs_znode.h>
31 #include <sys/dmu_objset.h>
37 static struct dentry
*
38 #ifdef HAVE_LOOKUP_NAMEIDATA
39 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
41 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
47 fstrans_cookie_t cookie
;
48 pathname_t
*ppn
= NULL
;
51 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
53 if (dlen(dentry
) > ZFS_MAXNAMELEN
)
54 return (ERR_PTR(-ENAMETOOLONG
));
57 cookie
= spl_fstrans_mark();
59 /* If we are a case insensitive fs, we need the real name */
60 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
) {
61 zfs_flags
= FIGNORECASE
;
62 pn
.pn_bufsize
= ZFS_MAXNAMELEN
;
63 pn
.pn_buf
= kmem_zalloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
67 error
= -zfs_lookup(dir
, dname(dentry
), &ip
, zfs_flags
, cr
, NULL
, ppn
);
68 spl_fstrans_unmark(cookie
);
69 ASSERT3S(error
, <=, 0);
72 spin_lock(&dentry
->d_lock
);
73 dentry
->d_time
= jiffies
;
75 d_set_d_op(dentry
, &zpl_dentry_operations
);
76 #endif /* HAVE_S_D_OP */
77 spin_unlock(&dentry
->d_lock
);
81 * If we have a case sensitive fs, we do not want to
82 * insert negative entries, so return NULL for ENOENT.
83 * Fall through if the error is not ENOENT. Also free memory.
86 kmem_free(pn
.pn_buf
, ZFS_MAXNAMELEN
);
92 return (d_splice_alias(NULL
, dentry
));
94 return (ERR_PTR(error
));
98 * If we are case insensitive, call the correct function
99 * to install the name.
102 struct dentry
*new_dentry
;
105 if (strcmp(dname(dentry
), pn
.pn_buf
) == 0) {
106 new_dentry
= d_splice_alias(ip
, dentry
);
108 ci_name
.name
= pn
.pn_buf
;
109 ci_name
.len
= strlen(pn
.pn_buf
);
110 new_dentry
= d_add_ci(dentry
, ip
, &ci_name
);
112 kmem_free(pn
.pn_buf
, ZFS_MAXNAMELEN
);
115 return (d_splice_alias(ip
, dentry
));
120 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, zpl_umode_t mode
, cred_t
*cr
)
122 vap
->va_mask
= ATTR_MODE
;
124 vap
->va_uid
= crgetfsuid(cr
);
126 if (dir
&& dir
->i_mode
& S_ISGID
) {
127 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
129 vap
->va_mode
|= S_ISGID
;
131 vap
->va_gid
= crgetfsgid(cr
);
136 #ifdef HAVE_CREATE_NAMEIDATA
137 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
138 struct nameidata
*nd
)
140 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
148 fstrans_cookie_t cookie
;
151 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
152 zpl_vap_init(vap
, dir
, mode
, cr
);
154 cookie
= spl_fstrans_mark();
155 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
157 d_instantiate(dentry
, ip
);
159 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
161 error
= zpl_init_acl(ip
, dir
);
164 (void) zfs_remove(dir
, dname(dentry
), cr
);
167 spl_fstrans_unmark(cookie
);
168 kmem_free(vap
, sizeof (vattr_t
));
170 ASSERT3S(error
, <=, 0);
176 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
183 fstrans_cookie_t cookie
;
186 * We currently expect Linux to supply rdev=0 for all sockets
187 * and fifos, but we want to know if this behavior ever changes.
189 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
193 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
194 zpl_vap_init(vap
, dir
, mode
, cr
);
197 cookie
= spl_fstrans_mark();
198 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
200 d_instantiate(dentry
, ip
);
202 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
204 error
= zpl_init_acl(ip
, dir
);
207 (void) zfs_remove(dir
, dname(dentry
), cr
);
210 spl_fstrans_unmark(cookie
);
211 kmem_free(vap
, sizeof (vattr_t
));
213 ASSERT3S(error
, <=, 0);
219 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
223 fstrans_cookie_t cookie
;
224 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
227 cookie
= spl_fstrans_mark();
228 error
= -zfs_remove(dir
, dname(dentry
), cr
);
231 * For a CI FS we must invalidate the dentry to prevent the
232 * creation of negative entries.
234 if (error
== 0 && zsb
->z_case
== ZFS_CASE_INSENSITIVE
)
235 d_invalidate(dentry
);
237 spl_fstrans_unmark(cookie
);
239 ASSERT3S(error
, <=, 0);
245 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
)
251 fstrans_cookie_t cookie
;
254 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
255 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
);
257 cookie
= spl_fstrans_mark();
258 error
= -zfs_mkdir(dir
, dname(dentry
), vap
, &ip
, cr
, 0, NULL
);
260 d_instantiate(dentry
, ip
);
262 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
264 error
= zpl_init_acl(ip
, dir
);
267 (void) zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
270 spl_fstrans_unmark(cookie
);
271 kmem_free(vap
, sizeof (vattr_t
));
273 ASSERT3S(error
, <=, 0);
279 zpl_rmdir(struct inode
* dir
, struct dentry
*dentry
)
283 fstrans_cookie_t cookie
;
284 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
287 cookie
= spl_fstrans_mark();
288 error
= -zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
291 * For a CI FS we must invalidate the dentry to prevent the
292 * creation of negative entries.
294 if (error
== 0 && zsb
->z_case
== ZFS_CASE_INSENSITIVE
)
295 d_invalidate(dentry
);
297 spl_fstrans_unmark(cookie
);
299 ASSERT3S(error
, <=, 0);
305 zpl_getattr_impl(const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
306 unsigned int query_flags
)
309 fstrans_cookie_t cookie
;
311 cookie
= spl_fstrans_mark();
314 * XXX request_mask and query_flags currently ignored.
317 error
= -zfs_getattr_fast(path
->dentry
->d_inode
, stat
);
318 spl_fstrans_unmark(cookie
);
319 ASSERT3S(error
, <=, 0);
323 ZPL_GETATTR_WRAPPER(zpl_getattr
);
326 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
328 struct inode
*ip
= dentry
->d_inode
;
332 fstrans_cookie_t cookie
;
334 error
= setattr_prepare(dentry
, ia
);
339 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
340 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
341 vap
->va_mode
= ia
->ia_mode
;
342 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
343 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
344 vap
->va_size
= ia
->ia_size
;
345 vap
->va_atime
= ia
->ia_atime
;
346 vap
->va_mtime
= ia
->ia_mtime
;
347 vap
->va_ctime
= ia
->ia_ctime
;
349 if (vap
->va_mask
& ATTR_ATIME
)
350 ip
->i_atime
= ia
->ia_atime
;
352 cookie
= spl_fstrans_mark();
353 error
= -zfs_setattr(ip
, vap
, 0, cr
);
354 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
355 error
= zpl_chmod_acl(ip
);
357 spl_fstrans_unmark(cookie
);
358 kmem_free(vap
, sizeof (vattr_t
));
360 ASSERT3S(error
, <=, 0);
366 zpl_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
367 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
371 fstrans_cookie_t cookie
;
373 /* We don't have renameat2(2) support */
378 cookie
= spl_fstrans_mark();
379 error
= -zfs_rename(sdip
, dname(sdentry
), tdip
, dname(tdentry
), cr
, 0);
380 spl_fstrans_unmark(cookie
);
382 ASSERT3S(error
, <=, 0);
387 #ifndef HAVE_RENAME_WANTS_FLAGS
389 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
390 struct inode
*tdip
, struct dentry
*tdentry
)
392 return (zpl_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
397 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
403 fstrans_cookie_t cookie
;
406 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
407 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
409 cookie
= spl_fstrans_mark();
410 error
= -zfs_symlink(dir
, dname(dentry
), vap
, (char *)name
, &ip
, cr
, 0);
412 d_instantiate(dentry
, ip
);
414 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
416 (void) zfs_remove(dir
, dname(dentry
), cr
);
419 spl_fstrans_unmark(cookie
);
420 kmem_free(vap
, sizeof (vattr_t
));
422 ASSERT3S(error
, <=, 0);
427 #if defined(HAVE_PUT_LINK_COOKIE)
429 zpl_put_link(struct inode
*unused
, void *cookie
)
431 kmem_free(cookie
, MAXPATHLEN
);
433 #elif defined(HAVE_PUT_LINK_NAMEIDATA)
435 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
437 const char *link
= nd_get_link(nd
);
440 kmem_free(link
, MAXPATHLEN
);
442 #elif defined(HAVE_PUT_LINK_DELAYED)
444 zpl_put_link(void *ptr
)
446 kmem_free(ptr
, MAXPATHLEN
);
451 zpl_get_link_common(struct dentry
*dentry
, struct inode
*ip
, char **link
)
453 fstrans_cookie_t cookie
;
461 iov
.iov_len
= MAXPATHLEN
;
462 iov
.iov_base
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
467 uio
.uio_resid
= (MAXPATHLEN
- 1);
468 uio
.uio_segflg
= UIO_SYSSPACE
;
470 cookie
= spl_fstrans_mark();
471 error
= -zfs_readlink(ip
, &uio
, cr
);
472 spl_fstrans_unmark(cookie
);
476 kmem_free(iov
.iov_base
, MAXPATHLEN
);
478 *link
= iov
.iov_base
;
483 #if defined(HAVE_GET_LINK_DELAYED)
485 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
,
486 struct delayed_call
*done
)
492 return (ERR_PTR(-ECHILD
));
494 error
= zpl_get_link_common(dentry
, inode
, &link
);
496 return (ERR_PTR(error
));
498 set_delayed_call(done
, zpl_put_link
, link
);
502 #elif defined(HAVE_GET_LINK_COOKIE)
504 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
, void **cookie
)
510 return (ERR_PTR(-ECHILD
));
512 error
= zpl_get_link_common(dentry
, inode
, &link
);
514 return (ERR_PTR(error
));
516 return (*cookie
= link
);
518 #elif defined(HAVE_FOLLOW_LINK_COOKIE)
520 zpl_follow_link(struct dentry
*dentry
, void **cookie
)
525 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
527 return (ERR_PTR(error
));
529 return (*cookie
= link
);
531 #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA)
533 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
538 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
540 nd_set_link(nd
, ERR_PTR(error
));
542 nd_set_link(nd
, link
);
549 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
552 struct inode
*ip
= old_dentry
->d_inode
;
554 fstrans_cookie_t cookie
;
556 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
560 ip
->i_ctime
= CURRENT_TIME_SEC
;
561 igrab(ip
); /* Use ihold() if available */
563 cookie
= spl_fstrans_mark();
564 error
= -zfs_link(dir
, ip
, dname(dentry
), cr
);
570 d_instantiate(dentry
, ip
);
572 spl_fstrans_unmark(cookie
);
574 ASSERT3S(error
, <=, 0);
579 #ifdef HAVE_INODE_TRUNCATE_RANGE
581 zpl_truncate_range(struct inode
*ip
, loff_t start
, loff_t end
)
585 fstrans_cookie_t cookie
;
587 ASSERT3S(start
, <=, end
);
590 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
591 * the end of the file". We don't want that.
601 bf
.l_len
= end
- start
;
603 cookie
= spl_fstrans_mark();
604 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
605 spl_fstrans_unmark(cookie
);
609 #endif /* HAVE_INODE_TRUNCATE_RANGE */
611 #ifdef HAVE_INODE_FALLOCATE
613 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
615 return (zpl_fallocate_common(ip
, mode
, offset
, len
));
617 #endif /* HAVE_INODE_FALLOCATE */
620 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
621 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
623 unsigned int flags
= (nd
? nd
->flags
: 0);
625 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
627 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
628 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
631 if (flags
& LOOKUP_RCU
)
635 * Automounted snapshots rely on periodic dentry revalidation
636 * to defer snapshots from being automatically unmounted.
639 if (time_after(jiffies
, zsb
->z_snap_defer_time
+
640 MAX(zfs_expire_snapshot
* HZ
/ 2, HZ
))) {
641 zsb
->z_snap_defer_time
= jiffies
;
642 zfsctl_snapshot_unmount_delay(zsb
->z_os
->os_spa
,
643 dmu_objset_id(zsb
->z_os
), zfs_expire_snapshot
);
648 * After a rollback negative dentries created before the rollback
649 * time must be invalidated. Otherwise they can obscure files which
650 * are only present in the rolled back dataset.
652 if (dentry
->d_inode
== NULL
) {
653 spin_lock(&dentry
->d_lock
);
654 error
= time_before(dentry
->d_time
, zsb
->z_rollback_time
);
655 spin_unlock(&dentry
->d_lock
);
662 * The dentry may reference a stale inode if a mounted file system
663 * was rolled back to a point in time where the object didn't exist.
665 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
671 const struct inode_operations zpl_inode_operations
= {
672 .setattr
= zpl_setattr
,
673 .getattr
= zpl_getattr
,
674 #ifdef HAVE_GENERIC_SETXATTR
675 .setxattr
= generic_setxattr
,
676 .getxattr
= generic_getxattr
,
677 .removexattr
= generic_removexattr
,
679 .listxattr
= zpl_xattr_list
,
680 #ifdef HAVE_INODE_TRUNCATE_RANGE
681 .truncate_range
= zpl_truncate_range
,
682 #endif /* HAVE_INODE_TRUNCATE_RANGE */
683 #ifdef HAVE_INODE_FALLOCATE
684 .fallocate
= zpl_fallocate
,
685 #endif /* HAVE_INODE_FALLOCATE */
686 #if defined(CONFIG_FS_POSIX_ACL)
687 #if defined(HAVE_SET_ACL)
688 .set_acl
= zpl_set_acl
,
690 #if defined(HAVE_GET_ACL)
691 .get_acl
= zpl_get_acl
,
692 #elif defined(HAVE_CHECK_ACL)
693 .check_acl
= zpl_check_acl
,
694 #elif defined(HAVE_PERMISSION)
695 .permission
= zpl_permission
,
696 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
697 #endif /* CONFIG_FS_POSIX_ACL */
700 const struct inode_operations zpl_dir_inode_operations
= {
701 .create
= zpl_create
,
702 .lookup
= zpl_lookup
,
704 .unlink
= zpl_unlink
,
705 .symlink
= zpl_symlink
,
709 #ifdef HAVE_RENAME_WANTS_FLAGS
710 .rename
= zpl_rename2
,
712 .rename
= zpl_rename
,
714 .setattr
= zpl_setattr
,
715 .getattr
= zpl_getattr
,
716 #ifdef HAVE_GENERIC_SETXATTR
717 .setxattr
= generic_setxattr
,
718 .getxattr
= generic_getxattr
,
719 .removexattr
= generic_removexattr
,
721 .listxattr
= zpl_xattr_list
,
722 #if defined(CONFIG_FS_POSIX_ACL)
723 #if defined(HAVE_SET_ACL)
724 .set_acl
= zpl_set_acl
,
726 #if defined(HAVE_GET_ACL)
727 .get_acl
= zpl_get_acl
,
728 #elif defined(HAVE_CHECK_ACL)
729 .check_acl
= zpl_check_acl
,
730 #elif defined(HAVE_PERMISSION)
731 .permission
= zpl_permission
,
732 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
733 #endif /* CONFIG_FS_POSIX_ACL */
736 const struct inode_operations zpl_symlink_inode_operations
= {
737 #ifdef HAVE_GENERIC_READLINK
738 .readlink
= generic_readlink
,
740 #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE)
741 .get_link
= zpl_get_link
,
742 #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA)
743 .follow_link
= zpl_follow_link
,
745 #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA)
746 .put_link
= zpl_put_link
,
748 .setattr
= zpl_setattr
,
749 .getattr
= zpl_getattr
,
750 #ifdef HAVE_GENERIC_SETXATTR
751 .setxattr
= generic_setxattr
,
752 .getxattr
= generic_getxattr
,
753 .removexattr
= generic_removexattr
,
755 .listxattr
= zpl_xattr_list
,
758 const struct inode_operations zpl_special_inode_operations
= {
759 .setattr
= zpl_setattr
,
760 .getattr
= zpl_getattr
,
761 #ifdef HAVE_GENERIC_SETXATTR
762 .setxattr
= generic_setxattr
,
763 .getxattr
= generic_getxattr
,
764 .removexattr
= generic_removexattr
,
766 .listxattr
= zpl_xattr_list
,
767 #if defined(CONFIG_FS_POSIX_ACL)
768 #if defined(HAVE_SET_ACL)
769 .set_acl
= zpl_set_acl
,
771 #if defined(HAVE_GET_ACL)
772 .get_acl
= zpl_get_acl
,
773 #elif defined(HAVE_CHECK_ACL)
774 .check_acl
= zpl_check_acl
,
775 #elif defined(HAVE_PERMISSION)
776 .permission
= zpl_permission
,
777 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
778 #endif /* CONFIG_FS_POSIX_ACL */
781 dentry_operations_t zpl_dentry_operations
= {
782 .d_revalidate
= zpl_revalidate
,