]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_inode.c
Fix false ENOENT on snapshot control dentries
[mirror_zfs.git] / module / zfs / zpl_inode.c
CommitLineData
ee154f01
BB
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>
ebe7e575 28#include <sys/zfs_znode.h>
ee154f01
BB
29#include <sys/vfs.h>
30#include <sys/zpl.h>
31
32
33static struct dentry *
8f195a90 34#ifdef HAVE_LOOKUP_NAMEIDATA
ee154f01 35zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
8f195a90
YS
36#else
37zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
38#endif
ee154f01 39{
81e97e21 40 cred_t *cr = CRED();
ee154f01 41 struct inode *ip;
ee154f01
BB
42 int error;
43
81e97e21 44 crhold(cr);
ee154f01
BB
45 error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, NULL);
46 ASSERT3S(error, <=, 0);
81e97e21 47 crfree(cr);
ee154f01
BB
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
ebe7e575 59void
9fd91dae 60zpl_vap_init(vattr_t *vap, struct inode *dir, struct dentry *dentry,
b39d3b9f 61 zpl_umode_t mode, cred_t *cr)
9fd91dae
BB
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
ee154f01 77static int
558ef6d0 78#ifdef HAVE_CREATE_NAMEIDATA
b39d3b9f 79zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
ee154f01 80 struct nameidata *nd)
558ef6d0
YS
81#else
82zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
83 bool flag)
84#endif
ee154f01 85{
81e97e21 86 cred_t *cr = CRED();
ee154f01
BB
87 struct inode *ip;
88 vattr_t *vap;
89 int error;
90
81e97e21 91 crhold(cr);
ee154f01 92 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 93 zpl_vap_init(vap, dir, dentry, mode, cr);
ee154f01 94
e89260a1
BB
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
ee154f01 101 kmem_free(vap, sizeof(vattr_t));
81e97e21 102 crfree(cr);
ee154f01
BB
103 ASSERT3S(error, <=, 0);
104
105 return (error);
106}
107
108static int
b39d3b9f
BB
109zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode,
110 dev_t rdev)
ee154f01 111{
81e97e21 112 cred_t *cr = CRED();
ee154f01
BB
113 struct inode *ip;
114 vattr_t *vap;
115 int error;
116
aa6d8c10
NB
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
81e97e21 124 crhold(cr);
ee154f01 125 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 126 zpl_vap_init(vap, dir, dentry, mode, cr);
ee154f01 127 vap->va_rdev = rdev;
ee154f01
BB
128
129 error = -zfs_create(dir, (char *)dentry->d_name.name,
81e97e21 130 vap, 0, mode, &ip, cr, 0, NULL);
ee154f01 131 kmem_free(vap, sizeof(vattr_t));
81e97e21 132 crfree(cr);
ee154f01
BB
133 ASSERT3S(error, <=, 0);
134
135 return (-error);
136}
137
138static int
139zpl_unlink(struct inode *dir, struct dentry *dentry)
140{
81e97e21 141 cred_t *cr = CRED();
ee154f01
BB
142 int error;
143
81e97e21 144 crhold(cr);
ee154f01 145 error = -zfs_remove(dir, dname(dentry), cr);
81e97e21 146 crfree(cr);
ee154f01
BB
147 ASSERT3S(error, <=, 0);
148
149 return (error);
150}
151
152static int
b39d3b9f 153zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode)
ee154f01 154{
81e97e21 155 cred_t *cr = CRED();
ee154f01
BB
156 vattr_t *vap;
157 struct inode *ip;
158 int error;
159
81e97e21 160 crhold(cr);
ee154f01 161 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 162 zpl_vap_init(vap, dir, dentry, mode | S_IFDIR, cr);
ee154f01
BB
163
164 error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL);
ee154f01 165 kmem_free(vap, sizeof(vattr_t));
81e97e21 166 crfree(cr);
ee154f01
BB
167 ASSERT3S(error, <=, 0);
168
169 return (error);
170}
171
172static int
173zpl_rmdir(struct inode * dir, struct dentry *dentry)
174{
81e97e21 175 cred_t *cr = CRED();
ee154f01
BB
176 int error;
177
81e97e21 178 crhold(cr);
ee154f01 179 error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0);
81e97e21 180 crfree(cr);
ee154f01
BB
181 ASSERT3S(error, <=, 0);
182
183 return (error);
184}
185
186static int
187zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
188{
ebe7e575 189 boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap;
ee154f01
BB
190 int error;
191
ebe7e575
BB
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
057e8eee 203 error = -zfs_getattr_fast(dentry->d_inode, stat);
ee154f01
BB
204 ASSERT3S(error, <=, 0);
205
206 return (error);
207}
208
209static int
5484965a 210zpl_setattr(struct dentry *dentry, struct iattr *ia)
ee154f01 211{
81e97e21 212 cred_t *cr = CRED();
5484965a 213 vattr_t *vap;
ee154f01
BB
214 int error;
215
5484965a 216 error = inode_change_ok(dentry->d_inode, ia);
ee154f01
BB
217 if (error)
218 return (error);
219
81e97e21 220 crhold(cr);
5484965a
BB
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));
81e97e21 234 crfree(cr);
ee154f01
BB
235 ASSERT3S(error, <=, 0);
236
5484965a 237 return (error);
ee154f01
BB
238}
239
240static int
241zpl_rename(struct inode *sdip, struct dentry *sdentry,
242 struct inode *tdip, struct dentry *tdentry)
243{
81e97e21 244 cred_t *cr = CRED();
ee154f01
BB
245 int error;
246
81e97e21 247 crhold(cr);
ee154f01 248 error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0);
81e97e21 249 crfree(cr);
ee154f01
BB
250 ASSERT3S(error, <=, 0);
251
252 return (error);
253}
254
255static int
256zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
257{
81e97e21 258 cred_t *cr = CRED();
ee154f01
BB
259 vattr_t *vap;
260 struct inode *ip;
261 int error;
262
81e97e21 263 crhold(cr);
ee154f01 264 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
9fd91dae 265 zpl_vap_init(vap, dir, dentry, S_IFLNK | S_IRWXUGO, cr);
ee154f01
BB
266
267 error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0);
ee154f01 268 kmem_free(vap, sizeof(vattr_t));
81e97e21 269 crfree(cr);
ee154f01
BB
270 ASSERT3S(error, <=, 0);
271
272 return (error);
273}
274
275static void *
276zpl_follow_link(struct dentry *dentry, struct nameidata *nd)
277{
81e97e21 278 cred_t *cr = CRED();
8b4f9a2d
BB
279 struct inode *ip = dentry->d_inode;
280 struct iovec iov;
281 uio_t uio;
282 char *link;
8b4f9a2d
BB
283 int error;
284
81e97e21 285 crhold(cr);
8b4f9a2d
BB
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
50950001 295 error = -zfs_readlink(ip, &uio, cr);
8b4f9a2d
BB
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
81e97e21 303 crfree(cr);
8b4f9a2d 304 return (NULL);
ee154f01
BB
305}
306
307static void
308zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
309{
0a6b03d3 310 const char *link = nd_get_link(nd);
ee154f01 311
ee154f01 312 if (!IS_ERR(link))
8b4f9a2d 313 kmem_free(link, MAXPATHLEN);
ee154f01
BB
314}
315
316static int
317zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
318{
81e97e21 319 cred_t *cr = CRED();
ee154f01 320 struct inode *ip = old_dentry->d_inode;
ee154f01
BB
321 int error;
322
323 if (ip->i_nlink >= ZFS_LINK_MAX)
324 return -EMLINK;
325
81e97e21 326 crhold(cr);
ee154f01
BB
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);
337out:
81e97e21 338 crfree(cr);
ee154f01
BB
339 ASSERT3S(error, <=, 0);
340
341 return (error);
342}
343
ea1fdf46 344#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57
ED
345static void
346zpl_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}
ea1fdf46 371#endif /* HAVE_INODE_TRUNCATE_RANGE */
5cb63a57 372
cb2d1901
ED
373#ifdef HAVE_INODE_FALLOCATE
374static long
375zpl_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
ee154f01 382const struct inode_operations zpl_inode_operations = {
ee154f01
BB
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,
ea1fdf46 397#ifdef HAVE_INODE_TRUNCATE_RANGE
5cb63a57 398 .truncate_range = zpl_truncate_range,
ea1fdf46 399#endif /* HAVE_INODE_TRUNCATE_RANGE */
cb2d1901
ED
400#ifdef HAVE_INODE_FALLOCATE
401 .fallocate = zpl_fallocate,
402#endif /* HAVE_INODE_FALLOCATE */
ee154f01
BB
403};
404
405const struct inode_operations zpl_dir_inode_operations = {
ee154f01
BB
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,
a6695d83
BB
416 .getattr = zpl_getattr,
417 .setxattr = generic_setxattr,
418 .getxattr = generic_getxattr,
419 .removexattr = generic_removexattr,
420 .listxattr = zpl_xattr_list,
ee154f01
BB
421};
422
423const struct inode_operations zpl_symlink_inode_operations = {
ee154f01
BB
424 .readlink = generic_readlink,
425 .follow_link = zpl_follow_link,
426 .put_link = zpl_put_link,
6f2255ba
BB
427 .setattr = zpl_setattr,
428 .getattr = zpl_getattr,
f31b3ebe
BB
429 .setxattr = generic_setxattr,
430 .getxattr = generic_getxattr,
431 .removexattr = generic_removexattr,
432 .listxattr = zpl_xattr_list,
ee154f01
BB
433};
434
435const struct inode_operations zpl_special_inode_operations = {
a6695d83
BB
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,
ee154f01 442};