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