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_init_acl(ip, dir));
141 d_instantiate(dentry, ip);
144 kmem_free(vap, sizeof(vattr_t));
146 ASSERT3S(error, <=, 0);
152 zpl_unlink(struct inode *dir, struct dentry *dentry)
158 error = -zfs_remove(dir, dname(dentry), cr);
160 ASSERT3S(error, <=, 0);
166 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
174 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
175 zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
177 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
179 VERIFY0(zpl_init_acl(ip, dir));
180 d_instantiate(dentry, ip);
183 kmem_free(vap, sizeof(vattr_t));
185 ASSERT3S(error, <=, 0);
191 zpl_rmdir(struct inode * dir, struct dentry *dentry)
197 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
199 ASSERT3S(error, <=, 0);
205 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
207 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
211 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
212 * unmounted automatically with the parent file system. This
213 * is done on the first getattr because it's not easy to get the
214 * vfsmount structure at mount time. This call path is explicitly
215 * marked unlikely to avoid any performance impact. FWIW, ext4
216 * resorts to a similar trick for sysadmin convenience.
218 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
219 mnt->mnt_flags |= MNT_SHRINKABLE;
221 error = -zfs_getattr_fast(dentry->d_inode, stat);
222 ASSERT3S(error, <=, 0);
228 zpl_setattr(struct dentry *dentry, struct iattr *ia)
230 struct inode *ip = dentry->d_inode;
235 error = inode_change_ok(ip, ia);
240 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
241 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
242 vap->va_mode = ia->ia_mode;
243 vap->va_uid = KUID_TO_SUID(ia->ia_uid);
244 vap->va_gid = KGID_TO_SGID(ia->ia_gid);
245 vap->va_size = ia->ia_size;
246 vap->va_atime = ia->ia_atime;
247 vap->va_mtime = ia->ia_mtime;
248 vap->va_ctime = ia->ia_ctime;
250 error = -zfs_setattr(ip, vap, 0, cr);
251 if (!error && (ia->ia_valid & ATTR_MODE))
252 error = zpl_chmod_acl(ip);
254 kmem_free(vap, sizeof(vattr_t));
256 ASSERT3S(error, <=, 0);
262 zpl_rename(struct inode *sdip, struct dentry *sdentry,
263 struct inode *tdip, struct dentry *tdentry)
269 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
271 ASSERT3S(error, <=, 0);
277 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
285 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
286 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
288 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
290 d_instantiate(dentry, ip);
292 kmem_free(vap, sizeof(vattr_t));
294 ASSERT3S(error, <=, 0);
300 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
303 struct inode *ip = dentry->d_inode;
311 iov.iov_len = MAXPATHLEN;
312 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
316 uio.uio_resid = (MAXPATHLEN - 1);
317 uio.uio_segflg = UIO_SYSSPACE;
319 error = -zfs_readlink(ip, &uio, cr);
321 kmem_free(link, MAXPATHLEN);
322 nd_set_link(nd, ERR_PTR(error));
324 nd_set_link(nd, link);
332 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
334 const char *link = nd_get_link(nd);
337 kmem_free(link, MAXPATHLEN);
341 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
344 struct inode *ip = old_dentry->d_inode;
347 if (ip->i_nlink >= ZFS_LINK_MAX)
351 ip->i_ctime = CURRENT_TIME_SEC;
352 igrab(ip); /* Use ihold() if available */
354 error = -zfs_link(dir, ip, dname(dentry), cr);
360 d_instantiate(dentry, ip);
363 ASSERT3S(error, <=, 0);
368 #ifdef HAVE_INODE_TRUNCATE_RANGE
370 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
375 ASSERT3S(start, <=, end);
378 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
379 * the end of the file". We don't want that.
389 bf.l_len = end - start;
391 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
395 #endif /* HAVE_INODE_TRUNCATE_RANGE */
397 #ifdef HAVE_INODE_FALLOCATE
399 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
401 return zpl_fallocate_common(ip, mode, offset, len);
403 #endif /* HAVE_INODE_FALLOCATE */
406 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
407 zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
409 unsigned int flags = (nd ? nd->flags : 0);
411 zpl_revalidate(struct dentry *dentry, unsigned int flags)
413 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
414 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
417 if (flags & LOOKUP_RCU)
421 * After a rollback negative dentries created before the rollback
422 * time must be invalidated. Otherwise they can obscure files which
423 * are only present in the rolled back dataset.
425 if (dentry->d_inode == NULL) {
426 spin_lock(&dentry->d_lock);
427 error = time_before(dentry->d_time, zsb->z_rollback_time);
428 spin_unlock(&dentry->d_lock);
435 * The dentry may reference a stale inode if a mounted file system
436 * was rolled back to a point in time where the object didn't exist.
438 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
444 const struct inode_operations zpl_inode_operations = {
445 .create = zpl_create,
447 .unlink = zpl_unlink,
448 .symlink = zpl_symlink,
452 .rename = zpl_rename,
453 .setattr = zpl_setattr,
454 .getattr = zpl_getattr,
455 .setxattr = generic_setxattr,
456 .getxattr = generic_getxattr,
457 .removexattr = generic_removexattr,
458 .listxattr = zpl_xattr_list,
459 #ifdef HAVE_INODE_TRUNCATE_RANGE
460 .truncate_range = zpl_truncate_range,
461 #endif /* HAVE_INODE_TRUNCATE_RANGE */
462 #ifdef HAVE_INODE_FALLOCATE
463 .fallocate = zpl_fallocate,
464 #endif /* HAVE_INODE_FALLOCATE */
465 #if defined(HAVE_GET_ACL)
466 .get_acl = zpl_get_acl,
467 #elif defined(HAVE_CHECK_ACL)
468 .check_acl = zpl_check_acl,
469 #elif defined(HAVE_PERMISSION)
470 .permission = zpl_permission,
471 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
474 const struct inode_operations zpl_dir_inode_operations = {
475 .create = zpl_create,
476 .lookup = zpl_lookup,
478 .unlink = zpl_unlink,
479 .symlink = zpl_symlink,
483 .rename = zpl_rename,
484 .setattr = zpl_setattr,
485 .getattr = zpl_getattr,
486 .setxattr = generic_setxattr,
487 .getxattr = generic_getxattr,
488 .removexattr = generic_removexattr,
489 .listxattr = zpl_xattr_list,
490 #if defined(HAVE_GET_ACL)
491 .get_acl = zpl_get_acl,
492 #elif defined(HAVE_CHECK_ACL)
493 .check_acl = zpl_check_acl,
494 #elif defined(HAVE_PERMISSION)
495 .permission = zpl_permission,
496 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
499 const struct inode_operations zpl_symlink_inode_operations = {
500 .readlink = generic_readlink,
501 .follow_link = zpl_follow_link,
502 .put_link = zpl_put_link,
503 .setattr = zpl_setattr,
504 .getattr = zpl_getattr,
505 .setxattr = generic_setxattr,
506 .getxattr = generic_getxattr,
507 .removexattr = generic_removexattr,
508 .listxattr = zpl_xattr_list,
511 const struct inode_operations zpl_special_inode_operations = {
512 .setattr = zpl_setattr,
513 .getattr = zpl_getattr,
514 .setxattr = generic_setxattr,
515 .getxattr = generic_getxattr,
516 .removexattr = generic_removexattr,
517 .listxattr = zpl_xattr_list,
518 #if defined(HAVE_GET_ACL)
519 .get_acl = zpl_get_acl,
520 #elif defined(HAVE_CHECK_ACL)
521 .check_acl = zpl_check_acl,
522 #elif defined(HAVE_PERMISSION)
523 .permission = zpl_permission,
524 #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
527 dentry_operations_t zpl_dentry_operations = {
528 .d_revalidate = zpl_revalidate,