]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_inode.c
cstyle: Resolve C style issues
[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 44 if (dlen(dentry) > ZFS_MAXNAMELEN)
d1d7e268 45 return (ERR_PTR(-ENAMETOOLONG));
9878a89d 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)
d1d7e268 61 return (d_splice_alias(NULL, dentry));
ee154f01 62 else
d1d7e268 63 return (ERR_PTR(error));
ee154f01
BB
64 }
65
d1d7e268 66 return (d_splice_alias(ip, dentry));
ee154f01
BB
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) {
570d6edf 77 vap->va_gid = KGID_TO_SGID(dir->i_gid);
9fd91dae
BB
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);
d1d7e268 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) {
023699cd
MM
105 VERIFY0(zpl_xattr_security_init(ip, dir, &dentry->d_name));
106 VERIFY0(zpl_init_acl(ip, dir));
7b3e34ba 107 d_instantiate(dentry, ip);
e89260a1
BB
108 }
109
d1d7e268 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);
d1d7e268 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);
023699cd 139 if (error == 0) {
227bc969 140 VERIFY0(zpl_xattr_security_init(ip, dir, &dentry->d_name));
023699cd 141 VERIFY0(zpl_init_acl(ip, dir));
7b3e34ba 142 d_instantiate(dentry, ip);
023699cd 143 }
7b3e34ba 144
d1d7e268 145 kmem_free(vap, sizeof (vattr_t));
81e97e21 146 crfree(cr);
ee154f01
BB
147 ASSERT3S(error, <=, 0);
148
34d5a5fd 149 return (error);
ee154f01
BB
150}
151
152static int
153zpl_unlink(struct inode *dir, struct dentry *dentry)
154{
81e97e21 155 cred_t *cr = CRED();
ee154f01
BB
156 int error;
157
81e97e21 158 crhold(cr);
ee154f01 159 error = -zfs_remove(dir, dname(dentry), cr);
81e97e21 160 crfree(cr);
ee154f01
BB
161 ASSERT3S(error, <=, 0);
162
163 return (error);
164}
165
166static int
b39d3b9f 167zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
ee154f01 168{
81e97e21 169 cred_t *cr = CRED();
ee154f01
BB
170 vattr_t *vap;
171 struct inode *ip;
172 int error;
173
81e97e21 174 crhold(cr);
d1d7e268 175 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
7b3e34ba 176 zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
ee154f01
BB
177
178 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
023699cd 179 if (error == 0) {
227bc969 180 VERIFY0(zpl_xattr_security_init(ip, dir, &dentry->d_name));
023699cd 181 VERIFY0(zpl_init_acl(ip, dir));
7b3e34ba 182 d_instantiate(dentry, ip);
023699cd 183 }
7b3e34ba 184
d1d7e268 185 kmem_free(vap, sizeof (vattr_t));
81e97e21 186 crfree(cr);
ee154f01
BB
187 ASSERT3S(error, <=, 0);
188
189 return (error);
190}
191
192static int
193zpl_rmdir(struct inode * dir, struct dentry *dentry)
194{
81e97e21 195 cred_t *cr = CRED();
ee154f01
BB
196 int error;
197
81e97e21 198 crhold(cr);
ee154f01 199 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
81e97e21 200 crfree(cr);
ee154f01
BB
201 ASSERT3S(error, <=, 0);
202
203 return (error);
204}
205
206static int
207zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
208{
ebe7e575 209 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
ee154f01
BB
210 int error;
211
ebe7e575
BB
212 /*
213 * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
214 * unmounted automatically with the parent file system. This
215 * is done on the first getattr because it's not easy to get the
216 * vfsmount structure at mount time. This call path is explicitly
217 * marked unlikely to avoid any performance impact. FWIW, ext4
218 * resorts to a similar trick for sysadmin convenience.
219 */
220 if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
221 mnt->mnt_flags |= MNT_SHRINKABLE;
222
057e8eee 223 error = -zfs_getattr_fast(dentry->d_inode, stat);
ee154f01
BB
224 ASSERT3S(error, <=, 0);
225
226 return (error);
227}
228
229static int
5484965a 230zpl_setattr(struct dentry *dentry, struct iattr *ia)
ee154f01 231{
023699cd 232 struct inode *ip = dentry->d_inode;
81e97e21 233 cred_t *cr = CRED();
5484965a 234 vattr_t *vap;
ee154f01
BB
235 int error;
236
023699cd 237 error = inode_change_ok(ip, ia);
ee154f01
BB
238 if (error)
239 return (error);
240
81e97e21 241 crhold(cr);
d1d7e268 242 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
5484965a
BB
243 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
244 vap->va_mode = ia->ia_mode;
570d6edf
RY
245 vap->va_uid = KUID_TO_SUID(ia->ia_uid);
246 vap->va_gid = KGID_TO_SGID(ia->ia_gid);
5484965a
BB
247 vap->va_size = ia->ia_size;
248 vap->va_atime = ia->ia_atime;
249 vap->va_mtime = ia->ia_mtime;
250 vap->va_ctime = ia->ia_ctime;
251
023699cd
MM
252 error = -zfs_setattr(ip, vap, 0, cr);
253 if (!error && (ia->ia_valid & ATTR_MODE))
254 error = zpl_chmod_acl(ip);
5484965a 255
d1d7e268 256 kmem_free(vap, sizeof (vattr_t));
81e97e21 257 crfree(cr);
ee154f01
BB
258 ASSERT3S(error, <=, 0);
259
5484965a 260 return (error);
ee154f01
BB
261}
262
263static int
264zpl_rename(struct inode *sdip, struct dentry *sdentry,
265 struct inode *tdip, struct dentry *tdentry)
266{
81e97e21 267 cred_t *cr = CRED();
ee154f01
BB
268 int error;
269
81e97e21 270 crhold(cr);
ee154f01 271 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
81e97e21 272 crfree(cr);
ee154f01
BB
273 ASSERT3S(error, <=, 0);
274
275 return (error);
276}
277
278static int
279zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
280{
81e97e21 281 cred_t *cr = CRED();
ee154f01
BB
282 vattr_t *vap;
283 struct inode *ip;
284 int error;
285
81e97e21 286 crhold(cr);
d1d7e268 287 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
7b3e34ba 288 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
ee154f01
BB
289
290 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
227bc969
MT
291 if (error == 0) {
292 VERIFY0(zpl_xattr_security_init(ip, dir, &dentry->d_name));
7b3e34ba 293 d_instantiate(dentry, ip);
227bc969 294 }
7b3e34ba 295
d1d7e268 296 kmem_free(vap, sizeof (vattr_t));
81e97e21 297 crfree(cr);
ee154f01
BB
298 ASSERT3S(error, <=, 0);
299
300 return (error);
301}
302
303static void *
304zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
305{
81e97e21 306 cred_t *cr = CRED();
8b4f9a2d
BB
307 struct inode *ip = dentry->d_inode;
308 struct iovec iov;
309 uio_t uio;
310 char *link;
8b4f9a2d
BB
311 int error;
312
81e97e21 313 crhold(cr);
8b4f9a2d
BB
314
315 iov.iov_len = MAXPATHLEN;
316 iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
317
318 uio.uio_iov = &iov;
319 uio.uio_iovcnt = 1;
320 uio.uio_resid = (MAXPATHLEN - 1);
321 uio.uio_segflg = UIO_SYSSPACE;
322
50950001 323 error = -zfs_readlink(ip, &uio, cr);
8b4f9a2d
BB
324 if (error) {
325 kmem_free(link, MAXPATHLEN);
326 nd_set_link(nd, ERR_PTR(error));
327 } else {
328 nd_set_link(nd, link);
329 }
330
81e97e21 331 crfree(cr);
8b4f9a2d 332 return (NULL);
ee154f01
BB
333}
334
335static void
336zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
337{
0a6b03d3 338 const char *link = nd_get_link(nd);
ee154f01 339
ee154f01 340 if (!IS_ERR(link))
8b4f9a2d 341 kmem_free(link, MAXPATHLEN);
ee154f01
BB
342}
343
344static int
345zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
346{
81e97e21 347 cred_t *cr = CRED();
ee154f01 348 struct inode *ip = old_dentry->d_inode;
ee154f01
BB
349 int error;
350
351 if (ip->i_nlink >= ZFS_LINK_MAX)
d1d7e268 352 return (-EMLINK);
ee154f01 353
81e97e21 354 crhold(cr);
ee154f01
BB
355 ip->i_ctime = CURRENT_TIME_SEC;
356 igrab(ip); /* Use ihold() if available */
357
358 error = -zfs_link(dir, ip, dname(dentry), cr);
359 if (error) {
360 iput(ip);
361 goto out;
362 }
363
364 d_instantiate(dentry, ip);
365out:
81e97e21 366 crfree(cr);
ee154f01
BB
367 ASSERT3S(error, <=, 0);
368
369 return (error);
370}
371
ea1fdf46 372#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57 373static void
d1d7e268 374zpl_truncate_range(struct inode *ip, loff_t start, loff_t end)
5cb63a57
ED
375{
376 cred_t *cr = CRED();
377 flock64_t bf;
378
379 ASSERT3S(start, <=, end);
380
381 /*
382 * zfs_freesp() will interpret (len == 0) as meaning "truncate until
383 * the end of the file". We don't want that.
384 */
385 if (start == end)
386 return;
387
388 crhold(cr);
389
390 bf.l_type = F_WRLCK;
391 bf.l_whence = 0;
392 bf.l_start = start;
393 bf.l_len = end - start;
394 bf.l_pid = 0;
395 zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
396
397 crfree(cr);
398}
ea1fdf46 399#endif /* HAVE_INODE_TRUNCATE_RANGE */
5cb63a57 400
cb2d1901
ED
401#ifdef HAVE_INODE_FALLOCATE
402static long
403zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
404{
d1d7e268 405 return (zpl_fallocate_common(ip, mode, offset, len));
cb2d1901
ED
406}
407#endif /* HAVE_INODE_FALLOCATE */
408
7b3e34ba
BB
409static int
410#ifdef HAVE_D_REVALIDATE_NAMEIDATA
411zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
412{
09a661e9 413 unsigned int flags = (nd ? nd->flags : 0);
7b3e34ba
BB
414#else
415zpl_revalidate(struct dentry *dentry, unsigned int flags)
416{
417#endif /* HAVE_D_REVALIDATE_NAMEIDATA */
418 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
419 int error;
420
421 if (flags & LOOKUP_RCU)
422 return (-ECHILD);
423
424 /*
425 * After a rollback negative dentries created before the rollback
426 * time must be invalidated. Otherwise they can obscure files which
427 * are only present in the rolled back dataset.
428 */
429 if (dentry->d_inode == NULL) {
430 spin_lock(&dentry->d_lock);
431 error = time_before(dentry->d_time, zsb->z_rollback_time);
432 spin_unlock(&dentry->d_lock);
433
434 if (error)
435 return (0);
436 }
437
438 /*
439 * The dentry may reference a stale inode if a mounted file system
440 * was rolled back to a point in time where the object didn't exist.
441 */
442 if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale)
443 return (0);
444
445 return (1);
446}
cb2d1901 447
ee154f01 448const struct inode_operations zpl_inode_operations = {
ee154f01
BB
449 .create = zpl_create,
450 .link = zpl_link,
451 .unlink = zpl_unlink,
452 .symlink = zpl_symlink,
453 .mkdir = zpl_mkdir,
454 .rmdir = zpl_rmdir,
455 .mknod = zpl_mknod,
456 .rename = zpl_rename,
457 .setattr = zpl_setattr,
458 .getattr = zpl_getattr,
459 .setxattr = generic_setxattr,
460 .getxattr = generic_getxattr,
461 .removexattr = generic_removexattr,
462 .listxattr = zpl_xattr_list,
ea1fdf46 463#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57 464 .truncate_range = zpl_truncate_range,
ea1fdf46 465#endif /* HAVE_INODE_TRUNCATE_RANGE */
cb2d1901
ED
466#ifdef HAVE_INODE_FALLOCATE
467 .fallocate = zpl_fallocate,
468#endif /* HAVE_INODE_FALLOCATE */
b695c34e 469#if defined(CONFIG_FS_POSIX_ACL)
023699cd
MM
470#if defined(HAVE_GET_ACL)
471 .get_acl = zpl_get_acl,
472#elif defined(HAVE_CHECK_ACL)
473 .check_acl = zpl_check_acl,
474#elif defined(HAVE_PERMISSION)
475 .permission = zpl_permission,
476#endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
b695c34e 477#endif /* CONFIG_FS_POSIX_ACL */
ee154f01
BB
478};
479
480const struct inode_operations zpl_dir_inode_operations = {
ee154f01
BB
481 .create = zpl_create,
482 .lookup = zpl_lookup,
483 .link = zpl_link,
484 .unlink = zpl_unlink,
485 .symlink = zpl_symlink,
486 .mkdir = zpl_mkdir,
487 .rmdir = zpl_rmdir,
488 .mknod = zpl_mknod,
489 .rename = zpl_rename,
490 .setattr = zpl_setattr,
a6695d83
BB
491 .getattr = zpl_getattr,
492 .setxattr = generic_setxattr,
493 .getxattr = generic_getxattr,
494 .removexattr = generic_removexattr,
495 .listxattr = zpl_xattr_list,
b695c34e 496#if defined(CONFIG_FS_POSIX_ACL)
023699cd
MM
497#if defined(HAVE_GET_ACL)
498 .get_acl = zpl_get_acl,
499#elif defined(HAVE_CHECK_ACL)
500 .check_acl = zpl_check_acl,
501#elif defined(HAVE_PERMISSION)
502 .permission = zpl_permission,
503#endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
b695c34e 504#endif /* CONFIG_FS_POSIX_ACL */
ee154f01
BB
505};
506
507const struct inode_operations zpl_symlink_inode_operations = {
ee154f01
BB
508 .readlink = generic_readlink,
509 .follow_link = zpl_follow_link,
510 .put_link = zpl_put_link,
6f2255ba
BB
511 .setattr = zpl_setattr,
512 .getattr = zpl_getattr,
f31b3ebe
BB
513 .setxattr = generic_setxattr,
514 .getxattr = generic_getxattr,
515 .removexattr = generic_removexattr,
516 .listxattr = zpl_xattr_list,
ee154f01
BB
517};
518
519const struct inode_operations zpl_special_inode_operations = {
a6695d83
BB
520 .setattr = zpl_setattr,
521 .getattr = zpl_getattr,
522 .setxattr = generic_setxattr,
523 .getxattr = generic_getxattr,
524 .removexattr = generic_removexattr,
525 .listxattr = zpl_xattr_list,
b695c34e 526#if defined(CONFIG_FS_POSIX_ACL)
023699cd
MM
527#if defined(HAVE_GET_ACL)
528 .get_acl = zpl_get_acl,
529#elif defined(HAVE_CHECK_ACL)
530 .check_acl = zpl_check_acl,
531#elif defined(HAVE_PERMISSION)
532 .permission = zpl_permission,
533#endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */
b695c34e 534#endif /* CONFIG_FS_POSIX_ACL */
ee154f01 535};
7b3e34ba
BB
536
537dentry_operations_t zpl_dentry_operations = {
538 .d_revalidate = zpl_revalidate,
539};