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