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)
45 error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
46 ASSERT3S(error, <=, 0);
49 spin_lock(&dentry->d_lock);
50 dentry->d_time = jiffies;
51 spin_unlock(&dentry->d_lock);
55 return d_splice_alias(NULL, dentry);
57 return ERR_PTR(error);
60 return d_splice_alias(ip, dentry);
64 zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr)
66 vap->va_mask = ATTR_MODE;
68 vap->va_uid = crgetfsuid(cr);
70 if (dir && dir->i_mode & S_ISGID) {
71 vap->va_gid = dir->i_gid;
73 vap->va_mode |= S_ISGID;
75 vap->va_gid = crgetfsgid(cr);
80 #ifdef HAVE_CREATE_NAMEIDATA
81 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
84 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
94 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
95 zpl_vap_init(vap, dir, mode, cr);
97 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
99 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
100 VERIFY3S(error, ==, 0);
101 d_instantiate(dentry, ip);
102 d_set_d_op(dentry, &zpl_dentry_operations);
105 kmem_free(vap, sizeof(vattr_t));
107 ASSERT3S(error, <=, 0);
113 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
122 * We currently expect Linux to supply rdev=0 for all sockets
123 * and fifos, but we want to know if this behavior ever changes.
125 if (S_ISSOCK(mode) || S_ISFIFO(mode))
129 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
130 zpl_vap_init(vap, dir, mode, cr);
133 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
135 d_instantiate(dentry, ip);
136 d_set_d_op(dentry, &zpl_dentry_operations);
139 kmem_free(vap, sizeof(vattr_t));
141 ASSERT3S(error, <=, 0);
147 zpl_unlink(struct inode *dir, struct dentry *dentry)
153 error = -zfs_remove(dir, dname(dentry), cr);
155 ASSERT3S(error, <=, 0);
161 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
169 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
170 zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
172 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
174 d_instantiate(dentry, ip);
175 d_set_d_op(dentry, &zpl_dentry_operations);
178 kmem_free(vap, sizeof(vattr_t));
180 ASSERT3S(error, <=, 0);
186 zpl_rmdir(struct inode * dir, struct dentry *dentry)
192 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
194 ASSERT3S(error, <=, 0);
200 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
202 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
206 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
207 * unmounted automatically with the parent file system. This
208 * is done on the first getattr because it's not easy to get the
209 * vfsmount structure at mount time. This call path is explicitly
210 * marked unlikely to avoid any performance impact. FWIW, ext4
211 * resorts to a similar trick for sysadmin convenience.
213 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
214 mnt->mnt_flags |= MNT_SHRINKABLE;
216 error = -zfs_getattr_fast(dentry->d_inode, stat);
217 ASSERT3S(error, <=, 0);
223 zpl_setattr(struct dentry *dentry, struct iattr *ia)
229 error = inode_change_ok(dentry->d_inode, ia);
234 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
235 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
236 vap->va_mode = ia->ia_mode;
237 vap->va_uid = ia->ia_uid;
238 vap->va_gid = ia->ia_gid;
239 vap->va_size = ia->ia_size;
240 vap->va_atime = ia->ia_atime;
241 vap->va_mtime = ia->ia_mtime;
242 vap->va_ctime = ia->ia_ctime;
244 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
246 kmem_free(vap, sizeof(vattr_t));
248 ASSERT3S(error, <=, 0);
254 zpl_rename(struct inode *sdip, struct dentry *sdentry,
255 struct inode *tdip, struct dentry *tdentry)
261 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
263 ASSERT3S(error, <=, 0);
269 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
277 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
278 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
280 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
282 d_instantiate(dentry, ip);
283 d_set_d_op(dentry, &zpl_dentry_operations);
286 kmem_free(vap, sizeof(vattr_t));
288 ASSERT3S(error, <=, 0);
294 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
297 struct inode *ip = dentry->d_inode;
305 iov.iov_len = MAXPATHLEN;
306 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
310 uio.uio_resid = (MAXPATHLEN - 1);
311 uio.uio_segflg = UIO_SYSSPACE;
313 error = -zfs_readlink(ip, &uio, cr);
315 kmem_free(link, MAXPATHLEN);
316 nd_set_link(nd, ERR_PTR(error));
318 nd_set_link(nd, link);
326 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
328 const char *link = nd_get_link(nd);
331 kmem_free(link, MAXPATHLEN);
335 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
338 struct inode *ip = old_dentry->d_inode;
341 if (ip->i_nlink >= ZFS_LINK_MAX)
345 ip->i_ctime = CURRENT_TIME_SEC;
346 igrab(ip); /* Use ihold() if available */
348 error = -zfs_link(dir, ip, dname(dentry), cr);
354 d_instantiate(dentry, ip);
355 d_set_d_op(dentry, &zpl_dentry_operations);
358 ASSERT3S(error, <=, 0);
363 #ifdef HAVE_INODE_TRUNCATE_RANGE
365 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
370 ASSERT3S(start, <=, end);
373 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
374 * the end of the file". We don't want that.
384 bf.l_len = end - start;
386 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
390 #endif /* HAVE_INODE_TRUNCATE_RANGE */
392 #ifdef HAVE_INODE_FALLOCATE
394 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
396 return zpl_fallocate_common(ip, mode, offset, len);
398 #endif /* HAVE_INODE_FALLOCATE */
401 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
402 zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
404 unsigned int flags = nd->flags;
406 zpl_revalidate(struct dentry *dentry, unsigned int flags)
408 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
409 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
412 if (flags & LOOKUP_RCU)
416 * After a rollback negative dentries created before the rollback
417 * time must be invalidated. Otherwise they can obscure files which
418 * are only present in the rolled back dataset.
420 if (dentry->d_inode == NULL) {
421 spin_lock(&dentry->d_lock);
422 error = time_before(dentry->d_time, zsb->z_rollback_time);
423 spin_unlock(&dentry->d_lock);
430 * The dentry may reference a stale inode if a mounted file system
431 * was rolled back to a point in time where the object didn't exist.
433 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
439 const struct inode_operations zpl_inode_operations = {
440 .create = zpl_create,
442 .unlink = zpl_unlink,
443 .symlink = zpl_symlink,
447 .rename = zpl_rename,
448 .setattr = zpl_setattr,
449 .getattr = zpl_getattr,
450 .setxattr = generic_setxattr,
451 .getxattr = generic_getxattr,
452 .removexattr = generic_removexattr,
453 .listxattr = zpl_xattr_list,
454 #ifdef HAVE_INODE_TRUNCATE_RANGE
455 .truncate_range = zpl_truncate_range,
456 #endif /* HAVE_INODE_TRUNCATE_RANGE */
457 #ifdef HAVE_INODE_FALLOCATE
458 .fallocate = zpl_fallocate,
459 #endif /* HAVE_INODE_FALLOCATE */
462 const struct inode_operations zpl_dir_inode_operations = {
463 .create = zpl_create,
464 .lookup = zpl_lookup,
466 .unlink = zpl_unlink,
467 .symlink = zpl_symlink,
471 .rename = zpl_rename,
472 .setattr = zpl_setattr,
473 .getattr = zpl_getattr,
474 .setxattr = generic_setxattr,
475 .getxattr = generic_getxattr,
476 .removexattr = generic_removexattr,
477 .listxattr = zpl_xattr_list,
480 const struct inode_operations zpl_symlink_inode_operations = {
481 .readlink = generic_readlink,
482 .follow_link = zpl_follow_link,
483 .put_link = zpl_put_link,
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,
492 const struct inode_operations zpl_special_inode_operations = {
493 .setattr = zpl_setattr,
494 .getattr = zpl_getattr,
495 .setxattr = generic_setxattr,
496 .getxattr = generic_getxattr,
497 .removexattr = generic_removexattr,
498 .listxattr = zpl_xattr_list,
501 dentry_operations_t zpl_dentry_operations = {
502 .d_revalidate = zpl_revalidate,