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(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
308 fstrans_cookie_t cookie
;
310 cookie
= spl_fstrans_mark();
311 error
= -zfs_getattr_fast(dentry
->d_inode
, stat
);
312 spl_fstrans_unmark(cookie
);
313 ASSERT3S(error
, <=, 0);
319 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
321 struct inode
*ip
= dentry
->d_inode
;
325 fstrans_cookie_t cookie
;
327 error
= setattr_prepare(dentry
, ia
);
332 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
333 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
334 vap
->va_mode
= ia
->ia_mode
;
335 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
336 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
337 vap
->va_size
= ia
->ia_size
;
338 vap
->va_atime
= ia
->ia_atime
;
339 vap
->va_mtime
= ia
->ia_mtime
;
340 vap
->va_ctime
= ia
->ia_ctime
;
342 if (vap
->va_mask
& ATTR_ATIME
)
343 ip
->i_atime
= ia
->ia_atime
;
345 cookie
= spl_fstrans_mark();
346 error
= -zfs_setattr(ip
, vap
, 0, cr
);
347 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
348 error
= zpl_chmod_acl(ip
);
350 spl_fstrans_unmark(cookie
);
351 kmem_free(vap
, sizeof (vattr_t
));
353 ASSERT3S(error
, <=, 0);
359 zpl_rename2(struct inode
*sdip
, struct dentry
*sdentry
,
360 struct inode
*tdip
, struct dentry
*tdentry
, unsigned int flags
)
364 fstrans_cookie_t cookie
;
366 /* We don't have renameat2(2) support */
371 cookie
= spl_fstrans_mark();
372 error
= -zfs_rename(sdip
, dname(sdentry
), tdip
, dname(tdentry
), cr
, 0);
373 spl_fstrans_unmark(cookie
);
375 ASSERT3S(error
, <=, 0);
380 #ifndef HAVE_RENAME_WANTS_FLAGS
382 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
383 struct inode
*tdip
, struct dentry
*tdentry
)
385 return (zpl_rename2(sdip
, sdentry
, tdip
, tdentry
, 0));
390 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
396 fstrans_cookie_t cookie
;
399 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
400 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
402 cookie
= spl_fstrans_mark();
403 error
= -zfs_symlink(dir
, dname(dentry
), vap
, (char *)name
, &ip
, cr
, 0);
405 d_instantiate(dentry
, ip
);
407 error
= zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
);
409 (void) zfs_remove(dir
, dname(dentry
), cr
);
412 spl_fstrans_unmark(cookie
);
413 kmem_free(vap
, sizeof (vattr_t
));
415 ASSERT3S(error
, <=, 0);
420 #if defined(HAVE_PUT_LINK_COOKIE)
422 zpl_put_link(struct inode
*unused
, void *cookie
)
424 kmem_free(cookie
, MAXPATHLEN
);
426 #elif defined(HAVE_PUT_LINK_NAMEIDATA)
428 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
430 const char *link
= nd_get_link(nd
);
433 kmem_free(link
, MAXPATHLEN
);
435 #elif defined(HAVE_PUT_LINK_DELAYED)
437 zpl_put_link(void *ptr
)
439 kmem_free(ptr
, MAXPATHLEN
);
444 zpl_get_link_common(struct dentry
*dentry
, struct inode
*ip
, char **link
)
446 fstrans_cookie_t cookie
;
454 iov
.iov_len
= MAXPATHLEN
;
455 iov
.iov_base
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
460 uio
.uio_resid
= (MAXPATHLEN
- 1);
461 uio
.uio_segflg
= UIO_SYSSPACE
;
463 cookie
= spl_fstrans_mark();
464 error
= -zfs_readlink(ip
, &uio
, cr
);
465 spl_fstrans_unmark(cookie
);
469 kmem_free(iov
.iov_base
, MAXPATHLEN
);
471 *link
= iov
.iov_base
;
476 #if defined(HAVE_GET_LINK_DELAYED)
478 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
,
479 struct delayed_call
*done
)
485 return (ERR_PTR(-ECHILD
));
487 error
= zpl_get_link_common(dentry
, inode
, &link
);
489 return (ERR_PTR(error
));
491 set_delayed_call(done
, zpl_put_link
, link
);
495 #elif defined(HAVE_GET_LINK_COOKIE)
497 zpl_get_link(struct dentry
*dentry
, struct inode
*inode
, void **cookie
)
503 return (ERR_PTR(-ECHILD
));
505 error
= zpl_get_link_common(dentry
, inode
, &link
);
507 return (ERR_PTR(error
));
509 return (*cookie
= link
);
511 #elif defined(HAVE_FOLLOW_LINK_COOKIE)
513 zpl_follow_link(struct dentry
*dentry
, void **cookie
)
518 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
520 return (ERR_PTR(error
));
522 return (*cookie
= link
);
524 #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA)
526 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
531 error
= zpl_get_link_common(dentry
, dentry
->d_inode
, &link
);
533 nd_set_link(nd
, ERR_PTR(error
));
535 nd_set_link(nd
, link
);
542 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
545 struct inode
*ip
= old_dentry
->d_inode
;
547 fstrans_cookie_t cookie
;
549 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
553 ip
->i_ctime
= CURRENT_TIME_SEC
;
554 igrab(ip
); /* Use ihold() if available */
556 cookie
= spl_fstrans_mark();
557 error
= -zfs_link(dir
, ip
, dname(dentry
), cr
);
563 d_instantiate(dentry
, ip
);
565 spl_fstrans_unmark(cookie
);
567 ASSERT3S(error
, <=, 0);
572 #ifdef HAVE_INODE_TRUNCATE_RANGE
574 zpl_truncate_range(struct inode
*ip
, loff_t start
, loff_t end
)
578 fstrans_cookie_t cookie
;
580 ASSERT3S(start
, <=, end
);
583 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
584 * the end of the file". We don't want that.
594 bf
.l_len
= end
- start
;
596 cookie
= spl_fstrans_mark();
597 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
598 spl_fstrans_unmark(cookie
);
602 #endif /* HAVE_INODE_TRUNCATE_RANGE */
604 #ifdef HAVE_INODE_FALLOCATE
606 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
608 return (zpl_fallocate_common(ip
, mode
, offset
, len
));
610 #endif /* HAVE_INODE_FALLOCATE */
613 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
614 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
616 unsigned int flags
= (nd
? nd
->flags
: 0);
618 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
620 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
621 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
624 if (flags
& LOOKUP_RCU
)
628 * Automounted snapshots rely on periodic dentry revalidation
629 * to defer snapshots from being automatically unmounted.
632 if (time_after(jiffies
, zsb
->z_snap_defer_time
+
633 MAX(zfs_expire_snapshot
* HZ
/ 2, HZ
))) {
634 zsb
->z_snap_defer_time
= jiffies
;
635 zfsctl_snapshot_unmount_delay(zsb
->z_os
->os_spa
,
636 dmu_objset_id(zsb
->z_os
), zfs_expire_snapshot
);
641 * After a rollback negative dentries created before the rollback
642 * time must be invalidated. Otherwise they can obscure files which
643 * are only present in the rolled back dataset.
645 if (dentry
->d_inode
== NULL
) {
646 spin_lock(&dentry
->d_lock
);
647 error
= time_before(dentry
->d_time
, zsb
->z_rollback_time
);
648 spin_unlock(&dentry
->d_lock
);
655 * The dentry may reference a stale inode if a mounted file system
656 * was rolled back to a point in time where the object didn't exist.
658 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
664 const struct inode_operations zpl_inode_operations
= {
665 .setattr
= zpl_setattr
,
666 .getattr
= zpl_getattr
,
667 #ifdef HAVE_GENERIC_SETXATTR
668 .setxattr
= generic_setxattr
,
669 .getxattr
= generic_getxattr
,
670 .removexattr
= generic_removexattr
,
672 .listxattr
= zpl_xattr_list
,
673 #ifdef HAVE_INODE_TRUNCATE_RANGE
674 .truncate_range
= zpl_truncate_range
,
675 #endif /* HAVE_INODE_TRUNCATE_RANGE */
676 #ifdef HAVE_INODE_FALLOCATE
677 .fallocate
= zpl_fallocate
,
678 #endif /* HAVE_INODE_FALLOCATE */
679 #if defined(CONFIG_FS_POSIX_ACL)
680 #if defined(HAVE_SET_ACL)
681 .set_acl
= zpl_set_acl
,
683 #if defined(HAVE_GET_ACL)
684 .get_acl
= zpl_get_acl
,
685 #elif defined(HAVE_CHECK_ACL)
686 .check_acl
= zpl_check_acl
,
687 #elif defined(HAVE_PERMISSION)
688 .permission
= zpl_permission
,
689 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
690 #endif /* CONFIG_FS_POSIX_ACL */
693 const struct inode_operations zpl_dir_inode_operations
= {
694 .create
= zpl_create
,
695 .lookup
= zpl_lookup
,
697 .unlink
= zpl_unlink
,
698 .symlink
= zpl_symlink
,
702 #ifdef HAVE_RENAME_WANTS_FLAGS
703 .rename
= zpl_rename2
,
705 .rename
= zpl_rename
,
707 .setattr
= zpl_setattr
,
708 .getattr
= zpl_getattr
,
709 #ifdef HAVE_GENERIC_SETXATTR
710 .setxattr
= generic_setxattr
,
711 .getxattr
= generic_getxattr
,
712 .removexattr
= generic_removexattr
,
714 .listxattr
= zpl_xattr_list
,
715 #if defined(CONFIG_FS_POSIX_ACL)
716 #if defined(HAVE_SET_ACL)
717 .set_acl
= zpl_set_acl
,
719 #if defined(HAVE_GET_ACL)
720 .get_acl
= zpl_get_acl
,
721 #elif defined(HAVE_CHECK_ACL)
722 .check_acl
= zpl_check_acl
,
723 #elif defined(HAVE_PERMISSION)
724 .permission
= zpl_permission
,
725 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
726 #endif /* CONFIG_FS_POSIX_ACL */
729 const struct inode_operations zpl_symlink_inode_operations
= {
730 #ifdef HAVE_GENERIC_READLINK
731 .readlink
= generic_readlink
,
733 #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE)
734 .get_link
= zpl_get_link
,
735 #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA)
736 .follow_link
= zpl_follow_link
,
738 #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA)
739 .put_link
= zpl_put_link
,
741 .setattr
= zpl_setattr
,
742 .getattr
= zpl_getattr
,
743 #ifdef HAVE_GENERIC_SETXATTR
744 .setxattr
= generic_setxattr
,
745 .getxattr
= generic_getxattr
,
746 .removexattr
= generic_removexattr
,
748 .listxattr
= zpl_xattr_list
,
751 const struct inode_operations zpl_special_inode_operations
= {
752 .setattr
= zpl_setattr
,
753 .getattr
= zpl_getattr
,
754 #ifdef HAVE_GENERIC_SETXATTR
755 .setxattr
= generic_setxattr
,
756 .getxattr
= generic_getxattr
,
757 .removexattr
= generic_removexattr
,
759 .listxattr
= zpl_xattr_list
,
760 #if defined(CONFIG_FS_POSIX_ACL)
761 #if defined(HAVE_SET_ACL)
762 .set_acl
= zpl_set_acl
,
764 #if defined(HAVE_GET_ACL)
765 .get_acl
= zpl_get_acl
,
766 #elif defined(HAVE_CHECK_ACL)
767 .check_acl
= zpl_check_acl
,
768 #elif defined(HAVE_PERMISSION)
769 .permission
= zpl_permission
,
770 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
771 #endif /* CONFIG_FS_POSIX_ACL */
774 dentry_operations_t zpl_dentry_operations
= {
775 .d_revalidate
= zpl_revalidate
,