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