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 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
87 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
88 zpl_vap_init(vap, dir, dentry, mode, cr);
90 error = -zfs_create(dir, (char *)dentry->d_name.name,
91 vap, 0, mode, &ip, cr, 0, NULL);
92 kmem_free(vap, sizeof(vattr_t));
94 ASSERT3S(error, <=, 0);
100 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
109 * We currently expect Linux to supply rdev=0 for all sockets
110 * and fifos, but we want to know if this behavior ever changes.
112 if (S_ISSOCK(mode) || S_ISFIFO(mode))
116 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
117 zpl_vap_init(vap, dir, dentry, mode, cr);
120 error = -zfs_create(dir, (char *)dentry->d_name.name,
121 vap, 0, mode, &ip, cr, 0, NULL);
122 kmem_free(vap, sizeof(vattr_t));
124 ASSERT3S(error, <=, 0);
130 zpl_unlink(struct inode *dir, struct dentry *dentry)
136 error = -zfs_remove(dir, dname(dentry), cr);
138 ASSERT3S(error, <=, 0);
144 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
152 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
153 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
155 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
156 kmem_free(vap, sizeof(vattr_t));
158 ASSERT3S(error, <=, 0);
164 zpl_rmdir(struct inode * dir, struct dentry *dentry)
170 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
172 ASSERT3S(error, <=, 0);
178 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
180 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
184 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
185 * unmounted automatically with the parent file system. This
186 * is done on the first getattr because it's not easy to get the
187 * vfsmount structure at mount time. This call path is explicitly
188 * marked unlikely to avoid any performance impact. FWIW, ext4
189 * resorts to a similar trick for sysadmin convenience.
191 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
192 mnt->mnt_flags |= MNT_SHRINKABLE;
194 error = -zfs_getattr_fast(dentry->d_inode, stat);
195 ASSERT3S(error, <=, 0);
201 zpl_setattr(struct dentry *dentry, struct iattr *ia)
207 error = inode_change_ok(dentry->d_inode, ia);
212 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
213 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
214 vap->va_mode = ia->ia_mode;
215 vap->va_uid = ia->ia_uid;
216 vap->va_gid = ia->ia_gid;
217 vap->va_size = ia->ia_size;
218 vap->va_atime = ia->ia_atime;
219 vap->va_mtime = ia->ia_mtime;
220 vap->va_ctime = ia->ia_ctime;
222 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
224 kmem_free(vap, sizeof(vattr_t));
226 ASSERT3S(error, <=, 0);
232 zpl_rename(struct inode *sdip, struct dentry *sdentry,
233 struct inode *tdip, struct dentry *tdentry)
239 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
241 ASSERT3S(error, <=, 0);
247 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
255 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
256 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
258 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
259 kmem_free(vap, sizeof(vattr_t));
261 ASSERT3S(error, <=, 0);
267 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
270 struct inode *ip = dentry->d_inode;
278 iov.iov_len = MAXPATHLEN;
279 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
283 uio.uio_resid = (MAXPATHLEN - 1);
284 uio.uio_segflg = UIO_SYSSPACE;
286 error = -zfs_readlink(ip, &uio, cr);
288 kmem_free(link, MAXPATHLEN);
289 nd_set_link(nd, ERR_PTR(error));
291 nd_set_link(nd, link);
299 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
301 const char *link = nd_get_link(nd);
304 kmem_free(link, MAXPATHLEN);
308 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
311 struct inode *ip = old_dentry->d_inode;
314 if (ip->i_nlink >= ZFS_LINK_MAX)
318 ip->i_ctime = CURRENT_TIME_SEC;
319 igrab(ip); /* Use ihold() if available */
321 error = -zfs_link(dir, ip, dname(dentry), cr);
327 d_instantiate(dentry, ip);
330 ASSERT3S(error, <=, 0);
335 #ifdef HAVE_INODE_TRUNCATE_RANGE
337 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
342 ASSERT3S(start, <=, end);
345 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
346 * the end of the file". We don't want that.
356 bf.l_len = end - start;
358 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
362 #endif /* HAVE_INODE_TRUNCATE_RANGE */
364 #ifdef HAVE_INODE_FALLOCATE
366 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
368 return zpl_fallocate_common(ip, mode, offset, len);
370 #endif /* HAVE_INODE_FALLOCATE */
373 const struct inode_operations zpl_inode_operations = {
374 .create = zpl_create,
376 .unlink = zpl_unlink,
377 .symlink = zpl_symlink,
381 .rename = zpl_rename,
382 .setattr = zpl_setattr,
383 .getattr = zpl_getattr,
384 .setxattr = generic_setxattr,
385 .getxattr = generic_getxattr,
386 .removexattr = generic_removexattr,
387 .listxattr = zpl_xattr_list,
388 #ifdef HAVE_INODE_TRUNCATE_RANGE
389 .truncate_range = zpl_truncate_range,
390 #endif /* HAVE_INODE_TRUNCATE_RANGE */
391 #ifdef HAVE_INODE_FALLOCATE
392 .fallocate = zpl_fallocate,
393 #endif /* HAVE_INODE_FALLOCATE */
396 const struct inode_operations zpl_dir_inode_operations = {
397 .create = zpl_create,
398 .lookup = zpl_lookup,
400 .unlink = zpl_unlink,
401 .symlink = zpl_symlink,
405 .rename = zpl_rename,
406 .setattr = zpl_setattr,
407 .getattr = zpl_getattr,
408 .setxattr = generic_setxattr,
409 .getxattr = generic_getxattr,
410 .removexattr = generic_removexattr,
411 .listxattr = zpl_xattr_list,
414 const struct inode_operations zpl_symlink_inode_operations = {
415 .readlink = generic_readlink,
416 .follow_link = zpl_follow_link,
417 .put_link = zpl_put_link,
418 .setattr = zpl_setattr,
419 .getattr = zpl_getattr,
420 .setxattr = generic_setxattr,
421 .getxattr = generic_getxattr,
422 .removexattr = generic_removexattr,
423 .listxattr = zpl_xattr_list,
426 const struct inode_operations zpl_special_inode_operations = {
427 .setattr = zpl_setattr,
428 .getattr = zpl_getattr,
429 .setxattr = generic_setxattr,
430 .getxattr = generic_getxattr,
431 .removexattr = generic_removexattr,
432 .listxattr = zpl_xattr_list,