Fix zpl_revalidate() NULL deref
[zfs.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
44         crhold(cr);
45         error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
46         ASSERT3S(error, <=, 0);
47         crfree(cr);
48
49         spin_lock(&dentry->d_lock);
50         dentry->d_time = jiffies;
51 #ifndef HAVE_S_D_OP
52         d_set_d_op(dentry, &zpl_dentry_operations);
53 #endif /* HAVE_S_D_OP */
54         spin_unlock(&dentry->d_lock);
55
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
66 void
67 zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr)
68 {
69         vap->va_mask = ATTR_MODE;
70         vap->va_mode = mode;
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
82 static int
83 #ifdef HAVE_CREATE_NAMEIDATA
84 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
85     struct nameidata *nd)
86 #else
87 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
88     bool flag)
89 #endif
90 {
91         cred_t *cr = CRED();
92         struct inode *ip;
93         vattr_t *vap;
94         int error;
95
96         crhold(cr);
97         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
98         zpl_vap_init(vap, dir, mode, cr);
99
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);
104                 d_instantiate(dentry, ip);
105         }
106
107         kmem_free(vap, sizeof(vattr_t));
108         crfree(cr);
109         ASSERT3S(error, <=, 0);
110
111         return (error);
112 }
113
114 static int
115 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
116     dev_t rdev)
117 {
118         cred_t *cr = CRED();
119         struct inode *ip;
120         vattr_t *vap;
121         int error;
122
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
130         crhold(cr);
131         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
132         zpl_vap_init(vap, dir, mode, cr);
133         vap->va_rdev = rdev;
134
135         error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL);
136         if (error == 0)
137                 d_instantiate(dentry, ip);
138
139         kmem_free(vap, sizeof(vattr_t));
140         crfree(cr);
141         ASSERT3S(error, <=, 0);
142
143         return (-error);
144 }
145
146 static int
147 zpl_unlink(struct inode *dir, struct dentry *dentry)
148 {
149         cred_t *cr = CRED();
150         int error;
151
152         crhold(cr);
153         error = -zfs_remove(dir, dname(dentry), cr);
154         crfree(cr);
155         ASSERT3S(error, <=, 0);
156
157         return (error);
158 }
159
160 static int
161 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
162 {
163         cred_t *cr = CRED();
164         vattr_t *vap;
165         struct inode *ip;
166         int error;
167
168         crhold(cr);
169         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
170         zpl_vap_init(vap, dir, mode | S_IFDIR, cr);
171
172         error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
173         if (error == 0)
174                 d_instantiate(dentry, ip);
175
176         kmem_free(vap, sizeof(vattr_t));
177         crfree(cr);
178         ASSERT3S(error, <=, 0);
179
180         return (error);
181 }
182
183 static int
184 zpl_rmdir(struct inode * dir, struct dentry *dentry)
185 {
186         cred_t *cr = CRED();
187         int error;
188
189         crhold(cr);
190         error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
191         crfree(cr);
192         ASSERT3S(error, <=, 0);
193
194         return (error);
195 }
196
197 static int
198 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
199 {
200         boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
201         int error;
202
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
214         error = -zfs_getattr_fast(dentry->d_inode, stat);
215         ASSERT3S(error, <=, 0);
216
217         return (error);
218 }
219
220 static int
221 zpl_setattr(struct dentry *dentry, struct iattr *ia)
222 {
223         cred_t *cr = CRED();
224         vattr_t *vap;
225         int error;
226
227         error = inode_change_ok(dentry->d_inode, ia);
228         if (error)
229                 return (error);
230
231         crhold(cr);
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));
245         crfree(cr);
246         ASSERT3S(error, <=, 0);
247
248         return (error);
249 }
250
251 static int
252 zpl_rename(struct inode *sdip, struct dentry *sdentry,
253     struct inode *tdip, struct dentry *tdentry)
254 {
255         cred_t *cr = CRED();
256         int error;
257
258         crhold(cr);
259         error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
260         crfree(cr);
261         ASSERT3S(error, <=, 0);
262
263         return (error);
264 }
265
266 static int
267 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
268 {
269         cred_t *cr = CRED();
270         vattr_t *vap;
271         struct inode *ip;
272         int error;
273
274         crhold(cr);
275         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
276         zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr);
277
278         error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
279         if (error == 0)
280                 d_instantiate(dentry, ip);
281
282         kmem_free(vap, sizeof(vattr_t));
283         crfree(cr);
284         ASSERT3S(error, <=, 0);
285
286         return (error);
287 }
288
289 static void *
290 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
291 {
292         cred_t *cr = CRED();
293         struct inode *ip = dentry->d_inode;
294         struct iovec iov;
295         uio_t uio;
296         char *link;
297         int error;
298
299         crhold(cr);
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
309         error = -zfs_readlink(ip, &uio, cr);
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
317         crfree(cr);
318         return (NULL);
319 }
320
321 static void
322 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
323 {
324         const char *link = nd_get_link(nd);
325
326         if (!IS_ERR(link))
327                 kmem_free(link, MAXPATHLEN);
328 }
329
330 static int
331 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
332 {
333         cred_t *cr = CRED();
334         struct inode *ip = old_dentry->d_inode;
335         int error;
336
337         if (ip->i_nlink >= ZFS_LINK_MAX)
338                 return -EMLINK;
339
340         crhold(cr);
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);
351 out:
352         crfree(cr);
353         ASSERT3S(error, <=, 0);
354
355         return (error);
356 }
357
358 #ifdef HAVE_INODE_TRUNCATE_RANGE
359 static void
360 zpl_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 }
385 #endif /* HAVE_INODE_TRUNCATE_RANGE */
386
387 #ifdef HAVE_INODE_FALLOCATE
388 static long
389 zpl_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
395 static int
396 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
397 zpl_revalidate(struct dentry *dentry, struct nameidata *nd)
398 {
399         unsigned int flags = (nd ? nd->flags : 0);
400 #else
401 zpl_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 }
433
434 const struct inode_operations zpl_inode_operations = {
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,
449 #ifdef HAVE_INODE_TRUNCATE_RANGE
450         .truncate_range = zpl_truncate_range,
451 #endif /* HAVE_INODE_TRUNCATE_RANGE */
452 #ifdef HAVE_INODE_FALLOCATE
453         .fallocate      = zpl_fallocate,
454 #endif /* HAVE_INODE_FALLOCATE */
455 };
456
457 const struct inode_operations zpl_dir_inode_operations = {
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,
468         .getattr        = zpl_getattr,
469         .setxattr       = generic_setxattr,
470         .getxattr       = generic_getxattr,
471         .removexattr    = generic_removexattr,
472         .listxattr      = zpl_xattr_list,
473 };
474
475 const struct inode_operations zpl_symlink_inode_operations = {
476         .readlink       = generic_readlink,
477         .follow_link    = zpl_follow_link,
478         .put_link       = zpl_put_link,
479         .setattr        = zpl_setattr,
480         .getattr        = zpl_getattr,
481         .setxattr       = generic_setxattr,
482         .getxattr       = generic_getxattr,
483         .removexattr    = generic_removexattr,
484         .listxattr      = zpl_xattr_list,
485 };
486
487 const struct inode_operations zpl_special_inode_operations = {
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,
494 };
495
496 dentry_operations_t zpl_dentry_operations = {
497         .d_revalidate   = zpl_revalidate,
498 };