]>
Commit | Line | Data |
---|---|---|
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 | ||
33 | static struct dentry * | |
8f195a90 | 34 | #ifdef HAVE_LOOKUP_NAMEIDATA |
ee154f01 | 35 | zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
8f195a90 YS |
36 | #else |
37 | zpl_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 | 48 | |
7b3e34ba BB |
49 | spin_lock(&dentry->d_lock); |
50 | dentry->d_time = jiffies; | |
51 | spin_unlock(&dentry->d_lock); | |
52 | ||
ee154f01 BB |
53 | if (error) { |
54 | if (error == -ENOENT) | |
55 | return d_splice_alias(NULL, dentry); | |
56 | else | |
57 | return ERR_PTR(error); | |
58 | } | |
59 | ||
60 | return d_splice_alias(ip, dentry); | |
61 | } | |
62 | ||
ebe7e575 | 63 | void |
7b3e34ba | 64 | zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr) |
9fd91dae BB |
65 | { |
66 | vap->va_mask = ATTR_MODE; | |
67 | vap->va_mode = mode; | |
9fd91dae BB |
68 | vap->va_uid = crgetfsuid(cr); |
69 | ||
70 | if (dir && dir->i_mode & S_ISGID) { | |
71 | vap->va_gid = dir->i_gid; | |
72 | if (S_ISDIR(mode)) | |
73 | vap->va_mode |= S_ISGID; | |
74 | } else { | |
75 | vap->va_gid = crgetfsgid(cr); | |
76 | } | |
77 | } | |
78 | ||
ee154f01 | 79 | static int |
558ef6d0 | 80 | #ifdef HAVE_CREATE_NAMEIDATA |
b39d3b9f | 81 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, |
ee154f01 | 82 | struct nameidata *nd) |
558ef6d0 YS |
83 | #else |
84 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, | |
85 | bool flag) | |
86 | #endif | |
ee154f01 | 87 | { |
81e97e21 | 88 | cred_t *cr = CRED(); |
ee154f01 BB |
89 | struct inode *ip; |
90 | vattr_t *vap; | |
91 | int error; | |
92 | ||
81e97e21 | 93 | crhold(cr); |
ee154f01 | 94 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
7b3e34ba | 95 | zpl_vap_init(vap, dir, mode, cr); |
ee154f01 | 96 | |
e89260a1 BB |
97 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); |
98 | if (error == 0) { | |
99 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
100 | VERIFY3S(error, ==, 0); | |
7b3e34ba BB |
101 | d_instantiate(dentry, ip); |
102 | d_set_d_op(dentry, &zpl_dentry_operations); | |
e89260a1 BB |
103 | } |
104 | ||
ee154f01 | 105 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 106 | crfree(cr); |
ee154f01 BB |
107 | ASSERT3S(error, <=, 0); |
108 | ||
109 | return (error); | |
110 | } | |
111 | ||
112 | static int | |
b39d3b9f BB |
113 | zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, |
114 | dev_t rdev) | |
ee154f01 | 115 | { |
81e97e21 | 116 | cred_t *cr = CRED(); |
ee154f01 BB |
117 | struct inode *ip; |
118 | vattr_t *vap; | |
119 | int error; | |
120 | ||
aa6d8c10 NB |
121 | /* |
122 | * We currently expect Linux to supply rdev=0 for all sockets | |
123 | * and fifos, but we want to know if this behavior ever changes. | |
124 | */ | |
125 | if (S_ISSOCK(mode) || S_ISFIFO(mode)) | |
126 | ASSERT(rdev == 0); | |
127 | ||
81e97e21 | 128 | crhold(cr); |
ee154f01 | 129 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
7b3e34ba | 130 | zpl_vap_init(vap, dir, mode, cr); |
ee154f01 | 131 | vap->va_rdev = rdev; |
ee154f01 | 132 | |
7b3e34ba BB |
133 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); |
134 | if (error == 0) { | |
135 | d_instantiate(dentry, ip); | |
136 | d_set_d_op(dentry, &zpl_dentry_operations); | |
137 | } | |
138 | ||
ee154f01 | 139 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 140 | crfree(cr); |
ee154f01 BB |
141 | ASSERT3S(error, <=, 0); |
142 | ||
143 | return (-error); | |
144 | } | |
145 | ||
146 | static int | |
147 | zpl_unlink(struct inode *dir, struct dentry *dentry) | |
148 | { | |
81e97e21 | 149 | cred_t *cr = CRED(); |
ee154f01 BB |
150 | int error; |
151 | ||
81e97e21 | 152 | crhold(cr); |
ee154f01 | 153 | error = -zfs_remove(dir, dname(dentry), cr); |
81e97e21 | 154 | crfree(cr); |
ee154f01 BB |
155 | ASSERT3S(error, <=, 0); |
156 | ||
157 | return (error); | |
158 | } | |
159 | ||
160 | static int | |
b39d3b9f | 161 | zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode) |
ee154f01 | 162 | { |
81e97e21 | 163 | cred_t *cr = CRED(); |
ee154f01 BB |
164 | vattr_t *vap; |
165 | struct inode *ip; | |
166 | int error; | |
167 | ||
81e97e21 | 168 | crhold(cr); |
ee154f01 | 169 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
7b3e34ba | 170 | zpl_vap_init(vap, dir, mode | S_IFDIR, cr); |
ee154f01 BB |
171 | |
172 | error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL); | |
7b3e34ba BB |
173 | if (error == 0) { |
174 | d_instantiate(dentry, ip); | |
175 | d_set_d_op(dentry, &zpl_dentry_operations); | |
176 | } | |
177 | ||
ee154f01 | 178 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 179 | crfree(cr); |
ee154f01 BB |
180 | ASSERT3S(error, <=, 0); |
181 | ||
182 | return (error); | |
183 | } | |
184 | ||
185 | static int | |
186 | zpl_rmdir(struct inode * dir, struct dentry *dentry) | |
187 | { | |
81e97e21 | 188 | cred_t *cr = CRED(); |
ee154f01 BB |
189 | int error; |
190 | ||
81e97e21 | 191 | crhold(cr); |
ee154f01 | 192 | error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0); |
81e97e21 | 193 | crfree(cr); |
ee154f01 BB |
194 | ASSERT3S(error, <=, 0); |
195 | ||
196 | return (error); | |
197 | } | |
198 | ||
199 | static int | |
200 | zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) | |
201 | { | |
ebe7e575 | 202 | boolean_t issnap = ITOZSB(dentry->d_inode)->z_issnap; |
ee154f01 BB |
203 | int error; |
204 | ||
ebe7e575 BB |
205 | /* |
206 | * Ensure MNT_SHRINKABLE is set on snapshots to ensure they are | |
207 | * unmounted automatically with the parent file system. This | |
208 | * is done on the first getattr because it's not easy to get the | |
209 | * vfsmount structure at mount time. This call path is explicitly | |
210 | * marked unlikely to avoid any performance impact. FWIW, ext4 | |
211 | * resorts to a similar trick for sysadmin convenience. | |
212 | */ | |
213 | if (unlikely(issnap && !(mnt->mnt_flags & MNT_SHRINKABLE))) | |
214 | mnt->mnt_flags |= MNT_SHRINKABLE; | |
215 | ||
057e8eee | 216 | error = -zfs_getattr_fast(dentry->d_inode, stat); |
ee154f01 BB |
217 | ASSERT3S(error, <=, 0); |
218 | ||
219 | return (error); | |
220 | } | |
221 | ||
222 | static int | |
5484965a | 223 | zpl_setattr(struct dentry *dentry, struct iattr *ia) |
ee154f01 | 224 | { |
81e97e21 | 225 | cred_t *cr = CRED(); |
5484965a | 226 | vattr_t *vap; |
ee154f01 BB |
227 | int error; |
228 | ||
5484965a | 229 | error = inode_change_ok(dentry->d_inode, ia); |
ee154f01 BB |
230 | if (error) |
231 | return (error); | |
232 | ||
81e97e21 | 233 | crhold(cr); |
5484965a BB |
234 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
235 | vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK; | |
236 | vap->va_mode = ia->ia_mode; | |
237 | vap->va_uid = ia->ia_uid; | |
238 | vap->va_gid = ia->ia_gid; | |
239 | vap->va_size = ia->ia_size; | |
240 | vap->va_atime = ia->ia_atime; | |
241 | vap->va_mtime = ia->ia_mtime; | |
242 | vap->va_ctime = ia->ia_ctime; | |
243 | ||
244 | error = -zfs_setattr(dentry->d_inode, vap, 0, cr); | |
245 | ||
246 | kmem_free(vap, sizeof(vattr_t)); | |
81e97e21 | 247 | crfree(cr); |
ee154f01 BB |
248 | ASSERT3S(error, <=, 0); |
249 | ||
5484965a | 250 | return (error); |
ee154f01 BB |
251 | } |
252 | ||
253 | static int | |
254 | zpl_rename(struct inode *sdip, struct dentry *sdentry, | |
255 | struct inode *tdip, struct dentry *tdentry) | |
256 | { | |
81e97e21 | 257 | cred_t *cr = CRED(); |
ee154f01 BB |
258 | int error; |
259 | ||
81e97e21 | 260 | crhold(cr); |
ee154f01 | 261 | error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0); |
81e97e21 | 262 | crfree(cr); |
ee154f01 BB |
263 | ASSERT3S(error, <=, 0); |
264 | ||
265 | return (error); | |
266 | } | |
267 | ||
268 | static int | |
269 | zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name) | |
270 | { | |
81e97e21 | 271 | cred_t *cr = CRED(); |
ee154f01 BB |
272 | vattr_t *vap; |
273 | struct inode *ip; | |
274 | int error; | |
275 | ||
81e97e21 | 276 | crhold(cr); |
ee154f01 | 277 | vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP); |
7b3e34ba | 278 | zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr); |
ee154f01 BB |
279 | |
280 | error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0); | |
7b3e34ba BB |
281 | if (error == 0) { |
282 | d_instantiate(dentry, ip); | |
283 | d_set_d_op(dentry, &zpl_dentry_operations); | |
284 | } | |
285 | ||
ee154f01 | 286 | kmem_free(vap, sizeof(vattr_t)); |
81e97e21 | 287 | crfree(cr); |
ee154f01 BB |
288 | ASSERT3S(error, <=, 0); |
289 | ||
290 | return (error); | |
291 | } | |
292 | ||
293 | static void * | |
294 | zpl_follow_link(struct dentry *dentry, struct nameidata *nd) | |
295 | { | |
81e97e21 | 296 | cred_t *cr = CRED(); |
8b4f9a2d BB |
297 | struct inode *ip = dentry->d_inode; |
298 | struct iovec iov; | |
299 | uio_t uio; | |
300 | char *link; | |
8b4f9a2d BB |
301 | int error; |
302 | ||
81e97e21 | 303 | crhold(cr); |
8b4f9a2d BB |
304 | |
305 | iov.iov_len = MAXPATHLEN; | |
306 | iov.iov_base = link = kmem_zalloc(MAXPATHLEN, KM_SLEEP); | |
307 | ||
308 | uio.uio_iov = &iov; | |
309 | uio.uio_iovcnt = 1; | |
310 | uio.uio_resid = (MAXPATHLEN - 1); | |
311 | uio.uio_segflg = UIO_SYSSPACE; | |
312 | ||
50950001 | 313 | error = -zfs_readlink(ip, &uio, cr); |
8b4f9a2d BB |
314 | if (error) { |
315 | kmem_free(link, MAXPATHLEN); | |
316 | nd_set_link(nd, ERR_PTR(error)); | |
317 | } else { | |
318 | nd_set_link(nd, link); | |
319 | } | |
320 | ||
81e97e21 | 321 | crfree(cr); |
8b4f9a2d | 322 | return (NULL); |
ee154f01 BB |
323 | } |
324 | ||
325 | static void | |
326 | zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) | |
327 | { | |
0a6b03d3 | 328 | const char *link = nd_get_link(nd); |
ee154f01 | 329 | |
ee154f01 | 330 | if (!IS_ERR(link)) |
8b4f9a2d | 331 | kmem_free(link, MAXPATHLEN); |
ee154f01 BB |
332 | } |
333 | ||
334 | static int | |
335 | zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
336 | { | |
81e97e21 | 337 | cred_t *cr = CRED(); |
ee154f01 | 338 | struct inode *ip = old_dentry->d_inode; |
ee154f01 BB |
339 | int error; |
340 | ||
341 | if (ip->i_nlink >= ZFS_LINK_MAX) | |
342 | return -EMLINK; | |
343 | ||
81e97e21 | 344 | crhold(cr); |
ee154f01 BB |
345 | ip->i_ctime = CURRENT_TIME_SEC; |
346 | igrab(ip); /* Use ihold() if available */ | |
347 | ||
348 | error = -zfs_link(dir, ip, dname(dentry), cr); | |
349 | if (error) { | |
350 | iput(ip); | |
351 | goto out; | |
352 | } | |
353 | ||
354 | d_instantiate(dentry, ip); | |
7b3e34ba | 355 | d_set_d_op(dentry, &zpl_dentry_operations); |
ee154f01 | 356 | out: |
81e97e21 | 357 | crfree(cr); |
ee154f01 BB |
358 | ASSERT3S(error, <=, 0); |
359 | ||
360 | return (error); | |
361 | } | |
362 | ||
ea1fdf46 | 363 | #ifdef HAVE_INODE_TRUNCATE_RANGE |
5cb63a57 ED |
364 | static void |
365 | zpl_truncate_range(struct inode* ip, loff_t start, loff_t end) | |
366 | { | |
367 | cred_t *cr = CRED(); | |
368 | flock64_t bf; | |
369 | ||
370 | ASSERT3S(start, <=, end); | |
371 | ||
372 | /* | |
373 | * zfs_freesp() will interpret (len == 0) as meaning "truncate until | |
374 | * the end of the file". We don't want that. | |
375 | */ | |
376 | if (start == end) | |
377 | return; | |
378 | ||
379 | crhold(cr); | |
380 | ||
381 | bf.l_type = F_WRLCK; | |
382 | bf.l_whence = 0; | |
383 | bf.l_start = start; | |
384 | bf.l_len = end - start; | |
385 | bf.l_pid = 0; | |
386 | zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr); | |
387 | ||
388 | crfree(cr); | |
389 | } | |
ea1fdf46 | 390 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ |
5cb63a57 | 391 | |
cb2d1901 ED |
392 | #ifdef HAVE_INODE_FALLOCATE |
393 | static long | |
394 | zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len) | |
395 | { | |
396 | return zpl_fallocate_common(ip, mode, offset, len); | |
397 | } | |
398 | #endif /* HAVE_INODE_FALLOCATE */ | |
399 | ||
7b3e34ba BB |
400 | static int |
401 | #ifdef HAVE_D_REVALIDATE_NAMEIDATA | |
402 | zpl_revalidate(struct dentry *dentry, struct nameidata *nd) | |
403 | { | |
404 | unsigned int flags = nd->flags; | |
405 | #else | |
406 | zpl_revalidate(struct dentry *dentry, unsigned int flags) | |
407 | { | |
408 | #endif /* HAVE_D_REVALIDATE_NAMEIDATA */ | |
409 | zfs_sb_t *zsb = dentry->d_sb->s_fs_info; | |
410 | int error; | |
411 | ||
412 | if (flags & LOOKUP_RCU) | |
413 | return (-ECHILD); | |
414 | ||
415 | /* | |
416 | * After a rollback negative dentries created before the rollback | |
417 | * time must be invalidated. Otherwise they can obscure files which | |
418 | * are only present in the rolled back dataset. | |
419 | */ | |
420 | if (dentry->d_inode == NULL) { | |
421 | spin_lock(&dentry->d_lock); | |
422 | error = time_before(dentry->d_time, zsb->z_rollback_time); | |
423 | spin_unlock(&dentry->d_lock); | |
424 | ||
425 | if (error) | |
426 | return (0); | |
427 | } | |
428 | ||
429 | /* | |
430 | * The dentry may reference a stale inode if a mounted file system | |
431 | * was rolled back to a point in time where the object didn't exist. | |
432 | */ | |
433 | if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale) | |
434 | return (0); | |
435 | ||
436 | return (1); | |
437 | } | |
cb2d1901 | 438 | |
ee154f01 | 439 | const struct inode_operations zpl_inode_operations = { |
ee154f01 BB |
440 | .create = zpl_create, |
441 | .link = zpl_link, | |
442 | .unlink = zpl_unlink, | |
443 | .symlink = zpl_symlink, | |
444 | .mkdir = zpl_mkdir, | |
445 | .rmdir = zpl_rmdir, | |
446 | .mknod = zpl_mknod, | |
447 | .rename = zpl_rename, | |
448 | .setattr = zpl_setattr, | |
449 | .getattr = zpl_getattr, | |
450 | .setxattr = generic_setxattr, | |
451 | .getxattr = generic_getxattr, | |
452 | .removexattr = generic_removexattr, | |
453 | .listxattr = zpl_xattr_list, | |
ea1fdf46 | 454 | #ifdef HAVE_INODE_TRUNCATE_RANGE |
5cb63a57 | 455 | .truncate_range = zpl_truncate_range, |
ea1fdf46 | 456 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ |
cb2d1901 ED |
457 | #ifdef HAVE_INODE_FALLOCATE |
458 | .fallocate = zpl_fallocate, | |
459 | #endif /* HAVE_INODE_FALLOCATE */ | |
ee154f01 BB |
460 | }; |
461 | ||
462 | const struct inode_operations zpl_dir_inode_operations = { | |
ee154f01 BB |
463 | .create = zpl_create, |
464 | .lookup = zpl_lookup, | |
465 | .link = zpl_link, | |
466 | .unlink = zpl_unlink, | |
467 | .symlink = zpl_symlink, | |
468 | .mkdir = zpl_mkdir, | |
469 | .rmdir = zpl_rmdir, | |
470 | .mknod = zpl_mknod, | |
471 | .rename = zpl_rename, | |
472 | .setattr = zpl_setattr, | |
a6695d83 BB |
473 | .getattr = zpl_getattr, |
474 | .setxattr = generic_setxattr, | |
475 | .getxattr = generic_getxattr, | |
476 | .removexattr = generic_removexattr, | |
477 | .listxattr = zpl_xattr_list, | |
ee154f01 BB |
478 | }; |
479 | ||
480 | const struct inode_operations zpl_symlink_inode_operations = { | |
ee154f01 BB |
481 | .readlink = generic_readlink, |
482 | .follow_link = zpl_follow_link, | |
483 | .put_link = zpl_put_link, | |
6f2255ba BB |
484 | .setattr = zpl_setattr, |
485 | .getattr = zpl_getattr, | |
f31b3ebe BB |
486 | .setxattr = generic_setxattr, |
487 | .getxattr = generic_getxattr, | |
488 | .removexattr = generic_removexattr, | |
489 | .listxattr = zpl_xattr_list, | |
ee154f01 BB |
490 | }; |
491 | ||
492 | const struct inode_operations zpl_special_inode_operations = { | |
a6695d83 BB |
493 | .setattr = zpl_setattr, |
494 | .getattr = zpl_getattr, | |
495 | .setxattr = generic_setxattr, | |
496 | .getxattr = generic_getxattr, | |
497 | .removexattr = generic_removexattr, | |
498 | .listxattr = zpl_xattr_list, | |
ee154f01 | 499 | }; |
7b3e34ba BB |
500 | |
501 | dentry_operations_t zpl_dentry_operations = { | |
502 | .d_revalidate = zpl_revalidate, | |
503 | }; |