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 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
53 if (dlen(dentry
) >= ZAP_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 (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
61 zfs_flags
= FIGNORECASE
;
66 error
= -zfs_lookup(dir
, dname(dentry
), &ip
, zfs_flags
, cr
, NULL
, ppn
);
67 spl_fstrans_unmark(cookie
);
68 ASSERT3S(error
, <=, 0);
71 spin_lock(&dentry
->d_lock
);
72 dentry
->d_time
= jiffies
;
74 d_set_d_op(dentry
, &zpl_dentry_operations
);
75 #endif /* HAVE_S_D_OP */
76 spin_unlock(&dentry
->d_lock
);
80 * If we have a case sensitive fs, we do not want to
81 * insert negative entries, so return NULL for ENOENT.
82 * Fall through if the error is not ENOENT. Also free memory.
91 return (d_splice_alias(NULL
, dentry
));
93 return (ERR_PTR(error
));
97 * If we are case insensitive, call the correct function
98 * to install the name.
101 struct dentry
*new_dentry
;
104 if (strcmp(dname(dentry
), pn
.pn_buf
) == 0) {
105 new_dentry
= d_splice_alias(ip
, dentry
);
107 ci_name
.name
= pn
.pn_buf
;
108 ci_name
.len
= strlen(pn
.pn_buf
);
109 new_dentry
= d_add_ci(dentry
, ip
, &ci_name
);
114 return (d_splice_alias(ip
, dentry
));
119 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, zpl_umode_t mode
, cred_t
*cr
)
121 vap
->va_mask
= ATTR_MODE
;
123 vap
->va_uid
= crgetfsuid(cr
);
125 if (dir
&& dir
->i_mode
& S_ISGID
) {
126 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
128 vap
->va_mode
|= S_ISGID
;
130 vap
->va_gid
= crgetfsgid(cr
);
135 #ifdef HAVE_CREATE_NAMEIDATA
136 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
137 struct nameidata
*nd
)
139 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
147 fstrans_cookie_t cookie
;
150 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
151 zpl_vap_init(vap
, dir
, mode
, cr
);
153 cookie
= spl_fstrans_mark();
154 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
156 d_instantiate(dentry
, ip
);
158 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
160 error
= zpl_init_acl(ip
, dir
);
163 (void) zfs_remove(dir
, dname(dentry
), cr
, 0);
166 spl_fstrans_unmark(cookie
);
167 kmem_free(vap
, sizeof (vattr_t
));
169 ASSERT3S(error
, <=, 0);
175 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
182 fstrans_cookie_t cookie
;
185 * We currently expect Linux to supply rdev=0 for all sockets
186 * and fifos, but we want to know if this behavior ever changes.
188 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
192 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
193 zpl_vap_init(vap
, dir
, mode
, cr
);
196 cookie
= spl_fstrans_mark();
197 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
199 d_instantiate(dentry
, ip
);
201 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
203 error
= zpl_init_acl(ip
, dir
);
206 (void) zfs_remove(dir
, dname(dentry
), cr
, 0);
209 spl_fstrans_unmark(cookie
);
210 kmem_free(vap
, sizeof (vattr_t
));
212 ASSERT3S(error
, <=, 0);
219 zpl_tmpfile(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
)
225 fstrans_cookie_t cookie
;
228 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
229 zpl_vap_init(vap
, dir
, mode
, cr
);
231 cookie
= spl_fstrans_mark();
232 error
= -zfs_tmpfile(dir
, vap
, 0, mode
, &ip
, cr
, 0, NULL
);
234 /* d_tmpfile will do drop_nlink, so we should set it first */
236 d_tmpfile(dentry
, ip
);
238 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
240 error
= zpl_init_acl(ip
, dir
);
242 * don't need to handle error here, file is already in
247 spl_fstrans_unmark(cookie
);
248 kmem_free(vap
, sizeof (vattr_t
));
250 ASSERT3S(error
, <=, 0);
257 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
261 fstrans_cookie_t cookie
;
262 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
265 cookie
= spl_fstrans_mark();
266 error
= -zfs_remove(dir
, dname(dentry
), cr
, 0);
269 * For a CI FS we must invalidate the dentry to prevent the
270 * creation of negative entries.
272 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
273 d_invalidate(dentry
);
275 spl_fstrans_unmark(cookie
);
277 ASSERT3S(error
, <=, 0);
283 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
)
289 fstrans_cookie_t cookie
;
292 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
293 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
);
295 cookie
= spl_fstrans_mark();
296 error
= -zfs_mkdir(dir
, dname(dentry
), vap
, &ip
, cr
, 0, NULL
);
298 d_instantiate(dentry
, ip
);
300 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
302 error
= zpl_init_acl(ip
, dir
);
305 (void) zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
308 spl_fstrans_unmark(cookie
);
309 kmem_free(vap
, sizeof (vattr_t
));
311 ASSERT3S(error
, <=, 0);
317 zpl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
321 fstrans_cookie_t cookie
;
322 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
325 cookie
= spl_fstrans_mark();
326 error
= -zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
329 * For a CI FS we must invalidate the dentry to prevent the
330 * creation of negative entries.
332 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
333 d_invalidate(dentry
);
335 spl_fstrans_unmark(cookie
);
337 ASSERT3S(error
, <=, 0);
343 zpl_getattr_impl(const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
344 unsigned int query_flags
)
347 fstrans_cookie_t cookie
;
349 cookie
= spl_fstrans_mark();
352 * XXX request_mask and query_flags currently ignored.
355 error
= -zfs_getattr_fast(path
->dentry
->d_inode
, stat
);
356 spl_fstrans_unmark(cookie
);
357 ASSERT3S(error
, <=, 0);
361 ZPL_GETATTR_WRAPPER(zpl_getattr
);
364 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
366 struct inode
*ip
= dentry
->d_inode
;
370 fstrans_cookie_t cookie
;
372 error
= setattr_prepare(dentry
, ia
);
377 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
378 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
379 vap
->va_mode
= ia
->ia_mode
;
380 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
381 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
382 vap
->va_size
= ia
->ia_size
;
383 vap
->va_atime
= ia
->ia_atime
;
384 vap
->va_mtime
= ia
->ia_mtime
;
385 vap
->va_ctime
= ia
->ia_ctime
;
387 if (vap
->va_mask
& ATTR_ATIME
) {
388 ip
->i_atime
= zpl_inode_timespec_trunc(ia
->ia_atime
,
389 ip
->i_sb
->s_time_gran
);
392 cookie
= spl_fstrans_mark();
393 error
= -zfs_setattr(ip
, vap
, 0, cr
);
394 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
395 error
= zpl_chmod_acl(ip
);
397 spl_fstrans_unmark(cookie
);
398 kmem_free(vap
, sizeof (vattr_t
));
400 ASSERT3S(error
, <=, 0);
406 zpl_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
407 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
411 fstrans_cookie_t cookie
;
413 /* We don't have renameat2(2) support */
418 cookie
= spl_fstrans_mark();
419 error
= -zfs_rename(sdip
, dname(sdentry
), tdip
, dname(tdentry
), cr
, 0);
420 spl_fstrans_unmark(cookie
);
422 ASSERT3S(error
, <=, 0);
427 #ifndef HAVE_RENAME_WANTS_FLAGS
429 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
430 struct inode
*tdip
, struct dentry
*tdentry
)
432 return (zpl_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
437 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
443 fstrans_cookie_t cookie
;
446 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
447 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
449 cookie
= spl_fstrans_mark();
450 error
= -zfs_symlink(dir
, dname(dentry
), vap
, (char *)name
, &ip
, cr
, 0);
452 d_instantiate(dentry
, ip
);
454 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
456 (void) zfs_remove(dir
, dname(dentry
), cr
, 0);
459 spl_fstrans_unmark(cookie
);
460 kmem_free(vap
, sizeof (vattr_t
));
462 ASSERT3S(error
, <=, 0);
467 #if defined(HAVE_PUT_LINK_COOKIE)
469 zpl_put_link(struct inode
*unused
, void *cookie
)
471 kmem_free(cookie
, MAXPATHLEN
);
473 #elif defined(HAVE_PUT_LINK_NAMEIDATA)
475 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
477 const char *link
= nd_get_link(nd
);
480 kmem_free(link
, MAXPATHLEN
);
482 #elif defined(HAVE_PUT_LINK_DELAYED)
484 zpl_put_link(void *ptr
)
486 kmem_free(ptr
, MAXPATHLEN
);
491 zpl_get_link_common(struct dentry
*dentry
, struct inode
*ip
, char **link
)
493 fstrans_cookie_t cookie
;
501 iov
.iov_len
= MAXPATHLEN
;
502 iov
.iov_base
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
507 uio
.uio_resid
= (MAXPATHLEN
- 1);
508 uio
.uio_segflg
= UIO_SYSSPACE
;
510 cookie
= spl_fstrans_mark();
511 error
= -zfs_readlink(ip
, &uio
, cr
);
512 spl_fstrans_unmark(cookie
);
516 kmem_free(iov
.iov_base
, MAXPATHLEN
);
518 *link
= iov
.iov_base
;
523 #if defined(HAVE_GET_LINK_DELAYED)
525 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
,
526 struct delayed_call
*done
)
532 return (ERR_PTR(-ECHILD
));
534 error
= zpl_get_link_common(dentry
, inode
, &link
);
536 return (ERR_PTR(error
));
538 set_delayed_call(done
, zpl_put_link
, link
);
542 #elif defined(HAVE_GET_LINK_COOKIE)
544 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
, void **cookie
)
550 return (ERR_PTR(-ECHILD
));
552 error
= zpl_get_link_common(dentry
, inode
, &link
);
554 return (ERR_PTR(error
));
556 return (*cookie
= link
);
558 #elif defined(HAVE_FOLLOW_LINK_COOKIE)
560 zpl_follow_link(struct dentry
*dentry
, void **cookie
)
565 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
567 return (ERR_PTR(error
));
569 return (*cookie
= link
);
571 #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA)
573 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
578 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
580 nd_set_link(nd
, ERR_PTR(error
));
582 nd_set_link(nd
, link
);
589 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
592 struct inode
*ip
= old_dentry
->d_inode
;
594 fstrans_cookie_t cookie
;
596 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
600 ip
->i_ctime
= current_time(ip
);
601 igrab(ip
); /* Use ihold() if available */
603 cookie
= spl_fstrans_mark();
604 error
= -zfs_link(dir
, ip
, dname(dentry
), cr
, 0);
610 d_instantiate(dentry
, ip
);
612 spl_fstrans_unmark(cookie
);
614 ASSERT3S(error
, <=, 0);
619 #ifdef HAVE_INODE_TRUNCATE_RANGE
621 zpl_truncate_range(struct inode
*ip
, loff_t start
, loff_t end
)
625 fstrans_cookie_t cookie
;
627 ASSERT3S(start
, <=, end
);
630 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
631 * the end of the file". We don't want that.
641 bf
.l_len
= end
- start
;
643 cookie
= spl_fstrans_mark();
644 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
645 spl_fstrans_unmark(cookie
);
649 #endif /* HAVE_INODE_TRUNCATE_RANGE */
651 #ifdef HAVE_INODE_FALLOCATE
653 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
655 return (zpl_fallocate_common(ip
, mode
, offset
, len
));
657 #endif /* HAVE_INODE_FALLOCATE */
660 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
661 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
663 unsigned int flags
= (nd
? nd
->flags
: 0);
665 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
667 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
669 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
672 if (flags
& LOOKUP_RCU
)
676 * Automounted snapshots rely on periodic dentry revalidation
677 * to defer snapshots from being automatically unmounted.
679 if (zfsvfs
->z_issnap
) {
680 if (time_after(jiffies
, zfsvfs
->z_snap_defer_time
+
681 MAX(zfs_expire_snapshot
* HZ
/ 2, HZ
))) {
682 zfsvfs
->z_snap_defer_time
= jiffies
;
683 zfsctl_snapshot_unmount_delay(zfsvfs
->z_os
->os_spa
,
684 dmu_objset_id(zfsvfs
->z_os
), zfs_expire_snapshot
);
689 * After a rollback negative dentries created before the rollback
690 * time must be invalidated. Otherwise they can obscure files which
691 * are only present in the rolled back dataset.
693 if (dentry
->d_inode
== NULL
) {
694 spin_lock(&dentry
->d_lock
);
695 error
= time_before(dentry
->d_time
, zfsvfs
->z_rollback_time
);
696 spin_unlock(&dentry
->d_lock
);
703 * The dentry may reference a stale inode if a mounted file system
704 * was rolled back to a point in time where the object didn't exist.
706 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
712 const struct inode_operations zpl_inode_operations
= {
713 .setattr
= zpl_setattr
,
714 .getattr
= zpl_getattr
,
715 #ifdef HAVE_GENERIC_SETXATTR
716 .setxattr
= generic_setxattr
,
717 .getxattr
= generic_getxattr
,
718 .removexattr
= generic_removexattr
,
720 .listxattr
= zpl_xattr_list
,
721 #ifdef HAVE_INODE_TRUNCATE_RANGE
722 .truncate_range
= zpl_truncate_range
,
723 #endif /* HAVE_INODE_TRUNCATE_RANGE */
724 #ifdef HAVE_INODE_FALLOCATE
725 .fallocate
= zpl_fallocate
,
726 #endif /* HAVE_INODE_FALLOCATE */
727 #if defined(CONFIG_FS_POSIX_ACL)
728 #if defined(HAVE_SET_ACL)
729 .set_acl
= zpl_set_acl
,
731 #if defined(HAVE_GET_ACL)
732 .get_acl
= zpl_get_acl
,
733 #elif defined(HAVE_CHECK_ACL)
734 .check_acl
= zpl_check_acl
,
735 #elif defined(HAVE_PERMISSION)
736 .permission
= zpl_permission
,
737 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
738 #endif /* CONFIG_FS_POSIX_ACL */
741 const struct inode_operations zpl_dir_inode_operations
= {
742 .create
= zpl_create
,
743 .lookup
= zpl_lookup
,
745 .unlink
= zpl_unlink
,
746 .symlink
= zpl_symlink
,
750 #ifdef HAVE_RENAME_WANTS_FLAGS
751 .rename
= zpl_rename2
,
753 .rename
= zpl_rename
,
756 .tmpfile
= zpl_tmpfile
,
758 .setattr
= zpl_setattr
,
759 .getattr
= zpl_getattr
,
760 #ifdef HAVE_GENERIC_SETXATTR
761 .setxattr
= generic_setxattr
,
762 .getxattr
= generic_getxattr
,
763 .removexattr
= generic_removexattr
,
765 .listxattr
= zpl_xattr_list
,
766 #if defined(CONFIG_FS_POSIX_ACL)
767 #if defined(HAVE_SET_ACL)
768 .set_acl
= zpl_set_acl
,
770 #if defined(HAVE_GET_ACL)
771 .get_acl
= zpl_get_acl
,
772 #elif defined(HAVE_CHECK_ACL)
773 .check_acl
= zpl_check_acl
,
774 #elif defined(HAVE_PERMISSION)
775 .permission
= zpl_permission
,
776 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
777 #endif /* CONFIG_FS_POSIX_ACL */
780 const struct inode_operations zpl_symlink_inode_operations
= {
781 #ifdef HAVE_GENERIC_READLINK
782 .readlink
= generic_readlink
,
784 #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE)
785 .get_link
= zpl_get_link
,
786 #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA)
787 .follow_link
= zpl_follow_link
,
789 #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA)
790 .put_link
= zpl_put_link
,
792 .setattr
= zpl_setattr
,
793 .getattr
= zpl_getattr
,
794 #ifdef HAVE_GENERIC_SETXATTR
795 .setxattr
= generic_setxattr
,
796 .getxattr
= generic_getxattr
,
797 .removexattr
= generic_removexattr
,
799 .listxattr
= zpl_xattr_list
,
802 const struct inode_operations zpl_special_inode_operations
= {
803 .setattr
= zpl_setattr
,
804 .getattr
= zpl_getattr
,
805 #ifdef HAVE_GENERIC_SETXATTR
806 .setxattr
= generic_setxattr
,
807 .getxattr
= generic_getxattr
,
808 .removexattr
= generic_removexattr
,
810 .listxattr
= zpl_xattr_list
,
811 #if defined(CONFIG_FS_POSIX_ACL)
812 #if defined(HAVE_SET_ACL)
813 .set_acl
= zpl_set_acl
,
815 #if defined(HAVE_GET_ACL)
816 .get_acl
= zpl_get_acl
,
817 #elif defined(HAVE_CHECK_ACL)
818 .check_acl
= zpl_check_acl
,
819 #elif defined(HAVE_PERMISSION)
820 .permission
= zpl_permission
,
821 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
822 #endif /* CONFIG_FS_POSIX_ACL */
825 dentry_operations_t zpl_dentry_operations
= {
826 .d_revalidate
= zpl_revalidate
,