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, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
97 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
98 VERIFY3S(error, ==, 0);
101 kmem_free(vap, sizeof(vattr_t));
103 ASSERT3S(error, <=, 0);
109 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
118 * We currently expect Linux to supply rdev=0 for all sockets
119 * and fifos, but we want to know if this behavior ever changes.
121 if (S_ISSOCK(mode) || S_ISFIFO(mode))
125 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
126 zpl_vap_init(vap, dir, dentry, mode, cr);
129 error = -zfs_create(dir, (char *)dentry->d_name.name,
130 vap, 0, mode, &ip, cr, 0, NULL);
131 kmem_free(vap, sizeof(vattr_t));
133 ASSERT3S(error, <=, 0);
139 zpl_unlink(struct inode *dir, struct dentry *dentry)
145 error = -zfs_remove(dir, dname(dentry), cr);
147 ASSERT3S(error, <=, 0);
153 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
161 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
162 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
164 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
165 kmem_free(vap, sizeof(vattr_t));
167 ASSERT3S(error, <=, 0);
173 zpl_rmdir(struct inode * dir, struct dentry *dentry)
179 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
181 ASSERT3S(error, <=, 0);
187 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
189 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
193 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
194 * unmounted automatically with the parent file system. This
195 * is done on the first getattr because it's not easy to get the
196 * vfsmount structure at mount time. This call path is explicitly
197 * marked unlikely to avoid any performance impact. FWIW, ext4
198 * resorts to a similar trick for sysadmin convenience.
200 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
201 mnt->mnt_flags |= MNT_SHRINKABLE;
203 error = -zfs_getattr_fast(dentry->d_inode, stat);
204 ASSERT3S(error, <=, 0);
210 zpl_setattr(struct dentry *dentry, struct iattr *ia)
216 error = inode_change_ok(dentry->d_inode, ia);
221 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
222 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
223 vap->va_mode = ia->ia_mode;
224 vap->va_uid = ia->ia_uid;
225 vap->va_gid = ia->ia_gid;
226 vap->va_size = ia->ia_size;
227 vap->va_atime = ia->ia_atime;
228 vap->va_mtime = ia->ia_mtime;
229 vap->va_ctime = ia->ia_ctime;
231 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
233 kmem_free(vap, sizeof(vattr_t));
235 ASSERT3S(error, <=, 0);
241 zpl_rename(struct inode *sdip, struct dentry *sdentry,
242 struct inode *tdip, struct dentry *tdentry)
248 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
250 ASSERT3S(error, <=, 0);
256 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
264 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
265 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
267 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
268 kmem_free(vap, sizeof(vattr_t));
270 ASSERT3S(error, <=, 0);
276 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
279 struct inode *ip = dentry->d_inode;
287 iov.iov_len = MAXPATHLEN;
288 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
292 uio.uio_resid = (MAXPATHLEN - 1);
293 uio.uio_segflg = UIO_SYSSPACE;
295 error = -zfs_readlink(ip, &uio, cr);
297 kmem_free(link, MAXPATHLEN);
298 nd_set_link(nd, ERR_PTR(error));
300 nd_set_link(nd, link);
308 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
310 const char *link = nd_get_link(nd);
313 kmem_free(link, MAXPATHLEN);
317 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
320 struct inode *ip = old_dentry->d_inode;
323 if (ip->i_nlink >= ZFS_LINK_MAX)
327 ip->i_ctime = CURRENT_TIME_SEC;
328 igrab(ip); /* Use ihold() if available */
330 error = -zfs_link(dir, ip, dname(dentry), cr);
336 d_instantiate(dentry, ip);
339 ASSERT3S(error, <=, 0);
344 #ifdef HAVE_INODE_TRUNCATE_RANGE
346 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
351 ASSERT3S(start, <=, end);
354 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
355 * the end of the file". We don't want that.
365 bf.l_len = end - start;
367 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
371 #endif /* HAVE_INODE_TRUNCATE_RANGE */
373 #ifdef HAVE_INODE_FALLOCATE
375 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
377 return zpl_fallocate_common(ip, mode, offset, len);
379 #endif /* HAVE_INODE_FALLOCATE */
382 const struct inode_operations zpl_inode_operations = {
383 .create = zpl_create,
385 .unlink = zpl_unlink,
386 .symlink = zpl_symlink,
390 .rename = zpl_rename,
391 .setattr = zpl_setattr,
392 .getattr = zpl_getattr,
393 .setxattr = generic_setxattr,
394 .getxattr = generic_getxattr,
395 .removexattr = generic_removexattr,
396 .listxattr = zpl_xattr_list,
397 #ifdef HAVE_INODE_TRUNCATE_RANGE
398 .truncate_range = zpl_truncate_range,
399 #endif /* HAVE_INODE_TRUNCATE_RANGE */
400 #ifdef HAVE_INODE_FALLOCATE
401 .fallocate = zpl_fallocate,
402 #endif /* HAVE_INODE_FALLOCATE */
405 const struct inode_operations zpl_dir_inode_operations = {
406 .create = zpl_create,
407 .lookup = zpl_lookup,
409 .unlink = zpl_unlink,
410 .symlink = zpl_symlink,
414 .rename = zpl_rename,
415 .setattr = zpl_setattr,
416 .getattr = zpl_getattr,
417 .setxattr = generic_setxattr,
418 .getxattr = generic_getxattr,
419 .removexattr = generic_removexattr,
420 .listxattr = zpl_xattr_list,
423 const struct inode_operations zpl_symlink_inode_operations = {
424 .readlink = generic_readlink,
425 .follow_link = zpl_follow_link,
426 .put_link = zpl_put_link,
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,
435 const struct inode_operations zpl_special_inode_operations = {
436 .setattr = zpl_setattr,
437 .getattr = zpl_getattr,
438 .setxattr = generic_setxattr,
439 .getxattr = generic_getxattr,
440 .removexattr = generic_removexattr,
441 .listxattr = zpl_xattr_list,