Improve fstat(2) performance
[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/vfs.h>
29 #include <sys/zpl.h>
30
31
32 static struct dentry *
33 zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
34 {
35         cred_t *cr = CRED();
36         struct inode *ip;
37         int error;
38
39         crhold(cr);
40         error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
41         ASSERT3S(error, <=, 0);
42         crfree(cr);
43
44         if (error) {
45                 if (error == -ENOENT)
46                         return d_splice_alias(NULL, dentry);
47                 else
48                         return ERR_PTR(error);
49         }
50
51         return d_splice_alias(ip, dentry);
52 }
53
54 static int
55 zpl_create(struct inode *dir, struct dentry *dentry, int mode,
56     struct nameidata *nd)
57 {
58         cred_t *cr = CRED();
59         struct inode *ip;
60         vattr_t *vap;
61         int error;
62
63         crhold(cr);
64         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
65         vap->va_mode = mode;
66         vap->va_mask = ATTR_MODE;
67         vap->va_uid = crgetfsuid(cr);
68         vap->va_gid = crgetfsgid(cr);
69         vap->va_dentry = dentry;
70
71         error = -zfs_create(dir, (char *)dentry->d_name.name,
72             vap, 0, mode, &ip, cr, 0, NULL);
73         kmem_free(vap, sizeof(vattr_t));
74         crfree(cr);
75         ASSERT3S(error, <=, 0);
76
77         return (error);
78 }
79
80 static int
81 zpl_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
82 {
83         cred_t *cr = CRED();
84         struct inode *ip;
85         vattr_t *vap;
86         int error;
87
88         /*
89          * We currently expect Linux to supply rdev=0 for all sockets
90          * and fifos, but we want to know if this behavior ever changes.
91          */
92         if (S_ISSOCK(mode) || S_ISFIFO(mode))
93                 ASSERT(rdev == 0);
94
95         crhold(cr);
96         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
97         vap->va_mode = mode;
98         vap->va_mask = ATTR_MODE;
99         vap->va_rdev = rdev;
100         vap->va_uid = crgetfsuid(cr);
101         vap->va_gid = crgetfsgid(cr);
102         vap->va_dentry = dentry;
103
104         error = -zfs_create(dir, (char *)dentry->d_name.name,
105             vap, 0, mode, &ip, cr, 0, NULL);
106         kmem_free(vap, sizeof(vattr_t));
107         crfree(cr);
108         ASSERT3S(error, <=, 0);
109
110         return (-error);
111 }
112
113 static int
114 zpl_unlink(struct inode *dir, struct dentry *dentry)
115 {
116         cred_t *cr = CRED();
117         int error;
118
119         crhold(cr);
120         error = -zfs_remove(dir, dname(dentry), cr);
121         crfree(cr);
122         ASSERT3S(error, <=, 0);
123
124         return (error);
125 }
126
127 static int
128 zpl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
129 {
130         cred_t *cr = CRED();
131         vattr_t *vap;
132         struct inode *ip;
133         int error;
134
135         crhold(cr);
136         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
137         vap->va_mode = S_IFDIR | mode;
138         vap->va_mask = ATTR_MODE;
139         vap->va_uid = crgetfsuid(cr);
140         vap->va_gid = crgetfsgid(cr);
141         vap->va_dentry = dentry;
142
143         error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
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_rmdir(struct inode * dir, struct dentry *dentry)
153 {
154         cred_t *cr = CRED();
155         int error;
156
157         crhold(cr);
158         error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
159         crfree(cr);
160         ASSERT3S(error, <=, 0);
161
162         return (error);
163 }
164
165 static int
166 zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
167 {
168         int error;
169
170         error = -zfs_getattr_fast(dentry->d_inode, stat);
171         ASSERT3S(error, <=, 0);
172
173         return (error);
174 }
175
176 static int
177 zpl_setattr(struct dentry *dentry, struct iattr *ia)
178 {
179         cred_t *cr = CRED();
180         vattr_t *vap;
181         int error;
182
183         error = inode_change_ok(dentry->d_inode, ia);
184         if (error)
185                 return (error);
186
187         crhold(cr);
188         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
189         vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
190         vap->va_mode = ia->ia_mode;
191         vap->va_uid = ia->ia_uid;
192         vap->va_gid = ia->ia_gid;
193         vap->va_size = ia->ia_size;
194         vap->va_atime = ia->ia_atime;
195         vap->va_mtime = ia->ia_mtime;
196         vap->va_ctime = ia->ia_ctime;
197
198         error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
199
200         kmem_free(vap, sizeof(vattr_t));
201         crfree(cr);
202         ASSERT3S(error, <=, 0);
203
204         return (error);
205 }
206
207 static int
208 zpl_rename(struct inode *sdip, struct dentry *sdentry,
209     struct inode *tdip, struct dentry *tdentry)
210 {
211         cred_t *cr = CRED();
212         int error;
213
214         crhold(cr);
215         error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
216         crfree(cr);
217         ASSERT3S(error, <=, 0);
218
219         return (error);
220 }
221
222 static int
223 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
224 {
225         cred_t *cr = CRED();
226         vattr_t *vap;
227         struct inode *ip;
228         int error;
229
230         crhold(cr);
231         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
232         vap->va_mode = S_IFLNK | S_IRWXUGO;
233         vap->va_mask = ATTR_MODE;
234         vap->va_uid = crgetfsuid(cr);
235         vap->va_gid = crgetfsgid(cr);
236         vap->va_dentry = dentry;
237
238         error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
239         kmem_free(vap, sizeof(vattr_t));
240         crfree(cr);
241         ASSERT3S(error, <=, 0);
242
243         return (error);
244 }
245
246 static void *
247 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
248 {
249         cred_t *cr = CRED();
250         struct inode *ip = dentry->d_inode;
251         struct iovec iov;
252         uio_t uio;
253         char *link;
254         int error;
255
256         crhold(cr);
257
258         iov.iov_len = MAXPATHLEN;
259         iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
260
261         uio.uio_iov = &iov;
262         uio.uio_iovcnt = 1;
263         uio.uio_resid = (MAXPATHLEN - 1);
264         uio.uio_segflg = UIO_SYSSPACE;
265
266         error = -zfs_readlink(ip, &uio, cr);
267         if (error) {
268                 kmem_free(link, MAXPATHLEN);
269                 nd_set_link(nd, ERR_PTR(error));
270         } else {
271                 nd_set_link(nd, link);
272         }
273
274         crfree(cr);
275         return (NULL);
276 }
277
278 static void
279 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
280 {
281         char *link;
282
283         link = nd_get_link(nd);
284         if (!IS_ERR(link))
285                 kmem_free(link, MAXPATHLEN);
286 }
287
288 static int
289 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
290 {
291         cred_t *cr = CRED();
292         struct inode *ip = old_dentry->d_inode;
293         int error;
294
295         if (ip->i_nlink >= ZFS_LINK_MAX)
296                 return -EMLINK;
297
298         crhold(cr);
299         ip->i_ctime = CURRENT_TIME_SEC;
300         igrab(ip); /* Use ihold() if available */
301
302         error = -zfs_link(dir, ip, dname(dentry), cr);
303         if (error) {
304                 iput(ip);
305                 goto out;
306         }
307
308         d_instantiate(dentry, ip);
309 out:
310         crfree(cr);
311         ASSERT3S(error, <=, 0);
312
313         return (error);
314 }
315
316 const struct inode_operations zpl_inode_operations = {
317         .create         = zpl_create,
318         .link           = zpl_link,
319         .unlink         = zpl_unlink,
320         .symlink        = zpl_symlink,
321         .mkdir          = zpl_mkdir,
322         .rmdir          = zpl_rmdir,
323         .mknod          = zpl_mknod,
324         .rename         = zpl_rename,
325         .setattr        = zpl_setattr,
326         .getattr        = zpl_getattr,
327         .setxattr       = generic_setxattr,
328         .getxattr       = generic_getxattr,
329         .removexattr    = generic_removexattr,
330         .listxattr      = zpl_xattr_list,
331 };
332
333 const struct inode_operations zpl_dir_inode_operations = {
334         .create         = zpl_create,
335         .lookup         = zpl_lookup,
336         .link           = zpl_link,
337         .unlink         = zpl_unlink,
338         .symlink        = zpl_symlink,
339         .mkdir          = zpl_mkdir,
340         .rmdir          = zpl_rmdir,
341         .mknod          = zpl_mknod,
342         .rename         = zpl_rename,
343         .setattr        = zpl_setattr,
344         .getattr        = zpl_getattr,
345         .setxattr       = generic_setxattr,
346         .getxattr       = generic_getxattr,
347         .removexattr    = generic_removexattr,
348         .listxattr      = zpl_xattr_list,
349 };
350
351 const struct inode_operations zpl_symlink_inode_operations = {
352         .readlink       = generic_readlink,
353         .follow_link    = zpl_follow_link,
354         .put_link       = zpl_put_link,
355 };
356
357 const struct inode_operations zpl_special_inode_operations = {
358         .setattr        = zpl_setattr,
359         .getattr        = zpl_getattr,
360         .setxattr       = generic_setxattr,
361         .getxattr       = generic_getxattr,
362         .removexattr    = generic_removexattr,
363         .listxattr      = zpl_xattr_list,
364 };