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