]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_inode.c
Update the zfs.8 "ZFS Volumes as Swap" section
[mirror_zfs.git] / module / zfs / zpl_inode.c
CommitLineData
ee154f01
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
23 */
24
25
26#include <sys/zfs_vfsops.h>
27#include <sys/zfs_vnops.h>
ebe7e575 28#include <sys/zfs_znode.h>
ee154f01
BB
29#include <sys/vfs.h>
30#include <sys/zpl.h>
31
32
33static struct dentry *
8f195a90 34#ifdef HAVE_LOOKUP_NAMEIDATA
ee154f01 35zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
8f195a90
YS
36#else
37zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
38#endif
ee154f01 39{
81e97e21 40 cred_t *cr = CRED();
ee154f01 41 struct inode *ip;
ee154f01
BB
42 int error;
43
81e97e21 44 crhold(cr);
ee154f01
BB
45 error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
46 ASSERT3S(error, <=, 0);
81e97e21 47 crfree(cr);
ee154f01 48
7b3e34ba
BB
49 spin_lock(&dentry->d_lock);
50 dentry->d_time = jiffies;
ee930353
BB
51#ifndef HAVE_S_D_OP
52 d_set_d_op(dentry, &zpl_dentry_operations);
53#endif /* HAVE_S_D_OP */
7b3e34ba
BB
54 spin_unlock(&dentry->d_lock);
55
ee154f01
BB
56 if (error) {
57 if (error == -ENOENT)
58 return d_splice_alias(NULL, dentry);
59 else
60 return ERR_PTR(error);
61 }
62
63 return d_splice_alias(ip, dentry);
64}
65
ebe7e575 66void
7b3e34ba 67zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr)
9fd91dae
BB
68{
69 vap->va_mask = ATTR_MODE;
70 vap->va_mode = mode;
9fd91dae
BB
71 vap->va_uid = crgetfsuid(cr);
72
73 if (dir && dir->i_mode & S_ISGID) {
74 vap->va_gid = dir->i_gid;
75 if (S_ISDIR(mode))
76 vap->va_mode |= S_ISGID;
77 } else {
78 vap->va_gid = crgetfsgid(cr);
79 }
80}
81
ee154f01 82static int
558ef6d0 83#ifdef HAVE_CREATE_NAMEIDATA
b39d3b9f 84zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
ee154f01 85 struct nameidata *nd)
558ef6d0
YS
86#else
87zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
88 bool flag)
89#endif
ee154f01 90{
81e97e21 91 cred_t *cr = CRED();
ee154f01
BB
92 struct inode *ip;
93 vattr_t *vap;
94 int error;
95
81e97e21 96 crhold(cr);
ee154f01 97 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
7b3e34ba 98 zpl_vap_init(vap, dir, mode, cr);
ee154f01 99
e89260a1
BB
100 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
101 if (error == 0) {
102 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
103 VERIFY3S(error, ==, 0);
7b3e34ba 104 d_instantiate(dentry, ip);
e89260a1
BB
105 }
106
ee154f01 107 kmem_free(vap, sizeof(vattr_t));
81e97e21 108 crfree(cr);
ee154f01
BB
109 ASSERT3S(error, <=, 0);
110
111 return (error);
112}
113
114static int
b39d3b9f
BB
115zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
116 dev_t rdev)
ee154f01 117{
81e97e21 118 cred_t *cr = CRED();
ee154f01
BB
119 struct inode *ip;
120 vattr_t *vap;
121 int error;
122
aa6d8c10
NB
123 /*
124 * We currently expect Linux to supply rdev=0 for all sockets
125 * and fifos, but we want to know if this behavior ever changes.
126 */
127 if (S_ISSOCK(mode) || S_ISFIFO(mode))
128 ASSERT(rdev == 0);
129
81e97e21 130 crhold(cr);
ee154f01 131 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
7b3e34ba 132 zpl_vap_init(vap, dir, mode, cr);
ee154f01 133 vap->va_rdev = rdev;
ee154f01 134
7b3e34ba 135 error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
ee930353 136 if (error == 0)
7b3e34ba 137 d_instantiate(dentry, ip);
7b3e34ba 138
ee154f01 139 kmem_free(vap, sizeof(vattr_t));
81e97e21 140 crfree(cr);
ee154f01
BB
141 ASSERT3S(error, <=, 0);
142
143 return (-error);
144}
145
146static int
147zpl_unlink(struct inode *dir, struct dentry *dentry)
148{
81e97e21 149 cred_t *cr = CRED();
ee154f01
BB
150 int error;
151
81e97e21 152 crhold(cr);
ee154f01 153 error = -zfs_remove(dir, dname(dentry), cr);
81e97e21 154 crfree(cr);
ee154f01
BB
155 ASSERT3S(error, <=, 0);
156
157 return (error);
158}
159
160static int
b39d3b9f 161zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
ee154f01 162{
81e97e21 163 cred_t *cr = CRED();
ee154f01
BB
164 vattr_t *vap;
165 struct inode *ip;
166 int error;
167
81e97e21 168 crhold(cr);
ee154f01 169 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
7b3e34ba 170 zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
ee154f01
BB
171
172 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
ee930353 173 if (error == 0)
7b3e34ba 174 d_instantiate(dentry, ip);
7b3e34ba 175
ee154f01 176 kmem_free(vap, sizeof(vattr_t));
81e97e21 177 crfree(cr);
ee154f01
BB
178 ASSERT3S(error, <=, 0);
179
180 return (error);
181}
182
183static int
184zpl_rmdir(struct inode * dir, struct dentry *dentry)
185{
81e97e21 186 cred_t *cr = CRED();
ee154f01
BB
187 int error;
188
81e97e21 189 crhold(cr);
ee154f01 190 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
81e97e21 191 crfree(cr);
ee154f01
BB
192 ASSERT3S(error, <=, 0);
193
194 return (error);
195}
196
197static int
198zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
199{
ebe7e575 200 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
ee154f01
BB
201 int error;
202
ebe7e575
BB
203 /*
204 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
205 * unmounted automatically with the parent file system. This
206 * is done on the first getattr because it's not easy to get the
207 * vfsmount structure at mount time. This call path is explicitly
208 * marked unlikely to avoid any performance impact. FWIW, ext4
209 * resorts to a similar trick for sysadmin convenience.
210 */
211 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
212 mnt->mnt_flags |= MNT_SHRINKABLE;
213
057e8eee 214 error = -zfs_getattr_fast(dentry->d_inode, stat);
ee154f01
BB
215 ASSERT3S(error, <=, 0);
216
217 return (error);
218}
219
220static int
5484965a 221zpl_setattr(struct dentry *dentry, struct iattr *ia)
ee154f01 222{
81e97e21 223 cred_t *cr = CRED();
5484965a 224 vattr_t *vap;
ee154f01
BB
225 int error;
226
5484965a 227 error = inode_change_ok(dentry->d_inode, ia);
ee154f01
BB
228 if (error)
229 return (error);
230
81e97e21 231 crhold(cr);
5484965a
BB
232 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
233 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
234 vap->va_mode = ia->ia_mode;
235 vap->va_uid = ia->ia_uid;
236 vap->va_gid = ia->ia_gid;
237 vap->va_size = ia->ia_size;
238 vap->va_atime = ia->ia_atime;
239 vap->va_mtime = ia->ia_mtime;
240 vap->va_ctime = ia->ia_ctime;
241
242 error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
243
244 kmem_free(vap, sizeof(vattr_t));
81e97e21 245 crfree(cr);
ee154f01
BB
246 ASSERT3S(error, <=, 0);
247
5484965a 248 return (error);
ee154f01
BB
249}
250
251static int
252zpl_rename(struct inode *sdip, struct dentry *sdentry,
253 struct inode *tdip, struct dentry *tdentry)
254{
81e97e21 255 cred_t *cr = CRED();
ee154f01
BB
256 int error;
257
81e97e21 258 crhold(cr);
ee154f01 259 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
81e97e21 260 crfree(cr);
ee154f01
BB
261 ASSERT3S(error, <=, 0);
262
263 return (error);
264}
265
266static int
267zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
268{
81e97e21 269 cred_t *cr = CRED();
ee154f01
BB
270 vattr_t *vap;
271 struct inode *ip;
272 int error;
273
81e97e21 274 crhold(cr);
ee154f01 275 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
7b3e34ba 276 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
ee154f01
BB
277
278 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
ee930353 279 if (error == 0)
7b3e34ba 280 d_instantiate(dentry, ip);
7b3e34ba 281
ee154f01 282 kmem_free(vap, sizeof(vattr_t));
81e97e21 283 crfree(cr);
ee154f01
BB
284 ASSERT3S(error, <=, 0);
285
286 return (error);
287}
288
289static void *
290zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
291{
81e97e21 292 cred_t *cr = CRED();
8b4f9a2d
BB
293 struct inode *ip = dentry->d_inode;
294 struct iovec iov;
295 uio_t uio;
296 char *link;
8b4f9a2d
BB
297 int error;
298
81e97e21 299 crhold(cr);
8b4f9a2d
BB
300
301 iov.iov_len = MAXPATHLEN;
302 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
303
304 uio.uio_iov = &iov;
305 uio.uio_iovcnt = 1;
306 uio.uio_resid = (MAXPATHLEN - 1);
307 uio.uio_segflg = UIO_SYSSPACE;
308
50950001 309 error = -zfs_readlink(ip, &uio, cr);
8b4f9a2d
BB
310 if (error) {
311 kmem_free(link, MAXPATHLEN);
312 nd_set_link(nd, ERR_PTR(error));
313 } else {
314 nd_set_link(nd, link);
315 }
316
81e97e21 317 crfree(cr);
8b4f9a2d 318 return (NULL);
ee154f01
BB
319}
320
321static void
322zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
323{
0a6b03d3 324 const char *link = nd_get_link(nd);
ee154f01 325
ee154f01 326 if (!IS_ERR(link))
8b4f9a2d 327 kmem_free(link, MAXPATHLEN);
ee154f01
BB
328}
329
330static int
331zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
332{
81e97e21 333 cred_t *cr = CRED();
ee154f01 334 struct inode *ip = old_dentry->d_inode;
ee154f01
BB
335 int error;
336
337 if (ip->i_nlink >= ZFS_LINK_MAX)
338 return -EMLINK;
339
81e97e21 340 crhold(cr);
ee154f01
BB
341 ip->i_ctime = CURRENT_TIME_SEC;
342 igrab(ip); /* Use ihold() if available */
343
344 error = -zfs_link(dir, ip, dname(dentry), cr);
345 if (error) {
346 iput(ip);
347 goto out;
348 }
349
350 d_instantiate(dentry, ip);
351out:
81e97e21 352 crfree(cr);
ee154f01
BB
353 ASSERT3S(error, <=, 0);
354
355 return (error);
356}
357
ea1fdf46 358#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57
ED
359static void
360zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
361{
362 cred_t *cr = CRED();
363 flock64_t bf;
364
365 ASSERT3S(start, <=, end);
366
367 /*
368 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
369 * the end of the file". We don't want that.
370 */
371 if (start == end)
372 return;
373
374 crhold(cr);
375
376 bf.l_type = F_WRLCK;
377 bf.l_whence = 0;
378 bf.l_start = start;
379 bf.l_len = end - start;
380 bf.l_pid = 0;
381 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
382
383 crfree(cr);
384}
ea1fdf46 385#endif /* HAVE_INODE_TRUNCATE_RANGE */
5cb63a57 386
cb2d1901
ED
387#ifdef HAVE_INODE_FALLOCATE
388static long
389zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
390{
391 return zpl_fallocate_common(ip, mode, offset, len);
392}
393#endif /* HAVE_INODE_FALLOCATE */
394
7b3e34ba
BB
395static int
396#ifdef HAVE_D_REVALIDATE_NAMEIDATA
397zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
398{
09a661e9 399 unsigned int flags = (nd ? nd->flags : 0);
7b3e34ba
BB
400#else
401zpl_revalidate(struct dentry *dentry, unsigned int flags)
402{
403#endif /* HAVE_D_REVALIDATE_NAMEIDATA */
404 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
405 int error;
406
407 if (flags & LOOKUP_RCU)
408 return (-ECHILD);
409
410 /*
411 * After a rollback negative dentries created before the rollback
412 * time must be invalidated. Otherwise they can obscure files which
413 * are only present in the rolled back dataset.
414 */
415 if (dentry->d_inode == NULL) {
416 spin_lock(&dentry->d_lock);
417 error = time_before(dentry->d_time, zsb->z_rollback_time);
418 spin_unlock(&dentry->d_lock);
419
420 if (error)
421 return (0);
422 }
423
424 /*
425 * The dentry may reference a stale inode if a mounted file system
426 * was rolled back to a point in time where the object didn't exist.
427 */
428 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
429 return (0);
430
431 return (1);
432}
cb2d1901 433
ee154f01 434const struct inode_operations zpl_inode_operations = {
ee154f01
BB
435 .create = zpl_create,
436 .link = zpl_link,
437 .unlink = zpl_unlink,
438 .symlink = zpl_symlink,
439 .mkdir = zpl_mkdir,
440 .rmdir = zpl_rmdir,
441 .mknod = zpl_mknod,
442 .rename = zpl_rename,
443 .setattr = zpl_setattr,
444 .getattr = zpl_getattr,
445 .setxattr = generic_setxattr,
446 .getxattr = generic_getxattr,
447 .removexattr = generic_removexattr,
448 .listxattr = zpl_xattr_list,
ea1fdf46 449#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57 450 .truncate_range = zpl_truncate_range,
ea1fdf46 451#endif /* HAVE_INODE_TRUNCATE_RANGE */
cb2d1901
ED
452#ifdef HAVE_INODE_FALLOCATE
453 .fallocate = zpl_fallocate,
454#endif /* HAVE_INODE_FALLOCATE */
ee154f01
BB
455};
456
457const struct inode_operations zpl_dir_inode_operations = {
ee154f01
BB
458 .create = zpl_create,
459 .lookup = zpl_lookup,
460 .link = zpl_link,
461 .unlink = zpl_unlink,
462 .symlink = zpl_symlink,
463 .mkdir = zpl_mkdir,
464 .rmdir = zpl_rmdir,
465 .mknod = zpl_mknod,
466 .rename = zpl_rename,
467 .setattr = zpl_setattr,
a6695d83
BB
468 .getattr = zpl_getattr,
469 .setxattr = generic_setxattr,
470 .getxattr = generic_getxattr,
471 .removexattr = generic_removexattr,
472 .listxattr = zpl_xattr_list,
ee154f01
BB
473};
474
475const struct inode_operations zpl_symlink_inode_operations = {
ee154f01
BB
476 .readlink = generic_readlink,
477 .follow_link = zpl_follow_link,
478 .put_link = zpl_put_link,
6f2255ba
BB
479 .setattr = zpl_setattr,
480 .getattr = zpl_getattr,
f31b3ebe
BB
481 .setxattr = generic_setxattr,
482 .getxattr = generic_getxattr,
483 .removexattr = generic_removexattr,
484 .listxattr = zpl_xattr_list,
ee154f01
BB
485};
486
487const struct inode_operations zpl_special_inode_operations = {
a6695d83
BB
488 .setattr = zpl_setattr,
489 .getattr = zpl_getattr,
490 .setxattr = generic_setxattr,
491 .getxattr = generic_getxattr,
492 .removexattr = generic_removexattr,
493 .listxattr = zpl_xattr_list,
ee154f01 494};
7b3e34ba
BB
495
496dentry_operations_t zpl_dentry_operations = {
497 .d_revalidate = zpl_revalidate,
498};