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