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
)
44 if (dlen(dentry
) > ZFS_MAXNAMELEN
)
45 return ERR_PTR(-ENAMETOOLONG
);
48 error
= -zfs_lookup(dir
, dname(dentry
), &ip
, 0, cr
, NULL
, NULL
);
49 ASSERT3S(error
, <=, 0);
52 spin_lock(&dentry
->d_lock
);
53 dentry
->d_time
= jiffies
;
55 d_set_d_op(dentry
, &zpl_dentry_operations
);
56 #endif /* HAVE_S_D_OP */
57 spin_unlock(&dentry
->d_lock
);
61 return d_splice_alias(NULL
, dentry
);
63 return ERR_PTR(error
);
66 return d_splice_alias(ip
, dentry
);
70 zpl_vap_init(vattr_t
*vap
, struct inode
*dir
, zpl_umode_t mode
, cred_t
*cr
)
72 vap
->va_mask
= ATTR_MODE
;
74 vap
->va_uid
= crgetfsuid(cr
);
76 if (dir
&& dir
->i_mode
& S_ISGID
) {
77 vap
->va_gid
= KGID_TO_SGID(dir
->i_gid
);
79 vap
->va_mode
|= S_ISGID
;
81 vap
->va_gid
= crgetfsgid(cr
);
86 #ifdef HAVE_CREATE_NAMEIDATA
87 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
90 zpl_create(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
100 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
101 zpl_vap_init(vap
, dir
, mode
, cr
);
103 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
105 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
106 VERIFY0(zpl_init_acl(ip
, dir
));
107 d_instantiate(dentry
, ip
);
110 kmem_free(vap
, sizeof(vattr_t
));
112 ASSERT3S(error
, <=, 0);
118 zpl_mknod(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
,
127 * We currently expect Linux to supply rdev=0 for all sockets
128 * and fifos, but we want to know if this behavior ever changes.
130 if (S_ISSOCK(mode
) || S_ISFIFO(mode
))
134 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
135 zpl_vap_init(vap
, dir
, mode
, cr
);
138 error
= -zfs_create(dir
, dname(dentry
), vap
, 0, mode
, &ip
, cr
, 0, NULL
);
140 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
141 VERIFY0(zpl_init_acl(ip
, dir
));
142 d_instantiate(dentry
, ip
);
145 kmem_free(vap
, sizeof(vattr_t
));
147 ASSERT3S(error
, <=, 0);
153 zpl_unlink(struct inode
*dir
, struct dentry
*dentry
)
159 error
= -zfs_remove(dir
, dname(dentry
), cr
);
161 ASSERT3S(error
, <=, 0);
167 zpl_mkdir(struct inode
*dir
, struct dentry
*dentry
, zpl_umode_t mode
)
175 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
176 zpl_vap_init(vap
, dir
, mode
| S_IFDIR
, cr
);
178 error
= -zfs_mkdir(dir
, dname(dentry
), vap
, &ip
, cr
, 0, NULL
);
180 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
181 VERIFY0(zpl_init_acl(ip
, dir
));
182 d_instantiate(dentry
, ip
);
185 kmem_free(vap
, sizeof(vattr_t
));
187 ASSERT3S(error
, <=, 0);
193 zpl_rmdir(struct inode
* dir
, struct dentry
*dentry
)
199 error
= -zfs_rmdir(dir
, dname(dentry
), NULL
, cr
, 0);
201 ASSERT3S(error
, <=, 0);
207 zpl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
209 boolean_t issnap
= ITOZSB(dentry
->d_inode
)->z_issnap
;
213 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
214 * unmounted automatically with the parent file system. This
215 * is done on the first getattr because it's not easy to get the
216 * vfsmount structure at mount time. This call path is explicitly
217 * marked unlikely to avoid any performance impact. FWIW, ext4
218 * resorts to a similar trick for sysadmin convenience.
220 if (unlikely(issnap
&& !(mnt
->mnt_flags
& MNT_SHRINKABLE
)))
221 mnt
->mnt_flags
|= MNT_SHRINKABLE
;
223 error
= -zfs_getattr_fast(dentry
->d_inode
, stat
);
224 ASSERT3S(error
, <=, 0);
230 zpl_setattr(struct dentry
*dentry
, struct iattr
*ia
)
232 struct inode
*ip
= dentry
->d_inode
;
237 error
= inode_change_ok(ip
, ia
);
242 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
243 vap
->va_mask
= ia
->ia_valid
& ATTR_IATTR_MASK
;
244 vap
->va_mode
= ia
->ia_mode
;
245 vap
->va_uid
= KUID_TO_SUID(ia
->ia_uid
);
246 vap
->va_gid
= KGID_TO_SGID(ia
->ia_gid
);
247 vap
->va_size
= ia
->ia_size
;
248 vap
->va_atime
= ia
->ia_atime
;
249 vap
->va_mtime
= ia
->ia_mtime
;
250 vap
->va_ctime
= ia
->ia_ctime
;
252 error
= -zfs_setattr(ip
, vap
, 0, cr
);
253 if (!error
&& (ia
->ia_valid
& ATTR_MODE
))
254 error
= zpl_chmod_acl(ip
);
256 kmem_free(vap
, sizeof(vattr_t
));
258 ASSERT3S(error
, <=, 0);
264 zpl_rename(struct inode
*sdip
, struct dentry
*sdentry
,
265 struct inode
*tdip
, struct dentry
*tdentry
)
271 error
= -zfs_rename(sdip
, dname(sdentry
), tdip
, dname(tdentry
), cr
, 0);
273 ASSERT3S(error
, <=, 0);
279 zpl_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
287 vap
= kmem_zalloc(sizeof(vattr_t
), KM_SLEEP
);
288 zpl_vap_init(vap
, dir
, S_IFLNK
| S_IRWXUGO
, cr
);
290 error
= -zfs_symlink(dir
, dname(dentry
), vap
, (char *)name
, &ip
, cr
, 0);
292 VERIFY0(zpl_xattr_security_init(ip
, dir
, &dentry
->d_name
));
293 d_instantiate(dentry
, ip
);
296 kmem_free(vap
, sizeof(vattr_t
));
298 ASSERT3S(error
, <=, 0);
304 zpl_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
307 struct inode
*ip
= dentry
->d_inode
;
315 iov
.iov_len
= MAXPATHLEN
;
316 iov
.iov_base
= link
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
320 uio
.uio_resid
= (MAXPATHLEN
- 1);
321 uio
.uio_segflg
= UIO_SYSSPACE
;
323 error
= -zfs_readlink(ip
, &uio
, cr
);
325 kmem_free(link
, MAXPATHLEN
);
326 nd_set_link(nd
, ERR_PTR(error
));
328 nd_set_link(nd
, link
);
336 zpl_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
338 const char *link
= nd_get_link(nd
);
341 kmem_free(link
, MAXPATHLEN
);
345 zpl_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
348 struct inode
*ip
= old_dentry
->d_inode
;
351 if (ip
->i_nlink
>= ZFS_LINK_MAX
)
355 ip
->i_ctime
= CURRENT_TIME_SEC
;
356 igrab(ip
); /* Use ihold() if available */
358 error
= -zfs_link(dir
, ip
, dname(dentry
), cr
);
364 d_instantiate(dentry
, ip
);
367 ASSERT3S(error
, <=, 0);
372 #ifdef HAVE_INODE_TRUNCATE_RANGE
374 zpl_truncate_range(struct inode
* ip
, loff_t start
, loff_t end
)
379 ASSERT3S(start
, <=, end
);
382 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
383 * the end of the file". We don't want that.
393 bf
.l_len
= end
- start
;
395 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
399 #endif /* HAVE_INODE_TRUNCATE_RANGE */
401 #ifdef HAVE_INODE_FALLOCATE
403 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
405 return zpl_fallocate_common(ip
, mode
, offset
, len
);
407 #endif /* HAVE_INODE_FALLOCATE */
410 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
411 zpl_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
413 unsigned int flags
= (nd
? nd
->flags
: 0);
415 zpl_revalidate(struct dentry
*dentry
, unsigned int flags
)
417 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
418 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
421 if (flags
& LOOKUP_RCU
)
425 * After a rollback negative dentries created before the rollback
426 * time must be invalidated. Otherwise they can obscure files which
427 * are only present in the rolled back dataset.
429 if (dentry
->d_inode
== NULL
) {
430 spin_lock(&dentry
->d_lock
);
431 error
= time_before(dentry
->d_time
, zsb
->z_rollback_time
);
432 spin_unlock(&dentry
->d_lock
);
439 * The dentry may reference a stale inode if a mounted file system
440 * was rolled back to a point in time where the object didn't exist.
442 if (dentry
->d_inode
&& ITOZ(dentry
->d_inode
)->z_is_stale
)
448 const struct inode_operations zpl_inode_operations
= {
449 .create
= zpl_create
,
451 .unlink
= zpl_unlink
,
452 .symlink
= zpl_symlink
,
456 .rename
= zpl_rename
,
457 .setattr
= zpl_setattr
,
458 .getattr
= zpl_getattr
,
459 .setxattr
= generic_setxattr
,
460 .getxattr
= generic_getxattr
,
461 .removexattr
= generic_removexattr
,
462 .listxattr
= zpl_xattr_list
,
463 #ifdef HAVE_INODE_TRUNCATE_RANGE
464 .truncate_range
= zpl_truncate_range
,
465 #endif /* HAVE_INODE_TRUNCATE_RANGE */
466 #ifdef HAVE_INODE_FALLOCATE
467 .fallocate
= zpl_fallocate
,
468 #endif /* HAVE_INODE_FALLOCATE */
469 #if defined(CONFIG_FS_POSIX_ACL)
470 #if defined(HAVE_GET_ACL)
471 .get_acl
= zpl_get_acl
,
472 #elif defined(HAVE_CHECK_ACL)
473 .check_acl
= zpl_check_acl
,
474 #elif defined(HAVE_PERMISSION)
475 .permission
= zpl_permission
,
476 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
477 #endif /* CONFIG_FS_POSIX_ACL */
480 const struct inode_operations zpl_dir_inode_operations
= {
481 .create
= zpl_create
,
482 .lookup
= zpl_lookup
,
484 .unlink
= zpl_unlink
,
485 .symlink
= zpl_symlink
,
489 .rename
= zpl_rename
,
490 .setattr
= zpl_setattr
,
491 .getattr
= zpl_getattr
,
492 .setxattr
= generic_setxattr
,
493 .getxattr
= generic_getxattr
,
494 .removexattr
= generic_removexattr
,
495 .listxattr
= zpl_xattr_list
,
496 #if defined(CONFIG_FS_POSIX_ACL)
497 #if defined(HAVE_GET_ACL)
498 .get_acl
= zpl_get_acl
,
499 #elif defined(HAVE_CHECK_ACL)
500 .check_acl
= zpl_check_acl
,
501 #elif defined(HAVE_PERMISSION)
502 .permission
= zpl_permission
,
503 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
504 #endif /* CONFIG_FS_POSIX_ACL */
507 const struct inode_operations zpl_symlink_inode_operations
= {
508 .readlink
= generic_readlink
,
509 .follow_link
= zpl_follow_link
,
510 .put_link
= zpl_put_link
,
511 .setattr
= zpl_setattr
,
512 .getattr
= zpl_getattr
,
513 .setxattr
= generic_setxattr
,
514 .getxattr
= generic_getxattr
,
515 .removexattr
= generic_removexattr
,
516 .listxattr
= zpl_xattr_list
,
519 const struct inode_operations zpl_special_inode_operations
= {
520 .setattr
= zpl_setattr
,
521 .getattr
= zpl_getattr
,
522 .setxattr
= generic_setxattr
,
523 .getxattr
= generic_getxattr
,
524 .removexattr
= generic_removexattr
,
525 .listxattr
= zpl_xattr_list
,
526 #if defined(CONFIG_FS_POSIX_ACL)
527 #if defined(HAVE_GET_ACL)
528 .get_acl
= zpl_get_acl
,
529 #elif defined(HAVE_CHECK_ACL)
530 .check_acl
= zpl_check_acl
,
531 #elif defined(HAVE_PERMISSION)
532 .permission
= zpl_permission
,
533 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
534 #endif /* CONFIG_FS_POSIX_ACL */
537 dentry_operations_t zpl_dentry_operations
= {
538 .d_revalidate
= zpl_revalidate
,