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>
37 static struct dentry
*
38 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
44 fstrans_cookie_t cookie
;
45 pathname_t
*ppn
= NULL
;
48 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
50 if (dlen(dentry
) >= ZAP_MAXNAMELEN
)
51 return (ERR_PTR(-ENAMETOOLONG
));
54 cookie
= spl_fstrans_mark();
56 /* If we are a case insensitive fs, we need the real name */
57 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
58 zfs_flags
= FIGNORECASE
;
63 error
= -zfs_lookup(ITOZ(dir
), dname(dentry
), &zp
,
64 zfs_flags
, cr
, NULL
, ppn
);
65 spl_fstrans_unmark(cookie
);
66 ASSERT3S(error
, <=, 0);
69 spin_lock(&dentry
->d_lock
);
70 dentry
->d_time
= jiffies
;
71 spin_unlock(&dentry
->d_lock
);
75 * If we have a case sensitive fs, we do not want to
76 * insert negative entries, so return NULL for ENOENT.
77 * Fall through if the error is not ENOENT. Also free memory.
86 return (d_splice_alias(NULL
, dentry
));
88 return (ERR_PTR(error
));
93 * If we are case insensitive, call the correct function
94 * to install the name.
97 struct dentry
*new_dentry
;
100 if (strcmp(dname(dentry
), pn
.pn_buf
) == 0) {
101 new_dentry
= d_splice_alias(ip
, dentry
);
103 ci_name
.name
= pn
.pn_buf
;
104 ci_name
.len
= strlen(pn
.pn_buf
);
105 new_dentry
= d_add_ci(dentry
, ip
, &ci_name
);
110 return (d_splice_alias(ip
, dentry
));
115 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, umode_t mode
, cred_t
*cr
)
117 vap
->va_mask
= ATTR_MODE
;
119 vap
->va_uid
= crgetuid(cr
);
121 if (dir
&& dir
->i_mode
& S_ISGID
) {
122 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
124 vap
->va_mode
|= S_ISGID
;
126 vap
->va_gid
= crgetgid(cr
);
131 #ifdef HAVE_IOPS_CREATE_USERNS
132 zpl_create(struct user_namespace
*user_ns
, struct inode
*dir
,
133 struct dentry
*dentry
, umode_t mode
, bool flag
)
135 zpl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, bool flag
)
142 fstrans_cookie_t cookie
;
145 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
146 zpl_vap_init(vap
, dir
, mode
, cr
);
148 cookie
= spl_fstrans_mark();
149 error
= -zfs_create(ITOZ(dir
), dname(dentry
), vap
, 0,
150 mode
, &zp
, cr
, 0, NULL
);
152 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
154 error
= zpl_init_acl(ZTOI(zp
), dir
);
157 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
158 remove_inode_hash(ZTOI(zp
));
161 d_instantiate(dentry
, ZTOI(zp
));
165 spl_fstrans_unmark(cookie
);
166 kmem_free(vap
, sizeof (vattr_t
));
168 ASSERT3S(error
, <=, 0);
174 #ifdef HAVE_IOPS_MKNOD_USERNS
175 zpl_mknod(struct user_namespace
*user_ns
, struct inode
*dir
,
176 struct dentry
*dentry
, umode_t mode
,
178 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
186 fstrans_cookie_t cookie
;
189 * We currently expect Linux to supply rdev=0 for all sockets
190 * and fifos, but we want to know if this behavior ever changes.
192 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
196 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
197 zpl_vap_init(vap
, dir
, mode
, cr
);
200 cookie
= spl_fstrans_mark();
201 error
= -zfs_create(ITOZ(dir
), dname(dentry
), vap
, 0,
202 mode
, &zp
, cr
, 0, NULL
);
204 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
206 error
= zpl_init_acl(ZTOI(zp
), dir
);
209 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
210 remove_inode_hash(ZTOI(zp
));
213 d_instantiate(dentry
, ZTOI(zp
));
217 spl_fstrans_unmark(cookie
);
218 kmem_free(vap
, sizeof (vattr_t
));
220 ASSERT3S(error
, <=, 0);
227 #ifdef HAVE_TMPFILE_USERNS
228 zpl_tmpfile(struct user_namespace
*userns
, struct inode
*dir
,
229 struct dentry
*dentry
, umode_t mode
)
231 zpl_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
238 fstrans_cookie_t cookie
;
241 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
243 * The VFS does not apply the umask, therefore it is applied here
244 * when POSIX ACLs are not enabled.
246 if (!IS_POSIXACL(dir
))
247 mode
&= ~current_umask();
248 zpl_vap_init(vap
, dir
, mode
, cr
);
250 cookie
= spl_fstrans_mark();
251 error
= -zfs_tmpfile(dir
, vap
, 0, mode
, &ip
, cr
, 0, NULL
);
253 /* d_tmpfile will do drop_nlink, so we should set it first */
255 d_tmpfile(dentry
, ip
);
257 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
259 error
= zpl_init_acl(ip
, dir
);
261 * don't need to handle error here, file is already in
266 spl_fstrans_unmark(cookie
);
267 kmem_free(vap
, sizeof (vattr_t
));
269 ASSERT3S(error
, <=, 0);
276 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
280 fstrans_cookie_t cookie
;
281 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
284 cookie
= spl_fstrans_mark();
285 error
= -zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
288 * For a CI FS we must invalidate the dentry to prevent the
289 * creation of negative entries.
291 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
292 d_invalidate(dentry
);
294 spl_fstrans_unmark(cookie
);
296 ASSERT3S(error
, <=, 0);
302 #ifdef HAVE_IOPS_MKDIR_USERNS
303 zpl_mkdir(struct user_namespace
*user_ns
, struct inode
*dir
,
304 struct dentry
*dentry
, umode_t mode
)
306 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
313 fstrans_cookie_t cookie
;
316 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
317 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
);
319 cookie
= spl_fstrans_mark();
320 error
= -zfs_mkdir(ITOZ(dir
), dname(dentry
), vap
, &zp
, cr
, 0, NULL
);
322 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
324 error
= zpl_init_acl(ZTOI(zp
), dir
);
327 (void) zfs_rmdir(ITOZ(dir
), dname(dentry
), NULL
, cr
, 0);
328 remove_inode_hash(ZTOI(zp
));
331 d_instantiate(dentry
, ZTOI(zp
));
335 spl_fstrans_unmark(cookie
);
336 kmem_free(vap
, sizeof (vattr_t
));
338 ASSERT3S(error
, <=, 0);
344 zpl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
348 fstrans_cookie_t cookie
;
349 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
352 cookie
= spl_fstrans_mark();
353 error
= -zfs_rmdir(ITOZ(dir
), dname(dentry
), NULL
, cr
, 0);
356 * For a CI FS we must invalidate the dentry to prevent the
357 * creation of negative entries.
359 if (error
== 0 && zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
)
360 d_invalidate(dentry
);
362 spl_fstrans_unmark(cookie
);
364 ASSERT3S(error
, <=, 0);
370 #ifdef HAVE_USERNS_IOPS_GETATTR
371 zpl_getattr_impl(struct user_namespace
*user_ns
,
372 const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
373 unsigned int query_flags
)
375 zpl_getattr_impl(const struct path
*path
, struct kstat
*stat
, u32 request_mask
,
376 unsigned int query_flags
)
380 fstrans_cookie_t cookie
;
381 struct inode
*ip
= path
->dentry
->d_inode
;
382 znode_t
*zp __maybe_unused
= ITOZ(ip
);
384 cookie
= spl_fstrans_mark();
387 * XXX query_flags currently ignored.
390 #ifdef HAVE_USERNS_IOPS_GETATTR
391 error
= -zfs_getattr_fast(user_ns
, ip
, stat
);
393 error
= -zfs_getattr_fast(kcred
->user_ns
, ip
, stat
);
397 if (request_mask
& STATX_BTIME
) {
398 stat
->btime
= zp
->z_btime
;
399 stat
->result_mask
|= STATX_BTIME
;
403 #ifdef STATX_ATTR_IMMUTABLE
404 if (zp
->z_pflags
& ZFS_IMMUTABLE
)
405 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
406 stat
->attributes_mask
|= STATX_ATTR_IMMUTABLE
;
409 #ifdef STATX_ATTR_APPEND
410 if (zp
->z_pflags
& ZFS_APPENDONLY
)
411 stat
->attributes
|= STATX_ATTR_APPEND
;
412 stat
->attributes_mask
|= STATX_ATTR_APPEND
;
415 #ifdef STATX_ATTR_NODUMP
416 if (zp
->z_pflags
& ZFS_NODUMP
)
417 stat
->attributes
|= STATX_ATTR_NODUMP
;
418 stat
->attributes_mask
|= STATX_ATTR_NODUMP
;
421 spl_fstrans_unmark(cookie
);
422 ASSERT3S(error
, <=, 0);
426 ZPL_GETATTR_WRAPPER(zpl_getattr
);
429 #ifdef HAVE_SETATTR_PREPARE_USERNS
430 zpl_setattr(struct user_namespace
*user_ns
, struct dentry
*dentry
,
433 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
436 struct inode
*ip
= dentry
->d_inode
;
440 fstrans_cookie_t cookie
;
442 error
= zpl_setattr_prepare(kcred
->user_ns
, dentry
, ia
);
447 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
448 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
449 vap
->va_mode
= ia
->ia_mode
;
450 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
451 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
452 vap
->va_size
= ia
->ia_size
;
453 vap
->va_atime
= ia
->ia_atime
;
454 vap
->va_mtime
= ia
->ia_mtime
;
455 vap
->va_ctime
= ia
->ia_ctime
;
457 if (vap
->va_mask
& ATTR_ATIME
)
458 ip
->i_atime
= zpl_inode_timestamp_truncate(ia
->ia_atime
, ip
);
460 cookie
= spl_fstrans_mark();
461 error
= -zfs_setattr(ITOZ(ip
), vap
, 0, cr
);
462 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
463 error
= zpl_chmod_acl(ip
);
465 spl_fstrans_unmark(cookie
);
466 kmem_free(vap
, sizeof (vattr_t
));
468 ASSERT3S(error
, <=, 0);
474 #ifdef HAVE_IOPS_RENAME_USERNS
475 zpl_rename2(struct user_namespace
*user_ns
, struct inode
*sdip
,
476 struct dentry
*sdentry
, struct inode
*tdip
, struct dentry
*tdentry
,
479 zpl_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
480 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
485 fstrans_cookie_t cookie
;
487 /* We don't have renameat2(2) support */
492 cookie
= spl_fstrans_mark();
493 error
= -zfs_rename(ITOZ(sdip
), dname(sdentry
), ITOZ(tdip
),
494 dname(tdentry
), cr
, 0);
495 spl_fstrans_unmark(cookie
);
497 ASSERT3S(error
, <=, 0);
502 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
504 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
505 struct inode
*tdip
, struct dentry
*tdentry
)
507 return (zpl_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
512 #ifdef HAVE_IOPS_SYMLINK_USERNS
513 zpl_symlink(struct user_namespace
*user_ns
, struct inode
*dir
,
514 struct dentry
*dentry
, const char *name
)
516 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
523 fstrans_cookie_t cookie
;
526 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
527 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
529 cookie
= spl_fstrans_mark();
530 error
= -zfs_symlink(ITOZ(dir
), dname(dentry
), vap
,
531 (char *)name
, &zp
, cr
, 0);
533 error
= zpl_xattr_security_init(ZTOI(zp
), dir
, &dentry
->d_name
);
535 (void) zfs_remove(ITOZ(dir
), dname(dentry
), cr
, 0);
536 remove_inode_hash(ZTOI(zp
));
539 d_instantiate(dentry
, ZTOI(zp
));
543 spl_fstrans_unmark(cookie
);
544 kmem_free(vap
, sizeof (vattr_t
));
546 ASSERT3S(error
, <=, 0);
551 #if defined(HAVE_PUT_LINK_COOKIE)
553 zpl_put_link(struct inode
*unused
, void *cookie
)
555 kmem_free(cookie
, MAXPATHLEN
);
557 #elif defined(HAVE_PUT_LINK_NAMEIDATA)
559 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
561 const char *link
= nd_get_link(nd
);
564 kmem_free(link
, MAXPATHLEN
);
566 #elif defined(HAVE_PUT_LINK_DELAYED)
568 zpl_put_link(void *ptr
)
570 kmem_free(ptr
, MAXPATHLEN
);
575 zpl_get_link_common(struct dentry
*dentry
, struct inode
*ip
, char **link
)
577 fstrans_cookie_t cookie
;
585 iov
.iov_len
= MAXPATHLEN
;
586 iov
.iov_base
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
589 zfs_uio_iovec_init(&uio
, &iov
, 1, 0, UIO_SYSSPACE
, MAXPATHLEN
- 1, 0);
591 cookie
= spl_fstrans_mark();
592 error
= -zfs_readlink(ip
, &uio
, cr
);
593 spl_fstrans_unmark(cookie
);
597 kmem_free(iov
.iov_base
, MAXPATHLEN
);
599 *link
= iov
.iov_base
;
604 #if defined(HAVE_GET_LINK_DELAYED)
606 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
,
607 struct delayed_call
*done
)
613 return (ERR_PTR(-ECHILD
));
615 error
= zpl_get_link_common(dentry
, inode
, &link
);
617 return (ERR_PTR(error
));
619 set_delayed_call(done
, zpl_put_link
, link
);
623 #elif defined(HAVE_GET_LINK_COOKIE)
625 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
, void **cookie
)
631 return (ERR_PTR(-ECHILD
));
633 error
= zpl_get_link_common(dentry
, inode
, &link
);
635 return (ERR_PTR(error
));
637 return (*cookie
= link
);
639 #elif defined(HAVE_FOLLOW_LINK_COOKIE)
641 zpl_follow_link(struct dentry
*dentry
, void **cookie
)
646 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
648 return (ERR_PTR(error
));
650 return (*cookie
= link
);
652 #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA)
654 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
659 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
661 nd_set_link(nd
, ERR_PTR(error
));
663 nd_set_link(nd
, link
);
670 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
673 struct inode
*ip
= old_dentry
->d_inode
;
675 fstrans_cookie_t cookie
;
677 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
681 ip
->i_ctime
= current_time(ip
);
682 /* Must have an existing ref, so igrab() cannot return NULL */
683 VERIFY3P(igrab(ip
), !=, NULL
);
685 cookie
= spl_fstrans_mark();
686 error
= -zfs_link(ITOZ(dir
), ITOZ(ip
), dname(dentry
), cr
, 0);
692 d_instantiate(dentry
, ip
);
694 spl_fstrans_unmark(cookie
);
696 ASSERT3S(error
, <=, 0);
702 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
703 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
705 unsigned int flags
= (nd
? nd
->flags
: 0);
707 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
709 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
711 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
714 if (flags
& LOOKUP_RCU
)
718 * After a rollback negative dentries created before the rollback
719 * time must be invalidated. Otherwise they can obscure files which
720 * are only present in the rolled back dataset.
722 if (dentry
->d_inode
== NULL
) {
723 spin_lock(&dentry
->d_lock
);
724 error
= time_before(dentry
->d_time
, zfsvfs
->z_rollback_time
);
725 spin_unlock(&dentry
->d_lock
);
732 * The dentry may reference a stale inode if a mounted file system
733 * was rolled back to a point in time where the object didn't exist.
735 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
741 const struct inode_operations zpl_inode_operations
= {
742 .setattr
= zpl_setattr
,
743 .getattr
= zpl_getattr
,
744 #ifdef HAVE_GENERIC_SETXATTR
745 .setxattr
= generic_setxattr
,
746 .getxattr
= generic_getxattr
,
747 .removexattr
= generic_removexattr
,
749 .listxattr
= zpl_xattr_list
,
750 #if defined(CONFIG_FS_POSIX_ACL)
751 #if defined(HAVE_SET_ACL)
752 .set_acl
= zpl_set_acl
,
753 #endif /* HAVE_SET_ACL */
754 .get_acl
= zpl_get_acl
,
755 #endif /* CONFIG_FS_POSIX_ACL */
758 const struct inode_operations zpl_dir_inode_operations
= {
759 .create
= zpl_create
,
760 .lookup
= zpl_lookup
,
762 .unlink
= zpl_unlink
,
763 .symlink
= zpl_symlink
,
767 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
768 .rename
= zpl_rename2
,
770 .rename
= zpl_rename
,
773 .tmpfile
= zpl_tmpfile
,
775 .setattr
= zpl_setattr
,
776 .getattr
= zpl_getattr
,
777 #ifdef HAVE_GENERIC_SETXATTR
778 .setxattr
= generic_setxattr
,
779 .getxattr
= generic_getxattr
,
780 .removexattr
= generic_removexattr
,
782 .listxattr
= zpl_xattr_list
,
783 #if defined(CONFIG_FS_POSIX_ACL)
784 #if defined(HAVE_SET_ACL)
785 .set_acl
= zpl_set_acl
,
786 #endif /* HAVE_SET_ACL */
787 .get_acl
= zpl_get_acl
,
788 #endif /* CONFIG_FS_POSIX_ACL */
791 const struct inode_operations zpl_symlink_inode_operations
= {
792 #ifdef HAVE_GENERIC_READLINK
793 .readlink
= generic_readlink
,
795 #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE)
796 .get_link
= zpl_get_link
,
797 #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA)
798 .follow_link
= zpl_follow_link
,
800 #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA)
801 .put_link
= zpl_put_link
,
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
,
813 const struct inode_operations zpl_special_inode_operations
= {
814 .setattr
= zpl_setattr
,
815 .getattr
= zpl_getattr
,
816 #ifdef HAVE_GENERIC_SETXATTR
817 .setxattr
= generic_setxattr
,
818 .getxattr
= generic_getxattr
,
819 .removexattr
= generic_removexattr
,
821 .listxattr
= zpl_xattr_list
,
822 #if defined(CONFIG_FS_POSIX_ACL)
823 #if defined(HAVE_SET_ACL)
824 .set_acl
= zpl_set_acl
,
825 #endif /* HAVE_SET_ACL */
826 .get_acl
= zpl_get_acl
,
827 #endif /* CONFIG_FS_POSIX_ACL */
830 dentry_operations_t zpl_dentry_operations
= {
831 .d_revalidate
= zpl_revalidate
,