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.
26 #include <sys/zfs_vfsops.h>
27 #include <sys/zfs_vnops.h>
28 #include <sys/zfs_znode.h>
33 static struct dentry
*
34 #ifdef HAVE_LOOKUP_NAMEIDATA
35 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
37 zpl_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
43 fstrans_cookie_t cookie
;
45 if (dlen(dentry
) > ZFS_MAXNAMELEN
)
46 return (ERR_PTR(-ENAMETOOLONG
));
49 cookie
= spl_fstrans_mark();
50 error
= -zfs_lookup(dir
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
51 spl_fstrans_unmark(cookie
);
52 ASSERT3S(error
, <=, 0);
55 spin_lock(&dentry
->d_lock
);
56 dentry
->d_time
= jiffies
;
58 d_set_d_op(dentry
, &zpl_dentry_operations
);
59 #endif /* HAVE_S_D_OP */
60 spin_unlock(&dentry
->d_lock
);
64 return (d_splice_alias(NULL
, dentry
));
66 return (ERR_PTR(error
));
69 return (d_splice_alias(ip
, dentry
));
73 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, zpl_umode_t mode
, cred_t
*cr
)
75 vap
->va_mask
= ATTR_MODE
;
77 vap
->va_uid
= crgetfsuid(cr
);
79 if (dir
&& dir
->i_mode
& S_ISGID
) {
80 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
82 vap
->va_mode
|= S_ISGID
;
84 vap
->va_gid
= crgetfsgid(cr
);
89 #ifdef HAVE_CREATE_NAMEIDATA
90 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
93 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
101 fstrans_cookie_t cookie
;
104 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
105 zpl_vap_init(vap
, dir
, mode
, cr
);
107 cookie
= spl_fstrans_mark();
108 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
110 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
111 VERIFY0(zpl_init_acl(ip
, dir
));
112 d_instantiate(dentry
, ip
);
115 spl_fstrans_unmark(cookie
);
116 kmem_free(vap
, sizeof (vattr_t
));
118 ASSERT3S(error
, <=, 0);
124 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
131 fstrans_cookie_t cookie
;
134 * We currently expect Linux to supply rdev=0 for all sockets
135 * and fifos, but we want to know if this behavior ever changes.
137 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
141 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
142 zpl_vap_init(vap
, dir
, mode
, cr
);
145 cookie
= spl_fstrans_mark();
146 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
148 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
149 VERIFY0(zpl_init_acl(ip
, dir
));
150 d_instantiate(dentry
, ip
);
153 spl_fstrans_unmark(cookie
);
154 kmem_free(vap
, sizeof (vattr_t
));
156 ASSERT3S(error
, <=, 0);
162 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
166 fstrans_cookie_t cookie
;
169 cookie
= spl_fstrans_mark();
170 error
= -zfs_remove(dir
, dname(dentry
), cr
);
171 spl_fstrans_unmark(cookie
);
173 ASSERT3S(error
, <=, 0);
179 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
)
185 fstrans_cookie_t cookie
;
188 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
189 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
);
191 cookie
= spl_fstrans_mark();
192 error
= -zfs_mkdir(dir
, dname(dentry
), vap
, &ip
, cr
, 0, NULL
);
194 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
195 VERIFY0(zpl_init_acl(ip
, dir
));
196 d_instantiate(dentry
, ip
);
199 spl_fstrans_unmark(cookie
);
200 kmem_free(vap
, sizeof (vattr_t
));
202 ASSERT3S(error
, <=, 0);
208 zpl_rmdir(struct inode
* dir
, struct dentry
*dentry
)
212 fstrans_cookie_t cookie
;
215 cookie
= spl_fstrans_mark();
216 error
= -zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
217 spl_fstrans_unmark(cookie
);
219 ASSERT3S(error
, <=, 0);
225 zpl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
227 boolean_t issnap
= ITOZSB(dentry
->d_inode
)->z_issnap
;
229 fstrans_cookie_t cookie
;
232 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
233 * unmounted automatically with the parent file system. This
234 * is done on the first getattr because it's not easy to get the
235 * vfsmount structure at mount time. This call path is explicitly
236 * marked unlikely to avoid any performance impact. FWIW, ext4
237 * resorts to a similar trick for sysadmin convenience.
239 if (unlikely(issnap
&& !(mnt
->mnt_flags
& MNT_SHRINKABLE
)))
240 mnt
->mnt_flags
|= MNT_SHRINKABLE
;
242 cookie
= spl_fstrans_mark();
243 error
= -zfs_getattr_fast(dentry
->d_inode
, stat
);
244 spl_fstrans_unmark(cookie
);
245 ASSERT3S(error
, <=, 0);
251 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
253 struct inode
*ip
= dentry
->d_inode
;
257 fstrans_cookie_t cookie
;
259 error
= inode_change_ok(ip
, ia
);
264 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
265 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
266 vap
->va_mode
= ia
->ia_mode
;
267 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
268 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
269 vap
->va_size
= ia
->ia_size
;
270 vap
->va_atime
= ia
->ia_atime
;
271 vap
->va_mtime
= ia
->ia_mtime
;
272 vap
->va_ctime
= ia
->ia_ctime
;
274 cookie
= spl_fstrans_mark();
275 error
= -zfs_setattr(ip
, vap
, 0, cr
);
276 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
277 error
= zpl_chmod_acl(ip
);
279 spl_fstrans_unmark(cookie
);
280 kmem_free(vap
, sizeof (vattr_t
));
282 ASSERT3S(error
, <=, 0);
288 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
289 struct inode
*tdip
, struct dentry
*tdentry
)
293 fstrans_cookie_t cookie
;
296 cookie
= spl_fstrans_mark();
297 error
= -zfs_rename(sdip
, dname(sdentry
), tdip
, dname(tdentry
), cr
, 0);
298 spl_fstrans_unmark(cookie
);
300 ASSERT3S(error
, <=, 0);
306 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
312 fstrans_cookie_t cookie
;
315 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
316 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
318 cookie
= spl_fstrans_mark();
319 error
= -zfs_symlink(dir
, dname(dentry
), vap
, (char *)name
, &ip
, cr
, 0);
321 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
322 d_instantiate(dentry
, ip
);
325 spl_fstrans_unmark(cookie
);
326 kmem_free(vap
, sizeof (vattr_t
));
328 ASSERT3S(error
, <=, 0);
334 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
337 struct inode
*ip
= dentry
->d_inode
;
342 fstrans_cookie_t cookie
;
346 iov
.iov_len
= MAXPATHLEN
;
347 iov
.iov_base
= link
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
351 uio
.uio_resid
= (MAXPATHLEN
- 1);
352 uio
.uio_segflg
= UIO_SYSSPACE
;
354 cookie
= spl_fstrans_mark();
355 error
= -zfs_readlink(ip
, &uio
, cr
);
356 spl_fstrans_unmark(cookie
);
358 kmem_free(link
, MAXPATHLEN
);
359 nd_set_link(nd
, ERR_PTR(error
));
361 nd_set_link(nd
, link
);
369 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
371 const char *link
= nd_get_link(nd
);
374 kmem_free(link
, MAXPATHLEN
);
378 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
381 struct inode
*ip
= old_dentry
->d_inode
;
383 fstrans_cookie_t cookie
;
385 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
389 ip
->i_ctime
= CURRENT_TIME_SEC
;
390 igrab(ip
); /* Use ihold() if available */
392 cookie
= spl_fstrans_mark();
393 error
= -zfs_link(dir
, ip
, dname(dentry
), cr
);
399 d_instantiate(dentry
, ip
);
401 spl_fstrans_unmark(cookie
);
403 ASSERT3S(error
, <=, 0);
408 #ifdef HAVE_INODE_TRUNCATE_RANGE
410 zpl_truncate_range(struct inode
*ip
, loff_t start
, loff_t end
)
414 fstrans_cookie_t cookie
;
416 ASSERT3S(start
, <=, end
);
419 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
420 * the end of the file". We don't want that.
430 bf
.l_len
= end
- start
;
432 cookie
= spl_fstrans_mark();
433 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
434 spl_fstrans_unmark(cookie
);
438 #endif /* HAVE_INODE_TRUNCATE_RANGE */
440 #ifdef HAVE_INODE_FALLOCATE
442 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
444 return (zpl_fallocate_common(ip
, mode
, offset
, len
));
446 #endif /* HAVE_INODE_FALLOCATE */
449 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
450 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
452 unsigned int flags
= (nd
? nd
->flags
: 0);
454 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
456 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
457 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
460 if (flags
& LOOKUP_RCU
)
464 * After a rollback negative dentries created before the rollback
465 * time must be invalidated. Otherwise they can obscure files which
466 * are only present in the rolled back dataset.
468 if (dentry
->d_inode
== NULL
) {
469 spin_lock(&dentry
->d_lock
);
470 error
= time_before(dentry
->d_time
, zsb
->z_rollback_time
);
471 spin_unlock(&dentry
->d_lock
);
478 * The dentry may reference a stale inode if a mounted file system
479 * was rolled back to a point in time where the object didn't exist.
481 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
487 const struct inode_operations zpl_inode_operations
= {
488 .create
= zpl_create
,
490 .unlink
= zpl_unlink
,
491 .symlink
= zpl_symlink
,
495 .rename
= zpl_rename
,
496 .setattr
= zpl_setattr
,
497 .getattr
= zpl_getattr
,
498 .setxattr
= generic_setxattr
,
499 .getxattr
= generic_getxattr
,
500 .removexattr
= generic_removexattr
,
501 .listxattr
= zpl_xattr_list
,
502 #ifdef HAVE_INODE_TRUNCATE_RANGE
503 .truncate_range
= zpl_truncate_range
,
504 #endif /* HAVE_INODE_TRUNCATE_RANGE */
505 #ifdef HAVE_INODE_FALLOCATE
506 .fallocate
= zpl_fallocate
,
507 #endif /* HAVE_INODE_FALLOCATE */
508 #if defined(CONFIG_FS_POSIX_ACL)
509 #if defined(HAVE_GET_ACL)
510 .get_acl
= zpl_get_acl
,
511 #elif defined(HAVE_CHECK_ACL)
512 .check_acl
= zpl_check_acl
,
513 #elif defined(HAVE_PERMISSION)
514 .permission
= zpl_permission
,
515 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
516 #endif /* CONFIG_FS_POSIX_ACL */
519 const struct inode_operations zpl_dir_inode_operations
= {
520 .create
= zpl_create
,
521 .lookup
= zpl_lookup
,
523 .unlink
= zpl_unlink
,
524 .symlink
= zpl_symlink
,
528 .rename
= zpl_rename
,
529 .setattr
= zpl_setattr
,
530 .getattr
= zpl_getattr
,
531 .setxattr
= generic_setxattr
,
532 .getxattr
= generic_getxattr
,
533 .removexattr
= generic_removexattr
,
534 .listxattr
= zpl_xattr_list
,
535 #if defined(CONFIG_FS_POSIX_ACL)
536 #if defined(HAVE_GET_ACL)
537 .get_acl
= zpl_get_acl
,
538 #elif defined(HAVE_CHECK_ACL)
539 .check_acl
= zpl_check_acl
,
540 #elif defined(HAVE_PERMISSION)
541 .permission
= zpl_permission
,
542 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
543 #endif /* CONFIG_FS_POSIX_ACL */
546 const struct inode_operations zpl_symlink_inode_operations
= {
547 .readlink
= generic_readlink
,
548 .follow_link
= zpl_follow_link
,
549 .put_link
= zpl_put_link
,
550 .setattr
= zpl_setattr
,
551 .getattr
= zpl_getattr
,
552 .setxattr
= generic_setxattr
,
553 .getxattr
= generic_getxattr
,
554 .removexattr
= generic_removexattr
,
555 .listxattr
= zpl_xattr_list
,
558 const struct inode_operations zpl_special_inode_operations
= {
559 .setattr
= zpl_setattr
,
560 .getattr
= zpl_getattr
,
561 .setxattr
= generic_setxattr
,
562 .getxattr
= generic_getxattr
,
563 .removexattr
= generic_removexattr
,
564 .listxattr
= zpl_xattr_list
,
565 #if defined(CONFIG_FS_POSIX_ACL)
566 #if defined(HAVE_GET_ACL)
567 .get_acl
= zpl_get_acl
,
568 #elif defined(HAVE_CHECK_ACL)
569 .check_acl
= zpl_check_acl
,
570 #elif defined(HAVE_PERMISSION)
571 .permission
= zpl_permission
,
572 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
573 #endif /* CONFIG_FS_POSIX_ACL */
576 dentry_operations_t zpl_dentry_operations
= {
577 .d_revalidate
= zpl_revalidate
,