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 https://opensource.org/licenses/CDDL-1.0.
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>
36 static struct dentry
*
37 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
43 fstrans_cookie_t cookie
;
44 pathname_t
*ppn
= NULL
;
47 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
49 if (dlen(dentry
) >= ZAP_MAXNAMELEN
)
50 return (ERR_PTR(-ENAMETOOLONG
));
53 cookie
= spl_fstrans_mark();
55 /* If we are a case insensitive fs, we need the real name */
56 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
57 zfs_flags
= FIGNORECASE
;
62 error
= -zfs_lookup(ITOZ(dir
), dname(dentry
), &zp
,
63 zfs_flags
, cr
, NULL
, ppn
);
64 spl_fstrans_unmark(cookie
);
65 ASSERT3S(error
, <=, 0);
68 spin_lock(&dentry
->d_lock
);
69 dentry
->d_time
= jiffies
;
70 spin_unlock(&dentry
->d_lock
);
74 * If we have a case sensitive fs, we do not want to
75 * insert negative entries, so return NULL for ENOENT.
76 * Fall through if the error is not ENOENT. Also free memory.
85 return (d_splice_alias(NULL
, dentry
));
87 return (ERR_PTR(error
));
92 * If we are case insensitive, call the correct function
93 * to install the name.
96 struct dentry
*new_dentry
;
99 if (strcmp(dname(dentry
), pn
.pn_buf
) == 0) {
100 new_dentry
= d_splice_alias(ip
, dentry
);
102 ci_name
.name
= pn
.pn_buf
;
103 ci_name
.len
= strlen(pn
.pn_buf
);
104 new_dentry
= d_add_ci(dentry
, ip
, &ci_name
);
109 return (d_splice_alias(ip
, dentry
));
114 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, umode_t mode
, cred_t
*cr
,
117 vap
->va_mask
= ATTR_MODE
;
120 vap
->va_uid
= zfs_uid_from_mnt((struct user_namespace
*)mnt_ns
,
123 if (dir
&& dir
->i_mode
& S_ISGID
) {
124 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
126 vap
->va_mode
|= S_ISGID
;
128 vap
->va_gid
= zfs_gid_from_mnt((struct user_namespace
*)mnt_ns
,
134 #ifdef HAVE_IOPS_CREATE_USERNS
135 zpl_create(struct user_namespace
*user_ns
, struct inode
*dir
,
136 struct dentry
*dentry
, umode_t mode
, bool flag
)
138 zpl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, bool flag
)
145 fstrans_cookie_t cookie
;
146 #ifndef HAVE_IOPS_CREATE_USERNS
147 zuserns_t
*user_ns
= NULL
;
151 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
152 zpl_vap_init(vap
, dir
, mode
, cr
, user_ns
);
154 cookie
= spl_fstrans_mark();
155 error
= -zfs_create(ITOZ(dir
), dname(dentry
), vap
, 0,
156 mode
, &zp
, cr
, 0, NULL
, user_ns
);
158 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
160 error
= zpl_init_acl(ZTOI(zp
), dir
);
163 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
164 remove_inode_hash(ZTOI(zp
));
167 d_instantiate(dentry
, ZTOI(zp
));
171 spl_fstrans_unmark(cookie
);
172 kmem_free(vap
, sizeof (vattr_t
));
174 ASSERT3S(error
, <=, 0);
180 #ifdef HAVE_IOPS_MKNOD_USERNS
181 zpl_mknod(struct user_namespace
*user_ns
, struct inode
*dir
,
182 struct dentry
*dentry
, umode_t mode
,
184 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
192 fstrans_cookie_t cookie
;
193 #ifndef HAVE_IOPS_MKNOD_USERNS
194 zuserns_t
*user_ns
= NULL
;
198 * We currently expect Linux to supply rdev=0 for all sockets
199 * and fifos, but we want to know if this behavior ever changes.
201 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
205 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
206 zpl_vap_init(vap
, dir
, mode
, cr
, user_ns
);
209 cookie
= spl_fstrans_mark();
210 error
= -zfs_create(ITOZ(dir
), dname(dentry
), vap
, 0,
211 mode
, &zp
, cr
, 0, NULL
, user_ns
);
213 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
215 error
= zpl_init_acl(ZTOI(zp
), dir
);
218 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
219 remove_inode_hash(ZTOI(zp
));
222 d_instantiate(dentry
, ZTOI(zp
));
226 spl_fstrans_unmark(cookie
);
227 kmem_free(vap
, sizeof (vattr_t
));
229 ASSERT3S(error
, <=, 0);
236 #ifdef HAVE_TMPFILE_USERNS
237 zpl_tmpfile(struct user_namespace
*userns
, struct inode
*dir
,
238 struct dentry
*dentry
, umode_t mode
)
240 zpl_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
247 fstrans_cookie_t cookie
;
248 #ifndef HAVE_TMPFILE_USERNS
249 zuserns_t
*userns
= NULL
;
253 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
255 * The VFS does not apply the umask, therefore it is applied here
256 * when POSIX ACLs are not enabled.
258 if (!IS_POSIXACL(dir
))
259 mode
&= ~current_umask();
260 zpl_vap_init(vap
, dir
, mode
, cr
, userns
);
262 cookie
= spl_fstrans_mark();
263 error
= -zfs_tmpfile(dir
, vap
, 0, mode
, &ip
, cr
, 0, NULL
, userns
);
265 /* d_tmpfile will do drop_nlink, so we should set it first */
267 d_tmpfile(dentry
, ip
);
269 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
271 error
= zpl_init_acl(ip
, dir
);
273 * don't need to handle error here, file is already in
278 spl_fstrans_unmark(cookie
);
279 kmem_free(vap
, sizeof (vattr_t
));
281 ASSERT3S(error
, <=, 0);
288 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
292 fstrans_cookie_t cookie
;
293 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
296 cookie
= spl_fstrans_mark();
297 error
= -zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
300 * For a CI FS we must invalidate the dentry to prevent the
301 * creation of negative entries.
303 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
304 d_invalidate(dentry
);
306 spl_fstrans_unmark(cookie
);
308 ASSERT3S(error
, <=, 0);
314 #ifdef HAVE_IOPS_MKDIR_USERNS
315 zpl_mkdir(struct user_namespace
*user_ns
, struct inode
*dir
,
316 struct dentry
*dentry
, umode_t mode
)
318 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
325 fstrans_cookie_t cookie
;
326 #ifndef HAVE_IOPS_MKDIR_USERNS
327 zuserns_t
*user_ns
= NULL
;
331 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
332 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
, user_ns
);
334 cookie
= spl_fstrans_mark();
335 error
= -zfs_mkdir(ITOZ(dir
), dname(dentry
), vap
, &zp
, cr
, 0, NULL
,
338 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
340 error
= zpl_init_acl(ZTOI(zp
), dir
);
343 (void) zfs_rmdir(ITOZ(dir
), dname(dentry
), NULL
, cr
, 0);
344 remove_inode_hash(ZTOI(zp
));
347 d_instantiate(dentry
, ZTOI(zp
));
351 spl_fstrans_unmark(cookie
);
352 kmem_free(vap
, sizeof (vattr_t
));
354 ASSERT3S(error
, <=, 0);
360 zpl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
364 fstrans_cookie_t cookie
;
365 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
368 cookie
= spl_fstrans_mark();
369 error
= -zfs_rmdir(ITOZ(dir
), dname(dentry
), NULL
, cr
, 0);
372 * For a CI FS we must invalidate the dentry to prevent the
373 * creation of negative entries.
375 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
376 d_invalidate(dentry
);
378 spl_fstrans_unmark(cookie
);
380 ASSERT3S(error
, <=, 0);
386 #ifdef HAVE_USERNS_IOPS_GETATTR
387 zpl_getattr_impl(struct user_namespace
*user_ns
,
388 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
389 unsigned int query_flags
)
391 zpl_getattr_impl(const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
392 unsigned int query_flags
)
396 fstrans_cookie_t cookie
;
397 struct inode
*ip
= path
->dentry
->d_inode
;
398 znode_t
*zp __maybe_unused
= ITOZ(ip
);
400 cookie
= spl_fstrans_mark();
403 * XXX query_flags currently ignored.
406 #ifdef HAVE_USERNS_IOPS_GETATTR
407 error
= -zfs_getattr_fast(user_ns
, ip
, stat
);
409 error
= -zfs_getattr_fast(kcred
->user_ns
, ip
, stat
);
413 if (request_mask
& STATX_BTIME
) {
414 stat
->btime
= zp
->z_btime
;
415 stat
->result_mask
|= STATX_BTIME
;
419 #ifdef STATX_ATTR_IMMUTABLE
420 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
421 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
422 stat
->attributes_mask
|= STATX_ATTR_IMMUTABLE
;
425 #ifdef STATX_ATTR_APPEND
426 if (zp
->z_pflags
& ZFS_APPENDONLY
)
427 stat
->attributes
|= STATX_ATTR_APPEND
;
428 stat
->attributes_mask
|= STATX_ATTR_APPEND
;
431 #ifdef STATX_ATTR_NODUMP
432 if (zp
->z_pflags
& ZFS_NODUMP
)
433 stat
->attributes
|= STATX_ATTR_NODUMP
;
434 stat
->attributes_mask
|= STATX_ATTR_NODUMP
;
437 spl_fstrans_unmark(cookie
);
438 ASSERT3S(error
, <=, 0);
442 ZPL_GETATTR_WRAPPER(zpl_getattr
);
445 #ifdef HAVE_SETATTR_PREPARE_USERNS
446 zpl_setattr(struct user_namespace
*user_ns
, struct dentry
*dentry
,
449 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
452 struct inode
*ip
= dentry
->d_inode
;
456 fstrans_cookie_t cookie
;
458 #ifdef HAVE_SETATTR_PREPARE_USERNS
459 error
= zpl_setattr_prepare(user_ns
, dentry
, ia
);
461 error
= zpl_setattr_prepare(kcred
->user_ns
, dentry
, ia
);
467 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
468 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
469 vap
->va_mode
= ia
->ia_mode
;
470 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
471 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
472 vap
->va_size
= ia
->ia_size
;
473 vap
->va_atime
= ia
->ia_atime
;
474 vap
->va_mtime
= ia
->ia_mtime
;
475 vap
->va_ctime
= ia
->ia_ctime
;
477 if (vap
->va_mask
& ATTR_ATIME
)
478 ip
->i_atime
= zpl_inode_timestamp_truncate(ia
->ia_atime
, ip
);
480 cookie
= spl_fstrans_mark();
481 #ifdef HAVE_SETATTR_PREPARE_USERNS
482 error
= -zfs_setattr(ITOZ(ip
), vap
, 0, cr
, user_ns
);
484 error
= -zfs_setattr(ITOZ(ip
), vap
, 0, cr
, NULL
);
486 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
487 error
= zpl_chmod_acl(ip
);
489 spl_fstrans_unmark(cookie
);
490 kmem_free(vap
, sizeof (vattr_t
));
492 ASSERT3S(error
, <=, 0);
498 #ifdef HAVE_IOPS_RENAME_USERNS
499 zpl_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
500 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
503 zpl_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
504 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
509 fstrans_cookie_t cookie
;
510 #ifndef HAVE_IOPS_RENAME_USERNS
511 zuserns_t
*user_ns
= NULL
;
514 /* We don't have renameat2(2) support */
519 cookie
= spl_fstrans_mark();
520 error
= -zfs_rename(ITOZ(sdip
), dname(sdentry
), ITOZ(tdip
),
521 dname(tdentry
), cr
, 0, user_ns
);
522 spl_fstrans_unmark(cookie
);
524 ASSERT3S(error
, <=, 0);
529 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
531 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
532 struct inode
*tdip
, struct dentry
*tdentry
)
534 return (zpl_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
539 #ifdef HAVE_IOPS_SYMLINK_USERNS
540 zpl_symlink(struct user_namespace
*user_ns
, struct inode
*dir
,
541 struct dentry
*dentry
, const char *name
)
543 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
550 fstrans_cookie_t cookie
;
551 #ifndef HAVE_IOPS_SYMLINK_USERNS
552 zuserns_t
*user_ns
= NULL
;
556 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
557 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
, user_ns
);
559 cookie
= spl_fstrans_mark();
560 error
= -zfs_symlink(ITOZ(dir
), dname(dentry
), vap
,
561 (char *)name
, &zp
, cr
, 0, user_ns
);
563 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
565 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
566 remove_inode_hash(ZTOI(zp
));
569 d_instantiate(dentry
, ZTOI(zp
));
573 spl_fstrans_unmark(cookie
);
574 kmem_free(vap
, sizeof (vattr_t
));
576 ASSERT3S(error
, <=, 0);
581 #if defined(HAVE_PUT_LINK_COOKIE)
583 zpl_put_link(struct inode
*unused
, void *cookie
)
585 kmem_free(cookie
, MAXPATHLEN
);
587 #elif defined(HAVE_PUT_LINK_NAMEIDATA)
589 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
591 const char *link
= nd_get_link(nd
);
594 kmem_free(link
, MAXPATHLEN
);
596 #elif defined(HAVE_PUT_LINK_DELAYED)
598 zpl_put_link(void *ptr
)
600 kmem_free(ptr
, MAXPATHLEN
);
605 zpl_get_link_common(struct dentry
*dentry
, struct inode
*ip
, char **link
)
607 fstrans_cookie_t cookie
;
615 iov
.iov_len
= MAXPATHLEN
;
616 iov
.iov_base
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
619 zfs_uio_iovec_init(&uio
, &iov
, 1, 0, UIO_SYSSPACE
, MAXPATHLEN
- 1, 0);
621 cookie
= spl_fstrans_mark();
622 error
= -zfs_readlink(ip
, &uio
, cr
);
623 spl_fstrans_unmark(cookie
);
627 kmem_free(iov
.iov_base
, MAXPATHLEN
);
629 *link
= iov
.iov_base
;
634 #if defined(HAVE_GET_LINK_DELAYED)
636 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
,
637 struct delayed_call
*done
)
643 return (ERR_PTR(-ECHILD
));
645 error
= zpl_get_link_common(dentry
, inode
, &link
);
647 return (ERR_PTR(error
));
649 set_delayed_call(done
, zpl_put_link
, link
);
653 #elif defined(HAVE_GET_LINK_COOKIE)
655 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
, void **cookie
)
661 return (ERR_PTR(-ECHILD
));
663 error
= zpl_get_link_common(dentry
, inode
, &link
);
665 return (ERR_PTR(error
));
667 return (*cookie
= link
);
669 #elif defined(HAVE_FOLLOW_LINK_COOKIE)
671 zpl_follow_link(struct dentry
*dentry
, void **cookie
)
676 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
678 return (ERR_PTR(error
));
680 return (*cookie
= link
);
682 #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA)
684 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
689 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
691 nd_set_link(nd
, ERR_PTR(error
));
693 nd_set_link(nd
, link
);
700 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
703 struct inode
*ip
= old_dentry
->d_inode
;
705 fstrans_cookie_t cookie
;
707 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
711 ip
->i_ctime
= current_time(ip
);
712 /* Must have an existing ref, so igrab() cannot return NULL */
713 VERIFY3P(igrab(ip
), !=, NULL
);
715 cookie
= spl_fstrans_mark();
716 error
= -zfs_link(ITOZ(dir
), ITOZ(ip
), dname(dentry
), cr
, 0);
722 d_instantiate(dentry
, ip
);
724 spl_fstrans_unmark(cookie
);
726 ASSERT3S(error
, <=, 0);
731 const struct inode_operations zpl_inode_operations
= {
732 .setattr
= zpl_setattr
,
733 .getattr
= zpl_getattr
,
734 #ifdef HAVE_GENERIC_SETXATTR
735 .setxattr
= generic_setxattr
,
736 .getxattr
= generic_getxattr
,
737 .removexattr
= generic_removexattr
,
739 .listxattr
= zpl_xattr_list
,
740 #if defined(CONFIG_FS_POSIX_ACL)
741 #if defined(HAVE_SET_ACL)
742 .set_acl
= zpl_set_acl
,
743 #endif /* HAVE_SET_ACL */
744 .get_acl
= zpl_get_acl
,
745 #endif /* CONFIG_FS_POSIX_ACL */
748 const struct inode_operations zpl_dir_inode_operations
= {
749 .create
= zpl_create
,
750 .lookup
= zpl_lookup
,
752 .unlink
= zpl_unlink
,
753 .symlink
= zpl_symlink
,
757 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
758 .rename
= zpl_rename2
,
760 .rename
= zpl_rename
,
763 .tmpfile
= zpl_tmpfile
,
765 .setattr
= zpl_setattr
,
766 .getattr
= zpl_getattr
,
767 #ifdef HAVE_GENERIC_SETXATTR
768 .setxattr
= generic_setxattr
,
769 .getxattr
= generic_getxattr
,
770 .removexattr
= generic_removexattr
,
772 .listxattr
= zpl_xattr_list
,
773 #if defined(CONFIG_FS_POSIX_ACL)
774 #if defined(HAVE_SET_ACL)
775 .set_acl
= zpl_set_acl
,
776 #endif /* HAVE_SET_ACL */
777 .get_acl
= zpl_get_acl
,
778 #endif /* CONFIG_FS_POSIX_ACL */
781 const struct inode_operations zpl_symlink_inode_operations
= {
782 #ifdef HAVE_GENERIC_READLINK
783 .readlink
= generic_readlink
,
785 #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE)
786 .get_link
= zpl_get_link
,
787 #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA)
788 .follow_link
= zpl_follow_link
,
790 #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA)
791 .put_link
= zpl_put_link
,
793 .setattr
= zpl_setattr
,
794 .getattr
= zpl_getattr
,
795 #ifdef HAVE_GENERIC_SETXATTR
796 .setxattr
= generic_setxattr
,
797 .getxattr
= generic_getxattr
,
798 .removexattr
= generic_removexattr
,
800 .listxattr
= zpl_xattr_list
,
803 const struct inode_operations zpl_special_inode_operations
= {
804 .setattr
= zpl_setattr
,
805 .getattr
= zpl_getattr
,
806 #ifdef HAVE_GENERIC_SETXATTR
807 .setxattr
= generic_setxattr
,
808 .getxattr
= generic_getxattr
,
809 .removexattr
= generic_removexattr
,
811 .listxattr
= zpl_xattr_list
,
812 #if defined(CONFIG_FS_POSIX_ACL)
813 #if defined(HAVE_SET_ACL)
814 .set_acl
= zpl_set_acl
,
815 #endif /* HAVE_SET_ACL */
816 .get_acl
= zpl_get_acl
,
817 #endif /* CONFIG_FS_POSIX_ACL */