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);
51 return d_splice_alias(NULL, dentry);
53 return ERR_PTR(error);
56 return d_splice_alias(ip, dentry);
60 zpl_vap_init(vattr_t *vap, struct inode *dir, struct dentry *dentry,
61 zpl_umode_t mode, cred_t *cr)
63 vap->va_mask = ATTR_MODE;
65 vap->va_dentry = dentry;
66 vap->va_uid = crgetfsuid(cr);
68 if (dir && dir->i_mode & S_ISGID) {
69 vap->va_gid = dir->i_gid;
71 vap->va_mode |= S_ISGID;
73 vap->va_gid = crgetfsgid(cr);
78 #ifdef HAVE_CREATE_NAMEIDATA
79 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
82 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
92 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
93 zpl_vap_init(vap, dir, dentry, mode, cr);
95 error = -zfs_create(dir, (char *)dentry->d_name.name,
96 vap, 0, mode, &ip, cr, 0, NULL);
97 kmem_free(vap, sizeof(vattr_t));
99 ASSERT3S(error, <=, 0);
105 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
114 * We currently expect Linux to supply rdev=0 for all sockets
115 * and fifos, but we want to know if this behavior ever changes.
117 if (S_ISSOCK(mode) || S_ISFIFO(mode))
121 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
122 zpl_vap_init(vap, dir, dentry, mode, cr);
125 error = -zfs_create(dir, (char *)dentry->d_name.name,
126 vap, 0, mode, &ip, cr, 0, NULL);
127 kmem_free(vap, sizeof(vattr_t));
129 ASSERT3S(error, <=, 0);
135 zpl_unlink(struct inode *dir, struct dentry *dentry)
141 error = -zfs_remove(dir, dname(dentry), cr);
143 ASSERT3S(error, <=, 0);
149 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
157 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
158 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
160 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
161 kmem_free(vap, sizeof(vattr_t));
163 ASSERT3S(error, <=, 0);
169 zpl_rmdir(struct inode * dir, struct dentry *dentry)
175 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
177 ASSERT3S(error, <=, 0);
183 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
185 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
189 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
190 * unmounted automatically with the parent file system. This
191 * is done on the first getattr because it's not easy to get the
192 * vfsmount structure at mount time. This call path is explicitly
193 * marked unlikely to avoid any performance impact. FWIW, ext4
194 * resorts to a similar trick for sysadmin convenience.
196 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
197 mnt->mnt_flags |= MNT_SHRINKABLE;
199 error = -zfs_getattr_fast(dentry->d_inode, stat);
200 ASSERT3S(error, <=, 0);
206 zpl_setattr(struct dentry *dentry, struct iattr *ia)
212 error = inode_change_ok(dentry->d_inode, ia);
217 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
218 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
219 vap->va_mode = ia->ia_mode;
220 vap->va_uid = ia->ia_uid;
221 vap->va_gid = ia->ia_gid;
222 vap->va_size = ia->ia_size;
223 vap->va_atime = ia->ia_atime;
224 vap->va_mtime = ia->ia_mtime;
225 vap->va_ctime = ia->ia_ctime;
227 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
229 kmem_free(vap, sizeof(vattr_t));
231 ASSERT3S(error, <=, 0);
237 zpl_rename(struct inode *sdip, struct dentry *sdentry,
238 struct inode *tdip, struct dentry *tdentry)
244 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
246 ASSERT3S(error, <=, 0);
252 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
260 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
261 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
263 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
264 kmem_free(vap, sizeof(vattr_t));
266 ASSERT3S(error, <=, 0);
272 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
275 struct inode *ip = dentry->d_inode;
283 iov.iov_len = MAXPATHLEN;
284 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
288 uio.uio_resid = (MAXPATHLEN - 1);
289 uio.uio_segflg = UIO_SYSSPACE;
291 error = -zfs_readlink(ip, &uio, cr);
293 kmem_free(link, MAXPATHLEN);
294 nd_set_link(nd, ERR_PTR(error));
296 nd_set_link(nd, link);
304 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
306 const char *link = nd_get_link(nd);
309 kmem_free(link, MAXPATHLEN);
313 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
316 struct inode *ip = old_dentry->d_inode;
319 if (ip->i_nlink >= ZFS_LINK_MAX)
323 ip->i_ctime = CURRENT_TIME_SEC;
324 igrab(ip); /* Use ihold() if available */
326 error = -zfs_link(dir, ip, dname(dentry), cr);
332 d_instantiate(dentry, ip);
335 ASSERT3S(error, <=, 0);
340 #ifdef HAVE_INODE_TRUNCATE_RANGE
342 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
347 ASSERT3S(start, <=, end);
350 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
351 * the end of the file". We don't want that.
361 bf.l_len = end - start;
363 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
367 #endif /* HAVE_INODE_TRUNCATE_RANGE */
369 #ifdef HAVE_INODE_FALLOCATE
371 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
373 return zpl_fallocate_common(ip, mode, offset, len);
375 #endif /* HAVE_INODE_FALLOCATE */
378 const struct inode_operations zpl_inode_operations = {
379 .create = zpl_create,
381 .unlink = zpl_unlink,
382 .symlink = zpl_symlink,
386 .rename = zpl_rename,
387 .setattr = zpl_setattr,
388 .getattr = zpl_getattr,
389 .setxattr = generic_setxattr,
390 .getxattr = generic_getxattr,
391 .removexattr = generic_removexattr,
392 .listxattr = zpl_xattr_list,
393 #ifdef HAVE_INODE_TRUNCATE_RANGE
394 .truncate_range = zpl_truncate_range,
395 #endif /* HAVE_INODE_TRUNCATE_RANGE */
396 #ifdef HAVE_INODE_FALLOCATE
397 .fallocate = zpl_fallocate,
398 #endif /* HAVE_INODE_FALLOCATE */
401 const struct inode_operations zpl_dir_inode_operations = {
402 .create = zpl_create,
403 .lookup = zpl_lookup,
405 .unlink = zpl_unlink,
406 .symlink = zpl_symlink,
410 .rename = zpl_rename,
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_symlink_inode_operations = {
420 .readlink = generic_readlink,
421 .follow_link = zpl_follow_link,
422 .put_link = zpl_put_link,
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,
431 const struct inode_operations zpl_special_inode_operations = {
432 .setattr = zpl_setattr,
433 .getattr = zpl_getattr,
434 .setxattr = generic_setxattr,
435 .getxattr = generic_getxattr,
436 .removexattr = generic_removexattr,
437 .listxattr = zpl_xattr_list,