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