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