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;
52 d_set_d_op(dentry, &zpl_dentry_operations);
53 #endif /* HAVE_S_D_OP */
54 spin_unlock(&dentry->d_lock);
58 return d_splice_alias(NULL, dentry);
60 return ERR_PTR(error);
63 return d_splice_alias(ip, dentry);
67 zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr)
69 vap->va_mask = ATTR_MODE;
71 vap->va_uid = crgetfsuid(cr);
73 if (dir && dir->i_mode & S_ISGID) {
74 vap->va_gid = dir->i_gid;
76 vap->va_mode |= S_ISGID;
78 vap->va_gid = crgetfsgid(cr);
83 #ifdef HAVE_CREATE_NAMEIDATA
84 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
87 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
97 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
98 zpl_vap_init(vap, dir, mode, cr);
100 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
102 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
103 VERIFY3S(error, ==, 0);
104 d_instantiate(dentry, ip);
107 kmem_free(vap, sizeof(vattr_t));
109 ASSERT3S(error, <=, 0);
115 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
124 * We currently expect Linux to supply rdev=0 for all sockets
125 * and fifos, but we want to know if this behavior ever changes.
127 if (S_ISSOCK(mode) || S_ISFIFO(mode))
131 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
132 zpl_vap_init(vap, dir, mode, cr);
135 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
137 d_instantiate(dentry, ip);
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);
176 kmem_free(vap, sizeof(vattr_t));
178 ASSERT3S(error, <=, 0);
184 zpl_rmdir(struct inode * dir, struct dentry *dentry)
190 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
192 ASSERT3S(error, <=, 0);
198 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
200 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
204 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
205 * unmounted automatically with the parent file system. This
206 * is done on the first getattr because it's not easy to get the
207 * vfsmount structure at mount time. This call path is explicitly
208 * marked unlikely to avoid any performance impact. FWIW, ext4
209 * resorts to a similar trick for sysadmin convenience.
211 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
212 mnt->mnt_flags |= MNT_SHRINKABLE;
214 error = -zfs_getattr_fast(dentry->d_inode, stat);
215 ASSERT3S(error, <=, 0);
221 zpl_setattr(struct dentry *dentry, struct iattr *ia)
227 error = inode_change_ok(dentry->d_inode, ia);
232 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
233 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
234 vap->va_mode = ia->ia_mode;
235 vap->va_uid = ia->ia_uid;
236 vap->va_gid = ia->ia_gid;
237 vap->va_size = ia->ia_size;
238 vap->va_atime = ia->ia_atime;
239 vap->va_mtime = ia->ia_mtime;
240 vap->va_ctime = ia->ia_ctime;
242 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
244 kmem_free(vap, sizeof(vattr_t));
246 ASSERT3S(error, <=, 0);
252 zpl_rename(struct inode *sdip, struct dentry *sdentry,
253 struct inode *tdip, struct dentry *tdentry)
259 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
261 ASSERT3S(error, <=, 0);
267 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
275 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
276 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
278 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
280 d_instantiate(dentry, ip);
282 kmem_free(vap, sizeof(vattr_t));
284 ASSERT3S(error, <=, 0);
290 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
293 struct inode *ip = dentry->d_inode;
301 iov.iov_len = MAXPATHLEN;
302 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
306 uio.uio_resid = (MAXPATHLEN - 1);
307 uio.uio_segflg = UIO_SYSSPACE;
309 error = -zfs_readlink(ip, &uio, cr);
311 kmem_free(link, MAXPATHLEN);
312 nd_set_link(nd, ERR_PTR(error));
314 nd_set_link(nd, link);
322 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
324 const char *link = nd_get_link(nd);
327 kmem_free(link, MAXPATHLEN);
331 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
334 struct inode *ip = old_dentry->d_inode;
337 if (ip->i_nlink >= ZFS_LINK_MAX)
341 ip->i_ctime = CURRENT_TIME_SEC;
342 igrab(ip); /* Use ihold() if available */
344 error = -zfs_link(dir, ip, dname(dentry), cr);
350 d_instantiate(dentry, ip);
353 ASSERT3S(error, <=, 0);
358 #ifdef HAVE_INODE_TRUNCATE_RANGE
360 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
365 ASSERT3S(start, <=, end);
368 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
369 * the end of the file". We don't want that.
379 bf.l_len = end - start;
381 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
385 #endif /* HAVE_INODE_TRUNCATE_RANGE */
387 #ifdef HAVE_INODE_FALLOCATE
389 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
391 return zpl_fallocate_common(ip, mode, offset, len);
393 #endif /* HAVE_INODE_FALLOCATE */
396 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
397 zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
399 unsigned int flags = (nd ? nd->flags : 0);
401 zpl_revalidate(struct dentry *dentry, unsigned int flags)
403 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
404 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
407 if (flags & LOOKUP_RCU)
411 * After a rollback negative dentries created before the rollback
412 * time must be invalidated. Otherwise they can obscure files which
413 * are only present in the rolled back dataset.
415 if (dentry->d_inode == NULL) {
416 spin_lock(&dentry->d_lock);
417 error = time_before(dentry->d_time, zsb->z_rollback_time);
418 spin_unlock(&dentry->d_lock);
425 * The dentry may reference a stale inode if a mounted file system
426 * was rolled back to a point in time where the object didn't exist.
428 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
434 const struct inode_operations zpl_inode_operations = {
435 .create = zpl_create,
437 .unlink = zpl_unlink,
438 .symlink = zpl_symlink,
442 .rename = zpl_rename,
443 .setattr = zpl_setattr,
444 .getattr = zpl_getattr,
445 .setxattr = generic_setxattr,
446 .getxattr = generic_getxattr,
447 .removexattr = generic_removexattr,
448 .listxattr = zpl_xattr_list,
449 #ifdef HAVE_INODE_TRUNCATE_RANGE
450 .truncate_range = zpl_truncate_range,
451 #endif /* HAVE_INODE_TRUNCATE_RANGE */
452 #ifdef HAVE_INODE_FALLOCATE
453 .fallocate = zpl_fallocate,
454 #endif /* HAVE_INODE_FALLOCATE */
457 const struct inode_operations zpl_dir_inode_operations = {
458 .create = zpl_create,
459 .lookup = zpl_lookup,
461 .unlink = zpl_unlink,
462 .symlink = zpl_symlink,
466 .rename = zpl_rename,
467 .setattr = zpl_setattr,
468 .getattr = zpl_getattr,
469 .setxattr = generic_setxattr,
470 .getxattr = generic_getxattr,
471 .removexattr = generic_removexattr,
472 .listxattr = zpl_xattr_list,
475 const struct inode_operations zpl_symlink_inode_operations = {
476 .readlink = generic_readlink,
477 .follow_link = zpl_follow_link,
478 .put_link = zpl_put_link,
479 .setattr = zpl_setattr,
480 .getattr = zpl_getattr,
481 .setxattr = generic_setxattr,
482 .getxattr = generic_getxattr,
483 .removexattr = generic_removexattr,
484 .listxattr = zpl_xattr_list,
487 const struct inode_operations zpl_special_inode_operations = {
488 .setattr = zpl_setattr,
489 .getattr = zpl_getattr,
490 .setxattr = generic_setxattr,
491 .getxattr = generic_getxattr,
492 .removexattr = generic_removexattr,
493 .listxattr = zpl_xattr_list,
496 dentry_operations_t zpl_dentry_operations = {
497 .d_revalidate = zpl_revalidate,