Linux 3.6 compat, iops->create()
[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         if (error) {
50                 if (error == -ENOENT)
51                         return d_splice_alias(NULL, dentry);
52                 else
53                         return ERR_PTR(error);
54         }
55
56         return d_splice_alias(ip, dentry);
57 }
58
59 void
60 zpl_vap_init(vattr_t *vap, struct inode *dir, struct dentry *dentry,
61     zpl_umode_t mode, cred_t *cr)
62 {
63         vap->va_mask = ATTR_MODE;
64         vap->va_mode = mode;
65         vap->va_dentry = dentry;
66         vap->va_uid = crgetfsuid(cr);
67
68         if (dir && dir->i_mode & S_ISGID) {
69                 vap->va_gid = dir->i_gid;
70                 if (S_ISDIR(mode))
71                         vap->va_mode |= S_ISGID;
72         } else {
73                 vap->va_gid = crgetfsgid(cr);
74         }
75 }
76
77 static int
78 #ifdef HAVE_CREATE_NAMEIDATA
79 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
80     struct nameidata *nd)
81 #else
82 zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
83     bool flag)
84 #endif
85 {
86         cred_t *cr = CRED();
87         struct inode *ip;
88         vattr_t *vap;
89         int error;
90
91         crhold(cr);
92         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
93         zpl_vap_init(vap, dir, dentry, mode, cr);
94
95         error = -zfs_create(dir, (char *)dentry->d_name.name,
96             vap, 0, mode, &ip, cr, 0, NULL);
97         kmem_free(vap, sizeof(vattr_t));
98         crfree(cr);
99         ASSERT3S(error, <=, 0);
100
101         return (error);
102 }
103
104 static int
105 zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
106     dev_t rdev)
107 {
108         cred_t *cr = CRED();
109         struct inode *ip;
110         vattr_t *vap;
111         int error;
112
113         /*
114          * We currently expect Linux to supply rdev=0 for all sockets
115          * and fifos, but we want to know if this behavior ever changes.
116          */
117         if (S_ISSOCK(mode) || S_ISFIFO(mode))
118                 ASSERT(rdev == 0);
119
120         crhold(cr);
121         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
122         zpl_vap_init(vap, dir, dentry, mode, cr);
123         vap->va_rdev = rdev;
124
125         error = -zfs_create(dir, (char *)dentry->d_name.name,
126             vap, 0, mode, &ip, cr, 0, NULL);
127         kmem_free(vap, sizeof(vattr_t));
128         crfree(cr);
129         ASSERT3S(error, <=, 0);
130
131         return (-error);
132 }
133
134 static int
135 zpl_unlink(struct inode *dir, struct dentry *dentry)
136 {
137         cred_t *cr = CRED();
138         int error;
139
140         crhold(cr);
141         error = -zfs_remove(dir, dname(dentry), cr);
142         crfree(cr);
143         ASSERT3S(error, <=, 0);
144
145         return (error);
146 }
147
148 static int
149 zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
150 {
151         cred_t *cr = CRED();
152         vattr_t *vap;
153         struct inode *ip;
154         int error;
155
156         crhold(cr);
157         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
158         zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
159
160         error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
161         kmem_free(vap, sizeof(vattr_t));
162         crfree(cr);
163         ASSERT3S(error, <=, 0);
164
165         return (error);
166 }
167
168 static int
169 zpl_rmdir(struct inode * dir, struct dentry *dentry)
170 {
171         cred_t *cr = CRED();
172         int error;
173
174         crhold(cr);
175         error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
176         crfree(cr);
177         ASSERT3S(error, <=, 0);
178
179         return (error);
180 }
181
182 static int
183 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
184 {
185         boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
186         int error;
187
188         /*
189          * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are
190          * unmounted automatically with the parent file system.  This
191          * is done on the first getattr because it's not easy to get the
192          * vfsmount structure at mount time.  This call path is explicitly
193          * marked unlikely to avoid any performance impact.  FWIW, ext4
194          * resorts to a similar trick for sysadmin convenience.
195          */
196         if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE)))
197                 mnt->mnt_flags |= MNT_SHRINKABLE;
198
199         error = -zfs_getattr_fast(dentry->d_inode, stat);
200         ASSERT3S(error, <=, 0);
201
202         return (error);
203 }
204
205 static int
206 zpl_setattr(struct dentry *dentry, struct iattr *ia)
207 {
208         cred_t *cr = CRED();
209         vattr_t *vap;
210         int error;
211
212         error = inode_change_ok(dentry->d_inode, ia);
213         if (error)
214                 return (error);
215
216         crhold(cr);
217         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
218         vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
219         vap->va_mode = ia->ia_mode;
220         vap->va_uid = ia->ia_uid;
221         vap->va_gid = ia->ia_gid;
222         vap->va_size = ia->ia_size;
223         vap->va_atime = ia->ia_atime;
224         vap->va_mtime = ia->ia_mtime;
225         vap->va_ctime = ia->ia_ctime;
226
227         error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
228
229         kmem_free(vap, sizeof(vattr_t));
230         crfree(cr);
231         ASSERT3S(error, <=, 0);
232
233         return (error);
234 }
235
236 static int
237 zpl_rename(struct inode *sdip, struct dentry *sdentry,
238     struct inode *tdip, struct dentry *tdentry)
239 {
240         cred_t *cr = CRED();
241         int error;
242
243         crhold(cr);
244         error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
245         crfree(cr);
246         ASSERT3S(error, <=, 0);
247
248         return (error);
249 }
250
251 static int
252 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
253 {
254         cred_t *cr = CRED();
255         vattr_t *vap;
256         struct inode *ip;
257         int error;
258
259         crhold(cr);
260         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
261         zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
262
263         error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
264         kmem_free(vap, sizeof(vattr_t));
265         crfree(cr);
266         ASSERT3S(error, <=, 0);
267
268         return (error);
269 }
270
271 static void *
272 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
273 {
274         cred_t *cr = CRED();
275         struct inode *ip = dentry->d_inode;
276         struct iovec iov;
277         uio_t uio;
278         char *link;
279         int error;
280
281         crhold(cr);
282
283         iov.iov_len = MAXPATHLEN;
284         iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
285
286         uio.uio_iov = &iov;
287         uio.uio_iovcnt = 1;
288         uio.uio_resid = (MAXPATHLEN - 1);
289         uio.uio_segflg = UIO_SYSSPACE;
290
291         error = -zfs_readlink(ip, &uio, cr);
292         if (error) {
293                 kmem_free(link, MAXPATHLEN);
294                 nd_set_link(nd, ERR_PTR(error));
295         } else {
296                 nd_set_link(nd, link);
297         }
298
299         crfree(cr);
300         return (NULL);
301 }
302
303 static void
304 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
305 {
306         const char *link = nd_get_link(nd);
307
308         if (!IS_ERR(link))
309                 kmem_free(link, MAXPATHLEN);
310 }
311
312 static int
313 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
314 {
315         cred_t *cr = CRED();
316         struct inode *ip = old_dentry->d_inode;
317         int error;
318
319         if (ip->i_nlink >= ZFS_LINK_MAX)
320                 return -EMLINK;
321
322         crhold(cr);
323         ip->i_ctime = CURRENT_TIME_SEC;
324         igrab(ip); /* Use ihold() if available */
325
326         error = -zfs_link(dir, ip, dname(dentry), cr);
327         if (error) {
328                 iput(ip);
329                 goto out;
330         }
331
332         d_instantiate(dentry, ip);
333 out:
334         crfree(cr);
335         ASSERT3S(error, <=, 0);
336
337         return (error);
338 }
339
340 #ifdef HAVE_INODE_TRUNCATE_RANGE
341 static void
342 zpl_truncate_range(struct inode* ip, loff_t start, loff_t end)
343 {
344         cred_t *cr = CRED();
345         flock64_t bf;
346
347         ASSERT3S(start, <=, end);
348
349         /*
350          * zfs_freesp() will interpret (len == 0) as meaning "truncate until
351          * the end of the file". We don't want that.
352          */
353         if (start == end)
354                 return;
355
356         crhold(cr);
357
358         bf.l_type = F_WRLCK;
359         bf.l_whence = 0;
360         bf.l_start = start;
361         bf.l_len = end - start;
362         bf.l_pid = 0;
363         zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr);
364
365         crfree(cr);
366 }
367 #endif /* HAVE_INODE_TRUNCATE_RANGE */
368
369 #ifdef HAVE_INODE_FALLOCATE
370 static long
371 zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len)
372 {
373         return zpl_fallocate_common(ip, mode, offset, len);
374 }
375 #endif /* HAVE_INODE_FALLOCATE */
376
377
378 const struct inode_operations zpl_inode_operations = {
379         .create         = zpl_create,
380         .link           = zpl_link,
381         .unlink         = zpl_unlink,
382         .symlink        = zpl_symlink,
383         .mkdir          = zpl_mkdir,
384         .rmdir          = zpl_rmdir,
385         .mknod          = zpl_mknod,
386         .rename         = zpl_rename,
387         .setattr        = zpl_setattr,
388         .getattr        = zpl_getattr,
389         .setxattr       = generic_setxattr,
390         .getxattr       = generic_getxattr,
391         .removexattr    = generic_removexattr,
392         .listxattr      = zpl_xattr_list,
393 #ifdef HAVE_INODE_TRUNCATE_RANGE
394         .truncate_range = zpl_truncate_range,
395 #endif /* HAVE_INODE_TRUNCATE_RANGE */
396 #ifdef HAVE_INODE_FALLOCATE
397         .fallocate      = zpl_fallocate,
398 #endif /* HAVE_INODE_FALLOCATE */
399 };
400
401 const struct inode_operations zpl_dir_inode_operations = {
402         .create         = zpl_create,
403         .lookup         = zpl_lookup,
404         .link           = zpl_link,
405         .unlink         = zpl_unlink,
406         .symlink        = zpl_symlink,
407         .mkdir          = zpl_mkdir,
408         .rmdir          = zpl_rmdir,
409         .mknod          = zpl_mknod,
410         .rename         = zpl_rename,
411         .setattr        = zpl_setattr,
412         .getattr        = zpl_getattr,
413         .setxattr       = generic_setxattr,
414         .getxattr       = generic_getxattr,
415         .removexattr    = generic_removexattr,
416         .listxattr      = zpl_xattr_list,
417 };
418
419 const struct inode_operations zpl_symlink_inode_operations = {
420         .readlink       = generic_readlink,
421         .follow_link    = zpl_follow_link,
422         .put_link       = zpl_put_link,
423         .setattr        = zpl_setattr,
424         .getattr        = zpl_getattr,
425         .setxattr       = generic_setxattr,
426         .getxattr       = generic_getxattr,
427         .removexattr    = generic_removexattr,
428         .listxattr      = zpl_xattr_list,
429 };
430
431 const struct inode_operations zpl_special_inode_operations = {
432         .setattr        = zpl_setattr,
433         .getattr        = zpl_getattr,
434         .setxattr       = generic_setxattr,
435         .getxattr       = generic_getxattr,
436         .removexattr    = generic_removexattr,
437         .listxattr      = zpl_xattr_list,
438 };