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 = 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 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
106 VERIFY3S(error, ==, 0);
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 d_instantiate(dentry, ip);
142 kmem_free(vap, sizeof(vattr_t));
144 ASSERT3S(error, <=, 0);
150 zpl_unlink(struct inode *dir, struct dentry *dentry)
156 error = -zfs_remove(dir, dname(dentry), cr);
158 ASSERT3S(error, <=, 0);
164 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
172 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
173 zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
175 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
177 d_instantiate(dentry, ip);
179 kmem_free(vap, sizeof(vattr_t));
181 ASSERT3S(error, <=, 0);
187 zpl_rmdir(struct inode * dir, struct dentry *dentry)
193 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
195 ASSERT3S(error, <=, 0);
201 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
203 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
207 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
208 * unmounted automatically with the parent file system. This
209 * is done on the first getattr because it's not easy to get the
210 * vfsmount structure at mount time. This call path is explicitly
211 * marked unlikely to avoid any performance impact. FWIW, ext4
212 * resorts to a similar trick for sysadmin convenience.
214 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
215 mnt->mnt_flags |= MNT_SHRINKABLE;
217 error = -zfs_getattr_fast(dentry->d_inode, stat);
218 ASSERT3S(error, <=, 0);
224 zpl_setattr(struct dentry *dentry, struct iattr *ia)
230 error = inode_change_ok(dentry->d_inode, ia);
235 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
236 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
237 vap->va_mode = ia->ia_mode;
238 vap->va_uid = ia->ia_uid;
239 vap->va_gid = ia->ia_gid;
240 vap->va_size = ia->ia_size;
241 vap->va_atime = ia->ia_atime;
242 vap->va_mtime = ia->ia_mtime;
243 vap->va_ctime = ia->ia_ctime;
245 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
247 kmem_free(vap, sizeof(vattr_t));
249 ASSERT3S(error, <=, 0);
255 zpl_rename(struct inode *sdip, struct dentry *sdentry,
256 struct inode *tdip, struct dentry *tdentry)
262 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
264 ASSERT3S(error, <=, 0);
270 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
278 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
279 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
281 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
283 d_instantiate(dentry, ip);
285 kmem_free(vap, sizeof(vattr_t));
287 ASSERT3S(error, <=, 0);
293 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
296 struct inode *ip = dentry->d_inode;
304 iov.iov_len = MAXPATHLEN;
305 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
309 uio.uio_resid = (MAXPATHLEN - 1);
310 uio.uio_segflg = UIO_SYSSPACE;
312 error = -zfs_readlink(ip, &uio, cr);
314 kmem_free(link, MAXPATHLEN);
315 nd_set_link(nd, ERR_PTR(error));
317 nd_set_link(nd, link);
325 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
327 const char *link = nd_get_link(nd);
330 kmem_free(link, MAXPATHLEN);
334 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
337 struct inode *ip = old_dentry->d_inode;
340 if (ip->i_nlink >= ZFS_LINK_MAX)
344 ip->i_ctime = CURRENT_TIME_SEC;
345 igrab(ip); /* Use ihold() if available */
347 error = -zfs_link(dir, ip, dname(dentry), cr);
353 d_instantiate(dentry, ip);
356 ASSERT3S(error, <=, 0);
361 #ifdef HAVE_INODE_TRUNCATE_RANGE
363 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
368 ASSERT3S(start, <=, end);
371 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
372 * the end of the file". We don't want that.
382 bf.l_len = end - start;
384 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
388 #endif /* HAVE_INODE_TRUNCATE_RANGE */
390 #ifdef HAVE_INODE_FALLOCATE
392 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
394 return zpl_fallocate_common(ip, mode, offset, len);
396 #endif /* HAVE_INODE_FALLOCATE */
399 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
400 zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
402 unsigned int flags = (nd ? nd->flags : 0);
404 zpl_revalidate(struct dentry *dentry, unsigned int flags)
406 #endif /* HAVE_D_REVALIDATE_NAMEIDATA */
407 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
410 if (flags & LOOKUP_RCU)
414 * After a rollback negative dentries created before the rollback
415 * time must be invalidated. Otherwise they can obscure files which
416 * are only present in the rolled back dataset.
418 if (dentry->d_inode == NULL) {
419 spin_lock(&dentry->d_lock);
420 error = time_before(dentry->d_time, zsb->z_rollback_time);
421 spin_unlock(&dentry->d_lock);
428 * The dentry may reference a stale inode if a mounted file system
429 * was rolled back to a point in time where the object didn't exist.
431 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
437 const struct inode_operations zpl_inode_operations = {
438 .create = zpl_create,
440 .unlink = zpl_unlink,
441 .symlink = zpl_symlink,
445 .rename = zpl_rename,
446 .setattr = zpl_setattr,
447 .getattr = zpl_getattr,
448 .setxattr = generic_setxattr,
449 .getxattr = generic_getxattr,
450 .removexattr = generic_removexattr,
451 .listxattr = zpl_xattr_list,
452 #ifdef HAVE_INODE_TRUNCATE_RANGE
453 .truncate_range = zpl_truncate_range,
454 #endif /* HAVE_INODE_TRUNCATE_RANGE */
455 #ifdef HAVE_INODE_FALLOCATE
456 .fallocate = zpl_fallocate,
457 #endif /* HAVE_INODE_FALLOCATE */
460 const struct inode_operations zpl_dir_inode_operations = {
461 .create = zpl_create,
462 .lookup = zpl_lookup,
464 .unlink = zpl_unlink,
465 .symlink = zpl_symlink,
469 .rename = zpl_rename,
470 .setattr = zpl_setattr,
471 .getattr = zpl_getattr,
472 .setxattr = generic_setxattr,
473 .getxattr = generic_getxattr,
474 .removexattr = generic_removexattr,
475 .listxattr = zpl_xattr_list,
478 const struct inode_operations zpl_symlink_inode_operations = {
479 .readlink = generic_readlink,
480 .follow_link = zpl_follow_link,
481 .put_link = zpl_put_link,
482 .setattr = zpl_setattr,
483 .getattr = zpl_getattr,
484 .setxattr = generic_setxattr,
485 .getxattr = generic_getxattr,
486 .removexattr = generic_removexattr,
487 .listxattr = zpl_xattr_list,
490 const struct inode_operations zpl_special_inode_operations = {
491 .setattr = zpl_setattr,
492 .getattr = zpl_getattr,
493 .setxattr = generic_setxattr,
494 .getxattr = generic_getxattr,
495 .removexattr = generic_removexattr,
496 .listxattr = zpl_xattr_list,
499 dentry_operations_t zpl_dentry_operations = {
500 .d_revalidate = zpl_revalidate,