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)
299 link = nd_get_link(nd);
301 kmem_free(link, MAXPATHLEN);
305 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
308 struct inode *ip = old_dentry->d_inode;
311 if (ip->i_nlink >= ZFS_LINK_MAX)
315 ip->i_ctime = CURRENT_TIME_SEC;
316 igrab(ip); /* Use ihold() if available */
318 error = -zfs_link(dir, ip, dname(dentry), cr);
324 d_instantiate(dentry, ip);
327 ASSERT3S(error, <=, 0);
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);
359 #ifdef HAVE_INODE_FALLOCATE
361 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
363 return zpl_fallocate_common(ip, mode, offset, len);
365 #endif /* HAVE_INODE_FALLOCATE */
368 const struct inode_operations zpl_inode_operations = {
369 .create = zpl_create,
371 .unlink = zpl_unlink,
372 .symlink = zpl_symlink,
376 .rename = zpl_rename,
377 .setattr = zpl_setattr,
378 .getattr = zpl_getattr,
379 .setxattr = generic_setxattr,
380 .getxattr = generic_getxattr,
381 .removexattr = generic_removexattr,
382 .listxattr = zpl_xattr_list,
383 .truncate_range = zpl_truncate_range,
384 #ifdef HAVE_INODE_FALLOCATE
385 .fallocate = zpl_fallocate,
386 #endif /* HAVE_INODE_FALLOCATE */
389 const struct inode_operations zpl_dir_inode_operations = {
390 .create = zpl_create,
391 .lookup = zpl_lookup,
393 .unlink = zpl_unlink,
394 .symlink = zpl_symlink,
398 .rename = zpl_rename,
399 .setattr = zpl_setattr,
400 .getattr = zpl_getattr,
401 .setxattr = generic_setxattr,
402 .getxattr = generic_getxattr,
403 .removexattr = generic_removexattr,
404 .listxattr = zpl_xattr_list,
407 const struct inode_operations zpl_symlink_inode_operations = {
408 .readlink = generic_readlink,
409 .follow_link = zpl_follow_link,
410 .put_link = zpl_put_link,
411 .setattr = zpl_setattr,
412 .getattr = zpl_getattr,
413 .setxattr = generic_setxattr,
414 .getxattr = generic_getxattr,
415 .removexattr = generic_removexattr,
416 .listxattr = zpl_xattr_list,
419 const struct inode_operations zpl_special_inode_operations = {
420 .setattr = zpl_setattr,
421 .getattr = zpl_getattr,
422 .setxattr = generic_setxattr,
423 .getxattr = generic_getxattr,
424 .removexattr = generic_removexattr,
425 .listxattr = zpl_xattr_list,