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 zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
41 error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
42 ASSERT3S(error, <=, 0);
47 return d_splice_alias(NULL, dentry);
49 return ERR_PTR(error);
52 return d_splice_alias(ip, dentry);
56 zpl_vap_init(vattr_t *vap, struct inode *dir, struct dentry *dentry,
57 zpl_umode_t mode, cred_t *cr)
59 vap->va_mask = ATTR_MODE;
61 vap->va_dentry = dentry;
62 vap->va_uid = crgetfsuid(cr);
64 if (dir && dir->i_mode & S_ISGID) {
65 vap->va_gid = dir->i_gid;
67 vap->va_mode |= S_ISGID;
69 vap->va_gid = crgetfsgid(cr);
74 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
83 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
84 zpl_vap_init(vap, dir, dentry, mode, cr);
86 error = -zfs_create(dir, (char *)dentry->d_name.name,
87 vap, 0, mode, &ip, cr, 0, NULL);
88 kmem_free(vap, sizeof(vattr_t));
90 ASSERT3S(error, <=, 0);
96 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
105 * We currently expect Linux to supply rdev=0 for all sockets
106 * and fifos, but we want to know if this behavior ever changes.
108 if (S_ISSOCK(mode) || S_ISFIFO(mode))
112 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
113 zpl_vap_init(vap, dir, dentry, mode, cr);
116 error = -zfs_create(dir, (char *)dentry->d_name.name,
117 vap, 0, mode, &ip, cr, 0, NULL);
118 kmem_free(vap, sizeof(vattr_t));
120 ASSERT3S(error, <=, 0);
126 zpl_unlink(struct inode *dir, struct dentry *dentry)
132 error = -zfs_remove(dir, dname(dentry), cr);
134 ASSERT3S(error, <=, 0);
140 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
148 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
149 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
151 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
152 kmem_free(vap, sizeof(vattr_t));
154 ASSERT3S(error, <=, 0);
160 zpl_rmdir(struct inode * dir, struct dentry *dentry)
166 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
168 ASSERT3S(error, <=, 0);
174 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
176 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
180 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
181 * unmounted automatically with the parent file system. This
182 * is done on the first getattr because it's not easy to get the
183 * vfsmount structure at mount time. This call path is explicitly
184 * marked unlikely to avoid any performance impact. FWIW, ext4
185 * resorts to a similar trick for sysadmin convenience.
187 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
188 mnt->mnt_flags |= MNT_SHRINKABLE;
190 error = -zfs_getattr_fast(dentry->d_inode, stat);
191 ASSERT3S(error, <=, 0);
197 zpl_setattr(struct dentry *dentry, struct iattr *ia)
203 error = inode_change_ok(dentry->d_inode, ia);
208 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
209 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
210 vap->va_mode = ia->ia_mode;
211 vap->va_uid = ia->ia_uid;
212 vap->va_gid = ia->ia_gid;
213 vap->va_size = ia->ia_size;
214 vap->va_atime = ia->ia_atime;
215 vap->va_mtime = ia->ia_mtime;
216 vap->va_ctime = ia->ia_ctime;
218 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
220 kmem_free(vap, sizeof(vattr_t));
222 ASSERT3S(error, <=, 0);
228 zpl_rename(struct inode *sdip, struct dentry *sdentry,
229 struct inode *tdip, struct dentry *tdentry)
235 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
237 ASSERT3S(error, <=, 0);
243 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
251 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
252 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
254 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
255 kmem_free(vap, sizeof(vattr_t));
257 ASSERT3S(error, <=, 0);
263 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
266 struct inode *ip = dentry->d_inode;
274 iov.iov_len = MAXPATHLEN;
275 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
279 uio.uio_resid = (MAXPATHLEN - 1);
280 uio.uio_segflg = UIO_SYSSPACE;
282 error = -zfs_readlink(ip, &uio, cr);
284 kmem_free(link, MAXPATHLEN);
285 nd_set_link(nd, ERR_PTR(error));
287 nd_set_link(nd, link);
295 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
297 const char *link = nd_get_link(nd);
300 kmem_free(link, MAXPATHLEN);
304 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
307 struct inode *ip = old_dentry->d_inode;
310 if (ip->i_nlink >= ZFS_LINK_MAX)
314 ip->i_ctime = CURRENT_TIME_SEC;
315 igrab(ip); /* Use ihold() if available */
317 error = -zfs_link(dir, ip, dname(dentry), cr);
323 d_instantiate(dentry, ip);
326 ASSERT3S(error, <=, 0);
331 #ifdef HAVE_INODE_TRUNCATE_RANGE
333 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
338 ASSERT3S(start, <=, end);
341 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
342 * the end of the file". We don't want that.
352 bf.l_len = end - start;
354 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
358 #endif /* HAVE_INODE_TRUNCATE_RANGE */
360 #ifdef HAVE_INODE_FALLOCATE
362 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
364 return zpl_fallocate_common(ip, mode, offset, len);
366 #endif /* HAVE_INODE_FALLOCATE */
369 const struct inode_operations zpl_inode_operations = {
370 .create = zpl_create,
372 .unlink = zpl_unlink,
373 .symlink = zpl_symlink,
377 .rename = zpl_rename,
378 .setattr = zpl_setattr,
379 .getattr = zpl_getattr,
380 .setxattr = generic_setxattr,
381 .getxattr = generic_getxattr,
382 .removexattr = generic_removexattr,
383 .listxattr = zpl_xattr_list,
384 #ifdef HAVE_INODE_TRUNCATE_RANGE
385 .truncate_range = zpl_truncate_range,
386 #endif /* HAVE_INODE_TRUNCATE_RANGE */
387 #ifdef HAVE_INODE_FALLOCATE
388 .fallocate = zpl_fallocate,
389 #endif /* HAVE_INODE_FALLOCATE */
392 const struct inode_operations zpl_dir_inode_operations = {
393 .create = zpl_create,
394 .lookup = zpl_lookup,
396 .unlink = zpl_unlink,
397 .symlink = zpl_symlink,
401 .rename = zpl_rename,
402 .setattr = zpl_setattr,
403 .getattr = zpl_getattr,
404 .setxattr = generic_setxattr,
405 .getxattr = generic_getxattr,
406 .removexattr = generic_removexattr,
407 .listxattr = zpl_xattr_list,
410 const struct inode_operations zpl_symlink_inode_operations = {
411 .readlink = generic_readlink,
412 .follow_link = zpl_follow_link,
413 .put_link = zpl_put_link,
414 .setattr = zpl_setattr,
415 .getattr = zpl_getattr,
416 .setxattr = generic_setxattr,
417 .getxattr = generic_getxattr,
418 .removexattr = generic_removexattr,
419 .listxattr = zpl_xattr_list,
422 const struct inode_operations zpl_special_inode_operations = {
423 .setattr = zpl_setattr,
424 .getattr = zpl_getattr,
425 .setxattr = generic_setxattr,
426 .getxattr = generic_getxattr,
427 .removexattr = generic_removexattr,
428 .listxattr = zpl_xattr_list,