]>
Commit | Line | Data |
---|---|---|
b6450630 SF |
1 | /* |
2 | * Copyright (C) 2005-2017 Junjiro R. Okajima | |
3 | * | |
4 | * This program, aufs is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * sub-routines for VFS | |
20 | */ | |
21 | ||
22 | #include <linux/namei.h> | |
23 | #include <linux/nsproxy.h> | |
24 | #include <linux/security.h> | |
25 | #include <linux/splice.h> | |
26 | #ifdef CONFIG_AUFS_BR_FUSE | |
27 | #include "../fs/mount.h" | |
28 | #endif | |
29 | #include "aufs.h" | |
30 | ||
31 | #ifdef CONFIG_AUFS_BR_FUSE | |
32 | int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb) | |
33 | { | |
34 | struct nsproxy *ns; | |
35 | ||
36 | if (!au_test_fuse(h_sb) || !au_userns) | |
37 | return 0; | |
38 | ||
39 | ns = current->nsproxy; | |
40 | /* no {get,put}_nsproxy(ns) */ | |
41 | return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES; | |
42 | } | |
43 | #endif | |
44 | ||
45 | int vfsub_sync_filesystem(struct super_block *h_sb, int wait) | |
46 | { | |
47 | int err; | |
48 | ||
49 | lockdep_off(); | |
50 | down_read(&h_sb->s_umount); | |
51 | err = __sync_filesystem(h_sb, wait); | |
52 | up_read(&h_sb->s_umount); | |
53 | lockdep_on(); | |
54 | ||
55 | return err; | |
56 | } | |
57 | ||
58 | /* ---------------------------------------------------------------------- */ | |
59 | ||
60 | int vfsub_update_h_iattr(struct path *h_path, int *did) | |
61 | { | |
62 | int err; | |
63 | struct kstat st; | |
64 | struct super_block *h_sb; | |
65 | ||
66 | /* for remote fs, leave work for its getattr or d_revalidate */ | |
67 | /* for bad i_attr fs, handle them in aufs_getattr() */ | |
68 | /* still some fs may acquire i_mutex. we need to skip them */ | |
69 | err = 0; | |
70 | if (!did) | |
71 | did = &err; | |
72 | h_sb = h_path->dentry->d_sb; | |
73 | *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb)); | |
74 | if (*did) | |
75 | err = vfsub_getattr(h_path, &st); | |
76 | ||
77 | return err; | |
78 | } | |
79 | ||
80 | /* ---------------------------------------------------------------------- */ | |
81 | ||
82 | struct file *vfsub_dentry_open(struct path *path, int flags) | |
83 | { | |
84 | struct file *file; | |
85 | ||
86 | file = dentry_open(path, flags /* | __FMODE_NONOTIFY */, | |
87 | current_cred()); | |
88 | if (!IS_ERR_OR_NULL(file) | |
89 | && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) | |
90 | i_readcount_inc(d_inode(path->dentry)); | |
91 | ||
92 | return file; | |
93 | } | |
94 | ||
95 | struct file *vfsub_filp_open(const char *path, int oflags, int mode) | |
96 | { | |
97 | struct file *file; | |
98 | ||
99 | lockdep_off(); | |
100 | file = filp_open(path, | |
101 | oflags /* | __FMODE_NONOTIFY */, | |
102 | mode); | |
103 | lockdep_on(); | |
104 | if (IS_ERR(file)) | |
105 | goto out; | |
106 | vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/ | |
107 | ||
108 | out: | |
109 | return file; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Ideally this function should call VFS:do_last() in order to keep all its | |
114 | * checkings. But it is very hard for aufs to regenerate several VFS internal | |
115 | * structure such as nameidata. This is a second (or third) best approach. | |
116 | * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open(). | |
117 | */ | |
118 | int vfsub_atomic_open(struct inode *dir, struct dentry *dentry, | |
119 | struct vfsub_aopen_args *args, struct au_branch *br) | |
120 | { | |
121 | int err; | |
122 | struct file *file = args->file; | |
123 | /* copied from linux/fs/namei.c:atomic_open() */ | |
124 | struct dentry *const DENTRY_NOT_SET = (void *)-1UL; | |
125 | ||
126 | IMustLock(dir); | |
127 | AuDebugOn(!dir->i_op->atomic_open); | |
128 | ||
129 | err = au_br_test_oflag(args->open_flag, br); | |
130 | if (unlikely(err)) | |
131 | goto out; | |
132 | ||
133 | args->file->f_path.dentry = DENTRY_NOT_SET; | |
134 | args->file->f_path.mnt = au_br_mnt(br); | |
135 | err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag, | |
136 | args->create_mode, args->opened); | |
137 | if (err >= 0) { | |
138 | /* some filesystems don't set FILE_CREATED while succeeded? */ | |
139 | if (*args->opened & FILE_CREATED) | |
140 | fsnotify_create(dir, dentry); | |
141 | } else | |
142 | goto out; | |
143 | ||
144 | ||
145 | if (!err) { | |
146 | /* todo: call VFS:may_open() here */ | |
147 | err = open_check_o_direct(file); | |
148 | /* todo: ima_file_check() too? */ | |
149 | if (!err && (args->open_flag & __FMODE_EXEC)) | |
150 | err = deny_write_access(file); | |
151 | if (unlikely(err)) | |
152 | /* note that the file is created and still opened */ | |
153 | goto out; | |
154 | } | |
155 | ||
156 | au_br_get(br); | |
157 | fsnotify_open(file); | |
158 | ||
159 | out: | |
160 | return err; | |
161 | } | |
162 | ||
163 | int vfsub_kern_path(const char *name, unsigned int flags, struct path *path) | |
164 | { | |
165 | int err; | |
166 | ||
167 | err = kern_path(name, flags, path); | |
168 | if (!err && d_is_positive(path->dentry)) | |
169 | vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/ | |
170 | return err; | |
171 | } | |
172 | ||
173 | struct dentry *vfsub_lookup_one_len_unlocked(const char *name, | |
174 | struct dentry *parent, int len) | |
175 | { | |
176 | struct path path = { | |
177 | .mnt = NULL | |
178 | }; | |
179 | ||
180 | path.dentry = lookup_one_len_unlocked(name, parent, len); | |
181 | if (IS_ERR(path.dentry)) | |
182 | goto out; | |
183 | if (d_is_positive(path.dentry)) | |
184 | vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/ | |
185 | ||
186 | out: | |
187 | AuTraceErrPtr(path.dentry); | |
188 | return path.dentry; | |
189 | } | |
190 | ||
191 | struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent, | |
192 | int len) | |
193 | { | |
194 | struct path path = { | |
195 | .mnt = NULL | |
196 | }; | |
197 | ||
198 | /* VFS checks it too, but by WARN_ON_ONCE() */ | |
199 | IMustLock(d_inode(parent)); | |
200 | ||
201 | path.dentry = lookup_one_len(name, parent, len); | |
202 | if (IS_ERR(path.dentry)) | |
203 | goto out; | |
204 | if (d_is_positive(path.dentry)) | |
205 | vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/ | |
206 | ||
207 | out: | |
208 | AuTraceErrPtr(path.dentry); | |
209 | return path.dentry; | |
210 | } | |
211 | ||
212 | void vfsub_call_lkup_one(void *args) | |
213 | { | |
214 | struct vfsub_lkup_one_args *a = args; | |
215 | *a->errp = vfsub_lkup_one(a->name, a->parent); | |
216 | } | |
217 | ||
218 | /* ---------------------------------------------------------------------- */ | |
219 | ||
220 | struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1, | |
221 | struct dentry *d2, struct au_hinode *hdir2) | |
222 | { | |
223 | struct dentry *d; | |
224 | ||
225 | lockdep_off(); | |
226 | d = lock_rename(d1, d2); | |
227 | lockdep_on(); | |
228 | au_hn_suspend(hdir1); | |
229 | if (hdir1 != hdir2) | |
230 | au_hn_suspend(hdir2); | |
231 | ||
232 | return d; | |
233 | } | |
234 | ||
235 | void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1, | |
236 | struct dentry *d2, struct au_hinode *hdir2) | |
237 | { | |
238 | au_hn_resume(hdir1); | |
239 | if (hdir1 != hdir2) | |
240 | au_hn_resume(hdir2); | |
241 | lockdep_off(); | |
242 | unlock_rename(d1, d2); | |
243 | lockdep_on(); | |
244 | } | |
245 | ||
246 | /* ---------------------------------------------------------------------- */ | |
247 | ||
248 | int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl) | |
249 | { | |
250 | int err; | |
251 | struct dentry *d; | |
252 | ||
253 | IMustLock(dir); | |
254 | ||
255 | d = path->dentry; | |
256 | path->dentry = d->d_parent; | |
257 | err = security_path_mknod(path, d, mode, 0); | |
258 | path->dentry = d; | |
259 | if (unlikely(err)) | |
260 | goto out; | |
261 | ||
262 | lockdep_off(); | |
263 | err = vfs_create(dir, path->dentry, mode, want_excl); | |
264 | lockdep_on(); | |
265 | if (!err) { | |
266 | struct path tmp = *path; | |
267 | int did; | |
268 | ||
269 | vfsub_update_h_iattr(&tmp, &did); | |
270 | if (did) { | |
271 | tmp.dentry = path->dentry->d_parent; | |
272 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
273 | } | |
274 | /*ignore*/ | |
275 | } | |
276 | ||
277 | out: | |
278 | return err; | |
279 | } | |
280 | ||
281 | int vfsub_symlink(struct inode *dir, struct path *path, const char *symname) | |
282 | { | |
283 | int err; | |
284 | struct dentry *d; | |
285 | ||
286 | IMustLock(dir); | |
287 | ||
288 | d = path->dentry; | |
289 | path->dentry = d->d_parent; | |
290 | err = security_path_symlink(path, d, symname); | |
291 | path->dentry = d; | |
292 | if (unlikely(err)) | |
293 | goto out; | |
294 | ||
295 | lockdep_off(); | |
296 | err = vfs_symlink(dir, path->dentry, symname); | |
297 | lockdep_on(); | |
298 | if (!err) { | |
299 | struct path tmp = *path; | |
300 | int did; | |
301 | ||
302 | vfsub_update_h_iattr(&tmp, &did); | |
303 | if (did) { | |
304 | tmp.dentry = path->dentry->d_parent; | |
305 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
306 | } | |
307 | /*ignore*/ | |
308 | } | |
309 | ||
310 | out: | |
311 | return err; | |
312 | } | |
313 | ||
314 | int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev) | |
315 | { | |
316 | int err; | |
317 | struct dentry *d; | |
318 | ||
319 | IMustLock(dir); | |
320 | ||
321 | d = path->dentry; | |
322 | path->dentry = d->d_parent; | |
323 | err = security_path_mknod(path, d, mode, new_encode_dev(dev)); | |
324 | path->dentry = d; | |
325 | if (unlikely(err)) | |
326 | goto out; | |
327 | ||
328 | lockdep_off(); | |
329 | err = vfs_mknod(dir, path->dentry, mode, dev); | |
330 | lockdep_on(); | |
331 | if (!err) { | |
332 | struct path tmp = *path; | |
333 | int did; | |
334 | ||
335 | vfsub_update_h_iattr(&tmp, &did); | |
336 | if (did) { | |
337 | tmp.dentry = path->dentry->d_parent; | |
338 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
339 | } | |
340 | /*ignore*/ | |
341 | } | |
342 | ||
343 | out: | |
344 | return err; | |
345 | } | |
346 | ||
347 | static int au_test_nlink(struct inode *inode) | |
348 | { | |
349 | const unsigned int link_max = UINT_MAX >> 1; /* rough margin */ | |
350 | ||
351 | if (!au_test_fs_no_limit_nlink(inode->i_sb) | |
352 | || inode->i_nlink < link_max) | |
353 | return 0; | |
354 | return -EMLINK; | |
355 | } | |
356 | ||
357 | int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path, | |
358 | struct inode **delegated_inode) | |
359 | { | |
360 | int err; | |
361 | struct dentry *d; | |
362 | ||
363 | IMustLock(dir); | |
364 | ||
365 | err = au_test_nlink(d_inode(src_dentry)); | |
366 | if (unlikely(err)) | |
367 | return err; | |
368 | ||
369 | /* we don't call may_linkat() */ | |
370 | d = path->dentry; | |
371 | path->dentry = d->d_parent; | |
372 | err = security_path_link(src_dentry, path, d); | |
373 | path->dentry = d; | |
374 | if (unlikely(err)) | |
375 | goto out; | |
376 | ||
377 | lockdep_off(); | |
378 | err = vfs_link(src_dentry, dir, path->dentry, delegated_inode); | |
379 | lockdep_on(); | |
380 | if (!err) { | |
381 | struct path tmp = *path; | |
382 | int did; | |
383 | ||
384 | /* fuse has different memory inode for the same inumber */ | |
385 | vfsub_update_h_iattr(&tmp, &did); | |
386 | if (did) { | |
387 | tmp.dentry = path->dentry->d_parent; | |
388 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
389 | tmp.dentry = src_dentry; | |
390 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
391 | } | |
392 | /*ignore*/ | |
393 | } | |
394 | ||
395 | out: | |
396 | return err; | |
397 | } | |
398 | ||
399 | int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry, | |
400 | struct inode *dir, struct path *path, | |
401 | struct inode **delegated_inode, unsigned int flags) | |
402 | { | |
403 | int err; | |
404 | struct path tmp = { | |
405 | .mnt = path->mnt | |
406 | }; | |
407 | struct dentry *d; | |
408 | ||
409 | IMustLock(dir); | |
410 | IMustLock(src_dir); | |
411 | ||
412 | d = path->dentry; | |
413 | path->dentry = d->d_parent; | |
414 | tmp.dentry = src_dentry->d_parent; | |
415 | err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0); | |
416 | path->dentry = d; | |
417 | if (unlikely(err)) | |
418 | goto out; | |
419 | ||
420 | lockdep_off(); | |
421 | err = vfs_rename(src_dir, src_dentry, dir, path->dentry, | |
422 | delegated_inode, flags); | |
423 | lockdep_on(); | |
424 | if (!err) { | |
425 | int did; | |
426 | ||
427 | tmp.dentry = d->d_parent; | |
428 | vfsub_update_h_iattr(&tmp, &did); | |
429 | if (did) { | |
430 | tmp.dentry = src_dentry; | |
431 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
432 | tmp.dentry = src_dentry->d_parent; | |
433 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
434 | } | |
435 | /*ignore*/ | |
436 | } | |
437 | ||
438 | out: | |
439 | return err; | |
440 | } | |
441 | ||
442 | int vfsub_mkdir(struct inode *dir, struct path *path, int mode) | |
443 | { | |
444 | int err; | |
445 | struct dentry *d; | |
446 | ||
447 | IMustLock(dir); | |
448 | ||
449 | d = path->dentry; | |
450 | path->dentry = d->d_parent; | |
451 | err = security_path_mkdir(path, d, mode); | |
452 | path->dentry = d; | |
453 | if (unlikely(err)) | |
454 | goto out; | |
455 | ||
456 | lockdep_off(); | |
457 | err = vfs_mkdir(dir, path->dentry, mode); | |
458 | lockdep_on(); | |
459 | if (!err) { | |
460 | struct path tmp = *path; | |
461 | int did; | |
462 | ||
463 | vfsub_update_h_iattr(&tmp, &did); | |
464 | if (did) { | |
465 | tmp.dentry = path->dentry->d_parent; | |
466 | vfsub_update_h_iattr(&tmp, /*did*/NULL); | |
467 | } | |
468 | /*ignore*/ | |
469 | } | |
470 | ||
471 | out: | |
472 | return err; | |
473 | } | |
474 | ||
475 | int vfsub_rmdir(struct inode *dir, struct path *path) | |
476 | { | |
477 | int err; | |
478 | struct dentry *d; | |
479 | ||
480 | IMustLock(dir); | |
481 | ||
482 | d = path->dentry; | |
483 | path->dentry = d->d_parent; | |
484 | err = security_path_rmdir(path, d); | |
485 | path->dentry = d; | |
486 | if (unlikely(err)) | |
487 | goto out; | |
488 | ||
489 | lockdep_off(); | |
490 | err = vfs_rmdir(dir, path->dentry); | |
491 | lockdep_on(); | |
492 | if (!err) { | |
493 | struct path tmp = { | |
494 | .dentry = path->dentry->d_parent, | |
495 | .mnt = path->mnt | |
496 | }; | |
497 | ||
498 | vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/ | |
499 | } | |
500 | ||
501 | out: | |
502 | return err; | |
503 | } | |
504 | ||
505 | /* ---------------------------------------------------------------------- */ | |
506 | ||
507 | /* todo: support mmap_sem? */ | |
508 | ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count, | |
509 | loff_t *ppos) | |
510 | { | |
511 | ssize_t err; | |
512 | ||
513 | lockdep_off(); | |
514 | err = vfs_read(file, ubuf, count, ppos); | |
515 | lockdep_on(); | |
516 | if (err >= 0) | |
517 | vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/ | |
518 | return err; | |
519 | } | |
520 | ||
521 | /* todo: kernel_read()? */ | |
522 | ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count, | |
523 | loff_t *ppos) | |
524 | { | |
525 | ssize_t err; | |
526 | mm_segment_t oldfs; | |
527 | union { | |
528 | void *k; | |
529 | char __user *u; | |
530 | } buf; | |
531 | ||
532 | buf.k = kbuf; | |
533 | oldfs = get_fs(); | |
534 | set_fs(KERNEL_DS); | |
535 | err = vfsub_read_u(file, buf.u, count, ppos); | |
536 | set_fs(oldfs); | |
537 | return err; | |
538 | } | |
539 | ||
540 | ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count, | |
541 | loff_t *ppos) | |
542 | { | |
543 | ssize_t err; | |
544 | ||
545 | lockdep_off(); | |
546 | err = vfs_write(file, ubuf, count, ppos); | |
547 | lockdep_on(); | |
548 | if (err >= 0) | |
549 | vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/ | |
550 | return err; | |
551 | } | |
552 | ||
553 | ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos) | |
554 | { | |
555 | ssize_t err; | |
556 | mm_segment_t oldfs; | |
557 | union { | |
558 | void *k; | |
559 | const char __user *u; | |
560 | } buf; | |
561 | ||
562 | buf.k = kbuf; | |
563 | oldfs = get_fs(); | |
564 | set_fs(KERNEL_DS); | |
565 | err = vfsub_write_u(file, buf.u, count, ppos); | |
566 | set_fs(oldfs); | |
567 | return err; | |
568 | } | |
569 | ||
570 | int vfsub_flush(struct file *file, fl_owner_t id) | |
571 | { | |
572 | int err; | |
573 | ||
574 | err = 0; | |
575 | if (file->f_op->flush) { | |
576 | if (!au_test_nfs(file->f_path.dentry->d_sb)) | |
577 | err = file->f_op->flush(file, id); | |
578 | else { | |
579 | lockdep_off(); | |
580 | err = file->f_op->flush(file, id); | |
581 | lockdep_on(); | |
582 | } | |
583 | if (!err) | |
584 | vfsub_update_h_iattr(&file->f_path, /*did*/NULL); | |
585 | /*ignore*/ | |
586 | } | |
587 | return err; | |
588 | } | |
589 | ||
590 | int vfsub_iterate_dir(struct file *file, struct dir_context *ctx) | |
591 | { | |
592 | int err; | |
593 | ||
594 | AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos); | |
595 | ||
596 | lockdep_off(); | |
597 | err = iterate_dir(file, ctx); | |
598 | lockdep_on(); | |
599 | if (err >= 0) | |
600 | vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/ | |
601 | ||
602 | return err; | |
603 | } | |
604 | ||
605 | long vfsub_splice_to(struct file *in, loff_t *ppos, | |
606 | struct pipe_inode_info *pipe, size_t len, | |
607 | unsigned int flags) | |
608 | { | |
609 | long err; | |
610 | ||
611 | lockdep_off(); | |
612 | err = do_splice_to(in, ppos, pipe, len, flags); | |
613 | lockdep_on(); | |
614 | file_accessed(in); | |
615 | if (err >= 0) | |
616 | vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/ | |
617 | return err; | |
618 | } | |
619 | ||
620 | long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out, | |
621 | loff_t *ppos, size_t len, unsigned int flags) | |
622 | { | |
623 | long err; | |
624 | ||
625 | lockdep_off(); | |
626 | err = do_splice_from(pipe, out, ppos, len, flags); | |
627 | lockdep_on(); | |
628 | if (err >= 0) | |
629 | vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/ | |
630 | return err; | |
631 | } | |
632 | ||
633 | int vfsub_fsync(struct file *file, struct path *path, int datasync) | |
634 | { | |
635 | int err; | |
636 | ||
637 | /* file can be NULL */ | |
638 | lockdep_off(); | |
639 | err = vfs_fsync(file, datasync); | |
640 | lockdep_on(); | |
641 | if (!err) { | |
642 | if (!path) { | |
643 | AuDebugOn(!file); | |
644 | path = &file->f_path; | |
645 | } | |
646 | vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/ | |
647 | } | |
648 | return err; | |
649 | } | |
650 | ||
651 | /* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */ | |
652 | int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr, | |
653 | struct file *h_file) | |
654 | { | |
655 | int err; | |
656 | struct inode *h_inode; | |
657 | struct super_block *h_sb; | |
658 | ||
659 | if (!h_file) { | |
660 | err = vfsub_truncate(h_path, length); | |
661 | goto out; | |
662 | } | |
663 | ||
664 | h_inode = d_inode(h_path->dentry); | |
665 | h_sb = h_inode->i_sb; | |
666 | lockdep_off(); | |
667 | sb_start_write(h_sb); | |
668 | lockdep_on(); | |
669 | err = locks_verify_truncate(h_inode, h_file, length); | |
670 | if (!err) | |
671 | err = security_path_truncate(h_path); | |
672 | if (!err) { | |
673 | lockdep_off(); | |
674 | err = do_truncate(h_path->dentry, length, attr, h_file); | |
675 | lockdep_on(); | |
676 | } | |
677 | lockdep_off(); | |
678 | sb_end_write(h_sb); | |
679 | lockdep_on(); | |
680 | ||
681 | out: | |
682 | return err; | |
683 | } | |
684 | ||
685 | /* ---------------------------------------------------------------------- */ | |
686 | ||
687 | struct au_vfsub_mkdir_args { | |
688 | int *errp; | |
689 | struct inode *dir; | |
690 | struct path *path; | |
691 | int mode; | |
692 | }; | |
693 | ||
694 | static void au_call_vfsub_mkdir(void *args) | |
695 | { | |
696 | struct au_vfsub_mkdir_args *a = args; | |
697 | *a->errp = vfsub_mkdir(a->dir, a->path, a->mode); | |
698 | } | |
699 | ||
700 | int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode) | |
701 | { | |
702 | int err, do_sio, wkq_err; | |
703 | ||
704 | do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE); | |
705 | if (!do_sio) { | |
706 | lockdep_off(); | |
707 | err = vfsub_mkdir(dir, path, mode); | |
708 | lockdep_on(); | |
709 | } else { | |
710 | struct au_vfsub_mkdir_args args = { | |
711 | .errp = &err, | |
712 | .dir = dir, | |
713 | .path = path, | |
714 | .mode = mode | |
715 | }; | |
716 | wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args); | |
717 | if (unlikely(wkq_err)) | |
718 | err = wkq_err; | |
719 | } | |
720 | ||
721 | return err; | |
722 | } | |
723 | ||
724 | struct au_vfsub_rmdir_args { | |
725 | int *errp; | |
726 | struct inode *dir; | |
727 | struct path *path; | |
728 | }; | |
729 | ||
730 | static void au_call_vfsub_rmdir(void *args) | |
731 | { | |
732 | struct au_vfsub_rmdir_args *a = args; | |
733 | *a->errp = vfsub_rmdir(a->dir, a->path); | |
734 | } | |
735 | ||
736 | int vfsub_sio_rmdir(struct inode *dir, struct path *path) | |
737 | { | |
738 | int err, do_sio, wkq_err; | |
739 | ||
740 | do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE); | |
741 | if (!do_sio) { | |
742 | lockdep_off(); | |
743 | err = vfsub_rmdir(dir, path); | |
744 | lockdep_on(); | |
745 | } else { | |
746 | struct au_vfsub_rmdir_args args = { | |
747 | .errp = &err, | |
748 | .dir = dir, | |
749 | .path = path | |
750 | }; | |
751 | wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args); | |
752 | if (unlikely(wkq_err)) | |
753 | err = wkq_err; | |
754 | } | |
755 | ||
756 | return err; | |
757 | } | |
758 | ||
759 | /* ---------------------------------------------------------------------- */ | |
760 | ||
761 | struct notify_change_args { | |
762 | int *errp; | |
763 | struct path *path; | |
764 | struct iattr *ia; | |
765 | struct inode **delegated_inode; | |
766 | }; | |
767 | ||
768 | static void call_notify_change(void *args) | |
769 | { | |
770 | struct notify_change_args *a = args; | |
771 | struct inode *h_inode; | |
772 | ||
773 | h_inode = d_inode(a->path->dentry); | |
774 | IMustLock(h_inode); | |
775 | ||
776 | *a->errp = -EPERM; | |
777 | if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) { | |
778 | lockdep_off(); | |
779 | *a->errp = notify_change(a->path->dentry, a->ia, | |
780 | a->delegated_inode); | |
781 | lockdep_on(); | |
782 | if (!*a->errp) | |
783 | vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/ | |
784 | } | |
785 | AuTraceErr(*a->errp); | |
786 | } | |
787 | ||
788 | int vfsub_notify_change(struct path *path, struct iattr *ia, | |
789 | struct inode **delegated_inode) | |
790 | { | |
791 | int err; | |
792 | struct notify_change_args args = { | |
793 | .errp = &err, | |
794 | .path = path, | |
795 | .ia = ia, | |
796 | .delegated_inode = delegated_inode | |
797 | }; | |
798 | ||
799 | call_notify_change(&args); | |
800 | ||
801 | return err; | |
802 | } | |
803 | ||
804 | int vfsub_sio_notify_change(struct path *path, struct iattr *ia, | |
805 | struct inode **delegated_inode) | |
806 | { | |
807 | int err, wkq_err; | |
808 | struct notify_change_args args = { | |
809 | .errp = &err, | |
810 | .path = path, | |
811 | .ia = ia, | |
812 | .delegated_inode = delegated_inode | |
813 | }; | |
814 | ||
815 | wkq_err = au_wkq_wait(call_notify_change, &args); | |
816 | if (unlikely(wkq_err)) | |
817 | err = wkq_err; | |
818 | ||
819 | return err; | |
820 | } | |
821 | ||
822 | /* ---------------------------------------------------------------------- */ | |
823 | ||
824 | struct unlink_args { | |
825 | int *errp; | |
826 | struct inode *dir; | |
827 | struct path *path; | |
828 | struct inode **delegated_inode; | |
829 | }; | |
830 | ||
831 | static void call_unlink(void *args) | |
832 | { | |
833 | struct unlink_args *a = args; | |
834 | struct dentry *d = a->path->dentry; | |
835 | struct inode *h_inode; | |
836 | const int stop_sillyrename = (au_test_nfs(d->d_sb) | |
837 | && au_dcount(d) == 1); | |
838 | ||
839 | IMustLock(a->dir); | |
840 | ||
841 | a->path->dentry = d->d_parent; | |
842 | *a->errp = security_path_unlink(a->path, d); | |
843 | a->path->dentry = d; | |
844 | if (unlikely(*a->errp)) | |
845 | return; | |
846 | ||
847 | if (!stop_sillyrename) | |
848 | dget(d); | |
849 | h_inode = NULL; | |
850 | if (d_is_positive(d)) { | |
851 | h_inode = d_inode(d); | |
852 | ihold(h_inode); | |
853 | } | |
854 | ||
855 | lockdep_off(); | |
856 | *a->errp = vfs_unlink(a->dir, d, a->delegated_inode); | |
857 | lockdep_on(); | |
858 | if (!*a->errp) { | |
859 | struct path tmp = { | |
860 | .dentry = d->d_parent, | |
861 | .mnt = a->path->mnt | |
862 | }; | |
863 | vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/ | |
864 | } | |
865 | ||
866 | if (!stop_sillyrename) | |
867 | dput(d); | |
868 | if (h_inode) | |
869 | iput(h_inode); | |
870 | ||
871 | AuTraceErr(*a->errp); | |
872 | } | |
873 | ||
874 | /* | |
875 | * @dir: must be locked. | |
876 | * @dentry: target dentry. | |
877 | */ | |
878 | int vfsub_unlink(struct inode *dir, struct path *path, | |
879 | struct inode **delegated_inode, int force) | |
880 | { | |
881 | int err; | |
882 | struct unlink_args args = { | |
883 | .errp = &err, | |
884 | .dir = dir, | |
885 | .path = path, | |
886 | .delegated_inode = delegated_inode | |
887 | }; | |
888 | ||
889 | if (!force) | |
890 | call_unlink(&args); | |
891 | else { | |
892 | int wkq_err; | |
893 | ||
894 | wkq_err = au_wkq_wait(call_unlink, &args); | |
895 | if (unlikely(wkq_err)) | |
896 | err = wkq_err; | |
897 | } | |
898 | ||
899 | return err; | |
900 | } |