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