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