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);
332 zpl_truncate_range(struct inode
* ip
, loff_t start
, loff_t end
)
337 ASSERT3S(start
, <=, end
);
340 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
341 * the end of the file". We don't want that.
351 bf
.l_len
= end
- start
;
353 zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, start
, cr
);
358 #ifdef HAVE_INODE_FALLOCATE
360 zpl_fallocate(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
362 return zpl_fallocate_common(ip
, mode
, offset
, len
);
364 #endif /* HAVE_INODE_FALLOCATE */
367 const struct inode_operations zpl_inode_operations
= {
368 .create
= zpl_create
,
370 .unlink
= zpl_unlink
,
371 .symlink
= zpl_symlink
,
375 .rename
= zpl_rename
,
376 .setattr
= zpl_setattr
,
377 .getattr
= zpl_getattr
,
378 .setxattr
= generic_setxattr
,
379 .getxattr
= generic_getxattr
,
380 .removexattr
= generic_removexattr
,
381 .listxattr
= zpl_xattr_list
,
382 .truncate_range
= zpl_truncate_range
,
383 #ifdef HAVE_INODE_FALLOCATE
384 .fallocate
= zpl_fallocate
,
385 #endif /* HAVE_INODE_FALLOCATE */
388 const struct inode_operations zpl_dir_inode_operations
= {
389 .create
= zpl_create
,
390 .lookup
= zpl_lookup
,
392 .unlink
= zpl_unlink
,
393 .symlink
= zpl_symlink
,
397 .rename
= zpl_rename
,
398 .setattr
= zpl_setattr
,
399 .getattr
= zpl_getattr
,
400 .setxattr
= generic_setxattr
,
401 .getxattr
= generic_getxattr
,
402 .removexattr
= generic_removexattr
,
403 .listxattr
= zpl_xattr_list
,
406 const struct inode_operations zpl_symlink_inode_operations
= {
407 .readlink
= generic_readlink
,
408 .follow_link
= zpl_follow_link
,
409 .put_link
= zpl_put_link
,
410 .setattr
= zpl_setattr
,
411 .getattr
= zpl_getattr
,
412 .setxattr
= generic_setxattr
,
413 .getxattr
= generic_getxattr
,
414 .removexattr
= generic_removexattr
,
415 .listxattr
= zpl_xattr_list
,
418 const struct inode_operations zpl_special_inode_operations
= {
419 .setattr
= zpl_setattr
,
420 .getattr
= zpl_getattr
,
421 .setxattr
= generic_setxattr
,
422 .getxattr
= generic_getxattr
,
423 .removexattr
= generic_removexattr
,
424 .listxattr
= zpl_xattr_list
,