]>
Commit | Line | Data |
---|---|---|
70e083d2 TG |
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 | * Copyright (c) 2015 by Chunwei Chen. All rights reserved. | |
24 | */ | |
25 | ||
26 | ||
27 | #include <sys/zfs_ctldir.h> | |
28 | #include <sys/zfs_vfsops.h> | |
29 | #include <sys/zfs_vnops.h> | |
30 | #include <sys/zfs_znode.h> | |
31 | #include <sys/dmu_objset.h> | |
32 | #include <sys/vfs.h> | |
33 | #include <sys/zpl.h> | |
34 | #include <sys/file.h> | |
35 | ||
36 | ||
37 | static struct dentry * | |
38 | #ifdef HAVE_LOOKUP_NAMEIDATA | |
39 | zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | |
40 | #else | |
41 | zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) | |
42 | #endif | |
43 | { | |
44 | cred_t *cr = CRED(); | |
45 | struct inode *ip; | |
46 | int error; | |
47 | fstrans_cookie_t cookie; | |
48 | pathname_t *ppn = NULL; | |
49 | pathname_t pn; | |
50 | int zfs_flags = 0; | |
86e3c28a | 51 | zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; |
70e083d2 | 52 | |
9784fa9e | 53 | if (dlen(dentry) >= ZAP_MAXNAMELEN) |
70e083d2 TG |
54 | return (ERR_PTR(-ENAMETOOLONG)); |
55 | ||
56 | crhold(cr); | |
57 | cookie = spl_fstrans_mark(); | |
58 | ||
59 | /* If we are a case insensitive fs, we need the real name */ | |
86e3c28a | 60 | if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { |
70e083d2 | 61 | zfs_flags = FIGNORECASE; |
86e3c28a | 62 | pn_alloc(&pn); |
70e083d2 TG |
63 | ppn = &pn; |
64 | } | |
65 | ||
66 | error = -zfs_lookup(dir, dname(dentry), &ip, zfs_flags, cr, NULL, ppn); | |
67 | spl_fstrans_unmark(cookie); | |
68 | ASSERT3S(error, <=, 0); | |
69 | crfree(cr); | |
70 | ||
71 | spin_lock(&dentry->d_lock); | |
72 | dentry->d_time = jiffies; | |
73 | #ifndef HAVE_S_D_OP | |
74 | d_set_d_op(dentry, &zpl_dentry_operations); | |
75 | #endif /* HAVE_S_D_OP */ | |
76 | spin_unlock(&dentry->d_lock); | |
77 | ||
78 | if (error) { | |
79 | /* | |
80 | * If we have a case sensitive fs, we do not want to | |
81 | * insert negative entries, so return NULL for ENOENT. | |
82 | * Fall through if the error is not ENOENT. Also free memory. | |
83 | */ | |
84 | if (ppn) { | |
86e3c28a | 85 | pn_free(ppn); |
70e083d2 TG |
86 | if (error == -ENOENT) |
87 | return (NULL); | |
88 | } | |
89 | ||
90 | if (error == -ENOENT) | |
91 | return (d_splice_alias(NULL, dentry)); | |
92 | else | |
93 | return (ERR_PTR(error)); | |
94 | } | |
95 | ||
96 | /* | |
97 | * If we are case insensitive, call the correct function | |
98 | * to install the name. | |
99 | */ | |
100 | if (ppn) { | |
101 | struct dentry *new_dentry; | |
102 | struct qstr ci_name; | |
103 | ||
104 | if (strcmp(dname(dentry), pn.pn_buf) == 0) { | |
105 | new_dentry = d_splice_alias(ip, dentry); | |
106 | } else { | |
107 | ci_name.name = pn.pn_buf; | |
108 | ci_name.len = strlen(pn.pn_buf); | |
109 | new_dentry = d_add_ci(dentry, ip, &ci_name); | |
110 | } | |
86e3c28a | 111 | pn_free(ppn); |
70e083d2 TG |
112 | return (new_dentry); |
113 | } else { | |
114 | return (d_splice_alias(ip, dentry)); | |
115 | } | |
116 | } | |
117 | ||
118 | void | |
119 | zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr) | |
120 | { | |
121 | vap->va_mask = ATTR_MODE; | |
122 | vap->va_mode = mode; | |
123 | vap->va_uid = crgetfsuid(cr); | |
124 | ||
125 | if (dir && dir->i_mode & S_ISGID) { | |
126 | vap->va_gid = KGID_TO_SGID(dir->i_gid); | |
127 | if (S_ISDIR(mode)) | |
128 | vap->va_mode |= S_ISGID; | |
129 | } else { | |
130 | vap->va_gid = crgetfsgid(cr); | |
131 | } | |
132 | } | |
133 | ||
134 | static int | |
135 | #ifdef HAVE_CREATE_NAMEIDATA | |
136 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, | |
137 | struct nameidata *nd) | |
138 | #else | |
139 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, | |
140 | bool flag) | |
141 | #endif | |
142 | { | |
143 | cred_t *cr = CRED(); | |
144 | struct inode *ip; | |
145 | vattr_t *vap; | |
146 | int error; | |
147 | fstrans_cookie_t cookie; | |
148 | ||
149 | crhold(cr); | |
150 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
151 | zpl_vap_init(vap, dir, mode, cr); | |
152 | ||
153 | cookie = spl_fstrans_mark(); | |
154 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); | |
155 | if (error == 0) { | |
156 | d_instantiate(dentry, ip); | |
157 | ||
158 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
159 | if (error == 0) | |
160 | error = zpl_init_acl(ip, dir); | |
161 | ||
162 | if (error) | |
86e3c28a | 163 | (void) zfs_remove(dir, dname(dentry), cr, 0); |
70e083d2 TG |
164 | } |
165 | ||
166 | spl_fstrans_unmark(cookie); | |
167 | kmem_free(vap, sizeof (vattr_t)); | |
168 | crfree(cr); | |
169 | ASSERT3S(error, <=, 0); | |
170 | ||
171 | return (error); | |
172 | } | |
173 | ||
174 | static int | |
175 | zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, | |
176 | dev_t rdev) | |
177 | { | |
178 | cred_t *cr = CRED(); | |
179 | struct inode *ip; | |
180 | vattr_t *vap; | |
181 | int error; | |
182 | fstrans_cookie_t cookie; | |
183 | ||
184 | /* | |
185 | * We currently expect Linux to supply rdev=0 for all sockets | |
186 | * and fifos, but we want to know if this behavior ever changes. | |
187 | */ | |
188 | if (S_ISSOCK(mode) || S_ISFIFO(mode)) | |
189 | ASSERT(rdev == 0); | |
190 | ||
191 | crhold(cr); | |
192 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
193 | zpl_vap_init(vap, dir, mode, cr); | |
194 | vap->va_rdev = rdev; | |
195 | ||
196 | cookie = spl_fstrans_mark(); | |
197 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); | |
198 | if (error == 0) { | |
199 | d_instantiate(dentry, ip); | |
200 | ||
201 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
202 | if (error == 0) | |
203 | error = zpl_init_acl(ip, dir); | |
204 | ||
205 | if (error) | |
86e3c28a | 206 | (void) zfs_remove(dir, dname(dentry), cr, 0); |
70e083d2 TG |
207 | } |
208 | ||
209 | spl_fstrans_unmark(cookie); | |
210 | kmem_free(vap, sizeof (vattr_t)); | |
211 | crfree(cr); | |
212 | ASSERT3S(error, <=, 0); | |
213 | ||
214 | return (error); | |
215 | } | |
216 | ||
86e3c28a CIK |
217 | #ifdef HAVE_TMPFILE |
218 | static int | |
219 | zpl_tmpfile(struct inode *dir, struct dentry *dentry, zpl_umode_t mode) | |
220 | { | |
221 | cred_t *cr = CRED(); | |
222 | struct inode *ip; | |
223 | vattr_t *vap; | |
224 | int error; | |
225 | fstrans_cookie_t cookie; | |
226 | ||
227 | crhold(cr); | |
228 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
229 | zpl_vap_init(vap, dir, mode, cr); | |
230 | ||
231 | cookie = spl_fstrans_mark(); | |
232 | error = -zfs_tmpfile(dir, vap, 0, mode, &ip, cr, 0, NULL); | |
233 | if (error == 0) { | |
234 | /* d_tmpfile will do drop_nlink, so we should set it first */ | |
235 | set_nlink(ip, 1); | |
236 | d_tmpfile(dentry, ip); | |
237 | ||
238 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
239 | if (error == 0) | |
240 | error = zpl_init_acl(ip, dir); | |
241 | /* | |
242 | * don't need to handle error here, file is already in | |
243 | * unlinked set. | |
244 | */ | |
245 | } | |
246 | ||
247 | spl_fstrans_unmark(cookie); | |
248 | kmem_free(vap, sizeof (vattr_t)); | |
249 | crfree(cr); | |
250 | ASSERT3S(error, <=, 0); | |
251 | ||
252 | return (error); | |
253 | } | |
254 | #endif | |
255 | ||
70e083d2 TG |
256 | static int |
257 | zpl_unlink(struct inode *dir, struct dentry *dentry) | |
258 | { | |
259 | cred_t *cr = CRED(); | |
260 | int error; | |
261 | fstrans_cookie_t cookie; | |
86e3c28a | 262 | zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; |
70e083d2 TG |
263 | |
264 | crhold(cr); | |
265 | cookie = spl_fstrans_mark(); | |
86e3c28a | 266 | error = -zfs_remove(dir, dname(dentry), cr, 0); |
70e083d2 TG |
267 | |
268 | /* | |
269 | * For a CI FS we must invalidate the dentry to prevent the | |
270 | * creation of negative entries. | |
271 | */ | |
86e3c28a | 272 | if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE) |
70e083d2 TG |
273 | d_invalidate(dentry); |
274 | ||
275 | spl_fstrans_unmark(cookie); | |
276 | crfree(cr); | |
277 | ASSERT3S(error, <=, 0); | |
278 | ||
279 | return (error); | |
280 | } | |
281 | ||
282 | static int | |
283 | zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode) | |
284 | { | |
285 | cred_t *cr = CRED(); | |
286 | vattr_t *vap; | |
287 | struct inode *ip; | |
288 | int error; | |
289 | fstrans_cookie_t cookie; | |
290 | ||
291 | crhold(cr); | |
292 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
293 | zpl_vap_init(vap, dir, mode | S_IFDIR, cr); | |
294 | ||
295 | cookie = spl_fstrans_mark(); | |
296 | error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL); | |
297 | if (error == 0) { | |
298 | d_instantiate(dentry, ip); | |
299 | ||
300 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
301 | if (error == 0) | |
302 | error = zpl_init_acl(ip, dir); | |
303 | ||
304 | if (error) | |
305 | (void) zfs_rmdir(dir, dname(dentry), NULL, cr, 0); | |
306 | } | |
307 | ||
308 | spl_fstrans_unmark(cookie); | |
309 | kmem_free(vap, sizeof (vattr_t)); | |
310 | crfree(cr); | |
311 | ASSERT3S(error, <=, 0); | |
312 | ||
313 | return (error); | |
314 | } | |
315 | ||
316 | static int | |
86e3c28a | 317 | zpl_rmdir(struct inode *dir, struct dentry *dentry) |
70e083d2 TG |
318 | { |
319 | cred_t *cr = CRED(); | |
320 | int error; | |
321 | fstrans_cookie_t cookie; | |
86e3c28a | 322 | zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; |
70e083d2 TG |
323 | |
324 | crhold(cr); | |
325 | cookie = spl_fstrans_mark(); | |
326 | error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0); | |
327 | ||
328 | /* | |
329 | * For a CI FS we must invalidate the dentry to prevent the | |
330 | * creation of negative entries. | |
331 | */ | |
86e3c28a | 332 | if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE) |
70e083d2 TG |
333 | d_invalidate(dentry); |
334 | ||
335 | spl_fstrans_unmark(cookie); | |
336 | crfree(cr); | |
337 | ASSERT3S(error, <=, 0); | |
338 | ||
339 | return (error); | |
340 | } | |
341 | ||
342 | static int | |
0d6103dd CIK |
343 | zpl_getattr_impl(const struct path *path, struct kstat *stat, u32 request_mask, |
344 | unsigned int query_flags) | |
70e083d2 TG |
345 | { |
346 | int error; | |
347 | fstrans_cookie_t cookie; | |
348 | ||
349 | cookie = spl_fstrans_mark(); | |
0d6103dd CIK |
350 | |
351 | /* | |
352 | * XXX request_mask and query_flags currently ignored. | |
353 | */ | |
354 | ||
355 | error = -zfs_getattr_fast(path->dentry->d_inode, stat); | |
70e083d2 TG |
356 | spl_fstrans_unmark(cookie); |
357 | ASSERT3S(error, <=, 0); | |
358 | ||
359 | return (error); | |
360 | } | |
0d6103dd | 361 | ZPL_GETATTR_WRAPPER(zpl_getattr); |
70e083d2 TG |
362 | |
363 | static int | |
364 | zpl_setattr(struct dentry *dentry, struct iattr *ia) | |
365 | { | |
366 | struct inode *ip = dentry->d_inode; | |
367 | cred_t *cr = CRED(); | |
368 | vattr_t *vap; | |
369 | int error; | |
370 | fstrans_cookie_t cookie; | |
371 | ||
372 | error = setattr_prepare(dentry, ia); | |
373 | if (error) | |
374 | return (error); | |
375 | ||
376 | crhold(cr); | |
377 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
378 | vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK; | |
379 | vap->va_mode = ia->ia_mode; | |
380 | vap->va_uid = KUID_TO_SUID(ia->ia_uid); | |
381 | vap->va_gid = KGID_TO_SGID(ia->ia_gid); | |
382 | vap->va_size = ia->ia_size; | |
383 | vap->va_atime = ia->ia_atime; | |
384 | vap->va_mtime = ia->ia_mtime; | |
385 | vap->va_ctime = ia->ia_ctime; | |
386 | ||
387 | if (vap->va_mask & ATTR_ATIME) | |
86e3c28a CIK |
388 | ip->i_atime = timespec_trunc(ia->ia_atime, |
389 | ip->i_sb->s_time_gran); | |
70e083d2 TG |
390 | |
391 | cookie = spl_fstrans_mark(); | |
392 | error = -zfs_setattr(ip, vap, 0, cr); | |
393 | if (!error && (ia->ia_valid & ATTR_MODE)) | |
394 | error = zpl_chmod_acl(ip); | |
395 | ||
396 | spl_fstrans_unmark(cookie); | |
397 | kmem_free(vap, sizeof (vattr_t)); | |
398 | crfree(cr); | |
399 | ASSERT3S(error, <=, 0); | |
400 | ||
401 | return (error); | |
402 | } | |
403 | ||
404 | static int | |
405 | zpl_rename2(struct inode *sdip, struct dentry *sdentry, | |
406 | struct inode *tdip, struct dentry *tdentry, unsigned int flags) | |
407 | { | |
408 | cred_t *cr = CRED(); | |
409 | int error; | |
410 | fstrans_cookie_t cookie; | |
411 | ||
412 | /* We don't have renameat2(2) support */ | |
413 | if (flags) | |
414 | return (-EINVAL); | |
415 | ||
416 | crhold(cr); | |
417 | cookie = spl_fstrans_mark(); | |
418 | error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0); | |
419 | spl_fstrans_unmark(cookie); | |
420 | crfree(cr); | |
421 | ASSERT3S(error, <=, 0); | |
422 | ||
423 | return (error); | |
424 | } | |
425 | ||
426 | #ifndef HAVE_RENAME_WANTS_FLAGS | |
427 | static int | |
428 | zpl_rename(struct inode *sdip, struct dentry *sdentry, | |
429 | struct inode *tdip, struct dentry *tdentry) | |
430 | { | |
431 | return (zpl_rename2(sdip, sdentry, tdip, tdentry, 0)); | |
432 | } | |
433 | #endif | |
434 | ||
435 | static int | |
436 | zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name) | |
437 | { | |
438 | cred_t *cr = CRED(); | |
439 | vattr_t *vap; | |
440 | struct inode *ip; | |
441 | int error; | |
442 | fstrans_cookie_t cookie; | |
443 | ||
444 | crhold(cr); | |
445 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); | |
446 | zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr); | |
447 | ||
448 | cookie = spl_fstrans_mark(); | |
449 | error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0); | |
450 | if (error == 0) { | |
451 | d_instantiate(dentry, ip); | |
452 | ||
453 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
454 | if (error) | |
86e3c28a | 455 | (void) zfs_remove(dir, dname(dentry), cr, 0); |
70e083d2 TG |
456 | } |
457 | ||
458 | spl_fstrans_unmark(cookie); | |
459 | kmem_free(vap, sizeof (vattr_t)); | |
460 | crfree(cr); | |
461 | ASSERT3S(error, <=, 0); | |
462 | ||
463 | return (error); | |
464 | } | |
465 | ||
466 | #if defined(HAVE_PUT_LINK_COOKIE) | |
467 | static void | |
468 | zpl_put_link(struct inode *unused, void *cookie) | |
469 | { | |
470 | kmem_free(cookie, MAXPATHLEN); | |
471 | } | |
472 | #elif defined(HAVE_PUT_LINK_NAMEIDATA) | |
473 | static void | |
474 | zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) | |
475 | { | |
476 | const char *link = nd_get_link(nd); | |
477 | ||
478 | if (!IS_ERR(link)) | |
479 | kmem_free(link, MAXPATHLEN); | |
480 | } | |
481 | #elif defined(HAVE_PUT_LINK_DELAYED) | |
482 | static void | |
483 | zpl_put_link(void *ptr) | |
484 | { | |
485 | kmem_free(ptr, MAXPATHLEN); | |
486 | } | |
487 | #endif | |
488 | ||
489 | static int | |
490 | zpl_get_link_common(struct dentry *dentry, struct inode *ip, char **link) | |
491 | { | |
492 | fstrans_cookie_t cookie; | |
493 | cred_t *cr = CRED(); | |
494 | struct iovec iov; | |
495 | uio_t uio; | |
496 | int error; | |
497 | ||
498 | crhold(cr); | |
499 | *link = NULL; | |
500 | iov.iov_len = MAXPATHLEN; | |
501 | iov.iov_base = kmem_zalloc(MAXPATHLEN, KM_SLEEP); | |
502 | ||
503 | uio.uio_iov = &iov; | |
504 | uio.uio_iovcnt = 1; | |
505 | uio.uio_skip = 0; | |
506 | uio.uio_resid = (MAXPATHLEN - 1); | |
507 | uio.uio_segflg = UIO_SYSSPACE; | |
508 | ||
509 | cookie = spl_fstrans_mark(); | |
510 | error = -zfs_readlink(ip, &uio, cr); | |
511 | spl_fstrans_unmark(cookie); | |
512 | crfree(cr); | |
513 | ||
514 | if (error) | |
515 | kmem_free(iov.iov_base, MAXPATHLEN); | |
516 | else | |
517 | *link = iov.iov_base; | |
518 | ||
519 | return (error); | |
520 | } | |
521 | ||
522 | #if defined(HAVE_GET_LINK_DELAYED) | |
523 | const char * | |
524 | zpl_get_link(struct dentry *dentry, struct inode *inode, | |
525 | struct delayed_call *done) | |
526 | { | |
527 | char *link = NULL; | |
528 | int error; | |
529 | ||
530 | if (!dentry) | |
531 | return (ERR_PTR(-ECHILD)); | |
532 | ||
533 | error = zpl_get_link_common(dentry, inode, &link); | |
534 | if (error) | |
535 | return (ERR_PTR(error)); | |
536 | ||
537 | set_delayed_call(done, zpl_put_link, link); | |
538 | ||
539 | return (link); | |
540 | } | |
541 | #elif defined(HAVE_GET_LINK_COOKIE) | |
542 | const char * | |
543 | zpl_get_link(struct dentry *dentry, struct inode *inode, void **cookie) | |
544 | { | |
545 | char *link = NULL; | |
546 | int error; | |
547 | ||
548 | if (!dentry) | |
549 | return (ERR_PTR(-ECHILD)); | |
550 | ||
551 | error = zpl_get_link_common(dentry, inode, &link); | |
552 | if (error) | |
553 | return (ERR_PTR(error)); | |
554 | ||
555 | return (*cookie = link); | |
556 | } | |
557 | #elif defined(HAVE_FOLLOW_LINK_COOKIE) | |
558 | const char * | |
559 | zpl_follow_link(struct dentry *dentry, void **cookie) | |
560 | { | |
561 | char *link = NULL; | |
562 | int error; | |
563 | ||
564 | error = zpl_get_link_common(dentry, dentry->d_inode, &link); | |
565 | if (error) | |
566 | return (ERR_PTR(error)); | |
567 | ||
568 | return (*cookie = link); | |
569 | } | |
570 | #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA) | |
571 | static void * | |
572 | zpl_follow_link(struct dentry *dentry, struct nameidata *nd) | |
573 | { | |
574 | char *link = NULL; | |
575 | int error; | |
576 | ||
577 | error = zpl_get_link_common(dentry, dentry->d_inode, &link); | |
578 | if (error) | |
579 | nd_set_link(nd, ERR_PTR(error)); | |
580 | else | |
581 | nd_set_link(nd, link); | |
582 | ||
583 | return (NULL); | |
584 | } | |
585 | #endif | |
586 | ||
587 | static int | |
588 | zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
589 | { | |
590 | cred_t *cr = CRED(); | |
591 | struct inode *ip = old_dentry->d_inode; | |
592 | int error; | |
593 | fstrans_cookie_t cookie; | |
594 | ||
595 | if (ip->i_nlink >= ZFS_LINK_MAX) | |
596 | return (-EMLINK); | |
597 | ||
598 | crhold(cr); | |
837ca015 | 599 | ip->i_ctime = current_time(ip); |
70e083d2 TG |
600 | igrab(ip); /* Use ihold() if available */ |
601 | ||
602 | cookie = spl_fstrans_mark(); | |
86e3c28a | 603 | error = -zfs_link(dir, ip, dname(dentry), cr, 0); |
70e083d2 TG |
604 | if (error) { |
605 | iput(ip); | |
606 | goto out; | |
607 | } | |
608 | ||
609 | d_instantiate(dentry, ip); | |
610 | out: | |
611 | spl_fstrans_unmark(cookie); | |
612 | crfree(cr); | |
613 | ASSERT3S(error, <=, 0); | |
614 | ||
615 | return (error); | |
616 | } | |
617 | ||
618 | #ifdef HAVE_INODE_TRUNCATE_RANGE | |
619 | static void | |
620 | zpl_truncate_range(struct inode *ip, loff_t start, loff_t end) | |
621 | { | |
622 | cred_t *cr = CRED(); | |
623 | flock64_t bf; | |
624 | fstrans_cookie_t cookie; | |
625 | ||
626 | ASSERT3S(start, <=, end); | |
627 | ||
628 | /* | |
629 | * zfs_freesp() will interpret (len == 0) as meaning "truncate until | |
630 | * the end of the file". We don't want that. | |
631 | */ | |
632 | if (start == end) | |
633 | return; | |
634 | ||
635 | crhold(cr); | |
636 | ||
637 | bf.l_type = F_WRLCK; | |
638 | bf.l_whence = 0; | |
639 | bf.l_start = start; | |
640 | bf.l_len = end - start; | |
641 | bf.l_pid = 0; | |
642 | cookie = spl_fstrans_mark(); | |
643 | zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr); | |
644 | spl_fstrans_unmark(cookie); | |
645 | ||
646 | crfree(cr); | |
647 | } | |
648 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ | |
649 | ||
650 | #ifdef HAVE_INODE_FALLOCATE | |
651 | static long | |
652 | zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len) | |
653 | { | |
654 | return (zpl_fallocate_common(ip, mode, offset, len)); | |
655 | } | |
656 | #endif /* HAVE_INODE_FALLOCATE */ | |
657 | ||
658 | static int | |
659 | #ifdef HAVE_D_REVALIDATE_NAMEIDATA | |
660 | zpl_revalidate(struct dentry *dentry, struct nameidata *nd) | |
661 | { | |
662 | unsigned int flags = (nd ? nd->flags : 0); | |
663 | #else | |
664 | zpl_revalidate(struct dentry *dentry, unsigned int flags) | |
665 | { | |
666 | #endif /* HAVE_D_REVALIDATE_NAMEIDATA */ | |
86e3c28a CIK |
667 | /* CSTYLED */ |
668 | zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info; | |
70e083d2 TG |
669 | int error; |
670 | ||
671 | if (flags & LOOKUP_RCU) | |
672 | return (-ECHILD); | |
673 | ||
674 | /* | |
675 | * Automounted snapshots rely on periodic dentry revalidation | |
676 | * to defer snapshots from being automatically unmounted. | |
677 | */ | |
86e3c28a CIK |
678 | if (zfsvfs->z_issnap) { |
679 | if (time_after(jiffies, zfsvfs->z_snap_defer_time + | |
70e083d2 | 680 | MAX(zfs_expire_snapshot * HZ / 2, HZ))) { |
86e3c28a CIK |
681 | zfsvfs->z_snap_defer_time = jiffies; |
682 | zfsctl_snapshot_unmount_delay(zfsvfs->z_os->os_spa, | |
683 | dmu_objset_id(zfsvfs->z_os), zfs_expire_snapshot); | |
70e083d2 TG |
684 | } |
685 | } | |
686 | ||
687 | /* | |
688 | * After a rollback negative dentries created before the rollback | |
689 | * time must be invalidated. Otherwise they can obscure files which | |
690 | * are only present in the rolled back dataset. | |
691 | */ | |
692 | if (dentry->d_inode == NULL) { | |
693 | spin_lock(&dentry->d_lock); | |
86e3c28a | 694 | error = time_before(dentry->d_time, zfsvfs->z_rollback_time); |
70e083d2 TG |
695 | spin_unlock(&dentry->d_lock); |
696 | ||
697 | if (error) | |
698 | return (0); | |
699 | } | |
700 | ||
701 | /* | |
702 | * The dentry may reference a stale inode if a mounted file system | |
703 | * was rolled back to a point in time where the object didn't exist. | |
704 | */ | |
705 | if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale) | |
706 | return (0); | |
707 | ||
708 | return (1); | |
709 | } | |
710 | ||
711 | const struct inode_operations zpl_inode_operations = { | |
712 | .setattr = zpl_setattr, | |
713 | .getattr = zpl_getattr, | |
714 | #ifdef HAVE_GENERIC_SETXATTR | |
715 | .setxattr = generic_setxattr, | |
716 | .getxattr = generic_getxattr, | |
717 | .removexattr = generic_removexattr, | |
718 | #endif | |
719 | .listxattr = zpl_xattr_list, | |
720 | #ifdef HAVE_INODE_TRUNCATE_RANGE | |
721 | .truncate_range = zpl_truncate_range, | |
722 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ | |
723 | #ifdef HAVE_INODE_FALLOCATE | |
724 | .fallocate = zpl_fallocate, | |
725 | #endif /* HAVE_INODE_FALLOCATE */ | |
726 | #if defined(CONFIG_FS_POSIX_ACL) | |
727 | #if defined(HAVE_SET_ACL) | |
728 | .set_acl = zpl_set_acl, | |
729 | #endif | |
730 | #if defined(HAVE_GET_ACL) | |
731 | .get_acl = zpl_get_acl, | |
732 | #elif defined(HAVE_CHECK_ACL) | |
733 | .check_acl = zpl_check_acl, | |
734 | #elif defined(HAVE_PERMISSION) | |
735 | .permission = zpl_permission, | |
736 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
737 | #endif /* CONFIG_FS_POSIX_ACL */ | |
738 | }; | |
739 | ||
740 | const struct inode_operations zpl_dir_inode_operations = { | |
741 | .create = zpl_create, | |
742 | .lookup = zpl_lookup, | |
743 | .link = zpl_link, | |
744 | .unlink = zpl_unlink, | |
745 | .symlink = zpl_symlink, | |
746 | .mkdir = zpl_mkdir, | |
747 | .rmdir = zpl_rmdir, | |
748 | .mknod = zpl_mknod, | |
749 | #ifdef HAVE_RENAME_WANTS_FLAGS | |
750 | .rename = zpl_rename2, | |
751 | #else | |
752 | .rename = zpl_rename, | |
86e3c28a CIK |
753 | #endif |
754 | #ifdef HAVE_TMPFILE | |
755 | .tmpfile = zpl_tmpfile, | |
70e083d2 TG |
756 | #endif |
757 | .setattr = zpl_setattr, | |
758 | .getattr = zpl_getattr, | |
759 | #ifdef HAVE_GENERIC_SETXATTR | |
760 | .setxattr = generic_setxattr, | |
761 | .getxattr = generic_getxattr, | |
762 | .removexattr = generic_removexattr, | |
763 | #endif | |
764 | .listxattr = zpl_xattr_list, | |
765 | #if defined(CONFIG_FS_POSIX_ACL) | |
766 | #if defined(HAVE_SET_ACL) | |
767 | .set_acl = zpl_set_acl, | |
768 | #endif | |
769 | #if defined(HAVE_GET_ACL) | |
770 | .get_acl = zpl_get_acl, | |
771 | #elif defined(HAVE_CHECK_ACL) | |
772 | .check_acl = zpl_check_acl, | |
773 | #elif defined(HAVE_PERMISSION) | |
774 | .permission = zpl_permission, | |
775 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
776 | #endif /* CONFIG_FS_POSIX_ACL */ | |
777 | }; | |
778 | ||
779 | const struct inode_operations zpl_symlink_inode_operations = { | |
780 | #ifdef HAVE_GENERIC_READLINK | |
781 | .readlink = generic_readlink, | |
782 | #endif | |
783 | #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE) | |
784 | .get_link = zpl_get_link, | |
785 | #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA) | |
786 | .follow_link = zpl_follow_link, | |
787 | #endif | |
788 | #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA) | |
789 | .put_link = zpl_put_link, | |
790 | #endif | |
791 | .setattr = zpl_setattr, | |
792 | .getattr = zpl_getattr, | |
793 | #ifdef HAVE_GENERIC_SETXATTR | |
794 | .setxattr = generic_setxattr, | |
795 | .getxattr = generic_getxattr, | |
796 | .removexattr = generic_removexattr, | |
797 | #endif | |
798 | .listxattr = zpl_xattr_list, | |
799 | }; | |
800 | ||
801 | const struct inode_operations zpl_special_inode_operations = { | |
802 | .setattr = zpl_setattr, | |
803 | .getattr = zpl_getattr, | |
804 | #ifdef HAVE_GENERIC_SETXATTR | |
805 | .setxattr = generic_setxattr, | |
806 | .getxattr = generic_getxattr, | |
807 | .removexattr = generic_removexattr, | |
808 | #endif | |
809 | .listxattr = zpl_xattr_list, | |
810 | #if defined(CONFIG_FS_POSIX_ACL) | |
811 | #if defined(HAVE_SET_ACL) | |
812 | .set_acl = zpl_set_acl, | |
813 | #endif | |
814 | #if defined(HAVE_GET_ACL) | |
815 | .get_acl = zpl_get_acl, | |
816 | #elif defined(HAVE_CHECK_ACL) | |
817 | .check_acl = zpl_check_acl, | |
818 | #elif defined(HAVE_PERMISSION) | |
819 | .permission = zpl_permission, | |
820 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
821 | #endif /* CONFIG_FS_POSIX_ACL */ | |
822 | }; | |
823 | ||
824 | dentry_operations_t zpl_dentry_operations = { | |
825 | .d_revalidate = zpl_revalidate, | |
826 | }; |