Don't store rdev in SA for FIFOs and sockets
[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         cred_t *cr = CRED();
169         vattr_t *vap;
170         struct inode *ip;
171         int error;
172
173         ip = dentry->d_inode;
174         crhold(cr);
175         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
176
177         error = -zfs_getattr(ip, vap, 0, cr);
178         if (error)
179                 goto out;
180
181         stat->ino = ip->i_ino;
182         stat->dev = ip->i_sb->s_dev;
183         stat->mode = vap->va_mode;
184         stat->nlink = vap->va_nlink;
185         stat->uid = vap->va_uid;
186         stat->gid = vap->va_gid;
187         stat->rdev = vap->va_rdev;
188         stat->size = vap->va_size;
189         stat->atime = vap->va_atime;
190         stat->mtime = vap->va_mtime;
191         stat->ctime = vap->va_ctime;
192         stat->blksize = vap->va_blksize;
193         stat->blocks = vap->va_nblocks;
194 out:
195         kmem_free(vap, sizeof(vattr_t));
196         crfree(cr);
197         ASSERT3S(error, <=, 0);
198
199         return (error);
200 }
201
202 static int
203 zpl_setattr(struct dentry *dentry, struct iattr *ia)
204 {
205         cred_t *cr = CRED();
206         vattr_t *vap;
207         int error;
208
209         error = inode_change_ok(dentry->d_inode, ia);
210         if (error)
211                 return (error);
212
213         crhold(cr);
214         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
215         vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
216         vap->va_mode = ia->ia_mode;
217         vap->va_uid = ia->ia_uid;
218         vap->va_gid = ia->ia_gid;
219         vap->va_size = ia->ia_size;
220         vap->va_atime = ia->ia_atime;
221         vap->va_mtime = ia->ia_mtime;
222         vap->va_ctime = ia->ia_ctime;
223
224         error = -zfs_setattr(dentry->d_inode, vap, 0, cr);
225
226         kmem_free(vap, sizeof(vattr_t));
227         crfree(cr);
228         ASSERT3S(error, <=, 0);
229
230         return (error);
231 }
232
233 static int
234 zpl_rename(struct inode *sdip, struct dentry *sdentry,
235     struct inode *tdip, struct dentry *tdentry)
236 {
237         cred_t *cr = CRED();
238         int error;
239
240         crhold(cr);
241         error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
242         crfree(cr);
243         ASSERT3S(error, <=, 0);
244
245         return (error);
246 }
247
248 static int
249 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
250 {
251         cred_t *cr = CRED();
252         vattr_t *vap;
253         struct inode *ip;
254         int error;
255
256         crhold(cr);
257         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
258         vap->va_mode = S_IFLNK | S_IRWXUGO;
259         vap->va_mask = ATTR_MODE;
260         vap->va_uid = crgetfsuid(cr);
261         vap->va_gid = crgetfsgid(cr);
262         vap->va_dentry = dentry;
263
264         error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
265         kmem_free(vap, sizeof(vattr_t));
266         crfree(cr);
267         ASSERT3S(error, <=, 0);
268
269         return (error);
270 }
271
272 static void *
273 zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
274 {
275         cred_t *cr = CRED();
276         struct inode *ip = dentry->d_inode;
277         struct iovec iov;
278         uio_t uio;
279         char *link;
280         int error;
281
282         crhold(cr);
283
284         iov.iov_len = MAXPATHLEN;
285         iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
286
287         uio.uio_iov = &iov;
288         uio.uio_iovcnt = 1;
289         uio.uio_resid = (MAXPATHLEN - 1);
290         uio.uio_segflg = UIO_SYSSPACE;
291
292         error = -zfs_readlink(ip, &uio, cr);
293         if (error) {
294                 kmem_free(link, MAXPATHLEN);
295                 nd_set_link(nd, ERR_PTR(error));
296         } else {
297                 nd_set_link(nd, link);
298         }
299
300         crfree(cr);
301         return (NULL);
302 }
303
304 static void
305 zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
306 {
307         char *link;
308
309         link = nd_get_link(nd);
310         if (!IS_ERR(link))
311                 kmem_free(link, MAXPATHLEN);
312 }
313
314 static int
315 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
316 {
317         cred_t *cr = CRED();
318         struct inode *ip = old_dentry->d_inode;
319         int error;
320
321         if (ip->i_nlink >= ZFS_LINK_MAX)
322                 return -EMLINK;
323
324         crhold(cr);
325         ip->i_ctime = CURRENT_TIME_SEC;
326         igrab(ip); /* Use ihold() if available */
327
328         error = -zfs_link(dir, ip, dname(dentry), cr);
329         if (error) {
330                 iput(ip);
331                 goto out;
332         }
333
334         d_instantiate(dentry, ip);
335 out:
336         crfree(cr);
337         ASSERT3S(error, <=, 0);
338
339         return (error);
340 }
341
342 const struct inode_operations zpl_inode_operations = {
343         .create         = zpl_create,
344         .link           = zpl_link,
345         .unlink         = zpl_unlink,
346         .symlink        = zpl_symlink,
347         .mkdir          = zpl_mkdir,
348         .rmdir          = zpl_rmdir,
349         .mknod          = zpl_mknod,
350         .rename         = zpl_rename,
351         .setattr        = zpl_setattr,
352         .getattr        = zpl_getattr,
353         .setxattr       = generic_setxattr,
354         .getxattr       = generic_getxattr,
355         .removexattr    = generic_removexattr,
356         .listxattr      = zpl_xattr_list,
357 };
358
359 const struct inode_operations zpl_dir_inode_operations = {
360         .create         = zpl_create,
361         .lookup         = zpl_lookup,
362         .link           = zpl_link,
363         .unlink         = zpl_unlink,
364         .symlink        = zpl_symlink,
365         .mkdir          = zpl_mkdir,
366         .rmdir          = zpl_rmdir,
367         .mknod          = zpl_mknod,
368         .rename         = zpl_rename,
369         .setattr        = zpl_setattr,
370         .getattr        = zpl_getattr,
371         .setxattr       = generic_setxattr,
372         .getxattr       = generic_getxattr,
373         .removexattr    = generic_removexattr,
374         .listxattr      = zpl_xattr_list,
375 };
376
377 const struct inode_operations zpl_symlink_inode_operations = {
378         .readlink       = generic_readlink,
379         .follow_link    = zpl_follow_link,
380         .put_link       = zpl_put_link,
381 };
382
383 const struct inode_operations zpl_special_inode_operations = {
384         .setattr        = zpl_setattr,
385         .getattr        = zpl_getattr,
386         .setxattr       = generic_setxattr,
387         .getxattr       = generic_getxattr,
388         .removexattr    = generic_removexattr,
389         .listxattr      = zpl_xattr_list,
390 };