]>
Commit | Line | Data |
---|---|---|
a3a49a17 SF |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
e4a3f096 | 3 | * Copyright (C) 2005-2020 Junjiro R. Okajima |
a3a49a17 SF |
4 | * |
5 | * This program, aufs is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * inode operations (except add/del/rename) | |
21 | */ | |
22 | ||
23 | #include <linux/device_cgroup.h> | |
24 | #include <linux/fs_stack.h> | |
25 | #include <linux/iversion.h> | |
26 | #include <linux/namei.h> | |
27 | #include <linux/security.h> | |
28 | #include "aufs.h" | |
29 | ||
30 | static int h_permission(struct inode *h_inode, int mask, | |
31 | struct path *h_path, int brperm) | |
32 | { | |
33 | int err; | |
34 | const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND)); | |
35 | ||
36 | err = -EPERM; | |
37 | if (write_mask && IS_IMMUTABLE(h_inode)) | |
38 | goto out; | |
39 | ||
40 | err = -EACCES; | |
41 | if (((mask & MAY_EXEC) | |
42 | && S_ISREG(h_inode->i_mode) | |
43 | && (path_noexec(h_path) | |
44 | || !(h_inode->i_mode & 0111)))) | |
45 | goto out; | |
46 | ||
47 | /* | |
48 | * - skip the lower fs test in the case of write to ro branch. | |
49 | * - nfs dir permission write check is optimized, but a policy for | |
50 | * link/rename requires a real check. | |
51 | * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.' | |
52 | * in this case, generic_permission() returns -EOPNOTSUPP. | |
53 | */ | |
54 | if ((write_mask && !au_br_writable(brperm)) | |
55 | || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode) | |
56 | && write_mask && !(mask & MAY_READ)) | |
57 | || !h_inode->i_op->permission) { | |
58 | /* AuLabel(generic_permission); */ | |
59 | /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */ | |
60 | err = generic_permission(h_inode, mask); | |
61 | if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode)) | |
62 | err = h_inode->i_op->permission(h_inode, mask); | |
63 | AuTraceErr(err); | |
64 | } else { | |
65 | /* AuLabel(h_inode->permission); */ | |
66 | err = h_inode->i_op->permission(h_inode, mask); | |
67 | AuTraceErr(err); | |
68 | } | |
69 | ||
70 | if (!err) | |
71 | err = devcgroup_inode_permission(h_inode, mask); | |
72 | if (!err) | |
73 | err = security_inode_permission(h_inode, mask); | |
74 | ||
a3a49a17 SF |
75 | out: |
76 | return err; | |
77 | } | |
78 | ||
79 | static int aufs_permission(struct inode *inode, int mask) | |
80 | { | |
81 | int err; | |
82 | aufs_bindex_t bindex, bbot; | |
83 | const unsigned char isdir = !!S_ISDIR(inode->i_mode), | |
84 | write_mask = !!(mask & (MAY_WRITE | MAY_APPEND)); | |
85 | struct inode *h_inode; | |
86 | struct super_block *sb; | |
87 | struct au_branch *br; | |
88 | ||
89 | /* todo: support rcu-walk? */ | |
90 | if (mask & MAY_NOT_BLOCK) | |
91 | return -ECHILD; | |
92 | ||
93 | sb = inode->i_sb; | |
94 | si_read_lock(sb, AuLock_FLUSH); | |
95 | ii_read_lock_child(inode); | |
e4a3f096 SF |
96 | #if 0 /* reserved for future use */ |
97 | /* | |
98 | * This test may be rather 'too much' since the test is essentially done | |
99 | * in the aufs_lookup(). Theoretically it is possible that the inode | |
100 | * generation doesn't match to the superblock's here. But it isn't a | |
101 | * big deal I suppose. | |
102 | */ | |
a3a49a17 SF |
103 | err = au_iigen_test(inode, au_sigen(sb)); |
104 | if (unlikely(err)) | |
105 | goto out; | |
106 | #endif | |
107 | ||
108 | if (!isdir | |
109 | || write_mask | |
110 | || au_opt_test(au_mntflags(sb), DIRPERM1)) { | |
111 | err = au_busy_or_stale(); | |
112 | h_inode = au_h_iptr(inode, au_ibtop(inode)); | |
113 | if (unlikely(!h_inode | |
114 | || (h_inode->i_mode & S_IFMT) | |
115 | != (inode->i_mode & S_IFMT))) | |
116 | goto out; | |
117 | ||
118 | err = 0; | |
119 | bindex = au_ibtop(inode); | |
120 | br = au_sbr(sb, bindex); | |
121 | err = h_permission(h_inode, mask, &br->br_path, br->br_perm); | |
122 | if (write_mask | |
123 | && !err | |
124 | && !special_file(h_inode->i_mode)) { | |
125 | /* test whether the upper writable branch exists */ | |
126 | err = -EROFS; | |
127 | for (; bindex >= 0; bindex--) | |
128 | if (!au_br_rdonly(au_sbr(sb, bindex))) { | |
129 | err = 0; | |
130 | break; | |
131 | } | |
132 | } | |
133 | goto out; | |
134 | } | |
135 | ||
136 | /* non-write to dir */ | |
137 | err = 0; | |
138 | bbot = au_ibbot(inode); | |
139 | for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) { | |
140 | h_inode = au_h_iptr(inode, bindex); | |
141 | if (h_inode) { | |
142 | err = au_busy_or_stale(); | |
143 | if (unlikely(!S_ISDIR(h_inode->i_mode))) | |
144 | break; | |
145 | ||
146 | br = au_sbr(sb, bindex); | |
147 | err = h_permission(h_inode, mask, &br->br_path, | |
148 | br->br_perm); | |
149 | } | |
150 | } | |
151 | ||
152 | out: | |
153 | ii_read_unlock(inode); | |
154 | si_read_unlock(sb); | |
155 | return err; | |
156 | } | |
157 | ||
158 | /* ---------------------------------------------------------------------- */ | |
159 | ||
160 | static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry, | |
161 | unsigned int flags) | |
162 | { | |
163 | struct dentry *ret, *parent; | |
164 | struct inode *inode; | |
165 | struct super_block *sb; | |
166 | int err, npositive; | |
167 | ||
168 | IMustLock(dir); | |
169 | ||
170 | /* todo: support rcu-walk? */ | |
171 | ret = ERR_PTR(-ECHILD); | |
172 | if (flags & LOOKUP_RCU) | |
173 | goto out; | |
174 | ||
175 | ret = ERR_PTR(-ENAMETOOLONG); | |
176 | if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN)) | |
177 | goto out; | |
178 | ||
179 | sb = dir->i_sb; | |
180 | err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM); | |
181 | ret = ERR_PTR(err); | |
182 | if (unlikely(err)) | |
183 | goto out; | |
184 | ||
185 | err = au_di_init(dentry); | |
186 | ret = ERR_PTR(err); | |
187 | if (unlikely(err)) | |
188 | goto out_si; | |
189 | ||
190 | inode = NULL; | |
191 | npositive = 0; /* suppress a warning */ | |
192 | parent = dentry->d_parent; /* dir inode is locked */ | |
193 | di_read_lock_parent(parent, AuLock_IR); | |
194 | err = au_alive_dir(parent); | |
195 | if (!err) | |
196 | err = au_digen_test(parent, au_sigen(sb)); | |
197 | if (!err) { | |
198 | /* regardless LOOKUP_CREATE, always ALLOW_NEG */ | |
199 | npositive = au_lkup_dentry(dentry, au_dbtop(parent), | |
200 | AuLkup_ALLOW_NEG); | |
201 | err = npositive; | |
202 | } | |
203 | di_read_unlock(parent, AuLock_IR); | |
204 | ret = ERR_PTR(err); | |
205 | if (unlikely(err < 0)) | |
206 | goto out_unlock; | |
207 | ||
208 | if (npositive) { | |
209 | inode = au_new_inode(dentry, /*must_new*/0); | |
210 | if (IS_ERR(inode)) { | |
211 | ret = (void *)inode; | |
212 | inode = NULL; | |
213 | goto out_unlock; | |
214 | } | |
215 | } | |
216 | ||
217 | if (inode) | |
218 | atomic_inc(&inode->i_count); | |
219 | ret = d_splice_alias(inode, dentry); | |
e4a3f096 | 220 | #if 0 /* reserved for future use */ |
a3a49a17 SF |
221 | if (unlikely(d_need_lookup(dentry))) { |
222 | spin_lock(&dentry->d_lock); | |
223 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; | |
224 | spin_unlock(&dentry->d_lock); | |
225 | } else | |
226 | #endif | |
227 | if (inode) { | |
228 | if (!IS_ERR(ret)) { | |
229 | iput(inode); | |
230 | if (ret && ret != dentry) | |
231 | ii_write_unlock(inode); | |
232 | } else { | |
233 | ii_write_unlock(inode); | |
234 | iput(inode); | |
235 | inode = NULL; | |
236 | } | |
237 | } | |
238 | ||
239 | out_unlock: | |
240 | di_write_unlock(dentry); | |
241 | out_si: | |
242 | si_read_unlock(sb); | |
243 | out: | |
244 | return ret; | |
245 | } | |
246 | ||
247 | /* ---------------------------------------------------------------------- */ | |
248 | ||
249 | /* | |
250 | * very dirty and complicated aufs ->atomic_open(). | |
251 | * aufs_atomic_open() | |
252 | * + au_aopen_or_create() | |
253 | * + add_simple() | |
254 | * + vfsub_atomic_open() | |
255 | * + branch fs ->atomic_open() | |
256 | * may call the actual 'open' for h_file | |
257 | * + inc br_nfiles only if opened | |
258 | * + au_aopen_no_open() or au_aopen_do_open() | |
259 | * | |
260 | * au_aopen_do_open() | |
261 | * + finish_open() | |
262 | * + au_do_aopen() | |
263 | * + au_do_open() the body of all 'open' | |
264 | * + au_do_open_nondir() | |
265 | * set the passed h_file | |
266 | * | |
267 | * au_aopen_no_open() | |
268 | * + finish_no_open() | |
269 | */ | |
270 | ||
271 | struct aopen_node { | |
272 | struct hlist_bl_node hblist; | |
273 | struct file *file, *h_file; | |
274 | }; | |
275 | ||
276 | static int au_do_aopen(struct inode *inode, struct file *file) | |
277 | { | |
278 | struct hlist_bl_head *aopen; | |
279 | struct hlist_bl_node *pos; | |
280 | struct aopen_node *node; | |
281 | struct au_do_open_args args = { | |
282 | .aopen = 1, | |
283 | .open = au_do_open_nondir | |
284 | }; | |
285 | ||
286 | aopen = &au_sbi(inode->i_sb)->si_aopen; | |
287 | hlist_bl_lock(aopen); | |
288 | hlist_bl_for_each_entry(node, pos, aopen, hblist) | |
289 | if (node->file == file) { | |
290 | args.h_file = node->h_file; | |
291 | break; | |
292 | } | |
293 | hlist_bl_unlock(aopen); | |
294 | /* AuDebugOn(!args.h_file); */ | |
295 | ||
296 | return au_do_open(file, &args); | |
297 | } | |
298 | ||
299 | static int au_aopen_do_open(struct file *file, struct dentry *dentry, | |
300 | struct aopen_node *aopen_node) | |
301 | { | |
302 | int err; | |
303 | struct hlist_bl_head *aopen; | |
304 | ||
305 | AuLabel(here); | |
306 | aopen = &au_sbi(dentry->d_sb)->si_aopen; | |
307 | au_hbl_add(&aopen_node->hblist, aopen); | |
308 | err = finish_open(file, dentry, au_do_aopen); | |
309 | au_hbl_del(&aopen_node->hblist, aopen); | |
310 | /* AuDbgFile(file); */ | |
311 | AuDbg("%pd%s%s\n", dentry, | |
312 | (file->f_mode & FMODE_CREATED) ? " created" : "", | |
313 | (file->f_mode & FMODE_OPENED) ? " opened" : ""); | |
314 | ||
315 | AuTraceErr(err); | |
316 | return err; | |
317 | } | |
318 | ||
319 | static int au_aopen_no_open(struct file *file, struct dentry *dentry) | |
320 | { | |
321 | int err; | |
322 | ||
323 | AuLabel(here); | |
324 | dget(dentry); | |
325 | err = finish_no_open(file, dentry); | |
326 | ||
327 | AuTraceErr(err); | |
328 | return err; | |
329 | } | |
330 | ||
331 | static int aufs_atomic_open(struct inode *dir, struct dentry *dentry, | |
332 | struct file *file, unsigned int open_flag, | |
333 | umode_t create_mode) | |
334 | { | |
335 | int err, did_open; | |
336 | unsigned int lkup_flags; | |
337 | aufs_bindex_t bindex; | |
338 | struct super_block *sb; | |
339 | struct dentry *parent, *d; | |
340 | struct vfsub_aopen_args args = { | |
341 | .open_flag = open_flag, | |
342 | .create_mode = create_mode | |
343 | }; | |
344 | struct aopen_node aopen_node = { | |
345 | .file = file | |
346 | }; | |
347 | ||
348 | IMustLock(dir); | |
349 | AuDbg("open_flag 0%o\n", open_flag); | |
350 | AuDbgDentry(dentry); | |
351 | ||
352 | err = 0; | |
353 | if (!au_di(dentry)) { | |
354 | lkup_flags = LOOKUP_OPEN; | |
355 | if (open_flag & O_CREAT) | |
356 | lkup_flags |= LOOKUP_CREATE; | |
357 | d = aufs_lookup(dir, dentry, lkup_flags); | |
358 | if (IS_ERR(d)) { | |
359 | err = PTR_ERR(d); | |
360 | AuTraceErr(err); | |
361 | goto out; | |
362 | } else if (d) { | |
363 | /* | |
364 | * obsoleted dentry found. | |
365 | * another error will be returned later. | |
366 | */ | |
367 | d_drop(d); | |
368 | AuDbgDentry(d); | |
369 | dput(d); | |
370 | } | |
371 | AuDbgDentry(dentry); | |
372 | } | |
373 | ||
374 | if (d_is_positive(dentry) | |
375 | || d_unhashed(dentry) | |
376 | || d_unlinked(dentry) | |
377 | || !(open_flag & O_CREAT)) { | |
378 | err = au_aopen_no_open(file, dentry); | |
379 | goto out; /* success */ | |
380 | } | |
381 | ||
382 | err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN); | |
383 | if (unlikely(err)) | |
384 | goto out; | |
385 | ||
386 | sb = dentry->d_sb; | |
387 | parent = dentry->d_parent; /* dir is locked */ | |
388 | di_write_lock_parent(parent); | |
389 | err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG); | |
390 | if (unlikely(err < 0)) | |
391 | goto out_parent; | |
392 | ||
393 | AuDbgDentry(dentry); | |
394 | if (d_is_positive(dentry)) { | |
395 | err = au_aopen_no_open(file, dentry); | |
396 | goto out_parent; /* success */ | |
397 | } | |
398 | ||
399 | args.file = alloc_empty_file(file->f_flags, current_cred()); | |
400 | err = PTR_ERR(args.file); | |
401 | if (IS_ERR(args.file)) | |
402 | goto out_parent; | |
403 | ||
404 | bindex = au_dbtop(dentry); | |
405 | err = au_aopen_or_create(dir, dentry, &args); | |
406 | AuTraceErr(err); | |
407 | AuDbgFile(args.file); | |
408 | file->f_mode = args.file->f_mode & ~FMODE_OPENED; | |
409 | did_open = !!(args.file->f_mode & FMODE_OPENED); | |
410 | if (!did_open) { | |
411 | fput(args.file); | |
412 | args.file = NULL; | |
413 | } | |
414 | di_write_unlock(parent); | |
415 | di_write_unlock(dentry); | |
416 | if (unlikely(err < 0)) { | |
417 | if (args.file) | |
418 | fput(args.file); | |
419 | goto out_sb; | |
420 | } | |
421 | ||
422 | if (!did_open) | |
423 | err = au_aopen_no_open(file, dentry); | |
424 | else { | |
425 | aopen_node.h_file = args.file; | |
426 | err = au_aopen_do_open(file, dentry, &aopen_node); | |
427 | } | |
428 | if (unlikely(err < 0)) { | |
429 | if (args.file) | |
430 | fput(args.file); | |
431 | if (did_open) | |
432 | au_lcnt_dec(&args.br->br_nfiles); | |
433 | } | |
434 | goto out_sb; /* success */ | |
435 | ||
436 | out_parent: | |
437 | di_write_unlock(parent); | |
438 | di_write_unlock(dentry); | |
439 | out_sb: | |
440 | si_read_unlock(sb); | |
441 | out: | |
442 | AuTraceErr(err); | |
443 | AuDbgFile(file); | |
444 | return err; | |
445 | } | |
446 | ||
447 | ||
448 | /* ---------------------------------------------------------------------- */ | |
449 | ||
450 | static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent, | |
451 | const unsigned char add_entry, aufs_bindex_t bcpup, | |
452 | aufs_bindex_t btop) | |
453 | { | |
454 | int err; | |
455 | struct dentry *h_parent; | |
456 | struct inode *h_dir; | |
457 | ||
458 | if (add_entry) | |
459 | IMustLock(d_inode(parent)); | |
460 | else | |
461 | di_write_lock_parent(parent); | |
462 | ||
463 | err = 0; | |
464 | if (!au_h_dptr(parent, bcpup)) { | |
465 | if (btop > bcpup) | |
466 | err = au_cpup_dirs(dentry, bcpup); | |
467 | else if (btop < bcpup) | |
468 | err = au_cpdown_dirs(dentry, bcpup); | |
469 | else | |
470 | BUG(); | |
471 | } | |
472 | if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) { | |
473 | h_parent = au_h_dptr(parent, bcpup); | |
474 | h_dir = d_inode(h_parent); | |
475 | inode_lock_shared_nested(h_dir, AuLsc_I_PARENT); | |
476 | err = au_lkup_neg(dentry, bcpup, /*wh*/0); | |
477 | /* todo: no unlock here */ | |
478 | inode_unlock_shared(h_dir); | |
479 | ||
480 | AuDbg("bcpup %d\n", bcpup); | |
481 | if (!err) { | |
482 | if (d_really_is_negative(dentry)) | |
483 | au_set_h_dptr(dentry, btop, NULL); | |
484 | au_update_dbrange(dentry, /*do_put_zero*/0); | |
485 | } | |
486 | } | |
487 | ||
488 | if (!add_entry) | |
489 | di_write_unlock(parent); | |
490 | if (!err) | |
491 | err = bcpup; /* success */ | |
492 | ||
493 | AuTraceErr(err); | |
494 | return err; | |
495 | } | |
496 | ||
497 | /* | |
498 | * decide the branch and the parent dir where we will create a new entry. | |
499 | * returns new bindex or an error. | |
500 | * copyup the parent dir if needed. | |
501 | */ | |
502 | int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry, | |
503 | struct au_wr_dir_args *args) | |
504 | { | |
505 | int err; | |
506 | unsigned int flags; | |
507 | aufs_bindex_t bcpup, btop, src_btop; | |
508 | const unsigned char add_entry | |
509 | = au_ftest_wrdir(args->flags, ADD_ENTRY) | |
510 | | au_ftest_wrdir(args->flags, TMPFILE); | |
511 | struct super_block *sb; | |
512 | struct dentry *parent; | |
513 | struct au_sbinfo *sbinfo; | |
514 | ||
515 | sb = dentry->d_sb; | |
516 | sbinfo = au_sbi(sb); | |
517 | parent = dget_parent(dentry); | |
518 | btop = au_dbtop(dentry); | |
519 | bcpup = btop; | |
520 | if (args->force_btgt < 0) { | |
521 | if (src_dentry) { | |
522 | src_btop = au_dbtop(src_dentry); | |
523 | if (src_btop < btop) | |
524 | bcpup = src_btop; | |
525 | } else if (add_entry) { | |
526 | flags = 0; | |
527 | if (au_ftest_wrdir(args->flags, ISDIR)) | |
528 | au_fset_wbr(flags, DIR); | |
529 | err = AuWbrCreate(sbinfo, dentry, flags); | |
530 | bcpup = err; | |
531 | } | |
532 | ||
533 | if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) { | |
534 | if (add_entry) | |
535 | err = AuWbrCopyup(sbinfo, dentry); | |
536 | else { | |
537 | if (!IS_ROOT(dentry)) { | |
538 | di_read_lock_parent(parent, !AuLock_IR); | |
539 | err = AuWbrCopyup(sbinfo, dentry); | |
540 | di_read_unlock(parent, !AuLock_IR); | |
541 | } else | |
542 | err = AuWbrCopyup(sbinfo, dentry); | |
543 | } | |
544 | bcpup = err; | |
545 | if (unlikely(err < 0)) | |
546 | goto out; | |
547 | } | |
548 | } else { | |
549 | bcpup = args->force_btgt; | |
550 | AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry))); | |
551 | } | |
552 | ||
553 | AuDbg("btop %d, bcpup %d\n", btop, bcpup); | |
554 | err = bcpup; | |
555 | if (bcpup == btop) | |
556 | goto out; /* success */ | |
557 | ||
558 | /* copyup the new parent into the branch we process */ | |
559 | err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop); | |
560 | if (err >= 0) { | |
561 | if (d_really_is_negative(dentry)) { | |
562 | au_set_h_dptr(dentry, btop, NULL); | |
563 | au_set_dbtop(dentry, bcpup); | |
564 | au_set_dbbot(dentry, bcpup); | |
565 | } | |
566 | AuDebugOn(add_entry | |
567 | && !au_ftest_wrdir(args->flags, TMPFILE) | |
568 | && !au_h_dptr(dentry, bcpup)); | |
569 | } | |
570 | ||
571 | out: | |
572 | dput(parent); | |
573 | return err; | |
574 | } | |
575 | ||
576 | /* ---------------------------------------------------------------------- */ | |
577 | ||
578 | void au_pin_hdir_unlock(struct au_pin *p) | |
579 | { | |
580 | if (p->hdir) | |
581 | au_hn_inode_unlock(p->hdir); | |
582 | } | |
583 | ||
584 | int au_pin_hdir_lock(struct au_pin *p) | |
585 | { | |
586 | int err; | |
587 | ||
588 | err = 0; | |
589 | if (!p->hdir) | |
590 | goto out; | |
591 | ||
592 | /* even if an error happens later, keep this lock */ | |
593 | au_hn_inode_lock_nested(p->hdir, p->lsc_hi); | |
594 | ||
595 | err = -EBUSY; | |
596 | if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent))) | |
597 | goto out; | |
598 | ||
599 | err = 0; | |
600 | if (p->h_dentry) | |
601 | err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode, | |
602 | p->h_parent, p->br); | |
603 | ||
604 | out: | |
605 | return err; | |
606 | } | |
607 | ||
608 | int au_pin_hdir_relock(struct au_pin *p) | |
609 | { | |
610 | int err, i; | |
611 | struct inode *h_i; | |
612 | struct dentry *h_d[] = { | |
613 | p->h_dentry, | |
614 | p->h_parent | |
615 | }; | |
616 | ||
617 | err = au_pin_hdir_lock(p); | |
618 | if (unlikely(err)) | |
619 | goto out; | |
620 | ||
621 | for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) { | |
622 | if (!h_d[i]) | |
623 | continue; | |
624 | if (d_is_positive(h_d[i])) { | |
625 | h_i = d_inode(h_d[i]); | |
626 | err = !h_i->i_nlink; | |
627 | } | |
628 | } | |
629 | ||
630 | out: | |
631 | return err; | |
632 | } | |
633 | ||
634 | static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task) | |
635 | { | |
0ef682b1 | 636 | atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task); |
a3a49a17 SF |
637 | } |
638 | ||
639 | void au_pin_hdir_acquire_nest(struct au_pin *p) | |
640 | { | |
641 | if (p->hdir) { | |
642 | rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map, | |
643 | p->lsc_hi, 0, NULL, _RET_IP_); | |
644 | au_pin_hdir_set_owner(p, current); | |
645 | } | |
646 | } | |
647 | ||
648 | void au_pin_hdir_release(struct au_pin *p) | |
649 | { | |
650 | if (p->hdir) { | |
651 | au_pin_hdir_set_owner(p, p->task); | |
652 | rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_); | |
653 | } | |
654 | } | |
655 | ||
656 | struct dentry *au_pinned_h_parent(struct au_pin *pin) | |
657 | { | |
658 | if (pin && pin->parent) | |
659 | return au_h_dptr(pin->parent, pin->bindex); | |
660 | return NULL; | |
661 | } | |
662 | ||
663 | void au_unpin(struct au_pin *p) | |
664 | { | |
665 | if (p->hdir) | |
666 | au_pin_hdir_unlock(p); | |
667 | if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE)) | |
668 | vfsub_mnt_drop_write(p->h_mnt); | |
669 | if (!p->hdir) | |
670 | return; | |
671 | ||
672 | if (!au_ftest_pin(p->flags, DI_LOCKED)) | |
673 | di_read_unlock(p->parent, AuLock_IR); | |
674 | iput(p->hdir->hi_inode); | |
675 | dput(p->parent); | |
676 | p->parent = NULL; | |
677 | p->hdir = NULL; | |
678 | p->h_mnt = NULL; | |
679 | /* do not clear p->task */ | |
680 | } | |
681 | ||
682 | int au_do_pin(struct au_pin *p) | |
683 | { | |
684 | int err; | |
685 | struct super_block *sb; | |
686 | struct inode *h_dir; | |
687 | ||
688 | err = 0; | |
689 | sb = p->dentry->d_sb; | |
690 | p->br = au_sbr(sb, p->bindex); | |
691 | if (IS_ROOT(p->dentry)) { | |
692 | if (au_ftest_pin(p->flags, MNT_WRITE)) { | |
693 | p->h_mnt = au_br_mnt(p->br); | |
694 | err = vfsub_mnt_want_write(p->h_mnt); | |
695 | if (unlikely(err)) { | |
696 | au_fclr_pin(p->flags, MNT_WRITE); | |
697 | goto out_err; | |
698 | } | |
699 | } | |
700 | goto out; | |
701 | } | |
702 | ||
703 | p->h_dentry = NULL; | |
704 | if (p->bindex <= au_dbbot(p->dentry)) | |
705 | p->h_dentry = au_h_dptr(p->dentry, p->bindex); | |
706 | ||
707 | p->parent = dget_parent(p->dentry); | |
708 | if (!au_ftest_pin(p->flags, DI_LOCKED)) | |
709 | di_read_lock(p->parent, AuLock_IR, p->lsc_di); | |
710 | ||
711 | h_dir = NULL; | |
712 | p->h_parent = au_h_dptr(p->parent, p->bindex); | |
713 | p->hdir = au_hi(d_inode(p->parent), p->bindex); | |
714 | if (p->hdir) | |
715 | h_dir = p->hdir->hi_inode; | |
716 | ||
717 | /* | |
718 | * udba case, or | |
719 | * if DI_LOCKED is not set, then p->parent may be different | |
720 | * and h_parent can be NULL. | |
721 | */ | |
722 | if (unlikely(!p->hdir || !h_dir || !p->h_parent)) { | |
723 | err = -EBUSY; | |
724 | if (!au_ftest_pin(p->flags, DI_LOCKED)) | |
725 | di_read_unlock(p->parent, AuLock_IR); | |
726 | dput(p->parent); | |
727 | p->parent = NULL; | |
728 | goto out_err; | |
729 | } | |
730 | ||
731 | if (au_ftest_pin(p->flags, MNT_WRITE)) { | |
732 | p->h_mnt = au_br_mnt(p->br); | |
733 | err = vfsub_mnt_want_write(p->h_mnt); | |
734 | if (unlikely(err)) { | |
735 | au_fclr_pin(p->flags, MNT_WRITE); | |
736 | if (!au_ftest_pin(p->flags, DI_LOCKED)) | |
737 | di_read_unlock(p->parent, AuLock_IR); | |
738 | dput(p->parent); | |
739 | p->parent = NULL; | |
740 | goto out_err; | |
741 | } | |
742 | } | |
743 | ||
744 | au_igrab(h_dir); | |
745 | err = au_pin_hdir_lock(p); | |
746 | if (!err) | |
747 | goto out; /* success */ | |
748 | ||
749 | au_unpin(p); | |
750 | ||
751 | out_err: | |
752 | pr_err("err %d\n", err); | |
753 | err = au_busy_or_stale(); | |
754 | out: | |
755 | return err; | |
756 | } | |
757 | ||
758 | void au_pin_init(struct au_pin *p, struct dentry *dentry, | |
759 | aufs_bindex_t bindex, int lsc_di, int lsc_hi, | |
760 | unsigned int udba, unsigned char flags) | |
761 | { | |
762 | p->dentry = dentry; | |
763 | p->udba = udba; | |
764 | p->lsc_di = lsc_di; | |
765 | p->lsc_hi = lsc_hi; | |
766 | p->flags = flags; | |
767 | p->bindex = bindex; | |
768 | ||
769 | p->parent = NULL; | |
770 | p->hdir = NULL; | |
771 | p->h_mnt = NULL; | |
772 | ||
773 | p->h_dentry = NULL; | |
774 | p->h_parent = NULL; | |
775 | p->br = NULL; | |
776 | p->task = current; | |
777 | } | |
778 | ||
779 | int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex, | |
780 | unsigned int udba, unsigned char flags) | |
781 | { | |
782 | au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2, | |
783 | udba, flags); | |
784 | return au_do_pin(pin); | |
785 | } | |
786 | ||
787 | /* ---------------------------------------------------------------------- */ | |
788 | ||
789 | /* | |
790 | * ->setattr() and ->getattr() are called in various cases. | |
791 | * chmod, stat: dentry is revalidated. | |
792 | * fchmod, fstat: file and dentry are not revalidated, additionally they may be | |
793 | * unhashed. | |
794 | * for ->setattr(), ia->ia_file is passed from ftruncate only. | |
795 | */ | |
796 | /* todo: consolidate with do_refresh() and simple_reval_dpath() */ | |
797 | int au_reval_for_attr(struct dentry *dentry, unsigned int sigen) | |
798 | { | |
799 | int err; | |
800 | struct dentry *parent; | |
801 | ||
802 | err = 0; | |
803 | if (au_digen_test(dentry, sigen)) { | |
804 | parent = dget_parent(dentry); | |
805 | di_read_lock_parent(parent, AuLock_IR); | |
806 | err = au_refresh_dentry(dentry, parent); | |
807 | di_read_unlock(parent, AuLock_IR); | |
808 | dput(parent); | |
809 | } | |
810 | ||
811 | AuTraceErr(err); | |
812 | return err; | |
813 | } | |
814 | ||
815 | int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia, | |
816 | struct au_icpup_args *a) | |
817 | { | |
818 | int err; | |
819 | loff_t sz; | |
820 | aufs_bindex_t btop, ibtop; | |
821 | struct dentry *hi_wh, *parent; | |
822 | struct inode *inode; | |
823 | struct au_wr_dir_args wr_dir_args = { | |
824 | .force_btgt = -1, | |
825 | .flags = 0 | |
826 | }; | |
827 | ||
828 | if (d_is_dir(dentry)) | |
829 | au_fset_wrdir(wr_dir_args.flags, ISDIR); | |
830 | /* plink or hi_wh() case */ | |
831 | btop = au_dbtop(dentry); | |
832 | inode = d_inode(dentry); | |
833 | ibtop = au_ibtop(inode); | |
834 | if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode)) | |
835 | wr_dir_args.force_btgt = ibtop; | |
836 | err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args); | |
837 | if (unlikely(err < 0)) | |
838 | goto out; | |
839 | a->btgt = err; | |
840 | if (err != btop) | |
841 | au_fset_icpup(a->flags, DID_CPUP); | |
842 | ||
843 | err = 0; | |
844 | a->pin_flags = AuPin_MNT_WRITE; | |
845 | parent = NULL; | |
846 | if (!IS_ROOT(dentry)) { | |
847 | au_fset_pin(a->pin_flags, DI_LOCKED); | |
848 | parent = dget_parent(dentry); | |
849 | di_write_lock_parent(parent); | |
850 | } | |
851 | ||
852 | err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags); | |
853 | if (unlikely(err)) | |
854 | goto out_parent; | |
855 | ||
856 | sz = -1; | |
857 | a->h_path.dentry = au_h_dptr(dentry, btop); | |
858 | a->h_inode = d_inode(a->h_path.dentry); | |
859 | if (ia && (ia->ia_valid & ATTR_SIZE)) { | |
860 | inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD); | |
861 | if (ia->ia_size < i_size_read(a->h_inode)) | |
862 | sz = ia->ia_size; | |
863 | inode_unlock_shared(a->h_inode); | |
864 | } | |
865 | ||
866 | hi_wh = NULL; | |
867 | if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) { | |
868 | hi_wh = au_hi_wh(inode, a->btgt); | |
869 | if (!hi_wh) { | |
870 | struct au_cp_generic cpg = { | |
871 | .dentry = dentry, | |
872 | .bdst = a->btgt, | |
873 | .bsrc = -1, | |
874 | .len = sz, | |
875 | .pin = &a->pin | |
876 | }; | |
877 | err = au_sio_cpup_wh(&cpg, /*file*/NULL); | |
878 | if (unlikely(err)) | |
879 | goto out_unlock; | |
880 | hi_wh = au_hi_wh(inode, a->btgt); | |
881 | /* todo: revalidate hi_wh? */ | |
882 | } | |
883 | } | |
884 | ||
885 | if (parent) { | |
886 | au_pin_set_parent_lflag(&a->pin, /*lflag*/0); | |
887 | di_downgrade_lock(parent, AuLock_IR); | |
888 | dput(parent); | |
889 | parent = NULL; | |
890 | } | |
891 | if (!au_ftest_icpup(a->flags, DID_CPUP)) | |
892 | goto out; /* success */ | |
893 | ||
894 | if (!d_unhashed(dentry)) { | |
895 | struct au_cp_generic cpg = { | |
896 | .dentry = dentry, | |
897 | .bdst = a->btgt, | |
898 | .bsrc = btop, | |
899 | .len = sz, | |
900 | .pin = &a->pin, | |
901 | .flags = AuCpup_DTIME | AuCpup_HOPEN | |
902 | }; | |
903 | err = au_sio_cpup_simple(&cpg); | |
904 | if (!err) | |
905 | a->h_path.dentry = au_h_dptr(dentry, a->btgt); | |
906 | } else if (!hi_wh) | |
907 | a->h_path.dentry = au_h_dptr(dentry, a->btgt); | |
908 | else | |
909 | a->h_path.dentry = hi_wh; /* do not dget here */ | |
910 | ||
911 | out_unlock: | |
912 | a->h_inode = d_inode(a->h_path.dentry); | |
913 | if (!err) | |
914 | goto out; /* success */ | |
915 | au_unpin(&a->pin); | |
916 | out_parent: | |
917 | if (parent) { | |
918 | di_write_unlock(parent); | |
919 | dput(parent); | |
920 | } | |
921 | out: | |
922 | if (!err) | |
923 | inode_lock_nested(a->h_inode, AuLsc_I_CHILD); | |
924 | return err; | |
925 | } | |
926 | ||
927 | static int aufs_setattr(struct dentry *dentry, struct iattr *ia) | |
928 | { | |
929 | int err; | |
930 | struct inode *inode, *delegated; | |
931 | struct super_block *sb; | |
932 | struct file *file; | |
933 | struct au_icpup_args *a; | |
934 | ||
935 | inode = d_inode(dentry); | |
936 | IMustLock(inode); | |
937 | ||
938 | err = setattr_prepare(dentry, ia); | |
939 | if (unlikely(err)) | |
940 | goto out; | |
941 | ||
942 | err = -ENOMEM; | |
943 | a = kzalloc(sizeof(*a), GFP_NOFS); | |
944 | if (unlikely(!a)) | |
945 | goto out; | |
946 | ||
947 | if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) | |
948 | ia->ia_valid &= ~ATTR_MODE; | |
949 | ||
950 | file = NULL; | |
951 | sb = dentry->d_sb; | |
952 | err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM); | |
953 | if (unlikely(err)) | |
954 | goto out_kfree; | |
955 | ||
956 | if (ia->ia_valid & ATTR_FILE) { | |
957 | /* currently ftruncate(2) only */ | |
958 | AuDebugOn(!d_is_reg(dentry)); | |
959 | file = ia->ia_file; | |
960 | err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, | |
961 | /*fi_lsc*/0); | |
962 | if (unlikely(err)) | |
963 | goto out_si; | |
964 | ia->ia_file = au_hf_top(file); | |
965 | a->udba = AuOpt_UDBA_NONE; | |
966 | } else { | |
967 | /* fchmod() doesn't pass ia_file */ | |
968 | a->udba = au_opt_udba(sb); | |
969 | di_write_lock_child(dentry); | |
970 | /* no d_unlinked(), to set UDBA_NONE for root */ | |
971 | if (d_unhashed(dentry)) | |
972 | a->udba = AuOpt_UDBA_NONE; | |
973 | if (a->udba != AuOpt_UDBA_NONE) { | |
974 | AuDebugOn(IS_ROOT(dentry)); | |
975 | err = au_reval_for_attr(dentry, au_sigen(sb)); | |
976 | if (unlikely(err)) | |
977 | goto out_dentry; | |
978 | } | |
979 | } | |
980 | ||
981 | err = au_pin_and_icpup(dentry, ia, a); | |
982 | if (unlikely(err < 0)) | |
983 | goto out_dentry; | |
984 | if (au_ftest_icpup(a->flags, DID_CPUP)) { | |
985 | ia->ia_file = NULL; | |
986 | ia->ia_valid &= ~ATTR_FILE; | |
987 | } | |
988 | ||
989 | a->h_path.mnt = au_sbr_mnt(sb, a->btgt); | |
990 | if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME)) | |
991 | == (ATTR_MODE | ATTR_CTIME)) { | |
992 | err = security_path_chmod(&a->h_path, ia->ia_mode); | |
993 | if (unlikely(err)) | |
994 | goto out_unlock; | |
995 | } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID)) | |
996 | && (ia->ia_valid & ATTR_CTIME)) { | |
997 | err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid); | |
998 | if (unlikely(err)) | |
999 | goto out_unlock; | |
1000 | } | |
1001 | ||
1002 | if (ia->ia_valid & ATTR_SIZE) { | |
1003 | struct file *f; | |
1004 | ||
1005 | if (ia->ia_size < i_size_read(inode)) | |
1006 | /* unmap only */ | |
1007 | truncate_setsize(inode, ia->ia_size); | |
1008 | ||
1009 | f = NULL; | |
1010 | if (ia->ia_valid & ATTR_FILE) | |
1011 | f = ia->ia_file; | |
1012 | inode_unlock(a->h_inode); | |
1013 | err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f); | |
1014 | inode_lock_nested(a->h_inode, AuLsc_I_CHILD); | |
1015 | } else { | |
1016 | delegated = NULL; | |
1017 | while (1) { | |
1018 | err = vfsub_notify_change(&a->h_path, ia, &delegated); | |
1019 | if (delegated) { | |
1020 | err = break_deleg_wait(&delegated); | |
1021 | if (!err) | |
1022 | continue; | |
1023 | } | |
1024 | break; | |
1025 | } | |
1026 | } | |
1027 | /* | |
1028 | * regardless aufs 'acl' option setting. | |
1029 | * why don't all acl-aware fs call this func from their ->setattr()? | |
1030 | */ | |
1031 | if (!err && (ia->ia_valid & ATTR_MODE)) | |
1032 | err = vfsub_acl_chmod(a->h_inode, ia->ia_mode); | |
1033 | if (!err) | |
1034 | au_cpup_attr_changeable(inode); | |
1035 | ||
1036 | out_unlock: | |
1037 | inode_unlock(a->h_inode); | |
1038 | au_unpin(&a->pin); | |
1039 | if (unlikely(err)) | |
1040 | au_update_dbtop(dentry); | |
1041 | out_dentry: | |
1042 | di_write_unlock(dentry); | |
1043 | if (file) { | |
1044 | fi_write_unlock(file); | |
1045 | ia->ia_file = file; | |
1046 | ia->ia_valid |= ATTR_FILE; | |
1047 | } | |
1048 | out_si: | |
1049 | si_read_unlock(sb); | |
1050 | out_kfree: | |
1051 | au_kfree_rcu(a); | |
1052 | out: | |
1053 | AuTraceErr(err); | |
1054 | return err; | |
1055 | } | |
1056 | ||
1057 | #if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL) | |
1058 | static int au_h_path_to_set_attr(struct dentry *dentry, | |
1059 | struct au_icpup_args *a, struct path *h_path) | |
1060 | { | |
1061 | int err; | |
1062 | struct super_block *sb; | |
1063 | ||
1064 | sb = dentry->d_sb; | |
1065 | a->udba = au_opt_udba(sb); | |
1066 | /* no d_unlinked(), to set UDBA_NONE for root */ | |
1067 | if (d_unhashed(dentry)) | |
1068 | a->udba = AuOpt_UDBA_NONE; | |
1069 | if (a->udba != AuOpt_UDBA_NONE) { | |
1070 | AuDebugOn(IS_ROOT(dentry)); | |
1071 | err = au_reval_for_attr(dentry, au_sigen(sb)); | |
1072 | if (unlikely(err)) | |
1073 | goto out; | |
1074 | } | |
1075 | err = au_pin_and_icpup(dentry, /*ia*/NULL, a); | |
1076 | if (unlikely(err < 0)) | |
1077 | goto out; | |
1078 | ||
1079 | h_path->dentry = a->h_path.dentry; | |
1080 | h_path->mnt = au_sbr_mnt(sb, a->btgt); | |
1081 | ||
1082 | out: | |
1083 | return err; | |
1084 | } | |
1085 | ||
1086 | ssize_t au_sxattr(struct dentry *dentry, struct inode *inode, | |
1087 | struct au_sxattr *arg) | |
1088 | { | |
1089 | int err; | |
1090 | struct path h_path; | |
1091 | struct super_block *sb; | |
1092 | struct au_icpup_args *a; | |
1093 | struct inode *h_inode; | |
1094 | ||
1095 | IMustLock(inode); | |
1096 | ||
1097 | err = -ENOMEM; | |
1098 | a = kzalloc(sizeof(*a), GFP_NOFS); | |
1099 | if (unlikely(!a)) | |
1100 | goto out; | |
1101 | ||
1102 | sb = dentry->d_sb; | |
1103 | err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM); | |
1104 | if (unlikely(err)) | |
1105 | goto out_kfree; | |
1106 | ||
1107 | h_path.dentry = NULL; /* silence gcc */ | |
1108 | di_write_lock_child(dentry); | |
1109 | err = au_h_path_to_set_attr(dentry, a, &h_path); | |
1110 | if (unlikely(err)) | |
1111 | goto out_di; | |
1112 | ||
1113 | inode_unlock(a->h_inode); | |
1114 | switch (arg->type) { | |
1115 | case AU_XATTR_SET: | |
1116 | AuDebugOn(d_is_negative(h_path.dentry)); | |
1117 | err = vfsub_setxattr(h_path.dentry, | |
1118 | arg->u.set.name, arg->u.set.value, | |
1119 | arg->u.set.size, arg->u.set.flags); | |
1120 | break; | |
1121 | case AU_ACL_SET: | |
1122 | err = -EOPNOTSUPP; | |
1123 | h_inode = d_inode(h_path.dentry); | |
1124 | if (h_inode->i_op->set_acl) | |
1125 | /* this will call posix_acl_update_mode */ | |
1126 | err = h_inode->i_op->set_acl(h_inode, | |
1127 | arg->u.acl_set.acl, | |
1128 | arg->u.acl_set.type); | |
1129 | break; | |
1130 | } | |
1131 | if (!err) | |
1132 | au_cpup_attr_timesizes(inode); | |
1133 | ||
1134 | au_unpin(&a->pin); | |
1135 | if (unlikely(err)) | |
1136 | au_update_dbtop(dentry); | |
1137 | ||
1138 | out_di: | |
1139 | di_write_unlock(dentry); | |
1140 | si_read_unlock(sb); | |
1141 | out_kfree: | |
1142 | au_kfree_rcu(a); | |
1143 | out: | |
1144 | AuTraceErr(err); | |
1145 | return err; | |
1146 | } | |
1147 | #endif | |
1148 | ||
1149 | static void au_refresh_iattr(struct inode *inode, struct kstat *st, | |
1150 | unsigned int nlink) | |
1151 | { | |
1152 | unsigned int n; | |
1153 | ||
1154 | inode->i_mode = st->mode; | |
1155 | /* don't i_[ug]id_write() here */ | |
1156 | inode->i_uid = st->uid; | |
1157 | inode->i_gid = st->gid; | |
1158 | inode->i_atime = st->atime; | |
1159 | inode->i_mtime = st->mtime; | |
1160 | inode->i_ctime = st->ctime; | |
1161 | ||
1162 | au_cpup_attr_nlink(inode, /*force*/0); | |
1163 | if (S_ISDIR(inode->i_mode)) { | |
1164 | n = inode->i_nlink; | |
1165 | n -= nlink; | |
1166 | n += st->nlink; | |
1167 | smp_mb(); /* for i_nlink */ | |
1168 | /* 0 can happen */ | |
1169 | set_nlink(inode, n); | |
1170 | } | |
1171 | ||
1172 | spin_lock(&inode->i_lock); | |
1173 | inode->i_blocks = st->blocks; | |
1174 | i_size_write(inode, st->size); | |
1175 | spin_unlock(&inode->i_lock); | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * common routine for aufs_getattr() and au_getxattr(). | |
1180 | * returns zero or negative (an error). | |
1181 | * @dentry will be read-locked in success. | |
1182 | */ | |
1183 | int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path, | |
1184 | int locked) | |
1185 | { | |
1186 | int err; | |
1187 | unsigned int mnt_flags, sigen; | |
1188 | unsigned char udba_none; | |
1189 | aufs_bindex_t bindex; | |
1190 | struct super_block *sb, *h_sb; | |
1191 | struct inode *inode; | |
1192 | ||
1193 | h_path->mnt = NULL; | |
1194 | h_path->dentry = NULL; | |
1195 | ||
1196 | err = 0; | |
1197 | sb = dentry->d_sb; | |
1198 | mnt_flags = au_mntflags(sb); | |
1199 | udba_none = !!au_opt_test(mnt_flags, UDBA_NONE); | |
1200 | ||
1201 | if (unlikely(locked)) | |
1202 | goto body; /* skip locking dinfo */ | |
1203 | ||
1204 | /* support fstat(2) */ | |
1205 | if (!d_unlinked(dentry) && !udba_none) { | |
1206 | sigen = au_sigen(sb); | |
1207 | err = au_digen_test(dentry, sigen); | |
1208 | if (!err) { | |
1209 | di_read_lock_child(dentry, AuLock_IR); | |
1210 | err = au_dbrange_test(dentry); | |
1211 | if (unlikely(err)) { | |
1212 | di_read_unlock(dentry, AuLock_IR); | |
1213 | goto out; | |
1214 | } | |
1215 | } else { | |
1216 | AuDebugOn(IS_ROOT(dentry)); | |
1217 | di_write_lock_child(dentry); | |
1218 | err = au_dbrange_test(dentry); | |
1219 | if (!err) | |
1220 | err = au_reval_for_attr(dentry, sigen); | |
1221 | if (!err) | |
1222 | di_downgrade_lock(dentry, AuLock_IR); | |
1223 | else { | |
1224 | di_write_unlock(dentry); | |
1225 | goto out; | |
1226 | } | |
1227 | } | |
1228 | } else | |
1229 | di_read_lock_child(dentry, AuLock_IR); | |
1230 | ||
1231 | body: | |
1232 | inode = d_inode(dentry); | |
1233 | bindex = au_ibtop(inode); | |
1234 | h_path->mnt = au_sbr_mnt(sb, bindex); | |
1235 | h_sb = h_path->mnt->mnt_sb; | |
1236 | if (!force | |
1237 | && !au_test_fs_bad_iattr(h_sb) | |
1238 | && udba_none) | |
1239 | goto out; /* success */ | |
1240 | ||
1241 | if (au_dbtop(dentry) == bindex) | |
1242 | h_path->dentry = au_h_dptr(dentry, bindex); | |
1243 | else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) { | |
1244 | h_path->dentry = au_plink_lkup(inode, bindex); | |
1245 | if (IS_ERR(h_path->dentry)) | |
1246 | /* pretending success */ | |
1247 | h_path->dentry = NULL; | |
1248 | else | |
1249 | dput(h_path->dentry); | |
1250 | } | |
1251 | ||
1252 | out: | |
1253 | return err; | |
1254 | } | |
1255 | ||
1256 | static int aufs_getattr(const struct path *path, struct kstat *st, | |
1257 | u32 request, unsigned int query) | |
1258 | { | |
1259 | int err; | |
1260 | unsigned char positive; | |
1261 | struct path h_path; | |
1262 | struct dentry *dentry; | |
1263 | struct inode *inode; | |
1264 | struct super_block *sb; | |
1265 | ||
1266 | dentry = path->dentry; | |
1267 | inode = d_inode(dentry); | |
1268 | sb = dentry->d_sb; | |
1269 | err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM); | |
1270 | if (unlikely(err)) | |
1271 | goto out; | |
1272 | err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0); | |
1273 | if (unlikely(err)) | |
1274 | goto out_si; | |
1275 | if (unlikely(!h_path.dentry)) | |
1276 | /* illegally overlapped or something */ | |
1277 | goto out_fill; /* pretending success */ | |
1278 | ||
1279 | positive = d_is_positive(h_path.dentry); | |
1280 | if (positive) | |
1281 | /* no vfsub version */ | |
1282 | err = vfs_getattr(&h_path, st, request, query); | |
1283 | if (!err) { | |
1284 | if (positive) | |
1285 | au_refresh_iattr(inode, st, | |
1286 | d_inode(h_path.dentry)->i_nlink); | |
1287 | goto out_fill; /* success */ | |
1288 | } | |
1289 | AuTraceErr(err); | |
1290 | goto out_di; | |
1291 | ||
1292 | out_fill: | |
1293 | generic_fillattr(inode, st); | |
1294 | out_di: | |
1295 | di_read_unlock(dentry, AuLock_IR); | |
1296 | out_si: | |
1297 | si_read_unlock(sb); | |
1298 | out: | |
1299 | AuTraceErr(err); | |
1300 | return err; | |
1301 | } | |
1302 | ||
1303 | /* ---------------------------------------------------------------------- */ | |
1304 | ||
1305 | static const char *aufs_get_link(struct dentry *dentry, struct inode *inode, | |
1306 | struct delayed_call *done) | |
1307 | { | |
1308 | const char *ret; | |
1309 | struct dentry *h_dentry; | |
1310 | struct inode *h_inode; | |
1311 | int err; | |
1312 | aufs_bindex_t bindex; | |
1313 | ||
1314 | ret = NULL; /* suppress a warning */ | |
1315 | err = -ECHILD; | |
1316 | if (!dentry) | |
1317 | goto out; | |
1318 | ||
1319 | err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN); | |
1320 | if (unlikely(err)) | |
1321 | goto out; | |
1322 | ||
1323 | err = au_d_hashed_positive(dentry); | |
1324 | if (unlikely(err)) | |
1325 | goto out_unlock; | |
1326 | ||
1327 | err = -EINVAL; | |
1328 | inode = d_inode(dentry); | |
1329 | bindex = au_ibtop(inode); | |
1330 | h_inode = au_h_iptr(inode, bindex); | |
1331 | if (unlikely(!h_inode->i_op->get_link)) | |
1332 | goto out_unlock; | |
1333 | ||
1334 | err = -EBUSY; | |
1335 | h_dentry = NULL; | |
1336 | if (au_dbtop(dentry) <= bindex) { | |
1337 | h_dentry = au_h_dptr(dentry, bindex); | |
1338 | if (h_dentry) | |
1339 | dget(h_dentry); | |
1340 | } | |
1341 | if (!h_dentry) { | |
1342 | h_dentry = d_find_any_alias(h_inode); | |
1343 | if (IS_ERR(h_dentry)) { | |
1344 | err = PTR_ERR(h_dentry); | |
1345 | goto out_unlock; | |
1346 | } | |
1347 | } | |
1348 | if (unlikely(!h_dentry)) | |
1349 | goto out_unlock; | |
1350 | ||
1351 | err = 0; | |
1352 | AuDbg("%ps\n", h_inode->i_op->get_link); | |
1353 | AuDbgDentry(h_dentry); | |
1354 | ret = vfs_get_link(h_dentry, done); | |
1355 | dput(h_dentry); | |
1356 | if (IS_ERR(ret)) | |
1357 | err = PTR_ERR(ret); | |
1358 | ||
1359 | out_unlock: | |
1360 | aufs_read_unlock(dentry, AuLock_IR); | |
1361 | out: | |
1362 | if (unlikely(err)) | |
1363 | ret = ERR_PTR(err); | |
1364 | AuTraceErrPtr(ret); | |
1365 | return ret; | |
1366 | } | |
1367 | ||
1368 | /* ---------------------------------------------------------------------- */ | |
1369 | ||
1370 | static int au_is_special(struct inode *inode) | |
1371 | { | |
1372 | return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK)); | |
1373 | } | |
1374 | ||
1375 | static int aufs_update_time(struct inode *inode, struct timespec64 *ts, | |
1376 | int flags) | |
1377 | { | |
1378 | int err; | |
1379 | aufs_bindex_t bindex; | |
1380 | struct super_block *sb; | |
1381 | struct inode *h_inode; | |
1382 | struct vfsmount *h_mnt; | |
1383 | ||
1384 | sb = inode->i_sb; | |
1385 | WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode), | |
1386 | "unexpected s_flags 0x%lx", sb->s_flags); | |
1387 | ||
1388 | /* mmap_sem might be acquired already, cf. aufs_mmap() */ | |
1389 | lockdep_off(); | |
1390 | si_read_lock(sb, AuLock_FLUSH); | |
1391 | ii_write_lock_child(inode); | |
1392 | ||
1393 | err = 0; | |
1394 | bindex = au_ibtop(inode); | |
1395 | h_inode = au_h_iptr(inode, bindex); | |
1396 | if (!au_test_ro(sb, bindex, inode)) { | |
1397 | h_mnt = au_sbr_mnt(sb, bindex); | |
1398 | err = vfsub_mnt_want_write(h_mnt); | |
1399 | if (!err) { | |
1400 | err = vfsub_update_time(h_inode, ts, flags); | |
1401 | vfsub_mnt_drop_write(h_mnt); | |
1402 | } | |
1403 | } else if (au_is_special(h_inode)) { | |
1404 | /* | |
1405 | * Never copy-up here. | |
1406 | * These special files may already be opened and used for | |
1407 | * communicating. If we copied it up, then the communication | |
1408 | * would be corrupted. | |
1409 | */ | |
1410 | AuWarn1("timestamps for i%lu are ignored " | |
1411 | "since it is on readonly branch (hi%lu).\n", | |
1412 | inode->i_ino, h_inode->i_ino); | |
1413 | } else if (flags & ~S_ATIME) { | |
1414 | err = -EIO; | |
1415 | AuIOErr1("unexpected flags 0x%x\n", flags); | |
1416 | AuDebugOn(1); | |
1417 | } | |
1418 | ||
1419 | if (!err) | |
1420 | au_cpup_attr_timesizes(inode); | |
1421 | ii_write_unlock(inode); | |
1422 | si_read_unlock(sb); | |
1423 | lockdep_on(); | |
1424 | ||
1425 | if (!err && (flags & S_VERSION)) | |
1426 | inode_inc_iversion(inode); | |
1427 | ||
1428 | return err; | |
1429 | } | |
1430 | ||
1431 | /* ---------------------------------------------------------------------- */ | |
1432 | ||
1433 | /* no getattr version will be set by module.c:aufs_init() */ | |
1434 | struct inode_operations aufs_iop_nogetattr[AuIop_Last], | |
1435 | aufs_iop[] = { | |
1436 | [AuIop_SYMLINK] = { | |
1437 | .permission = aufs_permission, | |
1438 | #ifdef CONFIG_FS_POSIX_ACL | |
1439 | .get_acl = aufs_get_acl, | |
1440 | .set_acl = aufs_set_acl, /* unsupport for symlink? */ | |
1441 | #endif | |
1442 | ||
1443 | .setattr = aufs_setattr, | |
1444 | .getattr = aufs_getattr, | |
1445 | ||
1446 | #ifdef CONFIG_AUFS_XATTR | |
1447 | .listxattr = aufs_listxattr, | |
1448 | #endif | |
1449 | ||
1450 | .get_link = aufs_get_link, | |
1451 | ||
1452 | /* .update_time = aufs_update_time */ | |
1453 | }, | |
1454 | [AuIop_DIR] = { | |
1455 | .create = aufs_create, | |
1456 | .lookup = aufs_lookup, | |
1457 | .link = aufs_link, | |
1458 | .unlink = aufs_unlink, | |
1459 | .symlink = aufs_symlink, | |
1460 | .mkdir = aufs_mkdir, | |
1461 | .rmdir = aufs_rmdir, | |
1462 | .mknod = aufs_mknod, | |
1463 | .rename = aufs_rename, | |
1464 | ||
1465 | .permission = aufs_permission, | |
1466 | #ifdef CONFIG_FS_POSIX_ACL | |
1467 | .get_acl = aufs_get_acl, | |
1468 | .set_acl = aufs_set_acl, | |
1469 | #endif | |
1470 | ||
1471 | .setattr = aufs_setattr, | |
1472 | .getattr = aufs_getattr, | |
1473 | ||
1474 | #ifdef CONFIG_AUFS_XATTR | |
1475 | .listxattr = aufs_listxattr, | |
1476 | #endif | |
1477 | ||
1478 | .update_time = aufs_update_time, | |
1479 | .atomic_open = aufs_atomic_open, | |
1480 | .tmpfile = aufs_tmpfile | |
1481 | }, | |
1482 | [AuIop_OTHER] = { | |
1483 | .permission = aufs_permission, | |
1484 | #ifdef CONFIG_FS_POSIX_ACL | |
1485 | .get_acl = aufs_get_acl, | |
1486 | .set_acl = aufs_set_acl, | |
1487 | #endif | |
1488 | ||
1489 | .setattr = aufs_setattr, | |
1490 | .getattr = aufs_getattr, | |
1491 | ||
1492 | #ifdef CONFIG_AUFS_XATTR | |
1493 | .listxattr = aufs_listxattr, | |
1494 | #endif | |
1495 | ||
1496 | .update_time = aufs_update_time | |
1497 | } | |
1498 | }; |