1 From 8dcf5b3ed788c0909dc1248a00a8997cdeefde1e Mon Sep 17 00:00:00 2001
2 From: Tom Briden <tom@decompile.me.uk>
3 Date: Thu, 11 Jan 2024 11:05:05 +0000
4 Subject: [PATCH] Make AUFS friendly (aufs6.x-rcN 20231106)
6 Patches applied from sfjro/aufs5-standalone
8 Documentation/ABI/testing/debugfs-aufs | 55 +
9 Documentation/ABI/testing/sysfs-aufs | 31 +
10 Documentation/filesystems/aufs/README | 409 ++++
11 .../filesystems/aufs/design/01intro.txt | 171 ++
12 .../filesystems/aufs/design/02struct.txt | 258 +++
13 .../filesystems/aufs/design/03atomic_open.txt | 85 +
14 .../filesystems/aufs/design/03lookup.txt | 113 +
15 .../filesystems/aufs/design/04branch.txt | 74 +
16 .../filesystems/aufs/design/05wbr_policy.txt | 64 +
17 .../filesystems/aufs/design/06dirren.dot | 44 +
18 .../filesystems/aufs/design/06dirren.txt | 102 +
19 .../filesystems/aufs/design/06fhsm.txt | 118 +
20 .../filesystems/aufs/design/06mmap.txt | 72 +
21 .../filesystems/aufs/design/06xattr.txt | 94 +
22 .../filesystems/aufs/design/07export.txt | 58 +
23 .../filesystems/aufs/design/08shwh.txt | 52 +
24 .../filesystems/aufs/design/10dynop.txt | 47 +
26 drivers/block/loop.c | 18 +
29 fs/aufs/Kconfig | 199 ++
30 fs/aufs/Makefile | 46 +
32 fs/aufs/branch.c | 1427 ++++++++++++
33 fs/aufs/branch.h | 375 ++++
34 fs/aufs/conf.mk | 40 +
35 fs/aufs/cpup.c | 1459 +++++++++++++
36 fs/aufs/cpup.h | 100 +
37 fs/aufs/dbgaufs.c | 526 +++++
38 fs/aufs/dbgaufs.h | 53 +
39 fs/aufs/dcsub.c | 225 ++
40 fs/aufs/dcsub.h | 137 ++
41 fs/aufs/debug.c | 448 ++++
42 fs/aufs/debug.h | 226 ++
43 fs/aufs/dentry.c | 1168 ++++++++++
44 fs/aufs/dentry.h | 270 +++
45 fs/aufs/dinfo.c | 555 +++++
46 fs/aufs/dir.c | 765 +++++++
47 fs/aufs/dir.h | 134 ++
48 fs/aufs/dirren.c | 1315 +++++++++++
49 fs/aufs/dirren.h | 140 ++
50 fs/aufs/dynop.c | 366 ++++
51 fs/aufs/dynop.h | 77 +
52 fs/aufs/export.c | 830 +++++++
53 fs/aufs/f_op.c | 771 +++++++
54 fs/aufs/fhsm.c | 426 ++++
55 fs/aufs/file.c | 865 ++++++++
56 fs/aufs/file.h | 342 +++
57 fs/aufs/finfo.c | 149 ++
58 fs/aufs/fsctx.c | 1242 +++++++++++
59 fs/aufs/fstype.h | 401 ++++
61 fs/aufs/hfsnotify.c | 290 +++
62 fs/aufs/hfsplus.c | 60 +
63 fs/aufs/hnotify.c | 715 ++++++
64 fs/aufs/i_op.c | 1516 +++++++++++++
65 fs/aufs/i_op_add.c | 972 +++++++++
66 fs/aufs/i_op_del.c | 523 +++++
67 fs/aufs/i_op_ren.c | 1260 +++++++++++
68 fs/aufs/iinfo.c | 286 +++
69 fs/aufs/inode.c | 531 +++++
70 fs/aufs/inode.h | 707 ++++++
71 fs/aufs/ioctl.c | 220 ++
72 fs/aufs/lcnt.h | 186 ++
73 fs/aufs/loop.c | 148 ++
75 fs/aufs/magic.mk | 31 +
76 fs/aufs/module.c | 273 +++
77 fs/aufs/module.h | 180 ++
78 fs/aufs/mvdown.c | 706 ++++++
79 fs/aufs/opts.c | 1032 +++++++++
80 fs/aufs/opts.h | 263 +++
81 fs/aufs/plink.c | 516 +++++
83 fs/aufs/posix_acl.c | 108 +
84 fs/aufs/procfs.c | 170 ++
85 fs/aufs/rdu.c | 384 ++++
86 fs/aufs/rwsem.h | 85 +
87 fs/aufs/sbinfo.c | 316 +++
88 fs/aufs/super.c | 871 ++++++++
89 fs/aufs/super.h | 592 +++++
90 fs/aufs/sysaufs.c | 94 +
91 fs/aufs/sysaufs.h | 102 +
92 fs/aufs/sysfs.c | 374 ++++
93 fs/aufs/sysrq.c | 149 ++
94 fs/aufs/vdir.c | 896 ++++++++
95 fs/aufs/vfsub.c | 918 ++++++++
96 fs/aufs/vfsub.h | 403 ++++
97 fs/aufs/wbr_policy.c | 830 +++++++
98 fs/aufs/whout.c | 1072 +++++++++
99 fs/aufs/whout.h | 87 +
100 fs/aufs/wkq.c | 372 ++++
102 fs/aufs/xattr.c | 360 +++
103 fs/aufs/xino.c | 1926 +++++++++++++++++
107 fs/file_table.c | 1 +
109 fs/notify/group.c | 1 +
111 fs/proc/nommu.c | 5 +-
112 fs/proc/task_mmu.c | 7 +-
113 fs/proc/task_nommu.c | 5 +-
114 fs/read_write.c | 2 +
117 include/linux/fs.h | 2 +
118 include/linux/lockdep.h | 2 +
119 include/linux/mm.h | 37 +
120 include/linux/mm_types.h | 6 +
121 include/linux/mnt_namespace.h | 3 +
122 include/linux/splice.h | 6 +
123 include/uapi/linux/aufs_type.h | 452 ++++
125 kernel/locking/lockdep.c | 4 +-
126 kernel/task_work.c | 1 +
132 security/security.c | 8 +
133 125 files changed, 38490 insertions(+), 23 deletions(-)
134 create mode 100644 Documentation/ABI/testing/debugfs-aufs
135 create mode 100644 Documentation/ABI/testing/sysfs-aufs
136 create mode 100644 Documentation/filesystems/aufs/README
137 create mode 100644 Documentation/filesystems/aufs/design/01intro.txt
138 create mode 100644 Documentation/filesystems/aufs/design/02struct.txt
139 create mode 100644 Documentation/filesystems/aufs/design/03atomic_open.txt
140 create mode 100644 Documentation/filesystems/aufs/design/03lookup.txt
141 create mode 100644 Documentation/filesystems/aufs/design/04branch.txt
142 create mode 100644 Documentation/filesystems/aufs/design/05wbr_policy.txt
143 create mode 100644 Documentation/filesystems/aufs/design/06dirren.dot
144 create mode 100644 Documentation/filesystems/aufs/design/06dirren.txt
145 create mode 100644 Documentation/filesystems/aufs/design/06fhsm.txt
146 create mode 100644 Documentation/filesystems/aufs/design/06mmap.txt
147 create mode 100644 Documentation/filesystems/aufs/design/06xattr.txt
148 create mode 100644 Documentation/filesystems/aufs/design/07export.txt
149 create mode 100644 Documentation/filesystems/aufs/design/08shwh.txt
150 create mode 100644 Documentation/filesystems/aufs/design/10dynop.txt
151 create mode 100644 fs/aufs/Kconfig
152 create mode 100644 fs/aufs/Makefile
153 create mode 100644 fs/aufs/aufs.h
154 create mode 100644 fs/aufs/branch.c
155 create mode 100644 fs/aufs/branch.h
156 create mode 100644 fs/aufs/conf.mk
157 create mode 100644 fs/aufs/cpup.c
158 create mode 100644 fs/aufs/cpup.h
159 create mode 100644 fs/aufs/dbgaufs.c
160 create mode 100644 fs/aufs/dbgaufs.h
161 create mode 100644 fs/aufs/dcsub.c
162 create mode 100644 fs/aufs/dcsub.h
163 create mode 100644 fs/aufs/debug.c
164 create mode 100644 fs/aufs/debug.h
165 create mode 100644 fs/aufs/dentry.c
166 create mode 100644 fs/aufs/dentry.h
167 create mode 100644 fs/aufs/dinfo.c
168 create mode 100644 fs/aufs/dir.c
169 create mode 100644 fs/aufs/dir.h
170 create mode 100644 fs/aufs/dirren.c
171 create mode 100644 fs/aufs/dirren.h
172 create mode 100644 fs/aufs/dynop.c
173 create mode 100644 fs/aufs/dynop.h
174 create mode 100644 fs/aufs/export.c
175 create mode 100644 fs/aufs/f_op.c
176 create mode 100644 fs/aufs/fhsm.c
177 create mode 100644 fs/aufs/file.c
178 create mode 100644 fs/aufs/file.h
179 create mode 100644 fs/aufs/finfo.c
180 create mode 100644 fs/aufs/fsctx.c
181 create mode 100644 fs/aufs/fstype.h
182 create mode 100644 fs/aufs/hbl.h
183 create mode 100644 fs/aufs/hfsnotify.c
184 create mode 100644 fs/aufs/hfsplus.c
185 create mode 100644 fs/aufs/hnotify.c
186 create mode 100644 fs/aufs/i_op.c
187 create mode 100644 fs/aufs/i_op_add.c
188 create mode 100644 fs/aufs/i_op_del.c
189 create mode 100644 fs/aufs/i_op_ren.c
190 create mode 100644 fs/aufs/iinfo.c
191 create mode 100644 fs/aufs/inode.c
192 create mode 100644 fs/aufs/inode.h
193 create mode 100644 fs/aufs/ioctl.c
194 create mode 100644 fs/aufs/lcnt.h
195 create mode 100644 fs/aufs/loop.c
196 create mode 100644 fs/aufs/loop.h
197 create mode 100644 fs/aufs/magic.mk
198 create mode 100644 fs/aufs/module.c
199 create mode 100644 fs/aufs/module.h
200 create mode 100644 fs/aufs/mvdown.c
201 create mode 100644 fs/aufs/opts.c
202 create mode 100644 fs/aufs/opts.h
203 create mode 100644 fs/aufs/plink.c
204 create mode 100644 fs/aufs/poll.c
205 create mode 100644 fs/aufs/posix_acl.c
206 create mode 100644 fs/aufs/procfs.c
207 create mode 100644 fs/aufs/rdu.c
208 create mode 100644 fs/aufs/rwsem.h
209 create mode 100644 fs/aufs/sbinfo.c
210 create mode 100644 fs/aufs/super.c
211 create mode 100644 fs/aufs/super.h
212 create mode 100644 fs/aufs/sysaufs.c
213 create mode 100644 fs/aufs/sysaufs.h
214 create mode 100644 fs/aufs/sysfs.c
215 create mode 100644 fs/aufs/sysrq.c
216 create mode 100644 fs/aufs/vdir.c
217 create mode 100644 fs/aufs/vfsub.c
218 create mode 100644 fs/aufs/vfsub.h
219 create mode 100644 fs/aufs/wbr_policy.c
220 create mode 100644 fs/aufs/whout.c
221 create mode 100644 fs/aufs/whout.h
222 create mode 100644 fs/aufs/wkq.c
223 create mode 100644 fs/aufs/wkq.h
224 create mode 100644 fs/aufs/xattr.c
225 create mode 100644 fs/aufs/xino.c
226 create mode 100644 include/uapi/linux/aufs_type.h
227 create mode 100644 mm/prfile.c
229 diff --git a/Documentation/ABI/testing/debugfs-aufs b/Documentation/ABI/testing/debugfs-aufs
231 index 00000000000000..45b739879d7696
233 +++ b/Documentation/ABI/testing/debugfs-aufs
235 +What: /debug/aufs/si_<id>/
237 +Contact: J. R. Okajima <hooanon05g@gmail.com>
239 + Under /debug/aufs, a directory named si_<id> is created
240 + per aufs mount, where <id> is a unique id generated
243 +What: /debug/aufs/si_<id>/plink
245 +Contact: J. R. Okajima <hooanon05g@gmail.com>
247 + It has three lines and shows the information about the
248 + pseudo-link. The first line is a single number
249 + representing a number of buckets. The second line is a
250 + number of pseudo-links per buckets (separated by a
251 + blank). The last line is a single number representing a
252 + total number of psedo-links.
253 + When the aufs mount option 'noplink' is specified, it
254 + will show "1\n0\n0\n".
256 +What: /debug/aufs/si_<id>/xib
258 +Contact: J. R. Okajima <hooanon05g@gmail.com>
260 + It shows the consumed blocks by xib (External Inode Number
261 + Bitmap), its block size and file size.
262 + When the aufs mount option 'noxino' is specified, it
263 + will be empty. About XINO files, see the aufs manual.
265 +What: /debug/aufs/si_<id>/xi<branch-index>
267 +Contact: J. R. Okajima <hooanon05g@gmail.com>
269 + It shows the consumed blocks by xino (External Inode Number
270 + Translation Table), its link count, block size and file
272 + Due to the file size limit, there may exist multiple
273 + xino files per branch. In this case, "-N" is added to
274 + the filename and it corresponds to the index of the
275 + internal xino array. "-0" is omitted.
276 + When the aufs mount option 'noxino' is specified, Those
277 + entries won't exist. About XINO files, see the aufs
280 +What: /debug/aufs/si_<id>/xigen
282 +Contact: J. R. Okajima <hooanon05g@gmail.com>
284 + It shows the consumed blocks by xigen (External Inode
285 + Generation Table), its block size and file size.
286 + If CONFIG_AUFS_EXPORT is disabled, this entry will not
288 + When the aufs mount option 'noxino' is specified, it
289 + will be empty. About XINO files, see the aufs manual.
290 diff --git a/Documentation/ABI/testing/sysfs-aufs b/Documentation/ABI/testing/sysfs-aufs
292 index 00000000000000..48500c0569e658
294 +++ b/Documentation/ABI/testing/sysfs-aufs
296 +What: /sys/fs/aufs/si_<id>/
298 +Contact: J. R. Okajima <hooanon05g@gmail.com>
300 + Under /sys/fs/aufs, a directory named si_<id> is created
301 + per aufs mount, where <id> is a unique id generated
304 +What: /sys/fs/aufs/si_<id>/br<idx>
306 +Contact: J. R. Okajima <hooanon05g@gmail.com>
308 + It shows the abolute path of a member directory (which
309 + is called branch) in aufs, and its permission.
311 +What: /sys/fs/aufs/si_<id>/brid<idx>
313 +Contact: J. R. Okajima <hooanon05g@gmail.com>
315 + It shows the id of a member directory (which is called
318 +What: /sys/fs/aufs/si_<id>/xi_path
320 +Contact: J. R. Okajima <hooanon05g@gmail.com>
322 + It shows the abolute path of XINO (External Inode Number
323 + Bitmap, Translation Table and Generation Table) file
324 + even if it is the default path.
325 + When the aufs mount option 'noxino' is specified, it
326 + will be empty. About XINO files, see the aufs manual.
327 diff --git a/Documentation/filesystems/aufs/README b/Documentation/filesystems/aufs/README
329 index 00000000000000..04a4d069bb52a0
331 +++ b/Documentation/filesystems/aufs/README
334 +Aufs6 -- advanced multi layered unification filesystem version 6.x
340 +----------------------------------------
341 +In the early days, aufs was entirely re-designed and re-implemented
342 +Unionfs Version 1.x series. Adding many original ideas, approaches,
343 +improvements and implementations, it became totally different from
344 +Unionfs while keeping the basic features.
345 +Later, Unionfs Version 2.x series began taking some of the same
346 +approaches to aufs1's.
347 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
348 +University and his team.
350 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
351 +aufs-linux.git or aufs-standalone.git.
352 +If you want older kernel version support,
353 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
354 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
355 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
356 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
357 + or aufs1 from CVS on SourceForge.
359 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
360 + were changed. Now they are aufs-linux.git and
361 + aufs-standalone.git and they contain aufs5 and later branches.
363 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
364 + According to Christoph Hellwig, linux rejects all union-type
365 + filesystems but UnionMount.
366 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
368 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
369 + UnionMount, and he pointed out an issue around a directory mutex
370 + lock and aufs addressed it. But it is still unsure whether aufs will
371 + be merged (or any other union solution).
372 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
376 +----------------------------------------
377 +- unite several directories into a single virtual filesystem. The member
378 + directory is called as a branch.
379 +- you can specify the permission flags to the branch, which are 'readonly',
380 + 'readwrite' and 'whiteout-able.'
381 +- by upper writable branch, internal copyup and whiteout, files/dirs on
382 + readonly branch are modifiable logically.
383 +- dynamic branch manipulation, add, del.
386 +Also there are many enhancements in aufs, such as:
387 +- test only the highest one for the directory permission (dirperm1)
388 +- copyup on open (coo=)
389 +- 'move' policy for copy-up between two writable branches, after
390 + checking free space.
392 +- readdir(3) in userspace.
393 +- keep inode number by external inode number table
394 +- keep the timestamps of file/dir in internal copyup operation
395 +- seekable directory, supporting NFS readdir.
396 +- whiteout is hardlinked in order to reduce the consumption of inodes
398 +- do not copyup, nor create a whiteout when it is unnecessary
399 +- revert a single systemcall when an error occurs in aufs
400 +- remount interface instead of ioctl
401 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
402 +- loopback mounted filesystem as a branch
403 +- kernel thread for removing the dir who has a plenty of whiteouts
404 +- support copyup sparse file (a file which has a 'hole' in it)
405 +- default permission flags for branches
406 +- selectable permission flags for ro branch, whether whiteout can
409 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
410 +- support multiple writable branches, some policies to select one
411 + among multiple writable branches.
412 +- a new semantics for link(2) and rename(2) to support multiple
414 +- no glibc changes are required.
415 +- pseudo hardlink (hardlink over branches)
416 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
417 + including NFS or remote filesystem branch.
418 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
421 +Currently these features are dropped temporary from aufs6.
422 +See design/08plan.txt in detail.
423 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
425 +- statistics of aufs thread (/sys/fs/aufs/stat)
427 +Features or just an idea in the future (see also design/*.txt),
428 +- reorder the branch index without del/re-add.
429 +- permanent xino files for NFSD
430 +- an option for refreshing the opened files after add/del branches
431 +- light version, without branch manipulation. (unnecessary?)
432 +- copyup in userspace
433 +- inotify in userspace
438 +----------------------------------------
439 +There are three GIT trees for aufs6, aufs-linux.git,
440 +aufs-standalone.git, and aufs-util.git.
441 +While the aufs-util is always necessary, you need either of aufs-linux
444 +The aufs-linux tree includes the whole linux mainline GIT tree,
445 +git://git.kernel.org/.../torvalds/linux.git.
446 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
447 +build aufs6 as an external kernel module.
448 +Several extra patches are not included in this tree. Only
449 +aufs-standalone tree contains them. They are described in the later
450 +section "Configuration and Compilation."
452 +On the other hand, the aufs-standalone tree has only aufs source files
453 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
454 +But you need to apply all aufs patches manually.
456 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
457 +represents the linux kernel version, "linux-6.x". For instance,
458 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
459 +"aufs6.x-rcN" branch.
462 +$ git clone --reference /your/linux/git/tree \
463 + git://github.com/sfjro/aufs-linux.git aufs-linux.git
464 +- if you don't have linux GIT tree, then remove "--reference ..."
466 +$ git checkout origin/aufs6.0
468 +Or You may want to directly git-pull aufs into your linux GIT tree, and
469 +leave the patch-work to GIT.
470 +$ cd /your/linux/git/tree
471 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
473 +$ git checkout -b my6.0 v6.0
474 +$ (add your local change...)
475 +$ git pull aufs aufs6.0
476 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
477 +- you may need to solve some conflicts between your_changes and
478 + aufs6.0. in this case, git-rerere is recommended so that you can
479 + solve the similar conflicts automatically when you upgrade to 6.1 or
480 + later in the future.
482 +o aufs-standalone tree
483 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
484 +$ cd aufs-standalone.git
485 +$ git checkout origin/aufs6.0
488 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
489 +- note that the public aufs-util.git is on SourceForge instead of
492 +$ git checkout origin/aufs6.0
494 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
495 +The minor version number, 'x' in '6.x', of aufs may not always
496 +follow the minor version number of the kernel.
497 +Because changes in the kernel that cause the use of a new
498 +minor version number do not always require changes to aufs-util.
500 +Since aufs-util has its own minor version number, you may not be
501 +able to find a GIT branch in aufs-util for your kernel's
502 +exact minor version number.
503 +In this case, you should git-checkout the branch for the
504 +nearest lower number.
506 +For (an unreleased) example:
507 +If you are using "linux-6.10" and the "aufs6.10" branch
508 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
509 +or something numerically smaller is the branch for your kernel.
511 +Also you can view all branches by
515 +3. Configuration and Compilation
516 +----------------------------------------
517 +Make sure you have git-checkout'ed the correct branch.
519 +For aufs-linux tree,
520 +- enable CONFIG_AUFS_FS.
521 +- set other aufs configurations if necessary.
522 +- for aufs5.13 and later
523 + Because aufs is not only an ordinary filesystem (callee of VFS), but
524 + also a caller of VFS functions for branch filesystems, subclassing of
525 + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
526 + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
527 + need to customize some LOCKDEP numbers. Here are what I use on my
529 + CONFIG_LOCKDEP_BITS=21
530 + CONFIG_LOCKDEP_CHAINS_BITS=21
531 + CONFIG_LOCKDEP_STACK_TRACE_BITS=24
532 + Also you will need to expand some constant values in LOCKDEP. Refer
533 + to lockdep-debug.patch in aufs-standalone.git.
535 +For aufs-standalone tree,
536 +There are several ways to build.
539 +- apply ./aufs6-kbuild.patch to your kernel source files.
540 +- apply ./aufs6-base.patch too.
541 +- apply ./aufs6-mmap.patch too.
542 +- apply ./aufs6-standalone.patch too, if you have a plan to set
543 + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
544 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
545 + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
546 +- enable CONFIG_AUFS_FS, you can select either
548 +- and build your kernel as usual.
549 +- install the built kernel.
550 +- install the header files too by "make headers_install" to the
551 + directory where you specify. By default, it is $PWD/usr.
552 + "make help" shows a brief note for headers_install.
553 +- and reboot your system.
556 +- module only (CONFIG_AUFS_FS=m).
557 +- apply ./aufs6-base.patch to your kernel source files.
558 +- apply ./aufs6-mmap.patch too.
559 +- apply ./aufs6-standalone.patch too.
560 +- build your kernel, don't forget "make headers_install", and reboot.
561 +- edit ./config.mk and set other aufs configurations if necessary.
562 + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
563 + every aufs configurations.
564 +- build the module by simple "make".
565 +- you can specify ${KDIR} make variable which points to your kernel
568 + + run "make install" to install the aufs module, or copy the built
569 + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
570 + + run "make install_headers" (instead of headers_install) to install
571 + the modified aufs header file (you can specify DESTDIR which is
572 + available in aufs standalone version's Makefile only), or copy
573 + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
574 + you like manually. By default, the target directory is $PWD/usr.
575 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
576 + kernel source tree.
578 +Note: The header file aufs_type.h is necessary to build aufs-util
579 + as well as "make headers_install" in the kernel source tree.
580 + headers_install is subject to be forgotten, but it is essentially
581 + necessary, not only for building aufs-util.
582 + You may not meet problems without headers_install in some older
586 +- read README in aufs-util, build and install it
587 +- note that your distribution may contain an obsoleted version of
588 + aufs_type.h in /usr/include/linux or something. When you build aufs
589 + utilities, make sure that your compiler refers the correct aufs header
590 + file which is built by "make headers_install."
591 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
592 + then run "make install_ulib" too. And refer to the aufs manual in
595 +There several other patches in aufs-standalone.git. They are all
596 +optional. When you meet some problems, they will help you.
597 +- aufs6-loopback.patch
598 + Supports a nested loopback mount in a branch-fs. This patch is
599 + unnecessary until aufs produces a message like "you may want to try
600 + another patch for loopback file".
602 + Modifies a system global kernel internal function get_next_ino() in
603 + order to stop assigning 0 for an inode-number. Not directly related to
604 + aufs, but recommended generally.
606 + Keeps the tmpfs inode number as the lowest value. Effective to reduce
607 + the size of aufs XINO files for tmpfs branch. Also it prevents the
608 + duplication of inode number, which is important for backup tools and
609 + other utilities. When you find aufs XINO files for tmpfs branch
610 + growing too much, try this patch.
611 +- lockdep-debug.patch
612 + Similar to some kernel configurations for LOCKDEP (see the top of
613 + this section), you will need expand some constants in LOCKDEP for
614 + aufs if you enable CONFIG_LOCKDEP.
618 +----------------------------------------
619 +At first, make sure aufs-util are installed, and please read the aufs
620 +manual, aufs.5 in aufs-util.git tree.
624 +$ mkdir /tmp/rw /tmp/aufs
625 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
627 +Here is another example. The result is equivalent.
628 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
630 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
631 +# mount -o remount,append:${HOME} /tmp/aufs
633 +Then, you can see whole tree of your home dir through /tmp/aufs. If
634 +you modify a file under /tmp/aufs, the one on your home directory is
635 +not affected, instead the same named file will be newly created under
636 +/tmp/rw. And all of your modification to a file will be applied to
637 +the one under /tmp/rw. This is called the file based Copy on Write
639 +Aufs mount options are described in aufs.5.
640 +If you run chroot or something and make your aufs as a root directory,
641 +then you need to customize the shutdown script. See the aufs manual in
644 +Additionally, there are some sample usages of aufs which are a
645 +diskless system with network booting, and LiveCD over NFS.
646 +See sample dir in CVS tree on SourceForge.
650 +----------------------------------------
651 +When you have any problems or strange behaviour in aufs, please let me
653 +- /proc/mounts (instead of the output of mount(8))
654 +- /sys/module/aufs/*
655 +- /sys/fs/aufs/* (if you have them)
656 +- /debug/aufs/* (if you have them)
657 +- linux kernel version
658 + if your kernel is not plain, for example modified by distributor,
659 + the url where i can download its source is necessary too.
660 +- aufs version which was printed at loading the module or booting the
661 + system, instead of the date you downloaded.
662 +- configuration (define/undefine CONFIG_AUFS_xxx)
663 +- kernel configuration or /proc/config.gz (if you have it)
664 +- LSM (linux security module, if you are using)
665 +- behaviour which you think to be incorrect
666 +- actual operation, reproducible one is better
667 +- mailto: aufs-users at lists.sourceforge.net
669 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
670 +and Feature Requests) on SourceForge. Please join and write to
675 +----------------------------------------
676 +Thanks to everyone who have tried and are using aufs, whoever
677 +have reported a bug or any feedback.
679 +Especially donators:
680 +Tomas Matejicek(slax.org) made a donation (much more than once).
681 + Since Apr 2010, Tomas M (the author of Slax and Linux Live
682 + scripts) is making "doubling" donations.
683 + Unfortunately I cannot list all of the donators, but I really
685 + It ends Aug 2010, but the ordinary donation URL is still available.
686 + <http://sourceforge.net/donate/index.php?group_id=167503>
687 +Dai Itasaka made a donation (2007/8).
688 +Chuck Smith made a donation (2008/4, 10 and 12).
689 +Henk Schoneveld made a donation (2008/9).
690 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
691 +Francois Dupoux made a donation (2008/11).
692 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
693 + aufs2 GIT tree (2009/2).
694 +William Grant made a donation (2009/3).
695 +Patrick Lane made a donation (2009/4).
696 +The Mail Archive (mail-archive.com) made donations (2009/5).
697 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
698 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
699 +Pavel Pronskiy made a donation (2011/2).
700 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
701 + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
702 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
704 +Sam Liddicott made a donation (2011/9).
705 +Era Scarecrow made a donation (2013/4).
706 +Bor Ratajc made a donation (2013/4).
707 +Alessandro Gorreta made a donation (2013/4).
708 +POIRETTE Marc made a donation (2013/4).
709 +Alessandro Gorreta made a donation (2013/4).
710 +lauri kasvandik made a donation (2013/5).
711 +"pemasu from Finland" made a donation (2013/7).
712 +The Parted Magic Project made a donation (2013/9 and 11).
713 +Pavel Barta made a donation (2013/10).
714 +Nikolay Pertsev made a donation (2014/5).
715 +James B made a donation (2014/7, 2015/7, and 2021/12).
716 +Stefano Di Biase made a donation (2014/8).
717 +Daniel Epellei made a donation (2015/1).
718 +OmegaPhil made a donation (2016/1, 2018/4).
719 +Tomasz Szewczyk made a donation (2016/4).
720 +James Burry made a donation (2016/12).
721 +Carsten Rose made a donation (2018/9).
722 +Porteus Kiosk made a donation (2018/10).
723 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
724 +Vasily Mikhaylichenko made a donation (2023/5).
726 +Thank you very much.
727 +Donations are always, including future donations, very important and
728 +helpful for me to keep on developing aufs.
732 +----------------------------------------
733 +If you are an experienced user, no explanation is needed. Aufs is
734 +just a linux filesystem.
739 +# Local variables: ;
742 diff --git a/Documentation/filesystems/aufs/design/01intro.txt b/Documentation/filesystems/aufs/design/01intro.txt
744 index 00000000000000..4c468b3264b56b
746 +++ b/Documentation/filesystems/aufs/design/01intro.txt
749 +# Copyright (C) 2005-2022 Junjiro R. Okajima
751 +# This program is free software; you can redistribute it and/or modify
752 +# it under the terms of the GNU General Public License as published by
753 +# the Free Software Foundation; either version 2 of the License, or
754 +# (at your option) any later version.
756 +# This program is distributed in the hope that it will be useful,
757 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
758 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
759 +# GNU General Public License for more details.
761 +# You should have received a copy of the GNU General Public License
762 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
765 +----------------------------------------
767 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
768 +1. abbrev. for "advanced multi-layered unification filesystem".
769 +2. abbrev. for "another unionfs".
770 +3. abbrev. for "auf das" in German which means "on the" in English.
771 + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
772 + But "Filesystem aufs Filesystem" is hard to understand.
773 +4. abbrev. for "African Urban Fashion Show".
775 +AUFS is a filesystem with features:
776 +- multi layered stackable unification filesystem, the member directory
777 + is called as a branch.
778 +- branch permission and attribute, 'readonly', 'real-readonly',
779 + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
781 +- internal "file copy-on-write".
782 +- logical deletion, whiteout.
783 +- dynamic branch manipulation, adding, deleting and changing permission.
784 +- allow bypassing aufs, user's direct branch access.
785 +- external inode number translation table and bitmap which maintains the
786 + persistent aufs inode number.
787 +- seekable directory, including NFS readdir.
788 +- file mapping, mmap and sharing pages.
789 +- pseudo-link, hardlink over branches.
790 +- loopback mounted filesystem as a branch.
791 +- several policies to select one among multiple writable branches.
792 +- revert a single systemcall when an error occurs in aufs.
796 +Multi Layered Stackable Unification Filesystem
797 +----------------------------------------------------------------------
798 +Most people already knows what it is.
799 +It is a filesystem which unifies several directories and provides a
800 +merged single directory. When users access a file, the access will be
801 +passed/re-directed/converted (sorry, I am not sure which English word is
802 +correct) to the real file on the member filesystem. The member
803 +filesystem is called 'lower filesystem' or 'branch' and has a mode
804 +'readonly' and 'readwrite.' And the deletion for a file on the lower
805 +readonly branch is handled by creating 'whiteout' on the upper writable
808 +On LKML, there have been discussions about UnionMount (Jan Blunck,
809 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
810 +different approaches to implement the merged-view.
811 +The former tries putting it into VFS, and the latter implements as a
812 +separate filesystem.
813 +(If I misunderstand about these implementations, please let me know and
814 +I shall correct it. Because it is a long time ago when I read their
815 +source files last time).
817 +UnionMount's approach will be able to small, but may be hard to share
818 +branches between several UnionMount since the whiteout in it is
819 +implemented in the inode on branch filesystem and always
820 +shared. According to Bharata's post, readdir does not seems to be
822 +There are several missing features known in this implementations such as
823 +- for users, the inode number may change silently. eg. copy-up.
824 +- link(2) may break by copy-up.
825 +- read(2) may get an obsoleted filedata (fstat(2) too).
826 +- fcntl(F_SETLK) may be broken by copy-up.
827 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
830 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
831 +merged into mainline. This is another implementation of UnionMount as a
832 +separated filesystem. All the limitations and known problems which
833 +UnionMount are equally inherited to "overlay" filesystem.
835 +Unionfs has a longer history. When I started implementing a stackable
836 +filesystem (Aug 2005), it already existed. It has virtual super_block,
837 +inode, dentry and file objects and they have an array pointing lower
838 +same kind objects. After contributing many patches for Unionfs, I
839 +re-started my project AUFS (Jun 2006).
841 +In AUFS, the structure of filesystem resembles to Unionfs, but I
842 +implemented my own ideas, approaches and enhancements and it became
843 +totally different one.
845 +Comparing DM snapshot and fs based implementation
846 +- the number of bytes to be copied between devices is much smaller.
847 +- the type of filesystem must be one and only.
848 +- the fs must be writable, no readonly fs, even for the lower original
849 + device. so the compression fs will not be usable. but if we use
850 + loopback mount, we may address this issue.
852 + mount /cdrom/squashfs.img /sq
853 + losetup /sq/ext2.img
854 + losetup /somewhere/cow
855 + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
856 +- it will be difficult (or needs more operations) to extract the
857 + difference between the original device and COW.
858 +- DM snapshot-merge may help a lot when users try merging. in the
859 + fs-layer union, users will use rsync(1).
861 +You may want to read my old paper "Filesystems in LiveCD"
862 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
865 +Several characters/aspects/persona of aufs
866 +----------------------------------------------------------------------
868 +Aufs has several characters, aspects or persona.
869 +1. a filesystem, callee of VFS helper
870 +2. sub-VFS, caller of VFS helper for branches
871 +3. a virtual filesystem which maintains persistent inode number
872 +4. reader/writer of files on branches such like an application
874 +1. Callee of VFS Helper
875 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
876 +unlink(2) from an application reaches sys_unlink() kernel function and
877 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
878 +calls filesystem specific unlink operation. Actually aufs implements the
879 +unlink operation but it behaves like a redirector.
881 +2. Caller of VFS Helper for Branches
882 +aufs_unlink() passes the unlink request to the branch filesystem as if
883 +it were called from VFS. So the called unlink operation of the branch
884 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
885 +every necessary pre/post operation for the branch filesystem.
886 +- acquire the lock for the parent dir on a branch
887 +- lookup in a branch
888 +- revalidate dentry on a branch
889 +- mnt_want_write() for a branch
890 +- vfs_unlink() for a branch
891 +- mnt_drop_write() for a branch
892 +- release the lock on a branch
894 +3. Persistent Inode Number
895 +One of the most important issue for a filesystem is to maintain inode
896 +numbers. This is particularly important to support exporting a
897 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
898 +backend block device for its own. But some storage is necessary to
899 +keep and maintain the inode numbers. It may be a large space and may not
900 +suit to keep in memory. Aufs rents some space from its first writable
901 +branch filesystem (by default) and creates file(s) on it. These files
902 +are created by aufs internally and removed soon (currently) keeping
904 +Note: Because these files are removed, they are totally gone after
905 + unmounting aufs. It means the inode numbers are not persistent
906 + across unmount or reboot. I have a plan to make them really
907 + persistent which will be important for aufs on NFS server.
909 +4. Read/Write Files Internally (copy-on-write)
910 +Because a branch can be readonly, when you write a file on it, aufs will
911 +"copy-up" it to the upper writable branch internally. And then write the
912 +originally requested thing to the file. Generally kernel doesn't
913 +open/read/write file actively. In aufs, even a single write may cause a
914 +internal "file copy". This behaviour is very similar to cp(1) command.
916 +Some people may think it is better to pass such work to user space
917 +helper, instead of doing in kernel space. Actually I am still thinking
918 +about it. But currently I have implemented it in kernel space.
919 diff --git a/Documentation/filesystems/aufs/design/02struct.txt b/Documentation/filesystems/aufs/design/02struct.txt
921 index 00000000000000..83be46121ae26f
923 +++ b/Documentation/filesystems/aufs/design/02struct.txt
926 +# Copyright (C) 2005-2022 Junjiro R. Okajima
928 +# This program is free software; you can redistribute it and/or modify
929 +# it under the terms of the GNU General Public License as published by
930 +# the Free Software Foundation; either version 2 of the License, or
931 +# (at your option) any later version.
933 +# This program is distributed in the hope that it will be useful,
934 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
935 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
936 +# GNU General Public License for more details.
938 +# You should have received a copy of the GNU General Public License
939 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
941 +Basic Aufs Internal Structure
943 +Superblock/Inode/Dentry/File Objects
944 +----------------------------------------------------------------------
945 +As like an ordinary filesystem, aufs has its own
946 +superblock/inode/dentry/file objects. All these objects have a
947 +dynamically allocated array and store the same kind of pointers to the
948 +lower filesystem, branch.
949 +For example, when you build a union with one readwrite branch and one
950 +readonly, mounted /au, /rw and /ro respectively.
952 +- /ro/fileA exists but /rw/fileA
954 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
955 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
956 +- [0] = NULL (because /rw/fileA doesn't exist)
959 +This style of an array is essentially same to the aufs
960 +superblock/inode/dentry/file objects.
962 +Because aufs supports manipulating branches, ie. add/delete/change
963 +branches dynamically, these objects has its own generation. When
964 +branches are changed, the generation in aufs superblock is
965 +incremented. And a generation in other object are compared when it is
966 +accessed. When a generation in other objects are obsoleted, aufs
967 +refreshes the internal array.
971 +----------------------------------------------------------------------
972 +Additionally aufs superblock has some data for policies to select one
973 +among multiple writable branches, XIB files, pseudo-links and kobject.
974 +See below in detail.
975 +About the policies which supports copy-down a directory, see
979 +Branch and XINO(External Inode Number Translation Table)
980 +----------------------------------------------------------------------
981 +Every branch has its own xino (external inode number translation table)
982 +file. The xino file is created and unlinked by aufs internally. When two
983 +members of a union exist on the same filesystem, they share the single
985 +The struct of a xino file is simple, just a sequence of aufs inode
986 +numbers which is indexed by the lower inode number.
987 +In the above sample, assume the inode number of /ro/fileA is i111 and
988 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
989 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
991 +When the inode numbers are not contiguous, the xino file will be sparse
992 +which has a hole in it and doesn't consume as much disk space as it
993 +might appear. If your branch filesystem consumes disk space for such
994 +holes, then you should specify 'xino=' option at mounting aufs.
996 +Aufs has a mount option to free the disk blocks for such holes in XINO
997 +files on tmpfs or ramdisk. But it is not so effective actually. If you
998 +meet a problem of disk shortage due to XINO files, then you should try
999 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1000 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1001 +the holes in XINO files.
1003 +Also a writable branch has three kinds of "whiteout bases". All these
1004 +are existed when the branch is joined to aufs, and their names are
1005 +whiteout-ed doubly, so that users will never see their names in aufs
1007 +1. a regular file which will be hardlinked to all whiteouts.
1008 +2. a directory to store a pseudo-link.
1009 +3. a directory to store an "orphan"-ed file temporary.
1012 + When you remove a file on a readonly branch, aufs handles it as a
1013 + logical deletion and creates a whiteout on the upper writable branch
1014 + as a hardlink of this file in order not to consume inode on the
1017 + See below, Pseudo-link.
1019 + When "fileC" exists on the lower readonly branch only and it is
1020 + opened and removed with its parent dir, and then user writes
1021 + something into it, then aufs copies-up fileC to this
1022 + directory. Because there is no other dir to store fileC. After
1023 + creating a file under this dir, the file is unlinked.
1025 +Because aufs supports manipulating branches, ie. add/delete/change
1026 +dynamically, a branch has its own id. When the branch order changes,
1027 +aufs finds the new index by searching the branch id.
1031 +----------------------------------------------------------------------
1032 +Assume "fileA" exists on the lower readonly branch only and it is
1033 +hardlinked to "fileB" on the branch. When you write something to fileA,
1034 +aufs copies-up it to the upper writable branch. Additionally aufs
1035 +creates a hardlink under the Pseudo-link Directory of the writable
1036 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1037 +simple list. If fileB is read after unlinking fileA, aufs returns
1038 +filedata from the pseudo-link instead of the lower readonly
1039 +branch. Because the pseudo-link is based upon the inode, to keep the
1040 +inode number by xino (see above) is essentially necessary.
1042 +All the hardlinks under the Pseudo-link Directory of the writable branch
1043 +should be restored in a proper location later. Aufs provides a utility
1044 +to do this. The userspace helpers executed at remounting and unmounting
1046 +During this utility is running, it puts aufs into the pseudo-link
1047 +maintenance mode. In this mode, only the process which began the
1048 +maintenance mode (and its child processes) is allowed to operate in
1049 +aufs. Some other processes which are not related to the pseudo-link will
1050 +be allowed to run too, but the rest have to return an error or wait
1051 +until the maintenance mode ends. If a process already acquires an inode
1052 +mutex (in VFS), it has to return an error.
1055 +XIB(external inode number bitmap)
1056 +----------------------------------------------------------------------
1057 +Addition to the xino file per a branch, aufs has an external inode number
1058 +bitmap in a superblock object. It is also an internal file such like a
1060 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1062 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1064 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1065 +reduce the number of consumed disk blocks for these files.
1068 +Virtual or Vertical Dir, and Readdir in Userspace
1069 +----------------------------------------------------------------------
1070 +In order to support multiple layers (branches), aufs readdir operation
1071 +constructs a virtual dir block on memory. For readdir, aufs calls
1072 +vfs_readdir() internally for each dir on branches, merges their entries
1073 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1074 +object. So the file object has its entry list until it is closed. The
1075 +entry list will be updated when the file position is zero and becomes
1076 +obsoleted. This decision is made in aufs automatically.
1078 +The dynamically allocated memory block for the name of entries has a
1079 +unit of 512 bytes (by default) and stores the names contiguously (no
1080 +padding). Another block for each entry is handled by kmem_cache too.
1081 +During building dir blocks, aufs creates hash list and judging whether
1082 +the entry is whiteouted by its upper branch or already listed.
1083 +The merged result is cached in the corresponding inode object and
1084 +maintained by a customizable life-time option.
1086 +Some people may call it can be a security hole or invite DoS attack
1087 +since the opened and once readdir-ed dir (file object) holds its entry
1088 +list and becomes a pressure for system memory. But I'd say it is similar
1089 +to files under /proc or /sys. The virtual files in them also holds a
1090 +memory page (generally) while they are opened. When an idea to reduce
1091 +memory for them is introduced, it will be applied to aufs too.
1092 +For those who really hate this situation, I've developed readdir(3)
1093 +library which operates this merging in userspace. You just need to set
1094 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1095 +kernel space for readdir(3).
1099 +----------------------------------------------------------------------
1100 +Aufs sometimes requires privilege access to a branch. For instance,
1101 +in copy-up/down operation. When a user process is going to make changes
1102 +to a file which exists in the lower readonly branch only, and the mode
1103 +of one of ancestor directories may not be writable by a user
1104 +process. Here aufs copy-up the file with its ancestors and they may
1105 +require privilege to set its owner/group/mode/etc.
1106 +This is a typical case of a application character of aufs (see
1109 +Aufs uses workqueue synchronously for this case. It creates its own
1110 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1111 +passes the request to call mkdir or write (for example), and wait for
1112 +its completion. This approach solves a problem of a signal handler
1114 +If aufs didn't adopt the workqueue and changed the privilege of the
1115 +process, then the process may receive the unexpected SIGXFSZ or other
1118 +Also aufs uses the system global workqueue ("events" kernel thread) too
1119 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1120 +whiteout base and etc. This is unrelated to a privilege.
1121 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1122 +superblock at the beginning, at the same time waits for the completion
1123 +of all queued asynchronous tasks.
1127 +----------------------------------------------------------------------
1128 +The whiteout in aufs is very similar to Unionfs's. That is represented
1129 +by its filename. UnionMount takes an approach of a file mode, but I am
1130 +afraid several utilities (find(1) or something) will have to support it.
1132 +Basically the whiteout represents "logical deletion" which stops aufs to
1133 +lookup further, but also it represents "dir is opaque" which also stop
1136 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1137 +In order to make several functions in a single systemcall to be
1138 +revertible, aufs adopts an approach to rename a directory to a temporary
1139 +unique whiteouted name.
1140 +For example, in rename(2) dir where the target dir already existed, aufs
1141 +renames the target dir to a temporary unique whiteouted name before the
1142 +actual rename on a branch, and then handles other actions (make it opaque,
1143 +update the attributes, etc). If an error happens in these actions, aufs
1144 +simply renames the whiteouted name back and returns an error. If all are
1145 +succeeded, aufs registers a function to remove the whiteouted unique
1146 +temporary name completely and asynchronously to the system global
1151 +----------------------------------------------------------------------
1152 +It is a well-known feature or concept.
1153 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1154 +internally and makes change to the new file on the upper writable branch.
1155 +When the trigger systemcall does not update the timestamps of the parent
1156 +dir, aufs reverts it after copy-up.
1159 +Move-down (aufs3.9 and later)
1160 +----------------------------------------------------------------------
1161 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1162 +the lower readonly branch to the upper writable branch when a user
1163 +changes something about the file.
1164 +"Move-down" is an opposite action of copy-up. Basically this action is
1165 +ran manually instead of automatically and internally.
1166 +For desgin and implementation, aufs has to consider these issues.
1167 +- whiteout for the file may exist on the lower branch.
1168 +- ancestor directories may not exist on the lower branch.
1169 +- diropq for the ancestor directories may exist on the upper branch.
1170 +- free space on the lower branch will reduce.
1171 +- another access to the file may happen during moving-down, including
1172 + UDBA (see "Revalidate Dentry and UDBA").
1173 +- the file should not be hard-linked nor pseudo-linked. they should be
1174 + handled by auplink utility later.
1176 +Sometimes users want to move-down a file from the upper writable branch
1177 +to the lower readonly or writable branch. For instance,
1178 +- the free space of the upper writable branch is going to run out.
1179 +- create a new intermediate branch between the upper and lower branch.
1182 +For this purpose, use "aumvdown" command in aufs-util.git.
1183 diff --git a/Documentation/filesystems/aufs/design/03atomic_open.txt b/Documentation/filesystems/aufs/design/03atomic_open.txt
1184 new file mode 100644
1185 index 00000000000000..4811f243246545
1187 +++ b/Documentation/filesystems/aufs/design/03atomic_open.txt
1190 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1192 +# This program is free software; you can redistribute it and/or modify
1193 +# it under the terms of the GNU General Public License as published by
1194 +# the Free Software Foundation; either version 2 of the License, or
1195 +# (at your option) any later version.
1197 +# This program is distributed in the hope that it will be useful,
1198 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1199 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1200 +# GNU General Public License for more details.
1202 +# You should have received a copy of the GNU General Public License
1203 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1205 +Support for a branch who has its ->atomic_open()
1206 +----------------------------------------------------------------------
1207 +The filesystems who implement its ->atomic_open() are not majority. For
1208 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1209 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1210 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1211 +sure whether all filesystems who have ->atomic_open() behave like this,
1212 +but NFSv4 surely returns the error.
1214 +In order to support ->atomic_open() for aufs, there are a few
1217 +A. Introduce aufs_atomic_open()
1218 + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1220 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1221 + an aufs user Pip Cet's approach
1222 + - calls aufs_create(), VFS finish_open() and notify_change().
1223 + - pass fake-mode to finish_open(), and then correct the mode by
1225 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1226 + - no aufs_atomic_open().
1227 + - aufs_lookup() registers the TID to an aufs internal object.
1228 + - aufs_create() does nothing when the matching TID is registered, but
1229 + registers the mode.
1230 + - aufs_open() calls branch fs's ->atomic_open() when the matching
1231 + TID is registered.
1232 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1234 + - no aufs_atomic_open().
1235 + - aufs_create() registers the TID to an internal object. this info
1236 + represents "this process created this file just now."
1237 + - when aufs gets EACCES from branch fs's ->open(), then confirm the
1238 + registered TID and re-try open() with superuser's credential.
1240 +Pros and cons for each approach.
1243 + - straightforward but highly depends upon VFS internal.
1244 + - the atomic behavaiour is kept.
1245 + - some of parameters such as nameidata are hard to reproduce for
1249 + - easy to implement.
1250 + - the atomic behavaiour is lost.
1252 + - the atomic behavaiour is kept.
1253 + - dirty and tricky.
1254 + - VFS checks whether the file is created correctly after calling
1255 + ->create(), which means this approach doesn't work.
1257 + - easy to implement.
1258 + - the atomic behavaiour is lost.
1259 + - to open a file with superuser's credential and give it to a user
1260 + process is a bad idea, since the file object keeps the credential
1261 + in it. It may affect LSM or something. This approach doesn't work
1264 +The approach A is ideal, but it hard to implement. So here is a
1265 +variation of A, which is to be implemented.
1267 +A-1. Introduce aufs_atomic_open()
1268 + - calls branch fs ->atomic_open() if exists. otherwise calls
1269 + vfs_create() and finish_open().
1270 + - the demerit is that the several checks after branch fs
1271 + ->atomic_open() are lost. in the ordinary case, the checks are
1272 + done by VFS:do_last(), lookup_open() and atomic_open(). some can
1273 + be implemented in aufs, but not all I am afraid.
1274 diff --git a/Documentation/filesystems/aufs/design/03lookup.txt b/Documentation/filesystems/aufs/design/03lookup.txt
1275 new file mode 100644
1276 index 00000000000000..766a28be02637a
1278 +++ b/Documentation/filesystems/aufs/design/03lookup.txt
1281 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1283 +# This program is free software; you can redistribute it and/or modify
1284 +# it under the terms of the GNU General Public License as published by
1285 +# the Free Software Foundation; either version 2 of the License, or
1286 +# (at your option) any later version.
1288 +# This program is distributed in the hope that it will be useful,
1289 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1290 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1291 +# GNU General Public License for more details.
1293 +# You should have received a copy of the GNU General Public License
1294 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1297 +----------------------------------------------------------------------
1298 +Since aufs has a character of sub-VFS (see Introduction), it operates
1299 +lookup for branches as VFS does. It may be a heavy work. But almost all
1300 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1301 +directly connected to its parent. Digging down the directory hierarchy
1302 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1305 +When a branch is a remote filesystem, aufs basically relies upon its
1306 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1308 +For d_revalidate, aufs implements three levels of revalidate tests. See
1309 +"Revalidate Dentry and UDBA" in detail.
1312 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1313 +----------------------------------------------------------------------
1314 +Let's try case study.
1315 +- aufs has two branches, upper readwrite and lower readonly.
1317 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1318 +- user invoked "chmod a+rx /au/dirA"
1319 +- the internal copy-up is activated and "/rw/dirA" is created and its
1320 + permission bits are set to world readable.
1321 +- then "/au/dirA" becomes world readable?
1323 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1324 +or it may be a natively readonly filesystem. If aufs respects the lower
1325 +branch, it should not respond readdir request from other users. But user
1326 +allowed it by chmod. Should really aufs rejects showing the entries
1329 +To be honest, I don't have a good solution for this case. So aufs
1330 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1332 +When dirperm1 is specified, aufs checks only the highest one for the
1333 +directory permission, and shows the entries. Otherwise, as usual, checks
1334 +every dir existing on all branches and rejects the request.
1336 +As a side effect, dirperm1 option improves the performance of aufs
1337 +because the number of permission check is reduced when the number of
1341 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1342 +----------------------------------------------------------------------
1343 +Generally VFS helpers re-validate a dentry as a part of lookup.
1344 +0. digging down the directory hierarchy.
1345 +1. lock the parent dir by its i_mutex.
1346 +2. lookup the final (child) entry.
1348 +4. call the actual operation (create, unlink, etc.)
1349 +5. unlock the parent dir
1351 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1352 +called. Actually aufs implements it and checks the dentry on a branch is
1354 +But it is not enough. Because aufs has to release the lock for the
1355 +parent dir on a branch at the end of ->lookup() (step 2) and
1356 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1358 +If the file on a branch is changed directly, eg. bypassing aufs, after
1359 +aufs released the lock, then the subsequent operation may cause
1360 +something unpleasant result.
1362 +This situation is a result of VFS architecture, ->lookup() and
1363 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1364 +design from VFS's point of view. It is just not suitable for sub-VFS
1367 +Aufs supports such case by three level of revalidation which is
1368 +selectable by user.
1369 +1. Simple Revalidate
1370 + Addition to the native flow in VFS's, confirm the child-parent
1371 + relationship on the branch just after locking the parent dir on the
1372 + branch in the "actual operation" (step 4). When this validation
1373 + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1374 + checks the validation of the dentry on branches.
1375 +2. Monitor Changes Internally by Inotify/Fsnotify
1376 + Addition to above, in the "actual operation" (step 4) aufs re-lookup
1377 + the dentry on the branch, and returns EBUSY if it finds different
1379 + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1380 + during it is in cache. When the event is notified, aufs registers a
1381 + function to kernel 'events' thread by schedule_work(). And the
1382 + function sets some special status to the cached aufs dentry and inode
1383 + private data. If they are not cached, then aufs has nothing to
1384 + do. When the same file is accessed through aufs (step 0-3) later,
1385 + aufs will detect the status and refresh all necessary data.
1386 + In this mode, aufs has to ignore the event which is fired by aufs
1388 +3. No Extra Validation
1389 + This is the simplest test and doesn't add any additional revalidation
1390 + test, and skip the revalidation in step 4. It is useful and improves
1391 + aufs performance when system surely hide the aufs branches from user,
1392 + by over-mounting something (or another method).
1393 diff --git a/Documentation/filesystems/aufs/design/04branch.txt b/Documentation/filesystems/aufs/design/04branch.txt
1394 new file mode 100644
1395 index 00000000000000..ffeb97dcaff350
1397 +++ b/Documentation/filesystems/aufs/design/04branch.txt
1400 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1402 +# This program is free software; you can redistribute it and/or modify
1403 +# it under the terms of the GNU General Public License as published by
1404 +# the Free Software Foundation; either version 2 of the License, or
1405 +# (at your option) any later version.
1407 +# This program is distributed in the hope that it will be useful,
1408 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1409 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1410 +# GNU General Public License for more details.
1412 +# You should have received a copy of the GNU General Public License
1413 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1415 +Branch Manipulation
1417 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1418 +and changing its permission/attribute, there are a lot of works to do.
1422 +----------------------------------------------------------------------
1423 +o Confirm the adding dir exists outside of aufs, including loopback
1424 + mount, and its various attributes.
1425 +o Initialize the xino file and whiteout bases if necessary.
1428 +o Check the owner/group/mode of the directory
1429 + When the owner/group/mode of the adding directory differs from the
1430 + existing branch, aufs issues a warning because it may impose a
1432 + For example, when a upper writable branch has a world writable empty
1433 + top directory, a malicious user can create any files on the writable
1434 + branch directly, like copy-up and modify manually. If something like
1435 + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1436 + writable branch, and the writable branch is world-writable, then a
1437 + malicious guy may create /etc/passwd on the writable branch directly
1438 + and the infected file will be valid in aufs.
1439 + I am afraid it can be a security issue, but aufs can do nothing except
1440 + producing a warning.
1444 +----------------------------------------------------------------------
1445 +o Confirm the deleting branch is not busy
1446 + To be general, there is one merit to adopt "remount" interface to
1447 + manipulate branches. It is to discard caches. At deleting a branch,
1448 + aufs checks the still cached (and connected) dentries and inodes. If
1449 + there are any, then they are all in-use. An inode without its
1450 + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1452 + For the cached one, aufs checks whether the same named entry exists on
1454 + If the cached one is a directory, because aufs provides a merged view
1455 + to users, as long as one dir is left on any branch aufs can show the
1456 + dir to users. In this case, the branch can be removed from aufs.
1457 + Otherwise aufs rejects deleting the branch.
1459 + If any file on the deleting branch is opened by aufs, then aufs
1463 +Modify the Permission of a Branch
1464 +----------------------------------------------------------------------
1465 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1468 +o rw --> ro: Confirm the modifying branch is not busy
1469 + Aufs rejects the request if any of these conditions are true.
1470 + - a file on the branch is mmap-ed.
1471 + - a regular file on the branch is opened for write and there is no
1472 + same named entry on the upper branch.
1473 diff --git a/Documentation/filesystems/aufs/design/05wbr_policy.txt b/Documentation/filesystems/aufs/design/05wbr_policy.txt
1474 new file mode 100644
1475 index 00000000000000..a2143bfe6efc87
1477 +++ b/Documentation/filesystems/aufs/design/05wbr_policy.txt
1480 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1482 +# This program is free software; you can redistribute it and/or modify
1483 +# it under the terms of the GNU General Public License as published by
1484 +# the Free Software Foundation; either version 2 of the License, or
1485 +# (at your option) any later version.
1487 +# This program is distributed in the hope that it will be useful,
1488 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1489 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1490 +# GNU General Public License for more details.
1492 +# You should have received a copy of the GNU General Public License
1493 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1495 +Policies to Select One among Multiple Writable Branches
1496 +----------------------------------------------------------------------
1497 +When the number of writable branch is more than one, aufs has to decide
1498 +the target branch for file creation or copy-up. By default, the highest
1499 +writable branch which has the parent (or ancestor) dir of the target
1500 +file is chosen (top-down-parent policy).
1501 +By user's request, aufs implements some other policies to select the
1502 +writable branch, for file creation several policies, round-robin,
1503 +most-free-space, and other policies. For copy-up, top-down-parent,
1504 +bottom-up-parent, bottom-up and others.
1506 +As expected, the round-robin policy selects the branch in circular. When
1507 +you have two writable branches and creates 10 new files, 5 files will be
1508 +created for each branch. mkdir(2) systemcall is an exception. When you
1509 +create 10 new directories, all will be created on the same branch.
1510 +And the most-free-space policy selects the one which has most free
1511 +space among the writable branches. The amount of free space will be
1512 +checked by aufs internally, and users can specify its time interval.
1514 +The policies for copy-up is more simple,
1515 +top-down-parent is equivalent to the same named on in create policy,
1516 +bottom-up-parent selects the writable branch where the parent dir
1517 +exists and the nearest upper one from the copyup-source,
1518 +bottom-up selects the nearest upper writable branch from the
1519 +copyup-source, regardless the existence of the parent dir.
1521 +There are some rules or exceptions to apply these policies.
1522 +- If there is a readonly branch above the policy-selected branch and
1523 + the parent dir is marked as opaque (a variation of whiteout), or the
1524 + target (creating) file is whiteout-ed on the upper readonly branch,
1525 + then the result of the policy is ignored and the target file will be
1526 + created on the nearest upper writable branch than the readonly branch.
1527 +- If there is a writable branch above the policy-selected branch and
1528 + the parent dir is marked as opaque or the target file is whiteouted
1529 + on the branch, then the result of the policy is ignored and the target
1530 + file will be created on the highest one among the upper writable
1531 + branches who has diropq or whiteout. In case of whiteout, aufs removes
1533 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1534 + They try selecting the branch where the source exists as possible
1535 + since copyup a large file will take long time. If it can't be,
1536 + ie. the branch where the source exists is readonly, then they will
1537 + follow the copyup policy.
1538 +- There is an exception for rename(2) when the target exists.
1539 + If the rename target exists, aufs compares the index of the branches
1540 + where the source and the target exists and selects the higher
1541 + one. If the selected branch is readonly, then aufs follows the
1543 diff --git a/Documentation/filesystems/aufs/design/06dirren.dot b/Documentation/filesystems/aufs/design/06dirren.dot
1544 new file mode 100644
1545 index 00000000000000..4e6c7e7c20ef13
1547 +++ b/Documentation/filesystems/aufs/design/06dirren.dot
1550 +// to view this graph, run dot(1) command in GRAPHVIZ.
1552 +// This program is free software; you can redistribute it and/or modify
1553 +// it under the terms of the GNU General Public License as published by
1554 +// the Free Software Foundation; either version 2 of the License, or
1555 +// (at your option) any later version.
1557 +// This program is distributed in the hope that it will be useful,
1558 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1559 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1560 +// GNU General Public License for more details.
1562 +// You should have received a copy of the GNU General Public License
1563 +// along with this program. If not, see <http://www.gnu.org/licenses/>.
1567 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1571 +aufs_rename -> whinfo [label="store/remove"];
1574 +inode_list [label="h_inum list in branch\ncache"];
1577 +whinode [label="h_inum list file"];
1580 +brmgmt [label="br_add/del/mod/umount"];
1582 +brmgmt -> inode_list [label="create/remove"];
1583 +brmgmt -> whinode [label="load/store"];
1585 +inode_list -> whinode [style=dashed,dir=both];
1587 +aufs_rename -> inode_list [label="add/del"];
1589 +aufs_lookup -> inode_list [label="search"];
1591 +aufs_lookup -> whinfo [label="load/remove"];
1593 diff --git a/Documentation/filesystems/aufs/design/06dirren.txt b/Documentation/filesystems/aufs/design/06dirren.txt
1594 new file mode 100644
1595 index 00000000000000..58ec5e22fe3d00
1597 +++ b/Documentation/filesystems/aufs/design/06dirren.txt
1600 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1602 +# This program is free software; you can redistribute it and/or modify
1603 +# it under the terms of the GNU General Public License as published by
1604 +# the Free Software Foundation; either version 2 of the License, or
1605 +# (at your option) any later version.
1607 +# This program is distributed in the hope that it will be useful,
1608 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1609 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1610 +# GNU General Public License for more details.
1612 +# You should have received a copy of the GNU General Public License
1613 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1615 +Special handling for renaming a directory (DIRREN)
1616 +----------------------------------------------------------------------
1617 +First, let's assume we have a simple usecase.
1621 +- /ro/dirA and /ro/dirA/file exist too
1622 +- there is no dirB on both branches
1623 +- a user issues rename("dirA", "dirB")
1625 +Now, what should aufs behave against this rename(2)?
1626 +There are a few possible cases.
1629 + since dirA exists on a readonly branch which cannot be renamed.
1631 + it is possible to copy-up dirA (only the dir itself), but the child
1632 + entries ("file" in this case) should not be. it must be a bad
1633 + approach to copy-up recursively.
1634 +C. returns a success.
1635 + even the branch /ro is readonly, aufs tries renaming it. Obviously it
1636 + is a violation of aufs' policy.
1637 +D. construct an extra information which indicates that /ro/dirA should
1638 + be handled as the name of dirB.
1639 + overlayfs has a similar feature called REDIRECT.
1641 +Until now, aufs implements the case B only which returns EXDEV, and
1642 +expects the userspace application behaves like mv(1) which tries
1643 +issueing rename(2) recursively.
1645 +A new aufs feature called DIRREN is introduced which implements the case
1646 +D. There are several "extra information" added.
1648 +1. detailed info per renamed directory
1649 + path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1650 +2. the inode-number list of directories on a branch
1651 + path: /rw/dirB/$AUFS_WH_DR_BRHINO
1653 +The filename of "detailed info per directory" represents the lower
1654 +branch, and its format is
1655 +- a type of the branch id
1657 + + uuid (not implemented yet)
1660 +- the inode-number of the branch root dir
1662 +And it contains these info in a single regular file.
1664 +- branch's inode-number of the logically renamed dir
1665 +- the name of the before-renamed dir
1667 +The "detailed info per directory" file is created in aufs rename(2), and
1668 +loaded in any lookup.
1669 +The info is considered in lookup for the matching case only. Here
1670 +"matching" means that the root of branch (in the info filename) is same
1671 +to the current looking-up branch. After looking-up the before-renamed
1672 +name, the inode-number is compared. And the matched dentry is used.
1674 +The "inode-number list of directories" is a regular file which contains
1675 +simply the inode-numbers on the branch. The file is created or updated
1676 +in removing the branch, and loaded in adding the branch. Its lifetime is
1677 +equal to the branch.
1678 +The list is referred in lookup, and when the current target inode is
1679 +found in the list, the aufs tries loading the "detailed info per
1680 +directory" and get the changed and valid name of the dir.
1682 +Theoretically these "extra informaiton" may be able to be put into XATTR
1683 +in the dir inode. But aufs doesn't choose this way because
1684 +1. XATTR may not be supported by the branch (or its configuration)
1685 +2. XATTR may have its size limit.
1686 +3. XATTR may be less easy to convert than a regular file, when the
1687 + format of the info is changed in the future.
1688 +At the same time, I agree that the regular file approach is much slower
1689 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1692 +This DIRREN feature is enabled by aufs configuration, and is activated
1693 +by a new mount option.
1695 +For the more complicated case, there is a work with UDBA option, which
1696 +is to dected the direct access to the branches (by-passing aufs) and to
1697 +maintain the cashes in aufs. Since a single cached aufs dentry may
1698 +contains two names, before- and after-rename, the name comparision in
1699 +UDBA handler may not work correctly. In this case, the behaviour will be
1700 +equivalen to udba=reval case.
1701 diff --git a/Documentation/filesystems/aufs/design/06fhsm.txt b/Documentation/filesystems/aufs/design/06fhsm.txt
1702 new file mode 100644
1703 index 00000000000000..d3b56325ff87ad
1705 +++ b/Documentation/filesystems/aufs/design/06fhsm.txt
1708 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1710 +# This program is free software; you can redistribute it and/or modify
1711 +# it under the terms of the GNU General Public License as published by
1712 +# the Free Software Foundation; either version 2 of the License, or
1713 +# (at your option) any later version.
1715 +# This program is distributed in the hope that it will be useful,
1716 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1717 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1718 +# GNU General Public License for more details.
1720 +# You should have received a copy of the GNU General Public License
1721 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1723 +File-based Hierarchical Storage Management (FHSM)
1724 +----------------------------------------------------------------------
1725 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1726 +storage world. Aufs provides this feature as file-based with multiple
1727 +writable branches, based upon the principle of "Colder, the Lower".
1728 +Here the word "colder" means that the less used files, and "lower" means
1729 +that the position in the order of the stacked branches vertically.
1730 +These multiple writable branches are prioritized, ie. the topmost one
1731 +should be the fastest drive and be used heavily.
1733 +o Characters in aufs FHSM story
1734 +- aufs itself and a new branch attribute.
1735 +- a new ioctl interface to move-down and to establish a connection with
1736 + the daemon ("move-down" is a converse of "copy-up").
1737 +- userspace tool and daemon.
1739 +The userspace daemon establishes a connection with aufs and waits for
1740 +the notification. The notified information is very similar to struct
1741 +statfs containing the number of consumed blocks and inodes.
1742 +When the consumed blocks/inodes of a branch exceeds the user-specified
1743 +upper watermark, the daemon activates its move-down process until the
1744 +consumed blocks/inodes reaches the user-specified lower watermark.
1746 +The actual move-down is done by aufs based upon the request from
1747 +user-space since we need to maintain the inode number and the internal
1748 +pointer arrays in aufs.
1750 +Currently aufs FHSM handles the regular files only. Additionally they
1751 +must not be hard-linked nor pseudo-linked.
1754 +o Cowork of aufs and the user-space daemon
1755 + During the userspace daemon established the connection, aufs sends a
1756 + small notification to it whenever aufs writes something into the
1757 + writable branch. But it may cost high since aufs issues statfs(2)
1758 + internally. So user can specify a new option to cache the
1759 + info. Actually the notification is controlled by these factors.
1760 + + the specified cache time.
1761 + + classified as "force" by aufs internally.
1762 + Until the specified time expires, aufs doesn't send the info
1763 + except the forced cases. When aufs decide forcing, the info is always
1764 + notified to userspace.
1765 + For example, the number of free inodes is generally large enough and
1766 + the shortage of it happens rarely. So aufs doesn't force the
1767 + notification when creating a new file, directory and others. This is
1768 + the typical case which aufs doesn't force.
1769 + When aufs writes the actual filedata and the files consumes any of new
1770 + blocks, the aufs forces notifying.
1773 +o Interfaces in aufs
1774 +- New branch attribute.
1776 + Specifies that the branch is managed by FHSM feature. In other word,
1777 + participant in the FHSM.
1778 + When nofhsm is set to the branch, it will not be the source/target
1779 + branch of the move-down operation. This attribute is set
1780 + independently from coo and moo attributes, and if you want full
1781 + FHSM, you should specify them as well.
1782 +- New mount option.
1784 + Specifies a second to suppress many less important info to be
1787 + + AUFS_CTL_FHSM_FD
1788 + create a new file descriptor which userspace can read the notification
1789 + (a subset of struct statfs) from aufs.
1790 +- Module parameter 'brs'
1791 + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1793 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1794 + When there are two or more branches with fhsm attributes,
1795 + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1796 + terminates it. As a result of remounting and branch-manipulation, the
1797 + number of branches with fhsm attribute can be one. In this case,
1798 + /sbin/mount.aufs will terminate the user-space daemon.
1801 +Finally the operation is done as these steps in kernel-space.
1803 + + no one else is using the file.
1804 + + the file is not hard-linked.
1805 + + the file is not pseudo-linked.
1806 + + the file is a regular file.
1807 + + the parent dir is not opaqued.
1808 +- find the target writable branch.
1809 +- make sure the file is not whiteout-ed by the upper (than the target)
1811 +- make the parent dir on the target branch.
1812 +- mutex lock the inode on the branch.
1813 +- unlink the whiteout on the target branch (if exists).
1814 +- lookup and create the whiteout-ed temporary name on the target branch.
1815 +- copy the file as the whiteout-ed temporary name on the target branch.
1816 +- rename the whiteout-ed temporary name to the original name.
1817 +- unlink the file on the source branch.
1818 +- maintain the internal pointer array and the external inode number
1820 +- maintain the timestamps and other attributes of the parent dir and the
1823 +And of course, in every step, an error may happen. So the operation
1824 +should restore the original file state after an error happens.
1825 diff --git a/Documentation/filesystems/aufs/design/06mmap.txt b/Documentation/filesystems/aufs/design/06mmap.txt
1826 new file mode 100644
1827 index 00000000000000..ddc65ce97fa3e8
1829 +++ b/Documentation/filesystems/aufs/design/06mmap.txt
1832 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1834 +# This program is free software; you can redistribute it and/or modify
1835 +# it under the terms of the GNU General Public License as published by
1836 +# the Free Software Foundation; either version 2 of the License, or
1837 +# (at your option) any later version.
1839 +# This program is distributed in the hope that it will be useful,
1840 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1841 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1842 +# GNU General Public License for more details.
1844 +# You should have received a copy of the GNU General Public License
1845 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1847 +mmap(2) -- File Memory Mapping
1848 +----------------------------------------------------------------------
1849 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1850 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1852 +This approach is simple and good, but there is one problem.
1853 +Under /proc, several entries show the mmapped files by its path (with
1854 +device and inode number), and the printed path will be the path on the
1855 +branch fs's instead of virtual aufs's.
1856 +This is not a problem in most cases, but some utilities lsof(1) (and its
1857 +user) may expect the path on aufs.
1859 +To address this issue, aufs adds a new member called vm_prfile in struct
1860 +vm_area_struct (and struct vm_region). The original vm_file points to
1861 +the file on the branch fs in order to handle everything correctly as
1862 +usual. The new vm_prfile points to a virtual file in aufs, and the
1863 +show-functions in procfs refers to vm_prfile if it is set.
1864 +Also we need to maintain several other places where touching vm_file
1866 +- fork()/clone() copies vma and the reference count of vm_file is
1868 +- merging vma maintains the ref count too.
1870 +This is not a good approach. It just fakes the printed path. But it
1871 +leaves all behaviour around f_mapping unchanged. This is surely an
1873 +Actually aufs had adopted another complicated approach which calls
1874 +generic_file_mmap() and handles struct vm_operations_struct. In this
1875 +approach, aufs met a hard problem and I could not solve it without
1876 +switching the approach.
1878 +There may be one more another approach which is
1879 +- bind-mount the branch-root onto the aufs-root internally
1880 +- grab the new vfsmount (ie. struct mount)
1881 +- lazy-umount the branch-root internally
1882 +- in open(2) the aufs-file, open the branch-file with the hidden
1883 + vfsmount (instead of the original branch's vfsmount)
1884 +- ideally this "bind-mount and lazy-umount" should be done atomically,
1885 + but it may be possible from userspace by the mount helper.
1887 +Adding the internal hidden vfsmount and using it in opening a file, the
1888 +file path under /proc will be printed correctly. This approach looks
1889 +smarter, but is not possible I am afraid.
1890 +- aufs-root may be bind-mount later. when it happens, another hidden
1891 + vfsmount will be required.
1892 +- it is hard to get the chance to bind-mount and lazy-umount
1893 + + in kernel-space, FS can have vfsmount in open(2) via
1894 + file->f_path, and aufs can know its vfsmount. But several locks are
1895 + already acquired, and if aufs tries to bind-mount and lazy-umount
1896 + here, then it may cause a deadlock.
1897 + + in user-space, bind-mount doesn't invoke the mount helper.
1898 +- since /proc shows dev and ino, aufs has to give vma these info. it
1899 + means a new member vm_prinode will be necessary. this is essentially
1900 + equivalent to vm_prfile described above.
1902 +I have to give up this "looks-smater" approach.
1903 diff --git a/Documentation/filesystems/aufs/design/06xattr.txt b/Documentation/filesystems/aufs/design/06xattr.txt
1904 new file mode 100644
1905 index 00000000000000..4e5ead3ad74194
1907 +++ b/Documentation/filesystems/aufs/design/06xattr.txt
1910 +# Copyright (C) 2014-2022 Junjiro R. Okajima
1912 +# This program is free software; you can redistribute it and/or modify
1913 +# it under the terms of the GNU General Public License as published by
1914 +# the Free Software Foundation; either version 2 of the License, or
1915 +# (at your option) any later version.
1917 +# This program is distributed in the hope that it will be useful,
1918 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1919 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1920 +# GNU General Public License for more details.
1922 +# You should have received a copy of the GNU General Public License
1923 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1925 +Listing XATTR/EA and getting the value
1926 +----------------------------------------------------------------------
1927 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
1928 +shows the values from the topmost existing file. This behaviour is good
1929 +for the non-dir entries since the bahaviour exactly matches the shown
1930 +information. But for the directories, aufs considers all the same named
1931 +entries on the lower branches. Which means, if one of the lower entry
1932 +rejects readdir call, then aufs returns an error even if the topmost
1933 +entry allows it. This behaviour is necessary to respect the branch fs's
1934 +security, but can make users confused since the user-visible standard
1935 +attributes don't match the behaviour.
1936 +To address this issue, aufs has a mount option called dirperm1 which
1937 +checks the permission for the topmost entry only, and ignores the lower
1938 +entry's permission.
1940 +A similar issue can happen around XATTR.
1941 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
1942 +always set. Otherwise these very unpleasant situation would happen.
1943 +- listxattr(2) may return the duplicated entries.
1944 +- users may not be able to remove or reset the XATTR forever,
1947 +XATTR/EA support in the internal (copy,move)-(up,down)
1948 +----------------------------------------------------------------------
1949 +Generally the extended attributes of inode are categorized as these.
1950 +- "security" for LSM and capability.
1951 +- "system" for posix ACL, 'acl' mount option is required for the branch
1953 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
1954 +- "user" for userspace, 'user_xattr' mount option is required for the
1955 + branch fs generally.
1957 +Moreover there are some other categories. Aufs handles these rather
1958 +unpopular categories as the ordinary ones, ie. there is no special
1959 +condition nor exception.
1961 +In copy-up, the support for XATTR on the dst branch may differ from the
1962 +src branch. In this case, the copy-up operation will get an error and
1963 +the original user operation which triggered the copy-up will fail. It
1964 +can happen that even all copy-up will fail.
1965 +When both of src and dst branches support XATTR and if an error occurs
1966 +during copying XATTR, then the copy-up should fail obviously. That is a
1967 +good reason and aufs should return an error to userspace. But when only
1968 +the src branch support that XATTR, aufs should not return an error.
1969 +For example, the src branch supports ACL but the dst branch doesn't
1970 +because the dst branch may natively un-support it or temporary
1971 +un-support it due to "noacl" mount option. Of course, the dst branch fs
1972 +may NOT return an error even if the XATTR is not supported. It is
1973 +totally up to the branch fs.
1975 +Anyway when the aufs internal copy-up gets an error from the dst branch
1976 +fs, then aufs tries removing the just copied entry and returns the error
1977 +to the userspace. The worst case of this situation will be all copy-up
1980 +For the copy-up operation, there two basic approaches.
1981 +- copy the specified XATTR only (by category above), and return the
1982 + error unconditionally if it happens.
1983 +- copy all XATTR, and ignore the error on the specified category only.
1985 +In order to support XATTR and to implement the correct behaviour, aufs
1986 +chooses the latter approach and introduces some new branch attributes,
1987 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
1988 +They correspond to the XATTR namespaces (see above). Additionally, to be
1989 +convenient, "icex" is also provided which means all "icex*" attributes
1990 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
1992 +The meaning of these attributes is to ignore the error from setting
1993 +XATTR on that branch.
1994 +Note that aufs tries copying all XATTR unconditionally, and ignores the
1995 +error from the dst branch according to the specified attributes.
1997 +Some XATTR may have its default value. The default value may come from
1998 +the parent dir or the environment. If the default value is set at the
1999 +file creating-time, it will be overwritten by copy-up.
2000 +Some contradiction may happen I am afraid.
2001 +Do we need another attribute to stop copying XATTR? I am unsure. For
2002 +now, aufs implements the branch attributes to ignore the error.
2003 diff --git a/Documentation/filesystems/aufs/design/07export.txt b/Documentation/filesystems/aufs/design/07export.txt
2004 new file mode 100644
2005 index 00000000000000..40b2f1f1204932
2007 +++ b/Documentation/filesystems/aufs/design/07export.txt
2010 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2012 +# This program is free software; you can redistribute it and/or modify
2013 +# it under the terms of the GNU General Public License as published by
2014 +# the Free Software Foundation; either version 2 of the License, or
2015 +# (at your option) any later version.
2017 +# This program is distributed in the hope that it will be useful,
2018 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2019 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020 +# GNU General Public License for more details.
2022 +# You should have received a copy of the GNU General Public License
2023 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
2025 +Export Aufs via NFS
2026 +----------------------------------------------------------------------
2027 +Here is an approach.
2028 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2030 +- iget_locked(): initialize aufs inode generation for a new inode, and
2031 + store it in xigen file.
2032 +- destroy_inode(): increment aufs inode generation and store it in xigen
2033 + file. it is necessary even if it is not unlinked, because any data of
2034 + inode may be changed by UDBA.
2035 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2036 + build file handle by
2037 + + branch id (4 bytes)
2038 + + superblock generation (4 bytes)
2039 + + inode number (4 or 8 bytes)
2040 + + parent dir inode number (4 or 8 bytes)
2041 + + inode generation (4 bytes))
2042 + + return value of exportfs_encode_fh() for the parent on a branch (4
2044 + + file handle for a branch (by exportfs_encode_fh())
2046 + + find the index of a branch from its id in handle, and check it is
2047 + still exist in aufs.
2048 + + 1st level: get the inode number from handle and search it in cache.
2049 + + 2nd level: if not found in cache, get the parent inode number from
2050 + the handle and search it in cache. and then open the found parent
2051 + dir, find the matching inode number by vfs_readdir() and get its
2052 + name, and call lookup_one_len() for the target dentry.
2053 + + 3rd level: if the parent dir is not cached, call
2054 + exportfs_decode_fh() for a branch and get the parent on a branch,
2055 + build a pathname of it, convert it a pathname in aufs, call
2056 + path_lookup(). now aufs gets a parent dir dentry, then handle it as
2058 + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2059 + for every branch, but not itself. to get this, (currently) aufs
2060 + searches in current->nsproxy->mnt_ns list. it may not be a good
2061 + idea, but I didn't get other approach.
2062 + + test the generation of the gotten inode.
2063 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2064 + convert it into ESTALE for NFSD.
2065 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2066 + lookup_one_len(), vfs_getattr(), encode_fh() and others.
2067 diff --git a/Documentation/filesystems/aufs/design/08shwh.txt b/Documentation/filesystems/aufs/design/08shwh.txt
2068 new file mode 100644
2069 index 00000000000000..67245e98e6b472
2071 +++ b/Documentation/filesystems/aufs/design/08shwh.txt
2074 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2076 +# This program is free software; you can redistribute it and/or modify
2077 +# it under the terms of the GNU General Public License as published by
2078 +# the Free Software Foundation; either version 2 of the License, or
2079 +# (at your option) any later version.
2081 +# This program is distributed in the hope that it will be useful,
2082 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2083 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2084 +# GNU General Public License for more details.
2086 +# You should have received a copy of the GNU General Public License
2087 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
2089 +Show Whiteout Mode (shwh)
2090 +----------------------------------------------------------------------
2091 +Generally aufs hides the name of whiteouts. But in some cases, to show
2092 +them is very useful for users. For instance, creating a new middle layer
2093 +(branch) by merging existing layers.
2095 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2096 +When you have three branches,
2097 +- Bottom: 'system', squashfs (underlying base system), read-only
2098 +- Middle: 'mods', squashfs, read-only
2099 +- Top: 'overlay', ram (tmpfs), read-write
2101 +The top layer is loaded at boot time and saved at shutdown, to preserve
2102 +the changes made to the system during the session.
2103 +When larger changes have been made, or smaller changes have accumulated,
2104 +the size of the saved top layer data grows. At this point, it would be
2105 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2106 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2107 +restoring save and load speed.
2109 +This merging is simplified by the use of another aufs mount, of just the
2110 +two overlay branches using the 'shwh' option.
2111 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2112 + aufs /livesys/merge_union
2114 +A merged view of these two branches is then available at
2115 +/livesys/merge_union, and the new feature is that the whiteouts are
2117 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2118 +writing to all branches. Also the default mode for all branches is 'ro'.
2119 +It is now possible to save the combined contents of the two overlay
2120 +branches to a new squashfs, e.g.:
2121 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2123 +This new squashfs archive can be stored on the boot device and the
2124 +initramfs will use it to replace the old one at the next boot.
2125 diff --git a/Documentation/filesystems/aufs/design/10dynop.txt b/Documentation/filesystems/aufs/design/10dynop.txt
2126 new file mode 100644
2127 index 00000000000000..da382ec2dc05d6
2129 +++ b/Documentation/filesystems/aufs/design/10dynop.txt
2132 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2134 +# This program is free software; you can redistribute it and/or modify
2135 +# it under the terms of the GNU General Public License as published by
2136 +# the Free Software Foundation; either version 2 of the License, or
2137 +# (at your option) any later version.
2139 +# This program is distributed in the hope that it will be useful,
2140 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2141 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2142 +# GNU General Public License for more details.
2144 +# You should have received a copy of the GNU General Public License
2145 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
2147 +Dynamically customizable FS operations
2148 +----------------------------------------------------------------------
2149 +Generally FS operations (struct inode_operations, struct
2150 +address_space_operations, struct file_operations, etc.) are defined as
2151 +"static const", but it never means that FS have only one set of
2152 +operation. Some FS have multiple sets of them. For instance, ext2 has
2153 +three sets, one for XIP, for NOBH, and for normal.
2154 +Since aufs overrides and redirects these operations, sometimes aufs has
2155 +to change its behaviour according to the branch FS type. More importantly
2156 +VFS acts differently if a function (member in the struct) is set or
2157 +not. It means aufs should have several sets of operations and select one
2158 +among them according to the branch FS definition.
2160 +In order to solve this problem and not to affect the behaviour of VFS,
2161 +aufs defines these operations dynamically. For instance, aufs defines
2162 +dummy direct_IO function for struct address_space_operations, but it may
2163 +not be set to the address_space_operations actually. When the branch FS
2164 +doesn't have it, aufs doesn't set it to its address_space_operations
2165 +while the function definition itself is still alive. So the behaviour
2166 +itself will not change, and it will return an error when direct_IO is
2169 +The lifetime of these dynamically generated operation object is
2170 +maintained by aufs branch object. When the branch is removed from aufs,
2171 +the reference counter of the object is decremented. When it reaches
2172 +zero, the dynamically generated operation object will be freed.
2174 +This approach is designed to support AIO (io_submit), Direct I/O and
2176 +Currently this approach is applied to address_space_operations for
2177 +regular files only.
2178 diff --git a/MAINTAINERS b/MAINTAINERS
2179 index a7c4cf8201e012..5b377eebc87952 100644
2182 @@ -3347,6 +3347,19 @@ F: include/uapi/linux/audit.h
2186 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
2187 +M: "J. R. Okajima" <hooanon05g@gmail.com>
2188 +L: aufs-users@lists.sourceforge.net (members only)
2189 +L: linux-unionfs@vger.kernel.org
2191 +W: http://aufs.sourceforge.net
2192 +T: git://github.com/sfjro/aufs4-linux.git
2193 +F: Documentation/ABI/testing/debugfs-aufs
2194 +F: Documentation/ABI/testing/sysfs-aufs
2195 +F: Documentation/filesystems/aufs/
2197 +F: include/uapi/linux/aufs_type.h
2199 AUXILIARY BUS DRIVER
2200 M: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2201 R: Dave Ertman <david.m.ertman@intel.com>
2202 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
2203 index 9f2d412fc560e1..1fefc6a8d049c7 100644
2204 --- a/drivers/block/loop.c
2205 +++ b/drivers/block/loop.c
2206 @@ -645,6 +645,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
2212 + * no get/put for file.
2214 +struct file *loop_backing_file(struct super_block *sb)
2217 + struct loop_device *l;
2220 + if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
2221 + l = sb->s_bdev->bd_disk->private_data;
2222 + ret = l->lo_backing_file;
2226 +EXPORT_SYMBOL_GPL(loop_backing_file);
2228 /* loop sysfs attributes */
2230 static ssize_t loop_attr_show(struct device *dev, char *page,
2231 diff --git a/fs/Kconfig b/fs/Kconfig
2232 index 42837617a55b54..6eaf5cde00fde8 100644
2235 @@ -333,6 +333,7 @@ source "fs/sysv/Kconfig"
2236 source "fs/ufs/Kconfig"
2237 source "fs/erofs/Kconfig"
2238 source "fs/vboxsf/Kconfig"
2239 +source "fs/aufs/Kconfig"
2241 endif # MISC_FILESYSTEMS
2243 diff --git a/fs/Makefile b/fs/Makefile
2244 index 75522f88e76367..8743be00607ec7 100644
2247 @@ -130,3 +130,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/
2248 obj-$(CONFIG_EROFS_FS) += erofs/
2249 obj-$(CONFIG_VBOXSF_FS) += vboxsf/
2250 obj-$(CONFIG_ZONEFS_FS) += zonefs/
2251 +obj-$(CONFIG_AUFS_FS) += aufs/
2252 diff --git a/fs/aufs/Kconfig b/fs/aufs/Kconfig
2253 new file mode 100644
2254 index 00000000000000..a5008b87a55f1f
2256 +++ b/fs/aufs/Kconfig
2258 +# SPDX-License-Identifier: GPL-2.0
2260 + tristate "Aufs (Advanced multi layered unification filesystem) support"
2262 + Aufs is a stackable unification filesystem such as Unionfs,
2263 + which unifies several directories and provides a merged single
2265 + In the early days, aufs was entirely re-designed and
2266 + re-implemented Unionfs Version 1.x series. Introducing many
2267 + original ideas, approaches and improvements, it becomes totally
2268 + different from Unionfs while keeping the basic features.
2272 + prompt "Maximum number of branches"
2273 + default AUFS_BRANCH_MAX_127
2275 + Specifies the maximum number of branches (or member directories)
2276 + in a single aufs. The larger value consumes more system
2277 + resources and has a minor impact to performance.
2278 +config AUFS_BRANCH_MAX_127
2281 + Specifies the maximum number of branches (or member directories)
2282 + in a single aufs. The larger value consumes more system
2283 + resources and has a minor impact to performance.
2284 +config AUFS_BRANCH_MAX_511
2287 + Specifies the maximum number of branches (or member directories)
2288 + in a single aufs. The larger value consumes more system
2289 + resources and has a minor impact to performance.
2290 +config AUFS_BRANCH_MAX_1023
2293 + Specifies the maximum number of branches (or member directories)
2294 + in a single aufs. The larger value consumes more system
2295 + resources and has a minor impact to performance.
2296 +config AUFS_BRANCH_MAX_32767
2299 + Specifies the maximum number of branches (or member directories)
2300 + in a single aufs. The larger value consumes more system
2301 + resources and has a minor impact to performance.
2304 +config AUFS_SBILIST
2306 + depends on AUFS_MAGIC_SYSRQ || PROC_FS
2309 + Automatic configuration for internal use.
2310 + When aufs supports Magic SysRq or /proc, enabled automatically.
2312 +config AUFS_HNOTIFY
2313 + bool "Detect direct branch access (bypassing aufs)"
2315 + If you want to modify files on branches directly, eg. bypassing aufs,
2316 + and want aufs to detect the changes of them fully, then enable this
2317 + option and use 'udba=notify' mount option.
2318 + Currently there is only one available configuration, "fsnotify".
2319 + It will have a negative impact to the performance.
2320 + See detail in aufs.5.
2323 + prompt "method" if AUFS_HNOTIFY
2324 + default AUFS_HFSNOTIFY
2325 +config AUFS_HFSNOTIFY
2331 + bool "NFS-exportable aufs"
2332 + depends on EXPORTFS
2334 + If you want to export your mounted aufs via NFS, then enable this
2335 + option. There are several requirements for this configuration.
2336 + See detail in aufs.5.
2338 +config AUFS_INO_T_64
2340 + depends on AUFS_EXPORT
2341 + depends on 64BIT && !(ALPHA || S390)
2344 + Automatic configuration for internal use.
2345 + /* typedef unsigned long/int __kernel_ino_t */
2346 + /* alpha and s390x are int */
2349 + bool "support for XATTR/EA (including Security Labels)"
2351 + If your branch fs supports XATTR/EA and you want to make them
2352 + available in aufs too, then enable this opsion and specify the
2353 + branch attributes for EA.
2354 + See detail in aufs.5.
2357 + bool "File-based Hierarchical Storage Management"
2359 + Hierarchical Storage Management (or HSM) is a well-known feature
2360 + in the storage world. Aufs provides this feature as file-based.
2361 + with multiple branches.
2362 + These multiple branches are prioritized, ie. the topmost one
2363 + should be the fastest drive and be used heavily.
2366 + bool "Readdir in userspace"
2368 + Aufs has two methods to provide a merged view for a directory,
2369 + by a user-space library and by kernel-space natively. The latter
2370 + is always enabled but sometimes large and slow.
2371 + If you enable this option, install the library in aufs2-util
2372 + package, and set some environment variables for your readdir(3),
2373 + then the work will be handled in user-space which generally
2374 + shows better performance in most cases.
2375 + See detail in aufs.5.
2378 + bool "Workaround for rename(2)-ing a directory"
2380 + By default, aufs returns EXDEV error in renameing a dir who has
2381 + his child on the lower branch, since it is a bad idea to issue
2382 + rename(2) internally for every lower branch. But user may not
2383 + accept this behaviour. So here is a workaround to allow such
2384 + rename(2) and store some extra information on the writable
2385 + branch. Obviously this costs high (and I don't like it).
2386 + To use this feature, you need to enable this configuration AND
2387 + to specify the mount option `dirren.'
2388 + See details in aufs.5 and the design documents.
2391 + bool "Show whiteouts"
2393 + If you want to make the whiteouts in aufs visible, then enable
2394 + this option and specify 'shwh' mount option. Although it may
2395 + sounds like philosophy or something, but in technically it
2396 + simply shows the name of whiteout with keeping its behaviour.
2398 +config AUFS_BR_RAMFS
2399 + bool "Ramfs (initramfs/rootfs) as an aufs branch"
2401 + If you want to use ramfs as an aufs branch fs, then enable this
2402 + option. Generally tmpfs is recommended.
2403 + Aufs prohibited them to be a branch fs by default, because
2404 + initramfs becomes unusable after switch_root or something
2405 + generally. If you sets initramfs as an aufs branch and boot your
2406 + system by switch_root, you will meet a problem easily since the
2407 + files in initramfs may be inaccessible.
2408 + Unless you are going to use ramfs as an aufs branch fs without
2409 + switch_root or something, leave it N.
2411 +config AUFS_BR_FUSE
2412 + bool "Fuse fs as an aufs branch"
2413 + depends on FUSE_FS
2416 + If you want to use fuse-based userspace filesystem as an aufs
2417 + branch fs, then enable this option.
2418 + It implements the internal poll(2) operation which is
2419 + implemented by fuse only (curretnly).
2424 + Automatic configuration for internal use.
2426 +config AUFS_BR_HFSPLUS
2427 + bool "Hfsplus as an aufs branch"
2428 + depends on HFSPLUS_FS
2431 + If you want to use hfsplus fs as an aufs branch fs, then enable
2432 + this option. This option introduces a small overhead at
2433 + copying-up a file on hfsplus.
2435 +config AUFS_BDEV_LOOP
2437 + depends on BLK_DEV_LOOP
2440 + Automatic configuration for internal use.
2441 + Convert =[ym] into =y.
2446 + Enable this to compile aufs internal debug code.
2447 + It will have a negative impact to the performance.
2449 +config AUFS_MAGIC_SYSRQ
2451 + depends on AUFS_DEBUG && MAGIC_SYSRQ
2454 + Automatic configuration for internal use.
2455 + When aufs supports Magic SysRq, enabled automatically.
2457 diff --git a/fs/aufs/Makefile b/fs/aufs/Makefile
2458 new file mode 100644
2459 index 00000000000000..4af8ecde3e3fad
2461 +++ b/fs/aufs/Makefile
2463 +# SPDX-License-Identifier: GPL-2.0
2465 +include ${src}/magic.mk
2466 +ifeq (${CONFIG_AUFS_FS},m)
2467 +include ${src}/conf.mk
2469 +-include ${src}/priv_def.mk
2471 +# cf. include/linux/kernel.h
2473 +ccflags-y += -DDEBUG
2474 +# sparse requires the full pathname
2476 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
2478 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
2481 +obj-$(CONFIG_AUFS_FS) += aufs.o
2482 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
2483 + wkq.o vfsub.o dcsub.o \
2484 + cpup.o whout.o wbr_policy.o \
2485 + dinfo.o dentry.o \
2487 + finfo.o file.o f_op.o \
2489 + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
2493 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
2494 +aufs-$(CONFIG_SYSFS) += sysfs.o
2495 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
2496 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
2497 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
2498 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
2499 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
2500 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
2501 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
2502 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
2503 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
2504 +aufs-$(CONFIG_AUFS_POLL) += poll.o
2505 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
2506 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
2507 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
2508 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
2509 diff --git a/fs/aufs/aufs.h b/fs/aufs/aufs.h
2510 new file mode 100644
2511 index 00000000000000..20430cf7904263
2513 +++ b/fs/aufs/aufs.h
2515 +/* SPDX-License-Identifier: GPL-2.0 */
2517 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2519 + * This program is free software; you can redistribute it and/or modify
2520 + * it under the terms of the GNU General Public License as published by
2521 + * the Free Software Foundation; either version 2 of the License, or
2522 + * (at your option) any later version.
2524 + * This program is distributed in the hope that it will be useful,
2525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2527 + * GNU General Public License for more details.
2529 + * You should have received a copy of the GNU General Public License
2530 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
2534 + * all header files
2542 +#define AuStub(type, name, body, ...) \
2543 + static inline type name(__VA_ARGS__) { body; }
2545 +#define AuStubVoid(name, ...) \
2546 + AuStub(void, name, , __VA_ARGS__)
2547 +#define AuStubInt0(name, ...) \
2548 + AuStub(int, name, return 0, __VA_ARGS__)
2552 +#include "branch.h"
2555 +#include "dbgaufs.h"
2556 +#include "dentry.h"
2558 +#include "dirren.h"
2561 +#include "fstype.h"
2566 +#include "module.h"
2570 +#include "sysaufs.h"
2575 +#endif /* __KERNEL__ */
2576 +#endif /* __AUFS_H__ */
2577 diff --git a/fs/aufs/branch.c b/fs/aufs/branch.c
2578 new file mode 100644
2579 index 00000000000000..2a85d744d2b5f7
2581 +++ b/fs/aufs/branch.c
2583 +// SPDX-License-Identifier: GPL-2.0
2585 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2587 + * This program is free software; you can redistribute it and/or modify
2588 + * it under the terms of the GNU General Public License as published by
2589 + * the Free Software Foundation; either version 2 of the License, or
2590 + * (at your option) any later version.
2592 + * This program is distributed in the hope that it will be useful,
2593 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2594 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2595 + * GNU General Public License for more details.
2597 + * You should have received a copy of the GNU General Public License
2598 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
2602 + * branch management
2605 +#include <linux/compat.h>
2606 +#include <linux/statfs.h>
2610 + * free a single branch
2612 +static void au_br_do_free(struct au_branch *br)
2615 + struct au_wbr *wbr;
2616 + struct au_dykey **key;
2618 + au_hnotify_fin_br(br);
2619 + /* always, regardless the mount option */
2620 + au_dr_hino_free(&br->br_dirren);
2623 + AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2624 + au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2625 + AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2626 + au_lcnt_fin(&br->br_count, /*do_sync*/0);
2630 + for (i = 0; i < AuBrWh_Last; i++)
2631 + dput(wbr->wbr_wh[i]);
2632 + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2633 + AuRwDestroy(&wbr->wbr_wh_rwsem);
2636 + if (br->br_fhsm) {
2637 + au_br_fhsm_fin(br->br_fhsm);
2638 + au_kfree_try_rcu(br->br_fhsm);
2641 + key = br->br_dykey;
2642 + for (i = 0; i < AuBrDynOp; i++, key++)
2648 + /* recursive lock, s_umount of branch's */
2649 + /* synchronize_rcu(); */ /* why? */
2651 + path_put(&br->br_path);
2653 + au_kfree_rcu(wbr);
2654 + au_lcnt_wait_for_fin(&br->br_nfiles);
2655 + au_lcnt_wait_for_fin(&br->br_count);
2656 + /* I don't know why, but percpu_refcount requires this */
2657 + /* synchronize_rcu(); */
2662 + * frees all branches
2664 +void au_br_free(struct au_sbinfo *sbinfo)
2666 + aufs_bindex_t bmax;
2667 + struct au_branch **br;
2669 + AuRwMustWriteLock(&sbinfo->si_rwsem);
2671 + bmax = sbinfo->si_bbot + 1;
2672 + br = sbinfo->si_branch;
2674 + au_br_do_free(*br++);
2678 + * find the index of a branch which is specified by @br_id.
2680 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2682 + aufs_bindex_t bindex, bbot;
2684 + bbot = au_sbbot(sb);
2685 + for (bindex = 0; bindex <= bbot; bindex++)
2686 + if (au_sbr_id(sb, bindex) == br_id)
2691 +/* ---------------------------------------------------------------------- */
2697 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2698 + struct dentry *h_root)
2700 + if (unlikely(h_adding == h_root
2701 + || au_test_loopback_overlap(sb, h_adding)))
2703 + if (h_adding->d_sb != h_root->d_sb)
2705 + return au_test_subdir(h_adding, h_root)
2706 + || au_test_subdir(h_root, h_adding);
2710 + * returns a newly allocated branch. @new_nbranch is a number of branches
2711 + * after adding a branch.
2713 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2716 + struct au_branch *add_branch;
2717 + struct dentry *root;
2718 + struct inode *inode;
2722 + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
2723 + if (unlikely(!add_branch))
2725 + add_branch->br_xino = au_xino_alloc(/*nfile*/1);
2726 + if (unlikely(!add_branch->br_xino))
2728 + err = au_hnotify_init_br(add_branch, perm);
2729 + if (unlikely(err))
2732 + if (au_br_writable(perm)) {
2733 + /* may be freed separately at changing the branch permission */
2734 + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
2736 + if (unlikely(!add_branch->br_wbr))
2740 + if (au_br_fhsm(perm)) {
2741 + err = au_fhsm_br_alloc(add_branch);
2742 + if (unlikely(err))
2746 + root = sb->s_root;
2747 + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
2749 + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
2751 + inode = d_inode(root);
2752 + err = au_hinode_realloc(au_ii(inode), new_nbranch,
2756 + return add_branch; /* success */
2759 + au_kfree_rcu(add_branch->br_wbr);
2761 + au_hnotify_fin_br(add_branch);
2763 + au_xino_put(add_branch);
2765 + au_kfree_rcu(add_branch);
2767 + return ERR_PTR(err);
2771 + * test if the branch permission is legal or not.
2773 +static int test_br(struct inode *inode, int brperm, char *path)
2777 + err = (au_br_writable(brperm) && IS_RDONLY(inode));
2782 + pr_err("write permission for readonly mount or inode, %s\n", path);
2790 + * 0: success, the caller will add it
2791 + * plus: success, it is already unified, the caller should ignore it
2794 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2797 + aufs_bindex_t bbot, bindex;
2798 + struct dentry *root, *h_dentry;
2799 + struct inode *inode, *h_inode;
2801 + root = sb->s_root;
2802 + bbot = au_sbbot(sb);
2803 + if (unlikely(bbot >= 0
2804 + && au_find_dbindex(root, add->path.dentry) >= 0)) {
2808 + pr_err("%s duplicated\n", add->pathname);
2813 + err = -ENOSPC; /* -E2BIG; */
2814 + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2815 + || AUFS_BRANCH_MAX - 1 <= bbot)) {
2816 + pr_err("number of branches exceeded %s\n", add->pathname);
2821 + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
2822 + pr_err("bad index %d\n", add->bindex);
2826 + inode = d_inode(add->path.dentry);
2828 + if (unlikely(!inode->i_nlink)) {
2829 + pr_err("no existence %s\n", add->pathname);
2834 + if (unlikely(inode->i_sb == sb)) {
2835 + pr_err("%s must be outside\n", add->pathname);
2839 + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2840 + pr_err("unsupported filesystem, %s (%s)\n",
2841 + add->pathname, au_sbtype(inode->i_sb));
2845 + if (unlikely(inode->i_sb->s_stack_depth)) {
2846 + pr_err("already stacked, %s (%s)\n",
2847 + add->pathname, au_sbtype(inode->i_sb));
2851 + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
2852 + if (unlikely(err))
2856 + return 0; /* success */
2859 + for (bindex = 0; bindex <= bbot; bindex++)
2860 + if (unlikely(test_overlap(sb, add->path.dentry,
2861 + au_h_dptr(root, bindex)))) {
2862 + pr_err("%s is overlapped\n", add->pathname);
2867 + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2868 + h_dentry = au_h_dptr(root, 0);
2869 + h_inode = d_inode(h_dentry);
2870 + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2871 + || !uid_eq(h_inode->i_uid, inode->i_uid)
2872 + || !gid_eq(h_inode->i_gid, inode->i_gid))
2873 + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2875 + i_uid_read(inode), i_gid_read(inode),
2876 + (inode->i_mode & S_IALLUGO),
2877 + i_uid_read(h_inode), i_gid_read(h_inode),
2878 + (h_inode->i_mode & S_IALLUGO));
2886 + * initialize or clean the whiteouts for an adding branch
2888 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2891 + int err, old_perm;
2892 + aufs_bindex_t bindex;
2893 + struct inode *h_inode;
2894 + struct au_wbr *wbr;
2895 + struct au_hinode *hdir;
2896 + struct dentry *h_dentry;
2898 + err = vfsub_mnt_want_write(au_br_mnt(br));
2899 + if (unlikely(err))
2903 + old_perm = br->br_perm;
2904 + br->br_perm = new_perm;
2907 + bindex = au_br_index(sb, br->br_id);
2908 + if (0 <= bindex) {
2909 + hdir = au_hi(d_inode(sb->s_root), bindex);
2910 + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
2912 + h_dentry = au_br_dentry(br);
2913 + h_inode = d_inode(h_dentry);
2914 + inode_lock_nested(h_inode, AuLsc_I_PARENT);
2917 + err = au_wh_init(br, sb);
2919 + wbr_wh_write_lock(wbr);
2920 + err = au_wh_init(br, sb);
2921 + wbr_wh_write_unlock(wbr);
2924 + au_hn_inode_unlock(hdir);
2926 + inode_unlock(h_inode);
2927 + vfsub_mnt_drop_write(au_br_mnt(br));
2928 + br->br_perm = old_perm;
2930 + if (!err && wbr && !au_br_writable(new_perm)) {
2931 + au_kfree_rcu(wbr);
2932 + br->br_wbr = NULL;
2939 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2943 + struct kstatfs kst;
2944 + struct au_wbr *wbr;
2947 + au_rw_init(&wbr->wbr_wh_rwsem);
2948 + atomic_set(&wbr->wbr_wh_running, 0);
2951 + * a limit for rmdir/rename a dir
2952 + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
2954 + err = vfs_statfs(&br->br_path, &kst);
2955 + if (unlikely(err))
2958 + if (kst.f_namelen >= NAME_MAX)
2959 + err = au_br_init_wh(sb, br, perm);
2961 + pr_err("%pd(%s), unsupported namelen %ld\n",
2963 + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
2969 +/* initialize a new branch */
2970 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2971 + struct au_opt_add *add)
2974 + struct au_branch *brbase;
2976 + struct inode *h_inode;
2979 + br->br_perm = add->perm;
2980 + br->br_path = add->path; /* set first, path_get() later */
2981 + spin_lock_init(&br->br_dykey_lock);
2982 + au_lcnt_init(&br->br_nfiles, /*release*/NULL);
2983 + au_lcnt_init(&br->br_count, /*release*/NULL);
2984 + br->br_id = au_new_br_id(sb);
2985 + AuDebugOn(br->br_id < 0);
2987 + /* always, regardless the given option */
2988 + err = au_dr_br_init(sb, br, &add->path);
2989 + if (unlikely(err))
2992 + if (au_br_writable(add->perm)) {
2993 + err = au_wbr_init(br, sb, add->perm);
2994 + if (unlikely(err))
2998 + if (au_opt_test(au_mntflags(sb), XINO)) {
2999 + brbase = au_sbr(sb, 0);
3000 + xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3002 + h_inode = d_inode(add->path.dentry);
3003 + err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3004 + if (unlikely(err)) {
3005 + AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3010 + sysaufs_br_init(br);
3011 + path_get(&br->br_path);
3012 + goto out; /* success */
3015 + memset(&br->br_path, 0, sizeof(br->br_path));
3020 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3021 + struct au_branch *br, aufs_bindex_t bbot,
3022 + aufs_bindex_t amount)
3024 + struct au_branch **brp;
3026 + AuRwMustWriteLock(&sbinfo->si_rwsem);
3028 + brp = sbinfo->si_branch + bindex;
3029 + memmove(brp + 1, brp, sizeof(*brp) * amount);
3031 + sbinfo->si_bbot++;
3032 + if (unlikely(bbot < 0))
3033 + sbinfo->si_bbot = 0;
3036 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3037 + aufs_bindex_t bbot, aufs_bindex_t amount)
3039 + struct au_hdentry *hdp;
3041 + AuRwMustWriteLock(&dinfo->di_rwsem);
3043 + hdp = au_hdentry(dinfo, bindex);
3044 + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3045 + au_h_dentry_init(hdp);
3047 + if (unlikely(bbot < 0))
3048 + dinfo->di_btop = 0;
3051 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3052 + aufs_bindex_t bbot, aufs_bindex_t amount)
3054 + struct au_hinode *hip;
3056 + AuRwMustWriteLock(&iinfo->ii_rwsem);
3058 + hip = au_hinode(iinfo, bindex);
3059 + memmove(hip + 1, hip, sizeof(*hip) * amount);
3060 + au_hinode_init(hip);
3062 + if (unlikely(bbot < 0))
3063 + iinfo->ii_btop = 0;
3066 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3067 + aufs_bindex_t bindex)
3069 + struct dentry *root, *h_dentry;
3070 + struct inode *root_inode, *h_inode;
3071 + aufs_bindex_t bbot, amount;
3073 + root = sb->s_root;
3074 + root_inode = d_inode(root);
3075 + bbot = au_sbbot(sb);
3076 + amount = bbot + 1 - bindex;
3077 + h_dentry = au_br_dentry(br);
3078 + au_sbilist_lock();
3079 + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3080 + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3081 + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3082 + au_set_h_dptr(root, bindex, dget(h_dentry));
3083 + h_inode = d_inode(h_dentry);
3084 + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3085 + au_sbilist_unlock();
3088 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3091 + aufs_bindex_t bbot, add_bindex;
3092 + struct dentry *root, *h_dentry;
3093 + struct inode *root_inode;
3094 + struct au_branch *add_branch;
3096 + root = sb->s_root;
3097 + root_inode = d_inode(root);
3098 + IMustLock(root_inode);
3099 + IiMustWriteLock(root_inode);
3100 + err = test_add(sb, add, remount);
3101 + if (unlikely(err < 0))
3105 + goto out; /* success */
3108 + bbot = au_sbbot(sb);
3109 + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3110 + err = PTR_ERR(add_branch);
3111 + if (IS_ERR(add_branch))
3114 + err = au_br_init(add_branch, sb, add);
3115 + if (unlikely(err)) {
3116 + au_br_do_free(add_branch);
3120 + add_bindex = add->bindex;
3121 + sysaufs_brs_del(sb, add_bindex); /* remove successors */
3122 + au_br_do_add(sb, add_branch, add_bindex);
3123 + sysaufs_brs_add(sb, add_bindex); /* append successors */
3124 + dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
3126 + h_dentry = add->path.dentry;
3127 + if (!add_bindex) {
3128 + au_cpup_attr_all(root_inode, /*force*/1);
3129 + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3131 + au_add_nlink(root_inode, d_inode(h_dentry));
3137 +/* ---------------------------------------------------------------------- */
3139 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3140 + unsigned long long max __maybe_unused,
3143 + unsigned long long n;
3144 + struct file **p, *f;
3145 + struct hlist_bl_head *files;
3146 + struct hlist_bl_node *pos;
3147 + struct au_finfo *finfo;
3151 + files = &au_sbi(sb)->si_files;
3152 + hlist_bl_lock(files);
3153 + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3154 + f = finfo->fi_file;
3156 + && !special_file(file_inode(f)->i_mode)) {
3160 + AuDebugOn(n > max);
3163 + hlist_bl_unlock(files);
3168 +static struct file **au_farray_alloc(struct super_block *sb,
3169 + unsigned long long *max)
3171 + struct au_sbinfo *sbi;
3174 + *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3175 + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3178 +static void au_farray_free(struct file **a, unsigned long long max)
3180 + unsigned long long ull;
3182 + for (ull = 0; ull < max; ull++)
3188 +/* ---------------------------------------------------------------------- */
3194 +/* to show the line number, do not make it inlined function */
3195 +#define AuVerbose(do_info, fmt, ...) do { \
3197 + pr_info(fmt, ##__VA_ARGS__); \
3200 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3201 + aufs_bindex_t bbot)
3203 + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3206 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3207 + aufs_bindex_t bbot)
3209 + return au_test_ibusy(d_inode(dentry), btop, bbot);
3213 + * test if the branch is deletable or not.
3215 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3216 + unsigned int sigen, const unsigned int verbose)
3218 + int err, i, j, ndentry;
3219 + aufs_bindex_t btop, bbot;
3220 + struct au_dcsub_pages dpages;
3221 + struct au_dpage *dpage;
3224 + err = au_dpages_init(&dpages, GFP_NOFS);
3225 + if (unlikely(err))
3227 + err = au_dcsub_pages(&dpages, root, NULL, NULL);
3228 + if (unlikely(err))
3231 + for (i = 0; !err && i < dpages.ndpage; i++) {
3232 + dpage = dpages.dpages + i;
3233 + ndentry = dpage->ndentry;
3234 + for (j = 0; !err && j < ndentry; j++) {
3235 + d = dpage->dentries[j];
3236 + AuDebugOn(au_dcount(d) <= 0);
3237 + if (!au_digen_test(d, sigen)) {
3238 + di_read_lock_child(d, AuLock_IR);
3239 + if (unlikely(au_dbrange_test(d))) {
3240 + di_read_unlock(d, AuLock_IR);
3244 + di_write_lock_child(d);
3245 + if (unlikely(au_dbrange_test(d))) {
3246 + di_write_unlock(d);
3249 + err = au_reval_dpath(d, sigen);
3251 + di_downgrade_lock(d, AuLock_IR);
3253 + di_write_unlock(d);
3258 + /* AuDbgDentry(d); */
3259 + btop = au_dbtop(d);
3260 + bbot = au_dbbot(d);
3261 + if (btop <= bindex
3263 + && au_h_dptr(d, bindex)
3264 + && au_test_dbusy(d, btop, bbot)) {
3266 + AuVerbose(verbose, "busy %pd\n", d);
3269 + di_read_unlock(d, AuLock_IR);
3274 + au_dpages_free(&dpages);
3279 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3280 + unsigned int sigen, const unsigned int verbose)
3283 + unsigned long long max, ull;
3284 + struct inode *i, **array;
3285 + aufs_bindex_t btop, bbot;
3287 + array = au_iarray_alloc(sb, &max);
3288 + err = PTR_ERR(array);
3289 + if (IS_ERR(array))
3293 + AuDbg("b%d\n", bindex);
3294 + for (ull = 0; !err && ull < max; ull++) {
3298 + if (i->i_ino == AUFS_ROOT_INO)
3301 + /* AuDbgInode(i); */
3302 + if (au_iigen(i, NULL) == sigen)
3303 + ii_read_lock_child(i);
3305 + ii_write_lock_child(i);
3306 + err = au_refresh_hinode_self(i);
3309 + ii_downgrade_lock(i);
3311 + ii_write_unlock(i);
3316 + btop = au_ibtop(i);
3317 + bbot = au_ibbot(i);
3318 + if (btop <= bindex
3320 + && au_h_iptr(i, bindex)
3321 + && au_test_ibusy(i, btop, bbot)) {
3323 + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3326 + ii_read_unlock(i);
3328 + au_iarray_free(array, max);
3334 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3335 + const unsigned int verbose)
3338 + unsigned int sigen;
3340 + sigen = au_sigen(root->d_sb);
3341 + DiMustNoWaiters(root);
3342 + IiMustNoWaiters(d_inode(root));
3343 + di_write_unlock(root);
3344 + err = test_dentry_busy(root, bindex, sigen, verbose);
3346 + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3347 + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3352 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3353 + struct file **to_free, int *idx)
3356 + unsigned char matched, root;
3357 + aufs_bindex_t bindex, bbot;
3358 + struct au_fidir *fidir;
3359 + struct au_hfile *hfile;
3362 + root = IS_ROOT(file->f_path.dentry);
3365 + to_free[*idx] = file;
3371 + fidir = au_fi(file)->fi_hdir;
3372 + AuDebugOn(!fidir);
3373 + bbot = au_fbbot_dir(file);
3374 + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3375 + hfile = fidir->fd_hfile + bindex;
3376 + if (!hfile->hf_file)
3379 + if (hfile->hf_br->br_id == br_id) {
3391 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3392 + struct file **to_free, int opened)
3395 + unsigned long long ull, max;
3396 + aufs_bindex_t btop;
3397 + struct file *file, **array;
3398 + struct dentry *root;
3399 + struct au_hfile *hfile;
3401 + array = au_farray_alloc(sb, &max);
3402 + err = PTR_ERR(array);
3403 + if (IS_ERR(array))
3408 + root = sb->s_root;
3409 + di_write_unlock(root);
3410 + for (ull = 0; ull < max; ull++) {
3411 + file = array[ull];
3412 + if (unlikely(!file))
3415 + /* AuDbg("%pD\n", file); */
3416 + fi_read_lock(file);
3417 + btop = au_fbtop(file);
3418 + if (!d_is_dir(file->f_path.dentry)) {
3419 + hfile = &au_fi(file)->fi_htop;
3420 + if (hfile->hf_br->br_id == br_id)
3423 + err = test_dir_busy(file, br_id, to_free, &idx);
3424 + fi_read_unlock(file);
3425 + if (unlikely(err))
3428 + di_write_lock_child(root);
3429 + au_farray_free(array, max);
3430 + AuDebugOn(idx > opened);
3436 +static void br_del_file(struct file **to_free, unsigned long long opened,
3437 + aufs_bindex_t br_id)
3439 + unsigned long long ull;
3440 + aufs_bindex_t bindex, btop, bbot, bfound;
3441 + struct file *file;
3442 + struct au_fidir *fidir;
3443 + struct au_hfile *hfile;
3445 + for (ull = 0; ull < opened; ull++) {
3446 + file = to_free[ull];
3447 + if (unlikely(!file))
3450 + /* AuDbg("%pD\n", file); */
3451 + AuDebugOn(!d_is_dir(file->f_path.dentry));
3453 + fidir = au_fi(file)->fi_hdir;
3454 + AuDebugOn(!fidir);
3455 + fi_write_lock(file);
3456 + btop = au_fbtop(file);
3457 + bbot = au_fbbot_dir(file);
3458 + for (bindex = btop; bindex <= bbot; bindex++) {
3459 + hfile = fidir->fd_hfile + bindex;
3460 + if (!hfile->hf_file)
3463 + if (hfile->hf_br->br_id == br_id) {
3468 + AuDebugOn(bfound < 0);
3469 + au_set_h_fptr(file, bfound, NULL);
3470 + if (bfound == btop) {
3471 + for (btop++; btop <= bbot; btop++)
3472 + if (au_hf_dir(file, btop)) {
3473 + au_set_fbtop(file, btop);
3477 + fi_write_unlock(file);
3481 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3482 + const aufs_bindex_t bindex,
3483 + const aufs_bindex_t bbot)
3485 + struct au_branch **brp, **p;
3487 + AuRwMustWriteLock(&sbinfo->si_rwsem);
3489 + brp = sbinfo->si_branch + bindex;
3490 + if (bindex < bbot)
3491 + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3492 + sbinfo->si_branch[0 + bbot] = NULL;
3493 + sbinfo->si_bbot--;
3495 + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3498 + sbinfo->si_branch = p;
3499 + /* harmless error */
3502 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3503 + const aufs_bindex_t bbot)
3505 + struct au_hdentry *hdp, *p;
3507 + AuRwMustWriteLock(&dinfo->di_rwsem);
3509 + hdp = au_hdentry(dinfo, bindex);
3510 + if (bindex < bbot)
3511 + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3512 + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3515 + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3518 + dinfo->di_hdentry = p;
3519 + /* harmless error */
3522 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3523 + const aufs_bindex_t bbot)
3525 + struct au_hinode *hip, *p;
3527 + AuRwMustWriteLock(&iinfo->ii_rwsem);
3529 + hip = au_hinode(iinfo, bindex);
3530 + if (bindex < bbot)
3531 + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3532 + /* au_hinode_init(au_hinode(iinfo, bbot)); */
3535 + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3538 + iinfo->ii_hinode = p;
3539 + /* harmless error */
3542 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3543 + struct au_branch *br)
3545 + aufs_bindex_t bbot;
3546 + struct au_sbinfo *sbinfo;
3547 + struct dentry *root, *h_root;
3548 + struct inode *inode, *h_inode;
3549 + struct au_hinode *hinode;
3551 + SiMustWriteLock(sb);
3553 + root = sb->s_root;
3554 + inode = d_inode(root);
3555 + sbinfo = au_sbi(sb);
3556 + bbot = sbinfo->si_bbot;
3558 + h_root = au_h_dptr(root, bindex);
3559 + hinode = au_hi(inode, bindex);
3560 + h_inode = au_igrab(hinode->hi_inode);
3563 + au_sbilist_lock();
3564 + au_br_do_del_brp(sbinfo, bindex, bbot);
3565 + au_br_do_del_hdp(au_di(root), bindex, bbot);
3566 + au_br_do_del_hip(au_ii(inode), bindex, bbot);
3567 + au_sbilist_unlock();
3569 + /* ignore an error */
3570 + au_dr_br_fin(sb, br); /* always, regardless the mount option */
3574 + au_br_do_free(br);
3577 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3578 + unsigned long long max, void *arg)
3583 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3586 + unsigned long long opened;
3587 + unsigned int mnt_flags;
3588 + aufs_bindex_t bindex, bbot, br_id;
3589 + unsigned char do_wh, verbose;
3590 + struct au_branch *br;
3591 + struct au_wbr *wbr;
3592 + struct dentry *root;
3593 + struct file **to_free;
3598 + root = sb->s_root;
3599 + bindex = au_find_dbindex(root, del->h_path.dentry);
3602 + goto out; /* success */
3604 + pr_err("%s no such branch\n", del->pathname);
3607 + AuDbg("bindex b%d\n", bindex);
3610 + mnt_flags = au_mntflags(sb);
3611 + verbose = !!au_opt_test(mnt_flags, VERBOSE);
3612 + bbot = au_sbbot(sb);
3613 + if (unlikely(!bbot)) {
3614 + AuVerbose(verbose, "no more branches left\n");
3618 + br = au_sbr(sb, bindex);
3619 + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3620 + if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3621 + AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3625 + br_id = br->br_id;
3626 + opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3627 + if (unlikely(opened)) {
3628 + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3629 + err = PTR_ERR(to_free);
3630 + if (IS_ERR(to_free))
3633 + err = test_file_busy(sb, br_id, to_free, opened);
3634 + if (unlikely(err)) {
3635 + AuVerbose(verbose, "%llu file(s) opened\n", opened);
3641 + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3643 + /* instead of WbrWhMustWriteLock(wbr) */
3644 + SiMustWriteLock(sb);
3645 + for (i = 0; i < AuBrWh_Last; i++) {
3646 + dput(wbr->wbr_wh[i]);
3647 + wbr->wbr_wh[i] = NULL;
3651 + err = test_children_busy(root, bindex, verbose);
3652 + if (unlikely(err)) {
3661 + * now we confirmed the branch is deletable.
3662 + * let's free the remaining opened dirs on the branch.
3664 + di_write_unlock(root);
3665 + br_del_file(to_free, opened, br_id);
3666 + di_write_lock_child(root);
3669 + sysaufs_brs_del(sb, bindex); /* remove successors */
3670 + dbgaufs_xino_del(br); /* remove one */
3671 + au_br_do_del(sb, bindex, br);
3672 + sysaufs_brs_add(sb, bindex); /* append successors */
3673 + dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
3676 + au_cpup_attr_all(d_inode(root), /*force*/1);
3677 + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3679 + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3680 + if (au_opt_test(mnt_flags, PLINK))
3681 + au_plink_half_refresh(sb, br_id);
3683 + goto out; /* success */
3687 + rerr = au_br_init_wh(sb, br, br->br_perm);
3689 + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3690 + del->pathname, rerr);
3693 + au_farray_free(to_free, opened);
3697 +/* ---------------------------------------------------------------------- */
3699 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3702 + aufs_bindex_t btop, bbot;
3703 + struct aufs_ibusy ibusy;
3704 + struct inode *inode, *h_inode;
3707 + if (unlikely(!capable(CAP_SYS_ADMIN)))
3710 + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3712 + /* VERIFY_WRITE */
3713 + err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
3714 + if (unlikely(err)) {
3721 + si_read_lock(sb, AuLock_FLUSH);
3722 + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
3726 + ibusy.h_ino = 0; /* invalid */
3727 + inode = ilookup(sb, ibusy.ino);
3729 + || inode->i_ino == AUFS_ROOT_INO
3730 + || au_is_bad_inode(inode))
3733 + ii_read_lock_child(inode);
3734 + btop = au_ibtop(inode);
3735 + bbot = au_ibbot(inode);
3736 + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
3737 + h_inode = au_h_iptr(inode, ibusy.bindex);
3738 + if (h_inode && au_test_ibusy(inode, btop, bbot))
3739 + ibusy.h_ino = h_inode->i_ino;
3741 + ii_read_unlock(inode);
3745 + si_read_unlock(sb);
3747 + err = __put_user(ibusy.h_ino, &arg->h_ino);
3748 + if (unlikely(err)) {
3757 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3759 + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3762 +#ifdef CONFIG_COMPAT
3763 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3765 + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3769 +/* ---------------------------------------------------------------------- */
3772 + * change a branch permission
3775 +static void au_warn_ima(void)
3778 + /* since it doesn't support mark_files_ro() */
3779 + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3783 +static int do_need_sigen_inc(int a, int b)
3785 + return au_br_whable(a) && !au_br_whable(b);
3788 +static int need_sigen_inc(int old, int new)
3790 + return do_need_sigen_inc(old, new)
3791 + || do_need_sigen_inc(new, old);
3794 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3797 + unsigned int mnt_flags;
3798 + unsigned long long ull, max;
3799 + aufs_bindex_t br_id;
3800 + unsigned char verbose, writer;
3801 + struct file *file, *hf, **array;
3802 + struct au_hfile *hfile;
3803 + struct inode *h_inode;
3805 + mnt_flags = au_mntflags(sb);
3806 + verbose = !!au_opt_test(mnt_flags, VERBOSE);
3808 + array = au_farray_alloc(sb, &max);
3809 + err = PTR_ERR(array);
3810 + if (IS_ERR(array))
3814 + br_id = au_sbr_id(sb, bindex);
3815 + for (ull = 0; ull < max; ull++) {
3816 + file = array[ull];
3817 + if (unlikely(!file))
3820 + /* AuDbg("%pD\n", file); */
3821 + fi_read_lock(file);
3822 + if (unlikely(au_test_mmapped(file))) {
3824 + AuVerbose(verbose, "mmapped %pD\n", file);
3826 + FiMustNoWaiters(file);
3827 + fi_read_unlock(file);
3831 + hfile = &au_fi(file)->fi_htop;
3832 + hf = hfile->hf_file;
3833 + if (!d_is_reg(file->f_path.dentry)
3834 + || !(file->f_mode & FMODE_WRITE)
3835 + || hfile->hf_br->br_id != br_id
3836 + || !(hf->f_mode & FMODE_WRITE))
3837 + array[ull] = NULL;
3843 + FiMustNoWaiters(file);
3844 + fi_read_unlock(file);
3852 + for (ull = 0; ull < max; ull++) {
3853 + file = array[ull];
3857 + /* todo: already flushed? */
3859 + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
3860 + * approach which resets f_mode and calls mnt_drop_write() and
3861 + * file_release_write() for each file, because the branch
3862 + * attribute in aufs world is totally different from the native
3865 + /* fi_read_lock(file); */
3866 + hfile = &au_fi(file)->fi_htop;
3867 + hf = hfile->hf_file;
3868 + /* fi_read_unlock(file); */
3869 + spin_lock(&hf->f_lock);
3870 + writer = !!(hf->f_mode & FMODE_WRITER);
3871 + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
3872 + spin_unlock(&hf->f_lock);
3874 + h_inode = file_inode(hf);
3875 + if (hf->f_mode & FMODE_READ)
3876 + i_readcount_inc(h_inode);
3877 + put_write_access(h_inode);
3878 + __mnt_drop_write(hf->f_path.mnt);
3883 + au_farray_free(array, max);
3889 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3893 + aufs_bindex_t bindex;
3894 + struct dentry *root;
3895 + struct au_branch *br;
3896 + struct au_br_fhsm *bf;
3898 + root = sb->s_root;
3899 + bindex = au_find_dbindex(root, mod->h_root);
3902 + return 0; /* success */
3904 + pr_err("%s no such branch\n", mod->path);
3907 + AuDbg("bindex b%d\n", bindex);
3909 + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
3910 + if (unlikely(err))
3913 + br = au_sbr(sb, bindex);
3914 + AuDebugOn(mod->h_root != au_br_dentry(br));
3915 + if (br->br_perm == mod->perm)
3916 + return 0; /* success */
3918 + /* pre-allocate for non-fhsm --> fhsm */
3920 + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
3921 + err = au_fhsm_br_alloc(br);
3922 + if (unlikely(err))
3925 + br->br_fhsm = NULL;
3928 + if (au_br_writable(br->br_perm)) {
3929 + /* remove whiteout base */
3930 + err = au_br_init_wh(sb, br, mod->perm);
3931 + if (unlikely(err))
3934 + if (!au_br_writable(mod->perm)) {
3935 + /* rw --> ro, file might be mmapped */
3936 + DiMustNoWaiters(root);
3937 + IiMustNoWaiters(d_inode(root));
3938 + di_write_unlock(root);
3939 + err = au_br_mod_files_ro(sb, bindex);
3940 + /* aufs_write_lock() calls ..._child() */
3941 + di_write_lock_child(root);
3943 + if (unlikely(err)) {
3945 + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
3948 + rerr = au_wbr_init(br, sb, br->br_perm);
3949 + if (unlikely(rerr)) {
3950 + AuIOErr("nested error %d (%d)\n",
3952 + br->br_perm = mod->perm;
3956 + } else if (au_br_writable(mod->perm)) {
3959 + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
3961 + err = au_wbr_init(br, sb, mod->perm);
3962 + if (unlikely(err)) {
3963 + au_kfree_rcu(br->br_wbr);
3964 + br->br_wbr = NULL;
3968 + if (unlikely(err))
3971 + if (au_br_fhsm(br->br_perm)) {
3972 + if (!au_br_fhsm(mod->perm)) {
3973 + /* fhsm --> non-fhsm */
3974 + au_br_fhsm_fin(br->br_fhsm);
3975 + au_kfree_rcu(br->br_fhsm);
3976 + br->br_fhsm = NULL;
3978 + } else if (au_br_fhsm(mod->perm))
3979 + /* non-fhsm --> fhsm */
3982 + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
3983 + br->br_perm = mod->perm;
3984 + goto out; /* success */
3987 + au_kfree_try_rcu(bf);
3993 +/* ---------------------------------------------------------------------- */
3995 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
3998 + struct kstatfs kstfs;
4000 + err = vfs_statfs(&br->br_path, &kstfs);
4002 + stfs->f_blocks = kstfs.f_blocks;
4003 + stfs->f_bavail = kstfs.f_bavail;
4004 + stfs->f_files = kstfs.f_files;
4005 + stfs->f_ffree = kstfs.f_ffree;
4010 diff --git a/fs/aufs/branch.h b/fs/aufs/branch.h
4011 new file mode 100644
4012 index 00000000000000..6818ba8df8a24f
4014 +++ b/fs/aufs/branch.h
4016 +/* SPDX-License-Identifier: GPL-2.0 */
4018 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4020 + * This program is free software; you can redistribute it and/or modify
4021 + * it under the terms of the GNU General Public License as published by
4022 + * the Free Software Foundation; either version 2 of the License, or
4023 + * (at your option) any later version.
4025 + * This program is distributed in the hope that it will be useful,
4026 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4027 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4028 + * GNU General Public License for more details.
4030 + * You should have received a copy of the GNU General Public License
4031 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
4035 + * branch filesystems and xino for them
4038 +#ifndef __AUFS_BRANCH_H__
4039 +#define __AUFS_BRANCH_H__
4043 +#include <linux/mount.h>
4044 +#include "dirren.h"
4050 +/* ---------------------------------------------------------------------- */
4054 + struct file **xi_file;
4055 + unsigned int xi_nfile;
4061 + /* reserved for future use */
4062 + /* unsigned long *bitmap; */
4063 + wait_queue_head_t wqh;
4066 + struct mutex xi_mtx; /* protects xi_file array */
4067 + struct hlist_bl_head xi_writing;
4069 + atomic_t xi_truncating;
4071 + struct kref xi_kref;
4074 +/* File-based Hierarchical Storage Management */
4075 +struct au_br_fhsm {
4076 +#ifdef CONFIG_AUFS_FHSM
4077 + struct mutex bf_lock;
4078 + unsigned long bf_jiffy;
4079 + struct aufs_stfs bf_stfs;
4084 +/* members for writable branch only */
4085 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4087 + struct au_rwsem wbr_wh_rwsem;
4088 + struct dentry *wbr_wh[AuBrWh_Last];
4089 + atomic_t wbr_wh_running;
4090 +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4091 +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4092 +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4095 + unsigned long long wbr_bytes;
4098 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4099 +#define AuBrDynOp (AuDyLast * 4)
4101 +#ifdef CONFIG_AUFS_HFSNOTIFY
4102 +/* support for asynchronous destruction */
4103 +struct au_br_hfsnotify {
4104 + struct fsnotify_group *hfsn_group;
4108 +/* sysfs entries */
4109 +struct au_brsysfs {
4111 + struct attribute attr;
4120 +/* protected by superblock rwsem */
4122 + struct au_xino *br_xino;
4124 + aufs_bindex_t br_id;
4127 + struct path br_path;
4128 + spinlock_t br_dykey_lock;
4129 + struct au_dykey *br_dykey[AuBrDynOp];
4130 + au_lcnt_t br_nfiles; /* opened files */
4131 + au_lcnt_t br_count; /* in-use for other */
4133 + struct au_wbr *br_wbr;
4134 + struct au_br_fhsm *br_fhsm;
4136 +#ifdef CONFIG_AUFS_HFSNOTIFY
4137 + struct au_br_hfsnotify *br_hfsn;
4140 +#ifdef CONFIG_SYSFS
4141 + /* entries under sysfs per mount-point */
4142 + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
4145 +#ifdef CONFIG_DEBUG_FS
4146 + struct dentry *br_dbgaufs; /* xino */
4149 + struct au_dr_br br_dirren;
4152 +/* ---------------------------------------------------------------------- */
4154 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4156 + return br->br_path.mnt;
4159 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4161 + return br->br_path.dentry;
4164 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4166 + return mnt_idmap(br->br_path.mnt);
4169 +static inline struct super_block *au_br_sb(struct au_branch *br)
4171 + return au_br_mnt(br)->mnt_sb;
4174 +static inline int au_br_rdonly(struct au_branch *br)
4176 + return (sb_rdonly(au_br_sb(br))
4177 + || !au_br_writable(br->br_perm))
4181 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4183 +#ifdef CONFIG_AUFS_HNOTIFY
4184 + return !(brperm & AuBrPerm_RR);
4190 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4192 + int err, exec_flag;
4195 + exec_flag = oflag & __FMODE_EXEC;
4196 + if (unlikely(exec_flag && path_noexec(&br->br_path)))
4202 +static inline void au_xino_get(struct au_branch *br)
4204 + struct au_xino *xi;
4208 + kref_get(&xi->xi_kref);
4211 +static inline int au_xino_count(struct au_branch *br)
4214 + struct au_xino *xi;
4219 + v = kref_read(&xi->xi_kref);
4224 +/* ---------------------------------------------------------------------- */
4228 +void au_br_free(struct au_sbinfo *sinfo);
4229 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4231 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4233 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4234 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4235 +#ifdef CONFIG_COMPAT
4236 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4239 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4242 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4245 +static const loff_t au_loff_max = LLONG_MAX;
4247 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4248 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4250 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4251 + struct file *copy_src);
4253 + struct au_xino *xi; /* switch between xino and xigen */
4255 + struct path *base;
4256 + struct file *copy_src;
4258 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4260 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4262 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4264 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4265 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4267 +int au_xib_trunc(struct super_block *sb);
4268 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4270 +struct au_xino *au_xino_alloc(unsigned int nfile);
4271 +int au_xino_put(struct au_branch *br);
4272 +struct file *au_xino_file1(struct au_xino *xi);
4274 +struct au_opt_xino;
4275 +void au_xino_clr(struct super_block *sb);
4276 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4277 +struct file *au_xino_def(struct super_block *sb);
4278 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4279 + struct path *base);
4281 +ino_t au_xino_new_ino(struct super_block *sb);
4282 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4284 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4285 + ino_t h_ino, int idx);
4286 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4289 +int au_xino_path(struct seq_file *seq, struct file *file);
4291 +/* ---------------------------------------------------------------------- */
4293 +/* @idx is signed to accept -1 meaning the first file */
4294 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4296 + struct file *file;
4303 + if (idx < xi->xi_nfile)
4304 + file = xi->xi_file[idx];
4306 + file = au_xino_file1(xi);
4312 +/* ---------------------------------------------------------------------- */
4314 +/* Superblock to branch */
4316 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4318 + return au_sbr(sb, bindex)->br_id;
4322 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4324 + return au_br_mnt(au_sbr(sb, bindex));
4328 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4330 + return au_br_idmap(au_sbr(sb, bindex));
4334 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4336 + return au_br_sb(au_sbr(sb, bindex));
4339 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4341 + return au_sbr(sb, bindex)->br_perm;
4344 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4346 + return au_br_whable(au_sbr_perm(sb, bindex));
4349 +/* ---------------------------------------------------------------------- */
4351 +#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4352 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4353 +#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4354 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4356 +#define wbr_wh_read_trylock_nested(wbr) \
4357 + au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4358 +#define wbr_wh_write_trylock_nested(wbr) \
4359 + au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4362 +#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4363 +#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4364 +#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4366 +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4367 +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4368 +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4370 +/* ---------------------------------------------------------------------- */
4372 +#ifdef CONFIG_AUFS_FHSM
4373 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4375 + mutex_init(&brfhsm->bf_lock);
4376 + brfhsm->bf_jiffy = 0;
4377 + brfhsm->bf_readable = 0;
4380 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4382 + mutex_destroy(&brfhsm->bf_lock);
4385 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4386 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4389 +#endif /* __KERNEL__ */
4390 +#endif /* __AUFS_BRANCH_H__ */
4391 diff --git a/fs/aufs/conf.mk b/fs/aufs/conf.mk
4392 new file mode 100644
4393 index 00000000000000..12782f8e0f3819
4395 +++ b/fs/aufs/conf.mk
4397 +# SPDX-License-Identifier: GPL-2.0
4399 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4403 +AuConfStr += ${1}=${${1}}
4407 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4409 + HNOTIFY HFSNOTIFY \
4421 +$(foreach i, ${AuConfAll}, \
4422 + $(eval $(call AuConf,CONFIG_AUFS_${i})))
4424 +AuConfName = ${obj}/conf.str
4425 +${AuConfName}.tmp: FORCE
4426 + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4427 +${AuConfName}: ${AuConfName}.tmp
4428 + @diff -q $< $@ > /dev/null 2>&1 || { \
4429 + echo ' GEN ' $@; \
4433 +clean-files += ${AuConfName} ${AuConfName}.tmp
4434 +${obj}/sysfs.o: ${AuConfName}
4436 +-include ${srctree}/${src}/conf_priv.mk
4437 diff --git a/fs/aufs/cpup.c b/fs/aufs/cpup.c
4438 new file mode 100644
4439 index 00000000000000..8c8bd5f7b28d81
4441 +++ b/fs/aufs/cpup.c
4443 +// SPDX-License-Identifier: GPL-2.0
4445 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4447 + * This program is free software; you can redistribute it and/or modify
4448 + * it under the terms of the GNU General Public License as published by
4449 + * the Free Software Foundation; either version 2 of the License, or
4450 + * (at your option) any later version.
4452 + * This program is distributed in the hope that it will be useful,
4453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4455 + * GNU General Public License for more details.
4457 + * You should have received a copy of the GNU General Public License
4458 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
4462 + * copy-up functions, see wbr_policy.c for copy-down
4465 +#include <linux/fs_stack.h>
4466 +#include <linux/mm.h>
4467 +#include <linux/task_work.h>
4470 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4472 + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4473 + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4475 + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4477 + dst->i_flags |= iflags & ~mask;
4478 + if (au_test_fs_notime(dst->i_sb))
4479 + dst->i_flags |= S_NOATIME | S_NOCMTIME;
4482 +void au_cpup_attr_timesizes(struct inode *inode)
4484 + struct inode *h_inode;
4486 + h_inode = au_h_iptr(inode, au_ibtop(inode));
4487 + fsstack_copy_attr_times(inode, h_inode);
4488 + fsstack_copy_inode_size(inode, h_inode);
4491 +void au_cpup_attr_nlink(struct inode *inode, int force)
4493 + struct inode *h_inode;
4494 + struct super_block *sb;
4495 + aufs_bindex_t bindex, bbot;
4498 + bindex = au_ibtop(inode);
4499 + h_inode = au_h_iptr(inode, bindex);
4501 + && !S_ISDIR(h_inode->i_mode)
4502 + && au_opt_test(au_mntflags(sb), PLINK)
4503 + && au_plink_test(inode))
4507 + * 0 can happen in revalidating.
4508 + * h_inode->i_mutex may not be held here, but it is harmless since once
4509 + * i_nlink reaches 0, it will never become positive except O_TMPFILE
4511 + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4512 + * the incorrect link count.
4514 + set_nlink(inode, h_inode->i_nlink);
4517 + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4518 + * it may includes whplink directory.
4520 + if (S_ISDIR(h_inode->i_mode)) {
4521 + bbot = au_ibbot(inode);
4522 + for (bindex++; bindex <= bbot; bindex++) {
4523 + h_inode = au_h_iptr(inode, bindex);
4525 + au_add_nlink(inode, h_inode);
4530 +void au_cpup_attr_changeable(struct inode *inode)
4532 + struct inode *h_inode;
4534 + h_inode = au_h_iptr(inode, au_ibtop(inode));
4535 + inode->i_mode = h_inode->i_mode;
4536 + inode->i_uid = h_inode->i_uid;
4537 + inode->i_gid = h_inode->i_gid;
4538 + au_cpup_attr_timesizes(inode);
4539 + au_cpup_attr_flags(inode, h_inode->i_flags);
4542 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4544 + struct au_iinfo *iinfo = au_ii(inode);
4546 + IiMustWriteLock(inode);
4548 + iinfo->ii_higen = h_inode->i_generation;
4549 + iinfo->ii_hsb1 = h_inode->i_sb;
4552 +void au_cpup_attr_all(struct inode *inode, int force)
4554 + struct inode *h_inode;
4556 + h_inode = au_h_iptr(inode, au_ibtop(inode));
4557 + au_cpup_attr_changeable(inode);
4558 + if (inode->i_nlink > 0)
4559 + au_cpup_attr_nlink(inode, force);
4560 + inode->i_rdev = h_inode->i_rdev;
4561 + inode->i_blkbits = h_inode->i_blkbits;
4562 + au_cpup_igen(inode, h_inode);
4565 +/* ---------------------------------------------------------------------- */
4567 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4569 +/* keep the timestamps of the parent dir when cpup */
4570 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4571 + struct path *h_path)
4573 + struct inode *h_inode;
4575 + dt->dt_dentry = dentry;
4576 + dt->dt_h_path = *h_path;
4577 + h_inode = d_inode(h_path->dentry);
4578 + dt->dt_atime = h_inode->i_atime;
4579 + dt->dt_mtime = h_inode->i_mtime;
4583 +void au_dtime_revert(struct au_dtime *dt)
4585 + struct iattr attr;
4588 + attr.ia_atime = dt->dt_atime;
4589 + attr.ia_mtime = dt->dt_mtime;
4590 + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4591 + | ATTR_ATIME | ATTR_ATIME_SET;
4593 + /* no delegation since this is a directory */
4594 + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4595 + if (unlikely(err))
4596 + pr_warn("restoring timestamps failed(%d). ignored\n", err);
4599 +/* ---------------------------------------------------------------------- */
4601 +/* internal use only */
4602 +struct au_cpup_reg_attr {
4605 + unsigned int iflags; /* inode->i_flags */
4608 +static noinline_for_stack
4609 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4610 + struct au_cpup_reg_attr *h_src_attr)
4612 + int err, sbits, icex;
4613 + unsigned int mnt_flags;
4614 + unsigned char verbose;
4616 + struct path h_path;
4617 + struct inode *h_isrc, *h_idst;
4618 + struct kstat *h_st;
4619 + struct au_branch *br;
4621 + br = au_sbr(dst->d_sb, bindex);
4622 + h_path.mnt = au_br_mnt(br);
4623 + h_path.dentry = au_h_dptr(dst, bindex);
4624 + h_idst = d_inode(h_path.dentry);
4625 + h_isrc = d_inode(h_src->dentry);
4626 + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4627 + | ATTR_ATIME | ATTR_MTIME
4628 + | ATTR_ATIME_SET | ATTR_MTIME_SET;
4629 + if (h_src_attr && h_src_attr->valid) {
4630 + h_st = &h_src_attr->st;
4631 + ia.ia_uid = h_st->uid;
4632 + ia.ia_gid = h_st->gid;
4633 + ia.ia_atime = h_st->atime;
4634 + ia.ia_mtime = h_st->mtime;
4635 + if (h_idst->i_mode != h_st->mode
4636 + && !S_ISLNK(h_idst->i_mode)) {
4637 + ia.ia_valid |= ATTR_MODE;
4638 + ia.ia_mode = h_st->mode;
4640 + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4641 + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4643 + ia.ia_uid = h_isrc->i_uid;
4644 + ia.ia_gid = h_isrc->i_gid;
4645 + ia.ia_atime = h_isrc->i_atime;
4646 + ia.ia_mtime = h_isrc->i_mtime;
4647 + if (h_idst->i_mode != h_isrc->i_mode
4648 + && !S_ISLNK(h_idst->i_mode)) {
4649 + ia.ia_valid |= ATTR_MODE;
4650 + ia.ia_mode = h_isrc->i_mode;
4652 + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4653 + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4655 + /* no delegation since it is just created */
4656 + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4658 + /* is this nfs only? */
4659 + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4660 + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4661 + ia.ia_mode = h_isrc->i_mode;
4662 + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4665 + icex = br->br_perm & AuBrAttr_ICEX;
4667 + mnt_flags = au_mntflags(dst->d_sb);
4668 + verbose = !!au_opt_test(mnt_flags, VERBOSE);
4669 + err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4675 +/* ---------------------------------------------------------------------- */
4677 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4678 + char *buf, unsigned long blksize)
4681 + size_t sz, rbytes, wbytes;
4682 + unsigned char all_zero;
4684 + struct inode *h_inode;
4685 + /* reduce stack usage */
4688 + zp = page_address(ZERO_PAGE(0));
4689 + if (unlikely(!zp))
4690 + return -ENOMEM; /* possible? */
4695 + AuDbg("len %lld\n", len);
4697 + if (len < blksize)
4701 + /* todo: signal_pending? */
4702 + while (!rbytes || err == -EAGAIN || err == -EINTR) {
4703 + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4706 + if (unlikely(err < 0))
4710 + if (len >= rbytes && rbytes == blksize)
4711 + all_zero = !memcmp(buf, zp, rbytes);
4718 + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4720 + /* todo: signal_pending? */
4721 + if (unlikely(err == -EAGAIN || err == -EINTR))
4723 + if (unlikely(err < 0))
4728 + if (unlikely(err < 0))
4734 + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4736 + if (unlikely(res < 0))
4743 + /* the last block may be a hole */
4744 + if (!err && all_zero) {
4745 + AuLabel(last hole);
4748 + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4749 + /* nfs requires this step to make last hole */
4750 + /* is this only nfs? */
4752 + /* todo: signal_pending? */
4753 + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4754 + } while (err == -EAGAIN || err == -EINTR);
4761 + ia->ia_size = dst->f_pos;
4762 + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4763 + ia->ia_file = dst;
4764 + h_inode = file_inode(dst);
4765 + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
4766 + /* no delegation since it is just created */
4767 + err = vfsub_notify_change(&dst->f_path, ia,
4768 + /*delegated*/NULL);
4769 + inode_unlock(h_inode);
4776 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4779 + unsigned long blksize;
4780 + unsigned char do_kfree;
4782 + struct super_block *h_sb;
4785 + h_sb = file_inode(dst)->i_sb;
4786 + blksize = h_sb->s_blocksize;
4787 + if (!blksize || PAGE_SIZE < blksize)
4788 + blksize = PAGE_SIZE;
4789 + AuDbg("blksize %lu\n", blksize);
4790 + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4792 + buf = kmalloc(blksize, GFP_NOFS);
4794 + buf = (void *)__get_free_page(GFP_NOFS);
4795 + if (unlikely(!buf))
4798 + if (len > (1 << 22))
4799 + AuDbg("copying a large file %lld\n", (long long)len);
4803 + err = au_do_copy_file(dst, src, len, buf, blksize);
4805 + AuDebugOn(!au_kfree_do_sz_test(blksize));
4806 + au_kfree_do_rcu(buf);
4808 + free_page((unsigned long)buf);
4814 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
4817 + struct super_block *h_src_sb;
4818 + struct inode *h_src_inode;
4820 + h_src_inode = file_inode(src);
4821 + h_src_sb = h_src_inode->i_sb;
4823 + /* XFS acquires inode_lock */
4824 + if (!au_test_xfs(h_src_sb))
4825 + err = au_copy_file(dst, src, len);
4827 + inode_unlock_shared(h_src_inode);
4828 + err = au_copy_file(dst, src, len);
4829 + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
4835 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
4839 + struct super_block *h_src_sb;
4840 + struct inode *h_src_inode;
4842 + h_src_inode = file_inode(src);
4843 + h_src_sb = h_src_inode->i_sb;
4844 + if (h_src_sb != file_inode(dst)->i_sb
4845 + || !dst->f_op->remap_file_range) {
4846 + err = au_do_copy(dst, src, len);
4850 + if (!au_test_nfs(h_src_sb)) {
4851 + inode_unlock_shared(h_src_inode);
4852 + lo = vfsub_clone_file_range(src, dst, len);
4853 + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
4855 + lo = vfsub_clone_file_range(src, dst, len);
4858 + goto out; /* success */
4859 + } else if (lo >= 0)
4860 + /* todo: possible? */
4861 + /* paritially succeeded */
4862 + AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
4863 + else if (lo != -EOPNOTSUPP) {
4864 + /* older XFS has a condition in cloning */
4869 + /* the backend fs on NFS may not support cloning */
4870 + err = au_do_copy(dst, src, len);
4878 + * to support a sparse file which is opened with O_APPEND,
4879 + * we need to close the file.
4881 +static int au_cp_regular(struct au_cp_generic *cpg)
4884 + enum { SRC, DST };
4886 + aufs_bindex_t bindex;
4887 + unsigned int flags;
4888 + struct dentry *dentry;
4890 + struct file *file;
4893 + .bindex = cpg->bsrc,
4894 + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4897 + .bindex = cpg->bdst,
4898 + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4899 + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4902 + struct au_branch *br;
4903 + struct super_block *sb, *h_src_sb;
4904 + struct inode *h_src_inode;
4905 + struct task_struct *tsk = current;
4907 + /* bsrc branch can be ro/rw. */
4908 + sb = cpg->dentry->d_sb;
4910 + for (i = 0; i < 2; i++, f++) {
4911 + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4912 + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4913 + /*file*/NULL, f->force_wr);
4914 + if (IS_ERR(f->file)) {
4915 + err = PTR_ERR(f->file);
4923 + /* try stopping to update while we copyup */
4924 + h_src_inode = d_inode(file[SRC].dentry);
4925 + h_src_sb = h_src_inode->i_sb;
4926 + if (!au_test_nfs(h_src_sb))
4927 + IMustLock(h_src_inode);
4928 + err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
4930 + /* i wonder if we had O_NO_DELAY_FPUT flag */
4931 + if (tsk->flags & PF_KTHREAD)
4932 + __fput_sync(file[DST].file);
4934 + /* it happened actually */
4935 + fput(file[DST].file);
4938 + * we have to call both since we don't know which place the file
4942 + flush_delayed_fput();
4944 + br = au_sbr(sb, file[DST].bindex);
4945 + au_lcnt_dec(&br->br_nfiles);
4948 + fput(file[SRC].file);
4949 + br = au_sbr(sb, file[SRC].bindex);
4950 + au_lcnt_dec(&br->br_nfiles);
4955 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4956 + struct au_cpup_reg_attr *h_src_attr)
4960 + struct path h_path;
4961 + struct inode *h_src_inode, *h_dst_inode;
4964 + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
4965 + l = i_size_read(h_src_inode);
4966 + if (cpg->len == -1 || l < cpg->len)
4969 + /* try stopping to update while we are referencing */
4970 + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
4971 + au_pin_hdir_unlock(cpg->pin);
4973 + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4974 + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4975 + h_src_attr->iflags = h_src_inode->i_flags;
4976 + if (!au_test_nfs(h_src_inode->i_sb))
4977 + err = vfsub_getattr(&h_path, &h_src_attr->st);
4979 + inode_unlock_shared(h_src_inode);
4980 + err = vfsub_getattr(&h_path, &h_src_attr->st);
4981 + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
4983 + if (unlikely(err)) {
4984 + inode_unlock_shared(h_src_inode);
4987 + h_src_attr->valid = 1;
4988 + if (!au_test_nfs(h_src_inode->i_sb)) {
4989 + err = au_cp_regular(cpg);
4990 + inode_unlock_shared(h_src_inode);
4992 + inode_unlock_shared(h_src_inode);
4993 + err = au_cp_regular(cpg);
4995 + rerr = au_pin_hdir_relock(cpg->pin);
4999 + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5000 + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5001 + h_dst_inode = d_inode(h_path.dentry);
5002 + spin_lock(&h_dst_inode->i_lock);
5003 + h_dst_inode->i_state |= I_LINKABLE;
5004 + spin_unlock(&h_dst_inode->i_lock);
5011 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5012 + struct inode *h_dir)
5015 + DEFINE_DELAYED_CALL(done);
5018 + sym = vfs_get_link(h_src, &done);
5019 + err = PTR_ERR(sym);
5023 + err = vfsub_symlink(h_dir, h_path, sym);
5026 + do_delayed_call(&done);
5031 + * regardless 'acl' option, reset all ACL.
5032 + * All ACL will be copied up later from the original entry on the lower branch.
5034 +static int au_reset_acl(struct path *h_path, umode_t mode)
5037 + struct dentry *h_dentry;
5038 + /* struct inode *h_inode; */
5039 + struct mnt_idmap *h_idmap;
5041 + h_idmap = mnt_idmap(h_path->mnt);
5042 + h_dentry = h_path->dentry;
5043 + /* h_inode = d_inode(h_dentry); */
5044 + /* forget_all_cached_acls(h_inode)); */
5045 + err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5047 + if (err == -EOPNOTSUPP)
5050 + err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5056 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5057 + struct inode *h_dir, struct path *h_path)
5060 + struct inode *dir, *inode;
5061 + struct mnt_idmap *h_idmap;
5063 + h_idmap = mnt_idmap(h_path->mnt);
5064 + err = vfsub_remove_acl(h_idmap, h_path->dentry,
5065 + XATTR_NAME_POSIX_ACL_DEFAULT);
5067 + if (err == -EOPNOTSUPP)
5069 + if (unlikely(err))
5073 + * strange behaviour from the users view,
5074 + * particularly setattr case
5076 + dir = d_inode(dst_parent);
5077 + if (au_ibtop(dir) == cpg->bdst)
5078 + au_cpup_attr_nlink(dir, /*force*/1);
5079 + inode = d_inode(cpg->dentry);
5080 + au_cpup_attr_nlink(inode, /*force*/1);
5086 +static noinline_for_stack
5087 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5088 + struct au_cpup_reg_attr *h_src_attr)
5092 + unsigned int mnt_flags;
5093 + unsigned char isdir, isreg, force;
5094 + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5095 + struct au_dtime dt;
5096 + struct path h_path;
5097 + struct dentry *h_src, *h_dst, *h_parent;
5098 + struct inode *h_inode, *h_dir;
5099 + struct super_block *sb;
5101 + /* bsrc branch can be ro/rw. */
5102 + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5103 + h_inode = d_inode(h_src);
5104 + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5106 + /* try stopping to be referenced while we are creating */
5107 + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5108 + if (au_ftest_cpup(cpg->flags, RENAME))
5109 + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5110 + AUFS_WH_PFX_LEN));
5111 + h_parent = h_dst->d_parent; /* dir inode is locked */
5112 + h_dir = d_inode(h_parent);
5114 + AuDebugOn(h_parent != h_dst->d_parent);
5116 + sb = cpg->dentry->d_sb;
5117 + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5119 + h_path.dentry = h_parent;
5120 + au_dtime_store(&dt, dst_parent, &h_path);
5122 + h_path.dentry = h_dst;
5126 + mode = h_inode->i_mode;
5127 + switch (mode & S_IFMT) {
5130 + err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5132 + err = au_do_cpup_regular(cpg, h_src_attr);
5136 + err = vfsub_mkdir(h_dir, &h_path, mode);
5138 + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5141 + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5145 + AuDebugOn(!capable(CAP_MKNOD));
5149 + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5152 + AuIOErr("Unknown inode type 0%o\n", mode);
5156 + err = au_reset_acl(&h_path, mode);
5158 + mnt_flags = au_mntflags(sb);
5159 + if (!au_opt_test(mnt_flags, UDBA_NONE)
5161 + && au_opt_test(mnt_flags, XINO)
5162 + && (h_inode->i_nlink == 1
5163 + || (h_inode->i_state & I_LINKABLE))
5164 + /* todo: unnecessary? */
5165 + /* && d_inode(cpg->dentry)->i_nlink == 1 */
5166 + && cpg->bdst < cpg->bsrc
5167 + && !au_ftest_cpup(cpg->flags, KEEPLINO))
5168 + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5169 + /* ignore this error */
5174 + force = !!cpg->len;
5175 + if (cpg->len == -1)
5176 + force = !!i_size_read(h_inode);
5178 + au_fhsm_wrote(sb, cpg->bdst, force);
5182 + au_dtime_revert(&dt);
5186 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5189 + struct dentry *dentry, *h_dentry, *h_parent, *parent;
5190 + struct path h_ppath;
5191 + struct inode *h_dir;
5192 + aufs_bindex_t bdst;
5194 + dentry = cpg->dentry;
5196 + h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5197 + h_dentry = au_h_dptr(dentry, bdst);
5198 + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5200 + au_set_h_dptr(dentry, bdst, NULL);
5201 + err = au_lkup_neg(dentry, bdst, /*wh*/0);
5203 + h_path->dentry = dget(au_h_dptr(dentry, bdst));
5204 + au_set_h_dptr(dentry, bdst, h_dentry);
5207 + parent = dget_parent(dentry);
5208 + h_ppath.dentry = au_h_dptr(parent, bdst);
5210 + h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5211 + if (IS_ERR(h_path->dentry))
5212 + err = PTR_ERR(h_path->dentry);
5214 + if (unlikely(err))
5217 + h_parent = h_dentry->d_parent; /* dir inode is locked */
5218 + h_dir = d_inode(h_parent);
5220 + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5221 + /* no delegation since it is just created */
5222 + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5224 + dput(h_path->dentry);
5231 + * copyup the @dentry from @bsrc to @bdst.
5232 + * the caller must set the both of lower dentries.
5233 + * @len is for truncating when it is -1 copyup the entire file.
5234 + * in link/rename cases, @dst_parent may be different from the real one.
5235 + * basic->bsrc can be larger than basic->bdst.
5236 + * aufs doesn't touch the credential so
5237 + * security_inode_copy_up{,_xattr}() are unnecessary.
5239 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5242 + aufs_bindex_t old_ibtop;
5243 + unsigned char isdir, plink;
5244 + struct dentry *h_src, *h_dst, *h_parent;
5245 + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5246 + struct super_block *sb;
5247 + struct au_branch *br;
5248 + struct path h_src_path;
5249 + /* to reduce stack size */
5251 + struct au_dtime dt;
5252 + struct path h_path;
5253 + struct au_cpup_reg_attr h_src_attr;
5257 + a = kmalloc(sizeof(*a), GFP_NOFS);
5260 + a->h_src_attr.valid = 0;
5262 + sb = cpg->dentry->d_sb;
5263 + br = au_sbr(sb, cpg->bdst);
5264 + a->h_path.mnt = au_br_mnt(br);
5265 + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5266 + h_parent = h_dst->d_parent; /* dir inode is locked */
5267 + h_dir = d_inode(h_parent);
5270 + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5271 + inode = d_inode(cpg->dentry);
5274 + dst_parent = dget_parent(cpg->dentry);
5278 + plink = !!au_opt_test(au_mntflags(sb), PLINK);
5279 + dst_inode = au_h_iptr(inode, cpg->bdst);
5281 + if (unlikely(!plink)) {
5283 + AuIOErr("hi%lu(i%lu) exists on b%d "
5284 + "but plink is disabled\n",
5285 + dst_inode->i_ino, inode->i_ino, cpg->bdst);
5289 + if (dst_inode->i_nlink) {
5290 + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5292 + h_src = au_plink_lkup(inode, cpg->bdst);
5293 + err = PTR_ERR(h_src);
5294 + if (IS_ERR(h_src))
5296 + if (unlikely(d_is_negative(h_src))) {
5298 + AuIOErr("i%lu exists on b%d "
5299 + "but not pseudo-linked\n",
5300 + inode->i_ino, cpg->bdst);
5306 + a->h_path.dentry = h_parent;
5307 + au_dtime_store(&a->dt, dst_parent, &a->h_path);
5310 + a->h_path.dentry = h_dst;
5312 + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5313 + if (!err && au_ftest_cpup(cpg->flags, RENAME))
5314 + err = au_do_ren_after_cpup(cpg, &a->h_path);
5316 + au_dtime_revert(&a->dt);
5317 + if (unlikely(err == -EWOULDBLOCK)) {
5318 + pr_warn("cannot retry for NFSv4 delegation"
5319 + " for an internal link\n");
5325 + /* todo: cpup_wh_file? */
5327 + au_update_ibrange(inode, /*do_put_zero*/1);
5330 + isdir = S_ISDIR(inode->i_mode);
5331 + old_ibtop = au_ibtop(inode);
5332 + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5333 + if (unlikely(err))
5335 + dst_inode = d_inode(h_dst);
5336 + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5337 + /* todo: necessary? */
5338 + /* au_pin_hdir_unlock(cpg->pin); */
5340 + h_src_path.dentry = h_src;
5341 + h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5342 + err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5343 + if (unlikely(err)) {
5344 + /* todo: necessary? */
5345 + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5346 + inode_unlock(dst_inode);
5350 + if (cpg->bdst < old_ibtop) {
5351 + if (S_ISREG(inode->i_mode)) {
5352 + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5353 + if (unlikely(err)) {
5354 + /* ignore an error */
5355 + /* au_pin_hdir_relock(cpg->pin); */
5356 + inode_unlock(dst_inode);
5360 + au_set_ibtop(inode, cpg->bdst);
5362 + au_set_ibbot(inode, cpg->bdst);
5363 + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5364 + au_hi_flags(inode, isdir));
5366 + /* todo: necessary? */
5367 + /* err = au_pin_hdir_relock(cpg->pin); */
5368 + inode_unlock(dst_inode);
5369 + if (unlikely(err))
5372 + src_inode = d_inode(h_src);
5374 + && (src_inode->i_nlink > 1
5375 + || src_inode->i_state & I_LINKABLE)
5377 + au_plink_append(inode, cpg->bdst, h_dst);
5379 + if (au_ftest_cpup(cpg->flags, RENAME)) {
5380 + a->h_path.dentry = h_dst;
5381 + err = au_do_ren_after_cpup(cpg, &a->h_path);
5384 + goto out_parent; /* success */
5388 + a->h_path.dentry = h_parent;
5389 + au_dtime_store(&a->dt, dst_parent, &a->h_path);
5390 + a->h_path.dentry = h_dst;
5392 + if (d_is_positive(h_dst)) {
5394 + /* no delegation since it is just created */
5395 + rerr = vfsub_unlink(h_dir, &a->h_path,
5396 + /*delegated*/NULL, /*force*/0);
5398 + rerr = vfsub_rmdir(h_dir, &a->h_path);
5400 + au_dtime_revert(&a->dt);
5402 + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5412 +#if 0 /* reserved */
5413 +struct au_cpup_single_args {
5415 + struct au_cp_generic *cpg;
5416 + struct dentry *dst_parent;
5419 +static void au_call_cpup_single(void *args)
5421 + struct au_cpup_single_args *a = args;
5423 + au_pin_hdir_acquire_nest(a->cpg->pin);
5424 + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5425 + au_pin_hdir_release(a->cpg->pin);
5430 + * prevent SIGXFSZ in copy-up.
5431 + * testing CAP_MKNOD is for generic fs,
5432 + * but CAP_FSETID is for xfs only, currently.
5434 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5437 + struct super_block *sb;
5438 + struct inode *h_dir;
5441 + sb = au_pinned_parent(pin)->d_sb;
5442 + if (!au_wkq_test()
5443 + && (!au_sbi(sb)->si_plink_maint_pid
5444 + || au_plink_maint(sb, AuLock_NOPLM))) {
5445 + switch (mode & S_IFMT) {
5447 + /* no condition about RLIMIT_FSIZE and the file size */
5452 + do_sio = !capable(CAP_MKNOD);
5456 + do_sio = ((mode & (S_ISUID | S_ISGID))
5457 + && !capable(CAP_FSETID));
5458 + /* this workaround may be removed in the future */
5460 + h_dir = au_pinned_h_dir(pin);
5461 + do_sio = h_dir->i_mode & S_ISVTX;
5468 +#if 0 /* reserved */
5469 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5472 + struct dentry *h_dentry;
5474 + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5475 + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5476 + err = au_cpup_single(cpg, dst_parent);
5478 + struct au_cpup_single_args args = {
5481 + .dst_parent = dst_parent
5483 + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5484 + if (unlikely(wkq_err))
5493 + * copyup the @dentry from the first active lower branch to @bdst,
5494 + * using au_cpup_single().
5496 +static int au_cpup_simple(struct au_cp_generic *cpg)
5499 + unsigned int flags_orig;
5500 + struct dentry *dentry;
5502 + AuDebugOn(cpg->bsrc < 0);
5504 + dentry = cpg->dentry;
5505 + DiMustWriteLock(dentry);
5507 + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5509 + flags_orig = cpg->flags;
5510 + au_fset_cpup(cpg->flags, RENAME);
5511 + err = au_cpup_single(cpg, NULL);
5512 + cpg->flags = flags_orig;
5514 + return 0; /* success */
5517 + au_set_h_dptr(dentry, cpg->bdst, NULL);
5518 + au_set_dbtop(dentry, cpg->bsrc);
5524 +struct au_cpup_simple_args {
5526 + struct au_cp_generic *cpg;
5529 +static void au_call_cpup_simple(void *args)
5531 + struct au_cpup_simple_args *a = args;
5533 + au_pin_hdir_acquire_nest(a->cpg->pin);
5534 + *a->errp = au_cpup_simple(a->cpg);
5535 + au_pin_hdir_release(a->cpg->pin);
5538 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5541 + struct dentry *dentry, *parent;
5542 + struct file *h_file;
5543 + struct inode *h_dir;
5544 + struct mnt_idmap *h_idmap;
5546 + dentry = cpg->dentry;
5548 + if (au_ftest_cpup(cpg->flags, HOPEN)) {
5549 + AuDebugOn(cpg->bsrc < 0);
5550 + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5551 + err = PTR_ERR(h_file);
5552 + if (IS_ERR(h_file))
5556 + parent = dget_parent(dentry);
5557 + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5558 + h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5559 + if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5560 + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5561 + err = au_cpup_simple(cpg);
5563 + struct au_cpup_simple_args args = {
5567 + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5568 + if (unlikely(wkq_err))
5574 + au_h_open_post(dentry, cpg->bsrc, h_file);
5580 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5582 + aufs_bindex_t bsrc, bbot;
5583 + struct dentry *dentry, *h_dentry;
5585 + if (cpg->bsrc < 0) {
5586 + dentry = cpg->dentry;
5587 + bbot = au_dbbot(dentry);
5588 + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5589 + h_dentry = au_h_dptr(dentry, bsrc);
5591 + AuDebugOn(d_is_negative(h_dentry));
5595 + AuDebugOn(bsrc > bbot);
5598 + AuDebugOn(cpg->bsrc <= cpg->bdst);
5599 + return au_do_sio_cpup_simple(cpg);
5602 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5604 + AuDebugOn(cpg->bdst <= cpg->bsrc);
5605 + return au_do_sio_cpup_simple(cpg);
5608 +/* ---------------------------------------------------------------------- */
5611 + * copyup the deleted file for writing.
5613 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5614 + struct file *file)
5617 + unsigned int flags_orig;
5618 + aufs_bindex_t bsrc_orig;
5619 + struct au_dinfo *dinfo;
5621 + struct au_hdentry *hd;
5622 + struct dentry *h_dentry;
5625 + dinfo = au_di(cpg->dentry);
5626 + AuRwMustWriteLock(&dinfo->di_rwsem);
5628 + bsrc_orig = cpg->bsrc;
5629 + cpg->bsrc = dinfo->di_btop;
5630 + hdst.hd = au_hdentry(dinfo, cpg->bdst);
5631 + hdst.h_dentry = hdst.hd->hd_dentry;
5632 + hdst.hd->hd_dentry = wh_dentry;
5633 + dinfo->di_btop = cpg->bdst;
5635 + hsrc.h_dentry = NULL;
5637 + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5638 + hsrc.h_dentry = hsrc.hd->hd_dentry;
5639 + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5641 + flags_orig = cpg->flags;
5642 + cpg->flags = !AuCpup_DTIME;
5643 + err = au_cpup_single(cpg, /*h_parent*/NULL);
5644 + cpg->flags = flags_orig;
5647 + err = au_reopen_nondir(file);
5648 + hsrc.hd->hd_dentry = hsrc.h_dentry;
5650 + hdst.hd->hd_dentry = hdst.h_dentry;
5651 + dinfo->di_btop = cpg->bsrc;
5652 + cpg->bsrc = bsrc_orig;
5657 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5660 + aufs_bindex_t bdst;
5661 + struct au_dtime dt;
5662 + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5663 + struct au_branch *br;
5664 + struct path h_path;
5666 + dentry = cpg->dentry;
5668 + br = au_sbr(dentry->d_sb, bdst);
5669 + parent = dget_parent(dentry);
5670 + h_parent = au_h_dptr(parent, bdst);
5671 + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5672 + err = PTR_ERR(wh_dentry);
5673 + if (IS_ERR(wh_dentry))
5676 + h_path.dentry = h_parent;
5677 + h_path.mnt = au_br_mnt(br);
5678 + au_dtime_store(&dt, parent, &h_path);
5679 + err = au_do_cpup_wh(cpg, wh_dentry, file);
5680 + if (unlikely(err))
5684 + h_path.dentry = wh_dentry;
5685 + if (!d_is_dir(wh_dentry)) {
5686 + /* no delegation since it is just created */
5687 + err = vfsub_unlink(d_inode(h_parent), &h_path,
5688 + /*delegated*/NULL, /*force*/0);
5690 + err = vfsub_rmdir(d_inode(h_parent), &h_path);
5691 + if (unlikely(err)) {
5692 + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5696 + au_dtime_revert(&dt);
5697 + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5706 +struct au_cpup_wh_args {
5708 + struct au_cp_generic *cpg;
5709 + struct file *file;
5712 +static void au_call_cpup_wh(void *args)
5714 + struct au_cpup_wh_args *a = args;
5716 + au_pin_hdir_acquire_nest(a->cpg->pin);
5717 + *a->errp = au_cpup_wh(a->cpg, a->file);
5718 + au_pin_hdir_release(a->cpg->pin);
5721 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5724 + aufs_bindex_t bdst;
5725 + struct dentry *dentry, *parent, *h_orph, *h_parent;
5726 + struct inode *dir, *h_dir, *h_tmpdir;
5727 + struct au_wbr *wbr;
5728 + struct au_pin wh_pin, *pin_orig;
5729 + struct mnt_idmap *h_idmap;
5731 + dentry = cpg->dentry;
5733 + parent = dget_parent(dentry);
5734 + dir = d_inode(parent);
5737 + h_dir = au_igrab(au_h_iptr(dir, bdst));
5740 + if (!h_dir->i_nlink) {
5741 + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5742 + h_orph = wbr->wbr_orph;
5744 + h_parent = dget(au_h_dptr(parent, bdst));
5745 + au_set_h_dptr(parent, bdst, dget(h_orph));
5746 + h_tmpdir = d_inode(h_orph);
5747 + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5749 + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
5750 + /* todo: au_h_open_pre()? */
5752 + pin_orig = cpg->pin;
5753 + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5754 + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5755 + cpg->pin = &wh_pin;
5758 + h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
5759 + if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
5760 + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5761 + err = au_cpup_wh(cpg, file);
5763 + struct au_cpup_wh_args args = {
5768 + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5769 + if (unlikely(wkq_err))
5774 + inode_unlock(h_tmpdir);
5775 + /* todo: au_h_open_post()? */
5776 + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5777 + au_set_h_dptr(parent, bdst, h_parent);
5778 + AuDebugOn(!pin_orig);
5779 + cpg->pin = pin_orig;
5787 +/* ---------------------------------------------------------------------- */
5790 + * generic routine for both of copy-up and copy-down.
5792 +/* cf. revalidate function in file.c */
5793 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5794 + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5795 + struct au_pin *pin,
5796 + struct dentry *h_parent, void *arg),
5800 + struct au_pin pin;
5801 + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
5804 + parent = dget_parent(dentry);
5805 + if (IS_ROOT(parent))
5808 + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5809 + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5811 + /* do not use au_dpage */
5812 + real_parent = parent;
5815 + parent = dget_parent(dentry);
5816 + h_parent = au_h_dptr(parent, bdst);
5818 + goto out; /* success */
5820 + /* find top dir which is necessary to cpup */
5824 + parent = dget_parent(d);
5825 + di_read_lock_parent3(parent, !AuLock_IR);
5826 + h_parent = au_h_dptr(parent, bdst);
5827 + di_read_unlock(parent, !AuLock_IR);
5828 + } while (!h_parent);
5830 + if (d != real_parent)
5831 + di_write_lock_child3(d);
5833 + /* somebody else might create while we were sleeping */
5834 + h_dentry = au_h_dptr(d, bdst);
5835 + if (!h_dentry || d_is_negative(h_dentry)) {
5837 + au_update_dbtop(d);
5839 + au_pin_set_dentry(&pin, d);
5840 + err = au_do_pin(&pin);
5842 + err = cp(d, bdst, &pin, h_parent, arg);
5847 + if (d != real_parent)
5848 + di_write_unlock(d);
5849 + if (unlikely(err))
5858 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5859 + struct au_pin *pin,
5860 + struct dentry *h_parent __maybe_unused,
5861 + void *arg __maybe_unused)
5863 + struct au_cp_generic cpg = {
5869 + .flags = AuCpup_DTIME
5871 + return au_sio_cpup_simple(&cpg);
5874 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5876 + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5879 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5882 + struct dentry *parent;
5883 + struct inode *dir;
5885 + parent = dget_parent(dentry);
5886 + dir = d_inode(parent);
5888 + if (au_h_iptr(dir, bdst))
5891 + di_read_unlock(parent, AuLock_IR);
5892 + di_write_lock_parent(parent);
5893 + /* someone else might change our inode while we were sleeping */
5894 + if (!au_h_iptr(dir, bdst))
5895 + err = au_cpup_dirs(dentry, bdst);
5896 + di_downgrade_lock(parent, AuLock_IR);
5902 diff --git a/fs/aufs/cpup.h b/fs/aufs/cpup.h
5903 new file mode 100644
5904 index 00000000000000..decd8f01425a05
5906 +++ b/fs/aufs/cpup.h
5908 +/* SPDX-License-Identifier: GPL-2.0 */
5910 + * Copyright (C) 2005-2022 Junjiro R. Okajima
5912 + * This program is free software; you can redistribute it and/or modify
5913 + * it under the terms of the GNU General Public License as published by
5914 + * the Free Software Foundation; either version 2 of the License, or
5915 + * (at your option) any later version.
5917 + * This program is distributed in the hope that it will be useful,
5918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5920 + * GNU General Public License for more details.
5922 + * You should have received a copy of the GNU General Public License
5923 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
5927 + * copy-up/down functions
5930 +#ifndef __AUFS_CPUP_H__
5931 +#define __AUFS_CPUP_H__
5935 +#include <linux/path.h>
5941 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5942 +void au_cpup_attr_timesizes(struct inode *inode);
5943 +void au_cpup_attr_nlink(struct inode *inode, int force);
5944 +void au_cpup_attr_changeable(struct inode *inode);
5945 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5946 +void au_cpup_attr_all(struct inode *inode, int force);
5948 +/* ---------------------------------------------------------------------- */
5950 +struct au_cp_generic {
5951 + struct dentry *dentry;
5952 + aufs_bindex_t bdst, bsrc;
5954 + struct au_pin *pin;
5955 + unsigned int flags;
5959 +#define AuCpup_DTIME 1 /* do dtime_store/revert */
5960 +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
5962 +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
5963 +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
5965 +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
5967 +#define AuCpup_RWDST (1 << 5) /* force write target even if
5968 + the branch is marked as RO */
5970 +#ifndef CONFIG_AUFS_BR_HFSPLUS
5971 +#undef AuCpup_HOPEN
5972 +#define AuCpup_HOPEN 0
5975 +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
5976 +#define au_fset_cpup(flags, name) \
5977 + do { (flags) |= AuCpup_##name; } while (0)
5978 +#define au_fclr_cpup(flags, name) \
5979 + do { (flags) &= ~AuCpup_##name; } while (0)
5981 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5982 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5983 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5984 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5986 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5987 + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5988 + struct au_pin *pin,
5989 + struct dentry *h_parent, void *arg),
5991 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5992 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5994 +/* ---------------------------------------------------------------------- */
5996 +/* keep timestamps when copyup */
5998 + struct dentry *dt_dentry;
5999 + struct path dt_h_path;
6000 + struct timespec64 dt_atime, dt_mtime;
6002 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6003 + struct path *h_path);
6004 +void au_dtime_revert(struct au_dtime *dt);
6006 +#endif /* __KERNEL__ */
6007 +#endif /* __AUFS_CPUP_H__ */
6008 diff --git a/fs/aufs/dbgaufs.c b/fs/aufs/dbgaufs.c
6009 new file mode 100644
6010 index 00000000000000..d5811df890b5f5
6012 +++ b/fs/aufs/dbgaufs.c
6014 +// SPDX-License-Identifier: GPL-2.0
6016 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6018 + * This program is free software; you can redistribute it and/or modify
6019 + * it under the terms of the GNU General Public License as published by
6020 + * the Free Software Foundation; either version 2 of the License, or
6021 + * (at your option) any later version.
6023 + * This program is distributed in the hope that it will be useful,
6024 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6025 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6026 + * GNU General Public License for more details.
6028 + * You should have received a copy of the GNU General Public License
6029 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6033 + * debugfs interface
6036 +#include <linux/debugfs.h>
6039 +#ifndef CONFIG_SYSFS
6040 +#error DEBUG_FS depends upon SYSFS
6043 +static struct dentry *dbgaufs;
6044 +static const mode_t dbgaufs_mode = 0444;
6046 +/* 20 is max digits length of ulong 64 */
6047 +struct dbgaufs_arg {
6053 + * common function for all XINO files
6055 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6056 + struct file *file)
6060 + p = file->private_data;
6062 + /* this is struct dbgaufs_arg */
6063 + AuDebugOn(!au_kfree_sz_test(p));
6064 + au_kfree_do_rcu(p);
6069 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6074 + struct dbgaufs_arg *p;
6077 + p = kmalloc(sizeof(*p), GFP_NOFS);
6083 + file->private_data = p;
6087 + err = vfsub_getattr(&xf->f_path, &st);
6091 + (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6092 + cnt, st.blocks, st.blksize,
6093 + (long long)st.size);
6095 + p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6096 + st.blocks, st.blksize,
6097 + (long long)st.size);
6098 + AuDebugOn(p->n >= sizeof(p->a));
6100 + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6108 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6109 + size_t count, loff_t *ppos)
6111 + struct dbgaufs_arg *p;
6113 + p = file->private_data;
6114 + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6117 +/* ---------------------------------------------------------------------- */
6119 +struct dbgaufs_plink_arg {
6124 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6125 + struct file *file)
6127 + free_page((unsigned long)file->private_data);
6131 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6133 + int err, i, limit;
6134 + unsigned long n, sum;
6135 + struct dbgaufs_plink_arg *p;
6136 + struct au_sbinfo *sbinfo;
6137 + struct super_block *sb;
6138 + struct hlist_bl_head *hbl;
6141 + p = (void *)get_zeroed_page(GFP_NOFS);
6146 + sbinfo = inode->i_private;
6147 + sb = sbinfo->si_sb;
6148 + si_noflush_read_lock(sb);
6149 + if (au_opt_test(au_mntflags(sb), PLINK)) {
6150 + limit = PAGE_SIZE - sizeof(p->n);
6152 + /* the number of buckets */
6153 + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6158 + for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6160 + n = au_hbl_count(hbl);
6163 + n = snprintf(p->a + p->n, limit, "%lu ", n);
6166 + if (unlikely(limit <= 0))
6169 + p->a[p->n - 1] = '\n';
6171 + /* the sum of plinks */
6172 + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6175 + if (unlikely(limit <= 0))
6178 +#define str "1\n0\n0\n"
6179 + p->n = sizeof(str) - 1;
6180 + strscpy(p->a, str, sizeof(str));
6183 + si_read_unlock(sb);
6186 + file->private_data = p;
6187 + goto out; /* success */
6190 + free_page((unsigned long)p);
6195 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6196 + size_t count, loff_t *ppos)
6198 + struct dbgaufs_plink_arg *p;
6200 + p = file->private_data;
6201 + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6204 +static const struct file_operations dbgaufs_plink_fop = {
6205 + .owner = THIS_MODULE,
6206 + .open = dbgaufs_plink_open,
6207 + .release = dbgaufs_plink_release,
6208 + .read = dbgaufs_plink_read
6211 +/* ---------------------------------------------------------------------- */
6213 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6216 + struct au_sbinfo *sbinfo;
6217 + struct super_block *sb;
6219 + sbinfo = inode->i_private;
6220 + sb = sbinfo->si_sb;
6221 + si_noflush_read_lock(sb);
6222 + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6223 + si_read_unlock(sb);
6227 +static const struct file_operations dbgaufs_xib_fop = {
6228 + .owner = THIS_MODULE,
6229 + .open = dbgaufs_xib_open,
6230 + .release = dbgaufs_xi_release,
6231 + .read = dbgaufs_xi_read
6234 +/* ---------------------------------------------------------------------- */
6236 +#define DbgaufsXi_PREFIX "xi"
6238 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6242 + aufs_bindex_t bindex;
6243 + char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6244 + struct au_sbinfo *sbinfo;
6245 + struct super_block *sb;
6246 + struct au_xino *xi;
6248 + struct qstr *name;
6249 + struct au_branch *br;
6252 + name = &file->f_path.dentry->d_name;
6253 + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6254 + || memcmp(name->name, DbgaufsXi_PREFIX,
6255 + sizeof(DbgaufsXi_PREFIX) - 1)))
6258 + AuDebugOn(name->len >= sizeof(a));
6259 + memcpy(a, name->name, name->len);
6260 + a[name->len] = '\0';
6261 + p = strchr(a, '-');
6264 + err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6265 + if (unlikely(err))
6270 + err = kstrtol(p + 1, 10, &l);
6271 + if (unlikely(err))
6277 + sbinfo = inode->i_private;
6278 + sb = sbinfo->si_sb;
6279 + si_noflush_read_lock(sb);
6280 + if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6282 + br = au_sbr(sb, bindex);
6284 + if (unlikely(idx >= xi->xi_nfile))
6286 + xf = au_xino_file(xi, idx);
6288 + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6289 + au_xino_count(br));
6292 + si_read_unlock(sb);
6298 +static const struct file_operations dbgaufs_xino_fop = {
6299 + .owner = THIS_MODULE,
6300 + .open = dbgaufs_xino_open,
6301 + .release = dbgaufs_xi_release,
6302 + .read = dbgaufs_xi_read
6305 +void dbgaufs_xino_del(struct au_branch *br)
6307 + struct dentry *dbgaufs;
6309 + dbgaufs = br->br_dbgaufs;
6313 + br->br_dbgaufs = NULL;
6314 + /* debugfs acquires the parent i_mutex */
6316 + debugfs_remove(dbgaufs);
6320 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6322 + aufs_bindex_t bbot;
6323 + struct au_branch *br;
6325 + if (!au_sbi(sb)->si_dbgaufs)
6328 + bbot = au_sbbot(sb);
6329 + for (; bindex <= bbot; bindex++) {
6330 + br = au_sbr(sb, bindex);
6331 + dbgaufs_xino_del(br);
6335 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6336 + unsigned int idx, struct dentry *parent,
6337 + struct au_sbinfo *sbinfo)
6339 + struct au_branch *br;
6341 + /* "xi" bindex(5) "-" idx(2) NULL */
6342 + char name[sizeof(DbgaufsXi_PREFIX) + 8];
6345 + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6347 + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6349 + br = au_sbr(sb, bindex);
6350 + if (br->br_dbgaufs) {
6351 + struct qstr qstr = QSTR_INIT(name, strlen(name));
6353 + if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6354 + /* debugfs acquires the parent i_mutex */
6356 + d = debugfs_rename(parent, br->br_dbgaufs, parent,
6360 + pr_warn("failed renaming %pd/%s, ignored.\n",
6365 + br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6366 + sbinfo, &dbgaufs_xino_fop);
6368 + if (unlikely(!br->br_dbgaufs))
6369 + pr_warn("failed creating %pd/%s, ignored.\n",
6374 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6375 + struct dentry *parent, struct au_sbinfo *sbinfo)
6377 + struct au_branch *br;
6378 + struct au_xino *xi;
6381 + br = au_sbr(sb, bindex);
6383 + for (u = 0; u < xi->xi_nfile; u++)
6384 + dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6387 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6389 + struct au_sbinfo *sbinfo;
6390 + struct dentry *parent;
6391 + aufs_bindex_t bbot;
6393 + if (!au_opt_test(au_mntflags(sb), XINO))
6396 + sbinfo = au_sbi(sb);
6397 + parent = sbinfo->si_dbgaufs;
6401 + bbot = au_sbbot(sb);
6403 + for (; bindex <= bbot; bindex++)
6404 + dbgaufs_br_add(sb, bindex, parent, sbinfo);
6406 + for (; bbot >= bindex; bbot--)
6407 + dbgaufs_br_add(sb, bbot, parent, sbinfo);
6410 +/* ---------------------------------------------------------------------- */
6412 +#ifdef CONFIG_AUFS_EXPORT
6413 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6416 + struct au_sbinfo *sbinfo;
6417 + struct super_block *sb;
6419 + sbinfo = inode->i_private;
6420 + sb = sbinfo->si_sb;
6421 + si_noflush_read_lock(sb);
6422 + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6423 + si_read_unlock(sb);
6427 +static const struct file_operations dbgaufs_xigen_fop = {
6428 + .owner = THIS_MODULE,
6429 + .open = dbgaufs_xigen_open,
6430 + .release = dbgaufs_xi_release,
6431 + .read = dbgaufs_xi_read
6434 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6439 + * This function is a dynamic '__init' function actually,
6440 + * so the tiny check for si_rwsem is unnecessary.
6442 + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6445 + sbinfo->si_dbgaufs_xigen = debugfs_create_file
6446 + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6447 + &dbgaufs_xigen_fop);
6448 + if (sbinfo->si_dbgaufs_xigen)
6454 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6458 +#endif /* CONFIG_AUFS_EXPORT */
6460 +/* ---------------------------------------------------------------------- */
6462 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6465 + * This function is a dynamic '__fin' function actually,
6466 + * so the tiny check for si_rwsem is unnecessary.
6468 + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6470 + debugfs_remove_recursive(sbinfo->si_dbgaufs);
6471 + sbinfo->si_dbgaufs = NULL;
6474 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6477 + char name[SysaufsSiNameLen];
6480 + * This function is a dynamic '__init' function actually,
6481 + * so the tiny check for si_rwsem is unnecessary.
6483 + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6487 + AuErr1("/debug/aufs is uninitialized\n");
6492 + sysaufs_name(sbinfo, name);
6493 + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6494 + if (unlikely(!sbinfo->si_dbgaufs))
6497 + /* regardless plink/noplink option */
6498 + sbinfo->si_dbgaufs_plink = debugfs_create_file
6499 + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6500 + &dbgaufs_plink_fop);
6501 + if (unlikely(!sbinfo->si_dbgaufs_plink))
6504 + /* regardless xino/noxino option */
6505 + sbinfo->si_dbgaufs_xib = debugfs_create_file
6506 + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6507 + &dbgaufs_xib_fop);
6508 + if (unlikely(!sbinfo->si_dbgaufs_xib))
6511 + err = dbgaufs_xigen_init(sbinfo);
6513 + goto out; /* success */
6516 + dbgaufs_si_fin(sbinfo);
6518 + if (unlikely(err))
6519 + pr_err("debugfs/aufs failed\n");
6523 +/* ---------------------------------------------------------------------- */
6525 +void dbgaufs_fin(void)
6527 + debugfs_remove(dbgaufs);
6530 +int __init dbgaufs_init(void)
6535 + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6540 diff --git a/fs/aufs/dbgaufs.h b/fs/aufs/dbgaufs.h
6541 new file mode 100644
6542 index 00000000000000..30f1694224e004
6544 +++ b/fs/aufs/dbgaufs.h
6546 +/* SPDX-License-Identifier: GPL-2.0 */
6548 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6550 + * This program is free software; you can redistribute it and/or modify
6551 + * it under the terms of the GNU General Public License as published by
6552 + * the Free Software Foundation; either version 2 of the License, or
6553 + * (at your option) any later version.
6555 + * This program is distributed in the hope that it will be useful,
6556 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6557 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6558 + * GNU General Public License for more details.
6560 + * You should have received a copy of the GNU General Public License
6561 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6565 + * debugfs interface
6568 +#ifndef __DBGAUFS_H__
6569 +#define __DBGAUFS_H__
6573 +struct super_block;
6577 +#ifdef CONFIG_DEBUG_FS
6579 +void dbgaufs_xino_del(struct au_branch *br);
6580 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6581 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6582 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6583 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6584 +void dbgaufs_fin(void);
6585 +int __init dbgaufs_init(void);
6587 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6588 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6589 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6591 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6592 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6593 +AuStubVoid(dbgaufs_fin, void)
6594 +AuStubInt0(__init dbgaufs_init, void)
6595 +#endif /* CONFIG_DEBUG_FS */
6597 +#endif /* __KERNEL__ */
6598 +#endif /* __DBGAUFS_H__ */
6599 diff --git a/fs/aufs/dcsub.c b/fs/aufs/dcsub.c
6600 new file mode 100644
6601 index 00000000000000..fa2a9ad2b49b61
6603 +++ b/fs/aufs/dcsub.c
6605 +// SPDX-License-Identifier: GPL-2.0
6607 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6609 + * This program is free software; you can redistribute it and/or modify
6610 + * it under the terms of the GNU General Public License as published by
6611 + * the Free Software Foundation; either version 2 of the License, or
6612 + * (at your option) any later version.
6614 + * This program is distributed in the hope that it will be useful,
6615 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6616 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6617 + * GNU General Public License for more details.
6619 + * You should have received a copy of the GNU General Public License
6620 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6624 + * sub-routines for dentry cache
6629 +static void au_dpage_free(struct au_dpage *dpage)
6632 + struct dentry **p;
6634 + p = dpage->dentries;
6635 + for (i = 0; i < dpage->ndentry; i++)
6637 + free_page((unsigned long)dpage->dentries);
6640 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6646 + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6647 + if (unlikely(!dpages->dpages))
6650 + p = (void *)__get_free_page(gfp);
6654 + dpages->dpages[0].ndentry = 0;
6655 + dpages->dpages[0].dentries = p;
6656 + dpages->ndpage = 1;
6657 + return 0; /* success */
6660 + au_kfree_try_rcu(dpages->dpages);
6665 +void au_dpages_free(struct au_dcsub_pages *dpages)
6668 + struct au_dpage *p;
6670 + p = dpages->dpages;
6671 + for (i = 0; i < dpages->ndpage; i++)
6672 + au_dpage_free(p++);
6673 + au_kfree_try_rcu(dpages->dpages);
6676 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6677 + struct dentry *dentry, gfp_t gfp)
6680 + struct au_dpage *dpage;
6683 + dpage = dpages->dpages + dpages->ndpage - 1;
6684 + sz = PAGE_SIZE / sizeof(dentry);
6685 + if (unlikely(dpage->ndentry >= sz)) {
6686 + AuLabel(new dpage);
6688 + sz = dpages->ndpage * sizeof(*dpages->dpages);
6689 + p = au_kzrealloc(dpages->dpages, sz,
6690 + sz + sizeof(*dpages->dpages), gfp,
6695 + dpages->dpages = p;
6696 + dpage = dpages->dpages + dpages->ndpage;
6697 + p = (void *)__get_free_page(gfp);
6701 + dpage->ndentry = 0;
6702 + dpage->dentries = p;
6706 + AuDebugOn(au_dcount(dentry) <= 0);
6707 + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6708 + return 0; /* success */
6714 +/* todo: BAD approach */
6715 +/* copied from linux/fs/dcache.c */
6723 +extern void d_walk(struct dentry *parent, void *data,
6724 + enum d_walk_ret (*enter)(void *, struct dentry *));
6726 +struct ac_dpages_arg {
6728 + struct au_dcsub_pages *dpages;
6729 + struct super_block *sb;
6730 + au_dpages_test test;
6734 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6736 + enum d_walk_ret ret;
6737 + struct ac_dpages_arg *arg = _arg;
6739 + ret = D_WALK_CONTINUE;
6740 + if (dentry->d_sb == arg->sb
6741 + && !IS_ROOT(dentry)
6742 + && au_dcount(dentry) > 0
6744 + && (!arg->test || arg->test(dentry, arg->arg))) {
6745 + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6746 + if (unlikely(arg->err))
6747 + ret = D_WALK_QUIT;
6753 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6754 + au_dpages_test test, void *arg)
6756 + struct ac_dpages_arg args = {
6764 + d_walk(root, &args, au_call_dpages_append);
6769 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6770 + int do_include, au_dpages_test test, void *arg)
6775 + write_seqlock(&rename_lock);
6776 + spin_lock(&dentry->d_lock);
6778 + && au_dcount(dentry) > 0
6779 + && (!test || test(dentry, arg)))
6780 + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6781 + spin_unlock(&dentry->d_lock);
6782 + if (unlikely(err))
6786 + * RCU for vfsmount is unnecessary since this is a traverse in a single
6789 + while (!IS_ROOT(dentry)) {
6790 + dentry = dentry->d_parent; /* rename_lock is locked */
6791 + spin_lock(&dentry->d_lock);
6792 + if (au_dcount(dentry) > 0
6793 + && (!test || test(dentry, arg)))
6794 + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6795 + spin_unlock(&dentry->d_lock);
6796 + if (unlikely(err))
6801 + write_sequnlock(&rename_lock);
6805 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6807 + return au_di(dentry) && dentry->d_sb == arg;
6810 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6811 + struct dentry *dentry, int do_include)
6813 + return au_dcsub_pages_rev(dpages, dentry, do_include,
6814 + au_dcsub_dpages_aufs, dentry->d_sb);
6817 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6819 + struct path path[2] = {
6828 + return path_is_under(path + 0, path + 1);
6830 diff --git a/fs/aufs/dcsub.h b/fs/aufs/dcsub.h
6831 new file mode 100644
6832 index 00000000000000..7e4f4eba0343b2
6834 +++ b/fs/aufs/dcsub.h
6836 +/* SPDX-License-Identifier: GPL-2.0 */
6838 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6840 + * This program is free software; you can redistribute it and/or modify
6841 + * it under the terms of the GNU General Public License as published by
6842 + * the Free Software Foundation; either version 2 of the License, or
6843 + * (at your option) any later version.
6845 + * This program is distributed in the hope that it will be useful,
6846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6848 + * GNU General Public License for more details.
6850 + * You should have received a copy of the GNU General Public License
6851 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6855 + * sub-routines for dentry cache
6858 +#ifndef __AUFS_DCSUB_H__
6859 +#define __AUFS_DCSUB_H__
6863 +#include <linux/dcache.h>
6864 +#include <linux/fs.h>
6868 + struct dentry **dentries;
6871 +struct au_dcsub_pages {
6873 + struct au_dpage *dpages;
6876 +/* ---------------------------------------------------------------------- */
6879 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6880 +void au_dpages_free(struct au_dcsub_pages *dpages);
6881 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6882 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6883 + au_dpages_test test, void *arg);
6884 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6885 + int do_include, au_dpages_test test, void *arg);
6886 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6887 + struct dentry *dentry, int do_include);
6888 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6890 +/* ---------------------------------------------------------------------- */
6893 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6894 + * include/linux/dcache.h. Try them (in the future).
6897 +static inline int au_d_hashed_positive(struct dentry *d)
6900 + struct inode *inode = d_inode(d);
6903 + if (unlikely(d_unhashed(d)
6904 + || d_is_negative(d)
6905 + || !inode->i_nlink))
6910 +static inline int au_d_linkable(struct dentry *d)
6913 + struct inode *inode = d_inode(d);
6915 + err = au_d_hashed_positive(d);
6917 + && d_is_positive(d)
6918 + && (inode->i_state & I_LINKABLE))
6923 +static inline int au_d_alive(struct dentry *d)
6926 + struct inode *inode;
6930 + err = au_d_hashed_positive(d);
6932 + inode = d_inode(d);
6933 + if (unlikely(d_unlinked(d)
6934 + || d_is_negative(d)
6935 + || !inode->i_nlink))
6941 +static inline int au_alive_dir(struct dentry *d)
6945 + err = au_d_alive(d);
6946 + if (unlikely(err || IS_DEADDIR(d_inode(d))))
6951 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6953 + return a->len == b->len
6954 + && !memcmp(a->name, b->name, a->len);
6959 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6961 + * the type of d_lockref.count became int, but the inlined function d_count()
6962 + * still returns unsigned int.
6963 + * I don't know why. Maybe it is for every d_count() users?
6964 + * Anyway au_dcount() lives on.
6966 +static inline int au_dcount(struct dentry *d)
6968 + return (int)d_count(d);
6971 +#endif /* __KERNEL__ */
6972 +#endif /* __AUFS_DCSUB_H__ */
6973 diff --git a/fs/aufs/debug.c b/fs/aufs/debug.c
6974 new file mode 100644
6975 index 00000000000000..e503bb357984ec
6977 +++ b/fs/aufs/debug.c
6979 +// SPDX-License-Identifier: GPL-2.0
6981 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6983 + * This program is free software; you can redistribute it and/or modify
6984 + * it under the terms of the GNU General Public License as published by
6985 + * the Free Software Foundation; either version 2 of the License, or
6986 + * (at your option) any later version.
6988 + * This program is distributed in the hope that it will be useful,
6989 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6990 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6991 + * GNU General Public License for more details.
6993 + * You should have received a copy of the GNU General Public License
6994 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6998 + * debug print functions
7001 +#include <linux/iversion.h>
7004 +/* Returns 0, or -errno. arg is in kp->arg. */
7005 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7009 + err = kstrtoint(val, 0, &n);
7019 +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7020 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7025 + return sprintf(buffer, "%d", atomic_read(a));
7028 +static const struct kernel_param_ops param_ops_atomic_t = {
7029 + .set = param_atomic_t_set,
7030 + .get = param_atomic_t_get
7031 + /* void (*free)(void *arg) */
7034 +atomic_t aufs_debug = ATOMIC_INIT(0);
7035 +MODULE_PARM_DESC(debug, "debug print");
7036 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7038 +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
7039 +char *au_plevel = KERN_DEBUG;
7040 +#define dpri(fmt, ...) do { \
7042 + && strcmp(au_plevel, KERN_DEBUG)) \
7043 + || au_debug_test()) \
7044 + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
7047 +/* ---------------------------------------------------------------------- */
7049 +void au_dpri_whlist(struct au_nhash *whlist)
7051 + unsigned long ul, n;
7052 + struct hlist_head *head;
7053 + struct au_vdir_wh *pos;
7055 + n = whlist->nh_num;
7056 + head = whlist->nh_head;
7057 + for (ul = 0; ul < n; ul++) {
7058 + hlist_for_each_entry(pos, head, wh_hash)
7059 + dpri("b%d, %.*s, %d\n",
7061 + pos->wh_str.len, pos->wh_str.name,
7067 +void au_dpri_vdir(struct au_vdir *vdir)
7070 + union au_vdir_deblk_p p;
7073 + if (!vdir || IS_ERR(vdir)) {
7074 + dpri("err %ld\n", PTR_ERR(vdir));
7078 + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7079 + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7080 + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7081 + for (ul = 0; ul < vdir->vd_nblk; ul++) {
7082 + p.deblk = vdir->vd_deblk[ul];
7084 + dpri("[%lu]: %p\n", ul, o);
7088 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7089 + struct dentry *wh)
7093 + struct timespec64 ctime;
7095 + if (!inode || IS_ERR(inode)) {
7096 + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7100 + /* the type of i_blocks depends upon CONFIG_LBDAF */
7101 + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7102 + && sizeof(inode->i_blocks) != sizeof(u64));
7104 + n = (void *)wh->d_name.name;
7105 + l = wh->d_name.len;
7108 + ctime = inode_get_ctime(inode);
7109 + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7110 + " acl %p, def_acl %p,"
7111 + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7113 + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7114 + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7115 + i_size_read(inode), (unsigned long long)inode->i_blocks,
7116 + inode->i_acl, inode->i_default_acl,
7117 + hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
7118 + inode->i_mapping ? inode->i_mapping->nrpages : 0,
7119 + inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7120 + inode->i_generation,
7121 + l ? ", wh " : "", l, n);
7125 +void au_dpri_inode(struct inode *inode)
7127 + struct au_iinfo *iinfo;
7128 + struct au_hinode *hi;
7129 + aufs_bindex_t bindex;
7132 + err = do_pri_inode(-1, inode, -1, NULL);
7133 + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7136 + iinfo = au_ii(inode);
7137 + dpri("i-1: btop %d, bbot %d, gen %d\n",
7138 + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7139 + if (iinfo->ii_btop < 0)
7142 + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7143 + hi = au_hinode(iinfo, bindex);
7145 + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7149 +void au_dpri_dalias(struct inode *inode)
7153 + spin_lock(&inode->i_lock);
7154 + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7155 + au_dpri_dentry(d);
7156 + spin_unlock(&inode->i_lock);
7159 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7161 + struct dentry *wh = NULL;
7163 + struct inode *inode;
7164 + struct au_iinfo *iinfo;
7165 + struct au_hinode *hi;
7167 + if (!dentry || IS_ERR(dentry)) {
7168 + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7171 + /* do not call dget_parent() here */
7172 + /* note: access d_xxx without d_lock */
7173 + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7174 + bindex, dentry, dentry,
7175 + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7176 + au_dcount(dentry), dentry->d_flags,
7177 + d_unhashed(dentry) ? "un" : "");
7180 + if (d_is_positive(dentry))
7181 + inode = d_inode(dentry);
7183 + && au_test_aufs(dentry->d_sb)
7185 + && !au_is_bad_inode(inode)) {
7186 + iinfo = au_ii(inode);
7187 + hi = au_hinode(iinfo, bindex);
7189 + wh = hi->hi_whdentry;
7191 + do_pri_inode(bindex, inode, hn, wh);
7195 +void au_dpri_dentry(struct dentry *dentry)
7197 + struct au_dinfo *dinfo;
7198 + aufs_bindex_t bindex;
7201 + err = do_pri_dentry(-1, dentry);
7202 + if (err || !au_test_aufs(dentry->d_sb))
7205 + dinfo = au_di(dentry);
7208 + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7209 + dinfo->di_btop, dinfo->di_bbot,
7210 + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7211 + dinfo->di_tmpfile);
7212 + if (dinfo->di_btop < 0)
7214 + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7215 + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7218 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7222 + if (!file || IS_ERR(file)) {
7223 + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7228 + && !IS_ERR_OR_NULL(file->f_path.dentry)
7229 + && au_test_aufs(file->f_path.dentry->d_sb)
7231 + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7232 + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7233 + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7234 + bindex, file->f_mode, file->f_flags, (long)file_count(file),
7235 + file->f_version, file->f_pos, a);
7236 + if (!IS_ERR_OR_NULL(file->f_path.dentry))
7237 + do_pri_dentry(bindex, file->f_path.dentry);
7241 +void au_dpri_file(struct file *file)
7243 + struct au_finfo *finfo;
7244 + struct au_fidir *fidir;
7245 + struct au_hfile *hfile;
7246 + aufs_bindex_t bindex;
7249 + err = do_pri_file(-1, file);
7251 + || IS_ERR_OR_NULL(file->f_path.dentry)
7252 + || !au_test_aufs(file->f_path.dentry->d_sb))
7255 + finfo = au_fi(file);
7258 + if (finfo->fi_btop < 0)
7260 + fidir = finfo->fi_hdir;
7262 + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7264 + for (bindex = finfo->fi_btop;
7265 + bindex >= 0 && bindex <= fidir->fd_bbot;
7267 + hfile = fidir->fd_hfile + bindex;
7268 + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7272 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7274 + struct vfsmount *mnt;
7275 + struct super_block *sb;
7277 + if (!br || IS_ERR(br))
7279 + mnt = au_br_mnt(br);
7280 + if (!mnt || IS_ERR(mnt))
7283 + if (!sb || IS_ERR(sb))
7286 + dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7287 + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7289 + bindex, br->br_perm, br->br_id, br->br_wbr,
7290 + au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7291 + sb->s_flags, sb->s_count,
7292 + atomic_read(&sb->s_active),
7293 + !!au_xino_file(br->br_xino, /*idx*/-1));
7297 + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7301 +void au_dpri_sb(struct super_block *sb)
7303 + struct au_sbinfo *sbinfo;
7304 + aufs_bindex_t bindex;
7306 + /* to reduce stack size */
7308 + struct vfsmount mnt;
7309 + struct au_branch fake;
7312 + /* this function can be called from magic sysrq */
7313 + a = kzalloc(sizeof(*a), GFP_ATOMIC);
7314 + if (unlikely(!a)) {
7315 + dpri("no memory\n");
7319 + a->mnt.mnt_sb = sb;
7320 + a->fake.br_path.mnt = &a->mnt;
7321 + err = do_pri_br(-1, &a->fake);
7323 + dpri("dev 0x%x\n", sb->s_dev);
7324 + if (err || !au_test_aufs(sb))
7327 + sbinfo = au_sbi(sb);
7330 + dpri("nw %d, gen %u, kobj %d\n",
7331 + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7332 + kref_read(&sbinfo->si_kobj.kref));
7333 + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7334 + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7337 +/* ---------------------------------------------------------------------- */
7339 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7341 + struct inode *h_inode, *inode = d_inode(dentry);
7342 + struct dentry *h_dentry;
7343 + aufs_bindex_t bindex, bbot, bi;
7345 + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7348 + bbot = au_dbbot(dentry);
7349 + bi = au_ibbot(inode);
7352 + bindex = au_dbtop(dentry);
7353 + bi = au_ibtop(inode);
7357 + for (; bindex <= bbot; bindex++) {
7358 + h_dentry = au_h_dptr(dentry, bindex);
7361 + h_inode = au_h_iptr(inode, bindex);
7362 + if (unlikely(h_inode != d_inode(h_dentry))) {
7364 + AuDbg("b%d, %s:%d\n", bindex, func, line);
7365 + AuDbgDentry(dentry);
7366 + AuDbgInode(inode);
7368 + if (au_test_fuse(h_inode->i_sb))
7376 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7379 + struct au_dcsub_pages dpages;
7380 + struct au_dpage *dpage;
7381 + struct dentry **dentries;
7383 + err = au_dpages_init(&dpages, GFP_NOFS);
7385 + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7387 + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7388 + dpage = dpages.dpages + i;
7389 + dentries = dpage->dentries;
7390 + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7391 + AuDebugOn(au_digen_test(dentries[j], sigen));
7393 + au_dpages_free(&dpages);
7396 +void au_dbg_verify_kthread(void)
7398 + if (au_wkq_test()) {
7401 + * It may be recursive, but udba=notify between two aufs mounts,
7402 + * where a single ro branch is shared, is not a problem.
7408 +/* ---------------------------------------------------------------------- */
7410 +int __init au_debug_init(void)
7412 + aufs_bindex_t bindex;
7413 + struct au_vdir_destr destr;
7416 + AuDebugOn(bindex >= 0);
7419 + AuDebugOn(destr.len < NAME_MAX);
7421 +#ifdef CONFIG_4KSTACKS
7422 + pr_warn("CONFIG_4KSTACKS is defined.\n");
7427 diff --git a/fs/aufs/debug.h b/fs/aufs/debug.h
7428 new file mode 100644
7429 index 00000000000000..f757588e5000ac
7431 +++ b/fs/aufs/debug.h
7433 +/* SPDX-License-Identifier: GPL-2.0 */
7435 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7437 + * This program is free software; you can redistribute it and/or modify
7438 + * it under the terms of the GNU General Public License as published by
7439 + * the Free Software Foundation; either version 2 of the License, or
7440 + * (at your option) any later version.
7442 + * This program is distributed in the hope that it will be useful,
7443 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7444 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7445 + * GNU General Public License for more details.
7447 + * You should have received a copy of the GNU General Public License
7448 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
7452 + * debug print functions
7455 +#ifndef __AUFS_DEBUG_H__
7456 +#define __AUFS_DEBUG_H__
7460 +#include <linux/atomic.h>
7461 +#include <linux/module.h>
7462 +#include <linux/kallsyms.h>
7463 +#include <linux/sysrq.h>
7465 +#ifdef CONFIG_AUFS_DEBUG
7466 +#define AuDebugOn(a) BUG_ON(a)
7468 +/* module parameter */
7469 +extern atomic_t aufs_debug;
7470 +static inline void au_debug_on(void)
7472 + atomic_inc(&aufs_debug);
7474 +static inline void au_debug_off(void)
7476 + atomic_dec_if_positive(&aufs_debug);
7479 +static inline int au_debug_test(void)
7481 + return atomic_read(&aufs_debug) > 0;
7484 +#define AuDebugOn(a) do {} while (0)
7485 +AuStubVoid(au_debug_on, void)
7486 +AuStubVoid(au_debug_off, void)
7487 +AuStubInt0(au_debug_test, void)
7488 +#endif /* CONFIG_AUFS_DEBUG */
7490 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7492 +/* ---------------------------------------------------------------------- */
7496 +#define AuDbg(fmt, ...) do { \
7497 + if (au_debug_test()) \
7498 + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7500 +#define AuLabel(l) AuDbg(#l "\n")
7501 +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7502 +#define AuWarn1(fmt, ...) do { \
7503 + static unsigned char _c; \
7505 + pr_warn(fmt, ##__VA_ARGS__); \
7508 +#define AuErr1(fmt, ...) do { \
7509 + static unsigned char _c; \
7511 + pr_err(fmt, ##__VA_ARGS__); \
7514 +#define AuIOErr1(fmt, ...) do { \
7515 + static unsigned char _c; \
7517 + AuIOErr(fmt, ##__VA_ARGS__); \
7520 +#define AuUnsupportMsg "This operation is not supported." \
7521 + " Please report this application to aufs-users ML."
7522 +#define AuUnsupport(fmt, ...) do { \
7523 + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7527 +#define AuTraceErr(e) do { \
7528 + if (unlikely((e) < 0)) \
7529 + AuDbg("err %d\n", (int)(e)); \
7532 +#define AuTraceErrPtr(p) do { \
7534 + AuDbg("err %ld\n", PTR_ERR(p)); \
7537 +/* dirty macros for debug print, use with "%.*s" and caution */
7538 +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
7540 +/* ---------------------------------------------------------------------- */
7543 +#ifdef CONFIG_AUFS_DEBUG
7544 +extern struct mutex au_dbg_mtx;
7545 +extern char *au_plevel;
7547 +void au_dpri_whlist(struct au_nhash *whlist);
7549 +void au_dpri_vdir(struct au_vdir *vdir);
7551 +void au_dpri_inode(struct inode *inode);
7552 +void au_dpri_dalias(struct inode *inode);
7553 +void au_dpri_dentry(struct dentry *dentry);
7555 +void au_dpri_file(struct file *filp);
7556 +struct super_block;
7557 +void au_dpri_sb(struct super_block *sb);
7559 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7560 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7561 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7562 +void au_dbg_verify_kthread(void);
7564 +int __init au_debug_init(void);
7566 +#define AuDbgWhlist(w) do { \
7567 + mutex_lock(&au_dbg_mtx); \
7569 + au_dpri_whlist(w); \
7570 + mutex_unlock(&au_dbg_mtx); \
7573 +#define AuDbgVdir(v) do { \
7574 + mutex_lock(&au_dbg_mtx); \
7576 + au_dpri_vdir(v); \
7577 + mutex_unlock(&au_dbg_mtx); \
7580 +#define AuDbgInode(i) do { \
7581 + mutex_lock(&au_dbg_mtx); \
7583 + au_dpri_inode(i); \
7584 + mutex_unlock(&au_dbg_mtx); \
7587 +#define AuDbgDAlias(i) do { \
7588 + mutex_lock(&au_dbg_mtx); \
7590 + au_dpri_dalias(i); \
7591 + mutex_unlock(&au_dbg_mtx); \
7594 +#define AuDbgDentry(d) do { \
7595 + mutex_lock(&au_dbg_mtx); \
7597 + au_dpri_dentry(d); \
7598 + mutex_unlock(&au_dbg_mtx); \
7601 +#define AuDbgFile(f) do { \
7602 + mutex_lock(&au_dbg_mtx); \
7604 + au_dpri_file(f); \
7605 + mutex_unlock(&au_dbg_mtx); \
7608 +#define AuDbgSb(sb) do { \
7609 + mutex_lock(&au_dbg_mtx); \
7610 + AuDbg(#sb "\n"); \
7612 + mutex_unlock(&au_dbg_mtx); \
7615 +#define AuDbgSym(addr) do { \
7616 + char sym[KSYM_SYMBOL_LEN]; \
7617 + sprint_symbol(sym, (unsigned long)addr); \
7618 + AuDbg("%s\n", sym); \
7621 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7622 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7623 +AuStubVoid(au_dbg_verify_kthread, void)
7624 +AuStubInt0(__init au_debug_init, void)
7626 +#define AuDbgWhlist(w) do {} while (0)
7627 +#define AuDbgVdir(v) do {} while (0)
7628 +#define AuDbgInode(i) do {} while (0)
7629 +#define AuDbgDAlias(i) do {} while (0)
7630 +#define AuDbgDentry(d) do {} while (0)
7631 +#define AuDbgFile(f) do {} while (0)
7632 +#define AuDbgSb(sb) do {} while (0)
7633 +#define AuDbgSym(addr) do {} while (0)
7634 +#endif /* CONFIG_AUFS_DEBUG */
7636 +/* ---------------------------------------------------------------------- */
7638 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7639 +int __init au_sysrq_init(void);
7640 +void au_sysrq_fin(void);
7642 +#ifdef CONFIG_HW_CONSOLE
7643 +#define au_dbg_blocked() do { \
7645 + handle_sysrq('w'); \
7648 +AuStubVoid(au_dbg_blocked, void)
7652 +AuStubInt0(__init au_sysrq_init, void)
7653 +AuStubVoid(au_sysrq_fin, void)
7654 +AuStubVoid(au_dbg_blocked, void)
7655 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7657 +#endif /* __KERNEL__ */
7658 +#endif /* __AUFS_DEBUG_H__ */
7659 diff --git a/fs/aufs/dentry.c b/fs/aufs/dentry.c
7660 new file mode 100644
7661 index 00000000000000..ed7ce8f6d1a1bf
7663 +++ b/fs/aufs/dentry.c
7665 +// SPDX-License-Identifier: GPL-2.0
7667 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7669 + * This program is free software; you can redistribute it and/or modify
7670 + * it under the terms of the GNU General Public License as published by
7671 + * the Free Software Foundation; either version 2 of the License, or
7672 + * (at your option) any later version.
7674 + * This program is distributed in the hope that it will be useful,
7675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7677 + * GNU General Public License for more details.
7679 + * You should have received a copy of the GNU General Public License
7680 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
7684 + * lookup and dentry operations
7687 +#include <linux/iversion.h>
7691 + * returns positive/negative dentry, NULL or an error.
7692 + * NULL means whiteout-ed or not-found.
7694 +static struct dentry*
7695 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7696 + aufs_bindex_t bindex, struct au_do_lookup_args *args)
7698 + struct dentry *h_dentry;
7699 + struct inode *h_inode;
7700 + struct au_branch *br;
7701 + struct mnt_idmap *h_idmap;
7702 + struct path h_path;
7703 + int wh_found, opq;
7704 + unsigned char wh_able;
7705 + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7706 + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7710 + br = au_sbr(dentry->d_sb, bindex);
7711 + h_path.dentry = h_parent;
7712 + h_path.mnt = au_br_mnt(br);
7713 + h_idmap = au_br_idmap(br);
7714 + wh_able = !!au_br_whable(br->br_perm);
7716 + wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
7718 + h_dentry = ERR_PTR(wh_found);
7721 + if (unlikely(wh_found < 0))
7724 + /* We found a whiteout */
7725 + /* au_set_dbbot(dentry, bindex); */
7726 + au_set_dbwh(dentry, bindex);
7728 + return NULL; /* success */
7732 + h_dentry = vfsub_lkup_one(args->name, &h_path);
7734 + h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
7735 + if (IS_ERR(h_dentry)) {
7736 + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7742 + h_inode = d_inode(h_dentry);
7743 + if (d_is_negative(h_dentry)) {
7746 + } else if (wh_found
7747 + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7749 + else if (au_ftest_lkup(args->flags, DIRREN)
7751 + && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
7752 + AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
7753 + (unsigned long long)h_inode->i_ino);
7757 + if (au_dbbot(dentry) <= bindex)
7758 + au_set_dbbot(dentry, bindex);
7759 + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
7760 + au_set_dbtop(dentry, bindex);
7761 + au_set_h_dptr(dentry, bindex, h_dentry);
7763 + if (!d_is_dir(h_dentry)
7765 + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7766 + goto out; /* success */
7768 + h_path.dentry = h_dentry;
7769 + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
7770 + opq = au_diropq_test(h_idmap, &h_path);
7771 + inode_unlock_shared(h_inode);
7773 + au_set_dbdiropq(dentry, bindex);
7774 + else if (unlikely(opq < 0)) {
7775 + au_set_h_dptr(dentry, bindex, NULL);
7776 + h_dentry = ERR_PTR(opq);
7787 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7789 + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7790 + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7796 + * returns the number of lower positive dentries,
7797 + * otherwise an error.
7798 + * can be called at unlinking with @type is zero.
7800 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
7801 + unsigned int flags)
7803 + int npositive, err;
7804 + aufs_bindex_t bindex, btail, bdiropq;
7805 + unsigned char isdir, dirperm1, dirren;
7806 + struct au_do_lookup_args args = {
7808 + .name = &dentry->d_name
7810 + struct dentry *parent;
7811 + struct super_block *sb;
7813 + sb = dentry->d_sb;
7814 + err = au_test_shwh(sb, args.name);
7815 + if (unlikely(err))
7818 + err = au_wh_name_alloc(&args.whname, args.name);
7819 + if (unlikely(err))
7822 + isdir = !!d_is_dir(dentry);
7823 + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7824 + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
7826 + au_fset_lkup(args.flags, DIRREN);
7829 + parent = dget_parent(dentry);
7830 + btail = au_dbtaildir(parent);
7831 + for (bindex = btop; bindex <= btail; bindex++) {
7832 + struct dentry *h_parent, *h_dentry;
7833 + struct inode *h_inode, *h_dir;
7834 + struct au_branch *br;
7836 + h_dentry = au_h_dptr(dentry, bindex);
7838 + if (d_is_positive(h_dentry))
7842 + h_parent = au_h_dptr(parent, bindex);
7843 + if (!h_parent || !d_is_dir(h_parent))
7847 + /* if the inum matches, then use the prepared name */
7848 + err = au_dr_lkup_name(&args, bindex);
7849 + if (unlikely(err))
7853 + h_dir = d_inode(h_parent);
7854 + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7855 + h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
7856 + inode_unlock_shared(h_dir);
7857 + err = PTR_ERR(h_dentry);
7858 + if (IS_ERR(h_dentry))
7861 + au_fclr_lkup(args.flags, ALLOW_NEG);
7863 + au_fset_lkup(args.flags, IGNORE_PERM);
7865 + if (au_dbwh(dentry) == bindex)
7869 + if (d_is_negative(h_dentry))
7871 + h_inode = d_inode(h_dentry);
7874 + args.type = h_inode->i_mode & S_IFMT;
7875 + if (args.type != S_IFDIR)
7878 + /* the type of lower may be different */
7879 + bdiropq = au_dbdiropq(dentry);
7880 + if (bdiropq >= 0 && bdiropq <= bindex)
7883 + br = au_sbr(sb, bindex);
7885 + && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
7886 + /*add_ent*/NULL)) {
7887 + /* prepare next name to lookup */
7888 + err = au_dr_lkup(&args, dentry, bindex);
7889 + if (unlikely(err))
7895 + AuLabel(positive);
7896 + au_update_dbtop(dentry);
7899 + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7900 + && au_dbtop(dentry) < 0)) {
7902 + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7908 + au_kfree_try_rcu(args.whname.name);
7910 + au_dr_lkup_fin(&args);
7915 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
7916 + struct path *ppath)
7918 + struct dentry *dentry;
7921 + if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
7922 + dentry = vfsub_lkup_one(name, ppath);
7924 + struct vfsub_lkup_one_args args = {
7930 + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7931 + if (unlikely(wkq_err))
7932 + dentry = ERR_PTR(wkq_err);
7939 + * lookup @dentry on @bindex which should be negative.
7941 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7944 + struct dentry *parent, *h_dentry;
7945 + struct au_branch *br;
7946 + struct mnt_idmap *h_idmap;
7947 + struct path h_ppath;
7949 + parent = dget_parent(dentry);
7950 + br = au_sbr(dentry->d_sb, bindex);
7951 + h_ppath.dentry = au_h_dptr(parent, bindex);
7952 + h_ppath.mnt = au_br_mnt(br);
7953 + h_idmap = au_br_idmap(br);
7955 + h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
7957 + h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
7958 + err = PTR_ERR(h_dentry);
7959 + if (IS_ERR(h_dentry))
7961 + if (unlikely(d_is_positive(h_dentry))) {
7963 + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7969 + if (bindex < au_dbtop(dentry))
7970 + au_set_dbtop(dentry, bindex);
7971 + if (au_dbbot(dentry) < bindex)
7972 + au_set_dbbot(dentry, bindex);
7973 + au_set_h_dptr(dentry, bindex, h_dentry);
7980 +/* ---------------------------------------------------------------------- */
7982 +/* subset of struct inode */
7984 + unsigned long i_ino;
7985 + /* unsigned int i_nlink; */
7991 + blkcnt_t i_blocks;
7996 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7998 + ia->i_ino = h_inode->i_ino;
7999 + /* ia->i_nlink = h_inode->i_nlink; */
8000 + ia->i_uid = h_inode->i_uid;
8001 + ia->i_gid = h_inode->i_gid;
8002 + ia->i_version = inode_query_iversion(h_inode);
8004 + ia->i_size = h_inode->i_size;
8005 + ia->i_blocks = h_inode->i_blocks;
8007 + ia->i_mode = (h_inode->i_mode & S_IFMT);
8010 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8012 + return ia->i_ino != h_inode->i_ino
8013 + /* || ia->i_nlink != h_inode->i_nlink */
8014 + || !uid_eq(ia->i_uid, h_inode->i_uid)
8015 + || !gid_eq(ia->i_gid, h_inode->i_gid)
8016 + || !inode_eq_iversion(h_inode, ia->i_version)
8018 + || ia->i_size != h_inode->i_size
8019 + || ia->i_blocks != h_inode->i_blocks
8021 + || ia->i_mode != (h_inode->i_mode & S_IFMT);
8024 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8025 + struct au_branch *br)
8028 + struct au_iattr ia;
8029 + struct inode *h_inode;
8030 + struct dentry *h_d;
8031 + struct super_block *h_sb;
8032 + struct path h_ppath;
8035 + memset(&ia, -1, sizeof(ia));
8036 + h_sb = h_dentry->d_sb;
8038 + if (d_is_positive(h_dentry)) {
8039 + h_inode = d_inode(h_dentry);
8040 + au_iattr_save(&ia, h_inode);
8041 + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8042 + /* nfs d_revalidate may return 0 for negative dentry */
8043 + /* fuse d_revalidate always return 0 for negative dentry */
8046 + /* main purpose is namei.c:cached_lookup() and d_revalidate */
8047 + h_ppath.dentry = h_parent;
8048 + h_ppath.mnt = au_br_mnt(br);
8049 + h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8050 + err = PTR_ERR(h_d);
8055 + if (unlikely(h_d != h_dentry
8056 + || d_inode(h_d) != h_inode
8057 + || (h_inode && au_iattr_test(&ia, h_inode))))
8058 + err = au_busy_or_stale();
8066 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8067 + struct dentry *h_parent, struct au_branch *br)
8072 + if (udba == AuOpt_UDBA_REVAL
8073 + && !au_test_fs_remote(h_dentry->d_sb)) {
8075 + err = (d_inode(h_dentry->d_parent) != h_dir);
8076 + } else if (udba != AuOpt_UDBA_NONE)
8077 + err = au_h_verify_dentry(h_dentry, h_parent, br);
8082 +/* ---------------------------------------------------------------------- */
8084 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8087 + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8088 + struct au_hdentry tmp, *p, *q;
8089 + struct au_dinfo *dinfo;
8090 + struct super_block *sb;
8092 + DiMustWriteLock(dentry);
8094 + sb = dentry->d_sb;
8095 + dinfo = au_di(dentry);
8096 + bbot = dinfo->di_bbot;
8097 + bwh = dinfo->di_bwh;
8098 + bdiropq = dinfo->di_bdiropq;
8099 + bindex = dinfo->di_btop;
8100 + p = au_hdentry(dinfo, bindex);
8101 + for (; bindex <= bbot; bindex++, p++) {
8102 + if (!p->hd_dentry)
8105 + new_bindex = au_br_index(sb, p->hd_id);
8106 + if (new_bindex == bindex)
8109 + if (dinfo->di_bwh == bindex)
8111 + if (dinfo->di_bdiropq == bindex)
8112 + bdiropq = new_bindex;
8113 + if (new_bindex < 0) {
8115 + p->hd_dentry = NULL;
8119 + /* swap two lower dentries, and loop again */
8120 + q = au_hdentry(dinfo, new_bindex);
8124 + if (tmp.hd_dentry) {
8130 + dinfo->di_bwh = -1;
8131 + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8132 + dinfo->di_bwh = bwh;
8134 + dinfo->di_bdiropq = -1;
8136 + && bdiropq <= au_sbbot(sb)
8137 + && au_sbr_whable(sb, bdiropq))
8138 + dinfo->di_bdiropq = bdiropq;
8141 + dinfo->di_btop = -1;
8142 + dinfo->di_bbot = -1;
8143 + bbot = au_dbbot(parent);
8145 + p = au_hdentry(dinfo, bindex);
8146 + for (; bindex <= bbot; bindex++, p++)
8147 + if (p->hd_dentry) {
8148 + dinfo->di_btop = bindex;
8152 + if (dinfo->di_btop >= 0) {
8154 + p = au_hdentry(dinfo, bindex);
8155 + for (; bindex >= 0; bindex--, p--)
8156 + if (p->hd_dentry) {
8157 + dinfo->di_bbot = bindex;
8166 +static void au_do_hide(struct dentry *dentry)
8168 + struct inode *inode;
8170 + if (d_really_is_positive(dentry)) {
8171 + inode = d_inode(dentry);
8172 + if (!d_is_dir(dentry)) {
8173 + if (inode->i_nlink && !d_unhashed(dentry))
8174 + drop_nlink(inode);
8176 + clear_nlink(inode);
8177 + /* stop next lookup */
8178 + inode->i_flags |= S_DEAD;
8180 + smp_mb(); /* necessary? */
8185 +static int au_hide_children(struct dentry *parent)
8187 + int err, i, j, ndentry;
8188 + struct au_dcsub_pages dpages;
8189 + struct au_dpage *dpage;
8190 + struct dentry *dentry;
8192 + err = au_dpages_init(&dpages, GFP_NOFS);
8193 + if (unlikely(err))
8195 + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8196 + if (unlikely(err))
8199 + /* in reverse order */
8200 + for (i = dpages.ndpage - 1; i >= 0; i--) {
8201 + dpage = dpages.dpages + i;
8202 + ndentry = dpage->ndentry;
8203 + for (j = ndentry - 1; j >= 0; j--) {
8204 + dentry = dpage->dentries[j];
8205 + if (dentry != parent)
8206 + au_do_hide(dentry);
8211 + au_dpages_free(&dpages);
8216 +static void au_hide(struct dentry *dentry)
8220 + AuDbgDentry(dentry);
8221 + if (d_is_dir(dentry)) {
8222 + /* shrink_dcache_parent(dentry); */
8223 + err = au_hide_children(dentry);
8224 + if (unlikely(err))
8225 + AuIOErr("%pd, failed hiding children, ignored %d\n",
8228 + au_do_hide(dentry);
8232 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8234 + * a dirty branch is added
8235 + * - on the top of layers
8236 + * - in the middle of layers
8237 + * - to the bottom of layers
8239 + * on the added branch there exists
8242 + * - a same named entry
8244 + * * negative --> positive
8245 + * * positive --> positive
8246 + * - type is unchanged
8247 + * - type is changed
8249 + * * negative --> negative
8250 + * * positive --> negative (rejected by au_br_del() for non-dir case)
8253 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8254 + struct au_dinfo *tmp)
8257 + aufs_bindex_t bindex, bbot;
8259 + struct dentry *dentry;
8260 + struct inode *inode;
8262 + } orig_h, tmp_h = {
8265 + struct au_hdentry *hd;
8266 + struct inode *inode, *h_inode;
8267 + struct dentry *h_dentry;
8270 + AuDebugOn(dinfo->di_btop < 0);
8272 + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8273 + orig_h.inode = NULL;
8274 + if (d_is_positive(orig_h.dentry)) {
8275 + orig_h.inode = d_inode(orig_h.dentry);
8276 + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8278 + if (tmp->di_btop >= 0) {
8279 + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8280 + if (d_is_positive(tmp_h.dentry)) {
8281 + tmp_h.inode = d_inode(tmp_h.dentry);
8282 + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8287 + if (d_really_is_positive(dentry))
8288 + inode = d_inode(dentry);
8289 + if (!orig_h.inode) {
8290 + AuDbg("negative originally\n");
8296 + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8297 + AuDebugOn(dinfo->di_bdiropq != -1);
8299 + if (!tmp_h.inode) {
8300 + AuDbg("negative --> negative\n");
8301 + /* should have only one negative lower */
8302 + if (tmp->di_btop >= 0
8303 + && tmp->di_btop < dinfo->di_btop) {
8304 + AuDebugOn(tmp->di_btop != tmp->di_bbot);
8305 + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8306 + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8307 + au_di_cp(dinfo, tmp);
8308 + hd = au_hdentry(tmp, tmp->di_btop);
8309 + au_set_h_dptr(dentry, tmp->di_btop,
8310 + dget(hd->hd_dentry));
8312 + au_dbg_verify_dinode(dentry);
8314 + AuDbg("negative --> positive\n");
8316 + * similar to the behaviour of creating with bypassing
8318 + * unhash it in order to force an error in the
8319 + * succeeding create operation.
8320 + * we should not set S_DEAD here.
8323 + /* au_di_swap(tmp, dinfo); */
8324 + au_dbg_verify_dinode(dentry);
8327 + AuDbg("positive originally\n");
8328 + /* inode may be NULL */
8329 + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8330 + if (!tmp_h.inode) {
8331 + AuDbg("positive --> negative\n");
8332 + /* or bypassing aufs */
8334 + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8335 + dinfo->di_bwh = tmp->di_bwh;
8337 + err = au_refresh_hinode_self(inode);
8338 + au_dbg_verify_dinode(dentry);
8339 + } else if (orig_h.mode == tmp_h.mode) {
8340 + AuDbg("positive --> positive, same type\n");
8341 + if (!S_ISDIR(orig_h.mode)
8342 + && dinfo->di_btop > tmp->di_btop) {
8344 + * similar to the behaviour of removing and
8349 + err = au_refresh_hinode_self(inode);
8350 + au_dbg_verify_dinode(dentry);
8352 + /* fill empty slots */
8353 + if (dinfo->di_btop > tmp->di_btop)
8354 + dinfo->di_btop = tmp->di_btop;
8355 + if (dinfo->di_bbot < tmp->di_bbot)
8356 + dinfo->di_bbot = tmp->di_bbot;
8357 + dinfo->di_bwh = tmp->di_bwh;
8358 + dinfo->di_bdiropq = tmp->di_bdiropq;
8359 + bbot = dinfo->di_bbot;
8360 + bindex = tmp->di_btop;
8361 + hd = au_hdentry(tmp, bindex);
8362 + for (; bindex <= bbot; bindex++, hd++) {
8363 + if (au_h_dptr(dentry, bindex))
8365 + h_dentry = hd->hd_dentry;
8368 + AuDebugOn(d_is_negative(h_dentry));
8369 + h_inode = d_inode(h_dentry);
8370 + AuDebugOn(orig_h.mode
8371 + != (h_inode->i_mode
8373 + au_set_h_dptr(dentry, bindex,
8377 + err = au_refresh_hinode(inode, dentry);
8378 + au_dbg_verify_dinode(dentry);
8381 + AuDbg("positive --> positive, different type\n");
8382 + /* similar to the behaviour of removing and creating */
8385 + err = au_refresh_hinode_self(inode);
8386 + au_dbg_verify_dinode(dentry);
8394 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8396 + const struct dentry_operations *dop
8397 + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8398 + static const unsigned int mask
8399 + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8401 + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8403 + if (dentry->d_op == dop)
8406 + AuDbg("%pd\n", dentry);
8407 + spin_lock(&dentry->d_lock);
8408 + if (dop == &aufs_dop)
8409 + dentry->d_flags |= mask;
8411 + dentry->d_flags &= ~mask;
8412 + dentry->d_op = dop;
8413 + spin_unlock(&dentry->d_lock);
8416 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8418 + int err, ebrange, nbr;
8419 + unsigned int sigen;
8420 + struct au_dinfo *dinfo, *tmp;
8421 + struct super_block *sb;
8422 + struct inode *inode;
8424 + DiMustWriteLock(dentry);
8425 + AuDebugOn(IS_ROOT(dentry));
8426 + AuDebugOn(d_really_is_negative(parent));
8428 + sb = dentry->d_sb;
8429 + sigen = au_sigen(sb);
8430 + err = au_digen_test(parent, sigen);
8431 + if (unlikely(err))
8434 + nbr = au_sbbot(sb) + 1;
8435 + dinfo = au_di(dentry);
8436 + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8437 + if (unlikely(err))
8439 + ebrange = au_dbrange_test(dentry);
8441 + ebrange = au_do_refresh_hdentry(dentry, parent);
8443 + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8444 + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8445 + if (d_really_is_positive(dentry)) {
8446 + inode = d_inode(dentry);
8447 + err = au_refresh_hinode_self(inode);
8449 + au_dbg_verify_dinode(dentry);
8451 + goto out_dgen; /* success */
8455 + /* temporary dinfo */
8456 + AuDbgDentry(dentry);
8458 + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8459 + if (unlikely(!tmp))
8461 + au_di_swap(tmp, dinfo);
8462 + /* returns the number of positive dentries */
8464 + * if current working dir is removed, it returns an error.
8465 + * but the dentry is legal.
8467 + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8468 + AuDbgDentry(dentry);
8469 + au_di_swap(tmp, dinfo);
8470 + if (err == -ENOENT)
8473 + /* compare/refresh by dinfo */
8474 + AuDbgDentry(dentry);
8475 + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8476 + au_dbg_verify_dinode(dentry);
8479 + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8480 + au_rw_write_unlock(&tmp->di_rwsem);
8482 + if (unlikely(err))
8486 + au_update_digen(dentry);
8488 + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8489 + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8490 + AuDbgDentry(dentry);
8496 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8497 + struct dentry *dentry, aufs_bindex_t bindex)
8502 + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8505 + AuDbg("b%d\n", bindex);
8507 + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8508 + * due to whiteout and branch permission.
8510 + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8511 + | LOOKUP_FOLLOW | LOOKUP_EXCL);
8512 + /* it may return tri-state */
8513 + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8515 + if (unlikely(valid < 0))
8525 +/* todo: remove this */
8526 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8527 + unsigned int flags, int do_udba, int dirren)
8530 + umode_t mode, h_mode;
8531 + aufs_bindex_t bindex, btail, btop, ibs, ibe;
8532 + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8533 + struct inode *h_inode, *h_cached_inode;
8534 + struct dentry *h_dentry;
8535 + struct qstr *name, *h_name;
8542 + unhashed = !!d_unhashed(dentry);
8543 + is_root = !!IS_ROOT(dentry);
8544 + name = &dentry->d_name;
8545 + tmpfile = au_di(dentry)->di_tmpfile;
8548 + * Theoretically, REVAL test should be unnecessary in case of
8550 + * But {fs,i}notify doesn't fire some necessary events,
8551 + * IN_ATTRIB for atime/nlink/pageio
8552 + * Let's do REVAL test too.
8554 + if (do_udba && inode) {
8555 + mode = (inode->i_mode & S_IFMT);
8556 + plus = (inode->i_nlink > 0);
8557 + ibs = au_ibtop(inode);
8558 + ibe = au_ibbot(inode);
8561 + btop = au_dbtop(dentry);
8563 + if (inode && S_ISDIR(inode->i_mode))
8564 + btail = au_dbtaildir(dentry);
8565 + for (bindex = btop; bindex <= btail; bindex++) {
8566 + h_dentry = au_h_dptr(dentry, bindex);
8570 + AuDbg("b%d, %pd\n", bindex, h_dentry);
8571 + h_nfs = !!au_test_nfs(h_dentry->d_sb);
8572 + spin_lock(&h_dentry->d_lock);
8573 + h_name = &h_dentry->d_name;
8574 + if (unlikely(do_udba
8577 + && (unhashed != !!d_unhashed(h_dentry)
8578 + || (!tmpfile && !dirren
8579 + && !au_qstreq(name, h_name))
8582 + && !(flags & LOOKUP_OPEN)
8583 + && (h_dentry->d_flags
8584 + & DCACHE_NFSFS_RENAMED)))
8588 + h_unhashed = d_unhashed(h_dentry);
8589 + spin_unlock(&h_dentry->d_lock);
8590 + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8591 + unhashed, h_unhashed, dentry, h_dentry);
8594 + spin_unlock(&h_dentry->d_lock);
8596 + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8597 + if (unlikely(err))
8598 + /* do not goto err, to keep the errno */
8601 + /* todo: plink too? */
8606 + if (unlikely(!!inode != d_is_positive(h_dentry)))
8610 + if (d_is_positive(h_dentry))
8611 + h_inode = d_inode(h_dentry);
8614 + h_cached_inode = h_inode;
8616 + h_mode = (h_inode->i_mode & S_IFMT);
8617 + h_plus = (h_inode->i_nlink > 0);
8619 + if (inode && ibs <= bindex && bindex <= ibe)
8620 + h_cached_inode = au_h_iptr(inode, bindex);
8623 + if (unlikely(plus != h_plus && !tmpfile))
8626 + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8628 + && !IS_ROOT(h_dentry)
8629 + && unhashed != d_unhashed(h_dentry)))
8632 + if (unlikely(mode != h_mode
8633 + || h_cached_inode != h_inode))
8646 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8647 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8650 + struct dentry *parent;
8652 + if (!au_digen_test(dentry, sigen))
8655 + parent = dget_parent(dentry);
8656 + di_read_lock_parent(parent, AuLock_IR);
8657 + AuDebugOn(au_digen_test(parent, sigen));
8658 + au_dbg_verify_gen(parent, sigen);
8659 + err = au_refresh_dentry(dentry, parent);
8660 + di_read_unlock(parent, AuLock_IR);
8666 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8669 + struct dentry *d, *parent;
8671 + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8672 + return simple_reval_dpath(dentry, sigen);
8674 + /* slow loop, keep it simple and stupid */
8675 + /* cf: au_cpup_dirs() */
8678 + while (au_digen_test(dentry, sigen)) {
8682 + parent = dget_parent(d);
8683 + if (!au_digen_test(parent, sigen))
8689 + di_write_lock_child2(d);
8691 + /* someone might update our dentry while we were sleeping */
8692 + if (au_digen_test(d, sigen)) {
8694 + * todo: consolidate with simple_reval_dpath(),
8695 + * do_refresh() and au_reval_for_attr().
8697 + di_read_lock_parent(parent, AuLock_IR);
8698 + err = au_refresh_dentry(d, parent);
8699 + di_read_unlock(parent, AuLock_IR);
8703 + di_write_unlock(d);
8705 + if (unlikely(err))
8713 + * if valid returns 1, otherwise 0.
8715 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8718 + unsigned int sigen;
8719 + unsigned char do_udba, dirren;
8720 + struct super_block *sb;
8721 + struct inode *inode;
8723 + /* todo: support rcu-walk? */
8724 + if (flags & LOOKUP_RCU)
8728 + if (unlikely(!au_di(dentry)))
8732 + sb = dentry->d_sb;
8735 + * i_mutex of parent dir may be held,
8736 + * but we should not return 'invalid' due to busy.
8738 + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8739 + if (unlikely(err)) {
8745 + if (d_really_is_positive(dentry))
8746 + inode = d_inode(dentry);
8747 + if (unlikely(inode && au_is_bad_inode(inode))) {
8752 + if (unlikely(au_dbrange_test(dentry))) {
8758 + sigen = au_sigen(sb);
8759 + if (au_digen_test(dentry, sigen)) {
8760 + AuDebugOn(IS_ROOT(dentry));
8761 + err = au_reval_dpath(dentry, sigen);
8762 + if (unlikely(err)) {
8767 + di_downgrade_lock(dentry, AuLock_IR);
8770 + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8772 + && !(inode->i_state && I_LINKABLE)
8773 + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8778 + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8779 + if (do_udba && inode) {
8780 + aufs_bindex_t btop = au_ibtop(inode);
8781 + struct inode *h_inode;
8784 + h_inode = au_h_iptr(inode, btop);
8785 + if (h_inode && au_test_higen(inode, h_inode)) {
8792 + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8793 + err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
8794 + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
8796 + AuDbg("both of real entry and whiteout found, %p, err %d\n",
8802 + di_downgrade_lock(dentry, AuLock_IR);
8804 + aufs_read_unlock(dentry, AuLock_IR);
8809 + AuDbg("%pd invalid, %d\n", dentry, valid);
8815 +static void aufs_d_release(struct dentry *dentry)
8817 + if (au_di(dentry)) {
8818 + au_di_fin(dentry);
8819 + au_hn_di_reinit(dentry);
8823 +const struct dentry_operations aufs_dop = {
8824 + .d_revalidate = aufs_d_revalidate,
8825 + .d_weak_revalidate = aufs_d_revalidate,
8826 + .d_release = aufs_d_release
8829 +/* aufs_dop without d_revalidate */
8830 +const struct dentry_operations aufs_dop_noreval = {
8831 + .d_release = aufs_d_release
8833 diff --git a/fs/aufs/dentry.h b/fs/aufs/dentry.h
8834 new file mode 100644
8835 index 00000000000000..da4c9063d7e1c8
8837 +++ b/fs/aufs/dentry.h
8839 +/* SPDX-License-Identifier: GPL-2.0 */
8841 + * Copyright (C) 2005-2022 Junjiro R. Okajima
8843 + * This program is free software; you can redistribute it and/or modify
8844 + * it under the terms of the GNU General Public License as published by
8845 + * the Free Software Foundation; either version 2 of the License, or
8846 + * (at your option) any later version.
8848 + * This program is distributed in the hope that it will be useful,
8849 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8850 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8851 + * GNU General Public License for more details.
8853 + * You should have received a copy of the GNU General Public License
8854 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
8858 + * lookup and dentry operations
8861 +#ifndef __AUFS_DENTRY_H__
8862 +#define __AUFS_DENTRY_H__
8866 +#include <linux/dcache.h>
8867 +#include "dirren.h"
8870 +struct au_hdentry {
8871 + struct dentry *hd_dentry;
8872 + aufs_bindex_t hd_id;
8876 + atomic_t di_generation;
8878 + struct au_rwsem di_rwsem;
8879 + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
8880 + unsigned char di_tmpfile; /* to allow the different name */
8881 + struct au_hdentry *di_hdentry;
8882 + struct file *di_htmpfile;
8883 + struct rcu_head rcu;
8884 +} ____cacheline_aligned_in_smp;
8886 +/* ---------------------------------------------------------------------- */
8888 +/* flags for au_lkup_dentry() */
8889 +#define AuLkup_ALLOW_NEG 1
8890 +#define AuLkup_IGNORE_PERM (1 << 1)
8891 +#define AuLkup_DIRREN (1 << 2)
8892 +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
8893 +#define au_fset_lkup(flags, name) \
8894 + do { (flags) |= AuLkup_##name; } while (0)
8895 +#define au_fclr_lkup(flags, name) \
8896 + do { (flags) &= ~AuLkup_##name; } while (0)
8898 +#ifndef CONFIG_AUFS_DIRREN
8899 +#undef AuLkup_DIRREN
8900 +#define AuLkup_DIRREN 0
8903 +struct au_do_lookup_args {
8904 + unsigned int flags;
8906 + struct qstr whname, *name;
8907 + struct au_dr_lookup dirren;
8910 +/* ---------------------------------------------------------------------- */
8913 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
8915 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8916 + struct path *ppath);
8917 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8918 + struct dentry *h_parent, struct au_branch *br);
8920 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8921 + unsigned int flags);
8922 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8923 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8924 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8925 +void au_refresh_dop(struct dentry *dentry, int force_reval);
8928 +void au_di_init_once(void *_di);
8929 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8930 +void au_di_free(struct au_dinfo *dinfo);
8931 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8932 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8933 +int au_di_init(struct dentry *dentry);
8934 +void au_di_fin(struct dentry *dentry);
8935 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
8937 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8938 +void di_read_unlock(struct dentry *d, int flags);
8939 +void di_downgrade_lock(struct dentry *d, int flags);
8940 +void di_write_lock(struct dentry *d, unsigned int lsc);
8941 +void di_write_unlock(struct dentry *d);
8942 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8943 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8944 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8946 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8947 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8948 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8949 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8951 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8952 + struct dentry *h_dentry);
8953 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8954 +int au_dbrange_test(struct dentry *dentry);
8955 +void au_update_digen(struct dentry *dentry);
8956 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8957 +void au_update_dbtop(struct dentry *dentry);
8958 +void au_update_dbbot(struct dentry *dentry);
8959 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8961 +/* ---------------------------------------------------------------------- */
8963 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8965 + return dentry->d_fsdata;
8968 +/* ---------------------------------------------------------------------- */
8970 +/* lock subclass for dinfo */
8972 + AuLsc_DI_CHILD, /* child first */
8973 + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
8974 + AuLsc_DI_CHILD3, /* copyup dirs */
8978 + AuLsc_DI_TMP /* temp for replacing dinfo */
8982 + * di_read_lock_child, di_write_lock_child,
8983 + * di_read_lock_child2, di_write_lock_child2,
8984 + * di_read_lock_child3, di_write_lock_child3,
8985 + * di_read_lock_parent, di_write_lock_parent,
8986 + * di_read_lock_parent2, di_write_lock_parent2,
8987 + * di_read_lock_parent3, di_write_lock_parent3,
8989 +#define AuReadLockFunc(name, lsc) \
8990 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8991 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8993 +#define AuWriteLockFunc(name, lsc) \
8994 +static inline void di_write_lock_##name(struct dentry *d) \
8995 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8997 +#define AuRWLockFuncs(name, lsc) \
8998 + AuReadLockFunc(name, lsc) \
8999 + AuWriteLockFunc(name, lsc)
9001 +AuRWLockFuncs(child, CHILD);
9002 +AuRWLockFuncs(child2, CHILD2);
9003 +AuRWLockFuncs(child3, CHILD3);
9004 +AuRWLockFuncs(parent, PARENT);
9005 +AuRWLockFuncs(parent2, PARENT2);
9006 +AuRWLockFuncs(parent3, PARENT3);
9008 +#undef AuReadLockFunc
9009 +#undef AuWriteLockFunc
9010 +#undef AuRWLockFuncs
9012 +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9013 +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9014 +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
9016 +/* ---------------------------------------------------------------------- */
9018 +/* todo: memory barrier? */
9019 +static inline unsigned int au_digen(struct dentry *d)
9021 + return atomic_read(&au_di(d)->di_generation);
9024 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9026 + hdentry->hd_dentry = NULL;
9029 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9030 + aufs_bindex_t bindex)
9032 + return di->di_hdentry + bindex;
9035 +static inline void au_hdput(struct au_hdentry *hd)
9038 + dput(hd->hd_dentry);
9041 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9043 + DiMustAnyLock(dentry);
9044 + return au_di(dentry)->di_btop;
9047 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9049 + DiMustAnyLock(dentry);
9050 + return au_di(dentry)->di_bbot;
9053 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9055 + DiMustAnyLock(dentry);
9056 + return au_di(dentry)->di_bwh;
9059 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9061 + DiMustAnyLock(dentry);
9062 + return au_di(dentry)->di_bdiropq;
9065 +/* todo: hard/soft set? */
9066 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9068 + DiMustWriteLock(dentry);
9069 + au_di(dentry)->di_btop = bindex;
9072 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9074 + DiMustWriteLock(dentry);
9075 + au_di(dentry)->di_bbot = bindex;
9078 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9080 + DiMustWriteLock(dentry);
9081 + /* dbwh can be outside of btop - bbot range */
9082 + au_di(dentry)->di_bwh = bindex;
9085 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9087 + DiMustWriteLock(dentry);
9088 + au_di(dentry)->di_bdiropq = bindex;
9091 +/* ---------------------------------------------------------------------- */
9093 +#ifdef CONFIG_AUFS_HNOTIFY
9094 +static inline void au_digen_dec(struct dentry *d)
9096 + atomic_dec(&au_di(d)->di_generation);
9099 +static inline void au_hn_di_reinit(struct dentry *dentry)
9101 + dentry->d_fsdata = NULL;
9104 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9105 +#endif /* CONFIG_AUFS_HNOTIFY */
9107 +#endif /* __KERNEL__ */
9108 +#endif /* __AUFS_DENTRY_H__ */
9109 diff --git a/fs/aufs/dinfo.c b/fs/aufs/dinfo.c
9110 new file mode 100644
9111 index 00000000000000..bf4a94414309dc
9113 +++ b/fs/aufs/dinfo.c
9115 +// SPDX-License-Identifier: GPL-2.0
9117 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9119 + * This program is free software; you can redistribute it and/or modify
9120 + * it under the terms of the GNU General Public License as published by
9121 + * the Free Software Foundation; either version 2 of the License, or
9122 + * (at your option) any later version.
9124 + * This program is distributed in the hope that it will be useful,
9125 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9126 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9127 + * GNU General Public License for more details.
9129 + * You should have received a copy of the GNU General Public License
9130 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
9134 + * dentry private data
9139 +void au_di_init_once(void *_dinfo)
9141 + struct au_dinfo *dinfo = _dinfo;
9143 + au_rw_init(&dinfo->di_rwsem);
9146 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9148 + struct au_dinfo *dinfo;
9151 + dinfo = au_cache_alloc_dinfo();
9152 + if (unlikely(!dinfo))
9155 + nbr = au_sbbot(sb) + 1;
9158 + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9159 + if (dinfo->di_hdentry) {
9160 + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9161 + dinfo->di_btop = -1;
9162 + dinfo->di_bbot = -1;
9163 + dinfo->di_bwh = -1;
9164 + dinfo->di_bdiropq = -1;
9165 + dinfo->di_tmpfile = 0;
9166 + for (i = 0; i < nbr; i++)
9167 + dinfo->di_hdentry[i].hd_id = -1;
9168 + dinfo->di_htmpfile = NULL;
9172 + au_cache_free_dinfo(dinfo);
9179 +void au_di_free(struct au_dinfo *dinfo)
9181 + struct au_hdentry *p;
9182 + aufs_bindex_t bbot, bindex;
9184 + /* dentry may not be revalidated */
9185 + bindex = dinfo->di_btop;
9186 + if (bindex >= 0) {
9187 + bbot = dinfo->di_bbot;
9188 + p = au_hdentry(dinfo, bindex);
9189 + while (bindex++ <= bbot)
9192 + au_kfree_try_rcu(dinfo->di_hdentry);
9193 + au_cache_free_dinfo(dinfo);
9196 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9198 + struct au_hdentry *p;
9201 + AuRwMustWriteLock(&a->di_rwsem);
9202 + AuRwMustWriteLock(&b->di_rwsem);
9204 +#define DiSwap(v, name) \
9206 + v = a->di_##name; \
9207 + a->di_##name = b->di_##name; \
9208 + b->di_##name = v; \
9211 + DiSwap(p, hdentry);
9215 + DiSwap(bi, bdiropq);
9221 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9223 + AuRwMustWriteLock(&dst->di_rwsem);
9224 + AuRwMustWriteLock(&src->di_rwsem);
9226 + dst->di_btop = src->di_btop;
9227 + dst->di_bbot = src->di_bbot;
9228 + dst->di_bwh = src->di_bwh;
9229 + dst->di_bdiropq = src->di_bdiropq;
9233 +int au_di_init(struct dentry *dentry)
9236 + struct super_block *sb;
9237 + struct au_dinfo *dinfo;
9240 + sb = dentry->d_sb;
9241 + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9243 + atomic_set(&dinfo->di_generation, au_sigen(sb));
9244 + /* smp_mb(); */ /* atomic_set */
9245 + dentry->d_fsdata = dinfo;
9252 +void au_di_fin(struct dentry *dentry)
9254 + struct au_dinfo *dinfo;
9256 + dinfo = au_di(dentry);
9257 + AuRwDestroy(&dinfo->di_rwsem);
9258 + au_di_free(dinfo);
9261 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9264 + struct au_hdentry *hdp;
9266 + AuRwMustWriteLock(&dinfo->di_rwsem);
9269 + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9271 + sz = sizeof(*hdp);
9272 + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9275 + dinfo->di_hdentry = hdp;
9282 +/* ---------------------------------------------------------------------- */
9284 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9287 + case AuLsc_DI_CHILD:
9288 + ii_write_lock_child(inode);
9290 + case AuLsc_DI_CHILD2:
9291 + ii_write_lock_child2(inode);
9293 + case AuLsc_DI_CHILD3:
9294 + ii_write_lock_child3(inode);
9296 + case AuLsc_DI_PARENT:
9297 + ii_write_lock_parent(inode);
9299 + case AuLsc_DI_PARENT2:
9300 + ii_write_lock_parent2(inode);
9302 + case AuLsc_DI_PARENT3:
9303 + ii_write_lock_parent3(inode);
9310 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9313 + case AuLsc_DI_CHILD:
9314 + ii_read_lock_child(inode);
9316 + case AuLsc_DI_CHILD2:
9317 + ii_read_lock_child2(inode);
9319 + case AuLsc_DI_CHILD3:
9320 + ii_read_lock_child3(inode);
9322 + case AuLsc_DI_PARENT:
9323 + ii_read_lock_parent(inode);
9325 + case AuLsc_DI_PARENT2:
9326 + ii_read_lock_parent2(inode);
9328 + case AuLsc_DI_PARENT3:
9329 + ii_read_lock_parent3(inode);
9336 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9338 + struct inode *inode;
9340 + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9341 + if (d_really_is_positive(d)) {
9342 + inode = d_inode(d);
9343 + if (au_ftest_lock(flags, IW))
9344 + do_ii_write_lock(inode, lsc);
9345 + else if (au_ftest_lock(flags, IR))
9346 + do_ii_read_lock(inode, lsc);
9350 +void di_read_unlock(struct dentry *d, int flags)
9352 + struct inode *inode;
9354 + if (d_really_is_positive(d)) {
9355 + inode = d_inode(d);
9356 + if (au_ftest_lock(flags, IW)) {
9357 + au_dbg_verify_dinode(d);
9358 + ii_write_unlock(inode);
9359 + } else if (au_ftest_lock(flags, IR)) {
9360 + au_dbg_verify_dinode(d);
9361 + ii_read_unlock(inode);
9364 + au_rw_read_unlock(&au_di(d)->di_rwsem);
9367 +void di_downgrade_lock(struct dentry *d, int flags)
9369 + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9370 + ii_downgrade_lock(d_inode(d));
9371 + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9374 +void di_write_lock(struct dentry *d, unsigned int lsc)
9376 + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9377 + if (d_really_is_positive(d))
9378 + do_ii_write_lock(d_inode(d), lsc);
9381 +void di_write_unlock(struct dentry *d)
9383 + au_dbg_verify_dinode(d);
9384 + if (d_really_is_positive(d))
9385 + ii_write_unlock(d_inode(d));
9386 + au_rw_write_unlock(&au_di(d)->di_rwsem);
9389 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9391 + AuDebugOn(d1 == d2
9392 + || d_inode(d1) == d_inode(d2)
9393 + || d1->d_sb != d2->d_sb);
9395 + if ((isdir && au_test_subdir(d1, d2))
9397 + di_write_lock_child(d1);
9398 + di_write_lock_child2(d2);
9400 + di_write_lock_child(d2);
9401 + di_write_lock_child2(d1);
9405 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9407 + AuDebugOn(d1 == d2
9408 + || d_inode(d1) == d_inode(d2)
9409 + || d1->d_sb != d2->d_sb);
9411 + if ((isdir && au_test_subdir(d1, d2))
9413 + di_write_lock_parent(d1);
9414 + di_write_lock_parent2(d2);
9416 + di_write_lock_parent(d2);
9417 + di_write_lock_parent2(d1);
9421 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9423 + di_write_unlock(d1);
9424 + if (d_inode(d1) == d_inode(d2))
9425 + au_rw_write_unlock(&au_di(d2)->di_rwsem);
9427 + di_write_unlock(d2);
9430 +/* ---------------------------------------------------------------------- */
9432 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9436 + DiMustAnyLock(dentry);
9438 + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9440 + AuDebugOn(bindex < 0);
9441 + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9442 + AuDebugOn(d && au_dcount(d) <= 0);
9447 + * extended version of au_h_dptr().
9448 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9451 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9453 + struct dentry *h_dentry;
9454 + struct inode *inode, *h_inode;
9456 + AuDebugOn(d_really_is_negative(dentry));
9459 + if (au_dbtop(dentry) <= bindex
9460 + && bindex <= au_dbbot(dentry))
9461 + h_dentry = au_h_dptr(dentry, bindex);
9462 + if (h_dentry && !au_d_linkable(h_dentry)) {
9464 + goto out; /* success */
9467 + inode = d_inode(dentry);
9468 + AuDebugOn(bindex < au_ibtop(inode));
9469 + AuDebugOn(au_ibbot(inode) < bindex);
9470 + h_inode = au_h_iptr(inode, bindex);
9471 + h_dentry = d_find_alias(h_inode);
9473 + if (!IS_ERR(h_dentry)) {
9474 + if (!au_d_linkable(h_dentry))
9475 + goto out; /* success */
9481 + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9482 + h_dentry = au_plink_lkup(inode, bindex);
9483 + AuDebugOn(!h_dentry);
9484 + if (!IS_ERR(h_dentry)) {
9485 + if (!au_d_hashed_positive(h_dentry))
9486 + goto out; /* success */
9493 + AuDbgDentry(h_dentry);
9497 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9499 + aufs_bindex_t bbot, bwh;
9501 + bbot = au_dbbot(dentry);
9503 + bwh = au_dbwh(dentry);
9506 + if (0 < bwh && bwh < bbot)
9512 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9514 + aufs_bindex_t bbot, bopq;
9516 + bbot = au_dbtail(dentry);
9518 + bopq = au_dbdiropq(dentry);
9519 + if (0 <= bopq && bopq < bbot)
9525 +/* ---------------------------------------------------------------------- */
9527 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9528 + struct dentry *h_dentry)
9530 + struct au_dinfo *dinfo;
9531 + struct au_hdentry *hd;
9532 + struct au_branch *br;
9534 + DiMustWriteLock(dentry);
9536 + dinfo = au_di(dentry);
9537 + hd = au_hdentry(dinfo, bindex);
9539 + hd->hd_dentry = h_dentry;
9541 + br = au_sbr(dentry->d_sb, bindex);
9542 + hd->hd_id = br->br_id;
9546 +int au_dbrange_test(struct dentry *dentry)
9549 + aufs_bindex_t btop, bbot;
9552 + btop = au_dbtop(dentry);
9553 + bbot = au_dbbot(dentry);
9555 + AuDebugOn(bbot < 0 && btop > bbot);
9558 + AuDebugOn(bbot >= 0);
9564 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9569 + if (unlikely(au_digen(dentry) != sigen
9570 + || au_iigen_test(d_inode(dentry), sigen)))
9576 +void au_update_digen(struct dentry *dentry)
9578 + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9579 + /* smp_mb(); */ /* atomic_set */
9582 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9584 + struct au_dinfo *dinfo;
9585 + struct dentry *h_d;
9586 + struct au_hdentry *hdp;
9587 + aufs_bindex_t bindex, bbot;
9589 + DiMustWriteLock(dentry);
9591 + dinfo = au_di(dentry);
9592 + if (!dinfo || dinfo->di_btop < 0)
9595 + if (do_put_zero) {
9596 + bbot = dinfo->di_bbot;
9597 + bindex = dinfo->di_btop;
9598 + hdp = au_hdentry(dinfo, bindex);
9599 + for (; bindex <= bbot; bindex++, hdp++) {
9600 + h_d = hdp->hd_dentry;
9601 + if (h_d && d_is_negative(h_d))
9602 + au_set_h_dptr(dentry, bindex, NULL);
9606 + dinfo->di_btop = 0;
9607 + hdp = au_hdentry(dinfo, dinfo->di_btop);
9608 + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9609 + if (hdp->hd_dentry)
9611 + if (dinfo->di_btop > dinfo->di_bbot) {
9612 + dinfo->di_btop = -1;
9613 + dinfo->di_bbot = -1;
9617 + hdp = au_hdentry(dinfo, dinfo->di_bbot);
9618 + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9619 + if (hdp->hd_dentry)
9621 + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9624 +void au_update_dbtop(struct dentry *dentry)
9626 + aufs_bindex_t bindex, bbot;
9627 + struct dentry *h_dentry;
9629 + bbot = au_dbbot(dentry);
9630 + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9631 + h_dentry = au_h_dptr(dentry, bindex);
9634 + if (d_is_positive(h_dentry)) {
9635 + au_set_dbtop(dentry, bindex);
9638 + au_set_h_dptr(dentry, bindex, NULL);
9642 +void au_update_dbbot(struct dentry *dentry)
9644 + aufs_bindex_t bindex, btop;
9645 + struct dentry *h_dentry;
9647 + btop = au_dbtop(dentry);
9648 + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9649 + h_dentry = au_h_dptr(dentry, bindex);
9652 + if (d_is_positive(h_dentry)) {
9653 + au_set_dbbot(dentry, bindex);
9656 + au_set_h_dptr(dentry, bindex, NULL);
9660 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9662 + aufs_bindex_t bindex, bbot;
9664 + bbot = au_dbbot(dentry);
9665 + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9666 + if (au_h_dptr(dentry, bindex) == h_dentry)
9670 diff --git a/fs/aufs/dir.c b/fs/aufs/dir.c
9671 new file mode 100644
9672 index 00000000000000..ee900ea3264b22
9676 +// SPDX-License-Identifier: GPL-2.0
9678 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9680 + * This program is free software; you can redistribute it and/or modify
9681 + * it under the terms of the GNU General Public License as published by
9682 + * the Free Software Foundation; either version 2 of the License, or
9683 + * (at your option) any later version.
9685 + * This program is distributed in the hope that it will be useful,
9686 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9687 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9688 + * GNU General Public License for more details.
9690 + * You should have received a copy of the GNU General Public License
9691 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
9695 + * directory operations
9698 +#include <linux/fs_stack.h>
9699 +#include <linux/iversion.h>
9702 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9704 + unsigned int nlink;
9706 + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9708 + nlink = dir->i_nlink;
9709 + nlink += h_dir->i_nlink - 2;
9710 + if (h_dir->i_nlink < 2)
9712 + smp_mb(); /* for i_nlink */
9713 + /* 0 can happen in revaliding */
9714 + set_nlink(dir, nlink);
9717 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9719 + unsigned int nlink;
9721 + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9723 + nlink = dir->i_nlink;
9724 + nlink -= h_dir->i_nlink - 2;
9725 + if (h_dir->i_nlink < 2)
9727 + smp_mb(); /* for i_nlink */
9728 + /* nlink == 0 means the branch-fs is broken */
9729 + set_nlink(dir, nlink);
9732 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9735 + aufs_bindex_t bindex, bbot;
9736 + struct file *h_file;
9737 + struct dentry *h_dentry;
9741 + AuDebugOn(!d_is_dir(file->f_path.dentry));
9743 + bbot = au_fbbot_dir(file);
9744 + for (bindex = au_fbtop(file);
9745 + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9747 + h_file = au_hf_dir(file, bindex);
9748 + if (h_file && file_inode(h_file))
9749 + sz += vfsub_f_size_read(h_file);
9752 + AuDebugOn(!dentry);
9753 + AuDebugOn(!d_is_dir(dentry));
9755 + bbot = au_dbtaildir(dentry);
9756 + for (bindex = au_dbtop(dentry);
9757 + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9759 + h_dentry = au_h_dptr(dentry, bindex);
9760 + if (h_dentry && d_is_positive(h_dentry))
9761 + sz += i_size_read(d_inode(h_dentry));
9764 + if (sz < KMALLOC_MAX_SIZE)
9765 + sz = roundup_pow_of_two(sz);
9766 + if (sz > KMALLOC_MAX_SIZE)
9767 + sz = KMALLOC_MAX_SIZE;
9768 + else if (sz < NAME_MAX) {
9769 + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9770 + sz = AUFS_RDBLK_DEF;
9775 +struct au_dir_ts_arg {
9776 + struct dentry *dentry;
9777 + aufs_bindex_t brid;
9780 +static void au_do_dir_ts(void *arg)
9782 + struct au_dir_ts_arg *a = arg;
9783 + struct au_dtime dt;
9784 + struct path h_path;
9785 + struct inode *dir, *h_dir;
9786 + struct super_block *sb;
9787 + struct au_branch *br;
9788 + struct au_hinode *hdir;
9790 + aufs_bindex_t btop, bindex;
9792 + sb = a->dentry->d_sb;
9793 + if (d_really_is_negative(a->dentry))
9795 + /* no dir->i_mutex lock */
9796 + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9798 + dir = d_inode(a->dentry);
9799 + btop = au_ibtop(dir);
9800 + bindex = au_br_index(sb, a->brid);
9801 + if (bindex < btop)
9804 + br = au_sbr(sb, bindex);
9805 + h_path.dentry = au_h_dptr(a->dentry, bindex);
9806 + if (!h_path.dentry)
9808 + h_path.mnt = au_br_mnt(br);
9809 + au_dtime_store(&dt, a->dentry, &h_path);
9811 + br = au_sbr(sb, btop);
9812 + if (!au_br_writable(br->br_perm))
9814 + h_path.dentry = au_h_dptr(a->dentry, btop);
9815 + h_path.mnt = au_br_mnt(br);
9816 + err = vfsub_mnt_want_write(h_path.mnt);
9819 + hdir = au_hi(dir, btop);
9820 + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
9821 + h_dir = au_h_iptr(dir, btop);
9822 + if (h_dir->i_nlink
9823 + && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9824 + dt.dt_h_path = h_path;
9825 + au_dtime_revert(&dt);
9827 + au_hn_inode_unlock(hdir);
9828 + vfsub_mnt_drop_write(h_path.mnt);
9829 + au_cpup_attr_timesizes(dir);
9832 + aufs_read_unlock(a->dentry, AuLock_DW);
9835 + au_nwt_done(&au_sbi(sb)->si_nowait);
9836 + au_kfree_try_rcu(arg);
9839 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9841 + int perm, wkq_err;
9842 + aufs_bindex_t btop;
9843 + struct au_dir_ts_arg *arg;
9844 + struct dentry *dentry;
9845 + struct super_block *sb;
9849 + dentry = d_find_any_alias(dir);
9850 + AuDebugOn(!dentry);
9851 + sb = dentry->d_sb;
9852 + btop = au_ibtop(dir);
9853 + if (btop == bindex) {
9854 + au_cpup_attr_timesizes(dir);
9858 + perm = au_sbr_perm(sb, btop);
9859 + if (!au_br_writable(perm))
9862 + arg = kmalloc(sizeof(*arg), GFP_NOFS);
9866 + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9867 + arg->brid = au_sbr_id(sb, bindex);
9868 + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9869 + if (unlikely(wkq_err)) {
9870 + pr_err("wkq %d\n", wkq_err);
9872 + au_kfree_try_rcu(arg);
9879 +/* ---------------------------------------------------------------------- */
9881 +static int reopen_dir(struct file *file)
9884 + unsigned int flags;
9885 + aufs_bindex_t bindex, btail, btop;
9886 + struct dentry *dentry, *h_dentry;
9887 + struct file *h_file;
9889 + /* open all lower dirs */
9890 + dentry = file->f_path.dentry;
9891 + btop = au_dbtop(dentry);
9892 + for (bindex = au_fbtop(file); bindex < btop; bindex++)
9893 + au_set_h_fptr(file, bindex, NULL);
9894 + au_set_fbtop(file, btop);
9896 + btail = au_dbtaildir(dentry);
9897 + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
9898 + au_set_h_fptr(file, bindex, NULL);
9899 + au_set_fbbot_dir(file, btail);
9901 + flags = vfsub_file_flags(file);
9902 + for (bindex = btop; bindex <= btail; bindex++) {
9903 + h_dentry = au_h_dptr(dentry, bindex);
9906 + h_file = au_hf_dir(file, bindex);
9910 + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9911 + err = PTR_ERR(h_file);
9912 + if (IS_ERR(h_file))
9913 + goto out; /* close all? */
9914 + au_set_h_fptr(file, bindex, h_file);
9916 + au_update_figen(file);
9917 + /* todo: necessary? */
9918 + /* file->f_ra = h_file->f_ra; */
9925 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9928 + aufs_bindex_t bindex, btail;
9929 + struct dentry *dentry, *h_dentry;
9930 + struct vfsmount *mnt;
9932 + FiMustWriteLock(file);
9933 + AuDebugOn(h_file);
9936 + mnt = file->f_path.mnt;
9937 + dentry = file->f_path.dentry;
9938 + file->f_version = inode_query_iversion(d_inode(dentry));
9939 + bindex = au_dbtop(dentry);
9940 + au_set_fbtop(file, bindex);
9941 + btail = au_dbtaildir(dentry);
9942 + au_set_fbbot_dir(file, btail);
9943 + for (; !err && bindex <= btail; bindex++) {
9944 + h_dentry = au_h_dptr(dentry, bindex);
9948 + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9949 + if (unlikely(err))
9951 + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9952 + if (IS_ERR(h_file)) {
9953 + err = PTR_ERR(h_file);
9956 + au_set_h_fptr(file, bindex, h_file);
9958 + au_update_figen(file);
9959 + /* todo: necessary? */
9960 + /* file->f_ra = h_file->f_ra; */
9962 + return 0; /* success */
9965 + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
9966 + au_set_h_fptr(file, bindex, NULL);
9967 + au_set_fbtop(file, -1);
9968 + au_set_fbbot_dir(file, -1);
9973 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9974 + struct file *file)
9977 + struct super_block *sb;
9978 + struct au_fidir *fidir;
9981 + sb = file->f_path.dentry->d_sb;
9982 + si_read_lock(sb, AuLock_FLUSH);
9983 + fidir = au_fidir_alloc(sb);
9985 + struct au_do_open_args args = {
9986 + .open = do_open_dir,
9989 + err = au_do_open(file, &args);
9990 + if (unlikely(err))
9991 + au_kfree_rcu(fidir);
9993 + si_read_unlock(sb);
9997 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9998 + struct file *file)
10000 + struct au_vdir *vdir_cache;
10001 + struct au_finfo *finfo;
10002 + struct au_fidir *fidir;
10003 + struct au_hfile *hf;
10004 + aufs_bindex_t bindex, bbot;
10006 + finfo = au_fi(file);
10007 + fidir = finfo->fi_hdir;
10009 + au_hbl_del(&finfo->fi_hlist,
10010 + &au_sbi(file->f_path.dentry->d_sb)->si_files);
10011 + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10013 + au_vdir_free(vdir_cache);
10015 + bindex = finfo->fi_btop;
10016 + if (bindex >= 0) {
10017 + hf = fidir->fd_hfile + bindex;
10019 + * calls fput() instead of filp_close(),
10020 + * since no dnotify or lock for the lower file.
10022 + bbot = fidir->fd_bbot;
10023 + for (; bindex <= bbot; bindex++, hf++)
10025 + au_hfput(hf, /*execed*/0);
10027 + au_kfree_rcu(fidir);
10028 + finfo->fi_hdir = NULL;
10030 + au_finfo_fin(file);
10034 +/* ---------------------------------------------------------------------- */
10036 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10039 + aufs_bindex_t bindex, bbot;
10040 + struct file *h_file;
10043 + bbot = au_fbbot_dir(file);
10044 + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10045 + h_file = au_hf_dir(file, bindex);
10047 + err = vfsub_flush(h_file, id);
10052 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10054 + return au_do_flush(file, id, au_do_flush_dir);
10057 +/* ---------------------------------------------------------------------- */
10059 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10062 + aufs_bindex_t bbot, bindex;
10063 + struct inode *inode;
10064 + struct super_block *sb;
10067 + sb = dentry->d_sb;
10068 + inode = d_inode(dentry);
10069 + IMustLock(inode);
10070 + bbot = au_dbbot(dentry);
10071 + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10072 + struct path h_path;
10074 + if (au_test_ro(sb, bindex, inode))
10076 + h_path.dentry = au_h_dptr(dentry, bindex);
10077 + if (!h_path.dentry)
10080 + h_path.mnt = au_sbr_mnt(sb, bindex);
10081 + err = vfsub_fsync(NULL, &h_path, datasync);
10087 +static int au_do_fsync_dir(struct file *file, int datasync)
10090 + aufs_bindex_t bbot, bindex;
10091 + struct file *h_file;
10092 + struct super_block *sb;
10093 + struct inode *inode;
10095 + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10096 + if (unlikely(err))
10099 + inode = file_inode(file);
10100 + sb = inode->i_sb;
10101 + bbot = au_fbbot_dir(file);
10102 + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10103 + h_file = au_hf_dir(file, bindex);
10104 + if (!h_file || au_test_ro(sb, bindex, inode))
10107 + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10115 + * @file may be NULL
10117 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10121 + struct dentry *dentry;
10122 + struct inode *inode;
10123 + struct super_block *sb;
10126 + dentry = file->f_path.dentry;
10127 + inode = d_inode(dentry);
10128 + inode_lock(inode);
10129 + sb = dentry->d_sb;
10130 + si_noflush_read_lock(sb);
10132 + err = au_do_fsync_dir(file, datasync);
10134 + di_write_lock_child(dentry);
10135 + err = au_do_fsync_dir_no_file(dentry, datasync);
10137 + au_cpup_attr_timesizes(inode);
10138 + di_write_unlock(dentry);
10140 + fi_write_unlock(file);
10142 + si_read_unlock(sb);
10143 + inode_unlock(inode);
10147 +/* ---------------------------------------------------------------------- */
10149 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10152 + struct dentry *dentry;
10153 + struct inode *inode, *h_inode;
10154 + struct super_block *sb;
10156 + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10158 + dentry = file->f_path.dentry;
10159 + inode = d_inode(dentry);
10160 + IMustLock(inode);
10162 + sb = dentry->d_sb;
10163 + si_read_lock(sb, AuLock_FLUSH);
10164 + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10165 + if (unlikely(err))
10167 + err = au_alive_dir(dentry);
10169 + err = au_vdir_init(file);
10170 + di_downgrade_lock(dentry, AuLock_IR);
10171 + if (unlikely(err))
10174 + h_inode = au_h_iptr(inode, au_ibtop(inode));
10175 + if (!au_test_nfsd()) {
10176 + err = au_vdir_fill_de(file, ctx);
10177 + fsstack_copy_attr_atime(inode, h_inode);
10180 + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10181 + * encode_fh() and others.
10183 + atomic_inc(&h_inode->i_count);
10184 + di_read_unlock(dentry, AuLock_IR);
10185 + si_read_unlock(sb);
10186 + err = au_vdir_fill_de(file, ctx);
10187 + fsstack_copy_attr_atime(inode, h_inode);
10188 + fi_write_unlock(file);
10196 + di_read_unlock(dentry, AuLock_IR);
10197 + fi_write_unlock(file);
10199 + si_read_unlock(sb);
10203 +/* ---------------------------------------------------------------------- */
10205 +#define AuTestEmpty_WHONLY 1
10206 +#define AuTestEmpty_CALLED (1 << 1)
10207 +#define AuTestEmpty_SHWH (1 << 2)
10208 +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
10209 +#define au_fset_testempty(flags, name) \
10210 + do { (flags) |= AuTestEmpty_##name; } while (0)
10211 +#define au_fclr_testempty(flags, name) \
10212 + do { (flags) &= ~AuTestEmpty_##name; } while (0)
10214 +#ifndef CONFIG_AUFS_SHWH
10215 +#undef AuTestEmpty_SHWH
10216 +#define AuTestEmpty_SHWH 0
10219 +struct test_empty_arg {
10220 + struct dir_context ctx;
10221 + struct au_nhash *whlist;
10222 + unsigned int flags;
10224 + aufs_bindex_t bindex;
10227 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10228 + int namelen, loff_t offset __maybe_unused, u64 ino,
10229 + unsigned int d_type)
10231 + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10233 + char *name = (void *)__name;
10236 + au_fset_testempty(arg->flags, CALLED);
10238 + if (name[0] == '.'
10239 + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10240 + goto out; /* success */
10242 + if (namelen <= AUFS_WH_PFX_LEN
10243 + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10244 + if (au_ftest_testempty(arg->flags, WHONLY)
10245 + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10246 + arg->err = -ENOTEMPTY;
10250 + name += AUFS_WH_PFX_LEN;
10251 + namelen -= AUFS_WH_PFX_LEN;
10252 + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10253 + arg->err = au_nhash_append_wh
10254 + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10255 + au_ftest_testempty(arg->flags, SHWH));
10259 + AuTraceErr(arg->err);
10260 + return !arg->err;
10263 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10266 + struct file *h_file;
10267 + struct au_branch *br;
10269 + h_file = au_h_open(dentry, arg->bindex,
10270 + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10271 + /*file*/NULL, /*force_wr*/0);
10272 + err = PTR_ERR(h_file);
10273 + if (IS_ERR(h_file))
10277 + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10278 + && !file_inode(h_file)->i_nlink)
10283 + au_fclr_testempty(arg->flags, CALLED);
10285 + err = vfsub_iterate_dir(h_file, &arg->ctx);
10288 + } while (!err && au_ftest_testempty(arg->flags, CALLED));
10292 + br = au_sbr(dentry->d_sb, arg->bindex);
10293 + au_lcnt_dec(&br->br_nfiles);
10298 +struct do_test_empty_args {
10300 + struct dentry *dentry;
10301 + struct test_empty_arg *arg;
10304 +static void call_do_test_empty(void *args)
10306 + struct do_test_empty_args *a = args;
10307 + *a->errp = do_test_empty(a->dentry, a->arg);
10310 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10312 + int err, wkq_err;
10313 + struct dentry *h_dentry;
10314 + struct inode *h_inode;
10315 + struct mnt_idmap *h_idmap;
10317 + h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10318 + h_dentry = au_h_dptr(dentry, arg->bindex);
10319 + h_inode = d_inode(h_dentry);
10320 + /* todo: i_mode changes anytime? */
10321 + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10322 + err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10323 + inode_unlock_shared(h_inode);
10325 + err = do_test_empty(dentry, arg);
10327 + struct do_test_empty_args args = {
10329 + .dentry = dentry,
10332 + unsigned int flags = arg->flags;
10334 + wkq_err = au_wkq_wait(call_do_test_empty, &args);
10335 + if (unlikely(wkq_err))
10337 + arg->flags = flags;
10343 +int au_test_empty_lower(struct dentry *dentry)
10346 + unsigned int rdhash;
10347 + aufs_bindex_t bindex, btop, btail;
10348 + struct au_nhash whlist;
10349 + struct test_empty_arg arg = {
10351 + .actor = test_empty_cb
10354 + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10356 + SiMustAnyLock(dentry->d_sb);
10358 + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10360 + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10361 + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10362 + if (unlikely(err))
10366 + arg.whlist = &whlist;
10367 + btop = au_dbtop(dentry);
10368 + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10369 + au_fset_testempty(arg.flags, SHWH);
10370 + test_empty = do_test_empty;
10371 + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10372 + test_empty = sio_test_empty;
10373 + arg.bindex = btop;
10374 + err = test_empty(dentry, &arg);
10375 + if (unlikely(err))
10378 + au_fset_testempty(arg.flags, WHONLY);
10379 + btail = au_dbtaildir(dentry);
10380 + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10381 + struct dentry *h_dentry;
10383 + h_dentry = au_h_dptr(dentry, bindex);
10384 + if (h_dentry && d_is_positive(h_dentry)) {
10385 + arg.bindex = bindex;
10386 + err = test_empty(dentry, &arg);
10391 + au_nhash_wh_free(&whlist);
10396 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10399 + struct test_empty_arg arg = {
10401 + .actor = test_empty_cb
10404 + aufs_bindex_t bindex, btail;
10407 + arg.whlist = whlist;
10408 + arg.flags = AuTestEmpty_WHONLY;
10409 + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10410 + au_fset_testempty(arg.flags, SHWH);
10411 + btail = au_dbtaildir(dentry);
10412 + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10413 + struct dentry *h_dentry;
10415 + h_dentry = au_h_dptr(dentry, bindex);
10416 + if (h_dentry && d_is_positive(h_dentry)) {
10417 + arg.bindex = bindex;
10418 + err = sio_test_empty(dentry, &arg);
10425 +/* ---------------------------------------------------------------------- */
10427 +const struct file_operations aufs_dir_fop = {
10428 + .owner = THIS_MODULE,
10429 + .llseek = default_llseek,
10430 + .read = generic_read_dir,
10431 + .iterate_shared = aufs_iterate_shared,
10432 + .unlocked_ioctl = aufs_ioctl_dir,
10433 +#ifdef CONFIG_COMPAT
10434 + .compat_ioctl = aufs_compat_ioctl_dir,
10436 + .open = aufs_open_dir,
10437 + .release = aufs_release_dir,
10438 + .flush = aufs_flush_dir,
10439 + .fsync = aufs_fsync_dir
10441 diff --git a/fs/aufs/dir.h b/fs/aufs/dir.h
10442 new file mode 100644
10443 index 00000000000000..382342bd6ecff3
10445 +++ b/fs/aufs/dir.h
10447 +/* SPDX-License-Identifier: GPL-2.0 */
10449 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10451 + * This program is free software; you can redistribute it and/or modify
10452 + * it under the terms of the GNU General Public License as published by
10453 + * the Free Software Foundation; either version 2 of the License, or
10454 + * (at your option) any later version.
10456 + * This program is distributed in the hope that it will be useful,
10457 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10458 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10459 + * GNU General Public License for more details.
10461 + * You should have received a copy of the GNU General Public License
10462 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
10466 + * directory operations
10469 +#ifndef __AUFS_DIR_H__
10470 +#define __AUFS_DIR_H__
10474 +#include <linux/fs.h>
10476 +/* ---------------------------------------------------------------------- */
10478 +/* need to be faster and smaller */
10481 + unsigned int nh_num;
10482 + struct hlist_head *nh_head;
10485 +struct au_vdir_destr {
10486 + unsigned char len;
10487 + unsigned char name[];
10490 +struct au_vdir_dehstr {
10491 + struct hlist_node hash;
10492 + struct au_vdir_destr *str;
10493 + struct rcu_head rcu;
10494 +} ____cacheline_aligned_in_smp;
10496 +struct au_vdir_de {
10498 + unsigned char de_type;
10499 + /* caution: packed */
10500 + struct au_vdir_destr de_str;
10503 +struct au_vdir_wh {
10504 + struct hlist_node wh_hash;
10505 +#ifdef CONFIG_AUFS_SHWH
10507 + aufs_bindex_t wh_bindex;
10508 + unsigned char wh_type;
10510 + aufs_bindex_t wh_bindex;
10512 + /* caution: packed */
10513 + struct au_vdir_destr wh_str;
10516 +union au_vdir_deblk_p {
10517 + unsigned char *deblk;
10518 + struct au_vdir_de *de;
10522 + unsigned char **vd_deblk;
10523 + unsigned long vd_nblk;
10525 + unsigned long ul;
10526 + union au_vdir_deblk_p p;
10530 + unsigned int vd_deblk_sz;
10531 + unsigned long vd_jiffy;
10532 + struct rcu_head rcu;
10533 +} ____cacheline_aligned_in_smp;
10535 +/* ---------------------------------------------------------------------- */
10538 +extern const struct file_operations aufs_dir_fop;
10539 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10540 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10541 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10542 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10543 +int au_test_empty_lower(struct dentry *dentry);
10544 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10547 +unsigned int au_rdhash_est(loff_t sz);
10548 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10549 +void au_nhash_wh_free(struct au_nhash *whlist);
10550 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10552 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10553 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10554 + unsigned int d_type, aufs_bindex_t bindex,
10555 + unsigned char shwh);
10556 +void au_vdir_free(struct au_vdir *vdir);
10557 +int au_vdir_init(struct file *file);
10558 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10561 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10563 +#ifdef CONFIG_AUFS_RDU
10565 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10566 +#ifdef CONFIG_COMPAT
10567 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10568 + unsigned long arg);
10571 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10572 + unsigned int cmd, unsigned long arg)
10573 +#ifdef CONFIG_COMPAT
10574 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10575 + unsigned int cmd, unsigned long arg)
10579 +#endif /* __KERNEL__ */
10580 +#endif /* __AUFS_DIR_H__ */
10581 diff --git a/fs/aufs/dirren.c b/fs/aufs/dirren.c
10582 new file mode 100644
10583 index 00000000000000..2a6295ab6576b8
10585 +++ b/fs/aufs/dirren.c
10587 +// SPDX-License-Identifier: GPL-2.0
10589 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10591 + * This program is free software; you can redistribute it and/or modify
10592 + * it under the terms of the GNU General Public License as published by
10593 + * the Free Software Foundation; either version 2 of the License, or
10594 + * (at your option) any later version.
10596 + * This program is distributed in the hope that it will be useful,
10597 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10598 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10599 + * GNU General Public License for more details.
10601 + * You should have received a copy of the GNU General Public License
10602 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
10606 + * special handling in renaming a directory
10607 + * in order to support looking-up the before-renamed name on the lower readonly
10611 +#include <linux/byteorder/generic.h>
10614 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10618 + idx = au_dr_ihash(ent->dr_h_ino);
10619 + au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10622 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10625 + struct hlist_bl_head *hbl;
10628 + for (i = 0; ret && i < AuDirren_NHASH; i++) {
10629 + hbl = dr->dr_h_ino + i;
10630 + hlist_bl_lock(hbl);
10631 + ret &= hlist_bl_empty(hbl);
10632 + hlist_bl_unlock(hbl);
10638 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10640 + struct au_dr_hino *found, *ent;
10641 + struct hlist_bl_head *hbl;
10642 + struct hlist_bl_node *pos;
10646 + idx = au_dr_ihash(ino);
10647 + hbl = dr->dr_h_ino + idx;
10648 + hlist_bl_lock(hbl);
10649 + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10650 + if (ent->dr_h_ino == ino) {
10654 + hlist_bl_unlock(hbl);
10659 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10660 + struct au_dr_hino *add_ent)
10663 + struct hlist_bl_head *hbl;
10664 + struct hlist_bl_node *pos;
10665 + struct au_dr_hino *ent;
10668 + idx = au_dr_ihash(ino);
10669 + hbl = dr->dr_h_ino + idx;
10670 +#if 0 /* debug print */
10672 + struct hlist_bl_node *tmp;
10674 + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10675 + AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10678 + hlist_bl_lock(hbl);
10679 + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10680 + if (ent->dr_h_ino == ino) {
10684 + if (!found && add_ent)
10685 + hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10686 + hlist_bl_unlock(hbl);
10688 + if (!found && add_ent)
10689 + AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10694 +void au_dr_hino_free(struct au_dr_br *dr)
10697 + struct hlist_bl_head *hbl;
10698 + struct hlist_bl_node *pos, *tmp;
10699 + struct au_dr_hino *ent;
10701 + /* SiMustWriteLock(sb); */
10703 + for (i = 0; i < AuDirren_NHASH; i++) {
10704 + hbl = dr->dr_h_ino + i;
10705 + /* no spinlock since sbinfo must be write-locked */
10706 + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10707 + au_kfree_rcu(ent);
10708 + INIT_HLIST_BL_HEAD(hbl);
10712 +/* returns the number of inodes or an error */
10713 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10714 + struct file *hinofile)
10718 + loff_t pos, oldsize;
10720 + struct inode *hinoinode;
10721 + struct hlist_bl_head *hbl;
10722 + struct hlist_bl_node *n1, *n2;
10723 + struct au_dr_hino *ent;
10725 + SiMustWriteLock(sb);
10726 + AuDebugOn(!au_br_writable(br->br_perm));
10728 + hinoinode = file_inode(hinofile);
10729 + oldsize = i_size_read(hinoinode);
10733 + hbl = br->br_dirren.dr_h_ino;
10734 + for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
10735 + /* no bit-lock since sbinfo must be write-locked */
10736 + hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
10737 + AuDbg("hi%llu, %pD2\n",
10738 + (unsigned long long)ent->dr_h_ino, hinofile);
10739 + u64 = cpu_to_be64(ent->dr_h_ino);
10740 + ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
10741 + if (ssz == sizeof(u64))
10744 + /* write error */
10745 + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
10752 + /* regardless the error */
10753 + if (pos < oldsize) {
10754 + err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
10762 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
10769 + struct au_dr_hino *ent;
10770 + struct inode *hinoinode;
10771 + struct hlist_bl_head *hbl;
10775 + hbl = dr->dr_h_ino;
10776 + hinoinode = file_inode(hinofile);
10777 + sz = i_size_read(hinoinode);
10778 + AuDebugOn(sz % sizeof(u64));
10779 + n = sz / sizeof(u64);
10781 + ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
10782 + if (unlikely(ssz != sizeof(u64))) {
10783 + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
10790 + ent = kmalloc(sizeof(*ent), GFP_NOFS);
10796 + ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
10797 + AuDbg("hi%llu, %pD2\n",
10798 + (unsigned long long)ent->dr_h_ino, hinofile);
10799 + hidx = au_dr_ihash(ent->dr_h_ino);
10800 + au_hbl_add(&ent->dr_hnode, hbl + hidx);
10802 + goto out; /* success */
10805 + au_dr_hino_free(dr);
10812 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
10813 + * @path is a switch to distinguish load and store.
10815 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
10816 + struct au_branch *br, const struct path *path)
10819 + unsigned char load, suspend;
10820 + struct file *hinofile;
10821 + struct au_hinode *hdir;
10822 + struct inode *dir, *delegated;
10823 + struct path hinopath;
10824 + struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
10825 + sizeof(AUFS_WH_DR_BRHINO) - 1);
10827 + AuDebugOn(bindex < 0 && !br);
10828 + AuDebugOn(bindex >= 0 && br);
10833 + br = au_sbr(sb, bindex);
10836 + path = &br->br_path;
10837 + AuDebugOn(!au_br_writable(br->br_perm));
10838 + if (unlikely(!au_br_writable(br->br_perm)))
10844 + dir = d_inode(sb->s_root);
10845 + hdir = au_hinode(au_ii(dir), bindex);
10846 + dir = hdir->hi_inode;
10847 + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
10849 + dir = d_inode(path->dentry);
10850 + inode_lock_nested(dir, AuLsc_I_CHILD);
10852 + hinopath.mnt = path->mnt;
10853 + hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
10854 + err = PTR_ERR(hinopath.dentry);
10855 + if (IS_ERR(hinopath.dentry))
10859 + flags = O_RDONLY;
10861 + if (d_is_negative(hinopath.dentry))
10862 + goto out_dput; /* success */
10864 + if (au_dr_hino_test_empty(&br->br_dirren)) {
10865 + if (d_is_positive(hinopath.dentry)) {
10866 + delegated = NULL;
10867 + err = vfsub_unlink(dir, &hinopath, &delegated,
10870 + if (unlikely(err))
10871 + pr_err("ignored err %d, %pd2\n",
10872 + err, hinopath.dentry);
10873 + if (unlikely(err == -EWOULDBLOCK))
10878 + } else if (!d_is_positive(hinopath.dentry)) {
10879 + err = vfsub_create(dir, &hinopath, 0600,
10880 + /*want_excl*/false);
10882 + if (unlikely(err))
10885 + flags = O_WRONLY;
10887 + hinofile = vfsub_dentry_open(&hinopath, flags);
10889 + au_hn_inode_unlock(hdir);
10891 + inode_unlock(dir);
10892 + dput(hinopath.dentry);
10893 + AuTraceErrPtr(hinofile);
10894 + if (IS_ERR(hinofile)) {
10895 + err = PTR_ERR(hinofile);
10900 + err = au_dr_hino_load(&br->br_dirren, hinofile);
10902 + err = au_dr_hino_store(sb, br, hinofile);
10907 + dput(hinopath.dentry);
10910 + au_hn_inode_unlock(hdir);
10912 + inode_unlock(dir);
10918 +/* ---------------------------------------------------------------------- */
10920 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
10923 + struct kstatfs kstfs;
10925 + struct dentry *dentry;
10926 + struct super_block *sb;
10928 + err = vfs_statfs((void *)path, &kstfs);
10930 + if (unlikely(err))
10933 + /* todo: support for UUID */
10935 + if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
10936 + brid->type = AuBrid_FSID;
10937 + brid->fsid = kstfs.f_fsid;
10939 + dentry = path->dentry;
10940 + sb = dentry->d_sb;
10943 + brid->type = AuBrid_DEV;
10952 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
10953 + const struct path *path)
10956 + struct au_dr_br *dr;
10957 + struct hlist_bl_head *hbl;
10959 + dr = &br->br_dirren;
10960 + hbl = dr->dr_h_ino;
10961 + for (i = 0; i < AuDirren_NHASH; i++, hbl++)
10962 + INIT_HLIST_BL_HEAD(hbl);
10964 + err = au_dr_brid_init(&dr->dr_brid, path);
10965 + if (unlikely(err))
10968 + if (au_opt_test(au_mntflags(sb), DIRREN))
10969 + err = au_dr_hino(sb, /*bindex*/-1, br, path);
10976 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
10981 + if (au_br_writable(br->br_perm))
10982 + err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
10984 + au_dr_hino_free(&br->br_dirren);
10989 +/* ---------------------------------------------------------------------- */
10991 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
10992 + char *buf, size_t sz)
10995 + unsigned int major, minor;
10999 + err = snprintf(p, sz, "%d_", brid->type);
11000 + AuDebugOn(err > sz);
11003 + switch (brid->type) {
11004 + case AuBrid_Unset:
11006 + case AuBrid_UUID:
11007 + err = snprintf(p, sz, "%pU", brid->uuid.b);
11009 + case AuBrid_FSID:
11010 + err = snprintf(p, sz, "%08x-%08x",
11011 + brid->fsid.val[0], brid->fsid.val[1]);
11014 + major = MAJOR(brid->dev);
11015 + minor = MINOR(brid->dev);
11016 + if (major <= 0xff && minor <= 0xff)
11017 + err = snprintf(p, sz, "%02x%02x", major, minor);
11019 + err = snprintf(p, sz, "%03x:%05x", major, minor);
11022 + AuDebugOn(err > sz);
11025 + err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11026 + AuDebugOn(err > sz);
11033 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11036 + struct dentry *br_dentry;
11037 + struct inode *br_inode;
11039 + br_dentry = au_br_dentry(br);
11040 + br_inode = d_inode(br_dentry);
11041 + rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11042 + AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11043 + AuDebugOn(rlen > len);
11048 +/* ---------------------------------------------------------------------- */
11051 + * from the given @h_dentry, construct drinfo at @*fdata.
11052 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11055 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11056 + struct dentry *h_dentry,
11057 + unsigned char *allocated)
11060 + struct au_drinfo_fdata *f, *p;
11061 + struct au_drinfo *drinfo;
11062 + struct inode *h_inode;
11063 + struct qstr *qname;
11067 + h_inode = d_inode(h_dentry);
11068 + qname = &h_dentry->d_name;
11069 + drinfo = &f->drinfo;
11070 + drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11071 + drinfo->oldnamelen = qname->len;
11072 + if (*allocated < sizeof(*f) + qname->len) {
11073 + v = roundup_pow_of_two(*allocated + qname->len);
11074 + p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11075 + if (unlikely(!p)) {
11083 + drinfo = &f->drinfo;
11085 + memcpy(drinfo->oldname, qname->name, qname->len);
11086 + AuDbg("i%llu, %.*s\n",
11087 + be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11088 + drinfo->oldname);
11095 +/* callers have to free the return value */
11096 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11098 + struct au_drinfo *ret, *drinfo;
11099 + struct au_drinfo_fdata fdata;
11104 + ret = ERR_PTR(-EIO);
11106 + ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11107 + if (unlikely(ssz != sizeof(fdata))) {
11108 + AuIOErr("ssz %zd, %u, %pD2\n",
11109 + ssz, (unsigned int)sizeof(fdata), file);
11113 + fdata.magic = ntohl((__force __be32)fdata.magic);
11114 + switch (fdata.magic) {
11115 + case AUFS_DRINFO_MAGIC_V1:
11118 + AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11119 + fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11123 + drinfo = &fdata.drinfo;
11124 + len = drinfo->oldnamelen;
11126 + AuIOErr("broken drinfo %pD2\n", file);
11131 + drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11132 + if (unlikely(h_ino && drinfo->ino != h_ino)) {
11133 + AuDbg("ignored i%llu, i%llu, %pD2\n",
11134 + (unsigned long long)drinfo->ino,
11135 + (unsigned long long)h_ino, file);
11136 + goto out; /* success */
11139 + ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11140 + if (unlikely(!ret)) {
11141 + ret = ERR_PTR(-ENOMEM);
11142 + AuTraceErrPtr(ret);
11147 + ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11148 + if (unlikely(ssz != len)) {
11149 + au_kfree_rcu(ret);
11150 + ret = ERR_PTR(-EIO);
11151 + AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11155 + AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11161 +/* ---------------------------------------------------------------------- */
11163 +/* in order to be revertible */
11164 +struct au_drinfo_rev_elm {
11166 + struct dentry *info_dentry;
11167 + struct au_drinfo *info_last;
11170 +struct au_drinfo_rev {
11171 + unsigned char already;
11172 + aufs_bindex_t nelm;
11173 + struct au_drinfo_rev_elm elm[];
11176 +/* todo: isn't it too large? */
11177 +struct au_drinfo_store {
11178 + struct path h_ppath;
11179 + struct dentry *h_dentry;
11180 + struct au_drinfo_fdata *fdata;
11181 + char *infoname; /* inside of whname, just after PFX */
11182 + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11183 + aufs_bindex_t btgt, btail;
11184 + unsigned char no_sio,
11185 + allocated, /* current size of *fdata */
11186 + infonamelen, /* room size for p */
11187 + whnamelen, /* length of the generated name */
11188 + renameback; /* renamed back */
11191 +/* on rename(2) error, the caller should revert it using @elm */
11192 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11193 + struct au_drinfo_rev_elm *elm)
11198 + struct path infopath = {
11199 + .mnt = w->h_ppath.mnt
11201 + struct inode *h_dir, *h_inode, *delegated;
11202 + struct file *infofile;
11203 + struct qstr *qname;
11206 + && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11208 + infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11210 + AuTraceErrPtr(infopath.dentry);
11211 + if (IS_ERR(infopath.dentry)) {
11212 + err = PTR_ERR(infopath.dentry);
11217 + h_dir = d_inode(w->h_ppath.dentry);
11218 + if (elm && d_is_negative(infopath.dentry)) {
11219 + err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11221 + if (unlikely(err))
11223 + elm->created = 1;
11224 + elm->info_dentry = dget(infopath.dentry);
11227 + infofile = vfsub_dentry_open(&infopath, O_RDWR);
11228 + AuTraceErrPtr(infofile);
11229 + if (IS_ERR(infofile)) {
11230 + err = PTR_ERR(infofile);
11234 + h_inode = d_inode(infopath.dentry);
11235 + if (elm && i_size_read(h_inode)) {
11236 + h_inode = d_inode(w->h_dentry);
11237 + elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11238 + AuTraceErrPtr(elm->info_last);
11239 + if (IS_ERR(elm->info_last)) {
11240 + err = PTR_ERR(elm->info_last);
11241 + elm->info_last = NULL;
11242 + AuDebugOn(elm->info_dentry);
11247 + if (elm && w->renameback) {
11248 + delegated = NULL;
11249 + err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11251 + if (unlikely(err == -EWOULDBLOCK))
11257 + qname = &w->h_dentry->d_name;
11258 + len = sizeof(*w->fdata) + qname->len;
11260 + len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11261 + ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11262 + if (ssz == len) {
11263 + AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11264 + w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11265 + goto out_fput; /* success */
11270 + /* the caller should revert it using @elm */
11276 + dput(infopath.dentry);
11282 +struct au_call_drinfo_do_store_args {
11284 + struct au_drinfo_store *w;
11285 + struct au_drinfo_rev_elm *elm;
11288 +static void au_call_drinfo_do_store(void *args)
11290 + struct au_call_drinfo_do_store_args *a = args;
11292 + *a->errp = au_drinfo_do_store(a->w, a->elm);
11295 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11296 + struct au_drinfo_rev_elm *elm)
11298 + int err, wkq_err;
11301 + err = au_drinfo_do_store(w, elm);
11303 + struct au_call_drinfo_do_store_args a = {
11308 + wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11309 + if (unlikely(wkq_err))
11317 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11318 + aufs_bindex_t btgt)
11322 + memset(w, 0, sizeof(*w));
11323 + w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11324 + strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
11325 + w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11326 + w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11328 + w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11331 + w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11332 + if (unlikely(!w->fdata)) {
11336 + w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11343 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11345 + au_kfree_rcu(w->fdata);
11348 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11349 + struct au_drinfo_store *w)
11351 + struct au_drinfo_rev_elm *elm;
11352 + struct inode *h_dir, *delegated;
11354 + struct path infopath = {
11355 + .mnt = w->h_ppath.mnt
11358 + h_dir = d_inode(w->h_ppath.dentry);
11359 + IMustLock(h_dir);
11363 + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11364 + AuDebugOn(elm->created && elm->info_last);
11365 + if (elm->created) {
11367 + delegated = NULL;
11368 + infopath.dentry = elm->info_dentry;
11369 + err = vfsub_unlink(h_dir, &infopath, &delegated,
11372 + if (unlikely(err == -EWOULDBLOCK))
11374 + dput(elm->info_dentry);
11375 + } else if (elm->info_last) {
11377 + w->fdata->drinfo = *elm->info_last;
11378 + memcpy(w->fdata->drinfo.oldname,
11379 + elm->info_last->oldname,
11380 + elm->info_last->oldnamelen);
11381 + err = au_drinfo_store_sio(w, /*elm*/NULL);
11382 + au_kfree_rcu(elm->info_last);
11384 + if (unlikely(err))
11385 + AuIOErr("%d, %s\n", err, w->whname);
11386 + /* go on even if err */
11390 +/* caller has to call au_dr_rename_fin() later */
11391 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11392 + struct qstr *dst_name, void *_rev)
11394 + int err, sz, nelm;
11395 + aufs_bindex_t bindex, btail;
11396 + struct au_drinfo_store work;
11397 + struct au_drinfo_rev *rev, **p;
11398 + struct au_drinfo_rev_elm *elm;
11399 + struct super_block *sb;
11400 + struct au_branch *br;
11401 + struct au_hinode *hdir;
11403 + err = au_drinfo_store_work_init(&work, btgt);
11405 + if (unlikely(err))
11409 + btail = au_dbtaildir(dentry);
11410 + nelm = btail - btgt;
11411 + sz = sizeof(*rev) + sizeof(*elm) * nelm;
11412 + rev = kcalloc(1, sz, GFP_NOFS);
11413 + if (unlikely(!rev)) {
11417 + rev->nelm = nelm;
11423 + sb = dentry->d_sb;
11424 + work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11425 + work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11426 + hdir = au_hi(d_inode(dentry), btgt);
11427 + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11428 + for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11429 + work.h_dentry = au_h_dptr(dentry, bindex);
11430 + if (!work.h_dentry)
11433 + err = au_drinfo_construct(&work.fdata, work.h_dentry,
11434 + &work.allocated);
11436 + if (unlikely(err))
11439 + work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11440 + br = au_sbr(sb, bindex);
11441 + work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11442 + work.whnamelen += au_drinfo_name(br, work.infoname,
11443 + work.infonamelen);
11444 + AuDbg("whname %.*s, i%llu, %.*s\n",
11445 + work.whnamelen, work.whname,
11446 + be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11447 + work.fdata->drinfo.oldnamelen,
11448 + work.fdata->drinfo.oldname);
11450 + err = au_drinfo_store_sio(&work, elm);
11452 + if (unlikely(err))
11455 + if (unlikely(err)) {
11456 + /* revert all drinfo */
11457 + au_drinfo_store_rev(rev, &work);
11458 + au_kfree_try_rcu(rev);
11461 + au_hn_inode_unlock(hdir);
11464 + au_drinfo_store_work_fin(&work);
11469 +/* ---------------------------------------------------------------------- */
11471 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11472 + struct qstr *dst_name, void *_rev)
11474 + int err, already;
11476 + struct super_block *sb;
11477 + struct au_branch *br;
11478 + struct au_dr_br *dr;
11479 + struct dentry *h_dentry;
11480 + struct inode *h_inode;
11481 + struct au_dr_hino *ent;
11482 + struct au_drinfo_rev *rev, **p;
11484 + AuDbg("bindex %d\n", bindex);
11487 + ent = kmalloc(sizeof(*ent), GFP_NOFS);
11488 + if (unlikely(!ent))
11492 + br = au_sbr(sb, bindex);
11493 + dr = &br->br_dirren;
11494 + h_dentry = au_h_dptr(src, bindex);
11495 + h_inode = d_inode(h_dentry);
11496 + ino = h_inode->i_ino;
11497 + ent->dr_h_ino = ino;
11498 + already = au_dr_hino_test_add(dr, ino, ent);
11499 + AuDbg("b%d, hi%llu, already %d\n",
11500 + bindex, (unsigned long long)ino, already);
11502 + err = au_drinfo_store(src, bindex, dst_name, _rev);
11507 + rev->already = already;
11508 + goto out; /* success */
11513 + au_dr_hino_del(dr, ent);
11514 + au_kfree_rcu(ent);
11521 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11523 + struct au_drinfo_rev *rev;
11524 + struct au_drinfo_rev_elm *elm;
11529 + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11530 + dput(elm->info_dentry);
11531 + au_kfree_rcu(elm->info_last);
11533 + au_kfree_try_rcu(rev);
11536 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11539 + struct au_drinfo_store work;
11540 + struct au_drinfo_rev *rev = _rev;
11541 + struct super_block *sb;
11542 + struct au_branch *br;
11543 + struct inode *h_inode;
11544 + struct au_dr_br *dr;
11545 + struct au_dr_hino *ent;
11547 + err = au_drinfo_store_work_init(&work, btgt);
11548 + if (unlikely(err))
11552 + br = au_sbr(sb, btgt);
11553 + work.h_ppath.dentry = au_h_dptr(src, btgt);
11554 + work.h_ppath.mnt = au_br_mnt(br);
11555 + au_drinfo_store_rev(rev, &work);
11556 + au_drinfo_store_work_fin(&work);
11557 + if (rev->already)
11560 + dr = &br->br_dirren;
11561 + h_inode = d_inode(work.h_ppath.dentry);
11562 + ent = au_dr_hino_find(dr, h_inode->i_ino);
11564 + au_dr_hino_del(dr, ent);
11565 + au_kfree_rcu(ent);
11568 + au_kfree_try_rcu(rev);
11569 + if (unlikely(err))
11570 + pr_err("failed to remove dirren info\n");
11573 +/* ---------------------------------------------------------------------- */
11575 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11576 + char *whname, int whnamelen,
11577 + struct dentry **info_dentry)
11579 + struct au_drinfo *drinfo;
11581 + struct inode *h_dir;
11582 + struct path infopath;
11585 + AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11587 + *info_dentry = NULL;
11590 + h_dir = d_inode(h_ppath->dentry);
11591 + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11592 + infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11593 + if (IS_ERR(infopath.dentry)) {
11594 + drinfo = (void *)infopath.dentry;
11598 + if (d_is_negative(infopath.dentry))
11599 + goto out_dput; /* success */
11601 + infopath.mnt = h_ppath->mnt;
11602 + f = vfsub_dentry_open(&infopath, O_RDONLY);
11603 + inode_unlock_shared(h_dir);
11606 + drinfo = (void *)f;
11610 + drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11611 + if (IS_ERR_OR_NULL(drinfo))
11614 + AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11615 + *info_dentry = dget(infopath.dentry); /* keep it alive */
11620 + dput(infopath.dentry);
11623 + inode_unlock_shared(h_dir);
11624 + AuTraceErrPtr(drinfo);
11628 +struct au_drinfo_do_load_args {
11629 + struct au_drinfo **drinfop;
11630 + struct path *h_ppath;
11633 + struct dentry **info_dentry;
11636 +static void au_call_drinfo_do_load(void *args)
11638 + struct au_drinfo_do_load_args *a = args;
11640 + *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11644 +struct au_drinfo_load {
11645 + struct path h_ppath;
11646 + struct qstr *qname;
11647 + unsigned char no_sio;
11649 + aufs_bindex_t ninfo;
11650 + struct au_drinfo **drinfo;
11653 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11654 + struct au_branch *br)
11656 + int err, wkq_err, whnamelen, e;
11657 + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11658 + = AUFS_WH_DR_INFO_PFX;
11659 + struct au_drinfo *drinfo;
11660 + struct qstr oldname;
11661 + struct inode *h_dir, *delegated;
11662 + struct dentry *info_dentry;
11663 + struct path infopath;
11665 + whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11666 + whnamelen += au_drinfo_name(br, whname + whnamelen,
11667 + sizeof(whname) - whnamelen);
11669 + drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11672 + struct au_drinfo_do_load_args args = {
11673 + .drinfop = &drinfo,
11674 + .h_ppath = &w->h_ppath,
11675 + .whname = whname,
11676 + .whnamelen = whnamelen,
11677 + .info_dentry = &info_dentry
11679 + wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11680 + if (unlikely(wkq_err))
11681 + drinfo = ERR_PTR(wkq_err);
11683 + err = PTR_ERR(drinfo);
11684 + if (IS_ERR_OR_NULL(drinfo))
11688 + oldname.len = drinfo->oldnamelen;
11689 + oldname.name = drinfo->oldname;
11690 + if (au_qstreq(w->qname, &oldname)) {
11691 + /* the name is renamed back */
11692 + au_kfree_rcu(drinfo);
11695 + infopath.dentry = info_dentry;
11696 + infopath.mnt = w->h_ppath.mnt;
11697 + h_dir = d_inode(w->h_ppath.dentry);
11698 + delegated = NULL;
11699 + inode_lock_nested(h_dir, AuLsc_I_PARENT);
11700 + e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11701 + inode_unlock(h_dir);
11703 + AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11704 + if (unlikely(e == -EWOULDBLOCK))
11707 + au_kfree_rcu(w->drinfo[bindex]);
11708 + w->drinfo[bindex] = drinfo;
11709 + dput(info_dentry);
11716 +/* ---------------------------------------------------------------------- */
11718 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11720 + struct au_drinfo **p = drinfo;
11723 + au_kfree_rcu(*drinfo++);
11724 + au_kfree_try_rcu(p);
11727 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
11728 + aufs_bindex_t btgt)
11731 + struct au_drinfo_load w;
11732 + aufs_bindex_t bindex, bbot;
11733 + struct au_branch *br;
11734 + struct inode *h_dir;
11735 + struct au_dr_hino *ent;
11736 + struct super_block *sb;
11738 + AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
11739 + AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
11740 + AuLNPair(&lkup->whname), btgt);
11742 + sb = dentry->d_sb;
11743 + bbot = au_sbbot(sb);
11744 + w.ninfo = bbot + 1;
11745 + if (!lkup->dirren.drinfo) {
11746 + lkup->dirren.drinfo = kcalloc(w.ninfo,
11747 + sizeof(*lkup->dirren.drinfo),
11749 + if (unlikely(!lkup->dirren.drinfo)) {
11753 + lkup->dirren.ninfo = w.ninfo;
11755 + w.drinfo = lkup->dirren.drinfo;
11756 + w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11757 + w.h_ppath.dentry = au_h_dptr(dentry, btgt);
11758 + AuDebugOn(!w.h_ppath.dentry);
11759 + w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11760 + w.qname = &dentry->d_name;
11763 + for (bindex = btgt + 1; bindex <= bbot; bindex++) {
11764 + br = au_sbr(sb, bindex);
11765 + err = au_drinfo_load(&w, bindex, br);
11766 + if (unlikely(err))
11768 + if (w.drinfo[bindex])
11772 + br = au_sbr(sb, btgt);
11773 + h_dir = d_inode(w.h_ppath.dentry);
11774 + ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
11776 + au_dr_hino_del(&br->br_dirren, ent);
11777 + au_kfree_rcu(ent);
11779 + goto out; /* success */
11782 + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
11783 + lkup->dirren.ninfo = 0;
11784 + lkup->dirren.drinfo = NULL;
11790 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
11792 + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
11795 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
11798 + struct au_drinfo *drinfo;
11801 + if (!lkup->dirren.drinfo)
11803 + AuDebugOn(lkup->dirren.ninfo <= btgt);
11804 + drinfo = lkup->dirren.drinfo[btgt];
11808 + au_kfree_try_rcu(lkup->whname.name);
11809 + lkup->whname.name = NULL;
11810 + lkup->dirren.dr_name.len = drinfo->oldnamelen;
11811 + lkup->dirren.dr_name.name = drinfo->oldname;
11812 + lkup->name = &lkup->dirren.dr_name;
11813 + err = au_wh_name_alloc(&lkup->whname, lkup->name);
11815 + AuDbg("name %.*s, whname %.*s, b%d\n",
11816 + AuLNPair(lkup->name), AuLNPair(&lkup->whname),
11824 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
11828 + struct au_drinfo *drinfo;
11831 + if (!lkup->dirren.drinfo)
11833 + AuDebugOn(lkup->dirren.ninfo <= bindex);
11834 + drinfo = lkup->dirren.drinfo[bindex];
11838 + match = (drinfo->ino == h_ino);
11839 + AuDbg("match %d\n", match);
11845 +/* ---------------------------------------------------------------------- */
11847 +int au_dr_opt_set(struct super_block *sb)
11850 + aufs_bindex_t bindex, bbot;
11851 + struct au_branch *br;
11854 + bbot = au_sbbot(sb);
11855 + for (bindex = 0; !err && bindex <= bbot; bindex++) {
11856 + br = au_sbr(sb, bindex);
11857 + err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
11863 +int au_dr_opt_flush(struct super_block *sb)
11866 + aufs_bindex_t bindex, bbot;
11867 + struct au_branch *br;
11870 + bbot = au_sbbot(sb);
11871 + for (bindex = 0; !err && bindex <= bbot; bindex++) {
11872 + br = au_sbr(sb, bindex);
11873 + if (au_br_writable(br->br_perm))
11874 + err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
11880 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
11883 + aufs_bindex_t bindex, bbot;
11884 + struct au_branch *br;
11888 + err = au_dr_opt_flush(sb);
11889 + if (unlikely(err))
11893 + bbot = au_sbbot(sb);
11894 + for (bindex = 0; bindex <= bbot; bindex++) {
11895 + br = au_sbr(sb, bindex);
11896 + au_dr_hino_free(&br->br_dirren);
11902 diff --git a/fs/aufs/dirren.h b/fs/aufs/dirren.h
11903 new file mode 100644
11904 index 00000000000000..d2d57d95a77ecc
11906 +++ b/fs/aufs/dirren.h
11908 +/* SPDX-License-Identifier: GPL-2.0 */
11910 + * Copyright (C) 2017-2022 Junjiro R. Okajima
11912 + * This program is free software; you can redistribute it and/or modify
11913 + * it under the terms of the GNU General Public License as published by
11914 + * the Free Software Foundation; either version 2 of the License, or
11915 + * (at your option) any later version.
11917 + * This program is distributed in the hope that it will be useful,
11918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11920 + * GNU General Public License for more details.
11922 + * You should have received a copy of the GNU General Public License
11923 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
11927 + * renamed dir info
11930 +#ifndef __AUFS_DIRREN_H__
11931 +#define __AUFS_DIRREN_H__
11935 +#include <linux/dcache.h>
11936 +#include <linux/statfs.h>
11937 +#include <linux/uuid.h>
11940 +#define AuDirren_NHASH 100
11942 +#ifdef CONFIG_AUFS_DIRREN
11943 +enum au_brid_type {
11950 +struct au_dr_brid {
11951 + enum au_brid_type type;
11953 + uuid_t uuid; /* unimplemented yet */
11959 +/* 20 is the max digits length of ulong 64 */
11960 +/* brid-type "_" uuid "_" inum */
11961 +#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
11962 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
11964 +struct au_dr_hino {
11965 + struct hlist_bl_node dr_hnode;
11970 + struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
11971 + struct au_dr_brid dr_brid;
11974 +struct au_dr_lookup {
11975 + /* dr_name is pointed by struct au_do_lookup_args.name */
11976 + struct qstr dr_name; /* subset of dr_info */
11977 + aufs_bindex_t ninfo;
11978 + struct au_drinfo **drinfo;
11981 +struct au_dr_hino;
11983 +struct au_dr_br { };
11984 +struct au_dr_lookup { };
11987 +/* ---------------------------------------------------------------------- */
11990 +struct au_do_lookup_args;
11992 +#ifdef CONFIG_AUFS_DIRREN
11993 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
11994 + struct au_dr_hino *add_ent);
11995 +void au_dr_hino_free(struct au_dr_br *dr);
11996 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11997 + const struct path *path);
11998 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
11999 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12000 + struct qstr *dst_name, void *_rev);
12001 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12002 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12003 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12004 + aufs_bindex_t bindex);
12005 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12006 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12008 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12009 +int au_dr_opt_set(struct super_block *sb);
12010 +int au_dr_opt_flush(struct super_block *sb);
12011 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12013 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12014 + struct au_dr_hino *add_ent);
12015 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12016 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12017 + const struct path *path);
12018 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12019 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12020 + struct qstr *dst_name, void *_rev);
12021 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12022 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12024 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12025 + aufs_bindex_t bindex);
12026 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12027 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12028 + aufs_bindex_t bindex, ino_t h_ino);
12029 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12030 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12031 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12032 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12035 +/* ---------------------------------------------------------------------- */
12037 +#ifdef CONFIG_AUFS_DIRREN
12038 +static inline int au_dr_ihash(ino_t h_ino)
12040 + return h_ino % AuDirren_NHASH;
12043 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12046 +#endif /* __KERNEL__ */
12047 +#endif /* __AUFS_DIRREN_H__ */
12048 diff --git a/fs/aufs/dynop.c b/fs/aufs/dynop.c
12049 new file mode 100644
12050 index 00000000000000..54fd16ecb6d340
12052 +++ b/fs/aufs/dynop.c
12054 +// SPDX-License-Identifier: GPL-2.0
12056 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12058 + * This program is free software; you can redistribute it and/or modify
12059 + * it under the terms of the GNU General Public License as published by
12060 + * the Free Software Foundation; either version 2 of the License, or
12061 + * (at your option) any later version.
12063 + * This program is distributed in the hope that it will be useful,
12064 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12065 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12066 + * GNU General Public License for more details.
12068 + * You should have received a copy of the GNU General Public License
12069 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
12073 + * dynamically customizable operations for regular files
12078 +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
12081 + * How large will these lists be?
12082 + * Usually just a few elements, 20-30 at most for each, I guess.
12084 +static struct hlist_bl_head dynop[AuDyLast];
12086 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12087 + const void *h_op)
12089 + struct au_dykey *key, *tmp;
12090 + struct hlist_bl_node *pos;
12093 + hlist_bl_lock(hbl);
12094 + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12095 + if (tmp->dk_op.dy_hop == h_op) {
12096 + if (kref_get_unless_zero(&tmp->dk_kref))
12100 + hlist_bl_unlock(hbl);
12105 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12107 + struct au_dykey **k, *found;
12108 + const void *h_op = key->dk_op.dy_hop;
12112 + k = br->br_dykey;
12113 + for (i = 0; i < AuBrDynOp; i++)
12115 + if (k[i]->dk_op.dy_hop == h_op) {
12122 + spin_lock(&br->br_dykey_lock);
12123 + for (; i < AuBrDynOp; i++)
12125 + if (k[i]->dk_op.dy_hop == h_op) {
12133 + spin_unlock(&br->br_dykey_lock);
12134 + BUG_ON(i == AuBrDynOp); /* expand the array */
12140 +/* kref_get() if @key is already added */
12141 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12143 + struct au_dykey *tmp, *found;
12144 + struct hlist_bl_node *pos;
12145 + const void *h_op = key->dk_op.dy_hop;
12148 + hlist_bl_lock(hbl);
12149 + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12150 + if (tmp->dk_op.dy_hop == h_op) {
12151 + if (kref_get_unless_zero(&tmp->dk_kref))
12156 + hlist_bl_add_head(&key->dk_hnode, hbl);
12157 + hlist_bl_unlock(hbl);
12164 +static void dy_free_rcu(struct rcu_head *rcu)
12166 + struct au_dykey *key;
12168 + key = container_of(rcu, struct au_dykey, dk_rcu);
12173 +static void dy_free(struct kref *kref)
12175 + struct au_dykey *key;
12176 + struct hlist_bl_head *hbl;
12178 + key = container_of(kref, struct au_dykey, dk_kref);
12179 + hbl = dynop + key->dk_op.dy_type;
12180 + au_hbl_del(&key->dk_hnode, hbl);
12181 + call_rcu(&key->dk_rcu, dy_free_rcu);
12184 +void au_dy_put(struct au_dykey *key)
12186 + kref_put(&key->dk_kref, dy_free);
12189 +/* ---------------------------------------------------------------------- */
12191 +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12193 +#ifdef CONFIG_AUFS_DEBUG
12194 +#define DyDbgDeclare(cnt) unsigned int cnt = 0
12195 +#define DyDbgInc(cnt) do { cnt++; } while (0)
12197 +#define DyDbgDeclare(cnt) do {} while (0)
12198 +#define DyDbgInc(cnt) do {} while (0)
12201 +#define DySet(func, dst, src, h_op, h_sb) do { \
12203 + if (h_op->func) { \
12205 + dst.func = src.func; \
12207 + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12211 +#define DySetForce(func, dst, src) do { \
12212 + AuDebugOn(!src.func); \
12214 + dst.func = src.func; \
12217 +#define DySetAop(func) \
12218 + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12219 +#define DySetAopForce(func) \
12220 + DySetForce(func, dyaop->da_op, aufs_aop)
12222 +static void dy_aop(struct au_dykey *key, const void *h_op,
12223 + struct super_block *h_sb __maybe_unused)
12225 + struct au_dyaop *dyaop = (void *)key;
12226 + const struct address_space_operations *h_aop = h_op;
12227 + DyDbgDeclare(cnt);
12229 + AuDbg("%s\n", au_sbtype(h_sb));
12231 + DySetAop(writepage);
12232 + DySetAopForce(read_folio); /* force */
12233 + DySetAop(writepages);
12234 + DySetAop(dirty_folio);
12235 + DySetAop(invalidate_folio);
12236 + DySetAop(readahead);
12237 + DySetAop(write_begin);
12238 + DySetAop(write_end);
12240 + DySetAop(release_folio);
12241 + DySetAop(free_folio);
12242 + /* this one will be changed according to an aufs mount option */
12243 + DySetAop(direct_IO);
12244 + DySetAop(migrate_folio);
12245 + DySetAop(launder_folio);
12246 + DySetAop(is_partially_uptodate);
12247 + DySetAop(is_dirty_writeback);
12248 + DySetAop(error_remove_page);
12249 + DySetAop(swap_activate);
12250 + DySetAop(swap_deactivate);
12251 + DySetAop(swap_rw);
12253 + DyDbgSize(cnt, *h_aop);
12256 +/* ---------------------------------------------------------------------- */
12258 +static void dy_bug(struct kref *kref)
12263 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12265 + struct au_dykey *key, *old;
12266 + struct hlist_bl_head *hbl;
12269 + void (*set)(struct au_dykey *key, const void *h_op,
12270 + struct super_block *h_sb __maybe_unused);
12272 + static const struct op a[] = {
12274 + .sz = sizeof(struct au_dyaop),
12278 + const struct op *p;
12280 + hbl = dynop + op->dy_type;
12281 + key = dy_gfind_get(hbl, op->dy_hop);
12283 + goto out_add; /* success */
12285 + p = a + op->dy_type;
12286 + key = kzalloc(p->sz, GFP_NOFS);
12287 + if (unlikely(!key)) {
12288 + key = ERR_PTR(-ENOMEM);
12292 + key->dk_op.dy_hop = op->dy_hop;
12293 + kref_init(&key->dk_kref);
12294 + p->set(key, op->dy_hop, au_br_sb(br));
12295 + old = dy_gadd(hbl, key);
12297 + au_kfree_rcu(key);
12302 + old = dy_bradd(br, key);
12304 + /* its ref-count should never be zero here */
12305 + kref_put(&key->dk_kref, dy_bug);
12310 +/* ---------------------------------------------------------------------- */
12312 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12313 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12314 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12315 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12316 + * See the aufs manual in detail.
12318 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12321 + dyaop->da_op.direct_IO = NULL;
12323 + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12326 +static struct au_dyaop *dy_aget(struct au_branch *br,
12327 + const struct address_space_operations *h_aop,
12330 + struct au_dyaop *dyaop;
12331 + struct au_dynop op;
12333 + op.dy_type = AuDy_AOP;
12334 + op.dy_haop = h_aop;
12335 + dyaop = (void *)dy_get(&op, br);
12336 + if (IS_ERR(dyaop))
12338 + dy_adx(dyaop, do_dx);
12344 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12345 + struct inode *h_inode)
12348 + struct super_block *sb;
12349 + struct au_branch *br;
12350 + struct au_dyaop *dyaop;
12352 + AuDebugOn(!S_ISREG(h_inode->i_mode));
12353 + IiMustWriteLock(inode);
12355 + sb = inode->i_sb;
12356 + br = au_sbr(sb, bindex);
12357 + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12358 + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12359 + err = PTR_ERR(dyaop);
12360 + if (IS_ERR(dyaop))
12361 + /* unnecessary to call dy_fput() */
12365 + inode->i_mapping->a_ops = &dyaop->da_op;
12372 + * Is it safe to replace a_ops during the inode/file is in operation?
12373 + * Yes, I hope so.
12375 +int au_dy_irefresh(struct inode *inode)
12378 + aufs_bindex_t btop;
12379 + struct inode *h_inode;
12382 + if (S_ISREG(inode->i_mode)) {
12383 + btop = au_ibtop(inode);
12384 + h_inode = au_h_iptr(inode, btop);
12385 + err = au_dy_iaop(inode, btop, h_inode);
12390 +void au_dy_arefresh(int do_dx)
12392 + struct hlist_bl_head *hbl;
12393 + struct hlist_bl_node *pos;
12394 + struct au_dykey *key;
12396 + hbl = dynop + AuDy_AOP;
12397 + hlist_bl_lock(hbl);
12398 + hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12399 + dy_adx((void *)key, do_dx);
12400 + hlist_bl_unlock(hbl);
12403 +/* ---------------------------------------------------------------------- */
12405 +void __init au_dy_init(void)
12409 + for (i = 0; i < AuDyLast; i++)
12410 + INIT_HLIST_BL_HEAD(dynop + i);
12413 +void au_dy_fin(void)
12417 + for (i = 0; i < AuDyLast; i++)
12418 + WARN_ON(!hlist_bl_empty(dynop + i));
12420 diff --git a/fs/aufs/dynop.h b/fs/aufs/dynop.h
12421 new file mode 100644
12422 index 00000000000000..60d89364b67600
12424 +++ b/fs/aufs/dynop.h
12426 +/* SPDX-License-Identifier: GPL-2.0 */
12428 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12430 + * This program is free software; you can redistribute it and/or modify
12431 + * it under the terms of the GNU General Public License as published by
12432 + * the Free Software Foundation; either version 2 of the License, or
12433 + * (at your option) any later version.
12435 + * This program is distributed in the hope that it will be useful,
12436 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12437 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12438 + * GNU General Public License for more details.
12440 + * You should have received a copy of the GNU General Public License
12441 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
12445 + * dynamically customizable operations (for regular files only)
12448 +#ifndef __AUFS_DYNOP_H__
12449 +#define __AUFS_DYNOP_H__
12453 +#include <linux/fs.h>
12454 +#include <linux/kref.h>
12456 +enum {AuDy_AOP, AuDyLast};
12461 + const void *dy_hop;
12462 + const struct address_space_operations *dy_haop;
12468 + struct hlist_bl_node dk_hnode;
12469 + struct rcu_head dk_rcu;
12471 + struct au_dynop dk_op;
12474 + * during I am in the branch local array, kref is gotten. when the
12475 + * branch is removed, kref is put.
12477 + struct kref dk_kref;
12480 +/* stop unioning since their sizes are very different from each other */
12482 + struct au_dykey da_key;
12483 + struct address_space_operations da_op; /* not const */
12485 +/* make sure that 'struct au_dykey *' can be any type */
12486 +static_assert(!offsetof(struct au_dyaop, da_key));
12488 +/* ---------------------------------------------------------------------- */
12492 +void au_dy_put(struct au_dykey *key);
12493 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12494 + struct inode *h_inode);
12495 +int au_dy_irefresh(struct inode *inode);
12496 +void au_dy_arefresh(int do_dio);
12498 +void __init au_dy_init(void);
12499 +void au_dy_fin(void);
12501 +#endif /* __KERNEL__ */
12502 +#endif /* __AUFS_DYNOP_H__ */
12503 diff --git a/fs/aufs/export.c b/fs/aufs/export.c
12504 new file mode 100644
12505 index 00000000000000..4c639e5d40f6bf
12507 +++ b/fs/aufs/export.c
12509 +// SPDX-License-Identifier: GPL-2.0
12511 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12513 + * This program is free software; you can redistribute it and/or modify
12514 + * it under the terms of the GNU General Public License as published by
12515 + * the Free Software Foundation; either version 2 of the License, or
12516 + * (at your option) any later version.
12518 + * This program is distributed in the hope that it will be useful,
12519 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12520 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12521 + * GNU General Public License for more details.
12523 + * You should have received a copy of the GNU General Public License
12524 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
12531 +#include <linux/exportfs.h>
12532 +#include <linux/fs_struct.h>
12533 +#include <linux/nsproxy.h>
12534 +#include <linux/random.h>
12535 +#include <linux/writeback.h>
12539 +#ifdef CONFIG_AUFS_INO_T_64
12547 +static ino_t decode_ino(__u32 *a)
12551 + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12553 +#ifdef CONFIG_AUFS_INO_T_64
12559 +static void encode_ino(__u32 *a, ino_t ino)
12565 +#ifdef CONFIG_AUFS_INO_T_64
12570 +/* NFS file handle */
12574 +#ifdef CONFIG_AUFS_INO_T_64
12575 + /* support 64bit inode number */
12588 + Fh_ino = Fh_ino1,
12589 + Fh_dir_ino = Fh_dir_ino1
12592 +static int au_test_anon(struct dentry *dentry)
12594 + /* note: read d_flags without d_lock */
12595 + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12598 +int au_test_nfsd(void)
12601 + struct task_struct *tsk = current;
12602 + char comm[sizeof(tsk->comm)];
12605 + if (tsk->flags & PF_KTHREAD) {
12606 + get_task_comm(comm, tsk);
12607 + ret = !strcmp(comm, "nfsd");
12613 +/* ---------------------------------------------------------------------- */
12614 +/* inode generation external table */
12616 +void au_xigen_inc(struct inode *inode)
12621 + struct super_block *sb;
12622 + struct au_sbinfo *sbinfo;
12624 + sb = inode->i_sb;
12625 + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12627 + sbinfo = au_sbi(sb);
12628 + pos = inode->i_ino;
12629 + pos *= sizeof(igen);
12630 + igen = inode->i_generation + 1;
12631 + sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12632 + if (sz == sizeof(igen))
12633 + return; /* success */
12635 + if (unlikely(sz >= 0))
12636 + AuIOErr("xigen error (%zd)\n", sz);
12639 +int au_xigen_new(struct inode *inode)
12644 + struct super_block *sb;
12645 + struct au_sbinfo *sbinfo;
12646 + struct file *file;
12649 + /* todo: dirty, at mount time */
12650 + if (inode->i_ino == AUFS_ROOT_INO)
12652 + sb = inode->i_sb;
12653 + SiMustAnyLock(sb);
12654 + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12658 + pos = inode->i_ino;
12659 + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12660 + AuIOErr1("too large i%lld\n", pos);
12663 + pos *= sizeof(inode->i_generation);
12666 + sbinfo = au_sbi(sb);
12667 + file = sbinfo->si_xigen;
12670 + if (vfsub_f_size_read(file)
12671 + < pos + sizeof(inode->i_generation)) {
12672 + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12673 + sz = xino_fwrite(file, &inode->i_generation,
12674 + sizeof(inode->i_generation), &pos);
12676 + sz = xino_fread(file, &inode->i_generation,
12677 + sizeof(inode->i_generation), &pos);
12678 + if (sz == sizeof(inode->i_generation))
12679 + goto out; /* success */
12682 + if (unlikely(sz >= 0)) {
12684 + AuIOErr("xigen error (%zd)\n", sz);
12691 +int au_xigen_set(struct super_block *sb, struct path *path)
12694 + struct au_sbinfo *sbinfo;
12695 + struct file *file;
12697 + SiMustWriteLock(sb);
12699 + sbinfo = au_sbi(sb);
12700 + file = au_xino_create2(sb, path, sbinfo->si_xigen);
12701 + err = PTR_ERR(file);
12702 + if (IS_ERR(file))
12705 + if (sbinfo->si_xigen)
12706 + fput(sbinfo->si_xigen);
12707 + sbinfo->si_xigen = file;
12714 +void au_xigen_clr(struct super_block *sb)
12716 + struct au_sbinfo *sbinfo;
12718 + SiMustWriteLock(sb);
12720 + sbinfo = au_sbi(sb);
12721 + if (sbinfo->si_xigen) {
12722 + fput(sbinfo->si_xigen);
12723 + sbinfo->si_xigen = NULL;
12727 +/* ---------------------------------------------------------------------- */
12729 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
12732 + struct dentry *dentry, *d;
12733 + struct inode *inode;
12734 + unsigned int sigen;
12737 + inode = ilookup(sb, ino);
12741 + dentry = ERR_PTR(-ESTALE);
12742 + sigen = au_sigen(sb);
12743 + if (unlikely(au_is_bad_inode(inode)
12744 + || IS_DEADDIR(inode)
12745 + || sigen != au_iigen(inode, NULL)))
12749 + if (!dir_ino || S_ISDIR(inode->i_mode))
12750 + dentry = d_find_alias(inode);
12752 + spin_lock(&inode->i_lock);
12753 + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
12754 + spin_lock(&d->d_lock);
12755 + if (!au_test_anon(d)
12756 + && d_inode(d->d_parent)->i_ino == dir_ino) {
12757 + dentry = dget_dlock(d);
12758 + spin_unlock(&d->d_lock);
12761 + spin_unlock(&d->d_lock);
12763 + spin_unlock(&inode->i_lock);
12765 + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
12766 + /* need to refresh */
12774 + AuTraceErrPtr(dentry);
12778 +/* ---------------------------------------------------------------------- */
12780 +/* todo: dirty? */
12781 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
12783 +struct au_compare_mnt_args {
12785 + struct super_block *sb;
12788 + struct vfsmount *mnt;
12791 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
12793 + struct au_compare_mnt_args *a = arg;
12795 + if (mnt->mnt_sb != a->sb)
12797 + a->mnt = mntget(mnt);
12801 +static struct vfsmount *au_mnt_get(struct super_block *sb)
12804 + struct path root;
12805 + struct au_compare_mnt_args args = {
12809 + get_fs_root(current->fs, &root);
12811 + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
12812 + rcu_read_unlock();
12815 + AuDebugOn(!args.mnt);
12819 +struct au_nfsd_si_lock {
12820 + unsigned int sigen;
12821 + aufs_bindex_t bindex, br_id;
12822 + unsigned char force_lock;
12825 +static int si_nfsd_read_lock(struct super_block *sb,
12826 + struct au_nfsd_si_lock *nsi_lock)
12829 + aufs_bindex_t bindex;
12831 + si_read_lock(sb, AuLock_FLUSH);
12833 + /* branch id may be wrapped around */
12835 + bindex = au_br_index(sb, nsi_lock->br_id);
12836 + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
12837 + goto out; /* success */
12841 + if (!nsi_lock->force_lock)
12842 + si_read_unlock(sb);
12845 + nsi_lock->bindex = bindex;
12849 +struct find_name_by_ino {
12850 + struct dir_context ctx;
12851 + int called, found;
12858 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
12859 + loff_t offset, u64 ino, unsigned int d_type)
12861 + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
12865 + if (a->ino != ino)
12868 + memcpy(a->name, name, namelen);
12869 + a->namelen = namelen;
12874 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
12875 + struct au_nfsd_si_lock *nsi_lock)
12877 + struct dentry *dentry, *parent;
12878 + struct file *file;
12879 + struct inode *dir;
12880 + struct find_name_by_ino arg = {
12882 + .actor = find_name_by_ino
12887 + parent = path->dentry;
12889 + si_read_unlock(parent->d_sb);
12890 + file = vfsub_dentry_open(path, au_dir_roflags);
12891 + dentry = (void *)file;
12892 + if (IS_ERR(file))
12895 + dentry = ERR_PTR(-ENOMEM);
12896 + arg.name = (void *)__get_free_page(GFP_NOFS);
12897 + if (unlikely(!arg.name))
12904 + err = vfsub_iterate_dir(file, &arg.ctx);
12905 + } while (!err && !arg.found && arg.called);
12906 + dentry = ERR_PTR(err);
12907 + if (unlikely(err))
12909 + /* instead of ENOENT */
12910 + dentry = ERR_PTR(-ESTALE);
12914 + /* do not call vfsub_lkup_one() */
12915 + dir = d_inode(parent);
12916 + dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
12917 + AuTraceErrPtr(dentry);
12918 + if (IS_ERR(dentry))
12920 + AuDebugOn(au_test_anon(dentry));
12921 + if (unlikely(d_really_is_negative(dentry))) {
12923 + dentry = ERR_PTR(-ENOENT);
12927 + free_page((unsigned long)arg.name);
12931 + if (unlikely(nsi_lock
12932 + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
12933 + if (!IS_ERR(dentry)) {
12935 + dentry = ERR_PTR(-ESTALE);
12937 + AuTraceErrPtr(dentry);
12941 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
12943 + struct au_nfsd_si_lock *nsi_lock)
12945 + struct dentry *dentry;
12946 + struct path path;
12948 + if (dir_ino != AUFS_ROOT_INO) {
12949 + path.dentry = decode_by_ino(sb, dir_ino, 0);
12950 + dentry = path.dentry;
12951 + if (!path.dentry || IS_ERR(path.dentry))
12953 + AuDebugOn(au_test_anon(path.dentry));
12955 + path.dentry = dget(sb->s_root);
12957 + path.mnt = au_mnt_get(sb);
12958 + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
12962 + AuTraceErrPtr(dentry);
12966 +/* ---------------------------------------------------------------------- */
12968 +static int h_acceptable(void *expv, struct dentry *dentry)
12973 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
12974 + char *buf, int len, struct super_block *sb)
12978 + struct path path;
12980 + p = d_path(h_rootpath, buf, len);
12985 + path.mnt = h_rootpath->mnt;
12986 + path.dentry = h_parent;
12987 + p = d_path(&path, buf, len);
12993 + path.mnt = au_mnt_get(sb);
12994 + path.dentry = sb->s_root;
12995 + p = d_path(&path, buf, len - strlen(p));
12996 + mntput(path.mnt);
13000 + p[strlen(p)] = '/';
13003 + AuTraceErrPtr(p);
13008 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13009 + int fh_len, struct au_nfsd_si_lock *nsi_lock)
13011 + struct dentry *dentry, *h_parent, *root;
13012 + struct super_block *h_sb;
13013 + char *pathname, *p;
13014 + struct vfsmount *h_mnt;
13015 + struct au_branch *br;
13017 + struct path path;
13019 + br = au_sbr(sb, nsi_lock->bindex);
13020 + h_mnt = au_br_mnt(br);
13021 + h_sb = h_mnt->mnt_sb;
13022 + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13024 + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13025 + fh_len - Fh_tail, fh[Fh_h_type],
13026 + h_acceptable, /*context*/NULL);
13028 + dentry = h_parent;
13029 + if (unlikely(!h_parent || IS_ERR(h_parent))) {
13030 + AuWarn1("%s decode_fh failed, %ld\n",
13031 + au_sbtype(h_sb), PTR_ERR(h_parent));
13035 + if (unlikely(au_test_anon(h_parent))) {
13036 + AuWarn1("%s decode_fh returned a disconnected dentry\n",
13037 + au_sbtype(h_sb));
13038 + goto out_h_parent;
13041 + dentry = ERR_PTR(-ENOMEM);
13042 + pathname = (void *)__get_free_page(GFP_NOFS);
13043 + if (unlikely(!pathname))
13044 + goto out_h_parent;
13046 + root = sb->s_root;
13047 + path.mnt = h_mnt;
13048 + di_read_lock_parent(root, !AuLock_IR);
13049 + path.dentry = au_h_dptr(root, nsi_lock->bindex);
13050 + di_read_unlock(root, !AuLock_IR);
13051 + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13052 + dentry = (void *)p;
13054 + goto out_pathname;
13056 + si_read_unlock(sb);
13057 + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13058 + dentry = ERR_PTR(err);
13059 + if (unlikely(err))
13062 + dentry = ERR_PTR(-ENOENT);
13063 + AuDebugOn(au_test_anon(path.dentry));
13064 + if (unlikely(d_really_is_negative(path.dentry)))
13067 + if (ino != d_inode(path.dentry)->i_ino)
13068 + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13070 + dentry = dget(path.dentry);
13075 + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13076 + if (!IS_ERR(dentry)) {
13078 + dentry = ERR_PTR(-ESTALE);
13081 + free_page((unsigned long)pathname);
13085 + AuTraceErrPtr(dentry);
13089 +/* ---------------------------------------------------------------------- */
13091 +static struct dentry *
13092 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13095 + struct dentry *dentry;
13096 + __u32 *fh = fid->raw;
13097 + struct au_branch *br;
13098 + ino_t ino, dir_ino;
13099 + struct au_nfsd_si_lock nsi_lock = {
13103 + dentry = ERR_PTR(-ESTALE);
13104 + /* it should never happen, but the file handle is unreliable */
13105 + if (unlikely(fh_len < Fh_tail))
13107 + nsi_lock.sigen = fh[Fh_sigen];
13108 + nsi_lock.br_id = fh[Fh_br_id];
13110 + /* branch id may be wrapped around */
13112 + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13114 + nsi_lock.force_lock = 1;
13116 + /* is this inode still cached? */
13117 + ino = decode_ino(fh + Fh_ino);
13118 + /* it should never happen */
13119 + if (unlikely(ino == AUFS_ROOT_INO))
13122 + dir_ino = decode_ino(fh + Fh_dir_ino);
13123 + dentry = decode_by_ino(sb, ino, dir_ino);
13124 + if (IS_ERR(dentry))
13129 + /* is the parent dir cached? */
13130 + br = au_sbr(sb, nsi_lock.bindex);
13131 + au_lcnt_inc(&br->br_nfiles);
13132 + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13133 + if (IS_ERR(dentry))
13138 + /* lookup path */
13139 + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13140 + if (IS_ERR(dentry))
13142 + if (unlikely(!dentry))
13143 + /* todo?: make it ESTALE */
13147 + if (!au_digen_test(dentry, au_sigen(sb))
13148 + && d_inode(dentry)->i_generation == fh[Fh_igen])
13149 + goto out_unlock; /* success */
13152 + dentry = ERR_PTR(-ESTALE);
13155 + au_lcnt_dec(&br->br_nfiles);
13156 + si_read_unlock(sb);
13158 + AuTraceErrPtr(dentry);
13162 +#if 0 /* reserved for future use */
13163 +/* support subtreecheck option */
13164 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13165 + int fh_len, int fh_type)
13167 + struct dentry *parent;
13168 + __u32 *fh = fid->raw;
13171 + dir_ino = decode_ino(fh + Fh_dir_ino);
13172 + parent = decode_by_ino(sb, dir_ino, 0);
13173 + if (IS_ERR(parent))
13176 + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13177 + dir_ino, fh, fh_len);
13180 + AuTraceErrPtr(parent);
13185 +/* ---------------------------------------------------------------------- */
13187 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13188 + struct inode *dir)
13191 + aufs_bindex_t bindex;
13192 + struct super_block *sb, *h_sb;
13193 + struct dentry *dentry, *parent, *h_parent;
13194 + struct inode *h_dir;
13195 + struct au_branch *br;
13198 + if (unlikely(*max_len <= Fh_tail)) {
13199 + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13203 + err = FILEID_ROOT;
13204 + if (inode->i_ino == AUFS_ROOT_INO) {
13205 + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13210 + sb = inode->i_sb;
13211 + err = si_read_lock(sb, AuLock_FLUSH);
13212 + if (unlikely(err))
13215 +#ifdef CONFIG_AUFS_DEBUG
13216 + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13217 + AuWarn1("NFS-exporting requires xino\n");
13221 + ii_read_lock_child(inode);
13222 + bindex = au_ibtop(inode);
13224 + dentry = d_find_any_alias(inode);
13225 + if (unlikely(!dentry))
13227 + AuDebugOn(au_test_anon(dentry));
13228 + parent = dget_parent(dentry);
13230 + if (unlikely(!parent))
13232 + if (d_really_is_positive(parent))
13233 + dir = d_inode(parent);
13236 + ii_read_lock_parent(dir);
13237 + h_dir = au_h_iptr(dir, bindex);
13238 + ii_read_unlock(dir);
13239 + if (unlikely(!h_dir))
13241 + h_parent = d_find_any_alias(h_dir);
13242 + if (unlikely(!h_parent))
13243 + goto out_hparent;
13246 + br = au_sbr(sb, bindex);
13247 + h_sb = au_br_sb(br);
13248 + if (unlikely(!h_sb->s_export_op)) {
13249 + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13250 + goto out_hparent;
13253 + fh[Fh_br_id] = br->br_id;
13254 + fh[Fh_sigen] = au_sigen(sb);
13255 + encode_ino(fh + Fh_ino, inode->i_ino);
13256 + encode_ino(fh + Fh_dir_ino, dir->i_ino);
13257 + fh[Fh_igen] = inode->i_generation;
13259 + *max_len -= Fh_tail;
13260 + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13262 + /*connectable or subtreecheck*/0);
13263 + err = fh[Fh_h_type];
13264 + *max_len += Fh_tail;
13265 + /* todo: macros? */
13266 + if (err != FILEID_INVALID)
13269 + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13276 + ii_read_unlock(inode);
13277 + si_read_unlock(sb);
13279 + if (unlikely(err < 0))
13280 + err = FILEID_INVALID;
13284 +/* ---------------------------------------------------------------------- */
13286 +static int aufs_commit_metadata(struct inode *inode)
13289 + aufs_bindex_t bindex;
13290 + struct super_block *sb;
13291 + struct inode *h_inode;
13292 + int (*f)(struct inode *inode);
13294 + sb = inode->i_sb;
13295 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13296 + ii_write_lock_child(inode);
13297 + bindex = au_ibtop(inode);
13298 + AuDebugOn(bindex < 0);
13299 + h_inode = au_h_iptr(inode, bindex);
13301 + f = h_inode->i_sb->s_export_op->commit_metadata;
13303 + err = f(h_inode);
13305 + err = sync_inode_metadata(h_inode, /*wait*/1);
13307 + au_cpup_attr_timesizes(inode);
13308 + ii_write_unlock(inode);
13309 + si_read_unlock(sb);
13313 +/* ---------------------------------------------------------------------- */
13315 +static struct export_operations aufs_export_op = {
13316 + .fh_to_dentry = aufs_fh_to_dentry,
13317 + /* .fh_to_parent = aufs_fh_to_parent, */
13318 + .encode_fh = aufs_encode_fh,
13319 + .commit_metadata = aufs_commit_metadata
13322 +void au_export_init(struct super_block *sb)
13324 + struct au_sbinfo *sbinfo;
13327 + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13328 + && IS_MODULE(CONFIG_EXPORTFS),
13329 + AUFS_NAME ": unsupported configuration "
13330 + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13332 + sb->s_export_op = &aufs_export_op;
13333 + sbinfo = au_sbi(sb);
13334 + sbinfo->si_xigen = NULL;
13335 + get_random_bytes(&u, sizeof(u));
13336 + BUILD_BUG_ON(sizeof(u) != sizeof(int));
13337 + atomic_set(&sbinfo->si_xigen_next, u);
13339 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
13340 new file mode 100644
13341 index 00000000000000..320ad8a9334d30
13343 +++ b/fs/aufs/f_op.c
13345 +// SPDX-License-Identifier: GPL-2.0
13347 + * Copyright (C) 2005-2022 Junjiro R. Okajima
13349 + * This program is free software; you can redistribute it and/or modify
13350 + * it under the terms of the GNU General Public License as published by
13351 + * the Free Software Foundation; either version 2 of the License, or
13352 + * (at your option) any later version.
13354 + * This program is distributed in the hope that it will be useful,
13355 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13356 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13357 + * GNU General Public License for more details.
13359 + * You should have received a copy of the GNU General Public License
13360 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
13364 + * file and vm operations
13367 +#include <linux/aio.h>
13368 +#include <linux/fs_stack.h>
13369 +#include <linux/mman.h>
13370 +#include <linux/security.h>
13373 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13376 + aufs_bindex_t bindex;
13377 + struct dentry *dentry, *h_dentry;
13378 + struct au_finfo *finfo;
13379 + struct inode *h_inode;
13381 + FiMustWriteLock(file);
13384 + dentry = file->f_path.dentry;
13385 + AuDebugOn(IS_ERR_OR_NULL(dentry));
13386 + finfo = au_fi(file);
13387 + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13388 + atomic_set(&finfo->fi_mmapped, 0);
13389 + bindex = au_dbtop(dentry);
13391 + h_dentry = au_h_dptr(dentry, bindex);
13392 + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13393 + if (unlikely(err))
13395 + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13396 + if (IS_ERR(h_file)) {
13397 + err = PTR_ERR(h_file);
13401 + h_dentry = h_file->f_path.dentry;
13402 + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13403 + if (unlikely(err))
13405 + /* br ref is already inc-ed */
13408 + if (flags & __O_TMPFILE) {
13409 + AuDebugOn(!h_file);
13410 + AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
13411 + au_di(dentry)->di_htmpfile = NULL;
13413 + if (!(flags & O_EXCL)) {
13414 + h_inode = file_inode(h_file);
13415 + spin_lock(&h_inode->i_lock);
13416 + h_inode->i_state |= I_LINKABLE;
13417 + spin_unlock(&h_inode->i_lock);
13420 + au_set_fbtop(file, bindex);
13421 + au_set_h_fptr(file, bindex, h_file);
13422 + au_update_figen(file);
13423 + /* todo: necessary? */
13424 + /* file->f_ra = h_file->f_ra; */
13430 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13431 + struct file *file)
13434 + struct super_block *sb;
13435 + struct au_do_open_args args = {
13436 + .open = au_do_open_nondir
13439 + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13440 + file, vfsub_file_flags(file), file->f_mode);
13442 + sb = file->f_path.dentry->d_sb;
13443 + si_read_lock(sb, AuLock_FLUSH);
13444 + err = au_do_open(file, &args);
13445 + si_read_unlock(sb);
13449 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13451 + struct au_finfo *finfo;
13452 + aufs_bindex_t bindex;
13454 + finfo = au_fi(file);
13455 + au_hbl_del(&finfo->fi_hlist,
13456 + &au_sbi(file->f_path.dentry->d_sb)->si_files);
13457 + bindex = finfo->fi_btop;
13459 + au_set_h_fptr(file, bindex, NULL);
13461 + au_finfo_fin(file);
13465 +/* ---------------------------------------------------------------------- */
13467 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13470 + struct file *h_file;
13473 + h_file = au_hf_top(file);
13475 + err = vfsub_flush(h_file, id);
13479 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13481 + return au_do_flush(file, id, au_do_flush_nondir);
13484 +/* ---------------------------------------------------------------------- */
13486 + * read and write functions acquire [fdi]_rwsem once, but release before
13487 + * mmap_sem. This is because to stop a race condition between mmap(2).
13488 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
13489 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13490 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13493 +/* Callers should call au_read_post() or fput() in the end */
13494 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
13496 + struct file *h_file;
13499 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
13501 + di_read_unlock(file->f_path.dentry, AuLock_IR);
13502 + h_file = au_hf_top(file);
13503 + get_file(h_file);
13505 + fi_read_unlock(file);
13507 + h_file = ERR_PTR(err);
13512 +static void au_read_post(struct inode *inode, struct file *h_file)
13514 + /* update without lock, I don't think it a problem */
13515 + fsstack_copy_attr_atime(inode, file_inode(h_file));
13519 +struct au_write_pre {
13521 + unsigned int lsc;
13525 + aufs_bindex_t btop;
13529 + * return with iinfo is write-locked
13530 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13533 +static struct file *au_write_pre(struct file *file, int do_ready,
13534 + struct au_write_pre *wpre)
13536 + struct file *h_file;
13537 + struct dentry *dentry;
13539 + unsigned int lsc;
13540 + struct au_pin pin;
13545 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
13546 + h_file = ERR_PTR(err);
13547 + if (unlikely(err))
13550 + dentry = file->f_path.dentry;
13552 + err = au_ready_to_write(file, -1, &pin);
13553 + if (unlikely(err)) {
13554 + h_file = ERR_PTR(err);
13555 + di_write_unlock(dentry);
13560 + di_downgrade_lock(dentry, /*flags*/0);
13562 + wpre->btop = au_fbtop(file);
13563 + h_file = au_hf_top(file);
13564 + get_file(h_file);
13566 + wpre->blks = file_inode(h_file)->i_blocks;
13569 + di_read_unlock(dentry, /*flags*/0);
13570 + vfsub_file_start_write(h_file);
13573 + fi_write_unlock(file);
13578 +static void au_write_post(struct inode *inode, struct file *h_file,
13579 + struct au_write_pre *wpre, ssize_t written)
13581 + struct inode *h_inode;
13583 + vfsub_file_end_write(h_file);
13584 + au_cpup_attr_timesizes(inode);
13585 + AuDebugOn(au_ibtop(inode) != wpre->btop);
13586 + h_inode = file_inode(h_file);
13587 + inode->i_mode = h_inode->i_mode;
13588 + ii_write_unlock(inode);
13589 + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13591 + au_fhsm_wrote(inode->i_sb, wpre->btop,
13592 + /*force*/h_inode->i_blocks > wpre->blks);
13597 + * todo: very ugly
13598 + * it locks both of i_mutex and si_rwsem for read in safe.
13599 + * if the plink maintenance mode continues forever (that is the problem),
13600 + * may loop forever.
13602 +static void au_mtx_and_read_lock(struct inode *inode)
13605 + struct super_block *sb = inode->i_sb;
13608 + inode_lock(inode);
13609 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13612 + inode_unlock(inode);
13613 + si_read_lock(sb, AuLock_NOPLMW);
13614 + si_read_unlock(sb);
13618 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13619 + struct iov_iter *iov_iter)
13622 + struct file *file;
13623 + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13625 + err = security_file_permission(h_file, rw);
13626 + if (unlikely(err))
13629 + err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
13631 + if (rw == MAY_READ)
13632 + iter = h_file->f_op->read_iter;
13633 + else if (rw == MAY_WRITE)
13634 + iter = h_file->f_op->write_iter;
13636 + file = kio->ki_filp;
13637 + kio->ki_filp = h_file;
13640 + err = iter(kio, iov_iter);
13643 + /* currently there is no such fs */
13645 + kio->ki_filp = file;
13651 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13654 + struct file *file, *h_file;
13655 + struct inode *inode;
13656 + struct super_block *sb;
13658 + file = kio->ki_filp;
13659 + inode = file_inode(file);
13660 + sb = inode->i_sb;
13661 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13663 + h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
13664 + err = PTR_ERR(h_file);
13665 + if (IS_ERR(h_file))
13668 + if (au_test_loopback_kthread()) {
13669 + au_warn_loopback(h_file->f_path.dentry->d_sb);
13670 + if (file->f_mapping != h_file->f_mapping) {
13671 + file->f_mapping = h_file->f_mapping;
13672 + smp_mb(); /* unnecessary? */
13675 + fi_read_unlock(file);
13677 + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13678 + /* todo: necessary? */
13679 + /* file->f_ra = h_file->f_ra; */
13680 + au_read_post(inode, h_file);
13683 + si_read_unlock(sb);
13687 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13690 + struct au_write_pre wpre;
13691 + struct inode *inode;
13692 + struct file *file, *h_file;
13694 + file = kio->ki_filp;
13695 + inode = file_inode(file);
13696 + au_mtx_and_read_lock(inode);
13699 + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13700 + err = PTR_ERR(h_file);
13701 + if (IS_ERR(h_file))
13704 + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13705 + au_write_post(inode, h_file, &wpre, err);
13708 + si_read_unlock(inode->i_sb);
13709 + inode_unlock(inode);
13714 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
13715 + * don't have their own .splice_{read,write} implimentations, and they use
13716 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
13717 + * simple converters to f_op->iter_read() and ->iter_write().
13718 + * But we keep our own implementations because some non-mainlined FSes may have
13719 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
13720 + * away an opportunity to co-work with aufs from them.
13722 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13723 + struct pipe_inode_info *pipe, size_t len,
13724 + unsigned int flags)
13727 + struct file *h_file;
13728 + struct inode *inode;
13729 + struct super_block *sb;
13731 + inode = file_inode(file);
13732 + sb = inode->i_sb;
13733 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13735 + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
13736 + err = PTR_ERR(h_file);
13737 + if (IS_ERR(h_file))
13740 + err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
13741 + /* todo: necessary? */
13742 + /* file->f_ra = h_file->f_ra; */
13743 + au_read_post(inode, h_file);
13746 + si_read_unlock(sb);
13751 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13752 + size_t len, unsigned int flags)
13755 + struct au_write_pre wpre;
13756 + struct inode *inode;
13757 + struct file *h_file;
13759 + inode = file_inode(file);
13760 + au_mtx_and_read_lock(inode);
13763 + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13764 + err = PTR_ERR(h_file);
13765 + if (IS_ERR(h_file))
13768 + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13769 + au_write_post(inode, h_file, &wpre, err);
13772 + si_read_unlock(inode->i_sb);
13773 + inode_unlock(inode);
13777 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13781 + struct au_write_pre wpre;
13782 + struct inode *inode;
13783 + struct file *h_file;
13785 + inode = file_inode(file);
13786 + au_mtx_and_read_lock(inode);
13789 + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13790 + err = PTR_ERR(h_file);
13791 + if (IS_ERR(h_file))
13795 + err = vfs_fallocate(h_file, mode, offset, len);
13798 + * we don't need to call file_modifed() here since au_write_post()
13799 + * is equivalent and copies-up all timestamps and permission bits.
13801 + au_write_post(inode, h_file, &wpre, /*written*/1);
13804 + si_read_unlock(inode->i_sb);
13805 + inode_unlock(inode);
13809 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
13810 + struct file *dst, loff_t dst_pos,
13811 + size_t len, unsigned int flags)
13814 + struct au_write_pre wpre;
13815 + enum { SRC, DST };
13817 + struct inode *inode;
13818 + struct file *h_file;
13819 + struct super_block *h_sb;
13821 +#define a_src a[SRC]
13822 +#define a_dst a[DST]
13825 + a_src.inode = file_inode(src);
13826 + if (unlikely(!S_ISREG(a_src.inode->i_mode)))
13828 + a_dst.inode = file_inode(dst);
13829 + if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
13832 + au_mtx_and_read_lock(a_dst.inode);
13834 + * in order to match the order in di_write_lock2_{child,parent}(),
13835 + * use f_path.dentry for this comparison.
13837 + if (src->f_path.dentry < dst->f_path.dentry) {
13838 + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
13839 + err = PTR_ERR(a_src.h_file);
13840 + if (IS_ERR(a_src.h_file))
13843 + wpre.lsc = AuLsc_FI_2;
13844 + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
13845 + err = PTR_ERR(a_dst.h_file);
13846 + if (IS_ERR(a_dst.h_file)) {
13847 + au_read_post(a_src.inode, a_src.h_file);
13851 + wpre.lsc = AuLsc_FI_1;
13852 + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
13853 + err = PTR_ERR(a_dst.h_file);
13854 + if (IS_ERR(a_dst.h_file))
13857 + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
13858 + err = PTR_ERR(a_src.h_file);
13859 + if (IS_ERR(a_src.h_file)) {
13860 + au_write_post(a_dst.inode, a_dst.h_file, &wpre,
13867 + a_src.h_sb = file_inode(a_src.h_file)->i_sb;
13868 + a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
13869 + if (unlikely(a_src.h_sb != a_dst.h_sb)) {
13875 + err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
13876 + dst_pos, len, flags);
13879 + au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
13880 + fi_read_unlock(src);
13881 + au_read_post(a_src.inode, a_src.h_file);
13883 + si_read_unlock(a_dst.inode->i_sb);
13884 + inode_unlock(a_dst.inode);
13891 +/* ---------------------------------------------------------------------- */
13894 + * The locking order around current->mmap_sem.
13895 + * - in most and regular cases
13896 + * file I/O syscall -- aufs_read() or something
13897 + * -- si_rwsem for read -- mmap_sem
13898 + * (Note that [fdi]i_rwsem are released before mmap_sem).
13900 + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13901 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
13902 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
13903 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13904 + * It means that when aufs acquires si_rwsem for write, the process should never
13905 + * acquire mmap_sem.
13907 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13908 + * problem either since any directory is not able to be mmap-ed.
13909 + * The similar scenario is applied to aufs_readlink() too.
13912 +#if 0 /* stop calling security_file_mmap() */
13913 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13914 +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
13916 +static unsigned long au_arch_prot_conv(unsigned long flags)
13918 + /* currently ppc64 only */
13919 +#ifdef CONFIG_PPC64
13920 + /* cf. linux/arch/powerpc/include/asm/mman.h */
13921 + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13922 + return AuConv_VM_PROT(flags, SAO);
13924 + AuDebugOn(arch_calc_vm_prot_bits(-1));
13929 +static unsigned long au_prot_conv(unsigned long flags)
13931 + return AuConv_VM_PROT(flags, READ)
13932 + | AuConv_VM_PROT(flags, WRITE)
13933 + | AuConv_VM_PROT(flags, EXEC)
13934 + | au_arch_prot_conv(flags);
13937 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13938 +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
13940 +static unsigned long au_flag_conv(unsigned long flags)
13942 + return AuConv_VM_MAP(flags, GROWSDOWN)
13943 + | AuConv_VM_MAP(flags, DENYWRITE)
13944 + | AuConv_VM_MAP(flags, LOCKED);
13948 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13951 + const unsigned char wlock
13952 + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13953 + struct super_block *sb;
13954 + struct file *h_file;
13955 + struct inode *inode;
13957 + AuDbgVmRegion(file, vma);
13959 + inode = file_inode(file);
13960 + sb = inode->i_sb;
13962 + si_read_lock(sb, AuLock_NOPLMW);
13964 + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
13966 + err = PTR_ERR(h_file);
13967 + if (IS_ERR(h_file))
13971 + au_set_mmapped(file);
13972 + au_vm_file_reset(vma, h_file);
13974 + * we cannot call security_mmap_file() here since it may acquire
13975 + * mmap_sem or i_mutex.
13977 + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13978 + * au_flag_conv(vma->vm_flags));
13981 + err = call_mmap(h_file, vma);
13983 + au_vm_prfile_set(vma, file);
13984 + fsstack_copy_attr_atime(inode, file_inode(h_file));
13985 + goto out_fput; /* success */
13987 + au_unset_mmapped(file);
13988 + au_vm_file_reset(vma, file);
13992 + ii_write_unlock(inode);
13997 + si_read_unlock(sb);
14003 +/* ---------------------------------------------------------------------- */
14005 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14009 + struct au_write_pre wpre;
14010 + struct inode *inode;
14011 + struct file *h_file;
14013 + err = 0; /* -EBADF; */ /* posix? */
14014 + if (unlikely(!(file->f_mode & FMODE_WRITE)))
14017 + inode = file_inode(file);
14018 + au_mtx_and_read_lock(inode);
14021 + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14022 + err = PTR_ERR(h_file);
14023 + if (IS_ERR(h_file))
14026 + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
14027 + au_write_post(inode, h_file, &wpre, /*written*/0);
14030 + si_read_unlock(inode->i_sb);
14031 + inode_unlock(inode);
14036 +static int aufs_fasync(int fd, struct file *file, int flag)
14039 + struct file *h_file;
14040 + struct super_block *sb;
14042 + sb = file->f_path.dentry->d_sb;
14043 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14045 + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
14046 + err = PTR_ERR(h_file);
14047 + if (IS_ERR(h_file))
14050 + if (h_file->f_op->fasync)
14051 + err = h_file->f_op->fasync(fd, h_file, flag);
14052 + fput(h_file); /* instead of au_read_post() */
14055 + si_read_unlock(sb);
14059 +static int aufs_setfl(struct file *file, unsigned long arg)
14062 + struct file *h_file;
14063 + struct super_block *sb;
14065 + sb = file->f_path.dentry->d_sb;
14066 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14068 + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
14069 + err = PTR_ERR(h_file);
14070 + if (IS_ERR(h_file))
14073 + /* stop calling h_file->fasync */
14074 + arg |= vfsub_file_flags(file) & FASYNC;
14075 + err = setfl(/*unused fd*/-1, h_file, arg);
14076 + fput(h_file); /* instead of au_read_post() */
14079 + si_read_unlock(sb);
14083 +/* ---------------------------------------------------------------------- */
14085 +const struct file_operations aufs_file_fop = {
14086 + .owner = THIS_MODULE,
14088 + .llseek = default_llseek,
14090 + .read_iter = aufs_read_iter,
14091 + .write_iter = aufs_write_iter,
14093 +#ifdef CONFIG_AUFS_POLL
14094 + .poll = aufs_poll,
14096 + .unlocked_ioctl = aufs_ioctl_nondir,
14097 +#ifdef CONFIG_COMPAT
14098 + .compat_ioctl = aufs_compat_ioctl_nondir,
14100 + .mmap = aufs_mmap,
14101 + .open = aufs_open_nondir,
14102 + .flush = aufs_flush_nondir,
14103 + .release = aufs_release_nondir,
14104 + .fsync = aufs_fsync_nondir,
14105 + .fasync = aufs_fasync,
14106 + .setfl = aufs_setfl,
14107 + .splice_write = aufs_splice_write,
14108 + .splice_read = aufs_splice_read,
14109 +#if 0 /* reserved for future use */
14110 + .aio_splice_write = aufs_aio_splice_write,
14111 + .aio_splice_read = aufs_aio_splice_read,
14113 + .fallocate = aufs_fallocate,
14114 + .copy_file_range = aufs_copy_file_range
14116 diff --git a/fs/aufs/fhsm.c b/fs/aufs/fhsm.c
14117 new file mode 100644
14118 index 00000000000000..91262fbbbfaed0
14120 +++ b/fs/aufs/fhsm.c
14122 +// SPDX-License-Identifier: GPL-2.0
14124 + * Copyright (C) 2011-2022 Junjiro R. Okajima
14126 + * This program is free software; you can redistribute it and/or modify
14127 + * it under the terms of the GNU General Public License as published by
14128 + * the Free Software Foundation; either version 2 of the License, or
14129 + * (at your option) any later version.
14131 + * This program is distributed in the hope that it will be useful,
14132 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14133 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14134 + * GNU General Public License for more details.
14136 + * You should have received a copy of the GNU General Public License
14137 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
14141 + * File-based Hierarchy Storage Management
14144 +#include <linux/anon_inodes.h>
14145 +#include <linux/poll.h>
14146 +#include <linux/seq_file.h>
14147 +#include <linux/statfs.h>
14150 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
14152 + struct au_sbinfo *sbinfo;
14153 + struct au_fhsm *fhsm;
14155 + SiMustAnyLock(sb);
14157 + sbinfo = au_sbi(sb);
14158 + fhsm = &sbinfo->si_fhsm;
14159 + AuDebugOn(!fhsm);
14160 + return fhsm->fhsm_bottom;
14163 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
14165 + struct au_sbinfo *sbinfo;
14166 + struct au_fhsm *fhsm;
14168 + SiMustWriteLock(sb);
14170 + sbinfo = au_sbi(sb);
14171 + fhsm = &sbinfo->si_fhsm;
14172 + AuDebugOn(!fhsm);
14173 + fhsm->fhsm_bottom = bindex;
14176 +/* ---------------------------------------------------------------------- */
14178 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
14180 + struct au_br_fhsm *bf;
14182 + bf = br->br_fhsm;
14183 + MtxMustLock(&bf->bf_lock);
14185 + return !bf->bf_readable
14186 + || time_after(jiffies,
14187 + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
14190 +/* ---------------------------------------------------------------------- */
14192 +static void au_fhsm_notify(struct super_block *sb, int val)
14194 + struct au_sbinfo *sbinfo;
14195 + struct au_fhsm *fhsm;
14197 + SiMustAnyLock(sb);
14199 + sbinfo = au_sbi(sb);
14200 + fhsm = &sbinfo->si_fhsm;
14201 + if (au_fhsm_pid(fhsm)
14202 + && atomic_read(&fhsm->fhsm_readable) != -1) {
14203 + atomic_set(&fhsm->fhsm_readable, val);
14205 + wake_up(&fhsm->fhsm_wqh);
14209 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
14210 + struct aufs_stfs *rstfs, int do_lock, int do_notify)
14213 + struct au_branch *br;
14214 + struct au_br_fhsm *bf;
14216 + br = au_sbr(sb, bindex);
14217 + AuDebugOn(au_br_rdonly(br));
14218 + bf = br->br_fhsm;
14222 + mutex_lock(&bf->bf_lock);
14224 + MtxMustLock(&bf->bf_lock);
14226 + /* sb->s_root for NFS is unreliable */
14227 + err = au_br_stfs(br, &bf->bf_stfs);
14228 + if (unlikely(err)) {
14229 + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
14233 + bf->bf_jiffy = jiffies;
14234 + bf->bf_readable = 1;
14236 + au_fhsm_notify(sb, /*val*/1);
14238 + *rstfs = bf->bf_stfs;
14242 + mutex_unlock(&bf->bf_lock);
14243 + au_fhsm_notify(sb, /*val*/1);
14248 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
14251 + struct au_sbinfo *sbinfo;
14252 + struct au_fhsm *fhsm;
14253 + struct au_branch *br;
14254 + struct au_br_fhsm *bf;
14256 + AuDbg("b%d, force %d\n", bindex, force);
14257 + SiMustAnyLock(sb);
14259 + sbinfo = au_sbi(sb);
14260 + fhsm = &sbinfo->si_fhsm;
14261 + if (!au_ftest_si(sbinfo, FHSM)
14262 + || fhsm->fhsm_bottom == bindex)
14265 + br = au_sbr(sb, bindex);
14266 + bf = br->br_fhsm;
14268 + mutex_lock(&bf->bf_lock);
14270 + || au_fhsm_pid(fhsm)
14271 + || au_fhsm_test_jiffy(sbinfo, br))
14272 + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
14274 + mutex_unlock(&bf->bf_lock);
14277 +void au_fhsm_wrote_all(struct super_block *sb, int force)
14279 + aufs_bindex_t bindex, bbot;
14280 + struct au_branch *br;
14282 + /* exclude the bottom */
14283 + bbot = au_fhsm_bottom(sb);
14284 + for (bindex = 0; bindex < bbot; bindex++) {
14285 + br = au_sbr(sb, bindex);
14286 + if (au_br_fhsm(br->br_perm))
14287 + au_fhsm_wrote(sb, bindex, force);
14291 +/* ---------------------------------------------------------------------- */
14293 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
14296 + struct au_sbinfo *sbinfo;
14297 + struct au_fhsm *fhsm;
14300 + sbinfo = file->private_data;
14301 + fhsm = &sbinfo->si_fhsm;
14302 + poll_wait(file, &fhsm->fhsm_wqh, wait);
14303 + if (atomic_read(&fhsm->fhsm_readable))
14304 + mask = EPOLLIN /* | EPOLLRDNORM */;
14307 + AuDbg("mask 0x%x\n", mask);
14311 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
14312 + struct aufs_stfs *stfs, __s16 brid)
14316 + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
14318 + err = __put_user(brid, &stbr->brid);
14319 + if (unlikely(err))
14325 +static ssize_t au_fhsm_do_read(struct super_block *sb,
14326 + struct aufs_stbr __user *stbr, size_t count)
14330 + aufs_bindex_t bindex, bbot;
14331 + struct au_branch *br;
14332 + struct au_br_fhsm *bf;
14334 + /* except the bottom branch */
14337 + bbot = au_fhsm_bottom(sb);
14338 + for (bindex = 0; !err && bindex < bbot; bindex++) {
14339 + br = au_sbr(sb, bindex);
14340 + if (!au_br_fhsm(br->br_perm))
14343 + bf = br->br_fhsm;
14344 + mutex_lock(&bf->bf_lock);
14345 + if (bf->bf_readable) {
14347 + if (count >= sizeof(*stbr))
14348 + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14351 + bf->bf_readable = 0;
14352 + count -= sizeof(*stbr);
14356 + mutex_unlock(&bf->bf_lock);
14359 + err = sizeof(*stbr) * nstbr;
14364 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14369 + aufs_bindex_t nfhsm, bindex, bbot;
14370 + struct au_sbinfo *sbinfo;
14371 + struct au_fhsm *fhsm;
14372 + struct au_branch *br;
14373 + struct super_block *sb;
14376 + sbinfo = file->private_data;
14377 + fhsm = &sbinfo->si_fhsm;
14379 + spin_lock_irq(&fhsm->fhsm_wqh.lock);
14380 + if (!atomic_read(&fhsm->fhsm_readable)) {
14381 + if (vfsub_file_flags(file) & O_NONBLOCK)
14384 + err = wait_event_interruptible_locked_irq
14386 + atomic_read(&fhsm->fhsm_readable));
14388 + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14389 + if (unlikely(err))
14392 + /* sb may already be dead */
14393 + au_rw_read_lock(&sbinfo->si_rwsem);
14394 + readable = atomic_read(&fhsm->fhsm_readable);
14395 + if (readable > 0) {
14396 + sb = sbinfo->si_sb;
14398 + /* exclude the bottom branch */
14400 + bbot = au_fhsm_bottom(sb);
14401 + for (bindex = 0; bindex < bbot; bindex++) {
14402 + br = au_sbr(sb, bindex);
14403 + if (au_br_fhsm(br->br_perm))
14407 + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14408 + atomic_set(&fhsm->fhsm_readable, 0);
14409 + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14413 + au_rw_read_unlock(&sbinfo->si_rwsem);
14421 +static int au_fhsm_release(struct inode *inode, struct file *file)
14423 + struct au_sbinfo *sbinfo;
14424 + struct au_fhsm *fhsm;
14426 + /* sb may already be dead */
14427 + sbinfo = file->private_data;
14428 + fhsm = &sbinfo->si_fhsm;
14429 + spin_lock(&fhsm->fhsm_spin);
14430 + fhsm->fhsm_pid = 0;
14431 + spin_unlock(&fhsm->fhsm_spin);
14432 + kobject_put(&sbinfo->si_kobj);
14437 +static const struct file_operations au_fhsm_fops = {
14438 + .owner = THIS_MODULE,
14439 + .llseek = noop_llseek,
14440 + .read = au_fhsm_read,
14441 + .poll = au_fhsm_poll,
14442 + .release = au_fhsm_release
14445 +int au_fhsm_fd(struct super_block *sb, int oflags)
14448 + struct au_sbinfo *sbinfo;
14449 + struct au_fhsm *fhsm;
14452 + if (unlikely(!capable(CAP_SYS_ADMIN)))
14456 + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14460 + sbinfo = au_sbi(sb);
14461 + fhsm = &sbinfo->si_fhsm;
14462 + spin_lock(&fhsm->fhsm_spin);
14463 + if (!fhsm->fhsm_pid)
14464 + fhsm->fhsm_pid = current->pid;
14467 + spin_unlock(&fhsm->fhsm_spin);
14468 + if (unlikely(err))
14471 + oflags |= O_RDONLY;
14472 + /* oflags |= FMODE_NONOTIFY; */
14473 + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14475 + if (unlikely(fd < 0))
14478 + /* succeed regardless 'fhsm' status */
14479 + kobject_get(&sbinfo->si_kobj);
14480 + si_noflush_read_lock(sb);
14481 + if (au_ftest_si(sbinfo, FHSM))
14482 + au_fhsm_wrote_all(sb, /*force*/0);
14483 + si_read_unlock(sb);
14484 + goto out; /* success */
14487 + spin_lock(&fhsm->fhsm_spin);
14488 + fhsm->fhsm_pid = 0;
14489 + spin_unlock(&fhsm->fhsm_spin);
14495 +/* ---------------------------------------------------------------------- */
14497 +int au_fhsm_br_alloc(struct au_branch *br)
14502 + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14504 + au_br_fhsm_init(br->br_fhsm);
14511 +/* ---------------------------------------------------------------------- */
14513 +void au_fhsm_fin(struct super_block *sb)
14515 + au_fhsm_notify(sb, /*val*/-1);
14518 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14520 + struct au_fhsm *fhsm;
14522 + fhsm = &sbinfo->si_fhsm;
14523 + spin_lock_init(&fhsm->fhsm_spin);
14524 + init_waitqueue_head(&fhsm->fhsm_wqh);
14525 + atomic_set(&fhsm->fhsm_readable, 0);
14526 + fhsm->fhsm_expire
14527 + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14528 + fhsm->fhsm_bottom = -1;
14531 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14533 + sbinfo->si_fhsm.fhsm_expire
14534 + = msecs_to_jiffies(sec * MSEC_PER_SEC);
14537 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14541 + if (!au_ftest_si(sbinfo, FHSM))
14544 + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14545 + if (u != AUFS_FHSM_CACHE_DEF_SEC)
14546 + seq_printf(seq, ",fhsm_sec=%u", u);
14548 diff --git a/fs/aufs/file.c b/fs/aufs/file.c
14549 new file mode 100644
14550 index 00000000000000..76ad444328db50
14552 +++ b/fs/aufs/file.c
14554 +// SPDX-License-Identifier: GPL-2.0
14556 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14558 + * This program is free software; you can redistribute it and/or modify
14559 + * it under the terms of the GNU General Public License as published by
14560 + * the Free Software Foundation; either version 2 of the License, or
14561 + * (at your option) any later version.
14563 + * This program is distributed in the hope that it will be useful,
14564 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14565 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14566 + * GNU General Public License for more details.
14568 + * You should have received a copy of the GNU General Public License
14569 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
14573 + * handling file/dir, and address_space operation
14576 +#ifdef CONFIG_AUFS_DEBUG
14577 +#include <linux/migrate.h>
14579 +#include <linux/pagemap.h>
14582 +/* drop flags for writing */
14583 +unsigned int au_file_roflags(unsigned int flags)
14585 + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14586 + flags |= O_RDONLY | O_NOATIME;
14590 +/* common functions to regular file and dir */
14591 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14592 + struct file *file, int force_wr)
14594 + struct file *h_file;
14595 + struct dentry *h_dentry;
14596 + struct inode *h_inode;
14597 + struct super_block *sb;
14598 + struct au_branch *br;
14599 + struct path h_path;
14602 + /* a race condition can happen between open and unlink/rmdir */
14603 + h_file = ERR_PTR(-ENOENT);
14604 + h_dentry = au_h_dptr(dentry, bindex);
14605 + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14607 + h_inode = d_inode(h_dentry);
14608 + spin_lock(&h_dentry->d_lock);
14609 + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14610 + /* || !d_inode(dentry)->i_nlink */
14612 + spin_unlock(&h_dentry->d_lock);
14613 + if (unlikely(err))
14616 + sb = dentry->d_sb;
14617 + br = au_sbr(sb, bindex);
14618 + err = au_br_test_oflag(flags, br);
14619 + h_file = ERR_PTR(err);
14620 + if (unlikely(err))
14623 + /* drop flags for writing */
14624 + if (au_test_ro(sb, bindex, d_inode(dentry))) {
14625 + if (force_wr && !(flags & O_WRONLY))
14627 + flags = au_file_roflags(flags);
14629 + h_file = ERR_PTR(-EROFS);
14630 + flags = au_file_roflags(flags);
14631 + if (unlikely(vfsub_native_ro(h_inode)
14632 + || IS_APPEND(h_inode)))
14634 + flags &= ~O_ACCMODE;
14635 + flags |= O_WRONLY;
14638 + flags &= ~O_CREAT;
14639 + au_lcnt_inc(&br->br_nfiles);
14640 + h_path.dentry = h_dentry;
14641 + h_path.mnt = au_br_mnt(br);
14643 + * vfs::backing_file_open() looks promising since it can get rid of
14644 + * mm::vm_prfile approach from my mind.
14645 + * but I keep current approach for a while.
14647 + h_file = vfsub_dentry_open(&h_path, flags);
14648 + if (IS_ERR(h_file))
14651 + if (flags & __FMODE_EXEC) {
14652 + err = deny_write_access(h_file);
14653 + if (unlikely(err)) {
14655 + h_file = ERR_PTR(err);
14659 + fsnotify_open(h_file);
14660 + goto out; /* success */
14663 + au_lcnt_dec(&br->br_nfiles);
14668 +static int au_cmoo(struct dentry *dentry)
14670 + int err, cmoo, matched;
14671 + unsigned int udba;
14672 + struct path h_path;
14673 + struct au_pin pin;
14674 + struct au_cp_generic cpg = {
14675 + .dentry = dentry,
14680 + .flags = AuCpup_DTIME | AuCpup_HOPEN
14682 + struct inode *delegated;
14683 + struct super_block *sb;
14684 + struct au_sbinfo *sbinfo;
14685 + struct au_fhsm *fhsm;
14687 + struct au_branch *br;
14688 + struct dentry *parent;
14689 + struct au_hinode *hdir;
14691 + DiMustWriteLock(dentry);
14692 + IiMustWriteLock(d_inode(dentry));
14695 + if (IS_ROOT(dentry))
14697 + cpg.bsrc = au_dbtop(dentry);
14701 + sb = dentry->d_sb;
14702 + sbinfo = au_sbi(sb);
14703 + fhsm = &sbinfo->si_fhsm;
14704 + pid = au_fhsm_pid(fhsm);
14707 + && (current->pid == pid
14708 + || rcu_dereference(current->real_parent)->pid == pid));
14709 + rcu_read_unlock();
14713 + br = au_sbr(sb, cpg.bsrc);
14714 + cmoo = au_br_cmoo(br->br_perm);
14717 + if (!d_is_reg(dentry))
14718 + cmoo &= AuBrAttr_COO_ALL;
14722 + parent = dget_parent(dentry);
14723 + di_write_lock_parent(parent);
14724 + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14726 + if (unlikely(err < 0)) {
14727 + err = 0; /* there is no upper writable branch */
14730 + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14732 + /* do not respect the coo attrib for the target branch */
14733 + err = au_cpup_dirs(dentry, cpg.bdst);
14734 + if (unlikely(err))
14737 + di_downgrade_lock(parent, AuLock_IR);
14738 + udba = au_opt_udba(sb);
14739 + err = au_pin(&pin, dentry, cpg.bdst, udba,
14740 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14741 + if (unlikely(err))
14744 + err = au_sio_cpup_simple(&cpg);
14746 + if (unlikely(err))
14748 + if (!(cmoo & AuBrWAttr_MOO))
14749 + goto out_parent; /* success */
14751 + err = au_pin(&pin, dentry, cpg.bsrc, udba,
14752 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14753 + if (unlikely(err))
14756 + h_path.mnt = au_br_mnt(br);
14757 + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14758 + hdir = au_hi(d_inode(parent), cpg.bsrc);
14759 + delegated = NULL;
14760 + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14762 + /* todo: keep h_dentry or not? */
14763 + if (unlikely(err == -EWOULDBLOCK)) {
14764 + pr_warn("cannot retry for NFSv4 delegation"
14765 + " for an internal unlink\n");
14768 + if (unlikely(err)) {
14769 + pr_err("unlink %pd after coo failed (%d), ignored\n",
14773 + goto out_parent; /* success */
14776 + di_downgrade_lock(parent, AuLock_IR);
14778 + di_read_unlock(parent, AuLock_IR);
14785 +int au_do_open(struct file *file, struct au_do_open_args *args)
14787 + int err, aopen = args->aopen;
14788 + struct dentry *dentry;
14789 + struct au_finfo *finfo;
14792 + err = au_finfo_init(file, args->fidir);
14795 + err = au_finfo_init(file, args->fidir);
14798 + if (unlikely(err))
14801 + dentry = file->f_path.dentry;
14802 + AuDebugOn(IS_ERR_OR_NULL(dentry));
14803 + di_write_lock_child(dentry);
14804 + err = au_cmoo(dentry);
14807 + err = args->open(file, vfsub_file_flags(file),
14808 + au_di(dentry)->di_htmpfile);
14809 + di_write_unlock(dentry);
14811 + di_downgrade_lock(dentry, AuLock_IR);
14813 + err = args->open(file, vfsub_file_flags(file),
14816 + di_read_unlock(dentry, AuLock_IR);
14820 + finfo = au_fi(file);
14822 + finfo->fi_file = file;
14823 + au_hbl_add(&finfo->fi_hlist,
14824 + &au_sbi(file->f_path.dentry->d_sb)->si_files);
14827 + fi_write_unlock(file);
14830 + fi_write_unlock(file);
14833 + if (unlikely(err)) {
14834 + finfo->fi_hdir = NULL;
14835 + au_finfo_fin(file);
14843 +int au_reopen_nondir(struct file *file)
14846 + aufs_bindex_t btop;
14847 + struct dentry *dentry;
14848 + struct au_branch *br;
14849 + struct file *h_file, *h_file_tmp;
14851 + dentry = file->f_path.dentry;
14852 + btop = au_dbtop(dentry);
14853 + br = au_sbr(dentry->d_sb, btop);
14854 + h_file_tmp = NULL;
14855 + if (au_fbtop(file) == btop) {
14856 + h_file = au_hf_top(file);
14857 + if (file->f_mode == h_file->f_mode)
14858 + return 0; /* success */
14859 + h_file_tmp = h_file;
14860 + get_file(h_file_tmp);
14861 + au_lcnt_inc(&br->br_nfiles);
14862 + au_set_h_fptr(file, btop, NULL);
14864 + AuDebugOn(au_fi(file)->fi_hdir);
14867 + * file exists on both of rw and ro
14868 + * open --> dbtop and fbtop are both 0
14869 + * prepend a branch as rw, "rw" become ro
14871 + * delete the top branch, "rw" becomes rw again
14872 + * --> dbtop is 1, fbtop is still 0
14873 + * write --> fbtop is 0 but dbtop is 1
14875 + /* AuDebugOn(au_fbtop(file) < btop); */
14877 + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14878 + file, /*force_wr*/0);
14879 + err = PTR_ERR(h_file);
14880 + if (IS_ERR(h_file)) {
14881 + if (h_file_tmp) {
14883 + au_set_h_fptr(file, btop, h_file_tmp);
14884 + h_file_tmp = NULL;
14886 + goto out; /* todo: close all? */
14890 + au_set_fbtop(file, btop);
14891 + au_set_h_fptr(file, btop, h_file);
14892 + au_update_figen(file);
14893 + /* todo: necessary? */
14894 + /* file->f_ra = h_file->f_ra; */
14897 + if (h_file_tmp) {
14898 + fput(h_file_tmp);
14899 + au_lcnt_dec(&br->br_nfiles);
14904 +/* ---------------------------------------------------------------------- */
14906 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14907 + struct dentry *hi_wh)
14910 + aufs_bindex_t btop;
14911 + struct au_dinfo *dinfo;
14912 + struct dentry *h_dentry;
14913 + struct au_hdentry *hdp;
14915 + dinfo = au_di(file->f_path.dentry);
14916 + AuRwMustWriteLock(&dinfo->di_rwsem);
14918 + btop = dinfo->di_btop;
14919 + dinfo->di_btop = btgt;
14920 + hdp = au_hdentry(dinfo, btgt);
14921 + h_dentry = hdp->hd_dentry;
14922 + hdp->hd_dentry = hi_wh;
14923 + err = au_reopen_nondir(file);
14924 + hdp->hd_dentry = h_dentry;
14925 + dinfo->di_btop = btop;
14930 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14931 + aufs_bindex_t bcpup, struct au_pin *pin)
14934 + struct inode *inode, *h_inode;
14935 + struct dentry *h_dentry, *hi_wh;
14936 + struct au_cp_generic cpg = {
14937 + .dentry = file->f_path.dentry,
14944 + au_update_dbtop(cpg.dentry);
14945 + inode = d_inode(cpg.dentry);
14947 + if (au_dbtop(cpg.dentry) <= bcpup
14948 + && au_dbbot(cpg.dentry) >= bcpup) {
14949 + h_dentry = au_h_dptr(cpg.dentry, bcpup);
14950 + if (h_dentry && d_is_positive(h_dentry))
14951 + h_inode = d_inode(h_dentry);
14953 + hi_wh = au_hi_wh(inode, bcpup);
14954 + if (!hi_wh && !h_inode)
14955 + err = au_sio_cpup_wh(&cpg, file);
14957 + /* already copied-up after unlink */
14958 + err = au_reopen_wh(file, bcpup, hi_wh);
14961 + && (inode->i_nlink > 1
14962 + || (inode->i_state & I_LINKABLE))
14963 + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14964 + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14970 + * prepare the @file for writing.
14972 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14975 + aufs_bindex_t dbtop;
14976 + struct dentry *parent;
14977 + struct inode *inode;
14978 + struct super_block *sb;
14979 + struct file *h_file;
14980 + struct au_cp_generic cpg = {
14981 + .dentry = file->f_path.dentry,
14986 + .flags = AuCpup_DTIME
14989 + sb = cpg.dentry->d_sb;
14990 + inode = d_inode(cpg.dentry);
14991 + cpg.bsrc = au_fbtop(file);
14992 + err = au_test_ro(sb, cpg.bsrc, inode);
14993 + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14994 + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14999 + /* need to cpup or reopen */
15000 + parent = dget_parent(cpg.dentry);
15001 + di_write_lock_parent(parent);
15002 + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
15004 + if (unlikely(err < 0))
15008 + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
15009 + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
15010 + if (unlikely(err))
15014 + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
15015 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15016 + if (unlikely(err))
15019 + dbtop = au_dbtop(cpg.dentry);
15020 + if (dbtop <= cpg.bdst)
15021 + cpg.bsrc = cpg.bdst;
15023 + if (dbtop <= cpg.bdst /* just reopen */
15024 + || !d_unhashed(cpg.dentry) /* copyup and reopen */
15026 + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
15027 + if (IS_ERR(h_file))
15028 + err = PTR_ERR(h_file);
15030 + di_downgrade_lock(parent, AuLock_IR);
15031 + if (dbtop > cpg.bdst)
15032 + err = au_sio_cpup_simple(&cpg);
15034 + err = au_reopen_nondir(file);
15035 + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
15037 + } else { /* copyup as wh and reopen */
15039 + * since writable hfsplus branch is not supported,
15040 + * h_open_pre/post() are unnecessary.
15042 + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
15043 + di_downgrade_lock(parent, AuLock_IR);
15047 + au_pin_set_parent_lflag(pin, /*lflag*/0);
15048 + goto out_dput; /* success */
15054 + di_downgrade_lock(parent, AuLock_IR);
15056 + di_read_unlock(parent, AuLock_IR);
15063 +/* ---------------------------------------------------------------------- */
15065 +int au_do_flush(struct file *file, fl_owner_t id,
15066 + int (*flush)(struct file *file, fl_owner_t id))
15069 + struct super_block *sb;
15070 + struct inode *inode;
15072 + inode = file_inode(file);
15073 + sb = inode->i_sb;
15074 + si_noflush_read_lock(sb);
15075 + fi_read_lock(file);
15076 + ii_read_lock_child(inode);
15078 + err = flush(file, id);
15079 + au_cpup_attr_timesizes(inode);
15081 + ii_read_unlock(inode);
15082 + fi_read_unlock(file);
15083 + si_read_unlock(sb);
15087 +/* ---------------------------------------------------------------------- */
15089 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
15092 + struct au_pin pin;
15093 + struct au_finfo *finfo;
15094 + struct dentry *parent, *hi_wh;
15095 + struct inode *inode;
15096 + struct super_block *sb;
15097 + struct au_cp_generic cpg = {
15098 + .dentry = file->f_path.dentry,
15103 + .flags = AuCpup_DTIME
15106 + FiMustWriteLock(file);
15109 + finfo = au_fi(file);
15110 + sb = cpg.dentry->d_sb;
15111 + inode = d_inode(cpg.dentry);
15112 + cpg.bdst = au_ibtop(inode);
15113 + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
15116 + parent = dget_parent(cpg.dentry);
15117 + if (au_test_ro(sb, cpg.bdst, inode)) {
15118 + di_read_lock_parent(parent, !AuLock_IR);
15119 + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
15121 + di_read_unlock(parent, !AuLock_IR);
15122 + if (unlikely(err < 0))
15127 + di_read_lock_parent(parent, AuLock_IR);
15128 + hi_wh = au_hi_wh(inode, cpg.bdst);
15129 + if (!S_ISDIR(inode->i_mode)
15130 + && au_opt_test(au_mntflags(sb), PLINK)
15131 + && au_plink_test(inode)
15132 + && !d_unhashed(cpg.dentry)
15133 + && cpg.bdst < au_dbtop(cpg.dentry)) {
15134 + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
15135 + if (unlikely(err))
15138 + /* always superio. */
15139 + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
15140 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15142 + err = au_sio_cpup_simple(&cpg);
15145 + } else if (hi_wh) {
15146 + /* already copied-up after unlink */
15147 + err = au_reopen_wh(file, cpg.bdst, hi_wh);
15148 + *need_reopen = 0;
15152 + di_read_unlock(parent, AuLock_IR);
15159 +static void au_do_refresh_dir(struct file *file)
15161 + aufs_bindex_t bindex, bbot, new_bindex, brid;
15162 + struct au_hfile *p, tmp, *q;
15163 + struct au_finfo *finfo;
15164 + struct super_block *sb;
15165 + struct au_fidir *fidir;
15167 + FiMustWriteLock(file);
15169 + sb = file->f_path.dentry->d_sb;
15170 + finfo = au_fi(file);
15171 + fidir = finfo->fi_hdir;
15172 + AuDebugOn(!fidir);
15173 + p = fidir->fd_hfile + finfo->fi_btop;
15174 + brid = p->hf_br->br_id;
15175 + bbot = fidir->fd_bbot;
15176 + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
15180 + new_bindex = au_br_index(sb, p->hf_br->br_id);
15181 + if (new_bindex == bindex)
15183 + if (new_bindex < 0) {
15184 + au_set_h_fptr(file, bindex, NULL);
15188 + /* swap two lower inode, and loop again */
15189 + q = fidir->fd_hfile + new_bindex;
15193 + if (tmp.hf_file) {
15199 + p = fidir->fd_hfile;
15200 + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
15201 + bbot = au_sbbot(sb);
15202 + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
15203 + finfo->fi_btop++, p++)
15204 + if (p->hf_file) {
15205 + if (file_inode(p->hf_file))
15207 + au_hfput(p, /*execed*/0);
15210 + bbot = au_br_index(sb, brid);
15211 + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
15212 + finfo->fi_btop++, p++)
15214 + au_hfput(p, /*execed*/0);
15215 + bbot = au_sbbot(sb);
15218 + p = fidir->fd_hfile + bbot;
15219 + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
15220 + fidir->fd_bbot--, p--)
15221 + if (p->hf_file) {
15222 + if (file_inode(p->hf_file))
15224 + au_hfput(p, /*execed*/0);
15226 + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
15230 + * after branch manipulating, refresh the file.
15232 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
15234 + int err, need_reopen, nbr;
15235 + aufs_bindex_t bbot, bindex;
15236 + struct dentry *dentry;
15237 + struct super_block *sb;
15238 + struct au_finfo *finfo;
15239 + struct au_hfile *hfile;
15241 + dentry = file->f_path.dentry;
15242 + sb = dentry->d_sb;
15243 + nbr = au_sbbot(sb) + 1;
15244 + finfo = au_fi(file);
15245 + if (!finfo->fi_hdir) {
15246 + hfile = &finfo->fi_htop;
15247 + AuDebugOn(!hfile->hf_file);
15248 + bindex = au_br_index(sb, hfile->hf_br->br_id);
15249 + AuDebugOn(bindex < 0);
15250 + if (bindex != finfo->fi_btop)
15251 + au_set_fbtop(file, bindex);
15253 + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
15254 + if (unlikely(err))
15256 + au_do_refresh_dir(file);
15261 + if (!au_test_mmapped(file))
15262 + err = au_file_refresh_by_inode(file, &need_reopen);
15263 + if (finfo->fi_hdir)
15264 + /* harmless if err */
15265 + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
15266 + if (!err && need_reopen && !d_unlinked(dentry))
15267 + err = reopen(file);
15269 + au_update_figen(file);
15270 + goto out; /* success */
15273 + /* error, close all lower files */
15274 + if (finfo->fi_hdir) {
15275 + bbot = au_fbbot_dir(file);
15276 + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
15277 + au_set_h_fptr(file, bindex, NULL);
15284 +/* common function to regular file and dir */
15285 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15286 + int wlock, unsigned int fi_lsc)
15289 + unsigned int sigen, figen;
15290 + aufs_bindex_t btop;
15291 + unsigned char pseudo_link;
15292 + struct dentry *dentry;
15293 + struct inode *inode;
15296 + dentry = file->f_path.dentry;
15297 + inode = d_inode(dentry);
15298 + sigen = au_sigen(dentry->d_sb);
15299 + fi_write_lock_nested(file, fi_lsc);
15300 + figen = au_figen(file);
15302 + di_write_lock_child(dentry);
15304 + di_write_lock_child2(dentry);
15305 + btop = au_dbtop(dentry);
15306 + pseudo_link = (btop != au_ibtop(inode));
15307 + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
15309 + di_downgrade_lock(dentry, AuLock_IR);
15310 + fi_downgrade_lock(file);
15312 + goto out; /* success */
15315 + AuDbg("sigen %d, figen %d\n", sigen, figen);
15316 + if (au_digen_test(dentry, sigen)) {
15317 + err = au_reval_dpath(dentry, sigen);
15318 + AuDebugOn(!err && au_digen_test(dentry, sigen));
15322 + err = refresh_file(file, reopen);
15325 + di_downgrade_lock(dentry, AuLock_IR);
15326 + fi_downgrade_lock(file);
15329 + di_write_unlock(dentry);
15330 + fi_write_unlock(file);
15337 +/* ---------------------------------------------------------------------- */
15339 +/* cf. aufs_nopage() */
15340 +/* for madvise(2) */
15341 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
15343 + folio_unlock(folio);
15347 +/* it will never be called, but necessary to support O_DIRECT */
15348 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
15349 +{ BUG(); return 0; }
15351 +/* they will never be called. */
15352 +#ifdef CONFIG_AUFS_DEBUG
15353 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
15354 + loff_t pos, unsigned len,
15355 + struct page **pagep, void **fsdata)
15356 +{ AuUnsupport(); return 0; }
15357 +static int aufs_write_end(struct file *file, struct address_space *mapping,
15358 + loff_t pos, unsigned len, unsigned copied,
15359 + struct page *page, void *fsdata)
15360 +{ AuUnsupport(); return 0; }
15361 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15362 +{ AuUnsupport(); return 0; }
15364 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
15365 +{ AuUnsupport(); return true; }
15366 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
15367 +{ AuUnsupport(); }
15368 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
15369 +{ AuUnsupport(); return true; }
15370 +#if 0 /* called by memory compaction regardless file */
15371 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
15372 + struct folio *src, enum migrate_mode mode)
15373 +{ AuUnsupport(); return 0; }
15375 +static int aufs_launder_folio(struct folio *folio)
15376 +{ AuUnsupport(); return 0; }
15377 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
15379 +{ AuUnsupport(); return true; }
15380 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
15382 +{ AuUnsupport(); }
15383 +static int aufs_error_remove_page(struct address_space *mapping,
15384 + struct page *page)
15385 +{ AuUnsupport(); return 0; }
15386 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15388 +{ AuUnsupport(); return 0; }
15389 +static void aufs_swap_deactivate(struct file *file)
15390 +{ AuUnsupport(); }
15391 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
15392 +{ AuUnsupport(); return 0; }
15393 +#endif /* CONFIG_AUFS_DEBUG */
15395 +const struct address_space_operations aufs_aop = {
15396 + .read_folio = aufs_read_folio,
15397 + .direct_IO = aufs_direct_IO,
15398 +#ifdef CONFIG_AUFS_DEBUG
15399 + .writepage = aufs_writepage,
15400 + /* no writepages, because of writepage */
15401 + .dirty_folio = aufs_dirty_folio,
15402 + /* no readpages, because of readpage */
15403 + .write_begin = aufs_write_begin,
15404 + .write_end = aufs_write_end,
15405 + /* no bmap, no block device */
15406 + .invalidate_folio = aufs_invalidate_folio,
15407 + .release_folio = aufs_release_folio,
15408 + /* is fallback_migrate_page ok? */
15409 + /* .migrate_folio = aufs_migrate_folio, */
15410 + .launder_folio = aufs_launder_folio,
15411 + .is_partially_uptodate = aufs_is_partially_uptodate,
15412 + .is_dirty_writeback = aufs_is_dirty_writeback,
15413 + .error_remove_page = aufs_error_remove_page,
15414 + .swap_activate = aufs_swap_activate,
15415 + .swap_deactivate = aufs_swap_deactivate,
15416 + .swap_rw = aufs_swap_rw
15417 +#endif /* CONFIG_AUFS_DEBUG */
15419 diff --git a/fs/aufs/file.h b/fs/aufs/file.h
15420 new file mode 100644
15421 index 00000000000000..b16f31e364a837
15423 +++ b/fs/aufs/file.h
15425 +/* SPDX-License-Identifier: GPL-2.0 */
15427 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15429 + * This program is free software; you can redistribute it and/or modify
15430 + * it under the terms of the GNU General Public License as published by
15431 + * the Free Software Foundation; either version 2 of the License, or
15432 + * (at your option) any later version.
15434 + * This program is distributed in the hope that it will be useful,
15435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15437 + * GNU General Public License for more details.
15439 + * You should have received a copy of the GNU General Public License
15440 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
15444 + * file operations
15447 +#ifndef __AUFS_FILE_H__
15448 +#define __AUFS_FILE_H__
15452 +#include <linux/file.h>
15453 +#include <linux/fs.h>
15454 +#include <linux/mm_types.h>
15455 +#include <linux/poll.h>
15456 +#include "rwsem.h"
15460 + struct file *hf_file;
15461 + struct au_branch *hf_br;
15466 + aufs_bindex_t fd_bbot;
15467 + aufs_bindex_t fd_nent;
15468 + struct au_vdir *fd_vdir_cache;
15469 + struct au_hfile fd_hfile[];
15472 +static inline int au_fidir_sz(int nent)
15474 + AuDebugOn(nent < 0);
15475 + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15479 + atomic_t fi_generation;
15481 + struct au_rwsem fi_rwsem;
15482 + aufs_bindex_t fi_btop;
15484 + /* do not union them */
15485 + struct { /* for non-dir */
15486 + struct au_hfile fi_htop;
15487 + atomic_t fi_mmapped;
15489 + struct au_fidir *fi_hdir; /* for dir only */
15491 + struct hlist_bl_node fi_hlist;
15492 + struct file *fi_file; /* very ugly */
15493 + struct rcu_head rcu;
15494 +} ____cacheline_aligned_in_smp;
15496 +/* ---------------------------------------------------------------------- */
15499 +extern const struct address_space_operations aufs_aop;
15500 +unsigned int au_file_roflags(unsigned int flags);
15501 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15502 + struct file *file, int force_wr);
15503 +struct au_do_open_args {
15505 + int (*open)(struct file *file, int flags,
15506 + struct file *h_file);
15507 + struct au_fidir *fidir;
15508 + struct file *h_file;
15510 +int au_do_open(struct file *file, struct au_do_open_args *args);
15511 +int au_reopen_nondir(struct file *file);
15513 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15514 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15515 + int wlock, unsigned int fi_lsc);
15516 +int au_do_flush(struct file *file, fl_owner_t id,
15517 + int (*flush)(struct file *file, fl_owner_t id));
15520 +#ifdef CONFIG_AUFS_POLL
15521 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15524 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15526 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15528 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15529 + struct file *h_file);
15531 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15532 + aufs_bindex_t bindex, int force_wr)
15533 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15534 + struct file *h_file);
15538 +extern const struct file_operations aufs_file_fop;
15539 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15540 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15541 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15544 +void au_hfput(struct au_hfile *hf, int execed);
15545 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15546 + struct file *h_file);
15548 +void au_update_figen(struct file *file);
15549 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15550 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15552 +void au_fi_init_once(void *_fi);
15553 +void au_finfo_fin(struct file *file);
15554 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15557 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15558 +#ifdef CONFIG_COMPAT
15559 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15560 + unsigned long arg);
15561 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15562 + unsigned long arg);
15565 +/* ---------------------------------------------------------------------- */
15567 +static inline struct au_finfo *au_fi(struct file *file)
15569 + return file->private_data;
15572 +/* ---------------------------------------------------------------------- */
15574 +#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15575 +#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15576 +#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15577 +#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15579 +#define fi_read_trylock_nested(f) \
15580 + au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15581 +#define fi_write_trylock_nested(f) \
15582 + au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15585 +#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15586 +#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15587 +#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15589 +/* lock subclass for finfo */
15595 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15597 + au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15600 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15602 + au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15606 + * fi_read_lock_1, fi_write_lock_1,
15607 + * fi_read_lock_2, fi_write_lock_2
15609 +#define AuReadLockFunc(name) \
15610 +static inline void fi_read_lock_##name(struct file *f) \
15611 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15613 +#define AuWriteLockFunc(name) \
15614 +static inline void fi_write_lock_##name(struct file *f) \
15615 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15617 +#define AuRWLockFuncs(name) \
15618 + AuReadLockFunc(name) \
15619 + AuWriteLockFunc(name)
15624 +#undef AuReadLockFunc
15625 +#undef AuWriteLockFunc
15626 +#undef AuRWLockFuncs
15628 +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15629 +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15630 +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15632 +/* ---------------------------------------------------------------------- */
15634 +/* todo: hard/soft set? */
15635 +static inline aufs_bindex_t au_fbtop(struct file *file)
15637 + FiMustAnyLock(file);
15638 + return au_fi(file)->fi_btop;
15641 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15643 + FiMustAnyLock(file);
15644 + AuDebugOn(!au_fi(file)->fi_hdir);
15645 + return au_fi(file)->fi_hdir->fd_bbot;
15648 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15650 + FiMustAnyLock(file);
15651 + AuDebugOn(!au_fi(file)->fi_hdir);
15652 + return au_fi(file)->fi_hdir->fd_vdir_cache;
15655 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15657 + FiMustWriteLock(file);
15658 + au_fi(file)->fi_btop = bindex;
15661 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15663 + FiMustWriteLock(file);
15664 + AuDebugOn(!au_fi(file)->fi_hdir);
15665 + au_fi(file)->fi_hdir->fd_bbot = bindex;
15668 +static inline void au_set_fvdir_cache(struct file *file,
15669 + struct au_vdir *vdir_cache)
15671 + FiMustWriteLock(file);
15672 + AuDebugOn(!au_fi(file)->fi_hdir);
15673 + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15676 +static inline struct file *au_hf_top(struct file *file)
15678 + FiMustAnyLock(file);
15679 + AuDebugOn(au_fi(file)->fi_hdir);
15680 + return au_fi(file)->fi_htop.hf_file;
15683 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15685 + FiMustAnyLock(file);
15686 + AuDebugOn(!au_fi(file)->fi_hdir);
15687 + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15690 +/* todo: memory barrier? */
15691 +static inline unsigned int au_figen(struct file *f)
15693 + return atomic_read(&au_fi(f)->fi_generation);
15696 +static inline void au_set_mmapped(struct file *f)
15698 + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15700 + pr_warn("fi_mmapped wrapped around\n");
15701 + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15705 +static inline void au_unset_mmapped(struct file *f)
15707 + atomic_dec(&au_fi(f)->fi_mmapped);
15710 +static inline int au_test_mmapped(struct file *f)
15712 + return atomic_read(&au_fi(f)->fi_mmapped);
15715 +/* customize vma->vm_file */
15717 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15718 + struct file *file)
15722 + f = vma->vm_file;
15724 + vma->vm_file = file;
15729 +#define AuDbgVmRegion(file, vma) do {} while (0)
15731 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15732 + struct file *file)
15734 + au_do_vm_file_reset(vma, file);
15737 +#define AuDbgVmRegion(file, vma) \
15738 + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15740 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15741 + struct file *file)
15745 + au_do_vm_file_reset(vma, file);
15746 + f = vma->vm_region->vm_file;
15748 + vma->vm_region->vm_file = file;
15751 +#endif /* CONFIG_MMU */
15753 +/* handle vma->vm_prfile */
15754 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15755 + struct file *file)
15758 + vma->vm_prfile = file;
15759 +#ifndef CONFIG_MMU
15761 + vma->vm_region->vm_prfile = file;
15765 +#endif /* __KERNEL__ */
15766 +#endif /* __AUFS_FILE_H__ */
15767 diff --git a/fs/aufs/finfo.c b/fs/aufs/finfo.c
15768 new file mode 100644
15769 index 00000000000000..7da7b286c006fc
15771 +++ b/fs/aufs/finfo.c
15773 +// SPDX-License-Identifier: GPL-2.0
15775 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15777 + * This program is free software; you can redistribute it and/or modify
15778 + * it under the terms of the GNU General Public License as published by
15779 + * the Free Software Foundation; either version 2 of the License, or
15780 + * (at your option) any later version.
15782 + * This program is distributed in the hope that it will be useful,
15783 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15784 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15785 + * GNU General Public License for more details.
15787 + * You should have received a copy of the GNU General Public License
15788 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
15792 + * file private data
15797 +void au_hfput(struct au_hfile *hf, int execed)
15800 + allow_write_access(hf->hf_file);
15801 + fput(hf->hf_file);
15802 + hf->hf_file = NULL;
15803 + au_lcnt_dec(&hf->hf_br->br_nfiles);
15804 + hf->hf_br = NULL;
15807 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15809 + struct au_finfo *finfo = au_fi(file);
15810 + struct au_hfile *hf;
15811 + struct au_fidir *fidir;
15813 + fidir = finfo->fi_hdir;
15815 + AuDebugOn(finfo->fi_btop != bindex);
15816 + hf = &finfo->fi_htop;
15818 + hf = fidir->fd_hfile + bindex;
15820 + if (hf && hf->hf_file)
15821 + au_hfput(hf, vfsub_file_execed(file));
15823 + FiMustWriteLock(file);
15824 + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15825 + hf->hf_file = val;
15826 + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15830 +void au_update_figen(struct file *file)
15832 + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15833 + /* smp_mb(); */ /* atomic_set */
15836 +/* ---------------------------------------------------------------------- */
15838 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15840 + struct au_fidir *fidir;
15843 + nbr = au_sbbot(sb) + 1;
15845 + nbr = 2; /* initial allocate for 2 branches */
15846 + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15848 + fidir->fd_bbot = -1;
15849 + fidir->fd_nent = nbr;
15855 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15858 + struct au_fidir *fidir, *p;
15860 + AuRwMustWriteLock(&finfo->fi_rwsem);
15861 + fidir = finfo->fi_hdir;
15862 + AuDebugOn(!fidir);
15865 + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15866 + GFP_NOFS, may_shrink);
15868 + p->fd_nent = nbr;
15869 + finfo->fi_hdir = p;
15876 +/* ---------------------------------------------------------------------- */
15878 +void au_finfo_fin(struct file *file)
15880 + struct au_finfo *finfo;
15882 + au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15884 + finfo = au_fi(file);
15885 + AuDebugOn(finfo->fi_hdir);
15886 + AuRwDestroy(&finfo->fi_rwsem);
15887 + au_cache_free_finfo(finfo);
15890 +void au_fi_init_once(void *_finfo)
15892 + struct au_finfo *finfo = _finfo;
15894 + au_rw_init(&finfo->fi_rwsem);
15897 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15900 + struct au_finfo *finfo;
15901 + struct dentry *dentry;
15904 + dentry = file->f_path.dentry;
15905 + finfo = au_cache_alloc_finfo();
15906 + if (unlikely(!finfo))
15910 + au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15911 + au_rw_write_lock(&finfo->fi_rwsem);
15912 + finfo->fi_btop = -1;
15913 + finfo->fi_hdir = fidir;
15914 + atomic_set(&finfo->fi_generation, au_digen(dentry));
15915 + /* smp_mb(); */ /* atomic_set */
15917 + file->private_data = finfo;
15922 diff --git a/fs/aufs/fsctx.c b/fs/aufs/fsctx.c
15923 new file mode 100644
15924 index 00000000000000..e14fe4d381c5c9
15926 +++ b/fs/aufs/fsctx.c
15928 +// SPDX-License-Identifier: GPL-2.0
15930 + * Copyright (C) 2022 Junjiro R. Okajima
15932 + * This program is free software; you can redistribute it and/or modify
15933 + * it under the terms of the GNU General Public License as published by
15934 + * the Free Software Foundation; either version 2 of the License, or
15935 + * (at your option) any later version.
15937 + * This program is distributed in the hope that it will be useful,
15938 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15939 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15940 + * GNU General Public License for more details.
15942 + * You should have received a copy of the GNU General Public License
15943 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
15947 + * fs context, aka new mount api
15950 +#include <linux/fs_context.h>
15953 +struct au_fsctx_opts {
15954 + aufs_bindex_t bindex;
15955 + unsigned char skipped;
15956 + struct au_opt *opt, *opt_tail;
15957 + struct super_block *sb;
15958 + struct au_sbinfo *sbinfo;
15959 + struct au_opts opts;
15962 +/* stop extra interpretation of errno in mount(8), and strange error messages */
15963 +static int cvt_err(int err)
15977 +static int au_fsctx_reconfigure(struct fs_context *fc)
15980 + unsigned int mntflags;
15981 + struct dentry *root;
15982 + struct super_block *sb;
15983 + struct inode *inode;
15984 + struct au_fsctx_opts *a = fc->fs_private;
15986 + AuDbg("fc %p\n", fc);
15990 + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15992 + di_write_lock_child(root);
15993 + err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
15994 + aufs_write_unlock(root);
15997 + inode = d_inode(root);
15998 + inode_lock(inode);
15999 + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16000 + if (unlikely(err))
16002 + di_write_lock_child(root);
16004 + /* au_opts_remount() may return an error */
16005 + err = au_opts_remount(sb, &a->opts);
16007 + if (au_ftest_opts(a->opts.flags, REFRESH))
16008 + au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16011 + if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16012 + mntflags = au_mntflags(sb);
16013 + do_dx = !!au_opt_test(mntflags, DIO);
16014 + au_dy_arefresh(do_dx);
16017 + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16018 + aufs_write_unlock(root);
16021 + inode_unlock(inode);
16022 + err = cvt_err(err);
16028 +/* ---------------------------------------------------------------------- */
16030 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16033 + struct au_fsctx_opts *a = fc->fs_private;
16034 + struct au_sbinfo *sbinfo = a->sbinfo;
16035 + struct dentry *root;
16036 + struct inode *inode;
16038 + sbinfo->si_sb = sb;
16039 + sb->s_fs_info = sbinfo;
16040 + kobject_get(&sbinfo->si_kobj);
16042 + __si_write_lock(sb);
16044 + au_sbilist_add(sb);
16046 + /* all timestamps always follow the ones on the branch */
16047 + sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16048 + sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16049 + sb->s_op = &aufs_sop;
16050 + sb->s_d_op = &aufs_dop;
16051 + sb->s_magic = AUFS_SUPER_MAGIC;
16052 + sb->s_maxbytes = 0;
16053 + sb->s_stack_depth = 1;
16054 + au_export_init(sb);
16055 + au_xattr_init(sb);
16057 + err = au_alloc_root(sb);
16058 + if (unlikely(err)) {
16059 + si_write_unlock(sb);
16062 + root = sb->s_root;
16063 + inode = d_inode(root);
16064 + ii_write_lock_parent(inode);
16065 + aufs_write_unlock(root);
16067 + /* lock vfs_inode first, then aufs. */
16068 + inode_lock(inode);
16069 + aufs_write_lock(root);
16070 + err = au_opts_mount(sb, &a->opts);
16072 + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16073 + sb->s_d_op = &aufs_dop_noreval;
16074 + /* infofc(fc, "%ps", sb->s_d_op); */
16075 + pr_info("%ps\n", sb->s_d_op);
16076 + au_refresh_dop(root, /*force_reval*/0);
16077 + sbinfo->si_iop_array = aufs_iop_nogetattr;
16078 + au_refresh_iop(inode, /*force_getattr*/0);
16080 + aufs_write_unlock(root);
16081 + inode_unlock(inode);
16083 + goto out; /* success */
16086 + sb->s_root = NULL;
16089 + if (unlikely(err))
16090 + kobject_put(&sbinfo->si_kobj);
16092 + err = cvt_err(err);
16097 +static int au_fsctx_get_tree(struct fs_context *fc)
16101 + AuDbg("fc %p\n", fc);
16102 + err = get_tree_nodev(fc, au_fsctx_fill_super);
16108 +/* ---------------------------------------------------------------------- */
16110 +static void au_fsctx_dump(struct au_opts *opts)
16112 +#ifdef CONFIG_AUFS_DEBUG
16113 + /* reduce stack space */
16115 + struct au_opt_add *add;
16116 + struct au_opt_del *del;
16117 + struct au_opt_mod *mod;
16118 + struct au_opt_xino *xino;
16119 + struct au_opt_xino_itrunc *xino_itrunc;
16120 + struct au_opt_wbr_create *create;
16122 + struct au_opt *opt;
16125 + while (opt->type != Opt_tail) {
16126 + switch (opt->type) {
16128 + u.add = &opt->add;
16129 + AuDbg("add {b%d, %s, 0x%x, %p}\n",
16130 + u.add->bindex, u.add->pathname, u.add->perm,
16131 + u.add->path.dentry);
16136 + u.del = &opt->del;
16137 + AuDbg("del {%s, %p}\n",
16138 + u.del->pathname, u.del->h_path.dentry);
16143 + u.mod = &opt->mod;
16144 + AuDbg("mod {%s, 0x%x, %p}\n",
16145 + u.mod->path, u.mod->perm, u.mod->h_root);
16148 + u.add = &opt->add;
16149 + AuDbg("append {b%d, %s, 0x%x, %p}\n",
16150 + u.add->bindex, u.add->pathname, u.add->perm,
16151 + u.add->path.dentry);
16153 + case Opt_prepend:
16154 + u.add = &opt->add;
16155 + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16156 + u.add->bindex, u.add->pathname, u.add->perm,
16157 + u.add->path.dentry);
16161 + AuDbg("dirwh %d\n", opt->dirwh);
16163 + case Opt_rdcache:
16164 + AuDbg("rdcache %d\n", opt->rdcache);
16167 + AuDbg("rdblk %d\n", opt->rdblk);
16170 + AuDbg("rdhash %u\n", opt->rdhash);
16174 + u.xino = &opt->xino;
16175 + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16178 +#define au_fsctx_TF(name) \
16179 + case Opt_##name: \
16183 + AuLabel(no##name); \
16186 + /* simple true/false flag */
16187 + au_fsctx_TF(trunc_xino);
16188 + au_fsctx_TF(trunc_xib);
16189 + au_fsctx_TF(dirperm1);
16190 + au_fsctx_TF(plink);
16191 + au_fsctx_TF(shwh);
16192 + au_fsctx_TF(dio);
16193 + au_fsctx_TF(warn_perm);
16194 + au_fsctx_TF(verbose);
16195 + au_fsctx_TF(sum);
16196 + au_fsctx_TF(dirren);
16197 + au_fsctx_TF(acl);
16198 +#undef au_fsctx_TF
16200 + case Opt_trunc_xino_path:
16202 + case Opt_itrunc_xino:
16203 + u.xino_itrunc = &opt->xino_itrunc;
16204 + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16210 + case Opt_list_plink:
16211 + AuLabel(list_plink);
16214 + AuDbg("udba %d, %s\n",
16215 + opt->udba, au_optstr_udba(opt->udba));
16217 + case Opt_diropq_a:
16218 + AuLabel(diropq_a);
16220 + case Opt_diropq_w:
16221 + AuLabel(diropq_w);
16226 + case Opt_wbr_create:
16227 + u.create = &opt->wbr_create;
16228 + AuDbg("create %d, %s\n", u.create->wbr_create,
16229 + au_optstr_wbr_create(u.create->wbr_create));
16230 + switch (u.create->wbr_create) {
16231 + case AuWbrCreate_MFSV:
16233 + case AuWbrCreate_PMFSV:
16234 + AuDbg("%d sec\n", u.create->mfs_second);
16236 + case AuWbrCreate_MFSRR:
16238 + case AuWbrCreate_TDMFS:
16239 + AuDbg("%llu watermark\n",
16240 + u.create->mfsrr_watermark);
16242 + case AuWbrCreate_MFSRRV:
16244 + case AuWbrCreate_TDMFSV:
16246 + case AuWbrCreate_PMFSRRV:
16247 + AuDbg("%llu watermark, %d sec\n",
16248 + u.create->mfsrr_watermark,
16249 + u.create->mfs_second);
16253 + case Opt_wbr_copyup:
16254 + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16255 + au_optstr_wbr_copyup(opt->wbr_copyup));
16257 + case Opt_fhsm_sec:
16258 + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16262 + AuDbg("type %d\n", opt->type);
16270 +/* ---------------------------------------------------------------------- */
16273 + * For conditionally compiled mount options.
16274 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16276 +#define au_ignore_flag(name, action) \
16277 + fsparam_flag(name, action), \
16278 + fsparam_flag("no" name, Opt_ignore_silent)
16280 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16281 + fsparam_string("br", Opt_br),
16283 + /* "add=%d:%s" or "ins=%d:%s" */
16284 + fsparam_string("add", Opt_add),
16285 + fsparam_string("ins", Opt_add),
16286 + fsparam_path("append", Opt_append),
16287 + fsparam_path("prepend", Opt_prepend),
16289 + fsparam_path("del", Opt_del),
16290 + /* fsparam_s32("idel", Opt_idel), */
16291 + fsparam_path("mod", Opt_mod),
16292 + /* fsparam_string("imod", Opt_imod), */
16294 + fsparam_s32("dirwh", Opt_dirwh),
16296 + fsparam_path("xino", Opt_xino),
16297 + fsparam_flag("noxino", Opt_noxino),
16298 + fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16299 + /* "trunc_xino_v=%d:%d" */
16300 + /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16301 + fsparam_path("trunc_xino", Opt_trunc_xino_path),
16302 + fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16303 + /* fsparam_path("zxino", Opt_zxino), */
16304 + fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16306 +#ifdef CONFIG_PROC_FS
16307 + fsparam_flag_no("plink", Opt_plink),
16309 + au_ignore_flag("plink", Opt_ignore),
16312 +#ifdef CONFIG_AUFS_DEBUG
16313 + fsparam_flag("list_plink", Opt_list_plink),
16316 + fsparam_string("udba", Opt_udba),
16318 + fsparam_flag_no("dio", Opt_dio),
16320 +#ifdef CONFIG_AUFS_DIRREN
16321 + fsparam_flag_no("dirren", Opt_dirren),
16323 + au_ignore_flag("dirren", Opt_ignore),
16326 +#ifdef CONFIG_AUFS_FHSM
16327 + fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16329 + fsparam_s32("fhsm_sec", Opt_ignore),
16332 + /* always | a | whiteouted | w */
16333 + fsparam_string("diropq", Opt_diropq),
16335 + fsparam_flag_no("warn_perm", Opt_warn_perm),
16337 +#ifdef CONFIG_AUFS_SHWH
16338 + fsparam_flag_no("shwh", Opt_shwh),
16340 + au_ignore_flag("shwh", Opt_err),
16343 + fsparam_flag_no("dirperm1", Opt_dirperm1),
16345 + fsparam_flag_no("verbose", Opt_verbose),
16346 + fsparam_flag("v", Opt_verbose),
16347 + fsparam_flag("quiet", Opt_noverbose),
16348 + fsparam_flag("q", Opt_noverbose),
16349 + /* user-space may handle this */
16350 + fsparam_flag("silent", Opt_noverbose),
16352 + fsparam_flag_no("sum", Opt_sum),
16353 + fsparam_flag("wsum", Opt_wsum),
16355 + fsparam_s32("rdcache", Opt_rdcache),
16356 + /* "def" or s32 */
16357 + fsparam_string("rdblk", Opt_rdblk),
16358 + /* "def" or s32 */
16359 + fsparam_string("rdhash", Opt_rdhash),
16361 + fsparam_string("create", Opt_wbr_create),
16362 + fsparam_string("create_policy", Opt_wbr_create),
16363 + fsparam_string("cpup", Opt_wbr_copyup),
16364 + fsparam_string("copyup", Opt_wbr_copyup),
16365 + fsparam_string("copyup_policy", Opt_wbr_copyup),
16367 + /* generic VFS flag */
16368 +#ifdef CONFIG_FS_POSIX_ACL
16369 + fsparam_flag_no("acl", Opt_acl),
16371 + au_ignore_flag("acl", Opt_ignore),
16374 + /* internal use for the scripts */
16375 + fsparam_string("si", Opt_ignore_silent),
16377 + /* obsoleted, keep them temporary */
16378 + fsparam_flag("nodlgt", Opt_ignore_silent),
16379 + fsparam_flag("clean_plink", Opt_ignore),
16380 + fsparam_string("dirs", Opt_br),
16381 + fsparam_u32("debug", Opt_ignore),
16382 + /* "whiteout" or "all" */
16383 + fsparam_string("delete", Opt_ignore),
16384 + fsparam_string("imap", Opt_ignore),
16386 + /* temporary workaround, due to old mount(8)? */
16387 + fsparam_flag("relatime", Opt_ignore_silent),
16392 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16393 + char *brspec, size_t speclen,
16394 + aufs_bindex_t bindex)
16399 + AuDbg("brspec %s\n", brspec);
16403 + speclen = strlen(brspec);
16404 + /* will be freed by au_fsctx_free() */
16405 + p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16406 + if (unlikely(!p)) {
16407 + errorfc(fc, "failed in %s", brspec);
16410 + err = au_opt_add(opt, p, fc->sb_flags, bindex);
16417 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16421 + struct au_fsctx_opts *a = fc->fs_private;
16422 + struct au_opt *opt = a->opt;
16423 + aufs_bindex_t bindex = a->bindex;
16425 + AuDbg("brspec %s\n", brspec);
16428 + while ((p = strsep(&brspec, ":")) && *p) {
16429 + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16431 + if (unlikely(err))
16435 + if (unlikely(opt > a->opt_tail)) {
16441 + opt->type = Opt_tail;
16444 + a->bindex = bindex;
16451 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16455 + struct au_fsctx_opts *a = fc->fs_private;
16456 + struct au_opt *opt = a->opt;
16459 + p = strchr(addspec, ':');
16460 + if (unlikely(!p)) {
16461 + errorfc(fc, "bad arg in %s", addspec);
16465 + err = kstrtoint(addspec, 0, &n);
16466 + if (unlikely(err)) {
16467 + errorfc(fc, "bad integer in %s", addspec);
16470 + AuDbg("n %d\n", n);
16471 + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16478 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16479 + struct fs_parameter *param)
16484 + /* will be freed by au_fsctx_free() */
16485 + del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16486 + if (unlikely(!del->pathname))
16488 + AuDbg("del %s\n", del->pathname);
16489 + err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16490 + if (unlikely(err))
16491 + errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16498 +#if 0 /* reserved for future use */
16499 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16500 + aufs_bindex_t bindex)
16503 + struct super_block *sb;
16504 + struct dentry *root;
16505 + struct au_fsctx_opts *a = fc->fs_private;
16511 + root = sb->s_root;
16512 + aufs_read_lock(root, AuLock_FLUSH);
16513 + if (bindex < 0 || au_sbbot(sb) < bindex) {
16514 + errorfc(fc, "out of bounds, %d", bindex);
16519 + del->h_path.dentry = dget(au_h_dptr(root, bindex));
16520 + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16523 + aufs_read_unlock(root, !AuLock_IR);
16529 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16530 + struct fs_parameter *param)
16533 + struct path path;
16537 + /* will be freed by au_fsctx_free() */
16538 + mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16539 + if (unlikely(!mod->path))
16543 + p = strchr(mod->path, '=');
16544 + if (unlikely(!p)) {
16545 + errorfc(fc, "no permission %s", mod->path);
16550 + err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16551 + if (unlikely(err)) {
16552 + errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16556 + mod->perm = au_br_perm_val(p);
16557 + AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16558 + mod->h_root = dget(path.dentry);
16566 +#if 0 /* reserved for future use */
16567 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16572 + struct super_block *sb;
16573 + struct dentry *root;
16574 + struct au_fsctx_opts *a = fc->fs_private;
16580 + p = strchr(ibrspec, ':');
16581 + if (unlikely(!p)) {
16582 + errorfc(fc, "no index, %s", ibrspec);
16586 + err = kstrtoint(ibrspec, 0, &n);
16587 + if (unlikely(err)) {
16588 + errorfc(fc, "bad integer in %s", ibrspec);
16591 + AuDbg("n %d\n", n);
16593 + root = sb->s_root;
16594 + aufs_read_lock(root, AuLock_FLUSH);
16595 + if (n < 0 || au_sbbot(sb) < n) {
16596 + errorfc(fc, "out of bounds, %d", bindex);
16601 + mod->perm = au_br_perm_val(p);
16602 + AuDbg("mod path %s, perm 0x%x, %s\n",
16603 + mod->path, mod->perm, p);
16604 + mod->h_root = dget(au_h_dptr(root, bindex));
16607 + aufs_read_unlock(root, !AuLock_IR);
16614 +static int au_fsctx_parse_xino(struct fs_context *fc,
16615 + struct au_opt_xino *xino,
16616 + struct fs_parameter *param)
16619 + struct au_fsctx_opts *a = fc->fs_private;
16622 + /* will be freed by au_opts_free() */
16623 + xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16624 + if (unlikely(!xino->path))
16626 + AuDbg("path %s\n", xino->path);
16628 + xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16630 + err = PTR_ERR(xino->file);
16631 + if (IS_ERR(xino->file)) {
16632 + xino->file = NULL;
16637 + if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16639 + errorfc(fc, "%s must be outside", xino->path);
16648 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16649 + struct au_opt_xino_itrunc *xino_itrunc,
16653 + aufs_bindex_t bbot, bindex;
16654 + struct path path;
16655 + struct dentry *root;
16656 + struct au_fsctx_opts *a = fc->fs_private;
16658 + AuDebugOn(!a->sb);
16660 + err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16661 + if (unlikely(err)) {
16662 + errorfc(fc, "lookup failed %s (%d)", pathname, err);
16666 + xino_itrunc->bindex = -1;
16667 + root = a->sb->s_root;
16668 + aufs_read_lock(root, AuLock_FLUSH);
16669 + bbot = au_sbbot(a->sb);
16670 + for (bindex = 0; bindex <= bbot; bindex++) {
16671 + if (au_h_dptr(root, bindex) == path.dentry) {
16672 + xino_itrunc->bindex = bindex;
16676 + aufs_read_unlock(root, !AuLock_IR);
16679 + if (unlikely(xino_itrunc->bindex < 0)) {
16681 + errorfc(fc, "no such branch %s", pathname);
16689 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16690 + struct au_opt_xino_itrunc *xino_itrunc,
16691 + unsigned int bindex)
16694 + aufs_bindex_t bbot;
16695 + struct super_block *sb;
16696 + struct au_fsctx_opts *a = fc->fs_private;
16702 + si_noflush_read_lock(sb);
16703 + bbot = au_sbbot(sb);
16704 + si_read_unlock(sb);
16705 + if (bindex <= bbot)
16706 + xino_itrunc->bindex = bindex;
16709 + errorfc(fc, "out of bounds, %u", bindex);
16716 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16719 + struct fs_parse_result result;
16720 + struct au_fsctx_opts *a = fc->fs_private;
16721 + struct au_opt *opt = a->opt;
16723 + AuDbg("fc %p, param {key %s, string %s}\n",
16724 + fc, param->key, param->string);
16725 + err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16726 + if (unlikely(err < 0))
16729 + AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16730 + token, result.negated, result.uint_64);
16736 + err = au_fsctx_parse_br(fc, param->string);
16739 + err = au_fsctx_parse_add(fc, param->string);
16742 + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16743 + /*dummy bindex*/1);
16745 + case Opt_prepend:
16746 + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16751 + err = au_fsctx_parse_del(fc, &opt->del, param);
16753 +#if 0 /* reserved for future use */
16760 + del->pathname = "(indexed)";
16761 + err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
16766 + err = au_fsctx_parse_mod(fc, &opt->mod, param);
16768 +#ifdef IMOD /* reserved for future use */
16775 + u.mod->path = "(indexed)";
16776 + err = au_opts_parse_imod(fc, &opt->mod, param->string);
16781 + err = au_fsctx_parse_xino(fc, &opt->xino, param);
16783 + case Opt_trunc_xino_path:
16785 + errorfc(fc, "no such branch %s", param->string);
16788 + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
16792 + case Opt_trunc_xino_v:
16798 + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
16802 + case Opt_itrunc_xino:
16804 + errorfc(fc, "out of bounds %s", param->string);
16807 + err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
16813 + opt->dirwh = result.int_32;
16816 + case Opt_rdcache:
16817 + if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
16818 + errorfc(fc, "rdcache must be smaller than %d",
16819 + AUFS_RDCACHE_MAX);
16823 + opt->rdcache = result.int_32;
16828 + opt->rdblk = AUFS_RDBLK_DEF;
16829 + if (!strcmp(param->string, "def"))
16832 + err = kstrtoint(param->string, 0, &result.int_32);
16833 + if (unlikely(err)) {
16834 + errorfc(fc, "bad value in %s", param->key);
16838 + if (unlikely(result.int_32 < 0
16839 + || result.int_32 > KMALLOC_MAX_SIZE)) {
16840 + errorfc(fc, "bad value in %s", param->key);
16843 + if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
16844 + errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
16848 + opt->rdblk = result.int_32;
16853 + opt->rdhash = AUFS_RDHASH_DEF;
16854 + if (!strcmp(param->string, "def"))
16857 + err = kstrtoint(param->string, 0, &result.int_32);
16858 + if (unlikely(err)) {
16859 + errorfc(fc, "bad value in %s", param->key);
16862 + /* how about zero? */
16863 + if (result.int_32 < 0
16864 + || result.int_32 * sizeof(struct hlist_head)
16865 + > KMALLOC_MAX_SIZE) {
16867 + errorfc(fc, "bad integer in %s", param->key);
16870 + opt->rdhash = result.int_32;
16875 + * As other options, fs/aufs/opts.c can handle these strings by
16876 + * match_token(). But "diropq=" is deprecated now and will
16877 + * never have other value. So simple strcmp() is enough here.
16879 + if (!strcmp(param->string, "a") ||
16880 + !strcmp(param->string, "always")) {
16882 + opt->type = Opt_diropq_a;
16883 + } else if (!strcmp(param->string, "w") ||
16884 + !strcmp(param->string, "whiteouted")) {
16886 + opt->type = Opt_diropq_w;
16888 + errorfc(fc, "unknown value %s", param->string);
16892 + opt->udba = au_udba_val(param->string);
16893 + if (opt->udba >= 0)
16896 + errorf(fc, "wrong value, %s", param->string);
16899 + case Opt_wbr_create:
16900 + opt->wbr_create.wbr_create
16901 + = au_wbr_create_val(param->string, &opt->wbr_create);
16902 + if (opt->wbr_create.wbr_create >= 0)
16905 + errorf(fc, "wrong value, %s", param->key);
16908 + case Opt_wbr_copyup:
16909 + opt->wbr_copyup = au_wbr_copyup_val(param->string);
16910 + if (opt->wbr_copyup >= 0)
16913 + errorfc(fc, "wrong value, %s", param->key);
16916 + case Opt_fhsm_sec:
16917 + if (unlikely(result.int_32 < 0)) {
16918 + errorfc(fc, "bad integer in %s\n", param->key);
16923 + opt->fhsm_second = result.int_32;
16925 + warnfc(fc, "ignored %s %s", param->key, param->string);
16928 + /* simple true/false flag */
16929 +#define au_fsctx_TF(name) \
16930 + case Opt_##name: \
16932 + opt->tf = !result.negated; \
16934 + au_fsctx_TF(trunc_xino);
16935 + au_fsctx_TF(trunc_xib);
16936 + au_fsctx_TF(dirperm1);
16937 + au_fsctx_TF(plink);
16938 + au_fsctx_TF(shwh);
16939 + au_fsctx_TF(dio);
16940 + au_fsctx_TF(warn_perm);
16941 + au_fsctx_TF(verbose);
16942 + au_fsctx_TF(sum);
16943 + au_fsctx_TF(dirren);
16944 + au_fsctx_TF(acl);
16945 +#undef au_fsctx_TF
16947 + case Opt_noverbose:
16949 + opt->type = Opt_verbose;
16955 + case Opt_list_plink:
16962 + warnfc(fc, "ignored %s", param->key);
16964 + case Opt_ignore_silent:
16973 + if (unlikely(err))
16981 + case Opt_noverbose:
16986 + opt->type = token;
16990 + if (unlikely(opt > a->opt_tail)) {
16994 + opt->type = Opt_tail;
17002 + * these options accept both 'name=val' and 'name:val' form.
17003 + * some accept optional '=' in its value.
17004 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17006 +static inline unsigned int is_colonopt(char *str)
17008 +#define do_test(name) \
17009 + if (!strncmp(str, name ":", sizeof(name))) \
17010 + return sizeof(name) - 1
17014 + do_test("append");
17015 + do_test("prepend");
17017 + /* do_test("idel"); */
17019 + /* do_test("imod"); */
17025 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17030 + struct au_fsctx_opts *a = fc->fs_private;
17033 + AuDbg("str %s\n", str);
17035 + u = is_colonopt(str);
17038 + str = strchr(str, ',');
17044 + AuDbg("str %s\n", str);
17046 + err = generic_parse_monolithic(fc, str);
17048 + au_fsctx_dump(&a->opts);
17053 +/* ---------------------------------------------------------------------- */
17055 +static void au_fsctx_opts_free(struct au_opts *opts)
17057 + struct au_opt *opt;
17060 + while (opt->type != Opt_tail) {
17061 + switch (opt->type) {
17066 + case Opt_prepend:
17067 + kfree(opt->add.pathname);
17068 + path_put(&opt->add.path);
17071 + kfree(opt->del.pathname);
17074 + path_put(&opt->del.h_path);
17077 + kfree(opt->mod.path);
17080 + dput(opt->mod.h_root);
17083 + kfree(opt->xino.path);
17084 + fput(opt->xino.file);
17091 +static void au_fsctx_free(struct fs_context *fc)
17093 + struct au_fsctx_opts *a = fc->fs_private;
17095 + /* fs_type=%p, root=%pD */
17096 + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17097 + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17099 + kobject_put(&a->sbinfo->si_kobj);
17100 + au_fsctx_opts_free(&a->opts);
17101 + free_page((unsigned long)a->opts.opt);
17105 +static const struct fs_context_operations au_fsctx_ops = {
17106 + .free = au_fsctx_free,
17107 + .parse_param = au_fsctx_parse_param,
17108 + .parse_monolithic = au_fsctx_parse_monolithic,
17109 + .get_tree = au_fsctx_get_tree,
17110 + .reconfigure = au_fsctx_reconfigure
17112 + * nfs4 requires ->dup()? No.
17113 + * I don't know what is this ->dup() for.
17117 +int aufs_fsctx_init(struct fs_context *fc)
17120 + struct au_fsctx_opts *a;
17122 + /* fs_type=%p, root=%pD */
17123 + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17124 + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17126 + /* they will be freed by au_fsctx_free() */
17128 + a = kzalloc(sizeof(*a), GFP_NOFS);
17129 + if (unlikely(!a))
17132 + a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17133 + if (unlikely(!a->opts.opt))
17135 + a->opt = a->opts.opt;
17136 + a->opt->type = Opt_tail;
17137 + a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17138 + a->opt_tail = a->opt + a->opts.max_opt - 1;
17139 + a->opts.sb_flags = fc->sb_flags;
17143 + AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17144 + a->opts.flags = AuOpts_REMOUNT;
17145 + a->sb = fc->root->d_sb;
17146 + a->sbinfo = au_sbi(a->sb);
17147 + kobject_get(&a->sbinfo->si_kobj);
17149 + a->sbinfo = au_si_alloc(a->sb);
17150 + AuDebugOn(!a->sbinfo);
17151 + err = PTR_ERR(a->sbinfo);
17152 + if (IS_ERR(a->sbinfo))
17154 + au_rw_write_unlock(&a->sbinfo->si_rwsem);
17158 + fc->fs_private = a;
17159 + fc->ops = &au_fsctx_ops;
17160 + goto out; /* success */
17163 + free_page((unsigned long)a->opts.opt);
17170 diff --git a/fs/aufs/fstype.h b/fs/aufs/fstype.h
17171 new file mode 100644
17172 index 00000000000000..24ff0d64c219bc
17174 +++ b/fs/aufs/fstype.h
17176 +/* SPDX-License-Identifier: GPL-2.0 */
17178 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17180 + * This program is free software; you can redistribute it and/or modify
17181 + * it under the terms of the GNU General Public License as published by
17182 + * the Free Software Foundation; either version 2 of the License, or
17183 + * (at your option) any later version.
17185 + * This program is distributed in the hope that it will be useful,
17186 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17187 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17188 + * GNU General Public License for more details.
17190 + * You should have received a copy of the GNU General Public License
17191 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
17195 + * judging filesystem type
17198 +#ifndef __AUFS_FSTYPE_H__
17199 +#define __AUFS_FSTYPE_H__
17203 +#include <linux/fs.h>
17204 +#include <linux/magic.h>
17205 +#include <linux/nfs_fs.h>
17206 +#include <linux/romfs_fs.h>
17208 +static inline int au_test_aufs(struct super_block *sb)
17210 + return sb->s_magic == AUFS_SUPER_MAGIC;
17213 +static inline const char *au_sbtype(struct super_block *sb)
17215 + return sb->s_type->name;
17218 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17220 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17221 + return sb->s_magic == ISOFS_SUPER_MAGIC;
17227 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17229 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17230 + return sb->s_magic == ROMFS_MAGIC;
17236 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17238 +#if IS_ENABLED(CONFIG_CRAMFS)
17239 + return sb->s_magic == CRAMFS_MAGIC;
17244 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17246 +#if IS_ENABLED(CONFIG_NFS_FS)
17247 + return sb->s_magic == NFS_SUPER_MAGIC;
17253 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17255 +#if IS_ENABLED(CONFIG_FUSE_FS)
17256 + return sb->s_magic == FUSE_SUPER_MAGIC;
17262 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17264 +#if IS_ENABLED(CONFIG_XFS_FS)
17265 + return sb->s_magic == XFS_SB_MAGIC;
17271 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17273 +#ifdef CONFIG_TMPFS
17274 + return sb->s_magic == TMPFS_MAGIC;
17280 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17282 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17283 + return !strcmp(au_sbtype(sb), "ecryptfs");
17289 +static inline int au_test_ramfs(struct super_block *sb)
17291 + return sb->s_magic == RAMFS_MAGIC;
17294 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17296 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17297 + return sb->s_magic == UBIFS_SUPER_MAGIC;
17303 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17305 +#ifdef CONFIG_PROC_FS
17306 + return sb->s_magic == PROC_SUPER_MAGIC;
17312 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17314 +#ifdef CONFIG_SYSFS
17315 + return sb->s_magic == SYSFS_MAGIC;
17321 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17323 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17324 + return sb->s_magic == CONFIGFS_MAGIC;
17330 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17332 +#if IS_ENABLED(CONFIG_MINIX_FS)
17333 + return sb->s_magic == MINIX3_SUPER_MAGIC
17334 + || sb->s_magic == MINIX2_SUPER_MAGIC
17335 + || sb->s_magic == MINIX2_SUPER_MAGIC2
17336 + || sb->s_magic == MINIX_SUPER_MAGIC
17337 + || sb->s_magic == MINIX_SUPER_MAGIC2;
17343 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17345 +#if IS_ENABLED(CONFIG_FAT_FS)
17346 + return sb->s_magic == MSDOS_SUPER_MAGIC;
17352 +static inline int au_test_msdos(struct super_block *sb)
17354 + return au_test_fat(sb);
17357 +static inline int au_test_vfat(struct super_block *sb)
17359 + return au_test_fat(sb);
17362 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17364 +#ifdef CONFIG_SECURITYFS
17365 + return sb->s_magic == SECURITYFS_MAGIC;
17371 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17373 +#if IS_ENABLED(CONFIG_SQUASHFS)
17374 + return sb->s_magic == SQUASHFS_MAGIC;
17380 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17382 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17383 + return sb->s_magic == BTRFS_SUPER_MAGIC;
17389 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17391 +#if IS_ENABLED(CONFIG_XENFS)
17392 + return sb->s_magic == XENFS_SUPER_MAGIC;
17398 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17400 +#ifdef CONFIG_DEBUG_FS
17401 + return sb->s_magic == DEBUGFS_MAGIC;
17407 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17409 +#if IS_ENABLED(CONFIG_NILFS)
17410 + return sb->s_magic == NILFS_SUPER_MAGIC;
17416 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17418 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17419 + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17425 +/* ---------------------------------------------------------------------- */
17427 + * they can't be an aufs branch.
17429 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17432 +#ifndef CONFIG_AUFS_BR_RAMFS
17433 + au_test_ramfs(sb) ||
17435 + au_test_procfs(sb)
17436 + || au_test_sysfs(sb)
17437 + || au_test_configfs(sb)
17438 + || au_test_debugfs(sb)
17439 + || au_test_securityfs(sb)
17440 + || au_test_xenfs(sb)
17441 + || au_test_ecryptfs(sb)
17442 + /* || !strcmp(au_sbtype(sb), "unionfs") */
17443 + || au_test_aufs(sb); /* will be supported in next version */
17446 +static inline int au_test_fs_remote(struct super_block *sb)
17448 + return !au_test_tmpfs(sb)
17449 +#ifdef CONFIG_AUFS_BR_RAMFS
17450 + && !au_test_ramfs(sb)
17452 + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17455 +/* ---------------------------------------------------------------------- */
17458 + * Note: these functions (below) are created after reading ->getattr() in all
17459 + * filesystems under linux/fs. it means we have to do so in every update...
17463 + * some filesystems require getattr to refresh the inode attributes before
17465 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17466 + * and leave the work for d_revalidate()
17468 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17470 + return au_test_nfs(sb)
17471 + || au_test_fuse(sb)
17472 + /* || au_test_btrfs(sb) */ /* untested */
17477 + * filesystems which don't maintain i_size or i_blocks.
17479 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17481 + return au_test_xfs(sb)
17482 + || au_test_btrfs(sb)
17483 + || au_test_ubifs(sb)
17484 + || au_test_hfsplus(sb) /* maintained, but incorrect */
17485 + /* || au_test_minix(sb) */ /* untested */
17490 + * filesystems which don't store the correct value in some of their inode
17493 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17495 + return au_test_fs_bad_iattr_size(sb)
17496 + || au_test_fat(sb)
17497 + || au_test_msdos(sb)
17498 + || au_test_vfat(sb);
17501 +/* they don't check i_nlink in link(2) */
17502 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17504 + return au_test_tmpfs(sb)
17505 +#ifdef CONFIG_AUFS_BR_RAMFS
17506 + || au_test_ramfs(sb)
17508 + || au_test_ubifs(sb)
17509 + || au_test_hfsplus(sb);
17513 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17515 +static inline int au_test_fs_notime(struct super_block *sb)
17517 + return au_test_nfs(sb)
17518 + || au_test_fuse(sb)
17519 + || au_test_ubifs(sb)
17523 +/* temporary support for i#1 in cramfs */
17524 +static inline int au_test_fs_unique_ino(struct inode *inode)
17526 + if (au_test_cramfs(inode->i_sb))
17527 + return inode->i_ino != 1;
17531 +/* ---------------------------------------------------------------------- */
17534 + * the filesystem where the xino files placed must support i/o after unlink and
17535 + * maintain i_size and i_blocks.
17537 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17539 + return au_test_fs_remote(sb)
17540 + || au_test_fs_bad_iattr_size(sb)
17541 + /* don't want unnecessary work for xino */
17542 + || au_test_aufs(sb)
17543 + || au_test_ecryptfs(sb)
17544 + || au_test_nilfs(sb);
17547 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17549 + return au_test_tmpfs(sb)
17550 + || au_test_ramfs(sb);
17554 + * test if the @sb is real-readonly.
17556 +static inline int au_test_fs_rr(struct super_block *sb)
17558 + return au_test_squashfs(sb)
17559 + || au_test_iso9660(sb)
17560 + || au_test_cramfs(sb)
17561 + || au_test_romfs(sb);
17565 + * test if the @inode is nfs with 'noacl' option
17566 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17568 +static inline int au_test_nfs_noacl(struct inode *inode)
17570 + return au_test_nfs(inode->i_sb)
17571 + /* && IS_POSIXACL(inode) */
17572 + && !nfs_server_capable(inode, NFS_CAP_ACLS);
17575 +#endif /* __KERNEL__ */
17576 +#endif /* __AUFS_FSTYPE_H__ */
17577 diff --git a/fs/aufs/hbl.h b/fs/aufs/hbl.h
17578 new file mode 100644
17579 index 00000000000000..7e1e66db0264ba
17581 +++ b/fs/aufs/hbl.h
17583 +/* SPDX-License-Identifier: GPL-2.0 */
17585 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17587 + * This program is free software; you can redistribute it and/or modify
17588 + * it under the terms of the GNU General Public License as published by
17589 + * the Free Software Foundation; either version 2 of the License, or
17590 + * (at your option) any later version.
17592 + * This program is distributed in the hope that it will be useful,
17593 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17594 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17595 + * GNU General Public License for more details.
17597 + * You should have received a copy of the GNU General Public License
17598 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
17602 + * helpers for hlist_bl.h
17605 +#ifndef __AUFS_HBL_H__
17606 +#define __AUFS_HBL_H__
17610 +#include <linux/list_bl.h>
17612 +static inline void au_hbl_add(struct hlist_bl_node *node,
17613 + struct hlist_bl_head *hbl)
17615 + hlist_bl_lock(hbl);
17616 + hlist_bl_add_head(node, hbl);
17617 + hlist_bl_unlock(hbl);
17620 +static inline void au_hbl_del(struct hlist_bl_node *node,
17621 + struct hlist_bl_head *hbl)
17623 + hlist_bl_lock(hbl);
17624 + hlist_bl_del(node);
17625 + hlist_bl_unlock(hbl);
17628 +#define au_hbl_for_each(pos, head) \
17629 + for (pos = hlist_bl_first(head); \
17633 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17635 + unsigned long cnt;
17636 + struct hlist_bl_node *pos;
17639 + hlist_bl_lock(hbl);
17640 + au_hbl_for_each(pos, hbl)
17642 + hlist_bl_unlock(hbl);
17646 +#endif /* __KERNEL__ */
17647 +#endif /* __AUFS_HBL_H__ */
17648 diff --git a/fs/aufs/hfsnotify.c b/fs/aufs/hfsnotify.c
17649 new file mode 100644
17650 index 00000000000000..caa900cea3a0c4
17652 +++ b/fs/aufs/hfsnotify.c
17654 +// SPDX-License-Identifier: GPL-2.0
17656 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17658 + * This program is free software; you can redistribute it and/or modify
17659 + * it under the terms of the GNU General Public License as published by
17660 + * the Free Software Foundation; either version 2 of the License, or
17661 + * (at your option) any later version.
17663 + * This program is distributed in the hope that it will be useful,
17664 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17665 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17666 + * GNU General Public License for more details.
17668 + * You should have received a copy of the GNU General Public License
17669 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
17673 + * fsnotify for the lower directories
17678 +/* FS_IN_IGNORED is unnecessary */
17679 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17680 + | FS_CREATE | FS_EVENT_ON_CHILD);
17681 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17682 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17684 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17686 + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17688 + /* AuDbg("here\n"); */
17689 + au_cache_free_hnotify(hn);
17690 + smp_mb__before_atomic(); /* for atomic64_dec */
17691 + if (atomic64_dec_and_test(&au_hfsn_ifree))
17692 + wake_up(&au_hfsn_wq);
17695 +static int au_hfsn_alloc(struct au_hinode *hinode)
17698 + struct au_hnotify *hn;
17699 + struct super_block *sb;
17700 + struct au_branch *br;
17701 + struct fsnotify_mark *mark;
17702 + aufs_bindex_t bindex;
17704 + hn = hinode->hi_notify;
17705 + sb = hn->hn_aufs_inode->i_sb;
17706 + bindex = au_br_index(sb, hinode->hi_id);
17707 + br = au_sbr(sb, bindex);
17708 + AuDebugOn(!br->br_hfsn);
17710 + mark = &hn->hn_mark;
17711 + fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17712 + mark->mask = AuHfsnMask;
17714 + * by udba rename or rmdir, aufs assign a new inode to the known
17715 + * h_inode, so specify 1 to allow dups.
17718 + err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17724 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17726 + struct fsnotify_mark *mark;
17727 + unsigned long long ull;
17728 + struct fsnotify_group *group;
17730 + ull = atomic64_inc_return(&au_hfsn_ifree);
17733 + mark = &hn->hn_mark;
17734 + spin_lock(&mark->lock);
17735 + group = mark->group;
17736 + fsnotify_get_group(group);
17737 + spin_unlock(&mark->lock);
17739 + fsnotify_destroy_mark(mark, group);
17740 + fsnotify_put_mark(mark);
17741 + fsnotify_put_group(group);
17744 + /* free hn by myself */
17748 +/* ---------------------------------------------------------------------- */
17750 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
17752 + struct fsnotify_mark *mark;
17754 + mark = &hinode->hi_notify->hn_mark;
17755 + spin_lock(&mark->lock);
17757 + AuDebugOn(mark->mask & AuHfsnMask);
17758 + mark->mask |= AuHfsnMask;
17760 + AuDebugOn(!(mark->mask & AuHfsnMask));
17761 + mark->mask &= ~AuHfsnMask;
17763 + spin_unlock(&mark->lock);
17764 + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
17767 +/* ---------------------------------------------------------------------- */
17769 +/* #define AuDbgHnotify */
17770 +#ifdef AuDbgHnotify
17771 +static char *au_hfsn_name(u32 mask)
17773 +#ifdef CONFIG_AUFS_DEBUG
17774 +#define test_ret(flag) \
17776 + if (mask & flag) \
17779 + test_ret(FS_ACCESS);
17780 + test_ret(FS_MODIFY);
17781 + test_ret(FS_ATTRIB);
17782 + test_ret(FS_CLOSE_WRITE);
17783 + test_ret(FS_CLOSE_NOWRITE);
17784 + test_ret(FS_OPEN);
17785 + test_ret(FS_MOVED_FROM);
17786 + test_ret(FS_MOVED_TO);
17787 + test_ret(FS_CREATE);
17788 + test_ret(FS_DELETE);
17789 + test_ret(FS_DELETE_SELF);
17790 + test_ret(FS_MOVE_SELF);
17791 + test_ret(FS_UNMOUNT);
17792 + test_ret(FS_Q_OVERFLOW);
17793 + test_ret(FS_IN_IGNORED);
17794 + test_ret(FS_ISDIR);
17795 + test_ret(FS_IN_ONESHOT);
17796 + test_ret(FS_EVENT_ON_CHILD);
17805 +/* ---------------------------------------------------------------------- */
17807 +static void au_hfsn_free_group(struct fsnotify_group *group)
17809 + struct au_br_hfsnotify *hfsn = group->private;
17811 + /* AuDbg("here\n"); */
17812 + au_kfree_try_rcu(hfsn);
17815 +static int au_hfsn_handle_event(struct fsnotify_group *group,
17816 + u32 mask, const void *data, int data_type,
17817 + struct inode *dir,
17818 + const struct qstr *file_name, u32 cookie,
17819 + struct fsnotify_iter_info *iter_info)
17822 + struct au_hnotify *hnotify;
17823 + struct inode *h_dir, *h_inode;
17824 + struct fsnotify_mark *inode_mark;
17826 + AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
17827 + || data_type == FSNOTIFY_EVENT_DENTRY));
17830 + /* if FS_UNMOUNT happens, there must be another bug */
17831 + AuDebugOn(mask & FS_UNMOUNT);
17832 + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
17837 +#ifdef AuDbgHnotify
17839 + if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
17840 + || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
17841 + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17842 + h_dir->i_ino, mask, au_hfsn_name(mask),
17843 + AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
17844 + /* WARN_ON(1); */
17849 + inode_mark = fsnotify_iter_inode_mark(iter_info);
17850 + AuDebugOn(!inode_mark);
17851 + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17852 + err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
17858 +static struct fsnotify_ops au_hfsn_ops = {
17859 + .handle_event = au_hfsn_handle_event,
17860 + .free_group_priv = au_hfsn_free_group,
17861 + .free_mark = au_hfsn_free_mark
17864 +/* ---------------------------------------------------------------------- */
17866 +static void au_hfsn_fin_br(struct au_branch *br)
17868 + struct au_br_hfsnotify *hfsn;
17870 + hfsn = br->br_hfsn;
17873 + fsnotify_put_group(hfsn->hfsn_group);
17878 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17881 + struct fsnotify_group *group;
17882 + struct au_br_hfsnotify *hfsn;
17885 + br->br_hfsn = NULL;
17886 + if (!au_br_hnotifyable(perm))
17890 + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17891 + if (unlikely(!hfsn))
17895 + group = fsnotify_alloc_group(&au_hfsn_ops,
17896 + /*flags - not for userspace*/0);
17897 + if (IS_ERR(group)) {
17898 + err = PTR_ERR(group);
17899 + pr_err("fsnotify_alloc_group() failed, %d\n", err);
17903 + group->private = hfsn;
17904 + hfsn->hfsn_group = group;
17905 + br->br_hfsn = hfsn;
17906 + goto out; /* success */
17909 + au_kfree_try_rcu(hfsn);
17914 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17919 + if (!br->br_hfsn)
17920 + err = au_hfsn_init_br(br, perm);
17925 +/* ---------------------------------------------------------------------- */
17927 +static void au_hfsn_fin(void)
17929 + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17930 + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17933 +const struct au_hnotify_op au_hnotify_op = {
17934 + .ctl = au_hfsn_ctl,
17935 + .alloc = au_hfsn_alloc,
17936 + .free = au_hfsn_free,
17938 + .fin = au_hfsn_fin,
17940 + .reset_br = au_hfsn_reset_br,
17941 + .fin_br = au_hfsn_fin_br,
17942 + .init_br = au_hfsn_init_br
17944 diff --git a/fs/aufs/hfsplus.c b/fs/aufs/hfsplus.c
17945 new file mode 100644
17946 index 00000000000000..c626ff2128c6b3
17948 +++ b/fs/aufs/hfsplus.c
17950 +// SPDX-License-Identifier: GPL-2.0
17952 + * Copyright (C) 2010-2022 Junjiro R. Okajima
17954 + * This program is free software; you can redistribute it and/or modify
17955 + * it under the terms of the GNU General Public License as published by
17956 + * the Free Software Foundation; either version 2 of the License, or
17957 + * (at your option) any later version.
17959 + * This program is distributed in the hope that it will be useful,
17960 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17961 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17962 + * GNU General Public License for more details.
17964 + * You should have received a copy of the GNU General Public License
17965 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
17969 + * special support for filesystems which acquires an inode mutex
17970 + * at final closing a file, eg, hfsplus.
17972 + * This trick is very simple and stupid, just to open the file before really
17973 + * necessary open to tell hfsplus that this is not the final closing.
17974 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17975 + * and au_h_open_post() after releasing it.
17980 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17983 + struct file *h_file;
17984 + struct dentry *h_dentry;
17986 + h_dentry = au_h_dptr(dentry, bindex);
17987 + AuDebugOn(!h_dentry);
17988 + AuDebugOn(d_is_negative(h_dentry));
17991 + if (au_test_hfsplus(h_dentry->d_sb)
17992 + && d_is_reg(h_dentry))
17993 + h_file = au_h_open(dentry, bindex,
17994 + O_RDONLY | O_NOATIME | O_LARGEFILE,
17995 + /*file*/NULL, force_wr);
17999 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18000 + struct file *h_file)
18002 + struct au_branch *br;
18006 + br = au_sbr(dentry->d_sb, bindex);
18007 + au_lcnt_dec(&br->br_nfiles);
18010 diff --git a/fs/aufs/hnotify.c b/fs/aufs/hnotify.c
18011 new file mode 100644
18012 index 00000000000000..8ca8c98d40a330
18014 +++ b/fs/aufs/hnotify.c
18016 +// SPDX-License-Identifier: GPL-2.0
18018 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18020 + * This program is free software; you can redistribute it and/or modify
18021 + * it under the terms of the GNU General Public License as published by
18022 + * the Free Software Foundation; either version 2 of the License, or
18023 + * (at your option) any later version.
18025 + * This program is distributed in the hope that it will be useful,
18026 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18027 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18028 + * GNU General Public License for more details.
18030 + * You should have received a copy of the GNU General Public License
18031 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
18035 + * abstraction to notify the direct changes on lower directories
18038 +/* #include <linux/iversion.h> */
18041 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18044 + struct au_hnotify *hn;
18047 + hn = au_cache_alloc_hnotify();
18049 + hn->hn_aufs_inode = inode;
18050 + hinode->hi_notify = hn;
18051 + err = au_hnotify_op.alloc(hinode);
18053 + if (unlikely(err)) {
18054 + hinode->hi_notify = NULL;
18055 + au_cache_free_hnotify(hn);
18057 + * The upper dir was removed by udba, but the same named
18058 + * dir left. In this case, aufs assigns a new inode
18059 + * number and set the monitor again.
18060 + * For the lower dir, the old monitor is still left.
18062 + if (err == -EEXIST)
18071 +void au_hn_free(struct au_hinode *hinode)
18073 + struct au_hnotify *hn;
18075 + hn = hinode->hi_notify;
18077 + hinode->hi_notify = NULL;
18078 + if (au_hnotify_op.free(hinode, hn))
18079 + au_cache_free_hnotify(hn);
18083 +/* ---------------------------------------------------------------------- */
18085 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18087 + if (hinode->hi_notify)
18088 + au_hnotify_op.ctl(hinode, do_set);
18091 +void au_hn_reset(struct inode *inode, unsigned int flags)
18093 + aufs_bindex_t bindex, bbot;
18094 + struct inode *hi;
18095 + struct dentry *iwhdentry;
18097 + bbot = au_ibbot(inode);
18098 + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18099 + hi = au_h_iptr(inode, bindex);
18103 + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18104 + iwhdentry = au_hi_wh(inode, bindex);
18108 + au_set_h_iptr(inode, bindex, NULL, 0);
18109 + au_set_h_iptr(inode, bindex, au_igrab(hi),
18110 + flags & ~AuHi_XINO);
18113 + /* inode_unlock(hi); */
18117 +/* ---------------------------------------------------------------------- */
18119 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18122 + aufs_bindex_t bindex, bbot, bfound, btop;
18123 + struct inode *h_i;
18126 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18127 + pr_warn("branch root dir was changed\n");
18132 + bbot = au_ibbot(inode);
18133 + btop = au_ibtop(inode);
18134 +#if 0 /* reserved for future use */
18135 + if (bindex == bbot) {
18136 + /* keep this ino in rename case */
18140 + for (bindex = btop; bindex <= bbot; bindex++)
18141 + if (au_h_iptr(inode, bindex) == h_inode) {
18148 + for (bindex = btop; bindex <= bbot; bindex++) {
18149 + h_i = au_h_iptr(inode, bindex);
18153 + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18154 + /* ignore this error */
18155 + /* bad action? */
18158 + /* children inode number will be broken */
18165 +static int hn_gen_tree(struct dentry *dentry)
18167 + int err, i, j, ndentry;
18168 + struct au_dcsub_pages dpages;
18169 + struct au_dpage *dpage;
18170 + struct dentry **dentries;
18172 + err = au_dpages_init(&dpages, GFP_NOFS);
18173 + if (unlikely(err))
18175 + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18176 + if (unlikely(err))
18179 + for (i = 0; i < dpages.ndpage; i++) {
18180 + dpage = dpages.dpages + i;
18181 + dentries = dpage->dentries;
18182 + ndentry = dpage->ndentry;
18183 + for (j = 0; j < ndentry; j++) {
18184 + struct dentry *d;
18191 + if (d_really_is_positive(d))
18192 + /* todo: reset children xino?
18193 + cached children only? */
18194 + au_iigen_dec(d_inode(d));
18199 + au_dpages_free(&dpages);
18205 + * return 0 if processed.
18207 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18208 + const unsigned int isdir)
18211 + struct dentry *d;
18212 + struct qstr *dname;
18215 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18216 + pr_warn("branch root dir was changed\n");
18222 + AuDebugOn(!name);
18223 + au_iigen_dec(inode);
18224 + spin_lock(&inode->i_lock);
18225 + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18226 + spin_lock(&d->d_lock);
18227 + dname = &d->d_name;
18228 + if (dname->len != nlen
18229 + && memcmp(dname->name, name, nlen)) {
18230 + spin_unlock(&d->d_lock);
18235 + spin_unlock(&d->d_lock);
18238 + spin_unlock(&inode->i_lock);
18240 + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18241 + d = d_find_any_alias(inode);
18243 + au_iigen_dec(inode);
18247 + spin_lock(&d->d_lock);
18248 + dname = &d->d_name;
18249 + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18250 + spin_unlock(&d->d_lock);
18251 + err = hn_gen_tree(d);
18252 + spin_lock(&d->d_lock);
18254 + spin_unlock(&d->d_lock);
18263 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18267 + if (IS_ROOT(dentry)) {
18268 + pr_warn("branch root dir was changed\n");
18274 + au_digen_dec(dentry);
18275 + if (d_really_is_positive(dentry))
18276 + au_iigen_dec(d_inode(dentry));
18278 + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18279 + if (d_really_is_positive(dentry))
18280 + err = hn_gen_tree(dentry);
18287 +/* ---------------------------------------------------------------------- */
18289 +/* hnotify job flags */
18290 +#define AuHnJob_XINO0 1
18291 +#define AuHnJob_GEN (1 << 1)
18292 +#define AuHnJob_DIRENT (1 << 2)
18293 +#define AuHnJob_ISDIR (1 << 3)
18294 +#define AuHnJob_TRYXINO0 (1 << 4)
18295 +#define AuHnJob_MNTPNT (1 << 5)
18296 +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
18297 +#define au_fset_hnjob(flags, name) \
18298 + do { (flags) |= AuHnJob_##name; } while (0)
18299 +#define au_fclr_hnjob(flags, name) \
18300 + do { (flags) &= ~AuHnJob_##name; } while (0)
18308 +struct au_hnotify_args {
18309 + struct inode *h_dir, *dir, *h_child_inode;
18311 + unsigned int flags[AuHnLast];
18312 + unsigned int h_child_nlen;
18313 + char h_child_name[];
18316 +struct hn_job_args {
18317 + unsigned int flags;
18318 + struct inode *inode, *h_inode, *dir, *h_dir;
18319 + struct dentry *dentry;
18324 +static int hn_job(struct hn_job_args *a)
18326 + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18330 + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18331 + hn_xino(a->inode, a->h_inode); /* ignore this error */
18333 + if (au_ftest_hnjob(a->flags, TRYXINO0)
18336 + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18337 + if (!a->h_inode->i_nlink
18338 + && !(a->h_inode->i_state & I_LINKABLE))
18339 + hn_xino(a->inode, a->h_inode); /* ignore this error */
18340 + inode_unlock_shared(a->h_inode);
18343 + /* make the generation obsolete */
18344 + if (au_ftest_hnjob(a->flags, GEN)) {
18347 + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18349 + if (e && a->dentry)
18350 + hn_gen_by_name(a->dentry, isdir);
18351 + /* ignore this error */
18354 + /* make dir entries obsolete */
18355 + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18356 + struct au_vdir *vdir;
18358 + vdir = au_ivdir(a->inode);
18360 + vdir->vd_jiffy = 0;
18361 + /* IMustLock(a->inode); */
18362 + /* inode_inc_iversion(a->inode); */
18365 + /* can do nothing but warn */
18366 + if (au_ftest_hnjob(a->flags, MNTPNT)
18368 + && d_mountpoint(a->dentry))
18369 + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18374 +/* ---------------------------------------------------------------------- */
18376 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18377 + struct inode *dir)
18379 + struct dentry *dentry, *d, *parent;
18380 + struct qstr *dname;
18382 + parent = d_find_any_alias(dir);
18387 + spin_lock(&parent->d_lock);
18388 + list_for_each_entry(d, &parent->d_subdirs, d_child) {
18389 + /* AuDbg("%pd\n", d); */
18390 + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18391 + dname = &d->d_name;
18392 + if (dname->len != nlen || memcmp(dname->name, name, nlen))
18393 + goto cont_unlock;
18397 + goto cont_unlock;
18398 + if (au_dcount(d) > 0) {
18399 + dentry = dget_dlock(d);
18400 + spin_unlock(&d->d_lock);
18405 + spin_unlock(&d->d_lock);
18407 + spin_unlock(&parent->d_lock);
18411 + di_write_lock_child(dentry);
18416 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18417 + aufs_bindex_t bindex, ino_t h_ino)
18419 + struct inode *inode;
18424 + err = au_xino_read(sb, bindex, h_ino, &ino);
18426 + inode = ilookup(sb, ino);
18430 + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18431 + pr_warn("wrong root branch\n");
18437 + ii_write_lock_child(inode);
18443 +static void au_hn_bh(void *_args)
18445 + struct au_hnotify_args *a = _args;
18446 + struct super_block *sb;
18447 + aufs_bindex_t bindex, bbot, bfound;
18448 + unsigned char xino, try_iput;
18450 + struct inode *inode;
18452 + struct hn_job_args args;
18453 + struct dentry *dentry;
18454 + struct au_sbinfo *sbinfo;
18456 + AuDebugOn(!_args);
18457 + AuDebugOn(!a->h_dir);
18458 + AuDebugOn(!a->dir);
18459 + AuDebugOn(!a->mask);
18460 + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18461 + a->mask, a->dir->i_ino, a->h_dir->i_ino,
18462 + a->h_child_inode ? a->h_child_inode->i_ino : 0);
18467 + * do not lock a->dir->i_mutex here
18468 + * because of d_revalidate() may cause a deadlock.
18470 + sb = a->dir->i_sb;
18472 + sbinfo = au_sbi(sb);
18473 + AuDebugOn(!sbinfo);
18474 + si_write_lock(sb, AuLock_NOPLMW);
18476 + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18477 + switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18478 + case FS_MOVED_FROM:
18479 + case FS_MOVED_TO:
18480 + AuWarn1("DIRREN with UDBA may not work correctly "
18481 + "for the direct rename(2)\n");
18484 + ii_read_lock_parent(a->dir);
18486 + bbot = au_ibbot(a->dir);
18487 + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18488 + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18492 + ii_read_unlock(a->dir);
18493 + if (unlikely(bfound < 0))
18496 + xino = !!au_opt_test(au_mntflags(sb), XINO);
18498 + if (a->h_child_inode)
18499 + h_ino = a->h_child_inode->i_ino;
18501 + if (a->h_child_nlen
18502 + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18503 + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18504 + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18507 + if (dentry && d_really_is_positive(dentry))
18508 + inode = d_inode(dentry);
18509 + if (xino && !inode && h_ino
18510 + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18511 + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18512 + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18513 + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18517 + args.flags = a->flags[AuHn_CHILD];
18518 + args.dentry = dentry;
18519 + args.inode = inode;
18520 + args.h_inode = a->h_child_inode;
18521 + args.dir = a->dir;
18522 + args.h_dir = a->h_dir;
18523 + args.h_name = a->h_child_name;
18524 + args.h_nlen = a->h_child_nlen;
18525 + err = hn_job(&args);
18527 + if (au_di(dentry))
18528 + di_write_unlock(dentry);
18531 + if (inode && try_iput) {
18532 + ii_write_unlock(inode);
18536 + ii_write_lock_parent(a->dir);
18537 + args.flags = a->flags[AuHn_PARENT];
18538 + args.dentry = NULL;
18539 + args.inode = a->dir;
18540 + args.h_inode = a->h_dir;
18542 + args.h_dir = NULL;
18543 + args.h_name = NULL;
18545 + err = hn_job(&args);
18546 + ii_write_unlock(a->dir);
18549 + iput(a->h_child_inode);
18552 + si_write_unlock(sb);
18553 + au_nwt_done(&sbinfo->si_nowait);
18557 +/* ---------------------------------------------------------------------- */
18559 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18560 + const struct qstr *h_child_qstr, struct inode *h_child_inode)
18563 + unsigned int flags[AuHnLast], f;
18564 + unsigned char isdir, isroot, wh;
18565 + struct inode *dir;
18566 + struct au_hnotify_args *args;
18567 + char *p, *h_child_name;
18570 + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18571 + dir = igrab(hnotify->hn_aufs_inode);
18575 + isroot = (dir->i_ino == AUFS_ROOT_INO);
18577 + h_child_name = (void *)h_child_qstr->name;
18578 + len = h_child_qstr->len;
18579 + if (h_child_name) {
18580 + if (len > AUFS_WH_PFX_LEN
18581 + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18582 + h_child_name += AUFS_WH_PFX_LEN;
18583 + len -= AUFS_WH_PFX_LEN;
18589 + if (h_child_inode)
18590 + isdir = !!S_ISDIR(h_child_inode->i_mode);
18591 + flags[AuHn_PARENT] = AuHnJob_ISDIR;
18592 + flags[AuHn_CHILD] = 0;
18594 + flags[AuHn_CHILD] = AuHnJob_ISDIR;
18595 + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18596 + au_fset_hnjob(flags[AuHn_CHILD], GEN);
18597 + switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18598 + case FS_MOVED_FROM:
18599 + case FS_MOVED_TO:
18600 + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18601 + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18604 + AuDebugOn(!h_child_name);
18609 + * aufs never be able to get this child inode.
18610 + * revalidation should be in d_revalidate()
18611 + * by checking i_nlink, i_generation or d_unhashed().
18613 + AuDebugOn(!h_child_name);
18614 + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18615 + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18623 + h_child_inode = NULL;
18626 + /* iput() and kfree() will be called in au_hnotify() */
18627 + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18628 + if (unlikely(!args)) {
18629 + AuErr1("no memory\n");
18633 + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18634 + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18635 + args->mask = mask;
18637 + args->h_dir = igrab(h_dir);
18638 + if (h_child_inode)
18639 + h_child_inode = igrab(h_child_inode); /* can be NULL */
18640 + args->h_child_inode = h_child_inode;
18641 + args->h_child_nlen = len;
18643 + p = (void *)args;
18644 + p += sizeof(*args);
18645 + memcpy(p, h_child_name, len);
18649 + /* NFS fires the event for silly-renamed one from kworker */
18651 + if (!dir->i_nlink
18652 + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18654 + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18655 + if (unlikely(err)) {
18656 + pr_err("wkq %d\n", err);
18657 + iput(args->h_child_inode);
18658 + iput(args->h_dir);
18660 + au_kfree_rcu(args);
18667 +/* ---------------------------------------------------------------------- */
18669 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18673 + AuDebugOn(!(udba & AuOptMask_UDBA));
18676 + if (au_hnotify_op.reset_br)
18677 + err = au_hnotify_op.reset_br(udba, br, perm);
18682 +int au_hnotify_init_br(struct au_branch *br, int perm)
18687 + if (au_hnotify_op.init_br)
18688 + err = au_hnotify_op.init_br(br, perm);
18693 +void au_hnotify_fin_br(struct au_branch *br)
18695 + if (au_hnotify_op.fin_br)
18696 + au_hnotify_op.fin_br(br);
18699 +static void au_hn_destroy_cache(void)
18701 + kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18702 + au_cache[AuCache_HNOTIFY] = NULL;
18705 +int __init au_hnotify_init(void)
18710 + au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18711 + if (au_cache[AuCache_HNOTIFY]) {
18713 + if (au_hnotify_op.init)
18714 + err = au_hnotify_op.init();
18715 + if (unlikely(err))
18716 + au_hn_destroy_cache();
18722 +void au_hnotify_fin(void)
18724 + if (au_hnotify_op.fin)
18725 + au_hnotify_op.fin();
18727 + /* cf. au_cache_fin() */
18728 + if (au_cache[AuCache_HNOTIFY])
18729 + au_hn_destroy_cache();
18731 diff --git a/fs/aufs/i_op.c b/fs/aufs/i_op.c
18732 new file mode 100644
18733 index 00000000000000..a5cc8743c2bca4
18735 +++ b/fs/aufs/i_op.c
18737 +// SPDX-License-Identifier: GPL-2.0
18739 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18741 + * This program is free software; you can redistribute it and/or modify
18742 + * it under the terms of the GNU General Public License as published by
18743 + * the Free Software Foundation; either version 2 of the License, or
18744 + * (at your option) any later version.
18746 + * This program is distributed in the hope that it will be useful,
18747 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18748 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18749 + * GNU General Public License for more details.
18751 + * You should have received a copy of the GNU General Public License
18752 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
18756 + * inode operations (except add/del/rename)
18759 +#include <linux/device_cgroup.h>
18760 +#include <linux/filelock.h>
18761 +#include <linux/fs_stack.h>
18762 +#include <linux/iversion.h>
18763 +#include <linux/security.h>
18766 +static int h_permission(struct inode *h_inode, int mask,
18767 + struct path *h_path, int brperm)
18770 + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18771 + struct mnt_idmap *h_idmap;
18774 + if (write_mask && IS_IMMUTABLE(h_inode))
18778 + if (((mask & MAY_EXEC)
18779 + && S_ISREG(h_inode->i_mode)
18780 + && (path_noexec(h_path)
18781 + || !(h_inode->i_mode & 0111))))
18785 + * - skip the lower fs test in the case of write to ro branch.
18786 + * - nfs dir permission write check is optimized, but a policy for
18787 + * link/rename requires a real check.
18788 + * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
18789 + * in this case, generic_permission() returns -EOPNOTSUPP.
18791 + h_idmap = mnt_idmap(h_path->mnt);
18792 + if ((write_mask && !au_br_writable(brperm))
18793 + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18794 + && write_mask && !(mask & MAY_READ))
18795 + || !h_inode->i_op->permission) {
18796 + /* AuLabel(generic_permission); */
18797 + /* AuDbg("get_inode_acl %ps\n",
18798 + h_inode->i_op->get_inode_acl); */
18799 + err = generic_permission(h_idmap, h_inode, mask);
18800 + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18801 + err = h_inode->i_op->permission(h_idmap, h_inode,
18805 + /* AuLabel(h_inode->permission); */
18806 + err = h_inode->i_op->permission(h_idmap, h_inode, mask);
18811 + err = devcgroup_inode_permission(h_inode, mask);
18813 + err = security_inode_permission(h_inode, mask);
18819 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
18823 + aufs_bindex_t bindex, bbot;
18824 + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18825 + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18826 + struct inode *h_inode;
18827 + struct super_block *sb;
18828 + struct au_branch *br;
18830 + /* todo: support rcu-walk? */
18831 + if (mask & MAY_NOT_BLOCK)
18834 + sb = inode->i_sb;
18835 + si_read_lock(sb, AuLock_FLUSH);
18836 + ii_read_lock_child(inode);
18837 +#if 0 /* reserved for future use */
18839 + * This test may be rather 'too much' since the test is essentially done
18840 + * in the aufs_lookup(). Theoretically it is possible that the inode
18841 + * generation doesn't match to the superblock's here. But it isn't a
18842 + * big deal I suppose.
18844 + err = au_iigen_test(inode, au_sigen(sb));
18845 + if (unlikely(err))
18851 + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18852 + err = au_busy_or_stale();
18853 + h_inode = au_h_iptr(inode, au_ibtop(inode));
18854 + if (unlikely(!h_inode
18855 + || (h_inode->i_mode & S_IFMT)
18856 + != (inode->i_mode & S_IFMT)))
18860 + bindex = au_ibtop(inode);
18861 + br = au_sbr(sb, bindex);
18862 + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
18865 + && !special_file(h_inode->i_mode)) {
18866 + /* test whether the upper writable branch exists */
18868 + for (; bindex >= 0; bindex--)
18869 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
18877 + /* non-write to dir */
18879 + bbot = au_ibbot(inode);
18880 + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
18881 + h_inode = au_h_iptr(inode, bindex);
18883 + err = au_busy_or_stale();
18884 + if (unlikely(!S_ISDIR(h_inode->i_mode)))
18887 + br = au_sbr(sb, bindex);
18888 + err = h_permission(h_inode, mask, &br->br_path,
18894 + ii_read_unlock(inode);
18895 + si_read_unlock(sb);
18899 +/* ---------------------------------------------------------------------- */
18901 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18902 + unsigned int flags)
18904 + struct dentry *ret, *parent;
18905 + struct inode *inode;
18906 + struct super_block *sb;
18907 + int err, npositive;
18911 + /* todo: support rcu-walk? */
18912 + ret = ERR_PTR(-ECHILD);
18913 + if (flags & LOOKUP_RCU)
18916 + ret = ERR_PTR(-ENAMETOOLONG);
18917 + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18921 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18922 + ret = ERR_PTR(err);
18923 + if (unlikely(err))
18926 + err = au_di_init(dentry);
18927 + ret = ERR_PTR(err);
18928 + if (unlikely(err))
18932 + npositive = 0; /* suppress a warning */
18933 + parent = dentry->d_parent; /* dir inode is locked */
18934 + di_read_lock_parent(parent, AuLock_IR);
18935 + err = au_alive_dir(parent);
18937 + err = au_digen_test(parent, au_sigen(sb));
18939 + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
18940 + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
18941 + AuLkup_ALLOW_NEG);
18944 + di_read_unlock(parent, AuLock_IR);
18945 + ret = ERR_PTR(err);
18946 + if (unlikely(err < 0))
18950 + inode = au_new_inode(dentry, /*must_new*/0);
18951 + if (IS_ERR(inode)) {
18952 + ret = (void *)inode;
18959 + atomic_inc(&inode->i_count);
18960 + ret = d_splice_alias(inode, dentry);
18961 +#if 0 /* reserved for future use */
18962 + if (unlikely(d_need_lookup(dentry))) {
18963 + spin_lock(&dentry->d_lock);
18964 + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18965 + spin_unlock(&dentry->d_lock);
18969 + if (!IS_ERR(ret)) {
18971 + if (ret && ret != dentry)
18972 + ii_write_unlock(inode);
18974 + ii_write_unlock(inode);
18981 + di_write_unlock(dentry);
18983 + si_read_unlock(sb);
18988 +/* ---------------------------------------------------------------------- */
18991 + * very dirty and complicated aufs ->atomic_open().
18992 + * aufs_atomic_open()
18993 + * + au_aopen_or_create()
18995 + * + vfsub_atomic_open()
18996 + * + branch fs ->atomic_open()
18997 + * may call the actual 'open' for h_file
18998 + * + inc br_nfiles only if opened
18999 + * + au_aopen_no_open() or au_aopen_do_open()
19001 + * au_aopen_do_open()
19002 + * + finish_open()
19003 + * + au_do_aopen()
19004 + * + au_do_open() the body of all 'open'
19005 + * + au_do_open_nondir()
19006 + * set the passed h_file
19008 + * au_aopen_no_open()
19009 + * + finish_no_open()
19012 +struct aopen_node {
19013 + struct hlist_bl_node hblist;
19014 + struct file *file, *h_file;
19017 +static int au_do_aopen(struct inode *inode, struct file *file)
19019 + struct hlist_bl_head *aopen;
19020 + struct hlist_bl_node *pos;
19021 + struct aopen_node *node;
19022 + struct au_do_open_args args = {
19024 + .open = au_do_open_nondir
19027 + aopen = &au_sbi(inode->i_sb)->si_aopen;
19028 + hlist_bl_lock(aopen);
19029 + hlist_bl_for_each_entry(node, pos, aopen, hblist)
19030 + if (node->file == file) {
19031 + args.h_file = node->h_file;
19034 + hlist_bl_unlock(aopen);
19035 + /* AuDebugOn(!args.h_file); */
19037 + return au_do_open(file, &args);
19040 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
19041 + struct aopen_node *aopen_node)
19044 + struct hlist_bl_head *aopen;
19047 + aopen = &au_sbi(dentry->d_sb)->si_aopen;
19048 + au_hbl_add(&aopen_node->hblist, aopen);
19049 + err = finish_open(file, dentry, au_do_aopen);
19050 + au_hbl_del(&aopen_node->hblist, aopen);
19051 + /* AuDbgFile(file); */
19052 + AuDbg("%pd%s%s\n", dentry,
19053 + (file->f_mode & FMODE_CREATED) ? " created" : "",
19054 + (file->f_mode & FMODE_OPENED) ? " opened" : "");
19060 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
19066 + err = finish_no_open(file, dentry);
19072 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
19073 + struct file *file, unsigned int open_flag,
19074 + umode_t create_mode)
19076 + int err, did_open;
19077 + unsigned int lkup_flags;
19078 + aufs_bindex_t bindex;
19079 + struct super_block *sb;
19080 + struct dentry *parent, *d;
19081 + struct vfsub_aopen_args args = {
19082 + .open_flag = open_flag,
19083 + .create_mode = create_mode
19085 + struct aopen_node aopen_node = {
19090 + AuDbg("open_flag 0%o\n", open_flag);
19091 + AuDbgDentry(dentry);
19094 + if (!au_di(dentry)) {
19095 + lkup_flags = LOOKUP_OPEN;
19096 + if (open_flag & O_CREAT)
19097 + lkup_flags |= LOOKUP_CREATE;
19098 + d = aufs_lookup(dir, dentry, lkup_flags);
19100 + err = PTR_ERR(d);
19105 + * obsoleted dentry found.
19106 + * another error will be returned later.
19112 + AuDbgDentry(dentry);
19115 + if (d_is_positive(dentry)
19116 + || d_unhashed(dentry)
19117 + || d_unlinked(dentry)
19118 + || !(open_flag & O_CREAT)) {
19119 + err = au_aopen_no_open(file, dentry);
19120 + goto out; /* success */
19123 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
19124 + if (unlikely(err))
19127 + sb = dentry->d_sb;
19128 + parent = dentry->d_parent; /* dir is locked */
19129 + di_write_lock_parent(parent);
19130 + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
19131 + if (unlikely(err < 0))
19134 + AuDbgDentry(dentry);
19135 + if (d_is_positive(dentry)) {
19136 + err = au_aopen_no_open(file, dentry);
19137 + goto out_parent; /* success */
19140 + args.file = alloc_empty_file(file->f_flags, current_cred());
19141 + err = PTR_ERR(args.file);
19142 + if (IS_ERR(args.file))
19145 + bindex = au_dbtop(dentry);
19146 + err = au_aopen_or_create(dir, dentry, &args);
19148 + AuDbgFile(args.file);
19149 + file->f_mode = args.file->f_mode & ~FMODE_OPENED;
19150 + did_open = !!(args.file->f_mode & FMODE_OPENED);
19153 + args.file = NULL;
19155 + di_write_unlock(parent);
19156 + di_write_unlock(dentry);
19157 + if (unlikely(err < 0)) {
19164 + err = au_aopen_no_open(file, dentry);
19166 + aopen_node.h_file = args.file;
19167 + err = au_aopen_do_open(file, dentry, &aopen_node);
19169 + if (unlikely(err < 0)) {
19173 + au_lcnt_dec(&args.br->br_nfiles);
19175 + goto out_sb; /* success */
19178 + di_write_unlock(parent);
19179 + di_write_unlock(dentry);
19181 + si_read_unlock(sb);
19189 +/* ---------------------------------------------------------------------- */
19191 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
19192 + const unsigned char add_entry, aufs_bindex_t bcpup,
19193 + aufs_bindex_t btop)
19196 + struct dentry *h_parent;
19197 + struct inode *h_dir;
19200 + IMustLock(d_inode(parent));
19202 + di_write_lock_parent(parent);
19205 + if (!au_h_dptr(parent, bcpup)) {
19206 + if (btop > bcpup)
19207 + err = au_cpup_dirs(dentry, bcpup);
19208 + else if (btop < bcpup)
19209 + err = au_cpdown_dirs(dentry, bcpup);
19213 + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
19214 + h_parent = au_h_dptr(parent, bcpup);
19215 + h_dir = d_inode(h_parent);
19216 + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
19217 + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
19218 + /* todo: no unlock here */
19219 + inode_unlock_shared(h_dir);
19221 + AuDbg("bcpup %d\n", bcpup);
19223 + if (d_really_is_negative(dentry))
19224 + au_set_h_dptr(dentry, btop, NULL);
19225 + au_update_dbrange(dentry, /*do_put_zero*/0);
19230 + di_write_unlock(parent);
19232 + err = bcpup; /* success */
19239 + * decide the branch and the parent dir where we will create a new entry.
19240 + * returns new bindex or an error.
19241 + * copyup the parent dir if needed.
19243 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19244 + struct au_wr_dir_args *args)
19247 + unsigned int flags;
19248 + aufs_bindex_t bcpup, btop, src_btop;
19249 + const unsigned char add_entry
19250 + = au_ftest_wrdir(args->flags, ADD_ENTRY)
19251 + | au_ftest_wrdir(args->flags, TMPFILE);
19252 + struct super_block *sb;
19253 + struct dentry *parent;
19254 + struct au_sbinfo *sbinfo;
19256 + sb = dentry->d_sb;
19257 + sbinfo = au_sbi(sb);
19258 + parent = dget_parent(dentry);
19259 + btop = au_dbtop(dentry);
19261 + if (args->force_btgt < 0) {
19262 + if (src_dentry) {
19263 + src_btop = au_dbtop(src_dentry);
19264 + if (src_btop < btop)
19265 + bcpup = src_btop;
19266 + } else if (add_entry) {
19268 + if (au_ftest_wrdir(args->flags, ISDIR))
19269 + au_fset_wbr(flags, DIR);
19270 + err = AuWbrCreate(sbinfo, dentry, flags);
19274 + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
19276 + err = AuWbrCopyup(sbinfo, dentry);
19278 + if (!IS_ROOT(dentry)) {
19279 + di_read_lock_parent(parent, !AuLock_IR);
19280 + err = AuWbrCopyup(sbinfo, dentry);
19281 + di_read_unlock(parent, !AuLock_IR);
19283 + err = AuWbrCopyup(sbinfo, dentry);
19286 + if (unlikely(err < 0))
19290 + bcpup = args->force_btgt;
19291 + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
19294 + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
19296 + if (bcpup == btop)
19297 + goto out; /* success */
19299 + /* copyup the new parent into the branch we process */
19300 + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
19302 + if (d_really_is_negative(dentry)) {
19303 + au_set_h_dptr(dentry, btop, NULL);
19304 + au_set_dbtop(dentry, bcpup);
19305 + au_set_dbbot(dentry, bcpup);
19307 + AuDebugOn(add_entry
19308 + && !au_ftest_wrdir(args->flags, TMPFILE)
19309 + && !au_h_dptr(dentry, bcpup));
19317 +/* ---------------------------------------------------------------------- */
19319 +void au_pin_hdir_unlock(struct au_pin *p)
19322 + au_hn_inode_unlock(p->hdir);
19325 +int au_pin_hdir_lock(struct au_pin *p)
19333 + /* even if an error happens later, keep this lock */
19334 + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
19337 + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
19342 + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
19343 + p->h_parent, p->br);
19349 +int au_pin_hdir_relock(struct au_pin *p)
19352 + struct inode *h_i;
19353 + struct dentry *h_d[] = {
19358 + err = au_pin_hdir_lock(p);
19359 + if (unlikely(err))
19362 + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
19365 + if (d_is_positive(h_d[i])) {
19366 + h_i = d_inode(h_d[i]);
19367 + err = !h_i->i_nlink;
19375 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
19377 + atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
19380 +void au_pin_hdir_acquire_nest(struct au_pin *p)
19383 + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
19384 + p->lsc_hi, 0, NULL, _RET_IP_);
19385 + au_pin_hdir_set_owner(p, current);
19389 +void au_pin_hdir_release(struct au_pin *p)
19392 + au_pin_hdir_set_owner(p, p->task);
19393 + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
19397 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
19399 + if (pin && pin->parent)
19400 + return au_h_dptr(pin->parent, pin->bindex);
19404 +void au_unpin(struct au_pin *p)
19407 + au_pin_hdir_unlock(p);
19408 + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
19409 + vfsub_mnt_drop_write(p->h_mnt);
19413 + if (!au_ftest_pin(p->flags, DI_LOCKED))
19414 + di_read_unlock(p->parent, AuLock_IR);
19415 + iput(p->hdir->hi_inode);
19417 + p->parent = NULL;
19420 + /* do not clear p->task */
19423 +int au_do_pin(struct au_pin *p)
19426 + struct super_block *sb;
19427 + struct inode *h_dir;
19430 + sb = p->dentry->d_sb;
19431 + p->br = au_sbr(sb, p->bindex);
19432 + if (IS_ROOT(p->dentry)) {
19433 + if (au_ftest_pin(p->flags, MNT_WRITE)) {
19434 + p->h_mnt = au_br_mnt(p->br);
19435 + err = vfsub_mnt_want_write(p->h_mnt);
19436 + if (unlikely(err)) {
19437 + au_fclr_pin(p->flags, MNT_WRITE);
19444 + p->h_dentry = NULL;
19445 + if (p->bindex <= au_dbbot(p->dentry))
19446 + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
19448 + p->parent = dget_parent(p->dentry);
19449 + if (!au_ftest_pin(p->flags, DI_LOCKED))
19450 + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
19453 + p->h_parent = au_h_dptr(p->parent, p->bindex);
19454 + p->hdir = au_hi(d_inode(p->parent), p->bindex);
19456 + h_dir = p->hdir->hi_inode;
19460 + * if DI_LOCKED is not set, then p->parent may be different
19461 + * and h_parent can be NULL.
19463 + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
19465 + if (!au_ftest_pin(p->flags, DI_LOCKED))
19466 + di_read_unlock(p->parent, AuLock_IR);
19468 + p->parent = NULL;
19472 + if (au_ftest_pin(p->flags, MNT_WRITE)) {
19473 + p->h_mnt = au_br_mnt(p->br);
19474 + err = vfsub_mnt_want_write(p->h_mnt);
19475 + if (unlikely(err)) {
19476 + au_fclr_pin(p->flags, MNT_WRITE);
19477 + if (!au_ftest_pin(p->flags, DI_LOCKED))
19478 + di_read_unlock(p->parent, AuLock_IR);
19480 + p->parent = NULL;
19486 + err = au_pin_hdir_lock(p);
19488 + goto out; /* success */
19493 + pr_err("err %d\n", err);
19494 + err = au_busy_or_stale();
19499 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
19500 + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19501 + unsigned int udba, unsigned char flags)
19503 + p->dentry = dentry;
19505 + p->lsc_di = lsc_di;
19506 + p->lsc_hi = lsc_hi;
19507 + p->flags = flags;
19508 + p->bindex = bindex;
19510 + p->parent = NULL;
19514 + p->h_dentry = NULL;
19515 + p->h_parent = NULL;
19517 + p->task = current;
19520 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19521 + unsigned int udba, unsigned char flags)
19523 + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19525 + return au_do_pin(pin);
19528 +/* ---------------------------------------------------------------------- */
19531 + * ->setattr() and ->getattr() are called in various cases.
19532 + * chmod, stat: dentry is revalidated.
19533 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19535 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
19537 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
19538 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
19541 + struct dentry *parent;
19544 + if (au_digen_test(dentry, sigen)) {
19545 + parent = dget_parent(dentry);
19546 + di_read_lock_parent(parent, AuLock_IR);
19547 + err = au_refresh_dentry(dentry, parent);
19548 + di_read_unlock(parent, AuLock_IR);
19556 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19557 + struct au_icpup_args *a)
19561 + aufs_bindex_t btop, ibtop;
19562 + struct dentry *hi_wh, *parent;
19563 + struct inode *inode;
19564 + struct au_wr_dir_args wr_dir_args = {
19565 + .force_btgt = -1,
19569 + if (d_is_dir(dentry))
19570 + au_fset_wrdir(wr_dir_args.flags, ISDIR);
19571 + /* plink or hi_wh() case */
19572 + btop = au_dbtop(dentry);
19573 + inode = d_inode(dentry);
19574 + ibtop = au_ibtop(inode);
19575 + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
19576 + wr_dir_args.force_btgt = ibtop;
19577 + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19578 + if (unlikely(err < 0))
19582 + au_fset_icpup(a->flags, DID_CPUP);
19585 + a->pin_flags = AuPin_MNT_WRITE;
19587 + if (!IS_ROOT(dentry)) {
19588 + au_fset_pin(a->pin_flags, DI_LOCKED);
19589 + parent = dget_parent(dentry);
19590 + di_write_lock_parent(parent);
19593 + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19594 + if (unlikely(err))
19598 + a->h_path.dentry = au_h_dptr(dentry, btop);
19599 + a->h_inode = d_inode(a->h_path.dentry);
19600 + if (ia && (ia->ia_valid & ATTR_SIZE)) {
19601 + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
19602 + if (ia->ia_size < i_size_read(a->h_inode))
19603 + sz = ia->ia_size;
19604 + inode_unlock_shared(a->h_inode);
19608 + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
19609 + hi_wh = au_hi_wh(inode, a->btgt);
19611 + struct au_cp_generic cpg = {
19612 + .dentry = dentry,
19618 + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
19619 + if (unlikely(err))
19621 + hi_wh = au_hi_wh(inode, a->btgt);
19622 + /* todo: revalidate hi_wh? */
19627 + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19628 + di_downgrade_lock(parent, AuLock_IR);
19632 + if (!au_ftest_icpup(a->flags, DID_CPUP))
19633 + goto out; /* success */
19635 + if (!d_unhashed(dentry)) {
19636 + struct au_cp_generic cpg = {
19637 + .dentry = dentry,
19642 + .flags = AuCpup_DTIME | AuCpup_HOPEN
19644 + err = au_sio_cpup_simple(&cpg);
19646 + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19647 + } else if (!hi_wh)
19648 + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19650 + a->h_path.dentry = hi_wh; /* do not dget here */
19653 + a->h_inode = d_inode(a->h_path.dentry);
19655 + goto out; /* success */
19656 + au_unpin(&a->pin);
19659 + di_write_unlock(parent);
19664 + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19668 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
19669 + struct iattr *ia)
19672 + struct inode *inode, *delegated;
19673 + struct super_block *sb;
19674 + struct file *file;
19675 + struct au_icpup_args *a;
19676 + struct mnt_idmap *h_idmap;
19678 + inode = d_inode(dentry);
19679 + IMustLock(inode);
19681 + err = setattr_prepare(idmap, dentry, ia);
19682 + if (unlikely(err))
19686 + a = kzalloc(sizeof(*a), GFP_NOFS);
19687 + if (unlikely(!a))
19690 + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19691 + ia->ia_valid &= ~ATTR_MODE;
19694 + sb = dentry->d_sb;
19695 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19696 + if (unlikely(err))
19699 + if (ia->ia_valid & ATTR_FILE) {
19700 + /* currently ftruncate(2) only */
19701 + AuDebugOn(!d_is_reg(dentry));
19702 + file = ia->ia_file;
19703 + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
19705 + if (unlikely(err))
19707 + ia->ia_file = au_hf_top(file);
19708 + a->udba = AuOpt_UDBA_NONE;
19710 + /* fchmod() doesn't pass ia_file */
19711 + a->udba = au_opt_udba(sb);
19712 + di_write_lock_child(dentry);
19713 + /* no d_unlinked(), to set UDBA_NONE for root */
19714 + if (d_unhashed(dentry))
19715 + a->udba = AuOpt_UDBA_NONE;
19716 + if (a->udba != AuOpt_UDBA_NONE) {
19717 + AuDebugOn(IS_ROOT(dentry));
19718 + err = au_reval_for_attr(dentry, au_sigen(sb));
19719 + if (unlikely(err))
19724 + err = au_pin_and_icpup(dentry, ia, a);
19725 + if (unlikely(err < 0))
19727 + if (au_ftest_icpup(a->flags, DID_CPUP)) {
19728 + ia->ia_file = NULL;
19729 + ia->ia_valid &= ~ATTR_FILE;
19732 + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19733 + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19734 + == (ATTR_MODE | ATTR_CTIME)) {
19735 + err = security_path_chmod(&a->h_path, ia->ia_mode);
19736 + if (unlikely(err))
19738 + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19739 + && (ia->ia_valid & ATTR_CTIME)) {
19740 + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19741 + if (unlikely(err))
19745 + if (ia->ia_valid & ATTR_SIZE) {
19748 + if (ia->ia_size < i_size_read(inode))
19750 + truncate_setsize(inode, ia->ia_size);
19753 + if (ia->ia_valid & ATTR_FILE)
19755 + inode_unlock(a->h_inode);
19756 + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19757 + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19759 + delegated = NULL;
19761 + err = vfsub_notify_change(&a->h_path, ia, &delegated);
19763 + err = break_deleg_wait(&delegated);
19771 + * regardless aufs 'acl' option setting.
19772 + * why don't all acl-aware fs call this func from their ->setattr()?
19774 + if (!err && (ia->ia_valid & ATTR_MODE)) {
19775 + h_idmap = mnt_idmap(a->h_path.mnt);
19776 + err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
19779 + au_cpup_attr_changeable(inode);
19782 + inode_unlock(a->h_inode);
19783 + au_unpin(&a->pin);
19784 + if (unlikely(err))
19785 + au_update_dbtop(dentry);
19787 + di_write_unlock(dentry);
19789 + fi_write_unlock(file);
19790 + ia->ia_file = file;
19791 + ia->ia_valid |= ATTR_FILE;
19794 + si_read_unlock(sb);
19802 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19803 +static int au_h_path_to_set_attr(struct dentry *dentry,
19804 + struct au_icpup_args *a, struct path *h_path)
19807 + struct super_block *sb;
19809 + sb = dentry->d_sb;
19810 + a->udba = au_opt_udba(sb);
19811 + /* no d_unlinked(), to set UDBA_NONE for root */
19812 + if (d_unhashed(dentry))
19813 + a->udba = AuOpt_UDBA_NONE;
19814 + if (a->udba != AuOpt_UDBA_NONE) {
19815 + AuDebugOn(IS_ROOT(dentry));
19816 + err = au_reval_for_attr(dentry, au_sigen(sb));
19817 + if (unlikely(err))
19820 + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19821 + if (unlikely(err < 0))
19824 + h_path->dentry = a->h_path.dentry;
19825 + h_path->mnt = au_sbr_mnt(sb, a->btgt);
19831 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19832 + struct au_sxattr *arg)
19835 + struct path h_path;
19836 + struct super_block *sb;
19837 + struct au_icpup_args *a;
19838 + struct inode *h_inode;
19839 + struct mnt_idmap *h_idmap;
19841 + IMustLock(inode);
19844 + a = kzalloc(sizeof(*a), GFP_NOFS);
19845 + if (unlikely(!a))
19848 + sb = dentry->d_sb;
19849 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19850 + if (unlikely(err))
19853 + h_path.dentry = NULL; /* silence gcc */
19854 + di_write_lock_child(dentry);
19855 + err = au_h_path_to_set_attr(dentry, a, &h_path);
19856 + if (unlikely(err))
19858 + h_idmap = mnt_idmap(h_path.mnt);
19860 + inode_unlock(a->h_inode);
19861 + switch (arg->type) {
19862 + case AU_XATTR_SET:
19863 + AuDebugOn(d_is_negative(h_path.dentry));
19864 + err = vfsub_setxattr(h_idmap, h_path.dentry,
19865 + arg->u.set.name, arg->u.set.value,
19866 + arg->u.set.size, arg->u.set.flags);
19869 + err = -EOPNOTSUPP;
19870 + h_inode = d_inode(h_path.dentry);
19871 + if (h_inode->i_op->set_acl) {
19872 + /* this will call posix_acl_update_mode */
19873 + err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
19874 + arg->u.acl_set.acl,
19875 + arg->u.acl_set.type);
19880 + au_cpup_attr_timesizes(inode);
19882 + au_unpin(&a->pin);
19883 + if (unlikely(err))
19884 + au_update_dbtop(dentry);
19887 + di_write_unlock(dentry);
19888 + si_read_unlock(sb);
19897 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19898 + unsigned int nlink)
19902 + inode->i_mode = st->mode;
19903 + /* don't i_[ug]id_write() here */
19904 + inode->i_uid = st->uid;
19905 + inode->i_gid = st->gid;
19906 + inode->i_atime = st->atime;
19907 + inode->i_mtime = st->mtime;
19908 + inode_set_ctime_to_ts(inode, st->ctime);
19910 + au_cpup_attr_nlink(inode, /*force*/0);
19911 + if (S_ISDIR(inode->i_mode)) {
19912 + n = inode->i_nlink;
19915 + smp_mb(); /* for i_nlink */
19916 + /* 0 can happen */
19917 + set_nlink(inode, n);
19920 + spin_lock(&inode->i_lock);
19921 + inode->i_blocks = st->blocks;
19922 + i_size_write(inode, st->size);
19923 + spin_unlock(&inode->i_lock);
19927 + * common routine for aufs_getattr() and au_getxattr().
19928 + * returns zero or negative (an error).
19929 + * @dentry will be read-locked in success.
19931 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
19932 + struct path *h_path, int locked)
19935 + unsigned int mnt_flags, sigen;
19936 + unsigned char udba_none;
19937 + aufs_bindex_t bindex;
19938 + struct super_block *sb, *h_sb;
19940 + h_path->mnt = NULL;
19941 + h_path->dentry = NULL;
19944 + sb = dentry->d_sb;
19945 + mnt_flags = au_mntflags(sb);
19946 + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19948 + if (unlikely(locked))
19949 + goto body; /* skip locking dinfo */
19951 + /* support fstat(2) */
19952 + if (!d_unlinked(dentry) && !udba_none) {
19953 + sigen = au_sigen(sb);
19954 + err = au_digen_test(dentry, sigen);
19956 + di_read_lock_child(dentry, AuLock_IR);
19957 + err = au_dbrange_test(dentry);
19958 + if (unlikely(err)) {
19959 + di_read_unlock(dentry, AuLock_IR);
19963 + AuDebugOn(IS_ROOT(dentry));
19964 + di_write_lock_child(dentry);
19965 + err = au_dbrange_test(dentry);
19967 + err = au_reval_for_attr(dentry, sigen);
19969 + di_downgrade_lock(dentry, AuLock_IR);
19971 + di_write_unlock(dentry);
19976 + di_read_lock_child(dentry, AuLock_IR);
19980 + inode = d_inode(dentry);
19981 + if (unlikely(!inode))
19984 + bindex = au_ibtop(inode);
19985 + h_path->mnt = au_sbr_mnt(sb, bindex);
19986 + h_sb = h_path->mnt->mnt_sb;
19988 + && !au_test_fs_bad_iattr(h_sb)
19990 + goto out; /* success */
19992 + if (au_dbtop(dentry) == bindex)
19993 + h_path->dentry = au_h_dptr(dentry, bindex);
19994 + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19995 + h_path->dentry = au_plink_lkup(inode, bindex);
19996 + if (IS_ERR(h_path->dentry))
19997 + /* pretending success */
19998 + h_path->dentry = NULL;
20000 + dput(h_path->dentry);
20007 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
20008 + struct kstat *st, u32 request, unsigned int query)
20011 + unsigned char positive;
20012 + struct path h_path;
20013 + struct dentry *dentry;
20014 + struct inode *inode;
20015 + struct super_block *sb;
20017 + dentry = path->dentry;
20018 + inode = d_inode(dentry);
20019 + sb = dentry->d_sb;
20020 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20021 + if (unlikely(err))
20023 + err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
20025 + if (unlikely(err))
20027 + if (unlikely(!h_path.dentry))
20028 + /* illegally overlapped or something */
20029 + goto out_fill; /* pretending success */
20031 + positive = d_is_positive(h_path.dentry);
20033 + /* no vfsub version */
20034 + err = vfs_getattr(&h_path, st, request, query);
20037 + au_refresh_iattr(inode, st,
20038 + d_inode(h_path.dentry)->i_nlink);
20039 + goto out_fill; /* success */
20045 + generic_fillattr(idmap, request, inode, st);
20047 + di_read_unlock(dentry, AuLock_IR);
20049 + si_read_unlock(sb);
20055 +/* ---------------------------------------------------------------------- */
20057 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
20058 + struct delayed_call *done)
20061 + struct dentry *h_dentry;
20062 + struct inode *h_inode;
20064 + aufs_bindex_t bindex;
20066 + ret = NULL; /* suppress a warning */
20071 + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
20072 + if (unlikely(err))
20075 + err = au_d_hashed_positive(dentry);
20076 + if (unlikely(err))
20080 + inode = d_inode(dentry);
20081 + bindex = au_ibtop(inode);
20082 + h_inode = au_h_iptr(inode, bindex);
20083 + if (unlikely(!h_inode->i_op->get_link))
20088 + if (au_dbtop(dentry) <= bindex) {
20089 + h_dentry = au_h_dptr(dentry, bindex);
20094 + h_dentry = d_find_any_alias(h_inode);
20095 + if (IS_ERR(h_dentry)) {
20096 + err = PTR_ERR(h_dentry);
20100 + if (unlikely(!h_dentry))
20104 + AuDbg("%ps\n", h_inode->i_op->get_link);
20105 + AuDbgDentry(h_dentry);
20106 + ret = vfs_get_link(h_dentry, done);
20109 + err = PTR_ERR(ret);
20112 + aufs_read_unlock(dentry, AuLock_IR);
20114 + if (unlikely(err))
20115 + ret = ERR_PTR(err);
20116 + AuTraceErrPtr(ret);
20120 +/* ---------------------------------------------------------------------- */
20122 +static int au_is_special(struct inode *inode)
20124 + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
20127 +static int aufs_update_time(struct inode *inode, int flags)
20130 + aufs_bindex_t bindex;
20131 + struct super_block *sb;
20132 + struct inode *h_inode;
20133 + struct vfsmount *h_mnt;
20135 + sb = inode->i_sb;
20136 + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
20137 + "unexpected s_flags 0x%lx", sb->s_flags);
20139 + /* mmap_sem might be acquired already, cf. aufs_mmap() */
20141 + si_read_lock(sb, AuLock_FLUSH);
20142 + ii_write_lock_child(inode);
20145 + bindex = au_ibtop(inode);
20146 + h_inode = au_h_iptr(inode, bindex);
20147 + if (!au_test_ro(sb, bindex, inode)) {
20148 + h_mnt = au_sbr_mnt(sb, bindex);
20149 + err = vfsub_mnt_want_write(h_mnt);
20151 + err = vfsub_update_time(h_inode, flags);
20152 + vfsub_mnt_drop_write(h_mnt);
20154 + } else if (au_is_special(h_inode)) {
20156 + * Never copy-up here.
20157 + * These special files may already be opened and used for
20158 + * communicating. If we copied it up, then the communication
20159 + * would be corrupted.
20161 + AuWarn1("timestamps for i%lu are ignored "
20162 + "since it is on readonly branch (hi%lu).\n",
20163 + inode->i_ino, h_inode->i_ino);
20164 + } else if (flags & ~S_ATIME) {
20166 + AuIOErr1("unexpected flags 0x%x\n", flags);
20171 + au_cpup_attr_timesizes(inode);
20172 + ii_write_unlock(inode);
20173 + si_read_unlock(sb);
20176 + if (!err && (flags & S_VERSION))
20177 + inode_inc_iversion(inode);
20182 +/* ---------------------------------------------------------------------- */
20184 +/* no getattr version will be set by module.c:aufs_init() */
20185 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
20187 + [AuIop_SYMLINK] = {
20188 + .permission = aufs_permission,
20189 +#ifdef CONFIG_FS_POSIX_ACL
20190 + .get_inode_acl = aufs_get_inode_acl,
20191 + .get_acl = aufs_get_acl,
20192 + .set_acl = aufs_set_acl, /* unsupport for symlink? */
20195 + .setattr = aufs_setattr,
20196 + .getattr = aufs_getattr,
20198 +#ifdef CONFIG_AUFS_XATTR
20199 + .listxattr = aufs_listxattr,
20202 + .get_link = aufs_get_link
20204 + /* .update_time = aufs_update_time */
20207 + .create = aufs_create,
20208 + .lookup = aufs_lookup,
20209 + .link = aufs_link,
20210 + .unlink = aufs_unlink,
20211 + .symlink = aufs_symlink,
20212 + .mkdir = aufs_mkdir,
20213 + .rmdir = aufs_rmdir,
20214 + .mknod = aufs_mknod,
20215 + .rename = aufs_rename,
20217 + .permission = aufs_permission,
20218 +#ifdef CONFIG_FS_POSIX_ACL
20219 + .get_inode_acl = aufs_get_inode_acl,
20220 + .get_acl = aufs_get_acl,
20221 + .set_acl = aufs_set_acl,
20224 + .setattr = aufs_setattr,
20225 + .getattr = aufs_getattr,
20227 +#ifdef CONFIG_AUFS_XATTR
20228 + .listxattr = aufs_listxattr,
20231 + .update_time = aufs_update_time,
20232 + .atomic_open = aufs_atomic_open,
20233 + .tmpfile = aufs_tmpfile
20235 + [AuIop_OTHER] = {
20236 + .permission = aufs_permission,
20237 +#ifdef CONFIG_FS_POSIX_ACL
20238 + .get_inode_acl = aufs_get_inode_acl,
20239 + .get_acl = aufs_get_acl,
20240 + .set_acl = aufs_set_acl,
20243 + .setattr = aufs_setattr,
20244 + .getattr = aufs_getattr,
20246 +#ifdef CONFIG_AUFS_XATTR
20247 + .listxattr = aufs_listxattr,
20250 + .update_time = aufs_update_time
20253 diff --git a/fs/aufs/i_op_add.c b/fs/aufs/i_op_add.c
20254 new file mode 100644
20255 index 00000000000000..f238050b802f29
20257 +++ b/fs/aufs/i_op_add.c
20259 +// SPDX-License-Identifier: GPL-2.0
20261 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20263 + * This program is free software; you can redistribute it and/or modify
20264 + * it under the terms of the GNU General Public License as published by
20265 + * the Free Software Foundation; either version 2 of the License, or
20266 + * (at your option) any later version.
20268 + * This program is distributed in the hope that it will be useful,
20269 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20270 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20271 + * GNU General Public License for more details.
20273 + * You should have received a copy of the GNU General Public License
20274 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
20278 + * inode operations (add entry)
20281 +#include <linux/iversion.h>
20285 + * final procedure of adding a new entry, except link(2).
20286 + * remove whiteout, instantiate, copyup the parent dir's times and size
20287 + * and update version.
20288 + * if it failed, re-create the removed whiteout.
20290 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20291 + struct dentry *wh_dentry, struct dentry *dentry)
20294 + aufs_bindex_t bwh;
20295 + struct path h_path;
20296 + struct super_block *sb;
20297 + struct inode *inode, *h_dir;
20298 + struct dentry *wh;
20303 + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20304 + IMustLock(h_dir);
20305 + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20306 + bwh = au_dbwh(dentry);
20307 + h_path.dentry = wh_dentry;
20308 + h_path.mnt = au_sbr_mnt(sb, bindex);
20309 + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20311 + if (unlikely(err))
20315 + inode = au_new_inode(dentry, /*must_new*/1);
20316 + if (!IS_ERR(inode)) {
20317 + d_instantiate(dentry, inode);
20318 + dir = d_inode(dentry->d_parent); /* dir inode is locked */
20320 + au_dir_ts(dir, bindex);
20321 + inode_inc_iversion(dir);
20322 + au_fhsm_wrote(sb, bindex, /*force*/0);
20323 + return 0; /* success */
20326 + err = PTR_ERR(inode);
20331 + /* dir inode is locked */
20332 + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20333 + rerr = PTR_ERR(wh);
20334 + if (IS_ERR(wh)) {
20335 + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20336 + dentry, err, rerr);
20345 +static int au_d_may_add(struct dentry *dentry)
20350 + if (unlikely(d_unhashed(dentry)))
20352 + if (unlikely(d_really_is_positive(dentry)))
20358 + * simple tests for the adding inode operations.
20359 + * following the checks in vfs, plus the parent-child relationship.
20361 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20362 + struct dentry *h_parent, int isdir)
20366 + struct dentry *h_dentry;
20367 + struct inode *h_inode;
20369 + err = -ENAMETOOLONG;
20370 + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20373 + h_dentry = au_h_dptr(dentry, bindex);
20374 + if (d_really_is_negative(dentry)) {
20376 + if (unlikely(d_is_positive(h_dentry)))
20379 + /* rename(2) case */
20381 + if (unlikely(d_is_negative(h_dentry)))
20383 + h_inode = d_inode(h_dentry);
20384 + if (unlikely(!h_inode->i_nlink))
20387 + h_mode = h_inode->i_mode;
20390 + if (unlikely(S_ISDIR(h_mode)))
20392 + } else if (unlikely(!S_ISDIR(h_mode))) {
20399 + /* expected parent dir is locked */
20400 + if (unlikely(h_parent != h_dentry->d_parent))
20409 + * initial procedure of adding a new entry.
20410 + * prepare writable branch and the parent dir, lock it,
20411 + * and lookup whiteout for the new entry.
20413 +static struct dentry*
20414 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20415 + struct dentry *src_dentry, struct au_pin *pin,
20416 + struct au_wr_dir_args *wr_dir_args)
20418 + struct dentry *wh_dentry, *h_parent;
20419 + struct super_block *sb;
20420 + struct au_branch *br;
20422 + unsigned int udba;
20423 + aufs_bindex_t bcpup;
20425 + AuDbg("%pd\n", dentry);
20427 + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20429 + wh_dentry = ERR_PTR(err);
20430 + if (unlikely(err < 0))
20433 + sb = dentry->d_sb;
20434 + udba = au_opt_udba(sb);
20435 + err = au_pin(pin, dentry, bcpup, udba,
20436 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20437 + wh_dentry = ERR_PTR(err);
20438 + if (unlikely(err))
20441 + h_parent = au_pinned_h_parent(pin);
20442 + if (udba != AuOpt_UDBA_NONE
20443 + && au_dbtop(dentry) == bcpup)
20444 + err = au_may_add(dentry, bcpup, h_parent,
20445 + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20446 + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20447 + err = -ENAMETOOLONG;
20448 + wh_dentry = ERR_PTR(err);
20449 + if (unlikely(err))
20452 + br = au_sbr(sb, bcpup);
20454 + struct path tmp = {
20455 + .dentry = h_parent,
20456 + .mnt = au_br_mnt(br)
20458 + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20461 + wh_dentry = NULL;
20462 + if (bcpup != au_dbwh(dentry))
20463 + goto out; /* success */
20466 + * ENAMETOOLONG here means that if we allowed create such name, then it
20467 + * would not be able to removed in the future. So we don't allow such
20468 + * name here and we don't handle ENAMETOOLONG differently here.
20470 + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20473 + if (IS_ERR(wh_dentry))
20476 + return wh_dentry;
20479 +/* ---------------------------------------------------------------------- */
20481 +enum { Mknod, Symlink, Creat };
20482 +struct simple_arg {
20489 + struct vfsub_aopen_args *aopen;
20492 + const char *symname;
20501 +static int add_simple(struct inode *dir, struct dentry *dentry,
20502 + struct simple_arg *arg)
20505 + aufs_bindex_t btop;
20506 + unsigned char created;
20507 + const unsigned char try_aopen
20508 + = (arg->type == Creat && arg->u.c.try_aopen);
20509 + struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20510 + struct dentry *wh_dentry, *parent;
20511 + struct inode *h_dir;
20512 + struct super_block *sb;
20513 + struct au_branch *br;
20514 + /* to reduce stack size */
20516 + struct au_dtime dt;
20517 + struct au_pin pin;
20518 + struct path h_path;
20519 + struct au_wr_dir_args wr_dir_args;
20522 + AuDbg("%pd\n", dentry);
20526 + a = kmalloc(sizeof(*a), GFP_NOFS);
20527 + if (unlikely(!a))
20529 + a->wr_dir_args.force_btgt = -1;
20530 + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
20532 + parent = dentry->d_parent; /* dir inode is locked */
20533 + if (!try_aopen) {
20534 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20535 + if (unlikely(err))
20538 + err = au_d_may_add(dentry);
20539 + if (unlikely(err))
20542 + di_write_lock_parent(parent);
20543 + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20544 + &a->pin, &a->wr_dir_args);
20545 + err = PTR_ERR(wh_dentry);
20546 + if (IS_ERR(wh_dentry))
20549 + btop = au_dbtop(dentry);
20550 + sb = dentry->d_sb;
20551 + br = au_sbr(sb, btop);
20552 + a->h_path.dentry = au_h_dptr(dentry, btop);
20553 + a->h_path.mnt = au_br_mnt(br);
20554 + h_dir = au_pinned_h_dir(&a->pin);
20555 + switch (arg->type) {
20557 + if (!try_aopen || !h_dir->i_op->atomic_open) {
20558 + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
20559 + arg->u.c.want_excl);
20561 + if (!err && try_aopen)
20562 + aopen->file->f_mode |= FMODE_CREATED;
20565 + err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20566 + AuDbg("err %d\n", err);
20567 + AuDbgFile(aopen->file);
20568 + created = err >= 0
20569 + && !!(aopen->file->f_mode & FMODE_CREATED);
20573 + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
20577 + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20584 + if (unlikely(err < 0))
20587 + err = epilog(dir, btop, wh_dentry, dentry);
20589 + goto out_unpin; /* success */
20592 + if (created /* && d_is_positive(a->h_path.dentry) */) {
20593 + /* no delegation since it is just created */
20594 + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20597 + AuIOErr("%pd revert failure(%d, %d)\n",
20598 + dentry, err, rerr);
20601 + au_dtime_revert(&a->dt);
20603 + if (try_aopen && h_dir->i_op->atomic_open
20604 + && (aopen->file->f_mode & FMODE_OPENED))
20605 + /* aopen->file is still opened */
20606 + au_lcnt_dec(&aopen->br->br_nfiles);
20609 + au_unpin(&a->pin);
20613 + di_write_unlock(parent);
20615 + if (unlikely(err)) {
20616 + au_update_dbtop(dentry);
20620 + aufs_read_unlock(dentry, AuLock_DW);
20627 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20628 + struct dentry *dentry, umode_t mode, dev_t dev)
20630 + struct simple_arg arg = {
20637 + return add_simple(dir, dentry, &arg);
20640 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20641 + struct dentry *dentry, const char *symname)
20643 + struct simple_arg arg = {
20645 + .u.s.symname = symname
20647 + return add_simple(dir, dentry, &arg);
20650 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20651 + struct dentry *dentry, umode_t mode, bool want_excl)
20653 + struct simple_arg arg = {
20657 + .want_excl = want_excl
20660 + return add_simple(dir, dentry, &arg);
20663 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20664 + struct vfsub_aopen_args *aopen_args)
20666 + struct simple_arg arg = {
20669 + .mode = aopen_args->create_mode,
20670 + .want_excl = aopen_args->open_flag & O_EXCL,
20671 + .try_aopen = true,
20672 + .aopen = aopen_args
20675 + return add_simple(dir, dentry, &arg);
20678 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20679 + struct file *file, umode_t mode)
20682 + aufs_bindex_t bindex;
20683 + struct path h_ppath;
20684 + struct super_block *sb;
20685 + struct au_branch *br;
20686 + struct dentry *dentry, *parent, *h_parent, *h_dentry;
20687 + struct inode *h_dir, *inode;
20688 + struct vfsmount *h_mnt;
20689 + struct mnt_idmap *h_idmap;
20690 + struct file *h_file;
20691 + struct au_wr_dir_args wr_dir_args = {
20692 + .force_btgt = -1,
20693 + .flags = AuWrDir_TMPFILE
20696 + /* copy-up may happen */
20701 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20702 + if (unlikely(err))
20705 + dentry = file->f_path.dentry;
20706 + err = au_di_init(dentry);
20707 + if (unlikely(err))
20711 + parent = d_find_any_alias(dir);
20712 + AuDebugOn(!parent);
20713 + di_write_lock_parent(parent);
20714 + if (unlikely(d_inode(parent) != dir))
20717 + err = au_digen_test(parent, au_sigen(sb));
20718 + if (unlikely(err))
20721 + bindex = au_dbtop(parent);
20722 + au_set_dbtop(dentry, bindex);
20723 + au_set_dbbot(dentry, bindex);
20724 + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20726 + if (unlikely(err < 0))
20729 + err = -EOPNOTSUPP;
20730 + h_dir = au_h_iptr(dir, bindex);
20731 + if (unlikely(!h_dir->i_op->tmpfile))
20734 + br = au_sbr(sb, bindex);
20735 + h_mnt = au_br_mnt(br);
20736 + err = vfsub_mnt_want_write(h_mnt);
20737 + if (unlikely(err))
20740 + h_idmap = mnt_idmap(h_mnt);
20741 + h_parent = au_h_dptr(parent, bindex);
20742 + h_ppath.mnt = h_mnt;
20743 + h_ppath.dentry = h_parent;
20744 + h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
20746 + if (IS_ERR(h_file)) {
20747 + err = PTR_ERR(h_file);
20752 + h_dentry = h_file->f_path.dentry;
20753 + au_set_dbtop(dentry, bindex);
20754 + au_set_dbbot(dentry, bindex);
20755 + au_set_h_dptr(dentry, bindex, dget(h_dentry));
20756 + inode = au_new_inode(dentry, /*must_new*/1);
20757 + if (IS_ERR(inode)) {
20758 + err = PTR_ERR(inode);
20759 + au_set_h_dptr(dentry, bindex, NULL);
20760 + au_set_dbtop(dentry, -1);
20761 + au_set_dbbot(dentry, -1);
20765 + if (!inode->i_nlink)
20766 + set_nlink(inode, 1);
20767 + d_tmpfile(file, inode);
20768 + au_di(dentry)->di_tmpfile = 1;
20769 + get_file(h_file);
20770 + au_di(dentry)->di_htmpfile = h_file;
20772 + /* update without i_mutex */
20773 + if (au_ibtop(dir) == au_dbtop(dentry))
20774 + au_cpup_attr_timesizes(dir);
20779 + vfsub_mnt_drop_write(h_mnt);
20781 + di_write_unlock(parent);
20783 + di_write_unlock(dentry);
20788 + au_di(dentry)->di_htmpfile = NULL;
20789 + au_di_fin(dentry);
20790 + dentry->d_fsdata = NULL;
20792 + si_read_unlock(sb);
20793 + if (!err && h_file) {
20795 + err = finish_open_simple(file, err);
20797 + au_lcnt_inc(&br->br_nfiles);
20800 + au_di(dentry)->di_htmpfile = NULL;
20801 + au_di_fin(dentry);
20802 + dentry->d_fsdata = NULL;
20806 + inode_unlock(dir);
20811 +/* ---------------------------------------------------------------------- */
20813 +struct au_link_args {
20814 + aufs_bindex_t bdst, bsrc;
20815 + struct au_pin pin;
20816 + struct path h_path;
20817 + struct dentry *src_parent, *parent;
20820 +static int au_cpup_before_link(struct dentry *src_dentry,
20821 + struct au_link_args *a)
20824 + struct dentry *h_src_dentry;
20825 + struct au_cp_generic cpg = {
20826 + .dentry = src_dentry,
20831 + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20834 + di_read_lock_parent(a->src_parent, AuLock_IR);
20835 + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20836 + if (unlikely(err))
20839 + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20840 + err = au_pin(&a->pin, src_dentry, a->bdst,
20841 + au_opt_udba(src_dentry->d_sb),
20842 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20843 + if (unlikely(err))
20846 + err = au_sio_cpup_simple(&cpg);
20847 + au_unpin(&a->pin);
20850 + di_read_unlock(a->src_parent, AuLock_IR);
20854 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20855 + struct au_link_args *a)
20858 + unsigned char plink;
20859 + aufs_bindex_t bbot;
20860 + struct dentry *h_src_dentry;
20861 + struct inode *h_inode, *inode, *delegated;
20862 + struct super_block *sb;
20863 + struct file *h_file;
20867 + sb = src_dentry->d_sb;
20868 + inode = d_inode(src_dentry);
20869 + if (au_ibtop(inode) <= a->bdst)
20870 + h_inode = au_h_iptr(inode, a->bdst);
20871 + if (!h_inode || !h_inode->i_nlink) {
20872 + /* copyup src_dentry as the name of dentry. */
20873 + bbot = au_dbbot(dentry);
20874 + if (bbot < a->bsrc)
20875 + au_set_dbbot(dentry, a->bsrc);
20876 + au_set_h_dptr(dentry, a->bsrc,
20877 + dget(au_h_dptr(src_dentry, a->bsrc)));
20878 + dget(a->h_path.dentry);
20879 + au_set_h_dptr(dentry, a->bdst, NULL);
20880 + AuDbg("temporary d_inode...\n");
20881 + spin_lock(&dentry->d_lock);
20882 + dentry->d_inode = d_inode(src_dentry); /* tmp */
20883 + spin_unlock(&dentry->d_lock);
20884 + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20885 + if (IS_ERR(h_file))
20886 + err = PTR_ERR(h_file);
20888 + struct au_cp_generic cpg = {
20889 + .dentry = dentry,
20894 + .flags = AuCpup_KEEPLINO
20896 + err = au_sio_cpup_simple(&cpg);
20897 + au_h_open_post(dentry, a->bsrc, h_file);
20899 + dput(a->h_path.dentry);
20900 + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20902 + au_set_h_dptr(dentry, a->bdst,
20903 + a->h_path.dentry);
20905 + spin_lock(&dentry->d_lock);
20906 + dentry->d_inode = NULL; /* restore */
20907 + spin_unlock(&dentry->d_lock);
20908 + AuDbg("temporary d_inode...done\n");
20909 + au_set_h_dptr(dentry, a->bsrc, NULL);
20910 + au_set_dbbot(dentry, bbot);
20912 + /* the inode of src_dentry already exists on a.bdst branch */
20913 + h_src_dentry = d_find_alias(h_inode);
20914 + if (!h_src_dentry && au_plink_test(inode)) {
20916 + h_src_dentry = au_plink_lkup(inode, a->bdst);
20917 + err = PTR_ERR(h_src_dentry);
20918 + if (IS_ERR(h_src_dentry))
20921 + if (unlikely(d_is_negative(h_src_dentry))) {
20922 + dput(h_src_dentry);
20923 + h_src_dentry = NULL;
20927 + if (h_src_dentry) {
20928 + delegated = NULL;
20929 + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20930 + &a->h_path, &delegated);
20931 + if (unlikely(err == -EWOULDBLOCK)) {
20932 + pr_warn("cannot retry for NFSv4 delegation"
20933 + " for an internal link\n");
20936 + dput(h_src_dentry);
20938 + AuIOErr("no dentry found for hi%lu on b%d\n",
20939 + h_inode->i_ino, a->bdst);
20944 + if (!err && !plink)
20945 + au_plink_append(inode, a->bdst, a->h_path.dentry);
20952 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20953 + struct dentry *dentry)
20956 + struct au_dtime dt;
20957 + struct au_link_args *a;
20958 + struct dentry *wh_dentry, *h_src_dentry;
20959 + struct inode *inode, *delegated;
20960 + struct super_block *sb;
20961 + struct au_wr_dir_args wr_dir_args = {
20962 + /* .force_btgt = -1, */
20963 + .flags = AuWrDir_ADD_ENTRY
20967 + inode = d_inode(src_dentry);
20968 + IMustLock(inode);
20971 + a = kzalloc(sizeof(*a), GFP_NOFS);
20972 + if (unlikely(!a))
20975 + a->parent = dentry->d_parent; /* dir inode is locked */
20976 + err = aufs_read_and_write_lock2(dentry, src_dentry,
20977 + AuLock_NOPLM | AuLock_GEN);
20978 + if (unlikely(err))
20980 + err = au_d_linkable(src_dentry);
20981 + if (unlikely(err))
20983 + err = au_d_may_add(dentry);
20984 + if (unlikely(err))
20987 + a->src_parent = dget_parent(src_dentry);
20988 + wr_dir_args.force_btgt = au_ibtop(inode);
20990 + di_write_lock_parent(a->parent);
20991 + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20992 + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20994 + err = PTR_ERR(wh_dentry);
20995 + if (IS_ERR(wh_dentry))
20999 + sb = dentry->d_sb;
21000 + a->bdst = au_dbtop(dentry);
21001 + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21002 + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21003 + a->bsrc = au_ibtop(inode);
21004 + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21005 + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21006 + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21007 + if (!h_src_dentry) {
21008 + a->bsrc = au_dbtop(src_dentry);
21009 + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21010 + AuDebugOn(!h_src_dentry);
21011 + } else if (IS_ERR(h_src_dentry)) {
21012 + err = PTR_ERR(h_src_dentry);
21017 + * aufs doesn't touch the credential so
21018 + * security_dentry_create_files_as() is unnecessary.
21020 + if (au_opt_test(au_mntflags(sb), PLINK)) {
21021 + if (a->bdst < a->bsrc
21022 + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21023 + err = au_cpup_or_link(src_dentry, dentry, a);
21025 + delegated = NULL;
21026 + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21027 + &a->h_path, &delegated);
21028 + if (unlikely(err == -EWOULDBLOCK)) {
21029 + pr_warn("cannot retry for NFSv4 delegation"
21030 + " for an internal link\n");
21034 + dput(h_src_dentry);
21037 + * copyup src_dentry to the branch we process,
21038 + * and then link(2) to it.
21040 + dput(h_src_dentry);
21041 + if (a->bdst < a->bsrc
21042 + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21043 + au_unpin(&a->pin);
21044 + di_write_unlock(a->parent);
21045 + err = au_cpup_before_link(src_dentry, a);
21046 + di_write_lock_parent(a->parent);
21048 + err = au_pin(&a->pin, dentry, a->bdst,
21050 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21051 + if (unlikely(err))
21055 + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21057 + if (h_src_dentry && d_is_positive(h_src_dentry)) {
21058 + delegated = NULL;
21059 + err = vfsub_link(h_src_dentry,
21060 + au_pinned_h_dir(&a->pin),
21061 + &a->h_path, &delegated);
21062 + if (unlikely(err == -EWOULDBLOCK)) {
21063 + pr_warn("cannot retry"
21064 + " for NFSv4 delegation"
21065 + " for an internal link\n");
21071 + if (unlikely(err))
21075 + a->h_path.dentry = wh_dentry;
21076 + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21078 + if (unlikely(err))
21082 + au_dir_ts(dir, a->bdst);
21083 + inode_inc_iversion(dir);
21084 + inc_nlink(inode);
21085 + inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
21086 + d_instantiate(dentry, au_igrab(inode));
21087 + if (d_unhashed(a->h_path.dentry))
21088 + /* some filesystem calls d_drop() */
21090 + /* some filesystems consume an inode even hardlink */
21091 + au_fhsm_wrote(sb, a->bdst, /*force*/0);
21092 + goto out_unpin; /* success */
21095 + /* no delegation since it is just created */
21096 + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21097 + /*delegated*/NULL, /*force*/0);
21098 + if (unlikely(rerr)) {
21099 + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21102 + au_dtime_revert(&dt);
21104 + au_unpin(&a->pin);
21108 + di_write_unlock(a->parent);
21109 + dput(a->src_parent);
21111 + if (unlikely(err)) {
21112 + au_update_dbtop(dentry);
21115 + aufs_read_and_write_unlock2(dentry, src_dentry);
21123 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21124 + struct dentry *dentry, umode_t mode)
21127 + aufs_bindex_t bindex;
21128 + unsigned char diropq;
21129 + struct path h_path;
21130 + struct dentry *wh_dentry, *parent, *opq_dentry;
21131 + struct inode *h_inode;
21132 + struct super_block *sb;
21134 + struct au_pin pin;
21135 + struct au_dtime dt;
21136 + } *a; /* reduce the stack usage */
21137 + struct au_wr_dir_args wr_dir_args = {
21138 + .force_btgt = -1,
21139 + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21145 + a = kmalloc(sizeof(*a), GFP_NOFS);
21146 + if (unlikely(!a))
21149 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21150 + if (unlikely(err))
21152 + err = au_d_may_add(dentry);
21153 + if (unlikely(err))
21156 + parent = dentry->d_parent; /* dir inode is locked */
21157 + di_write_lock_parent(parent);
21158 + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21159 + &a->pin, &wr_dir_args);
21160 + err = PTR_ERR(wh_dentry);
21161 + if (IS_ERR(wh_dentry))
21164 + sb = dentry->d_sb;
21165 + bindex = au_dbtop(dentry);
21166 + h_path.dentry = au_h_dptr(dentry, bindex);
21167 + h_path.mnt = au_sbr_mnt(sb, bindex);
21168 + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21169 + if (unlikely(err))
21172 + /* make the dir opaque */
21174 + h_inode = d_inode(h_path.dentry);
21176 + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21177 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
21178 + opq_dentry = au_diropq_create(dentry, bindex);
21179 + inode_unlock(h_inode);
21180 + err = PTR_ERR(opq_dentry);
21181 + if (IS_ERR(opq_dentry))
21183 + dput(opq_dentry);
21187 + err = epilog(dir, bindex, wh_dentry, dentry);
21190 + goto out_unpin; /* success */
21195 + AuLabel(revert opq);
21196 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
21197 + rerr = au_diropq_remove(dentry, bindex);
21198 + inode_unlock(h_inode);
21200 + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21201 + dentry, err, rerr);
21207 + AuLabel(revert dir);
21208 + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21210 + AuIOErr("%pd reverting dir failed(%d, %d)\n",
21211 + dentry, err, rerr);
21214 + au_dtime_revert(&a->dt);
21216 + au_unpin(&a->pin);
21219 + di_write_unlock(parent);
21221 + if (unlikely(err)) {
21222 + au_update_dbtop(dentry);
21225 + aufs_read_unlock(dentry, AuLock_DW);
21231 diff --git a/fs/aufs/i_op_del.c b/fs/aufs/i_op_del.c
21232 new file mode 100644
21233 index 00000000000000..8d9631492b930a
21235 +++ b/fs/aufs/i_op_del.c
21237 +// SPDX-License-Identifier: GPL-2.0
21239 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21241 + * This program is free software; you can redistribute it and/or modify
21242 + * it under the terms of the GNU General Public License as published by
21243 + * the Free Software Foundation; either version 2 of the License, or
21244 + * (at your option) any later version.
21246 + * This program is distributed in the hope that it will be useful,
21247 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21248 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21249 + * GNU General Public License for more details.
21251 + * You should have received a copy of the GNU General Public License
21252 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
21256 + * inode operations (del entry)
21259 +#include <linux/iversion.h>
21263 + * decide if a new whiteout for @dentry is necessary or not.
21264 + * when it is necessary, prepare the parent dir for the upper branch whose
21265 + * branch index is @bcpup for creation. the actual creation of the whiteout will
21266 + * be done by caller.
21268 + * 0: wh is unnecessary
21269 + * plus: wh is necessary
21272 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21274 + int need_wh, err;
21275 + aufs_bindex_t btop;
21276 + struct super_block *sb;
21278 + sb = dentry->d_sb;
21279 + btop = au_dbtop(dentry);
21280 + if (*bcpup < 0) {
21282 + if (au_test_ro(sb, btop, d_inode(dentry))) {
21283 + err = AuWbrCopyup(au_sbi(sb), dentry);
21285 + if (unlikely(err < 0))
21289 + AuDebugOn(btop < *bcpup
21290 + || au_test_ro(sb, *bcpup, d_inode(dentry)));
21291 + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
21293 + if (*bcpup != btop) {
21294 + err = au_cpup_dirs(dentry, *bcpup);
21295 + if (unlikely(err))
21299 + struct au_dinfo *dinfo, *tmp;
21301 + need_wh = -ENOMEM;
21302 + dinfo = au_di(dentry);
21303 + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
21305 + au_di_cp(tmp, dinfo);
21306 + au_di_swap(tmp, dinfo);
21307 + /* returns the number of positive dentries */
21308 + need_wh = au_lkup_dentry(dentry, btop + 1,
21309 + /* AuLkup_IGNORE_PERM */ 0);
21310 + au_di_swap(tmp, dinfo);
21311 + au_rw_write_unlock(&tmp->di_rwsem);
21315 + AuDbg("need_wh %d\n", need_wh);
21323 + * simple tests for the del-entry operations.
21324 + * following the checks in vfs, plus the parent-child relationship.
21326 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
21327 + struct dentry *h_parent, int isdir)
21331 + struct dentry *h_dentry, *h_latest;
21332 + struct inode *h_inode;
21333 + struct path h_ppath;
21334 + struct super_block *sb;
21335 + struct au_branch *br;
21336 + struct mnt_idmap *h_idmap;
21338 + h_dentry = au_h_dptr(dentry, bindex);
21339 + if (d_really_is_positive(dentry)) {
21341 + if (unlikely(d_is_negative(h_dentry)))
21343 + h_inode = d_inode(h_dentry);
21344 + if (unlikely(!h_inode->i_nlink))
21347 + h_mode = h_inode->i_mode;
21350 + if (unlikely(S_ISDIR(h_mode)))
21352 + } else if (unlikely(!S_ISDIR(h_mode))) {
21357 + /* rename(2) case */
21359 + if (unlikely(d_is_positive(h_dentry)))
21364 + /* expected parent dir is locked */
21365 + if (unlikely(h_parent != h_dentry->d_parent))
21370 + * rmdir a dir may break the consistency on some filesystem.
21371 + * let's try heavy test.
21374 + sb = dentry->d_sb;
21375 + br = au_sbr(sb, bindex);
21376 + h_idmap = au_br_idmap(br);
21377 + if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
21378 + && au_test_h_perm(h_idmap, d_inode(h_parent),
21379 + MAY_EXEC | MAY_WRITE)))
21382 + h_ppath.dentry = h_parent;
21383 + h_ppath.mnt = au_br_mnt(br);
21384 + h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
21386 + if (IS_ERR(h_latest))
21388 + if (h_latest == h_dentry)
21397 + * decide the branch where we operate for @dentry. the branch index will be set
21398 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
21399 + * dir for reverting.
21400 + * when a new whiteout is necessary, create it.
21402 +static struct dentry*
21403 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
21404 + struct au_dtime *dt, struct au_pin *pin)
21406 + struct dentry *wh_dentry;
21407 + struct super_block *sb;
21408 + struct path h_path;
21409 + int err, need_wh;
21410 + unsigned int udba;
21411 + aufs_bindex_t bcpup;
21413 + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
21414 + wh_dentry = ERR_PTR(need_wh);
21415 + if (unlikely(need_wh < 0))
21418 + sb = dentry->d_sb;
21419 + udba = au_opt_udba(sb);
21421 + err = au_pin(pin, dentry, bcpup, udba,
21422 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21423 + wh_dentry = ERR_PTR(err);
21424 + if (unlikely(err))
21427 + h_path.dentry = au_pinned_h_parent(pin);
21428 + if (udba != AuOpt_UDBA_NONE
21429 + && au_dbtop(dentry) == bcpup) {
21430 + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
21431 + wh_dentry = ERR_PTR(err);
21432 + if (unlikely(err))
21436 + h_path.mnt = au_sbr_mnt(sb, bcpup);
21437 + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
21438 + wh_dentry = NULL;
21440 + goto out; /* success, no need to create whiteout */
21442 + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
21443 + if (IS_ERR(wh_dentry))
21446 + /* returns with the parent is locked and wh_dentry is dget-ed */
21447 + goto out; /* success */
21452 + return wh_dentry;
21456 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
21457 + * in order to be revertible and save time for removing many child whiteouts
21459 + * returns 1 when there are too many child whiteout and caller should remove
21460 + * them asynchronously. returns 0 when the number of children is enough small to
21461 + * remove now or the branch fs is a remote fs.
21462 + * otherwise return an error.
21464 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
21465 + struct au_nhash *whlist, struct inode *dir)
21467 + int rmdir_later, err, dirwh;
21468 + struct dentry *h_dentry;
21469 + struct super_block *sb;
21470 + struct inode *inode;
21472 + sb = dentry->d_sb;
21473 + SiMustAnyLock(sb);
21474 + h_dentry = au_h_dptr(dentry, bindex);
21475 + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
21476 + if (unlikely(err))
21479 + /* stop monitoring */
21480 + inode = d_inode(dentry);
21481 + au_hn_free(au_hi(inode, bindex));
21483 + if (!au_test_fs_remote(h_dentry->d_sb)) {
21484 + dirwh = au_sbi(sb)->si_dirwh;
21485 + rmdir_later = (dirwh <= 1);
21486 + if (!rmdir_later)
21487 + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
21490 + return rmdir_later;
21493 + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
21494 + if (unlikely(err)) {
21495 + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
21496 + h_dentry, bindex, err);
21506 + * final procedure for deleting a entry.
21507 + * maintain dentry and iattr.
21509 +static void epilog(struct inode *dir, struct dentry *dentry,
21510 + aufs_bindex_t bindex)
21512 + struct inode *inode;
21514 + inode = d_inode(dentry);
21516 + inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
21518 + au_dir_ts(dir, bindex);
21519 + inode_inc_iversion(dir);
21523 + * when an error happened, remove the created whiteout and revert everything.
21525 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
21526 + aufs_bindex_t bwh, struct dentry *wh_dentry,
21527 + struct dentry *dentry, struct au_dtime *dt)
21530 + struct path h_path = {
21531 + .dentry = wh_dentry,
21532 + .mnt = au_sbr_mnt(dir->i_sb, bindex)
21535 + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
21537 + au_set_dbwh(dentry, bwh);
21538 + au_dtime_revert(dt);
21542 + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
21546 +/* ---------------------------------------------------------------------- */
21548 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
21551 + aufs_bindex_t bwh, bindex, btop;
21552 + struct inode *inode, *h_dir, *delegated, *h_inode;
21553 + struct dentry *parent, *wh_dentry;
21554 + /* to reduce stack size */
21556 + struct au_dtime dt;
21557 + struct au_pin pin;
21558 + struct path h_path;
21564 + a = kmalloc(sizeof(*a), GFP_NOFS);
21565 + if (unlikely(!a))
21568 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21569 + if (unlikely(err))
21571 + err = au_d_hashed_positive(dentry);
21572 + if (unlikely(err))
21574 + inode = d_inode(dentry);
21575 + IMustLock(inode);
21577 + if (unlikely(d_is_dir(dentry)))
21578 + goto out_unlock; /* possible? */
21580 + btop = au_dbtop(dentry);
21581 + bwh = au_dbwh(dentry);
21583 + parent = dentry->d_parent; /* dir inode is locked */
21584 + di_write_lock_parent(parent);
21585 + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
21587 + err = PTR_ERR(wh_dentry);
21588 + if (IS_ERR(wh_dentry))
21591 + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
21592 + a->h_path.dentry = au_h_dptr(dentry, btop);
21593 + dget(a->h_path.dentry);
21594 + if (bindex == btop) {
21595 + h_dir = au_pinned_h_dir(&a->pin);
21596 + delegated = NULL;
21597 + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
21598 + if (unlikely(err == -EWOULDBLOCK)) {
21599 + pr_warn("cannot retry for NFSv4 delegation"
21600 + " for an internal unlink\n");
21604 + /* dir inode is locked */
21605 + h_dir = d_inode(wh_dentry->d_parent);
21606 + IMustLock(h_dir);
21611 + vfsub_drop_nlink(inode);
21612 + epilog(dir, dentry, bindex);
21614 + /* update target timestamps */
21615 + if (bindex == btop) {
21616 + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
21618 + h_inode = d_inode(a->h_path.dentry);
21619 + inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
21621 + /* todo: this timestamp may be reverted later */
21622 + inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
21623 + goto out_unpin; /* success */
21630 + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
21637 + au_unpin(&a->pin);
21639 + dput(a->h_path.dentry);
21641 + di_write_unlock(parent);
21643 + aufs_read_unlock(dentry, AuLock_DW);
21650 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
21652 + int err, rmdir_later;
21653 + aufs_bindex_t bwh, bindex, btop;
21654 + struct inode *inode;
21655 + struct dentry *parent, *wh_dentry, *h_dentry;
21656 + struct au_whtmp_rmdir *args;
21657 + /* to reduce stack size */
21659 + struct au_dtime dt;
21660 + struct au_pin pin;
21666 + a = kmalloc(sizeof(*a), GFP_NOFS);
21667 + if (unlikely(!a))
21670 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21671 + if (unlikely(err))
21673 + err = au_alive_dir(dentry);
21674 + if (unlikely(err))
21676 + inode = d_inode(dentry);
21677 + IMustLock(inode);
21679 + if (unlikely(!d_is_dir(dentry)))
21680 + goto out_unlock; /* possible? */
21683 + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
21684 + if (unlikely(!args))
21687 + parent = dentry->d_parent; /* dir inode is locked */
21688 + di_write_lock_parent(parent);
21689 + err = au_test_empty(dentry, &args->whlist);
21690 + if (unlikely(err))
21693 + btop = au_dbtop(dentry);
21694 + bwh = au_dbwh(dentry);
21696 + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
21698 + err = PTR_ERR(wh_dentry);
21699 + if (IS_ERR(wh_dentry))
21702 + h_dentry = au_h_dptr(dentry, btop);
21705 + if (bindex == btop) {
21706 + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
21708 + rmdir_later = err;
21712 + /* stop monitoring */
21713 + au_hn_free(au_hi(inode, btop));
21715 + /* dir inode is locked */
21716 + IMustLock(d_inode(wh_dentry->d_parent));
21721 + vfsub_dead_dir(inode);
21722 + au_set_dbdiropq(dentry, -1);
21723 + epilog(dir, dentry, bindex);
21725 + if (rmdir_later) {
21726 + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
21730 + goto out_unpin; /* success */
21738 + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
21745 + au_unpin(&a->pin);
21749 + di_write_unlock(parent);
21751 + au_whtmp_rmdir_free(args);
21753 + aufs_read_unlock(dentry, AuLock_DW);
21760 diff --git a/fs/aufs/i_op_ren.c b/fs/aufs/i_op_ren.c
21761 new file mode 100644
21762 index 00000000000000..e5a3d5549ac7ae
21764 +++ b/fs/aufs/i_op_ren.c
21766 +// SPDX-License-Identifier: GPL-2.0
21768 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21770 + * This program is free software; you can redistribute it and/or modify
21771 + * it under the terms of the GNU General Public License as published by
21772 + * the Free Software Foundation; either version 2 of the License, or
21773 + * (at your option) any later version.
21775 + * This program is distributed in the hope that it will be useful,
21776 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21777 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21778 + * GNU General Public License for more details.
21780 + * You should have received a copy of the GNU General Public License
21781 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
21785 + * inode operation (rename entry)
21786 + * todo: this is crazy monster
21789 +#include <linux/iversion.h>
21792 +enum { AuSRC, AuDST, AuSrcDst };
21793 +enum { AuPARENT, AuCHILD, AuParentChild };
21795 +#define AuRen_ISDIR_SRC 1
21796 +#define AuRen_ISDIR_DST (1 << 1)
21797 +#define AuRen_ISSAMEDIR (1 << 2)
21798 +#define AuRen_WHSRC (1 << 3)
21799 +#define AuRen_WHDST (1 << 4)
21800 +#define AuRen_MNT_WRITE (1 << 5)
21801 +#define AuRen_DT_DSTDIR (1 << 6)
21802 +#define AuRen_DIROPQ_SRC (1 << 7)
21803 +#define AuRen_DIROPQ_DST (1 << 8)
21804 +#define AuRen_DIRREN (1 << 9)
21805 +#define AuRen_DROPPED_SRC (1 << 10)
21806 +#define AuRen_DROPPED_DST (1 << 11)
21807 +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
21808 +#define au_fset_ren(flags, name) \
21809 + do { (flags) |= AuRen_##name; } while (0)
21810 +#define au_fclr_ren(flags, name) \
21811 + do { (flags) &= ~AuRen_##name; } while (0)
21813 +#ifndef CONFIG_AUFS_DIRREN
21814 +#undef AuRen_DIRREN
21815 +#define AuRen_DIRREN 0
21818 +struct au_ren_args {
21820 + struct dentry *dentry, *h_dentry, *parent, *h_parent,
21822 + struct inode *dir, *inode;
21823 + struct au_hinode *hdir, *hinode;
21824 + struct au_dtime dt[AuParentChild];
21825 + aufs_bindex_t btop, bdiropq;
21828 +#define src_dentry sd[AuSRC].dentry
21829 +#define src_dir sd[AuSRC].dir
21830 +#define src_inode sd[AuSRC].inode
21831 +#define src_h_dentry sd[AuSRC].h_dentry
21832 +#define src_parent sd[AuSRC].parent
21833 +#define src_h_parent sd[AuSRC].h_parent
21834 +#define src_wh_dentry sd[AuSRC].wh_dentry
21835 +#define src_hdir sd[AuSRC].hdir
21836 +#define src_hinode sd[AuSRC].hinode
21837 +#define src_h_dir sd[AuSRC].hdir->hi_inode
21838 +#define src_dt sd[AuSRC].dt
21839 +#define src_btop sd[AuSRC].btop
21840 +#define src_bdiropq sd[AuSRC].bdiropq
21842 +#define dst_dentry sd[AuDST].dentry
21843 +#define dst_dir sd[AuDST].dir
21844 +#define dst_inode sd[AuDST].inode
21845 +#define dst_h_dentry sd[AuDST].h_dentry
21846 +#define dst_parent sd[AuDST].parent
21847 +#define dst_h_parent sd[AuDST].h_parent
21848 +#define dst_wh_dentry sd[AuDST].wh_dentry
21849 +#define dst_hdir sd[AuDST].hdir
21850 +#define dst_hinode sd[AuDST].hinode
21851 +#define dst_h_dir sd[AuDST].hdir->hi_inode
21852 +#define dst_dt sd[AuDST].dt
21853 +#define dst_btop sd[AuDST].btop
21854 +#define dst_bdiropq sd[AuDST].bdiropq
21856 + struct dentry *h_trap;
21857 + struct au_branch *br;
21858 + struct path h_path;
21859 + struct au_nhash whlist;
21860 + aufs_bindex_t btgt, src_bwh;
21863 + unsigned short auren_flags;
21864 + unsigned char flags; /* syscall parameter */
21865 + unsigned char exchange;
21868 + struct au_whtmp_rmdir *thargs;
21869 + struct dentry *h_dst;
21870 + struct au_hinode *h_root;
21873 +/* ---------------------------------------------------------------------- */
21876 + * functions for reverting.
21877 + * when an error happened in a single rename systemcall, we should revert
21878 + * everything as if nothing happened.
21879 + * we don't need to revert the copied-up/down the parent dir since they are
21883 +#define RevertFailure(fmt, ...) do { \
21884 + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
21885 + ##__VA_ARGS__, err, rerr); \
21889 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
21892 + struct dentry *d;
21893 +#define src_or_dst(member) a->sd[idx].member
21895 + d = src_or_dst(dentry); /* {src,dst}_dentry */
21896 + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
21897 + rerr = au_diropq_remove(d, a->btgt);
21898 + au_hn_inode_unlock(src_or_dst(hinode));
21899 + au_set_dbdiropq(d, src_or_dst(bdiropq));
21901 + RevertFailure("remove diropq %pd", d);
21903 +#undef src_or_dst_
21906 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
21908 + if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
21909 + au_ren_do_rev_diropq(err, a, AuSRC);
21910 + if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
21911 + au_ren_do_rev_diropq(err, a, AuDST);
21914 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
21917 + struct inode *delegated;
21918 + struct path h_ppath = {
21919 + .dentry = a->src_h_parent,
21920 + .mnt = a->h_path.mnt
21923 + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
21924 + rerr = PTR_ERR(a->h_path.dentry);
21925 + if (IS_ERR(a->h_path.dentry)) {
21926 + RevertFailure("lkup one %pd", a->src_dentry);
21930 + delegated = NULL;
21931 + rerr = vfsub_rename(a->dst_h_dir,
21932 + au_h_dptr(a->src_dentry, a->btgt),
21933 + a->src_h_dir, &a->h_path, &delegated, a->flags);
21934 + if (unlikely(rerr == -EWOULDBLOCK)) {
21935 + pr_warn("cannot retry for NFSv4 delegation"
21936 + " for an internal rename\n");
21939 + d_drop(a->h_path.dentry);
21940 + dput(a->h_path.dentry);
21941 + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
21943 + RevertFailure("rename %pd", a->src_dentry);
21946 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
21949 + struct inode *delegated;
21950 + struct path h_ppath = {
21951 + .dentry = a->dst_h_parent,
21952 + .mnt = a->h_path.mnt
21955 + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
21956 + rerr = PTR_ERR(a->h_path.dentry);
21957 + if (IS_ERR(a->h_path.dentry)) {
21958 + RevertFailure("lkup one %pd", a->dst_dentry);
21961 + if (d_is_positive(a->h_path.dentry)) {
21962 + d_drop(a->h_path.dentry);
21963 + dput(a->h_path.dentry);
21967 + delegated = NULL;
21968 + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
21969 + &delegated, a->flags);
21970 + if (unlikely(rerr == -EWOULDBLOCK)) {
21971 + pr_warn("cannot retry for NFSv4 delegation"
21972 + " for an internal rename\n");
21975 + d_drop(a->h_path.dentry);
21976 + dput(a->h_path.dentry);
21978 + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
21980 + RevertFailure("rename %pd", a->h_dst);
21983 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
21987 + a->h_path.dentry = a->src_wh_dentry;
21988 + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
21989 + au_set_dbwh(a->src_dentry, a->src_bwh);
21991 + RevertFailure("unlink %pd", a->src_wh_dentry);
21993 +#undef RevertFailure
21995 +/* ---------------------------------------------------------------------- */
21998 + * when we have to copyup the renaming entry, do it with the rename-target name
21999 + * in order to minimize the cost (the later actual rename is unnecessary).
22000 + * otherwise rename it on the target branch.
22002 +static int au_ren_or_cpup(struct au_ren_args *a)
22005 + struct dentry *d;
22006 + struct inode *delegated;
22008 + d = a->src_dentry;
22009 + if (au_dbtop(d) == a->btgt) {
22010 + a->h_path.dentry = a->dst_h_dentry;
22011 + AuDebugOn(au_dbtop(d) != a->btgt);
22012 + delegated = NULL;
22013 + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22014 + a->dst_h_dir, &a->h_path, &delegated,
22016 + if (unlikely(err == -EWOULDBLOCK)) {
22017 + pr_warn("cannot retry for NFSv4 delegation"
22018 + " for an internal rename\n");
22024 + if (!err && a->h_dst)
22025 + /* it will be set to dinfo later */
22031 +/* cf. aufs_rmdir() */
22032 +static int au_ren_del_whtmp(struct au_ren_args *a)
22035 + struct inode *dir;
22037 + dir = a->dst_dir;
22038 + SiMustAnyLock(dir->i_sb);
22039 + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22040 + au_sbi(dir->i_sb)->si_dirwh)
22041 + || au_test_fs_remote(a->h_dst->d_sb)) {
22042 + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22043 + if (unlikely(err))
22044 + pr_warn("failed removing whtmp dir %pd (%d), "
22045 + "ignored.\n", a->h_dst, err);
22047 + au_nhash_wh_free(&a->thargs->whlist);
22048 + a->thargs->whlist = a->whlist;
22049 + a->whlist.nh_num = 0;
22050 + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22052 + a->thargs = NULL;
22058 +/* make it 'opaque' dir. */
22059 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22062 + struct dentry *d, *diropq;
22063 +#define src_or_dst(member) a->sd[idx].member
22066 + d = src_or_dst(dentry); /* {src,dst}_dentry */
22067 + src_or_dst(bdiropq) = au_dbdiropq(d);
22068 + src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22069 + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22070 + diropq = au_diropq_create(d, a->btgt);
22071 + au_hn_inode_unlock(src_or_dst(hinode));
22072 + if (IS_ERR(diropq))
22073 + err = PTR_ERR(diropq);
22077 +#undef src_or_dst_
22081 +static int au_ren_diropq(struct au_ren_args *a)
22084 + unsigned char always;
22085 + struct dentry *d;
22088 + d = a->dst_dentry; /* already renamed on the branch */
22089 + always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22090 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22091 + && !au_ftest_ren(a->auren_flags, DIRREN)
22092 + && a->btgt != au_dbdiropq(a->src_dentry)
22093 + && (a->dst_wh_dentry
22094 + || a->btgt <= au_dbdiropq(d)
22095 + /* hide the lower to keep xino */
22096 + /* the lowers may not be a dir, but we hide them anyway */
22097 + || a->btgt < au_dbbot(d)
22100 + err = au_ren_do_diropq(a, AuSRC);
22101 + if (unlikely(err))
22103 + au_fset_ren(a->auren_flags, DIROPQ_SRC);
22105 + if (!a->exchange)
22106 + goto out; /* success */
22108 + d = a->src_dentry; /* already renamed on the branch */
22109 + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22110 + && a->btgt != au_dbdiropq(a->dst_dentry)
22111 + && (a->btgt < au_dbdiropq(d)
22112 + || a->btgt < au_dbbot(d)
22114 + AuDbgDentry(a->src_dentry);
22115 + AuDbgDentry(a->dst_dentry);
22116 + err = au_ren_do_diropq(a, AuDST);
22117 + if (unlikely(err))
22118 + goto out_rev_src;
22119 + au_fset_ren(a->auren_flags, DIROPQ_DST);
22121 + goto out; /* success */
22124 + AuDbg("err %d, reverting src\n", err);
22125 + au_ren_rev_diropq(err, a);
22130 +static int do_rename(struct au_ren_args *a)
22133 + struct dentry *d, *h_d;
22134 + struct inode *h_inode;
22136 + if (!a->exchange) {
22137 + /* prepare workqueue args for asynchronous rmdir */
22138 + h_d = a->dst_h_dentry;
22139 + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22140 + /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22141 + && d_is_positive(h_d)) {
22143 + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22145 + if (unlikely(!a->thargs))
22147 + a->h_dst = dget(h_d);
22150 + /* create whiteout for src_dentry */
22151 + if (au_ftest_ren(a->auren_flags, WHSRC)) {
22152 + a->src_bwh = au_dbwh(a->src_dentry);
22153 + AuDebugOn(a->src_bwh >= 0);
22154 + a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22155 + a->src_h_parent);
22156 + err = PTR_ERR(a->src_wh_dentry);
22157 + if (IS_ERR(a->src_wh_dentry))
22161 + /* lookup whiteout for dentry */
22162 + if (au_ftest_ren(a->auren_flags, WHDST)) {
22163 + h_d = au_wh_lkup(a->dst_h_parent,
22164 + &a->dst_dentry->d_name, a->br);
22165 + err = PTR_ERR(h_d);
22168 + if (d_is_negative(h_d))
22171 + a->dst_wh_dentry = h_d;
22174 + /* rename dentry to tmpwh */
22176 + err = au_whtmp_ren(a->dst_h_dentry, a->br);
22177 + if (unlikely(err))
22180 + d = a->dst_dentry;
22181 + au_set_h_dptr(d, a->btgt, NULL);
22182 + err = au_lkup_neg(d, a->btgt, /*wh*/0);
22183 + if (unlikely(err))
22185 + a->dst_h_dentry = au_h_dptr(d, a->btgt);
22189 + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22190 +#if 0 /* debugging */
22191 + BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22192 + && d_is_positive(a->dst_h_dentry)
22193 + && a->src_btop != a->btgt);
22196 + /* rename by vfs_rename or cpup */
22197 + err = au_ren_or_cpup(a);
22198 + if (unlikely(err))
22199 + /* leave the copied-up one */
22202 + /* make dir opaque */
22203 + err = au_ren_diropq(a);
22204 + if (unlikely(err))
22207 + /* update target timestamps */
22208 + if (a->exchange) {
22209 + AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22210 + a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22211 + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22212 + h_inode = d_inode(a->h_path.dentry);
22213 + inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
22215 + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22216 + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22217 + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22218 + h_inode = d_inode(a->h_path.dentry);
22219 + inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
22221 + if (!a->exchange) {
22222 + /* remove whiteout for dentry */
22223 + if (a->dst_wh_dentry) {
22224 + a->h_path.dentry = a->dst_wh_dentry;
22225 + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22227 + if (unlikely(err))
22231 + /* remove whtmp */
22233 + au_ren_del_whtmp(a); /* ignore this error */
22235 + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22238 + goto out_success;
22241 + au_ren_rev_diropq(err, a);
22243 + au_ren_rev_rename(err, a);
22247 + au_ren_rev_whtmp(err, a);
22249 + dput(a->dst_wh_dentry);
22250 + a->dst_wh_dentry = NULL;
22252 + if (a->src_wh_dentry)
22253 + au_ren_rev_whsrc(err, a);
22255 + dput(a->src_wh_dentry);
22256 + dput(a->dst_wh_dentry);
22260 + au_whtmp_rmdir_free(a->thargs);
22261 + a->thargs = NULL;
22267 +/* ---------------------------------------------------------------------- */
22270 + * test if @dentry dir can be rename destination or not.
22271 + * success means, it is a logically empty dir.
22273 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22275 + return au_test_empty(dentry, whlist);
22279 + * test if @a->src_dentry dir can be rename source or not.
22280 + * if it can, return 0.
22282 + * - it is a logically empty dir.
22283 + * - or, it exists on writable branch and has no children including whiteouts
22284 + * on the lower branch unless DIRREN is on.
22286 +static int may_rename_srcdir(struct au_ren_args *a)
22289 + unsigned int rdhash;
22290 + aufs_bindex_t btop, btgt;
22291 + struct dentry *dentry;
22292 + struct super_block *sb;
22293 + struct au_sbinfo *sbinfo;
22295 + dentry = a->src_dentry;
22296 + sb = dentry->d_sb;
22297 + sbinfo = au_sbi(sb);
22298 + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22299 + au_fset_ren(a->auren_flags, DIRREN);
22302 + btop = au_dbtop(dentry);
22303 + if (btop != btgt) {
22304 + struct au_nhash whlist;
22306 + SiMustAnyLock(sb);
22307 + rdhash = sbinfo->si_rdhash;
22309 + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
22311 + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
22312 + if (unlikely(err))
22314 + err = au_test_empty(dentry, &whlist);
22315 + au_nhash_wh_free(&whlist);
22319 + if (btop == au_dbtaildir(dentry))
22320 + return 0; /* success */
22322 + err = au_test_empty_lower(dentry);
22325 + if (err == -ENOTEMPTY) {
22326 + if (au_ftest_ren(a->auren_flags, DIRREN)) {
22329 + AuWarn1("renaming dir who has child(ren) on multiple "
22330 + "branches, is not supported\n");
22337 +/* side effect: sets whlist and h_dentry */
22338 +static int au_ren_may_dir(struct au_ren_args *a)
22341 + unsigned int rdhash;
22342 + struct dentry *d;
22344 + d = a->dst_dentry;
22345 + SiMustAnyLock(d->d_sb);
22348 + if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
22349 + rdhash = au_sbi(d->d_sb)->si_rdhash;
22351 + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
22352 + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
22353 + if (unlikely(err))
22356 + if (!a->exchange) {
22357 + au_set_dbtop(d, a->dst_btop);
22358 + err = may_rename_dstdir(d, &a->whlist);
22359 + au_set_dbtop(d, a->btgt);
22361 + err = may_rename_srcdir(a);
22363 + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
22364 + if (unlikely(err))
22367 + d = a->src_dentry;
22368 + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
22369 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
22370 + err = may_rename_srcdir(a);
22371 + if (unlikely(err)) {
22372 + au_nhash_wh_free(&a->whlist);
22373 + a->whlist.nh_num = 0;
22380 +/* ---------------------------------------------------------------------- */
22383 + * simple tests for rename.
22384 + * following the checks in vfs, plus the parent-child relationship.
22386 +static int au_may_ren(struct au_ren_args *a)
22389 + struct inode *h_inode;
22391 + if (a->src_btop == a->btgt) {
22392 + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
22393 + au_ftest_ren(a->auren_flags, ISDIR_SRC));
22394 + if (unlikely(err))
22397 + if (unlikely(a->src_h_dentry == a->h_trap))
22402 + if (a->dst_btop != a->btgt)
22405 + err = -ENOTEMPTY;
22406 + if (unlikely(a->dst_h_dentry == a->h_trap))
22410 + isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
22411 + if (d_really_is_negative(a->dst_dentry)) {
22412 + if (d_is_negative(a->dst_h_dentry))
22413 + err = au_may_add(a->dst_dentry, a->btgt,
22414 + a->dst_h_parent, isdir);
22416 + if (unlikely(d_is_negative(a->dst_h_dentry)))
22418 + h_inode = d_inode(a->dst_h_dentry);
22419 + if (h_inode->i_nlink)
22420 + err = au_may_del(a->dst_dentry, a->btgt,
22421 + a->dst_h_parent, isdir);
22425 + if (unlikely(err == -ENOENT || err == -EEXIST))
22431 +/* ---------------------------------------------------------------------- */
22436 + * - src_dir and dir by lock_rename()
22437 + * - inode if exists
22440 + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
22442 + * + di_write_lock2_child()
22443 + * + di_write_lock_child()
22444 + * + ii_write_lock_child()
22445 + * + di_write_lock_child2()
22446 + * + ii_write_lock_child2()
22447 + * + src_parent and parent
22448 + * + di_write_lock_parent()
22449 + * + ii_write_lock_parent()
22450 + * + di_write_lock_parent2()
22451 + * + ii_write_lock_parent2()
22452 + * + lower src_dir and dir by vfsub_lock_rename()
22453 + * + verify the every relationships between child and parent. if any
22454 + * of them failed, unlock all and return -EBUSY.
22456 +static void au_ren_unlock(struct au_ren_args *a)
22458 + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
22459 + a->dst_h_parent, a->dst_hdir);
22460 + if (au_ftest_ren(a->auren_flags, DIRREN)
22462 + au_hn_inode_unlock(a->h_root);
22463 + if (au_ftest_ren(a->auren_flags, MNT_WRITE))
22464 + vfsub_mnt_drop_write(au_br_mnt(a->br));
22467 +static int au_ren_lock(struct au_ren_args *a)
22470 + unsigned int udba;
22473 + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
22474 + a->src_hdir = au_hi(a->src_dir, a->btgt);
22475 + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
22476 + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
22478 + err = vfsub_mnt_want_write(au_br_mnt(a->br));
22479 + if (unlikely(err))
22481 + au_fset_ren(a->auren_flags, MNT_WRITE);
22482 + if (au_ftest_ren(a->auren_flags, DIRREN)) {
22483 + struct dentry *root;
22484 + struct inode *dir;
22487 + * sbinfo is already locked, so this ii_read_lock is
22488 + * unnecessary. but our debugging feature checks it.
22490 + root = a->src_inode->i_sb->s_root;
22491 + if (root != a->src_parent && root != a->dst_parent) {
22492 + dir = d_inode(root);
22493 + ii_read_lock_parent3(dir);
22494 + a->h_root = au_hi(dir, a->btgt);
22495 + ii_read_unlock(dir);
22496 + au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
22499 + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
22500 + a->dst_h_parent, a->dst_hdir);
22501 + udba = au_opt_udba(a->src_dentry->d_sb);
22502 + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
22503 + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
22504 + err = au_busy_or_stale();
22505 + if (!err && au_dbtop(a->src_dentry) == a->btgt)
22506 + err = au_h_verify(a->src_h_dentry, udba,
22507 + d_inode(a->src_h_parent), a->src_h_parent,
22509 + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
22510 + err = au_h_verify(a->dst_h_dentry, udba,
22511 + d_inode(a->dst_h_parent), a->dst_h_parent,
22514 + goto out; /* success */
22516 + err = au_busy_or_stale();
22517 + au_ren_unlock(a);
22523 +/* ---------------------------------------------------------------------- */
22525 +static void au_ren_refresh_dir(struct au_ren_args *a)
22527 + struct inode *dir;
22529 + dir = a->dst_dir;
22530 + inode_inc_iversion(dir);
22531 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
22532 + /* is this updating defined in POSIX? */
22533 + au_cpup_attr_timesizes(a->src_inode);
22534 + au_cpup_attr_nlink(dir, /*force*/1);
22536 + au_dir_ts(dir, a->btgt);
22538 + if (a->exchange) {
22539 + dir = a->src_dir;
22540 + inode_inc_iversion(dir);
22541 + if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
22542 + /* is this updating defined in POSIX? */
22543 + au_cpup_attr_timesizes(a->dst_inode);
22544 + au_cpup_attr_nlink(dir, /*force*/1);
22546 + au_dir_ts(dir, a->btgt);
22549 + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
22552 + dir = a->src_dir;
22553 + inode_inc_iversion(dir);
22554 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
22555 + au_cpup_attr_nlink(dir, /*force*/1);
22556 + au_dir_ts(dir, a->btgt);
22559 +static void au_ren_refresh(struct au_ren_args *a)
22561 + aufs_bindex_t bbot, bindex;
22562 + struct dentry *d, *h_d;
22563 + struct inode *i, *h_i;
22564 + struct super_block *sb;
22566 + d = a->dst_dentry;
22569 + /* already dget-ed by au_ren_or_cpup() */
22570 + au_set_h_dptr(d, a->btgt, a->h_dst);
22572 + i = a->dst_inode;
22574 + if (!a->exchange) {
22575 + if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
22576 + vfsub_drop_nlink(i);
22578 + vfsub_dead_dir(i);
22579 + au_cpup_attr_timesizes(i);
22581 + au_update_dbrange(d, /*do_put_zero*/1);
22583 + au_cpup_attr_nlink(i, /*force*/1);
22586 + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
22587 + au_set_h_dptr(d, bindex, NULL);
22588 + bbot = au_dbbot(d);
22589 + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
22590 + au_set_h_dptr(d, bindex, NULL);
22591 + au_update_dbrange(d, /*do_put_zero*/0);
22595 + || au_ftest_ren(a->auren_flags, DIRREN)) {
22596 + d_drop(a->src_dentry);
22597 + if (au_ftest_ren(a->auren_flags, DIRREN))
22598 + au_set_dbwh(a->src_dentry, -1);
22602 + d = a->src_dentry;
22603 + au_set_dbwh(d, -1);
22604 + bbot = au_dbbot(d);
22605 + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
22606 + h_d = au_h_dptr(d, bindex);
22608 + au_set_h_dptr(d, bindex, NULL);
22610 + au_set_dbbot(d, a->btgt);
22613 + i = a->src_inode;
22614 + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
22615 + return; /* success */
22617 + bbot = au_ibbot(i);
22618 + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
22619 + h_i = au_h_iptr(i, bindex);
22621 + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
22622 + /* ignore this error */
22623 + au_set_h_iptr(i, bindex, NULL, 0);
22626 + au_set_ibbot(i, a->btgt);
22629 +/* ---------------------------------------------------------------------- */
22631 +/* mainly for link(2) and rename(2) */
22632 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
22634 + aufs_bindex_t bdiropq, bwh;
22635 + struct dentry *parent;
22636 + struct au_branch *br;
22638 + parent = dentry->d_parent;
22639 + IMustLock(d_inode(parent)); /* dir is locked */
22641 + bdiropq = au_dbdiropq(parent);
22642 + bwh = au_dbwh(dentry);
22643 + br = au_sbr(dentry->d_sb, btgt);
22644 + if (au_br_rdonly(br)
22645 + || (0 <= bdiropq && bdiropq < btgt)
22646 + || (0 <= bwh && bwh < btgt))
22649 + AuDbg("btgt %d\n", btgt);
22653 +/* sets src_btop, dst_btop and btgt */
22654 +static int au_ren_wbr(struct au_ren_args *a)
22657 + struct au_wr_dir_args wr_dir_args = {
22658 + /* .force_btgt = -1, */
22659 + .flags = AuWrDir_ADD_ENTRY
22662 + a->src_btop = au_dbtop(a->src_dentry);
22663 + a->dst_btop = au_dbtop(a->dst_dentry);
22664 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22665 + || au_ftest_ren(a->auren_flags, ISDIR_DST))
22666 + au_fset_wrdir(wr_dir_args.flags, ISDIR);
22667 + wr_dir_args.force_btgt = a->src_btop;
22668 + if (a->dst_inode && a->dst_btop < a->src_btop)
22669 + wr_dir_args.force_btgt = a->dst_btop;
22670 + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
22671 + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
22674 + au_update_dbtop(a->dst_dentry);
22679 +static void au_ren_dt(struct au_ren_args *a)
22681 + a->h_path.dentry = a->src_h_parent;
22682 + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
22683 + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
22684 + a->h_path.dentry = a->dst_h_parent;
22685 + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
22688 + au_fclr_ren(a->auren_flags, DT_DSTDIR);
22689 + if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
22693 + a->h_path.dentry = a->src_h_dentry;
22694 + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
22695 + if (d_is_positive(a->dst_h_dentry)) {
22696 + au_fset_ren(a->auren_flags, DT_DSTDIR);
22697 + a->h_path.dentry = a->dst_h_dentry;
22698 + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
22702 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
22704 + struct dentry *h_d;
22705 + struct inode *h_inode;
22707 + au_dtime_revert(a->src_dt + AuPARENT);
22708 + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
22709 + au_dtime_revert(a->dst_dt + AuPARENT);
22711 + if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
22712 + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
22713 + h_inode = d_inode(h_d);
22714 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
22715 + au_dtime_revert(a->src_dt + AuCHILD);
22716 + inode_unlock(h_inode);
22718 + if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
22719 + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
22720 + h_inode = d_inode(h_d);
22721 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
22722 + au_dtime_revert(a->dst_dt + AuCHILD);
22723 + inode_unlock(h_inode);
22728 +/* ---------------------------------------------------------------------- */
22730 +int aufs_rename(struct mnt_idmap *idmap,
22731 + struct inode *_src_dir, struct dentry *_src_dentry,
22732 + struct inode *_dst_dir, struct dentry *_dst_dentry,
22733 + unsigned int _flags)
22735 + int err, lock_flags;
22737 + /* reduce stack space */
22738 + struct au_ren_args *a;
22739 + struct au_pin pin;
22741 + AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
22742 + IMustLock(_src_dir);
22743 + IMustLock(_dst_dir);
22746 + if (unlikely(_flags & RENAME_WHITEOUT))
22750 + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
22751 + a = kzalloc(sizeof(*a), GFP_NOFS);
22752 + if (unlikely(!a))
22755 + a->flags = _flags;
22756 + BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
22757 + && RENAME_EXCHANGE > U8_MAX);
22758 + a->exchange = _flags & RENAME_EXCHANGE;
22759 + a->src_dir = _src_dir;
22760 + a->src_dentry = _src_dentry;
22761 + a->src_inode = NULL;
22762 + if (d_really_is_positive(a->src_dentry))
22763 + a->src_inode = d_inode(a->src_dentry);
22764 + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
22765 + a->dst_dir = _dst_dir;
22766 + a->dst_dentry = _dst_dentry;
22767 + a->dst_inode = NULL;
22768 + if (d_really_is_positive(a->dst_dentry))
22769 + a->dst_inode = d_inode(a->dst_dentry);
22770 + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
22771 + if (a->dst_inode) {
22773 + * if EXCHANGE && src is non-dir && dst is dir,
22774 + * dst is not locked.
22776 + /* IMustLock(a->dst_inode); */
22777 + au_igrab(a->dst_inode);
22781 + lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
22782 + if (d_is_dir(a->src_dentry)) {
22783 + au_fset_ren(a->auren_flags, ISDIR_SRC);
22784 + if (unlikely(!a->exchange
22785 + && d_really_is_positive(a->dst_dentry)
22786 + && !d_is_dir(a->dst_dentry)))
22788 + lock_flags |= AuLock_DIRS;
22790 + if (a->dst_inode && d_is_dir(a->dst_dentry)) {
22791 + au_fset_ren(a->auren_flags, ISDIR_DST);
22792 + if (unlikely(!a->exchange
22793 + && d_really_is_positive(a->src_dentry)
22794 + && !d_is_dir(a->src_dentry)))
22796 + lock_flags |= AuLock_DIRS;
22798 + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
22800 + if (unlikely(err))
22803 + err = au_d_hashed_positive(a->src_dentry);
22804 + if (unlikely(err))
22807 + if (a->dst_inode) {
22809 + * If it is a dir, VFS unhash it before this
22810 + * function. It means we cannot rely upon d_unhashed().
22812 + if (unlikely(!a->dst_inode->i_nlink))
22814 + if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
22815 + err = au_d_hashed_positive(a->dst_dentry);
22816 + if (unlikely(err && !a->exchange))
22818 + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
22820 + } else if (unlikely(d_unhashed(a->dst_dentry)))
22824 + * is it possible?
22825 + * yes, it happened (in linux-3.3-rcN) but I don't know why.
22826 + * there may exist a problem somewhere else.
22829 + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
22832 + au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
22833 + di_write_lock_parent(a->dst_parent);
22835 + /* which branch we process */
22836 + err = au_ren_wbr(a);
22837 + if (unlikely(err < 0))
22839 + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
22840 + a->h_path.mnt = au_br_mnt(a->br);
22842 + /* are they available to be renamed */
22843 + err = au_ren_may_dir(a);
22844 + if (unlikely(err))
22845 + goto out_children;
22847 + /* prepare the writable parent dir on the same branch */
22848 + if (a->dst_btop == a->btgt) {
22849 + au_fset_ren(a->auren_flags, WHDST);
22851 + err = au_cpup_dirs(a->dst_dentry, a->btgt);
22852 + if (unlikely(err))
22853 + goto out_children;
22857 + if (!a->exchange) {
22858 + if (a->src_dir != a->dst_dir) {
22860 + * this temporary unlock is safe,
22861 + * because both dir->i_mutex are locked.
22863 + di_write_unlock(a->dst_parent);
22864 + di_write_lock_parent(a->src_parent);
22865 + err = au_wr_dir_need_wh(a->src_dentry,
22866 + au_ftest_ren(a->auren_flags,
22869 + di_write_unlock(a->src_parent);
22870 + di_write_lock2_parent(a->src_parent, a->dst_parent,
22872 + au_fclr_ren(a->auren_flags, ISSAMEDIR);
22874 + err = au_wr_dir_need_wh(a->src_dentry,
22875 + au_ftest_ren(a->auren_flags,
22879 + if (unlikely(err < 0))
22880 + goto out_children;
22882 + au_fset_ren(a->auren_flags, WHSRC);
22885 + if (a->src_btop != a->btgt) {
22886 + err = au_pin(&pin, a->src_dentry, a->btgt,
22887 + au_opt_udba(a->src_dentry->d_sb),
22888 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22890 + struct au_cp_generic cpg = {
22891 + .dentry = a->src_dentry,
22893 + .bsrc = a->src_btop,
22896 + .flags = AuCpup_DTIME | AuCpup_HOPEN
22898 + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
22899 + err = au_sio_cpup_simple(&cpg);
22902 + if (unlikely(err))
22903 + goto out_children;
22904 + a->src_btop = a->btgt;
22905 + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
22906 + if (!a->exchange)
22907 + au_fset_ren(a->auren_flags, WHSRC);
22911 + if (a->exchange && a->dst_inode
22912 + && a->dst_btop != a->btgt) {
22913 + err = au_pin(&pin, a->dst_dentry, a->btgt,
22914 + au_opt_udba(a->dst_dentry->d_sb),
22915 + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22917 + struct au_cp_generic cpg = {
22918 + .dentry = a->dst_dentry,
22920 + .bsrc = a->dst_btop,
22923 + .flags = AuCpup_DTIME | AuCpup_HOPEN
22925 + err = au_sio_cpup_simple(&cpg);
22928 + if (unlikely(err))
22929 + goto out_children;
22930 + a->dst_btop = a->btgt;
22931 + a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
22934 + /* lock them all */
22935 + err = au_ren_lock(a);
22936 + if (unlikely(err))
22937 + /* leave the copied-up one */
22938 + goto out_children;
22940 + if (!a->exchange) {
22941 + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
22942 + err = au_may_ren(a);
22943 + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
22944 + err = -ENAMETOOLONG;
22945 + if (unlikely(err))
22949 + /* store timestamps to be revertible */
22952 + /* store dirren info */
22953 + if (au_ftest_ren(a->auren_flags, DIRREN)) {
22954 + err = au_dr_rename(a->src_dentry, a->btgt,
22955 + &a->dst_dentry->d_name, &rev);
22957 + if (unlikely(err))
22962 + err = do_rename(a);
22963 + if (unlikely(err))
22966 + if (au_ftest_ren(a->auren_flags, DIRREN))
22967 + au_dr_rename_fin(a->src_dentry, a->btgt, rev);
22969 + /* update dir attributes */
22970 + au_ren_refresh_dir(a);
22972 + /* dput/iput all lower dentries */
22973 + au_ren_refresh(a);
22975 + goto out_hdir; /* success */
22978 + if (au_ftest_ren(a->auren_flags, DIRREN))
22979 + au_dr_rename_rev(a->src_dentry, a->btgt, rev);
22981 + au_ren_rev_dt(err, a);
22983 + au_ren_unlock(a);
22985 + au_nhash_wh_free(&a->whlist);
22986 + if (err && a->dst_inode && a->dst_btop != a->btgt) {
22987 + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
22988 + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
22989 + au_set_dbtop(a->dst_dentry, a->dst_btop);
22993 + if (d_unhashed(a->src_dentry))
22994 + au_fset_ren(a->auren_flags, DROPPED_SRC);
22995 + if (d_unhashed(a->dst_dentry))
22996 + au_fset_ren(a->auren_flags, DROPPED_DST);
22997 + if (!a->exchange)
22998 + d_move(a->src_dentry, a->dst_dentry);
23000 + d_exchange(a->src_dentry, a->dst_dentry);
23001 + if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23002 + d_drop(a->dst_dentry);
23004 + if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23005 + d_drop(a->src_dentry);
23007 + au_update_dbtop(a->dst_dentry);
23008 + if (!a->dst_inode)
23009 + d_drop(a->dst_dentry);
23011 + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23012 + di_write_unlock(a->dst_parent);
23014 + di_write_unlock2(a->src_parent, a->dst_parent);
23016 + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23018 + iput(a->dst_inode);
23020 + au_whtmp_rmdir_free(a->thargs);
23026 diff --git a/fs/aufs/iinfo.c b/fs/aufs/iinfo.c
23027 new file mode 100644
23028 index 00000000000000..0d3a2487643bdc
23030 +++ b/fs/aufs/iinfo.c
23032 +// SPDX-License-Identifier: GPL-2.0
23034 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23036 + * This program is free software; you can redistribute it and/or modify
23037 + * it under the terms of the GNU General Public License as published by
23038 + * the Free Software Foundation; either version 2 of the License, or
23039 + * (at your option) any later version.
23041 + * This program is distributed in the hope that it will be useful,
23042 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23043 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23044 + * GNU General Public License for more details.
23046 + * You should have received a copy of the GNU General Public License
23047 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
23051 + * inode private data
23056 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
23058 + struct inode *h_inode;
23059 + struct au_hinode *hinode;
23061 + IiMustAnyLock(inode);
23063 + hinode = au_hinode(au_ii(inode), bindex);
23064 + h_inode = hinode->hi_inode;
23065 + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
23069 +/* todo: hard/soft set? */
23070 +void au_hiput(struct au_hinode *hinode)
23072 + au_hn_free(hinode);
23073 + dput(hinode->hi_whdentry);
23074 + iput(hinode->hi_inode);
23077 +unsigned int au_hi_flags(struct inode *inode, int isdir)
23079 + unsigned int flags;
23080 + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
23083 + if (au_opt_test(mnt_flags, XINO))
23084 + au_fset_hi(flags, XINO);
23085 + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
23086 + au_fset_hi(flags, HNOTIFY);
23090 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
23091 + struct inode *h_inode, unsigned int flags)
23093 + struct au_hinode *hinode;
23094 + struct inode *hi;
23095 + struct au_iinfo *iinfo = au_ii(inode);
23097 + IiMustWriteLock(inode);
23099 + hinode = au_hinode(iinfo, bindex);
23100 + hi = hinode->hi_inode;
23101 + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
23104 + au_hiput(hinode);
23105 + hinode->hi_inode = h_inode;
23108 + struct super_block *sb = inode->i_sb;
23109 + struct au_branch *br;
23111 + AuDebugOn(inode->i_mode
23112 + && (h_inode->i_mode & S_IFMT)
23113 + != (inode->i_mode & S_IFMT));
23114 + if (bindex == iinfo->ii_btop)
23115 + au_cpup_igen(inode, h_inode);
23116 + br = au_sbr(sb, bindex);
23117 + hinode->hi_id = br->br_id;
23118 + if (au_ftest_hi(flags, XINO)) {
23119 + err = au_xino_write(sb, bindex, h_inode->i_ino,
23121 + if (unlikely(err))
23122 + AuIOErr1("failed au_xino_write() %d\n", err);
23125 + if (au_ftest_hi(flags, HNOTIFY)
23126 + && au_br_hnotifyable(br->br_perm)) {
23127 + err = au_hn_alloc(hinode, inode);
23128 + if (unlikely(err))
23129 + AuIOErr1("au_hn_alloc() %d\n", err);
23134 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
23135 + struct dentry *h_wh)
23137 + struct au_hinode *hinode;
23139 + IiMustWriteLock(inode);
23141 + hinode = au_hinode(au_ii(inode), bindex);
23142 + AuDebugOn(hinode->hi_whdentry);
23143 + hinode->hi_whdentry = h_wh;
23146 +void au_update_iigen(struct inode *inode, int half)
23148 + struct au_iinfo *iinfo;
23149 + struct au_iigen *iigen;
23150 + unsigned int sigen;
23152 + sigen = au_sigen(inode->i_sb);
23153 + iinfo = au_ii(inode);
23154 + iigen = &iinfo->ii_generation;
23155 + spin_lock(&iigen->ig_spin);
23156 + iigen->ig_generation = sigen;
23158 + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
23160 + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
23161 + spin_unlock(&iigen->ig_spin);
23164 +/* it may be called at remount time, too */
23165 +void au_update_ibrange(struct inode *inode, int do_put_zero)
23167 + struct au_iinfo *iinfo;
23168 + aufs_bindex_t bindex, bbot;
23170 + AuDebugOn(au_is_bad_inode(inode));
23171 + IiMustWriteLock(inode);
23173 + iinfo = au_ii(inode);
23174 + if (do_put_zero && iinfo->ii_btop >= 0) {
23175 + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
23177 + struct inode *h_i;
23179 + h_i = au_hinode(iinfo, bindex)->hi_inode;
23182 + && !(h_i->i_state & I_LINKABLE))
23183 + au_set_h_iptr(inode, bindex, NULL, 0);
23187 + iinfo->ii_btop = -1;
23188 + iinfo->ii_bbot = -1;
23189 + bbot = au_sbbot(inode->i_sb);
23190 + for (bindex = 0; bindex <= bbot; bindex++)
23191 + if (au_hinode(iinfo, bindex)->hi_inode) {
23192 + iinfo->ii_btop = bindex;
23195 + if (iinfo->ii_btop >= 0)
23196 + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
23197 + if (au_hinode(iinfo, bindex)->hi_inode) {
23198 + iinfo->ii_bbot = bindex;
23201 + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
23204 +/* ---------------------------------------------------------------------- */
23206 +void au_icntnr_init_once(void *_c)
23208 + struct au_icntnr *c = _c;
23209 + struct au_iinfo *iinfo = &c->iinfo;
23211 + spin_lock_init(&iinfo->ii_generation.ig_spin);
23212 + au_rw_init(&iinfo->ii_rwsem);
23213 + inode_init_once(&c->vfs_inode);
23216 +void au_hinode_init(struct au_hinode *hinode)
23218 + hinode->hi_inode = NULL;
23219 + hinode->hi_id = -1;
23220 + au_hn_init(hinode);
23221 + hinode->hi_whdentry = NULL;
23224 +int au_iinfo_init(struct inode *inode)
23226 + struct au_iinfo *iinfo;
23227 + struct super_block *sb;
23228 + struct au_hinode *hi;
23231 + sb = inode->i_sb;
23232 + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
23233 + nbr = au_sbbot(sb) + 1;
23234 + if (unlikely(nbr <= 0))
23236 + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
23238 + au_lcnt_inc(&au_sbi(sb)->si_ninodes);
23240 + iinfo->ii_hinode = hi;
23241 + for (i = 0; i < nbr; i++, hi++)
23242 + au_hinode_init(hi);
23244 + iinfo->ii_generation.ig_generation = au_sigen(sb);
23245 + iinfo->ii_btop = -1;
23246 + iinfo->ii_bbot = -1;
23247 + iinfo->ii_vdir = NULL;
23253 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
23256 + struct au_hinode *hip;
23258 + AuRwMustWriteLock(&iinfo->ii_rwsem);
23261 + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
23264 + iinfo->ii_hinode = hip;
23265 + i = iinfo->ii_bbot + 1;
23267 + for (; i < nbr; i++, hip++)
23268 + au_hinode_init(hip);
23275 +void au_iinfo_fin(struct inode *inode)
23277 + struct au_iinfo *iinfo;
23278 + struct au_hinode *hi;
23279 + struct super_block *sb;
23280 + aufs_bindex_t bindex, bbot;
23281 + const unsigned char unlinked = !inode->i_nlink;
23283 + AuDebugOn(au_is_bad_inode(inode));
23285 + sb = inode->i_sb;
23286 + au_lcnt_dec(&au_sbi(sb)->si_ninodes);
23287 + if (si_pid_test(sb))
23288 + au_xino_delete_inode(inode, unlinked);
23291 + * it is safe to hide the dependency between sbinfo and
23295 + si_noflush_read_lock(sb);
23296 + au_xino_delete_inode(inode, unlinked);
23297 + si_read_unlock(sb);
23301 + iinfo = au_ii(inode);
23302 + if (iinfo->ii_vdir)
23303 + au_vdir_free(iinfo->ii_vdir);
23305 + bindex = iinfo->ii_btop;
23306 + if (bindex >= 0) {
23307 + hi = au_hinode(iinfo, bindex);
23308 + bbot = iinfo->ii_bbot;
23309 + while (bindex++ <= bbot) {
23310 + if (hi->hi_inode)
23315 + au_kfree_rcu(iinfo->ii_hinode);
23316 + AuRwDestroy(&iinfo->ii_rwsem);
23318 diff --git a/fs/aufs/inode.c b/fs/aufs/inode.c
23319 new file mode 100644
23320 index 00000000000000..7dbf6a6f2feec3
23322 +++ b/fs/aufs/inode.c
23324 +// SPDX-License-Identifier: GPL-2.0
23326 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23328 + * This program is free software; you can redistribute it and/or modify
23329 + * it under the terms of the GNU General Public License as published by
23330 + * the Free Software Foundation; either version 2 of the License, or
23331 + * (at your option) any later version.
23333 + * This program is distributed in the hope that it will be useful,
23334 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23335 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23336 + * GNU General Public License for more details.
23338 + * You should have received a copy of the GNU General Public License
23339 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
23343 + * inode functions
23346 +#include <linux/iversion.h>
23349 +struct inode *au_igrab(struct inode *inode)
23352 + AuDebugOn(!atomic_read(&inode->i_count));
23358 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
23360 + au_cpup_attr_all(inode, /*force*/0);
23361 + au_update_iigen(inode, /*half*/1);
23363 + inode_inc_iversion(inode);
23366 +static int au_ii_refresh(struct inode *inode, int *update)
23370 + aufs_bindex_t bindex, new_bindex;
23371 + struct super_block *sb;
23372 + struct au_iinfo *iinfo;
23373 + struct au_hinode *p, *q, tmp;
23375 + AuDebugOn(au_is_bad_inode(inode));
23376 + IiMustWriteLock(inode);
23379 + sb = inode->i_sb;
23380 + nbr = au_sbbot(sb) + 1;
23381 + type = inode->i_mode & S_IFMT;
23382 + iinfo = au_ii(inode);
23383 + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
23384 + if (unlikely(err))
23387 + AuDebugOn(iinfo->ii_btop < 0);
23388 + p = au_hinode(iinfo, iinfo->ii_btop);
23389 + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
23391 + if (!p->hi_inode)
23394 + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
23395 + new_bindex = au_br_index(sb, p->hi_id);
23396 + if (new_bindex == bindex)
23399 + if (new_bindex < 0) {
23402 + p->hi_inode = NULL;
23406 + if (new_bindex < iinfo->ii_btop)
23407 + iinfo->ii_btop = new_bindex;
23408 + if (iinfo->ii_bbot < new_bindex)
23409 + iinfo->ii_bbot = new_bindex;
23410 + /* swap two lower inode, and loop again */
23411 + q = au_hinode(iinfo, new_bindex);
23415 + if (tmp.hi_inode) {
23420 + au_update_ibrange(inode, /*do_put_zero*/0);
23421 + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
23422 + e = au_dy_irefresh(inode);
23423 + if (unlikely(e && !err))
23431 +void au_refresh_iop(struct inode *inode, int force_getattr)
23434 + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
23435 + const struct inode_operations *iop
23436 + = force_getattr ? aufs_iop : sbi->si_iop_array;
23438 + if (inode->i_op == iop)
23441 + switch (inode->i_mode & S_IFMT) {
23443 + type = AuIop_DIR;
23446 + type = AuIop_SYMLINK;
23449 + type = AuIop_OTHER;
23453 + inode->i_op = iop + type;
23454 + /* unnecessary smp_wmb() */
23457 +int au_refresh_hinode_self(struct inode *inode)
23461 + err = au_ii_refresh(inode, &update);
23463 + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
23469 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
23471 + int err, e, update;
23472 + unsigned int flags;
23474 + aufs_bindex_t bindex, bbot;
23475 + unsigned char isdir;
23476 + struct au_hinode *p;
23477 + struct au_iinfo *iinfo;
23479 + err = au_ii_refresh(inode, &update);
23480 + if (unlikely(err))
23484 + iinfo = au_ii(inode);
23485 + p = au_hinode(iinfo, iinfo->ii_btop);
23486 + mode = (inode->i_mode & S_IFMT);
23487 + isdir = S_ISDIR(mode);
23488 + flags = au_hi_flags(inode, isdir);
23489 + bbot = au_dbbot(dentry);
23490 + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
23491 + struct inode *h_i, *h_inode;
23492 + struct dentry *h_d;
23494 + h_d = au_h_dptr(dentry, bindex);
23495 + if (!h_d || d_is_negative(h_d))
23498 + h_inode = d_inode(h_d);
23499 + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
23500 + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
23501 + h_i = au_h_iptr(inode, bindex);
23503 + if (h_i == h_inode)
23509 + if (bindex < iinfo->ii_btop)
23510 + iinfo->ii_btop = bindex;
23511 + if (iinfo->ii_bbot < bindex)
23512 + iinfo->ii_bbot = bindex;
23513 + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
23516 + au_update_ibrange(inode, /*do_put_zero*/0);
23517 + e = au_dy_irefresh(inode);
23518 + if (unlikely(e && !err))
23521 + au_refresh_hinode_attr(inode, update && isdir);
23528 +static int set_inode(struct inode *inode, struct dentry *dentry)
23531 + unsigned int flags;
23533 + aufs_bindex_t bindex, btop, btail;
23534 + unsigned char isdir;
23535 + struct dentry *h_dentry;
23536 + struct inode *h_inode;
23537 + struct au_iinfo *iinfo;
23538 + const struct inode_operations *iop;
23540 + IiMustWriteLock(inode);
23544 + iop = au_sbi(inode->i_sb)->si_iop_array;
23545 + btop = au_dbtop(dentry);
23546 + h_dentry = au_h_dptr(dentry, btop);
23547 + h_inode = d_inode(h_dentry);
23548 + mode = h_inode->i_mode;
23549 + switch (mode & S_IFMT) {
23551 + btail = au_dbtail(dentry);
23552 + inode->i_op = iop + AuIop_OTHER;
23553 + inode->i_fop = &aufs_file_fop;
23554 + err = au_dy_iaop(inode, btop, h_inode);
23555 + if (unlikely(err))
23560 + btail = au_dbtaildir(dentry);
23561 + inode->i_op = iop + AuIop_DIR;
23562 + inode->i_fop = &aufs_dir_fop;
23565 + btail = au_dbtail(dentry);
23566 + inode->i_op = iop + AuIop_SYMLINK;
23572 + btail = au_dbtail(dentry);
23573 + inode->i_op = iop + AuIop_OTHER;
23574 + init_special_inode(inode, mode, h_inode->i_rdev);
23577 + AuIOErr("Unknown file type 0%o\n", mode);
23582 + /* do not set hnotify for whiteouted dirs (SHWH mode) */
23583 + flags = au_hi_flags(inode, isdir);
23584 + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
23585 + && au_ftest_hi(flags, HNOTIFY)
23586 + && dentry->d_name.len > AUFS_WH_PFX_LEN
23587 + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
23588 + au_fclr_hi(flags, HNOTIFY);
23589 + iinfo = au_ii(inode);
23590 + iinfo->ii_btop = btop;
23591 + iinfo->ii_bbot = btail;
23592 + for (bindex = btop; bindex <= btail; bindex++) {
23593 + h_dentry = au_h_dptr(dentry, bindex);
23595 + au_set_h_iptr(inode, bindex,
23596 + au_igrab(d_inode(h_dentry)), flags);
23598 + au_cpup_attr_all(inode, /*force*/1);
23600 + * to force calling aufs_get_inode_acl() every time,
23601 + * do not call cache_no_acl() for aufs inode.
23609 + * successful returns with iinfo write_locked
23611 + * zero: success, matched
23612 + * plus: no error, but unmatched
23614 +static int reval_inode(struct inode *inode, struct dentry *dentry)
23617 + unsigned int gen, igflags;
23618 + aufs_bindex_t bindex, bbot;
23619 + struct inode *h_inode, *h_dinode;
23620 + struct dentry *h_dentry;
23623 + * before this function, if aufs got any iinfo lock, it must be only
23624 + * one, the parent dir.
23625 + * it can happen by UDBA and the obsoleted inode number.
23628 + if (unlikely(inode->i_ino == parent_ino(dentry)))
23632 + ii_write_lock_new_child(inode);
23633 + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
23634 + h_dinode = d_inode(h_dentry);
23635 + bbot = au_ibbot(inode);
23636 + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
23637 + h_inode = au_h_iptr(inode, bindex);
23638 + if (!h_inode || h_inode != h_dinode)
23642 + gen = au_iigen(inode, &igflags);
23643 + if (gen == au_digen(dentry)
23644 + && !au_ig_ftest(igflags, HALF_REFRESHED))
23647 + /* fully refresh inode using dentry */
23648 + err = au_refresh_hinode(inode, dentry);
23650 + au_update_iigen(inode, /*half*/0);
23654 + if (unlikely(err))
23655 + ii_write_unlock(inode);
23660 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
23661 + unsigned int d_type, ino_t *ino)
23664 + const int isnondir = d_type != DT_DIR;
23666 + /* prevent hardlinked inode number from race condition */
23668 + err = au_xinondir_enter(sb, bindex, h_ino, &idx);
23669 + if (unlikely(err))
23673 + err = au_xino_read(sb, bindex, h_ino, ino);
23674 + if (unlikely(err))
23675 + goto out_xinondir;
23679 + *ino = au_xino_new_ino(sb);
23680 + if (unlikely(!*ino))
23681 + goto out_xinondir;
23682 + err = au_xino_write(sb, bindex, h_ino, *ino);
23683 + if (unlikely(err))
23684 + goto out_xinondir;
23688 + if (isnondir && idx >= 0)
23689 + au_xinondir_leave(sb, bindex, h_ino, idx);
23694 +/* successful returns with iinfo write_locked */
23695 +/* todo: return with unlocked? */
23696 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
23698 + struct inode *inode, *h_inode;
23699 + struct dentry *h_dentry;
23700 + struct super_block *sb;
23701 + ino_t h_ino, ino;
23702 + int err, idx, hlinked;
23703 + aufs_bindex_t btop;
23705 + sb = dentry->d_sb;
23706 + btop = au_dbtop(dentry);
23707 + h_dentry = au_h_dptr(dentry, btop);
23708 + h_inode = d_inode(h_dentry);
23709 + h_ino = h_inode->i_ino;
23710 + hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
23714 + * stop 'race'-ing between hardlinks under different
23718 + err = au_xinondir_enter(sb, btop, h_ino, &idx);
23719 + inode = ERR_PTR(err);
23720 + if (unlikely(err))
23724 + err = au_xino_read(sb, btop, h_ino, &ino);
23725 + inode = ERR_PTR(err);
23726 + if (unlikely(err))
23727 + goto out_xinondir;
23730 + ino = au_xino_new_ino(sb);
23731 + if (unlikely(!ino)) {
23732 + inode = ERR_PTR(-EIO);
23733 + goto out_xinondir;
23737 + AuDbg("i%lu\n", (unsigned long)ino);
23738 + inode = au_iget_locked(sb, ino);
23739 + err = PTR_ERR(inode);
23740 + if (IS_ERR(inode))
23741 + goto out_xinondir;
23743 + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
23744 + if (inode->i_state & I_NEW) {
23745 + ii_write_lock_new_child(inode);
23746 + err = set_inode(inode, dentry);
23748 + unlock_new_inode(inode);
23749 + goto out_xinondir; /* success */
23753 + * iget_failed() calls iput(), but we need to call
23754 + * ii_write_unlock() after iget_failed(). so dirty hack for
23757 + atomic_inc(&inode->i_count);
23758 + iget_failed(inode);
23759 + ii_write_unlock(inode);
23760 + au_xino_write(sb, btop, h_ino, /*ino*/0);
23761 + /* ignore this error */
23763 + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
23765 + * horrible race condition between lookup, readdir and copyup
23766 + * (or something).
23768 + if (hlinked && idx >= 0)
23769 + au_xinondir_leave(sb, btop, h_ino, idx);
23770 + err = reval_inode(inode, dentry);
23771 + if (unlikely(err < 0)) {
23776 + goto out; /* success */
23777 + else if (hlinked && idx >= 0) {
23778 + err = au_xinondir_enter(sb, btop, h_ino, &idx);
23779 + if (unlikely(err)) {
23781 + inode = ERR_PTR(err);
23787 + if (unlikely(au_test_fs_unique_ino(h_inode)))
23788 + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
23789 + " b%d, %s, %pd, hi%lu, i%lu.\n",
23790 + btop, au_sbtype(h_dentry->d_sb), dentry,
23791 + (unsigned long)h_ino, (unsigned long)ino);
23793 + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
23796 + if (hlinked && idx >= 0)
23797 + au_xinondir_leave(sb, btop, h_ino, idx);
23803 + inode = ERR_PTR(err);
23805 + if (hlinked && idx >= 0)
23806 + au_xinondir_leave(sb, btop, h_ino, idx);
23811 +/* ---------------------------------------------------------------------- */
23813 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
23814 + struct inode *inode)
23817 + struct inode *hi;
23819 + err = au_br_rdonly(au_sbr(sb, bindex));
23821 + /* pseudo-link after flushed may happen out of bounds */
23824 + && au_ibtop(inode) <= bindex
23825 + && bindex <= au_ibbot(inode)) {
23827 + * permission check is unnecessary since vfsub routine
23828 + * will be called later
23830 + hi = au_h_iptr(inode, bindex);
23832 + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
23838 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
23841 + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
23843 + return inode_permission(h_idmap, h_inode, mask);
23846 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
23849 + if (au_test_nfs(h_inode->i_sb)
23850 + && (mask & MAY_WRITE)
23851 + && S_ISDIR(h_inode->i_mode))
23852 + mask |= MAY_READ; /* force permission check */
23853 + return au_test_h_perm(h_idmap, h_inode, mask);
23855 diff --git a/fs/aufs/inode.h b/fs/aufs/inode.h
23856 new file mode 100644
23857 index 00000000000000..76c3bd49674a29
23859 +++ b/fs/aufs/inode.h
23861 +/* SPDX-License-Identifier: GPL-2.0 */
23863 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23865 + * This program is free software; you can redistribute it and/or modify
23866 + * it under the terms of the GNU General Public License as published by
23867 + * the Free Software Foundation; either version 2 of the License, or
23868 + * (at your option) any later version.
23870 + * This program is distributed in the hope that it will be useful,
23871 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23872 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23873 + * GNU General Public License for more details.
23875 + * You should have received a copy of the GNU General Public License
23876 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
23880 + * inode operations
23883 +#ifndef __AUFS_INODE_H__
23884 +#define __AUFS_INODE_H__
23888 +#include <linux/fsnotify.h>
23889 +#include "rwsem.h"
23893 +struct au_hnotify {
23894 +#ifdef CONFIG_AUFS_HNOTIFY
23895 +#ifdef CONFIG_AUFS_HFSNOTIFY
23896 + /* never use fsnotify_add_vfsmount_mark() */
23897 + struct fsnotify_mark hn_mark;
23899 + struct inode *hn_aufs_inode; /* no get/put */
23900 + struct rcu_head rcu;
23902 +} ____cacheline_aligned_in_smp;
23904 +struct au_hinode {
23905 + struct inode *hi_inode;
23906 + aufs_bindex_t hi_id;
23907 +#ifdef CONFIG_AUFS_HNOTIFY
23908 + struct au_hnotify *hi_notify;
23911 + /* reference to the copied-up whiteout with get/put */
23912 + struct dentry *hi_whdentry;
23916 +#define AuIG_HALF_REFRESHED 1
23917 +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
23918 +#define au_ig_fset(flags, name) \
23919 + do { (flags) |= AuIG_##name; } while (0)
23920 +#define au_ig_fclr(flags, name) \
23921 + do { (flags) &= ~AuIG_##name; } while (0)
23924 + spinlock_t ig_spin;
23925 + __u32 ig_generation, ig_flags;
23930 + struct au_iigen ii_generation;
23931 + struct super_block *ii_hsb1; /* no get/put */
23933 + struct au_rwsem ii_rwsem;
23934 + aufs_bindex_t ii_btop, ii_bbot;
23936 + struct au_hinode *ii_hinode;
23937 + struct au_vdir *ii_vdir;
23940 +struct au_icntnr {
23941 + struct au_iinfo iinfo;
23942 + struct inode vfs_inode;
23943 + struct hlist_bl_node plink;
23944 + struct rcu_head rcu;
23945 +} ____cacheline_aligned_in_smp;
23947 +/* au_pin flags */
23948 +#define AuPin_DI_LOCKED 1
23949 +#define AuPin_MNT_WRITE (1 << 1)
23950 +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
23951 +#define au_fset_pin(flags, name) \
23952 + do { (flags) |= AuPin_##name; } while (0)
23953 +#define au_fclr_pin(flags, name) \
23954 + do { (flags) &= ~AuPin_##name; } while (0)
23958 + struct dentry *dentry;
23959 + unsigned int udba;
23960 + unsigned char lsc_di, lsc_hi, flags;
23961 + aufs_bindex_t bindex;
23964 + struct dentry *parent;
23965 + struct au_hinode *hdir;
23966 + struct vfsmount *h_mnt;
23968 + /* temporary unlock/relock for copyup */
23969 + struct dentry *h_dentry, *h_parent;
23970 + struct au_branch *br;
23971 + struct task_struct *task;
23974 +void au_pin_hdir_unlock(struct au_pin *p);
23975 +int au_pin_hdir_lock(struct au_pin *p);
23976 +int au_pin_hdir_relock(struct au_pin *p);
23977 +void au_pin_hdir_acquire_nest(struct au_pin *p);
23978 +void au_pin_hdir_release(struct au_pin *p);
23980 +/* ---------------------------------------------------------------------- */
23982 +static inline struct au_iinfo *au_ii(struct inode *inode)
23984 + BUG_ON(is_bad_inode(inode));
23985 + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
23988 +/* ---------------------------------------------------------------------- */
23991 +struct inode *au_igrab(struct inode *inode);
23992 +void au_refresh_iop(struct inode *inode, int force_getattr);
23993 +int au_refresh_hinode_self(struct inode *inode);
23994 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
23995 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
23996 + unsigned int d_type, ino_t *ino);
23997 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
23998 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
23999 + struct inode *inode);
24000 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
24002 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
24005 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
24006 + ino_t h_ino, unsigned int d_type, ino_t *ino)
24008 +#ifdef CONFIG_AUFS_SHWH
24009 + return au_ino(sb, bindex, h_ino, d_type, ino);
24022 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
24023 + aufs_iop_nogetattr[AuIop_Last];
24025 +/* au_wr_dir flags */
24026 +#define AuWrDir_ADD_ENTRY 1
24027 +#define AuWrDir_ISDIR (1 << 1)
24028 +#define AuWrDir_TMPFILE (1 << 2)
24029 +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
24030 +#define au_fset_wrdir(flags, name) \
24031 + do { (flags) |= AuWrDir_##name; } while (0)
24032 +#define au_fclr_wrdir(flags, name) \
24033 + do { (flags) &= ~AuWrDir_##name; } while (0)
24035 +struct au_wr_dir_args {
24036 + aufs_bindex_t force_btgt;
24037 + unsigned char flags;
24039 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
24040 + struct au_wr_dir_args *args);
24042 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
24043 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
24044 + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
24045 + unsigned int udba, unsigned char flags);
24046 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
24047 + unsigned int udba, unsigned char flags) __must_check;
24048 +int au_do_pin(struct au_pin *pin) __must_check;
24049 +void au_unpin(struct au_pin *pin);
24050 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
24052 +#define AuIcpup_DID_CPUP 1
24053 +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
24054 +#define au_fset_icpup(flags, name) \
24055 + do { (flags) |= AuIcpup_##name; } while (0)
24056 +#define au_fclr_icpup(flags, name) \
24057 + do { (flags) &= ~AuIcpup_##name; } while (0)
24059 +struct au_icpup_args {
24060 + unsigned char flags;
24061 + unsigned char pin_flags;
24062 + aufs_bindex_t btgt;
24063 + unsigned int udba;
24064 + struct au_pin pin;
24065 + struct path h_path;
24066 + struct inode *h_inode;
24069 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
24070 + struct au_icpup_args *a);
24072 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
24073 + struct path *h_path, int locked);
24076 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
24077 + struct dentry *h_parent, int isdir);
24078 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
24079 + struct dentry *dentry, umode_t mode, dev_t dev);
24080 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
24081 + struct dentry *dentry, const char *symname);
24082 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
24083 + struct dentry *dentry, umode_t mode, bool want_excl);
24084 +struct vfsub_aopen_args;
24085 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
24086 + struct vfsub_aopen_args *args);
24087 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
24088 + struct file *file, umode_t mode);
24089 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
24090 + struct dentry *dentry);
24091 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
24092 + struct dentry *dentry, umode_t mode);
24095 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
24096 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
24097 + struct dentry *h_parent, int isdir);
24098 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
24099 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
24102 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
24103 +int aufs_rename(struct mnt_idmap *idmap,
24104 + struct inode *_src_dir, struct dentry *_src_dentry,
24105 + struct inode *_dst_dir, struct dentry *_dst_dentry,
24106 + unsigned int _flags);
24109 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
24110 +void au_hiput(struct au_hinode *hinode);
24111 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
24112 + struct dentry *h_wh);
24113 +unsigned int au_hi_flags(struct inode *inode, int isdir);
24115 +/* hinode flags */
24116 +#define AuHi_XINO 1
24117 +#define AuHi_HNOTIFY (1 << 1)
24118 +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
24119 +#define au_fset_hi(flags, name) \
24120 + do { (flags) |= AuHi_##name; } while (0)
24121 +#define au_fclr_hi(flags, name) \
24122 + do { (flags) &= ~AuHi_##name; } while (0)
24124 +#ifndef CONFIG_AUFS_HNOTIFY
24125 +#undef AuHi_HNOTIFY
24126 +#define AuHi_HNOTIFY 0
24129 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
24130 + struct inode *h_inode, unsigned int flags);
24132 +void au_update_iigen(struct inode *inode, int half);
24133 +void au_update_ibrange(struct inode *inode, int do_put_zero);
24135 +void au_icntnr_init_once(void *_c);
24136 +void au_hinode_init(struct au_hinode *hinode);
24137 +int au_iinfo_init(struct inode *inode);
24138 +void au_iinfo_fin(struct inode *inode);
24139 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
24141 +#ifdef CONFIG_PROC_FS
24143 +int au_plink_maint(struct super_block *sb, int flags);
24145 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
24146 +int au_plink_maint_enter(struct super_block *sb);
24147 +#ifdef CONFIG_AUFS_DEBUG
24148 +void au_plink_list(struct super_block *sb);
24150 +AuStubVoid(au_plink_list, struct super_block *sb)
24152 +int au_plink_test(struct inode *inode);
24153 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
24154 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
24155 + struct dentry *h_dentry);
24156 +void au_plink_put(struct super_block *sb, int verbose);
24157 +void au_plink_clean(struct super_block *sb, int verbose);
24158 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
24160 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
24161 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
24162 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
24163 +AuStubVoid(au_plink_list, struct super_block *sb);
24164 +AuStubInt0(au_plink_test, struct inode *inode);
24165 +AuStub(struct dentry *, au_plink_lkup, return NULL,
24166 + struct inode *inode, aufs_bindex_t bindex);
24167 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
24168 + struct dentry *h_dentry);
24169 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
24170 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
24171 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
24172 +#endif /* CONFIG_PROC_FS */
24174 +#ifdef CONFIG_AUFS_XATTR
24176 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
24177 + unsigned int verbose);
24178 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
24179 +void au_xattr_init(struct super_block *sb);
24181 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
24182 + int ignore_flags, unsigned int verbose);
24183 +AuStubVoid(au_xattr_init, struct super_block *sb);
24186 +#ifdef CONFIG_FS_POSIX_ACL
24187 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
24188 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
24189 + struct dentry *dentry, int type);
24190 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
24191 + struct posix_acl *acl, int type);
24194 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
24200 +struct au_sxattr {
24204 + const char *name;
24205 + const void *value;
24210 + struct posix_acl *acl;
24215 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
24216 + struct au_sxattr *arg);
24219 +/* ---------------------------------------------------------------------- */
24221 +/* lock subclass for iinfo */
24223 + AuLsc_II_CHILD, /* child first */
24224 + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
24225 + AuLsc_II_CHILD3, /* copyup dirs */
24226 + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
24227 + AuLsc_II_PARENT2,
24228 + AuLsc_II_PARENT3, /* copyup dirs */
24229 + AuLsc_II_NEW_CHILD
24233 + * ii_read_lock_child, ii_write_lock_child,
24234 + * ii_read_lock_child2, ii_write_lock_child2,
24235 + * ii_read_lock_child3, ii_write_lock_child3,
24236 + * ii_read_lock_parent, ii_write_lock_parent,
24237 + * ii_read_lock_parent2, ii_write_lock_parent2,
24238 + * ii_read_lock_parent3, ii_write_lock_parent3,
24239 + * ii_read_lock_new_child, ii_write_lock_new_child,
24241 +#define AuReadLockFunc(name, lsc) \
24242 +static inline void ii_read_lock_##name(struct inode *i) \
24244 + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
24247 +#define AuWriteLockFunc(name, lsc) \
24248 +static inline void ii_write_lock_##name(struct inode *i) \
24250 + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
24253 +#define AuRWLockFuncs(name, lsc) \
24254 + AuReadLockFunc(name, lsc) \
24255 + AuWriteLockFunc(name, lsc)
24257 +AuRWLockFuncs(child, CHILD);
24258 +AuRWLockFuncs(child2, CHILD2);
24259 +AuRWLockFuncs(child3, CHILD3);
24260 +AuRWLockFuncs(parent, PARENT);
24261 +AuRWLockFuncs(parent2, PARENT2);
24262 +AuRWLockFuncs(parent3, PARENT3);
24263 +AuRWLockFuncs(new_child, NEW_CHILD);
24265 +#undef AuReadLockFunc
24266 +#undef AuWriteLockFunc
24267 +#undef AuRWLockFuncs
24269 +#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
24270 +#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
24271 +#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
24273 +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
24274 +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
24275 +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
24277 +/* ---------------------------------------------------------------------- */
24279 +static inline void au_icntnr_init(struct au_icntnr *c)
24281 +#ifdef CONFIG_AUFS_DEBUG
24282 + c->vfs_inode.i_mode = 0;
24286 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
24288 + unsigned int gen;
24289 + struct au_iinfo *iinfo;
24290 + struct au_iigen *iigen;
24292 + iinfo = au_ii(inode);
24293 + iigen = &iinfo->ii_generation;
24294 + spin_lock(&iigen->ig_spin);
24296 + *igflags = iigen->ig_flags;
24297 + gen = iigen->ig_generation;
24298 + spin_unlock(&iigen->ig_spin);
24303 +/* tiny test for inode number */
24304 +/* tmpfs generation is too rough */
24305 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
24307 + struct au_iinfo *iinfo;
24309 + iinfo = au_ii(inode);
24310 + AuRwMustAnyLock(&iinfo->ii_rwsem);
24311 + return !(iinfo->ii_hsb1 == h_inode->i_sb
24312 + && iinfo->ii_higen == h_inode->i_generation);
24315 +static inline void au_iigen_dec(struct inode *inode)
24317 + struct au_iinfo *iinfo;
24318 + struct au_iigen *iigen;
24320 + iinfo = au_ii(inode);
24321 + iigen = &iinfo->ii_generation;
24322 + spin_lock(&iigen->ig_spin);
24323 + iigen->ig_generation--;
24324 + spin_unlock(&iigen->ig_spin);
24327 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
24332 + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
24338 +/* ---------------------------------------------------------------------- */
24340 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
24341 + aufs_bindex_t bindex)
24343 + return iinfo->ii_hinode + bindex;
24346 +static inline int au_is_bad_inode(struct inode *inode)
24348 + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
24351 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
24352 + aufs_bindex_t bindex)
24354 + IiMustAnyLock(inode);
24355 + return au_hinode(au_ii(inode), bindex)->hi_id;
24358 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
24360 + IiMustAnyLock(inode);
24361 + return au_ii(inode)->ii_btop;
24364 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
24366 + IiMustAnyLock(inode);
24367 + return au_ii(inode)->ii_bbot;
24370 +static inline struct au_vdir *au_ivdir(struct inode *inode)
24372 + IiMustAnyLock(inode);
24373 + return au_ii(inode)->ii_vdir;
24376 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
24378 + IiMustAnyLock(inode);
24379 + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
24382 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
24384 + IiMustWriteLock(inode);
24385 + au_ii(inode)->ii_btop = bindex;
24388 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
24390 + IiMustWriteLock(inode);
24391 + au_ii(inode)->ii_bbot = bindex;
24394 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
24396 + IiMustWriteLock(inode);
24397 + au_ii(inode)->ii_vdir = vdir;
24400 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
24402 + IiMustAnyLock(inode);
24403 + return au_hinode(au_ii(inode), bindex);
24406 +/* ---------------------------------------------------------------------- */
24408 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
24411 + return pin->parent;
24415 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
24417 + if (pin && pin->hdir)
24418 + return pin->hdir->hi_inode;
24422 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
24425 + return pin->hdir;
24429 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
24432 + pin->dentry = dentry;
24435 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
24436 + unsigned char lflag)
24440 + au_fset_pin(pin->flags, DI_LOCKED);
24442 + au_fclr_pin(pin->flags, DI_LOCKED);
24446 +#if 0 /* reserved */
24447 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
24450 + dput(pin->parent);
24451 + pin->parent = dget(parent);
24456 +/* ---------------------------------------------------------------------- */
24459 +#ifdef CONFIG_AUFS_HNOTIFY
24460 +struct au_hnotify_op {
24461 + void (*ctl)(struct au_hinode *hinode, int do_set);
24462 + int (*alloc)(struct au_hinode *hinode);
24465 + * if it returns true, the caller should free hinode->hi_notify,
24466 + * otherwise ->free() frees it.
24468 + int (*free)(struct au_hinode *hinode,
24469 + struct au_hnotify *hn) __must_check;
24471 + void (*fin)(void);
24472 + int (*init)(void);
24474 + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
24475 + void (*fin_br)(struct au_branch *br);
24476 + int (*init_br)(struct au_branch *br, int perm);
24480 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
24481 +void au_hn_free(struct au_hinode *hinode);
24482 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
24483 +void au_hn_reset(struct inode *inode, unsigned int flags);
24484 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
24485 + const struct qstr *h_child_qstr, struct inode *h_child_inode);
24486 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
24487 +int au_hnotify_init_br(struct au_branch *br, int perm);
24488 +void au_hnotify_fin_br(struct au_branch *br);
24489 +int __init au_hnotify_init(void);
24490 +void au_hnotify_fin(void);
24493 +extern const struct au_hnotify_op au_hnotify_op;
24496 +void au_hn_init(struct au_hinode *hinode)
24498 + hinode->hi_notify = NULL;
24501 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
24503 + return hinode->hi_notify;
24507 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
24508 + struct au_hinode *hinode __maybe_unused,
24509 + struct inode *inode __maybe_unused)
24510 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
24511 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
24512 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
24513 + int do_set __maybe_unused)
24514 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
24515 + unsigned int flags __maybe_unused)
24516 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
24517 + struct au_branch *br __maybe_unused,
24518 + int perm __maybe_unused)
24519 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
24520 + int perm __maybe_unused)
24521 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
24522 +AuStubInt0(__init au_hnotify_init, void)
24523 +AuStubVoid(au_hnotify_fin, void)
24524 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
24525 +#endif /* CONFIG_AUFS_HNOTIFY */
24527 +static inline void au_hn_suspend(struct au_hinode *hdir)
24529 + au_hn_ctl(hdir, /*do_set*/0);
24532 +static inline void au_hn_resume(struct au_hinode *hdir)
24534 + au_hn_ctl(hdir, /*do_set*/1);
24537 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
24539 + inode_lock(hdir->hi_inode);
24540 + au_hn_suspend(hdir);
24543 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
24544 + unsigned int sc __maybe_unused)
24546 + inode_lock_nested(hdir->hi_inode, sc);
24547 + au_hn_suspend(hdir);
24550 +#if 0 /* unused */
24551 +#include "vfsub.h"
24552 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
24555 + inode_lock_shared_nested(hdir->hi_inode, sc);
24556 + au_hn_suspend(hdir);
24560 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
24562 + au_hn_resume(hdir);
24563 + inode_unlock(hdir->hi_inode);
24566 +#endif /* __KERNEL__ */
24567 +#endif /* __AUFS_INODE_H__ */
24568 diff --git a/fs/aufs/ioctl.c b/fs/aufs/ioctl.c
24569 new file mode 100644
24570 index 00000000000000..2d8d91084099f9
24572 +++ b/fs/aufs/ioctl.c
24574 +// SPDX-License-Identifier: GPL-2.0
24576 + * Copyright (C) 2005-2022 Junjiro R. Okajima
24578 + * This program is free software; you can redistribute it and/or modify
24579 + * it under the terms of the GNU General Public License as published by
24580 + * the Free Software Foundation; either version 2 of the License, or
24581 + * (at your option) any later version.
24583 + * This program is distributed in the hope that it will be useful,
24584 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24585 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24586 + * GNU General Public License for more details.
24588 + * You should have received a copy of the GNU General Public License
24589 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
24594 + * plink-management and readdir in userspace.
24595 + * assist the pathconf(3) wrapper library.
24597 + * File-based Hierarchical Storage Management.
24600 +#include <linux/compat.h>
24601 +#include <linux/file.h>
24604 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
24607 + aufs_bindex_t wbi, bindex, bbot;
24608 + struct file *h_file;
24609 + struct super_block *sb;
24610 + struct dentry *root;
24611 + struct au_branch *br;
24612 + struct aufs_wbr_fd wbrfd = {
24613 + .oflags = au_dir_roflags,
24616 + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
24617 + | O_NOATIME | O_CLOEXEC;
24619 + AuDebugOn(wbrfd.oflags & ~valid);
24622 + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
24623 + if (unlikely(err)) {
24629 + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
24630 + wbrfd.oflags |= au_dir_roflags;
24631 + AuDbg("0%o\n", wbrfd.oflags);
24632 + if (unlikely(wbrfd.oflags & ~valid))
24636 + fd = get_unused_fd_flags(0);
24638 + if (unlikely(fd < 0))
24641 + h_file = ERR_PTR(-EINVAL);
24644 + sb = path->dentry->d_sb;
24645 + root = sb->s_root;
24646 + aufs_read_lock(root, AuLock_IR);
24647 + bbot = au_sbbot(sb);
24648 + if (wbrfd.brid >= 0) {
24649 + wbi = au_br_index(sb, wbrfd.brid);
24650 + if (unlikely(wbi < 0 || wbi > bbot))
24654 + h_file = ERR_PTR(-ENOENT);
24655 + br = au_sbr(sb, wbi);
24656 + if (!au_br_writable(br->br_perm)) {
24660 + bindex = wbi + 1;
24662 + for (; bindex <= bbot; bindex++) {
24663 + br = au_sbr(sb, bindex);
24664 + if (au_br_writable(br->br_perm)) {
24666 + br = au_sbr(sb, wbi);
24671 + AuDbg("wbi %d\n", wbi);
24673 + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
24677 + aufs_read_unlock(root, AuLock_IR);
24678 + err = PTR_ERR(h_file);
24679 + if (IS_ERR(h_file))
24682 + au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
24683 + fd_install(fd, h_file);
24685 + goto out; /* success */
24688 + put_unused_fd(fd);
24694 +/* ---------------------------------------------------------------------- */
24696 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
24699 + struct dentry *dentry;
24702 + case AUFS_CTL_RDU:
24703 + case AUFS_CTL_RDU_INO:
24704 + err = au_rdu_ioctl(file, cmd, arg);
24707 + case AUFS_CTL_WBR_FD:
24708 + err = au_wbr_fd(&file->f_path, (void __user *)arg);
24711 + case AUFS_CTL_IBUSY:
24712 + err = au_ibusy_ioctl(file, arg);
24715 + case AUFS_CTL_BRINFO:
24716 + err = au_brinfo_ioctl(file, arg);
24719 + case AUFS_CTL_FHSM_FD:
24720 + dentry = file->f_path.dentry;
24721 + if (IS_ROOT(dentry))
24722 + err = au_fhsm_fd(dentry->d_sb, arg);
24728 + /* do not call the lower */
24729 + AuDbg("0x%x\n", cmd);
24737 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
24742 + case AUFS_CTL_MVDOWN:
24743 + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
24746 + case AUFS_CTL_WBR_FD:
24747 + err = au_wbr_fd(&file->f_path, (void __user *)arg);
24751 + /* do not call the lower */
24752 + AuDbg("0x%x\n", cmd);
24760 +#ifdef CONFIG_COMPAT
24761 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
24762 + unsigned long arg)
24767 + case AUFS_CTL_RDU:
24768 + case AUFS_CTL_RDU_INO:
24769 + err = au_rdu_compat_ioctl(file, cmd, arg);
24772 + case AUFS_CTL_IBUSY:
24773 + err = au_ibusy_compat_ioctl(file, arg);
24776 + case AUFS_CTL_BRINFO:
24777 + err = au_brinfo_compat_ioctl(file, arg);
24781 + err = aufs_ioctl_dir(file, cmd, arg);
24788 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
24789 + unsigned long arg)
24791 + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
24794 diff --git a/fs/aufs/lcnt.h b/fs/aufs/lcnt.h
24795 new file mode 100644
24796 index 00000000000000..6dc37108565fa2
24798 +++ b/fs/aufs/lcnt.h
24800 +/* SPDX-License-Identifier: GPL-2.0 */
24802 + * Copyright (C) 2018-2022 Junjiro R. Okajima
24804 + * This program is free software; you can redistribute it and/or modify
24805 + * it under the terms of the GNU General Public License as published by
24806 + * the Free Software Foundation; either version 2 of the License, or
24807 + * (at your option) any later version.
24809 + * This program is distributed in the hope that it will be useful,
24810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24812 + * GNU General Public License for more details.
24814 + * You should have received a copy of the GNU General Public License
24815 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
24819 + * simple long counter wrapper
24822 +#ifndef __AUFS_LCNT_H__
24823 +#define __AUFS_LCNT_H__
24827 +#include "debug.h"
24829 +#define AuLCntATOMIC 1
24830 +#define AuLCntPCPUCNT 2
24832 + * why does percpu_refcount require extra synchronize_rcu()s in
24833 + * au_br_do_free()
24835 +#define AuLCntPCPUREF 3
24837 +/* #define AuLCntChosen AuLCntATOMIC */
24838 +#define AuLCntChosen AuLCntPCPUCNT
24839 +/* #define AuLCntChosen AuLCntPCPUREF */
24841 +#if AuLCntChosen == AuLCntATOMIC
24842 +#include <linux/atomic.h>
24844 +typedef atomic_long_t au_lcnt_t;
24846 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24848 + atomic_long_set(cnt, 0);
24852 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24857 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24858 + int do_sync __maybe_unused)
24863 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24865 + atomic_long_inc(cnt);
24868 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24870 + atomic_long_dec(cnt);
24873 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24875 + return atomic_long_read(cnt);
24879 +#if AuLCntChosen == AuLCntPCPUCNT
24880 +#include <linux/percpu_counter.h>
24882 +typedef struct percpu_counter au_lcnt_t;
24884 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24886 + return percpu_counter_init(cnt, 0, GFP_NOFS);
24889 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24894 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24896 + percpu_counter_destroy(cnt);
24899 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24901 + percpu_counter_inc(cnt);
24904 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24906 + percpu_counter_dec(cnt);
24909 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24913 + n = percpu_counter_sum(cnt);
24915 + if (LONG_MAX != LLONG_MAX
24917 + AuWarn1("%s\n", "wrap-around");
24923 +#if AuLCntChosen == AuLCntPCPUREF
24924 +#include <linux/percpu-refcount.h>
24926 +typedef struct percpu_ref au_lcnt_t;
24928 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24931 + release = percpu_ref_exit;
24932 + return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24935 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24937 + synchronize_rcu();
24940 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24942 + percpu_ref_kill(cnt);
24944 + au_lcnt_wait_for_fin(cnt);
24947 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24949 + percpu_ref_get(cnt);
24952 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24954 + percpu_ref_put(cnt);
24958 + * avoid calling this func as possible.
24960 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24964 + percpu_ref_switch_to_atomic_sync(cnt);
24965 + l = atomic_long_read(&cnt->count);
24967 + percpu_ref_switch_to_percpu(cnt);
24969 + /* percpu_ref is initialized by 1 instead of 0 */
24974 +#ifdef CONFIG_AUFS_DEBUG
24975 +#define AuLCntZero(val) do { \
24978 + AuDbg("%s = %ld\n", #val, l); \
24981 +#define AuLCntZero(val) do {} while (0)
24984 +#endif /* __KERNEL__ */
24985 +#endif /* __AUFS_LCNT_H__ */
24986 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
24987 new file mode 100644
24988 index 00000000000000..38319058859501
24990 +++ b/fs/aufs/loop.c
24992 +// SPDX-License-Identifier: GPL-2.0
24994 + * Copyright (C) 2005-2022 Junjiro R. Okajima
24996 + * This program is free software; you can redistribute it and/or modify
24997 + * it under the terms of the GNU General Public License as published by
24998 + * the Free Software Foundation; either version 2 of the License, or
24999 + * (at your option) any later version.
25001 + * This program is distributed in the hope that it will be useful,
25002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25004 + * GNU General Public License for more details.
25006 + * You should have received a copy of the GNU General Public License
25007 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
25011 + * support for loopback block device as a branch
25016 +/* added into drivers/block/loop.c */
25017 +static struct file *(*backing_file_func)(struct super_block *sb);
25020 + * test if two lower dentries have overlapping branches.
25022 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25024 + struct super_block *h_sb;
25025 + struct file *backing_file;
25027 + if (unlikely(!backing_file_func)) {
25028 + /* don't load "loop" module here */
25029 + backing_file_func = symbol_get(loop_backing_file);
25030 + if (unlikely(!backing_file_func))
25031 + /* "loop" module is not loaded */
25035 + h_sb = h_adding->d_sb;
25036 + backing_file = backing_file_func(h_sb);
25037 + if (!backing_file)
25040 + h_adding = backing_file->f_path.dentry;
25042 + * h_adding can be local NFS.
25043 + * in this case aufs cannot detect the loop.
25045 + if (unlikely(h_adding->d_sb == sb))
25047 + return !!au_test_subdir(h_adding, sb->s_root);
25050 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25051 +int au_test_loopback_kthread(void)
25054 + struct task_struct *tsk = current;
25055 + char c, comm[sizeof(tsk->comm)];
25058 + if (tsk->flags & PF_KTHREAD) {
25059 + get_task_comm(comm, tsk);
25061 + ret = ('0' <= c && c <= '9'
25062 + && !strncmp(comm, "loop", 4));
25068 +/* ---------------------------------------------------------------------- */
25070 +#define au_warn_loopback_step 16
25071 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25072 +static unsigned long *au_warn_loopback_array;
25074 +void au_warn_loopback(struct super_block *h_sb)
25076 + int i, new_nelem;
25077 + unsigned long *a, magic;
25078 + static DEFINE_SPINLOCK(spin);
25080 + magic = h_sb->s_magic;
25081 + spin_lock(&spin);
25082 + a = au_warn_loopback_array;
25083 + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25084 + if (a[i] == magic) {
25085 + spin_unlock(&spin);
25089 + /* h_sb is new to us, print it */
25090 + if (i < au_warn_loopback_nelem) {
25095 + /* expand the array */
25096 + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25097 + a = au_kzrealloc(au_warn_loopback_array,
25098 + au_warn_loopback_nelem * sizeof(unsigned long),
25099 + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25100 + /*may_shrink*/0);
25102 + au_warn_loopback_nelem = new_nelem;
25103 + au_warn_loopback_array = a;
25108 + spin_unlock(&spin);
25109 + AuWarn1("realloc failed, ignored\n");
25113 + spin_unlock(&spin);
25114 + pr_warn("you may want to try another patch for loopback file "
25115 + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25118 +int au_loopback_init(void)
25121 + struct super_block *sb __maybe_unused;
25123 + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25126 + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25127 + sizeof(unsigned long), GFP_NOFS);
25128 + if (unlikely(!au_warn_loopback_array))
25134 +void au_loopback_fin(void)
25136 + if (backing_file_func)
25137 + symbol_put(loop_backing_file);
25138 + au_kfree_try_rcu(au_warn_loopback_array);
25140 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
25141 new file mode 100644
25142 index 00000000000000..9726b8354e00db
25144 +++ b/fs/aufs/loop.h
25146 +/* SPDX-License-Identifier: GPL-2.0 */
25148 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25150 + * This program is free software; you can redistribute it and/or modify
25151 + * it under the terms of the GNU General Public License as published by
25152 + * the Free Software Foundation; either version 2 of the License, or
25153 + * (at your option) any later version.
25155 + * This program is distributed in the hope that it will be useful,
25156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25158 + * GNU General Public License for more details.
25160 + * You should have received a copy of the GNU General Public License
25161 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
25165 + * support for loopback mount as a branch
25168 +#ifndef __AUFS_LOOP_H__
25169 +#define __AUFS_LOOP_H__
25174 +struct super_block;
25176 +#ifdef CONFIG_AUFS_BDEV_LOOP
25177 +/* drivers/block/loop.c */
25178 +struct file *loop_backing_file(struct super_block *sb);
25181 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25182 +int au_test_loopback_kthread(void);
25183 +void au_warn_loopback(struct super_block *h_sb);
25185 +int au_loopback_init(void);
25186 +void au_loopback_fin(void);
25188 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25190 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25191 + struct dentry *h_adding)
25192 +AuStubInt0(au_test_loopback_kthread, void)
25193 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25195 +AuStubInt0(au_loopback_init, void)
25196 +AuStubVoid(au_loopback_fin, void)
25197 +#endif /* BLK_DEV_LOOP */
25199 +#endif /* __KERNEL__ */
25200 +#endif /* __AUFS_LOOP_H__ */
25201 diff --git a/fs/aufs/magic.mk b/fs/aufs/magic.mk
25202 new file mode 100644
25203 index 00000000000000..7bc9eef3ffec55
25205 +++ b/fs/aufs/magic.mk
25207 +# SPDX-License-Identifier: GPL-2.0
25209 +# defined in ${srctree}/fs/fuse/inode.c
25211 +ifdef CONFIG_FUSE_FS
25212 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25215 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25217 +ifdef CONFIG_XFS_FS
25218 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25221 +# defined in ${srctree}/fs/configfs/mount.c
25223 +ifdef CONFIG_CONFIGFS_FS
25224 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25227 +# defined in ${srctree}/fs/ubifs/ubifs.h
25229 +ifdef CONFIG_UBIFS_FS
25230 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25233 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25235 +ifdef CONFIG_HFSPLUS_FS
25236 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25238 diff --git a/fs/aufs/module.c b/fs/aufs/module.c
25239 new file mode 100644
25240 index 00000000000000..c2815fffb5c6ae
25242 +++ b/fs/aufs/module.c
25244 +// SPDX-License-Identifier: GPL-2.0
25246 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25248 + * This program is free software; you can redistribute it and/or modify
25249 + * it under the terms of the GNU General Public License as published by
25250 + * the Free Software Foundation; either version 2 of the License, or
25251 + * (at your option) any later version.
25253 + * This program is distributed in the hope that it will be useful,
25254 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25255 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25256 + * GNU General Public License for more details.
25258 + * You should have received a copy of the GNU General Public License
25259 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
25263 + * module global variables and operations
25266 +#include <linux/module.h>
25267 +#include <linux/seq_file.h>
25270 +/* shrinkable realloc */
25271 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25279 +#if 0 /* unused */
25286 + AuDebugOn(!new_sz);
25289 + diff = au_kmidx_sub(sz, new_sz);
25295 + /* expand or SLOB */
25296 + p = krealloc(p, new_sz, gfp);
25297 + else if (new_sz < sz && may_shrink) {
25301 + q = kmalloc(new_sz, gfp);
25304 + memcpy(q, p, new_sz);
25305 + au_kfree_try_rcu(p);
25316 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25319 + p = au_krealloc(p, new_sz, gfp, may_shrink);
25320 + if (p && new_sz > nused)
25321 + memset(p + nused, 0, new_sz - nused);
25325 +/* ---------------------------------------------------------------------- */
25329 +struct kmem_cache *au_cache[AuCache_Last];
25331 +static void au_cache_fin(void)
25336 + * Make sure all delayed rcu free inodes are flushed before we
25341 + /* excluding AuCache_HNOTIFY */
25342 + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25343 + for (i = 0; i < AuCache_HNOTIFY; i++) {
25344 + kmem_cache_destroy(au_cache[i]);
25345 + au_cache[i] = NULL;
25349 +static int __init au_cache_init(void)
25351 + au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25352 + if (au_cache[AuCache_DINFO])
25353 + /* SLAB_DESTROY_BY_RCU */
25354 + au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25355 + au_icntnr_init_once);
25356 + if (au_cache[AuCache_ICNTNR])
25357 + au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25358 + au_fi_init_once);
25359 + if (au_cache[AuCache_FINFO])
25360 + au_cache[AuCache_VDIR] = AuCache(au_vdir);
25361 + if (au_cache[AuCache_VDIR])
25362 + au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25363 + if (au_cache[AuCache_DEHSTR])
25370 +/* ---------------------------------------------------------------------- */
25372 +int au_dir_roflags;
25374 +#ifdef CONFIG_AUFS_SBILIST
25376 + * iterate_supers_type() doesn't protect us from
25377 + * remounting (branch management)
25379 +struct hlist_bl_head au_sbilist;
25383 + * functions for module interface.
25385 +MODULE_LICENSE("GPL");
25386 +/* MODULE_LICENSE("GPL v2"); */
25387 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25388 +MODULE_DESCRIPTION(AUFS_NAME
25389 + " -- Advanced multi layered unification filesystem");
25390 +MODULE_VERSION(AUFS_VERSION);
25391 +MODULE_ALIAS_FS(AUFS_NAME);
25393 +/* this module parameter has no meaning when SYSFS is disabled */
25394 +int sysaufs_brs = 1;
25395 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25396 +module_param_named(brs, sysaufs_brs, int, 0444);
25398 +/* this module parameter has no meaning when USER_NS is disabled */
25400 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25401 +module_param_named(allow_userns, au_userns, bool, 0444);
25403 +/* ---------------------------------------------------------------------- */
25405 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25407 +int au_seq_path(struct seq_file *seq, struct path *path)
25411 + err = seq_path(seq, path, au_esc_chars);
25420 +/* ---------------------------------------------------------------------- */
25422 +static int __init aufs_init(void)
25427 + p = au_esc_chars;
25428 + for (i = 1; i <= ' '; i++)
25434 + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25436 + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25437 + for (i = 0; i < AuIop_Last; i++)
25438 + aufs_iop_nogetattr[i].getattr = NULL;
25440 + memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
25442 + au_sbilist_init();
25443 + sysaufs_brs_init();
25446 + err = sysaufs_init();
25447 + if (unlikely(err))
25449 + err = dbgaufs_init();
25450 + if (unlikely(err))
25451 + goto out_sysaufs;
25452 + err = au_procfs_init();
25453 + if (unlikely(err))
25454 + goto out_dbgaufs;
25455 + err = au_wkq_init();
25456 + if (unlikely(err))
25458 + err = au_loopback_init();
25459 + if (unlikely(err))
25461 + err = au_hnotify_init();
25462 + if (unlikely(err))
25463 + goto out_loopback;
25464 + err = au_sysrq_init();
25465 + if (unlikely(err))
25467 + err = au_cache_init();
25468 + if (unlikely(err))
25471 + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25472 + err = register_filesystem(&aufs_fs_type);
25473 + if (unlikely(err))
25476 + /* since we define pr_fmt, call printk directly */
25477 + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25478 + goto out; /* success */
25485 + au_hnotify_fin();
25487 + au_loopback_fin();
25501 +static void __exit aufs_exit(void)
25503 + unregister_filesystem(&aufs_fs_type);
25506 + au_hnotify_fin();
25507 + au_loopback_fin();
25515 +module_init(aufs_init);
25516 +module_exit(aufs_exit);
25517 diff --git a/fs/aufs/module.h b/fs/aufs/module.h
25518 new file mode 100644
25519 index 00000000000000..a9d21d3e5414ea
25521 +++ b/fs/aufs/module.h
25523 +/* SPDX-License-Identifier: GPL-2.0 */
25525 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25527 + * This program is free software; you can redistribute it and/or modify
25528 + * it under the terms of the GNU General Public License as published by
25529 + * the Free Software Foundation; either version 2 of the License, or
25530 + * (at your option) any later version.
25532 + * This program is distributed in the hope that it will be useful,
25533 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25534 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25535 + * GNU General Public License for more details.
25537 + * You should have received a copy of the GNU General Public License
25538 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
25542 + * module initialization and module-global
25545 +#ifndef __AUFS_MODULE_H__
25546 +#define __AUFS_MODULE_H__
25550 +#include <linux/slab.h>
25551 +#include "debug.h"
25552 +#include "dentry.h"
25555 +#include "inode.h"
25560 +/* module parameters */
25561 +extern int sysaufs_brs;
25562 +extern bool au_userns;
25564 +/* ---------------------------------------------------------------------- */
25566 +extern int au_dir_roflags;
25568 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
25569 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25573 + * Comparing the size of the object with sizeof(struct rcu_head)
25574 + * case 1: object is always larger
25575 + * --> au_kfree_rcu() or au_kfree_do_rcu()
25576 + * case 2: object is always smaller
25577 + * --> au_kfree_small()
25578 + * case 3: object can be any size
25579 + * --> au_kfree_try_rcu()
25582 +static inline void au_kfree_do_rcu(const void *p)
25585 + struct rcu_head rcu;
25586 + } *a = (void *)p;
25588 + kfree_rcu(a, rcu);
25591 +#define au_kfree_rcu(_p) do { \
25592 + typeof(_p) p = (_p); \
25593 + BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
25595 + au_kfree_do_rcu(p); \
25598 +#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
25599 +#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
25601 +static inline void au_kfree_try_rcu(const void *p)
25605 + if (au_kfree_sz_test(p))
25606 + au_kfree_do_rcu(p);
25611 +static inline void au_kfree_small(const void *p)
25615 + AuDebugOn(au_kfree_sz_test(p));
25619 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
25621 +#ifndef CONFIG_SLOB
25622 + return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
25624 + return -1; /* SLOB is untested */
25628 +int au_seq_path(struct seq_file *seq, struct path *path);
25630 +#ifdef CONFIG_PROC_FS
25632 +int __init au_procfs_init(void);
25633 +void au_procfs_fin(void);
25635 +AuStubInt0(au_procfs_init, void);
25636 +AuStubVoid(au_procfs_fin, void);
25639 +/* ---------------------------------------------------------------------- */
25648 + AuCache_HNOTIFY, /* must be last */
25652 +extern struct kmem_cache *au_cache[AuCache_Last];
25654 +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25655 +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
25656 +#define AuCacheCtor(type, ctor) \
25657 + kmem_cache_create(#type, sizeof(struct type), \
25658 + __alignof__(struct type), AuCacheFlags, ctor)
25660 +#define AuCacheFuncAlloc(name, index) \
25661 + static inline struct au_##name *au_cache_alloc_##name(void) \
25662 + { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
25664 +#define AuCacheFuncs(name, index) \
25665 + static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25666 + { kmem_cache_free(au_cache[AuCache_##index], p); } \
25668 + static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25669 + { void *p = rcu; \
25670 + p -= offsetof(struct au_##name, rcu); \
25671 + kmem_cache_free(au_cache[AuCache_##index], p); } \
25672 + static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25673 + { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25674 + call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
25676 + static inline void au_cache_free_##name(struct au_##name *p) \
25677 + { /* au_cache_free_##name##_norcu(p); */ \
25678 + au_cache_free_##name##_rcu(p); }
25680 +AuCacheFuncs(dinfo, DINFO);
25681 +AuCacheFuncAlloc(dinfo, DINFO);
25683 +AuCacheFuncs(icntnr, ICNTNR);
25684 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
25685 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
25687 +AuCacheFuncs(finfo, FINFO);
25688 +AuCacheFuncAlloc(finfo, FINFO);
25690 +AuCacheFuncs(vdir, VDIR);
25691 +AuCacheFuncAlloc(vdir, VDIR);
25693 +AuCacheFuncs(vdir_dehstr, DEHSTR);
25694 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
25696 +#ifdef CONFIG_AUFS_HNOTIFY
25697 +AuCacheFuncs(hnotify, HNOTIFY);
25698 +AuCacheFuncAlloc(hnotify, HNOTIFY);
25701 +#endif /* __KERNEL__ */
25702 +#endif /* __AUFS_MODULE_H__ */
25703 diff --git a/fs/aufs/mvdown.c b/fs/aufs/mvdown.c
25704 new file mode 100644
25705 index 00000000000000..daafc27e64b0aa
25707 +++ b/fs/aufs/mvdown.c
25709 +// SPDX-License-Identifier: GPL-2.0
25711 + * Copyright (C) 2011-2022 Junjiro R. Okajima
25713 + * This program is free software; you can redistribute it and/or modify
25714 + * it under the terms of the GNU General Public License as published by
25715 + * the Free Software Foundation; either version 2 of the License, or
25716 + * (at your option) any later version.
25718 + * This program is distributed in the hope that it will be useful,
25719 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25720 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25721 + * GNU General Public License for more details.
25723 + * You should have received a copy of the GNU General Public License
25724 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
25728 + * move-down, opposite of copy-up
25733 +struct au_mvd_args {
25735 + struct super_block *h_sb;
25736 + struct dentry *h_parent;
25737 + struct au_hinode *hdir;
25738 + struct inode *h_dir, *h_inode;
25739 + struct au_pin pin;
25740 + } info[AUFS_MVDOWN_NARRAY];
25742 + struct aufs_mvdown mvdown;
25743 + struct dentry *dentry, *parent;
25744 + struct inode *inode, *dir;
25745 + struct super_block *sb;
25746 + aufs_bindex_t bopq, bwh, bfound;
25747 + unsigned char rename_lock;
25750 +#define mvd_errno mvdown.au_errno
25751 +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25752 +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25753 +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25754 +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25756 +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25757 +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25758 +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25759 +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25760 +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
25761 +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
25763 +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25764 +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25765 +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25766 +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25767 +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
25768 +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
25770 +#define AU_MVD_PR(flag, ...) do { \
25772 + pr_err(__VA_ARGS__); \
25775 +static int find_lower_writable(struct au_mvd_args *a)
25777 + struct super_block *sb;
25778 + aufs_bindex_t bindex, bbot;
25779 + struct au_branch *br;
25782 + bindex = a->mvd_bsrc;
25783 + bbot = au_sbbot(sb);
25784 + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25785 + for (bindex++; bindex <= bbot; bindex++) {
25786 + br = au_sbr(sb, bindex);
25787 + if (au_br_fhsm(br->br_perm)
25788 + && !sb_rdonly(au_br_sb(br)))
25791 + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25792 + for (bindex++; bindex <= bbot; bindex++) {
25793 + br = au_sbr(sb, bindex);
25794 + if (!au_br_rdonly(br))
25798 + for (bindex++; bindex <= bbot; bindex++) {
25799 + br = au_sbr(sb, bindex);
25800 + if (!sb_rdonly(au_br_sb(br))) {
25801 + if (au_br_rdonly(br))
25803 + |= AUFS_MVDOWN_ROLOWER_R;
25811 +/* make the parent dir on bdst */
25812 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25817 + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25818 + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25819 + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25820 + a->mvd_h_dst_parent = NULL;
25821 + if (au_dbbot(a->parent) >= a->mvd_bdst)
25822 + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25823 + if (!a->mvd_h_dst_parent) {
25824 + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25825 + if (unlikely(err)) {
25826 + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25829 + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25837 +/* lock them all */
25838 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25841 + struct dentry *h_trap;
25843 + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25844 + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25845 + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25846 + au_opt_udba(a->sb),
25847 + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25849 + if (unlikely(err)) {
25850 + AU_MVD_PR(dmsg, "pin_dst failed\n");
25854 + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25855 + a->rename_lock = 0;
25856 + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25857 + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25858 + au_opt_udba(a->sb),
25859 + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25860 + err = au_do_pin(&a->mvd_pin_src);
25862 + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25863 + if (unlikely(err)) {
25864 + AU_MVD_PR(dmsg, "pin_src failed\n");
25867 + goto out; /* success */
25870 + a->rename_lock = 1;
25871 + au_pin_hdir_unlock(&a->mvd_pin_dst);
25872 + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25873 + au_opt_udba(a->sb),
25874 + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25876 + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25877 + if (unlikely(err)) {
25878 + AU_MVD_PR(dmsg, "pin_src failed\n");
25879 + au_pin_hdir_lock(&a->mvd_pin_dst);
25882 + au_pin_hdir_unlock(&a->mvd_pin_src);
25883 + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25884 + a->mvd_h_dst_parent, a->mvd_hdir_dst);
25886 + err = (h_trap != a->mvd_h_src_parent);
25888 + err = (h_trap != a->mvd_h_dst_parent);
25890 + BUG_ON(err); /* it should never happen */
25891 + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25894 + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25895 + a->mvd_h_dst_parent, a->mvd_hdir_dst);
25896 + au_pin_hdir_lock(&a->mvd_pin_src);
25897 + au_unpin(&a->mvd_pin_src);
25898 + au_pin_hdir_lock(&a->mvd_pin_dst);
25901 + goto out; /* success */
25904 + au_unpin(&a->mvd_pin_dst);
25910 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25912 + if (!a->rename_lock)
25913 + au_unpin(&a->mvd_pin_src);
25915 + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25916 + a->mvd_h_dst_parent, a->mvd_hdir_dst);
25917 + au_pin_hdir_lock(&a->mvd_pin_src);
25918 + au_unpin(&a->mvd_pin_src);
25919 + au_pin_hdir_lock(&a->mvd_pin_dst);
25921 + au_unpin(&a->mvd_pin_dst);
25924 +/* copy-down the file */
25925 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25928 + struct au_cp_generic cpg = {
25929 + .dentry = a->dentry,
25930 + .bdst = a->mvd_bdst,
25931 + .bsrc = a->mvd_bsrc,
25933 + .pin = &a->mvd_pin_dst,
25934 + .flags = AuCpup_DTIME | AuCpup_HOPEN
25937 + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25938 + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25939 + au_fset_cpup(cpg.flags, OVERWRITE);
25940 + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25941 + au_fset_cpup(cpg.flags, RWDST);
25942 + err = au_sio_cpdown_simple(&cpg);
25943 + if (unlikely(err))
25944 + AU_MVD_PR(dmsg, "cpdown failed\n");
25951 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25954 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25957 + struct path h_path;
25958 + struct au_branch *br;
25959 + struct inode *delegated;
25961 + br = au_sbr(a->sb, a->mvd_bdst);
25962 + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25963 + err = PTR_ERR(h_path.dentry);
25964 + if (IS_ERR(h_path.dentry)) {
25965 + AU_MVD_PR(dmsg, "wh_lkup failed\n");
25970 + if (d_is_positive(h_path.dentry)) {
25971 + h_path.mnt = au_br_mnt(br);
25972 + delegated = NULL;
25973 + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25974 + &delegated, /*force*/0);
25975 + if (unlikely(err == -EWOULDBLOCK)) {
25976 + pr_warn("cannot retry for NFSv4 delegation"
25977 + " for an internal unlink\n");
25980 + if (unlikely(err))
25981 + AU_MVD_PR(dmsg, "wh_unlink failed\n");
25983 + dput(h_path.dentry);
25991 + * unlink the topmost h_dentry
25993 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25996 + struct path h_path;
25997 + struct inode *delegated;
25999 + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26000 + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26001 + delegated = NULL;
26002 + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26003 + if (unlikely(err == -EWOULDBLOCK)) {
26004 + pr_warn("cannot retry for NFSv4 delegation"
26005 + " for an internal unlink\n");
26008 + if (unlikely(err))
26009 + AU_MVD_PR(dmsg, "unlink failed\n");
26015 +/* Since mvdown succeeded, we ignore an error of this function */
26016 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26019 + struct au_branch *br;
26021 + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26022 + br = au_sbr(a->sb, a->mvd_bsrc);
26023 + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26025 + br = au_sbr(a->sb, a->mvd_bdst);
26026 + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26027 + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26030 + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26032 + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26036 + * copy-down the file and unlink the bsrc file.
26037 + * - unlink the bdst whout if exist
26038 + * - copy-down the file (with whtmp name and rename)
26039 + * - unlink the bsrc file
26041 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26045 + err = au_do_mkdir(dmsg, a);
26047 + err = au_do_lock(dmsg, a);
26048 + if (unlikely(err))
26052 + * do not revert the activities we made on bdst since they should be
26053 + * harmless in aufs.
26056 + err = au_do_cpdown(dmsg, a);
26058 + err = au_do_unlink_wh(dmsg, a);
26059 + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26060 + err = au_do_unlink(dmsg, a);
26061 + if (unlikely(err))
26064 + AuDbg("%pd2, 0x%x, %d --> %d\n",
26065 + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26066 + if (find_lower_writable(a) < 0)
26067 + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26069 + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26070 + au_do_stfs(dmsg, a);
26072 + /* maintain internal array */
26073 + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26074 + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26075 + au_set_dbtop(a->dentry, a->mvd_bdst);
26076 + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26077 + au_set_ibtop(a->inode, a->mvd_bdst);
26079 + /* hide the lower */
26080 + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26081 + au_set_dbbot(a->dentry, a->mvd_bsrc);
26082 + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26083 + au_set_ibbot(a->inode, a->mvd_bsrc);
26085 + if (au_dbbot(a->dentry) < a->mvd_bdst)
26086 + au_set_dbbot(a->dentry, a->mvd_bdst);
26087 + if (au_ibbot(a->inode) < a->mvd_bdst)
26088 + au_set_ibbot(a->inode, a->mvd_bdst);
26091 + au_do_unlock(dmsg, a);
26097 +/* ---------------------------------------------------------------------- */
26099 +/* make sure the file is idle */
26100 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26102 + int err, plinked;
26105 + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26106 + if (au_dbtop(a->dentry) == a->mvd_bsrc
26107 + && au_dcount(a->dentry) == 1
26108 + && atomic_read(&a->inode->i_count) == 1
26109 + /* && a->mvd_h_src_inode->i_nlink == 1 */
26110 + && (!plinked || !au_plink_test(a->inode))
26111 + && a->inode->i_nlink == 1)
26116 + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26117 + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26118 + atomic_read(&a->inode->i_count), a->inode->i_nlink,
26119 + a->mvd_h_src_inode->i_nlink,
26120 + plinked, plinked ? au_plink_test(a->inode) : 0);
26127 +/* make sure the parent dir is fine */
26128 +static int au_mvd_args_parent(const unsigned char dmsg,
26129 + struct au_mvd_args *a)
26132 + aufs_bindex_t bindex;
26135 + if (unlikely(au_alive_dir(a->parent))) {
26137 + AU_MVD_PR(dmsg, "parent dir is dead\n");
26141 + a->bopq = au_dbdiropq(a->parent);
26142 + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26143 + AuDbg("b%d\n", bindex);
26144 + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26145 + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26147 + a->mvd_errno = EAU_MVDOWN_OPAQUE;
26148 + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26149 + a->bopq, a->mvd_bdst);
26157 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26158 + struct au_mvd_args *a)
26161 + struct au_dinfo *dinfo, *tmp;
26163 + /* lookup the next lower positive entry */
26165 + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26166 + if (unlikely(!tmp))
26171 + dinfo = au_di(a->dentry);
26172 + au_di_cp(tmp, dinfo);
26173 + au_di_swap(tmp, dinfo);
26175 + /* returns the number of positive dentries */
26176 + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26177 + /* AuLkup_IGNORE_PERM */ 0);
26179 + a->bwh = au_dbwh(a->dentry);
26180 + else if (err > 0)
26181 + a->bfound = au_dbtop(a->dentry);
26183 + au_di_swap(tmp, dinfo);
26184 + au_rw_write_unlock(&tmp->di_rwsem);
26186 + if (unlikely(err < 0))
26187 + AU_MVD_PR(dmsg, "failed look-up lower\n");
26190 + * here, we have these cases.
26192 + * no positive dentry under bsrc. there are more sub-cases.
26194 + * there no whiteout, we can safely move-down.
26197 + * bsrc < bwh && bwh < bdst
26198 + * there is a whiteout on RO branch. cannot proceed.
26200 + * there is a whiteout on the RW target branch. it should
26203 + * there is a whiteout somewhere unrelated branch.
26204 + * -1 < bfound && bfound <= bsrc
26207 + * found, but it is on RO branch between bsrc and bdst. cannot
26210 + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26213 + * found, after we create the file on bdst, it will be hidden.
26216 + AuDebugOn(a->bfound == -1
26218 + && a->bwh <= a->mvd_bsrc);
26219 + AuDebugOn(-1 < a->bfound
26220 + && a->bfound <= a->mvd_bsrc);
26223 + if (a->bfound == -1
26224 + && a->mvd_bsrc < a->bwh
26226 + && a->bwh < a->mvd_bdst) {
26227 + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26228 + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26229 + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26231 + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26232 + a->mvd_errno = EAU_MVDOWN_UPPER;
26233 + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26234 + a->mvd_bdst, a->bfound);
26238 + err = 0; /* success */
26245 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26250 + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26251 + && a->bfound == a->mvd_bdst)
26257 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26260 + struct au_branch *br;
26263 + if (unlikely(S_ISDIR(a->inode->i_mode)))
26267 + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26268 + a->mvd_bsrc = au_ibtop(a->inode);
26270 + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26271 + if (unlikely(a->mvd_bsrc < 0
26272 + || (a->mvd_bsrc < au_dbtop(a->dentry)
26273 + || au_dbbot(a->dentry) < a->mvd_bsrc
26274 + || !au_h_dptr(a->dentry, a->mvd_bsrc))
26275 + || (a->mvd_bsrc < au_ibtop(a->inode)
26276 + || au_ibbot(a->inode) < a->mvd_bsrc
26277 + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26278 + a->mvd_errno = EAU_MVDOWN_NOUPPER;
26279 + AU_MVD_PR(dmsg, "no upper\n");
26283 + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26284 + a->mvd_errno = EAU_MVDOWN_BOTTOM;
26285 + AU_MVD_PR(dmsg, "on the bottom\n");
26288 + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26289 + br = au_sbr(a->sb, a->mvd_bsrc);
26290 + err = au_br_rdonly(br);
26291 + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26292 + if (unlikely(err))
26294 + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26295 + || IS_APPEND(a->mvd_h_src_inode))) {
26297 + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26303 + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26304 + a->mvd_bdst = find_lower_writable(a);
26305 + if (unlikely(a->mvd_bdst < 0)) {
26306 + a->mvd_errno = EAU_MVDOWN_BOTTOM;
26307 + AU_MVD_PR(dmsg, "no writable lower branch\n");
26311 + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26312 + if (unlikely(a->mvd_bdst < 0
26313 + || au_sbbot(a->sb) < a->mvd_bdst)) {
26314 + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26315 + AU_MVD_PR(dmsg, "no lower brid\n");
26320 + err = au_mvd_args_busy(dmsg, a);
26322 + err = au_mvd_args_parent(dmsg, a);
26324 + err = au_mvd_args_intermediate(dmsg, a);
26326 + err = au_mvd_args_exist(dmsg, a);
26328 + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26335 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26338 + unsigned char dmsg;
26339 + struct au_mvd_args *args;
26340 + struct inode *inode;
26342 + inode = d_inode(dentry);
26344 + if (unlikely(!capable(CAP_SYS_ADMIN)))
26348 + args = kmalloc(sizeof(*args), GFP_NOFS);
26349 + if (unlikely(!args))
26352 + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26354 + /* VERIFY_WRITE */
26355 + err = !access_ok(uarg, sizeof(*uarg));
26356 + if (unlikely(err)) {
26361 + AuDbg("flags 0x%x\n", args->mvdown.flags);
26362 + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26363 + args->mvdown.au_errno = 0;
26364 + args->dentry = dentry;
26365 + args->inode = inode;
26366 + args->sb = dentry->d_sb;
26369 + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26370 + args->parent = dget_parent(dentry);
26371 + args->dir = d_inode(args->parent);
26372 + inode_lock_nested(args->dir, I_MUTEX_PARENT);
26373 + dput(args->parent);
26374 + if (unlikely(args->parent != dentry->d_parent)) {
26375 + AU_MVD_PR(dmsg, "parent dir is moved\n");
26379 + inode_lock_nested(inode, I_MUTEX_CHILD);
26380 + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26381 + if (unlikely(err))
26384 + di_write_lock_parent(args->parent);
26385 + err = au_mvd_args(dmsg, args);
26386 + if (unlikely(err))
26389 + err = au_do_mvdown(dmsg, args);
26390 + if (unlikely(err))
26393 + au_cpup_attr_timesizes(args->dir);
26394 + au_cpup_attr_timesizes(inode);
26395 + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26396 + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26397 + /* au_digen_dec(dentry); */
26400 + di_write_unlock(args->parent);
26401 + aufs_read_unlock(dentry, AuLock_DW);
26403 + inode_unlock(inode);
26405 + inode_unlock(args->dir);
26407 + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26410 + au_kfree_rcu(args);
26415 diff --git a/fs/aufs/opts.c b/fs/aufs/opts.c
26416 new file mode 100644
26417 index 00000000000000..20421440c00417
26419 +++ b/fs/aufs/opts.c
26421 +// SPDX-License-Identifier: GPL-2.0
26423 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26425 + * This program is free software; you can redistribute it and/or modify
26426 + * it under the terms of the GNU General Public License as published by
26427 + * the Free Software Foundation; either version 2 of the License, or
26428 + * (at your option) any later version.
26430 + * This program is distributed in the hope that it will be useful,
26431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26433 + * GNU General Public License for more details.
26435 + * You should have received a copy of the GNU General Public License
26436 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
26440 + * mount options/flags
26443 +#include <linux/types.h> /* a distribution requires */
26444 +#include <linux/parser.h>
26447 +/* ---------------------------------------------------------------------- */
26449 +static const char *au_parser_pattern(int val, match_table_t tbl)
26451 + struct match_token *p;
26454 + while (p->pattern) {
26455 + if (p->token == val)
26456 + return p->pattern;
26463 +static const char *au_optstr(int *val, match_table_t tbl)
26465 + struct match_token *p;
26472 + while (p->pattern) {
26474 + && (v & p->token) == p->token) {
26475 + *val &= ~p->token;
26476 + return p->pattern;
26485 +/* ---------------------------------------------------------------------- */
26487 +static match_table_t brperm = {
26488 + {AuBrPerm_RO, AUFS_BRPERM_RO},
26489 + {AuBrPerm_RR, AUFS_BRPERM_RR},
26490 + {AuBrPerm_RW, AUFS_BRPERM_RW},
26494 +static match_table_t brattr = {
26496 + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26497 + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26498 + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26499 + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26500 +#ifdef CONFIG_AUFS_FHSM
26501 + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26503 +#ifdef CONFIG_AUFS_XATTR
26504 + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26505 + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26506 + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26507 + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26508 + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26509 + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26512 + /* ro/rr branch */
26513 + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26516 + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26517 + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26522 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26529 + p = strchr(str, '+');
26532 + v = match_token(str, table, args);
26534 + if (v & AuBrAttr_CMOO_Mask)
26535 + attr &= ~AuBrAttr_CMOO_Mask;
26540 + pr_warn("ignored branch attribute %s\n", str);
26550 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26558 + p = au_optstr(&perm, brattr);
26561 + memcpy(q, p, sz + 1);
26567 + p = au_optstr(&perm, brattr);
26571 + memcpy(q, p, sz + 1);
26577 + return q - str->a;
26580 +int au_br_perm_val(char *perm)
26582 + int val, bad, sz;
26584 + substring_t args[MAX_OPT_ARGS];
26585 + au_br_perm_str_t attr;
26587 + p = strchr(perm, '+');
26590 + val = match_token(perm, brperm, args);
26594 + pr_warn("ignored branch permission %s\n", perm);
26595 + val = AuBrPerm_RO;
26601 + val |= br_attr_val(p + 1, brattr, args);
26604 + switch (val & AuBrPerm_Mask) {
26605 + case AuBrPerm_RO:
26606 + case AuBrPerm_RR:
26607 + bad = val & AuBrWAttr_Mask;
26608 + val &= ~AuBrWAttr_Mask;
26610 + case AuBrPerm_RW:
26611 + bad = val & AuBrRAttr_Mask;
26612 + val &= ~AuBrRAttr_Mask;
26617 + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26618 + * does not treat it as an error, just warning.
26619 + * this is a tiny guard for the user operation.
26621 + if (val & AuBrAttr_UNPIN) {
26622 + bad |= AuBrAttr_UNPIN;
26623 + val &= ~AuBrAttr_UNPIN;
26626 + if (unlikely(bad)) {
26627 + sz = au_do_optstr_br_attr(&attr, bad);
26629 + pr_warn("ignored branch attribute %s\n", attr.a);
26636 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26638 + au_br_perm_str_t attr;
26644 + p = au_optstr(&perm, brperm);
26645 + AuDebugOn(!p || !*p);
26647 + memcpy(q, p, sz + 1);
26650 + sz = au_do_optstr_br_attr(&attr, perm);
26653 + memcpy(q, attr.a, sz + 1);
26656 + AuDebugOn(strlen(str->a) >= sizeof(str->a));
26659 +/* ---------------------------------------------------------------------- */
26661 +static match_table_t udbalevel = {
26662 + {AuOpt_UDBA_REVAL, "reval"},
26663 + {AuOpt_UDBA_NONE, "none"},
26664 +#ifdef CONFIG_AUFS_HNOTIFY
26665 + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26666 +#ifdef CONFIG_AUFS_HFSNOTIFY
26667 + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26673 +int au_udba_val(char *str)
26675 + substring_t args[MAX_OPT_ARGS];
26677 + return match_token(str, udbalevel, args);
26680 +const char *au_optstr_udba(int udba)
26682 + return au_parser_pattern(udba, udbalevel);
26685 +/* ---------------------------------------------------------------------- */
26687 +static match_table_t au_wbr_create_policy = {
26688 + {AuWbrCreate_TDP, "tdp"},
26689 + {AuWbrCreate_TDP, "top-down-parent"},
26690 + {AuWbrCreate_RR, "rr"},
26691 + {AuWbrCreate_RR, "round-robin"},
26692 + {AuWbrCreate_MFS, "mfs"},
26693 + {AuWbrCreate_MFS, "most-free-space"},
26694 + {AuWbrCreate_MFSV, "mfs:%d"},
26695 + {AuWbrCreate_MFSV, "most-free-space:%d"},
26697 + /* top-down regardless the parent, and then mfs */
26698 + {AuWbrCreate_TDMFS, "tdmfs:%d"},
26699 + {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26701 + {AuWbrCreate_MFSRR, "mfsrr:%d"},
26702 + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26703 + {AuWbrCreate_PMFS, "pmfs"},
26704 + {AuWbrCreate_PMFSV, "pmfs:%d"},
26705 + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26706 + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26711 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26712 + struct au_opt_wbr_create *create)
26715 + unsigned long long ull;
26718 + if (!match_u64(arg, &ull))
26719 + create->mfsrr_watermark = ull;
26721 + pr_err("bad integer in %s\n", str);
26728 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26729 + struct au_opt_wbr_create *create)
26734 + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26735 + create->mfs_second = n;
26737 + pr_err("bad integer in %s\n", str);
26744 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26747 + substring_t args[MAX_OPT_ARGS];
26749 + err = match_token(str, au_wbr_create_policy, args);
26750 + create->wbr_create = err;
26752 + case AuWbrCreate_MFSRRV:
26753 + case AuWbrCreate_TDMFSV:
26754 + case AuWbrCreate_PMFSRRV:
26755 + e = au_wbr_mfs_wmark(&args[0], str, create);
26757 + e = au_wbr_mfs_sec(&args[1], str, create);
26761 + case AuWbrCreate_MFSRR:
26762 + case AuWbrCreate_TDMFS:
26763 + case AuWbrCreate_PMFSRR:
26764 + e = au_wbr_mfs_wmark(&args[0], str, create);
26765 + if (unlikely(e)) {
26770 + case AuWbrCreate_MFS:
26771 + case AuWbrCreate_PMFS:
26772 + create->mfs_second = AUFS_MFS_DEF_SEC;
26774 + case AuWbrCreate_MFSV:
26775 + case AuWbrCreate_PMFSV:
26776 + e = au_wbr_mfs_sec(&args[0], str, create);
26785 +const char *au_optstr_wbr_create(int wbr_create)
26787 + return au_parser_pattern(wbr_create, au_wbr_create_policy);
26790 +static match_table_t au_wbr_copyup_policy = {
26791 + {AuWbrCopyup_TDP, "tdp"},
26792 + {AuWbrCopyup_TDP, "top-down-parent"},
26793 + {AuWbrCopyup_BUP, "bup"},
26794 + {AuWbrCopyup_BUP, "bottom-up-parent"},
26795 + {AuWbrCopyup_BU, "bu"},
26796 + {AuWbrCopyup_BU, "bottom-up"},
26800 +int au_wbr_copyup_val(char *str)
26802 + substring_t args[MAX_OPT_ARGS];
26804 + return match_token(str, au_wbr_copyup_policy, args);
26807 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26809 + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26812 +/* ---------------------------------------------------------------------- */
26814 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26815 + aufs_bindex_t bindex)
26818 + struct au_opt_add *add = &opt->add;
26821 + add->bindex = bindex;
26822 + add->perm = AuBrPerm_RO;
26823 + add->pathname = opt_str;
26824 + p = strchr(opt_str, '=');
26828 + add->perm = au_br_perm_val(p);
26831 + err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
26834 + add->perm = AuBrPerm_RO;
26835 + if (au_test_fs_rr(add->path.dentry->d_sb))
26836 + add->perm = AuBrPerm_RR;
26837 + else if (!bindex && !(sb_flags & SB_RDONLY))
26838 + add->perm = AuBrPerm_RW;
26840 + opt->type = Opt_add;
26843 + pr_err("lookup failed %s (%d)\n", add->pathname, err);
26850 +static int au_opt_wbr_create(struct super_block *sb,
26851 + struct au_opt_wbr_create *create)
26854 + struct au_sbinfo *sbinfo;
26856 + SiMustWriteLock(sb);
26858 + err = 1; /* handled */
26859 + sbinfo = au_sbi(sb);
26860 + if (sbinfo->si_wbr_create_ops->fin) {
26861 + err = sbinfo->si_wbr_create_ops->fin(sb);
26866 + sbinfo->si_wbr_create = create->wbr_create;
26867 + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26868 + switch (create->wbr_create) {
26869 + case AuWbrCreate_MFSRRV:
26870 + case AuWbrCreate_MFSRR:
26871 + case AuWbrCreate_TDMFS:
26872 + case AuWbrCreate_TDMFSV:
26873 + case AuWbrCreate_PMFSRR:
26874 + case AuWbrCreate_PMFSRRV:
26875 + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26877 + case AuWbrCreate_MFS:
26878 + case AuWbrCreate_MFSV:
26879 + case AuWbrCreate_PMFS:
26880 + case AuWbrCreate_PMFSV:
26881 + sbinfo->si_wbr_mfs.mfs_expire
26882 + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26886 + if (sbinfo->si_wbr_create_ops->init)
26887 + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26894 + * plus: processed without an error
26895 + * zero: unprocessed
26897 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26898 + struct au_opts *opts)
26901 + struct au_sbinfo *sbinfo;
26903 + SiMustWriteLock(sb);
26905 + err = 1; /* handled */
26906 + sbinfo = au_sbi(sb);
26907 + switch (opt->type) {
26909 + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26910 + sbinfo->si_mntflags |= opt->udba;
26911 + opts->given_udba |= opt->udba;
26916 + au_opt_set(sbinfo->si_mntflags, PLINK);
26918 + if (au_opt_test(sbinfo->si_mntflags, PLINK))
26919 + au_plink_put(sb, /*verbose*/1);
26920 + au_opt_clr(sbinfo->si_mntflags, PLINK);
26923 + case Opt_list_plink:
26924 + if (au_opt_test(sbinfo->si_mntflags, PLINK))
26925 + au_plink_list(sb);
26930 + au_opt_set(sbinfo->si_mntflags, DIO);
26931 + au_fset_opts(opts->flags, REFRESH_DYAOP);
26933 + au_opt_clr(sbinfo->si_mntflags, DIO);
26934 + au_fset_opts(opts->flags, REFRESH_DYAOP);
26938 + case Opt_fhsm_sec:
26939 + au_fhsm_set(sbinfo, opt->fhsm_second);
26942 + case Opt_diropq_a:
26943 + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26945 + case Opt_diropq_w:
26946 + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26949 + case Opt_warn_perm:
26951 + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26953 + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26956 + case Opt_verbose:
26958 + au_opt_set(sbinfo->si_mntflags, VERBOSE);
26960 + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26965 + au_opt_set(sbinfo->si_mntflags, SUM);
26967 + au_opt_clr(sbinfo->si_mntflags, SUM);
26968 + au_opt_clr(sbinfo->si_mntflags, SUM_W);
26972 + au_opt_clr(sbinfo->si_mntflags, SUM);
26973 + au_opt_set(sbinfo->si_mntflags, SUM_W);
26976 + case Opt_wbr_create:
26977 + err = au_opt_wbr_create(sb, &opt->wbr_create);
26979 + case Opt_wbr_copyup:
26980 + sbinfo->si_wbr_copyup = opt->wbr_copyup;
26981 + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26985 + sbinfo->si_dirwh = opt->dirwh;
26988 + case Opt_rdcache:
26989 + sbinfo->si_rdcache
26990 + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26993 + sbinfo->si_rdblk = opt->rdblk;
26996 + sbinfo->si_rdhash = opt->rdhash;
27001 + au_opt_set(sbinfo->si_mntflags, SHWH);
27003 + au_opt_clr(sbinfo->si_mntflags, SHWH);
27006 + case Opt_dirperm1:
27008 + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27010 + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27013 + case Opt_trunc_xino:
27015 + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27017 + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27020 + case Opt_trunc_xino_path:
27021 + case Opt_itrunc_xino:
27022 + err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27028 + case Opt_trunc_xib:
27030 + au_fset_opts(opts->flags, TRUNC_XIB);
27032 + au_fclr_opts(opts->flags, TRUNC_XIB);
27038 + if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27039 + err = au_dr_opt_set(sb);
27044 + au_opt_set(sbinfo->si_mntflags, DIRREN);
27046 + if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27047 + err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27053 + au_opt_clr(sbinfo->si_mntflags, DIRREN);
27059 + sb->s_flags |= SB_POSIXACL;
27061 + sb->s_flags &= ~SB_POSIXACL;
27073 + * returns tri-state.
27074 + * plus: processed without an error
27075 + * zero: unprocessed
27078 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27079 + struct au_opts *opts)
27081 + int err, do_refresh;
27084 + switch (opt->type) {
27086 + opt->add.bindex = au_sbbot(sb) + 1;
27087 + if (opt->add.bindex < 0)
27088 + opt->add.bindex = 0;
27090 + /* Always goto add, not fallthrough */
27091 + case Opt_prepend:
27092 + opt->add.bindex = 0;
27094 + add: /* indented label */
27096 + err = au_br_add(sb, &opt->add,
27097 + au_ftest_opts(opts->flags, REMOUNT));
27100 + au_fset_opts(opts->flags, REFRESH);
27106 + err = au_br_del(sb, &opt->del,
27107 + au_ftest_opts(opts->flags, REMOUNT));
27110 + au_fset_opts(opts->flags, TRUNC_XIB);
27111 + au_fset_opts(opts->flags, REFRESH);
27117 + err = au_br_mod(sb, &opt->mod,
27118 + au_ftest_opts(opts->flags, REMOUNT),
27123 + au_fset_opts(opts->flags, REFRESH);
27130 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27131 + struct au_opt_xino **opt_xino,
27132 + struct au_opts *opts)
27137 + switch (opt->type) {
27139 + err = au_xino_set(sb, &opt->xino,
27140 + !!au_ftest_opts(opts->flags, REMOUNT));
27142 + *opt_xino = &opt->xino;
27146 + *opt_xino = (void *)-1;
27153 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27154 + unsigned int pending)
27157 + aufs_bindex_t bindex, bbot;
27158 + unsigned char do_plink, skip, do_free, can_no_dreval;
27159 + struct au_branch *br;
27160 + struct au_wbr *wbr;
27161 + struct dentry *root, *dentry;
27162 + struct inode *dir, *h_dir;
27163 + struct au_sbinfo *sbinfo;
27164 + struct au_hinode *hdir;
27166 + SiMustAnyLock(sb);
27168 + sbinfo = au_sbi(sb);
27169 + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27171 + if (!(sb_flags & SB_RDONLY)) {
27172 + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27173 + pr_warn("first branch should be rw\n");
27174 + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27175 + pr_warn_once("shwh should be used with ro\n");
27178 + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27179 + && !au_opt_test(sbinfo->si_mntflags, XINO))
27180 + pr_warn_once("udba=*notify requires xino\n");
27182 + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27183 + pr_warn_once("dirperm1 breaks the protection"
27184 + " by the permission bits on the lower branch\n");
27188 + root = sb->s_root;
27189 + dir = d_inode(root);
27190 + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27191 + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27193 + bbot = au_sbbot(sb);
27194 + for (bindex = 0; !err && bindex <= bbot; bindex++) {
27196 + h_dir = au_h_iptr(dir, bindex);
27197 + br = au_sbr(sb, bindex);
27199 + if ((br->br_perm & AuBrAttr_ICEX)
27200 + && !h_dir->i_op->listxattr)
27201 + br->br_perm &= ~AuBrAttr_ICEX;
27202 +#if 0 /* untested */
27203 + if ((br->br_perm & AuBrAttr_ICEX_SEC)
27204 + && (au_br_sb(br)->s_flags & SB_NOSEC))
27205 + br->br_perm &= ~AuBrAttr_ICEX_SEC;
27209 + wbr = br->br_wbr;
27211 + wbr_wh_read_lock(wbr);
27213 + if (!au_br_writable(br->br_perm)) {
27216 + || (!wbr->wbr_whbase
27217 + && !wbr->wbr_plink
27218 + && !wbr->wbr_orph));
27219 + } else if (!au_br_wh_linkable(br->br_perm)) {
27220 + /* skip = (!br->br_whbase && !br->br_orph); */
27221 + skip = (!wbr || !wbr->wbr_whbase);
27222 + if (skip && wbr) {
27224 + skip = !!wbr->wbr_plink;
27226 + skip = !wbr->wbr_plink;
27229 + /* skip = (br->br_whbase && br->br_ohph); */
27230 + skip = (wbr && wbr->wbr_whbase);
27233 + skip = !!wbr->wbr_plink;
27235 + skip = !wbr->wbr_plink;
27239 + wbr_wh_read_unlock(wbr);
27241 + if (can_no_dreval) {
27242 + dentry = br->br_path.dentry;
27243 + spin_lock(&dentry->d_lock);
27244 + if (dentry->d_flags &
27245 + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27246 + can_no_dreval = 0;
27247 + spin_unlock(&dentry->d_lock);
27250 + if (au_br_fhsm(br->br_perm)) {
27252 + AuDebugOn(!br->br_fhsm);
27258 + hdir = au_hi(dir, bindex);
27259 + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27261 + wbr_wh_write_lock(wbr);
27262 + err = au_wh_init(br, sb);
27264 + wbr_wh_write_unlock(wbr);
27265 + au_hn_inode_unlock(hdir);
27267 + if (!err && do_free) {
27268 + au_kfree_rcu(wbr);
27269 + br->br_wbr = NULL;
27273 + if (can_no_dreval)
27274 + au_fset_si(sbinfo, NO_DREVAL);
27276 + au_fclr_si(sbinfo, NO_DREVAL);
27279 + au_fset_si(sbinfo, FHSM);
27280 + for (bindex = bbot; bindex >= 0; bindex--) {
27281 + br = au_sbr(sb, bindex);
27282 + if (au_br_fhsm(br->br_perm)) {
27283 + au_fhsm_set_bottom(sb, bindex);
27288 + au_fclr_si(sbinfo, FHSM);
27289 + au_fhsm_set_bottom(sb, -1);
27295 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27298 + unsigned int tmp;
27299 + aufs_bindex_t bindex, bbot;
27300 + struct au_opt *opt;
27301 + struct au_opt_xino *opt_xino, xino;
27302 + struct au_sbinfo *sbinfo;
27303 + struct au_branch *br;
27304 + struct inode *dir;
27306 + SiMustWriteLock(sb);
27311 + while (err >= 0 && opt->type != Opt_tail)
27312 + err = au_opt_simple(sb, opt++, opts);
27315 + else if (unlikely(err < 0))
27318 + /* disable xino and udba temporary */
27319 + sbinfo = au_sbi(sb);
27320 + tmp = sbinfo->si_mntflags;
27321 + au_opt_clr(sbinfo->si_mntflags, XINO);
27322 + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27325 + while (err >= 0 && opt->type != Opt_tail)
27326 + err = au_opt_br(sb, opt++, opts);
27329 + else if (unlikely(err < 0))
27332 + bbot = au_sbbot(sb);
27333 + if (unlikely(bbot < 0)) {
27335 + pr_err("no branches\n");
27339 + if (au_opt_test(tmp, XINO))
27340 + au_opt_set(sbinfo->si_mntflags, XINO);
27342 + while (!err && opt->type != Opt_tail)
27343 + err = au_opt_xino(sb, opt++, &opt_xino, opts);
27344 + if (unlikely(err))
27347 + err = au_opts_verify(sb, sb->s_flags, tmp);
27348 + if (unlikely(err))
27351 + /* restore xino */
27352 + if (au_opt_test(tmp, XINO) && !opt_xino) {
27353 + xino.file = au_xino_def(sb);
27354 + err = PTR_ERR(xino.file);
27355 + if (IS_ERR(xino.file))
27358 + err = au_xino_set(sb, &xino, /*remount*/0);
27360 + if (unlikely(err))
27364 + /* restore udba */
27365 + tmp &= AuOptMask_UDBA;
27366 + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27367 + sbinfo->si_mntflags |= tmp;
27368 + bbot = au_sbbot(sb);
27369 + for (bindex = 0; bindex <= bbot; bindex++) {
27370 + br = au_sbr(sb, bindex);
27371 + err = au_hnotify_reset_br(tmp, br, br->br_perm);
27372 + if (unlikely(err))
27373 + AuIOErr("hnotify failed on br %d, %d, ignored\n",
27375 + /* go on even if err */
27377 + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27378 + dir = d_inode(sb->s_root);
27379 + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27386 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27389 + unsigned char no_dreval;
27390 + struct inode *dir;
27391 + struct au_opt_xino *opt_xino;
27392 + struct au_opt *opt;
27393 + struct au_sbinfo *sbinfo;
27395 + SiMustWriteLock(sb);
27397 + err = au_dr_opt_flush(sb);
27398 + if (unlikely(err))
27400 + au_fset_opts(opts->flags, DR_FLUSHED);
27402 + dir = d_inode(sb->s_root);
27403 + sbinfo = au_sbi(sb);
27406 + while (err >= 0 && opt->type != Opt_tail) {
27407 + err = au_opt_simple(sb, opt, opts);
27409 + err = au_opt_br(sb, opt, opts);
27411 + err = au_opt_xino(sb, opt, &opt_xino, opts);
27417 + /* go on even err */
27419 + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27420 + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27421 + if (unlikely(rerr && !err))
27424 + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27425 + au_fset_opts(opts->flags, REFRESH_IDOP);
27427 + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27428 + rerr = au_xib_trunc(sb);
27429 + if (unlikely(rerr && !err))
27433 + /* will be handled by the caller */
27434 + if (!au_ftest_opts(opts->flags, REFRESH)
27435 + && (opts->given_udba
27436 + || au_opt_test(sbinfo->si_mntflags, XINO)
27437 + || au_ftest_opts(opts->flags, REFRESH_IDOP)
27439 + au_fset_opts(opts->flags, REFRESH);
27441 + AuDbg("status 0x%x\n", opts->flags);
27447 +/* ---------------------------------------------------------------------- */
27449 +unsigned int au_opt_udba(struct super_block *sb)
27451 + return au_mntflags(sb) & AuOptMask_UDBA;
27453 diff --git a/fs/aufs/opts.h b/fs/aufs/opts.h
27454 new file mode 100644
27455 index 00000000000000..d8bbdbf0560ae9
27457 +++ b/fs/aufs/opts.h
27459 +/* SPDX-License-Identifier: GPL-2.0 */
27461 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27463 + * This program is free software; you can redistribute it and/or modify
27464 + * it under the terms of the GNU General Public License as published by
27465 + * the Free Software Foundation; either version 2 of the License, or
27466 + * (at your option) any later version.
27468 + * This program is distributed in the hope that it will be useful,
27469 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27470 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27471 + * GNU General Public License for more details.
27473 + * You should have received a copy of the GNU General Public License
27474 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
27478 + * mount options/flags
27481 +#ifndef __AUFS_OPTS_H__
27482 +#define __AUFS_OPTS_H__
27486 +#include <linux/fs_parser.h>
27487 +#include <linux/namei.h>
27488 +#include <linux/path.h>
27492 + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27493 + Opt_idel, Opt_imod,
27494 + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27495 + Opt_xino, Opt_noxino,
27496 + Opt_trunc_xino, Opt_trunc_xino_v,
27497 + Opt_trunc_xino_path, Opt_itrunc_xino,
27500 + Opt_plink, Opt_list_plink,
27503 + Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27505 + Opt_wbr_copyup, Opt_wbr_create,
27507 + Opt_verbose, Opt_noverbose,
27508 + Opt_sum, Opt_wsum,
27512 + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27515 +/* ---------------------------------------------------------------------- */
27518 +#define AuOpt_XINO 1 /* external inode number bitmap
27519 + and translation table */
27520 +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27521 +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27522 +#define AuOpt_UDBA_REVAL (1 << 3)
27523 +#define AuOpt_UDBA_HNOTIFY (1 << 4)
27524 +#define AuOpt_SHWH (1 << 5) /* show whiteout */
27525 +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
27526 +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27528 +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27529 +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27530 +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27531 +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27532 +#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
27533 +#define AuOpt_DIO (1 << 14) /* direct io */
27534 +#define AuOpt_DIRREN (1 << 15) /* directory rename */
27536 +#ifndef CONFIG_AUFS_HNOTIFY
27537 +#undef AuOpt_UDBA_HNOTIFY
27538 +#define AuOpt_UDBA_HNOTIFY 0
27540 +#ifndef CONFIG_AUFS_DIRREN
27541 +#undef AuOpt_DIRREN
27542 +#define AuOpt_DIRREN 0
27544 +#ifndef CONFIG_AUFS_SHWH
27546 +#define AuOpt_SHWH 0
27549 +#define AuOpt_Def (AuOpt_XINO \
27550 + | AuOpt_UDBA_REVAL \
27552 + /* | AuOpt_DIRPERM1 */ \
27553 + | AuOpt_WARN_PERM)
27554 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27555 + | AuOpt_UDBA_REVAL \
27556 + | AuOpt_UDBA_HNOTIFY)
27558 +#define AuOpt_LkupDirFlags (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
27560 +#define au_opt_test(flags, name) (flags & AuOpt_##name)
27561 +#define au_opt_set(flags, name) do { \
27562 + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27563 + ((flags) |= AuOpt_##name); \
27565 +#define au_opt_set_udba(flags, name) do { \
27566 + (flags) &= ~AuOptMask_UDBA; \
27567 + ((flags) |= AuOpt_##name); \
27569 +#define au_opt_clr(flags, name) do { \
27570 + ((flags) &= ~AuOpt_##name); \
27573 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27575 +#ifdef CONFIG_PROC_FS
27578 + return mntflags & ~AuOpt_PLINK;
27582 +/* ---------------------------------------------------------------------- */
27584 +/* policies to select one among multiple writable branches */
27586 + AuWbrCreate_TDP, /* top down parent */
27587 + AuWbrCreate_RR, /* round robin */
27588 + AuWbrCreate_MFS, /* most free space */
27589 + AuWbrCreate_MFSV, /* mfs with seconds */
27590 + AuWbrCreate_MFSRR, /* mfs then rr */
27591 + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
27592 + AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27593 + AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
27594 + AuWbrCreate_PMFS, /* parent and mfs */
27595 + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
27596 + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27597 + AuWbrCreate_PMFSRRV, /* plus seconds */
27599 + AuWbrCreate_Def = AuWbrCreate_TDP
27603 + AuWbrCopyup_TDP, /* top down parent */
27604 + AuWbrCopyup_BUP, /* bottom up parent */
27605 + AuWbrCopyup_BU, /* bottom up */
27607 + AuWbrCopyup_Def = AuWbrCopyup_TDP
27610 +/* ---------------------------------------------------------------------- */
27614 +struct au_opt_add {
27615 + aufs_bindex_t bindex;
27618 + struct path path;
27621 +struct au_opt_del {
27623 + struct path h_path;
27626 +struct au_opt_mod {
27629 + struct dentry *h_root;
27632 +struct au_opt_xino {
27634 + struct file *file;
27637 +struct au_opt_xino_itrunc {
27638 + aufs_bindex_t bindex;
27641 +struct au_opt_wbr_create {
27644 + unsigned long long mfsrr_watermark;
27650 + struct au_opt_xino xino;
27651 + struct au_opt_xino_itrunc xino_itrunc;
27652 + struct au_opt_add add;
27653 + struct au_opt_del del;
27654 + struct au_opt_mod mod;
27657 + unsigned int rdblk;
27658 + unsigned int rdhash;
27660 + struct au_opt_wbr_create wbr_create;
27662 + unsigned int fhsm_second;
27663 + bool tf; /* generic flag, true or false */
27668 +#define AuOpts_REMOUNT 1
27669 +#define AuOpts_REFRESH (1 << 1)
27670 +#define AuOpts_TRUNC_XIB (1 << 2)
27671 +#define AuOpts_REFRESH_DYAOP (1 << 3)
27672 +#define AuOpts_REFRESH_IDOP (1 << 4)
27673 +#define AuOpts_DR_FLUSHED (1 << 5)
27674 +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
27675 +#define au_fset_opts(flags, name) \
27676 + do { (flags) |= AuOpts_##name; } while (0)
27677 +#define au_fclr_opts(flags, name) \
27678 + do { (flags) &= ~AuOpts_##name; } while (0)
27680 +#ifndef CONFIG_AUFS_DIRREN
27681 +#undef AuOpts_DR_FLUSHED
27682 +#define AuOpts_DR_FLUSHED 0
27686 + struct au_opt *opt;
27689 + unsigned int given_udba;
27690 + unsigned int flags;
27691 + unsigned long sb_flags;
27694 +/* ---------------------------------------------------------------------- */
27697 +int au_br_perm_val(char *perm);
27698 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27699 +int au_udba_val(char *str);
27700 +const char *au_optstr_udba(int udba);
27701 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
27702 +const char *au_optstr_wbr_create(int wbr_create);
27703 +int au_wbr_copyup_val(char *str);
27704 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27706 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27707 + aufs_bindex_t bindex);
27708 +struct super_block;
27709 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27710 + unsigned int pending);
27711 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27712 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27714 +unsigned int au_opt_udba(struct super_block *sb);
27717 +int aufs_fsctx_init(struct fs_context *fc);
27718 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
27720 +#endif /* __KERNEL__ */
27721 +#endif /* __AUFS_OPTS_H__ */
27722 diff --git a/fs/aufs/plink.c b/fs/aufs/plink.c
27723 new file mode 100644
27724 index 00000000000000..5f9a5b50dcc6ba
27726 +++ b/fs/aufs/plink.c
27728 +// SPDX-License-Identifier: GPL-2.0
27730 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27732 + * This program is free software; you can redistribute it and/or modify
27733 + * it under the terms of the GNU General Public License as published by
27734 + * the Free Software Foundation; either version 2 of the License, or
27735 + * (at your option) any later version.
27737 + * This program is distributed in the hope that it will be useful,
27738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27740 + * GNU General Public License for more details.
27742 + * You should have received a copy of the GNU General Public License
27743 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
27753 + * the pseudo-link maintenance mode.
27754 + * during a user process maintains the pseudo-links,
27755 + * prohibit adding a new plink and branch manipulation.
27759 + * For entry functions which will handle plink, and i_mutex is already held
27761 + * They cannot wait and should return an error at once.
27762 + * Callers has to check the error.
27764 + * For entry functions which will handle plink, but i_mutex is not held
27766 + * They can wait the plink maintenance mode to finish.
27768 + * They behave like F_SETLK and F_SETLKW.
27769 + * If the caller never handle plink, then both flags are unnecessary.
27772 +int au_plink_maint(struct super_block *sb, int flags)
27776 + struct task_struct *parent, *prev;
27777 + struct au_sbinfo *sbi;
27779 + SiMustAnyLock(sb);
27782 + if (!au_opt_test(au_mntflags(sb), PLINK))
27785 + sbi = au_sbi(sb);
27786 + pid = sbi->si_plink_maint_pid;
27787 + if (!pid || pid == current->pid)
27790 + /* todo: it highly depends upon /sbin/mount.aufs */
27792 + parent = current;
27796 + parent = rcu_dereference(parent->real_parent);
27797 + if (parent == prev)
27799 + ppid = task_pid_vnr(parent);
27800 + if (pid == ppid) {
27801 + rcu_read_unlock();
27806 + rcu_read_unlock();
27808 + if (au_ftest_lock(flags, NOPLMW)) {
27809 + /* if there is no i_mutex lock in VFS, we don't need to wait */
27810 + /* AuDebugOn(!lockdep_depth(current)); */
27811 + while (sbi->si_plink_maint_pid) {
27812 + si_read_unlock(sb);
27813 + /* gave up wake_up_bit() */
27814 + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27816 + if (au_ftest_lock(flags, FLUSH))
27817 + au_nwt_flush(&sbi->si_nowait);
27818 + si_noflush_read_lock(sb);
27820 + } else if (au_ftest_lock(flags, NOPLM)) {
27821 + AuDbg("ppid %d, pid %d\n", ppid, pid);
27829 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27831 + spin_lock(&sbinfo->si_plink_maint_lock);
27832 + sbinfo->si_plink_maint_pid = 0;
27833 + spin_unlock(&sbinfo->si_plink_maint_lock);
27834 + wake_up_all(&sbinfo->si_plink_wq);
27837 +int au_plink_maint_enter(struct super_block *sb)
27840 + struct au_sbinfo *sbinfo;
27843 + sbinfo = au_sbi(sb);
27844 + /* make sure i am the only one in this fs */
27845 + si_write_lock(sb, AuLock_FLUSH);
27846 + if (au_opt_test(au_mntflags(sb), PLINK)) {
27847 + spin_lock(&sbinfo->si_plink_maint_lock);
27848 + if (!sbinfo->si_plink_maint_pid)
27849 + sbinfo->si_plink_maint_pid = current->pid;
27852 + spin_unlock(&sbinfo->si_plink_maint_lock);
27854 + si_write_unlock(sb);
27859 +/* ---------------------------------------------------------------------- */
27861 +#ifdef CONFIG_AUFS_DEBUG
27862 +void au_plink_list(struct super_block *sb)
27865 + struct au_sbinfo *sbinfo;
27866 + struct hlist_bl_head *hbl;
27867 + struct hlist_bl_node *pos;
27868 + struct au_icntnr *icntnr;
27870 + SiMustAnyLock(sb);
27872 + sbinfo = au_sbi(sb);
27873 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27874 + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27876 + for (i = 0; i < AuPlink_NHASH; i++) {
27877 + hbl = sbinfo->si_plink + i;
27878 + hlist_bl_lock(hbl);
27879 + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27880 + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27881 + hlist_bl_unlock(hbl);
27886 +/* is the inode pseudo-linked? */
27887 +int au_plink_test(struct inode *inode)
27890 + struct au_sbinfo *sbinfo;
27891 + struct hlist_bl_head *hbl;
27892 + struct hlist_bl_node *pos;
27893 + struct au_icntnr *icntnr;
27895 + sbinfo = au_sbi(inode->i_sb);
27896 + AuRwMustAnyLock(&sbinfo->si_rwsem);
27897 + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27898 + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27901 + i = au_plink_hash(inode->i_ino);
27902 + hbl = sbinfo->si_plink + i;
27903 + hlist_bl_lock(hbl);
27904 + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27905 + if (&icntnr->vfs_inode == inode) {
27909 + hlist_bl_unlock(hbl);
27913 +/* ---------------------------------------------------------------------- */
27916 + * generate a name for plink.
27917 + * the file will be stored under AUFS_WH_PLINKDIR.
27919 +/* 20 is max digits length of ulong 64 */
27920 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27922 +static int plink_name(char *name, int len, struct inode *inode,
27923 + aufs_bindex_t bindex)
27926 + struct inode *h_inode;
27928 + h_inode = au_h_iptr(inode, bindex);
27929 + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27933 +struct au_do_plink_lkup_args {
27934 + struct dentry **errp;
27935 + struct qstr *tgtname;
27936 + struct path *h_ppath;
27939 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27940 + struct path *h_ppath)
27942 + struct dentry *h_dentry;
27943 + struct inode *h_inode;
27945 + h_inode = d_inode(h_ppath->dentry);
27946 + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27947 + h_dentry = vfsub_lkup_one(tgtname, h_ppath);
27948 + inode_unlock_shared(h_inode);
27953 +static void au_call_do_plink_lkup(void *args)
27955 + struct au_do_plink_lkup_args *a = args;
27956 + *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
27959 +/* lookup the plink-ed @inode under the branch at @bindex */
27960 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27962 + struct dentry *h_dentry;
27963 + struct au_branch *br;
27964 + struct path h_ppath;
27966 + char a[PLINK_NAME_LEN];
27967 + struct qstr tgtname = QSTR_INIT(a, 0);
27969 + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27971 + br = au_sbr(inode->i_sb, bindex);
27972 + h_ppath.dentry = br->br_wbr->wbr_plink;
27973 + h_ppath.mnt = au_br_mnt(br);
27974 + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27976 + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27977 + struct au_do_plink_lkup_args args = {
27978 + .errp = &h_dentry,
27979 + .tgtname = &tgtname,
27980 + .h_ppath = &h_ppath
27983 + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27984 + if (unlikely(wkq_err))
27985 + h_dentry = ERR_PTR(wkq_err);
27987 + h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
27992 +/* create a pseudo-link */
27993 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
27994 + struct dentry *h_dentry)
27997 + struct path h_path;
27998 + struct inode *h_dir, *delegated;
28000 + h_dir = d_inode(h_ppath->dentry);
28001 + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28002 + h_path.mnt = h_ppath->mnt;
28004 + h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28005 + err = PTR_ERR(h_path.dentry);
28006 + if (IS_ERR(h_path.dentry))
28010 + /* wh.plink dir is not monitored */
28011 + /* todo: is it really safe? */
28012 + if (d_is_positive(h_path.dentry)
28013 + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28014 + delegated = NULL;
28015 + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28016 + if (unlikely(err == -EWOULDBLOCK)) {
28017 + pr_warn("cannot retry for NFSv4 delegation"
28018 + " for an internal unlink\n");
28021 + dput(h_path.dentry);
28022 + h_path.dentry = NULL;
28026 + if (!err && d_is_negative(h_path.dentry)) {
28027 + delegated = NULL;
28028 + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28029 + if (unlikely(err == -EWOULDBLOCK)) {
28030 + pr_warn("cannot retry for NFSv4 delegation"
28031 + " for an internal link\n");
28035 + dput(h_path.dentry);
28038 + inode_unlock(h_dir);
28042 +struct do_whplink_args {
28044 + struct qstr *tgt;
28045 + struct path *h_ppath;
28046 + struct dentry *h_dentry;
28049 +static void call_do_whplink(void *args)
28051 + struct do_whplink_args *a = args;
28052 + *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28055 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28056 + aufs_bindex_t bindex)
28058 + int err, wkq_err;
28059 + struct au_branch *br;
28060 + struct au_wbr *wbr;
28061 + struct path h_ppath;
28062 + char a[PLINK_NAME_LEN];
28063 + struct qstr tgtname = QSTR_INIT(a, 0);
28065 + br = au_sbr(inode->i_sb, bindex);
28066 + wbr = br->br_wbr;
28067 + h_ppath.dentry = wbr->wbr_plink;
28068 + h_ppath.mnt = au_br_mnt(br);
28069 + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28071 + /* always superio. */
28072 + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28073 + struct do_whplink_args args = {
28076 + .h_ppath = &h_ppath,
28077 + .h_dentry = h_dentry
28079 + wkq_err = au_wkq_wait(call_do_whplink, &args);
28080 + if (unlikely(wkq_err))
28083 + err = do_whplink(&tgtname, &h_ppath, h_dentry);
28089 + * create a new pseudo-link for @h_dentry on @bindex.
28090 + * the linked inode is held in aufs @inode.
28092 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28093 + struct dentry *h_dentry)
28095 + struct super_block *sb;
28096 + struct au_sbinfo *sbinfo;
28097 + struct hlist_bl_head *hbl;
28098 + struct hlist_bl_node *pos;
28099 + struct au_icntnr *icntnr;
28100 + int found, err, cnt, i;
28102 + sb = inode->i_sb;
28103 + sbinfo = au_sbi(sb);
28104 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28105 + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28107 + found = au_plink_test(inode);
28111 + i = au_plink_hash(inode->i_ino);
28112 + hbl = sbinfo->si_plink + i;
28115 + hlist_bl_lock(hbl);
28116 + hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28117 + if (&icntnr->vfs_inode == inode) {
28123 + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28124 + hlist_bl_add_head(&icntnr->plink, hbl);
28126 + hlist_bl_unlock(hbl);
28128 + cnt = au_hbl_count(hbl);
28129 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28130 + if (cnt > AUFS_PLINK_WARN)
28131 + AuWarn1(msg ", %d\n", cnt);
28133 + err = whplink(h_dentry, inode, bindex);
28134 + if (unlikely(err)) {
28135 + pr_warn("err %d, damaged pseudo link.\n", err);
28136 + au_hbl_del(&icntnr->plink, hbl);
28137 + iput(&icntnr->vfs_inode);
28140 + iput(&icntnr->vfs_inode);
28143 +/* free all plinks */
28144 +void au_plink_put(struct super_block *sb, int verbose)
28147 + struct au_sbinfo *sbinfo;
28148 + struct hlist_bl_head *hbl;
28149 + struct hlist_bl_node *pos, *tmp;
28150 + struct au_icntnr *icntnr;
28152 + SiMustWriteLock(sb);
28154 + sbinfo = au_sbi(sb);
28155 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28156 + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28158 + /* no spin_lock since sbinfo is write-locked */
28160 + for (i = 0; i < AuPlink_NHASH; i++) {
28161 + hbl = sbinfo->si_plink + i;
28162 + if (!warned && verbose && !hlist_bl_empty(hbl)) {
28163 + pr_warn("pseudo-link is not flushed");
28166 + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28167 + iput(&icntnr->vfs_inode);
28168 + INIT_HLIST_BL_HEAD(hbl);
28172 +void au_plink_clean(struct super_block *sb, int verbose)
28174 + struct dentry *root;
28176 + root = sb->s_root;
28177 + aufs_write_lock(root);
28178 + if (au_opt_test(au_mntflags(sb), PLINK))
28179 + au_plink_put(sb, verbose);
28180 + aufs_write_unlock(root);
28183 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28186 + aufs_bindex_t btop, bbot, bindex;
28189 + btop = au_ibtop(inode);
28190 + bbot = au_ibbot(inode);
28192 + for (bindex = btop; bindex <= bbot; bindex++) {
28193 + if (!au_h_iptr(inode, bindex)
28194 + || au_ii_br_id(inode, bindex) != br_id)
28196 + au_set_h_iptr(inode, bindex, NULL, 0);
28201 + for (bindex = btop; bindex <= bbot; bindex++)
28202 + if (au_h_iptr(inode, bindex)) {
28212 +/* free the plinks on a branch specified by @br_id */
28213 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28215 + struct au_sbinfo *sbinfo;
28216 + struct hlist_bl_head *hbl;
28217 + struct hlist_bl_node *pos, *tmp;
28218 + struct au_icntnr *icntnr;
28219 + struct inode *inode;
28222 + SiMustWriteLock(sb);
28224 + sbinfo = au_sbi(sb);
28225 + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28226 + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28228 + /* no bit_lock since sbinfo is write-locked */
28229 + for (i = 0; i < AuPlink_NHASH; i++) {
28230 + hbl = sbinfo->si_plink + i;
28231 + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28232 + inode = au_igrab(&icntnr->vfs_inode);
28233 + ii_write_lock_child(inode);
28234 + do_put = au_plink_do_half_refresh(inode, br_id);
28236 + hlist_bl_del(&icntnr->plink);
28239 + ii_write_unlock(inode);
28244 diff --git a/fs/aufs/poll.c b/fs/aufs/poll.c
28245 new file mode 100644
28246 index 00000000000000..83defa454c8e18
28248 +++ b/fs/aufs/poll.c
28250 +// SPDX-License-Identifier: GPL-2.0
28252 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28254 + * This program is free software; you can redistribute it and/or modify
28255 + * it under the terms of the GNU General Public License as published by
28256 + * the Free Software Foundation; either version 2 of the License, or
28257 + * (at your option) any later version.
28259 + * This program is distributed in the hope that it will be useful,
28260 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28261 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28262 + * GNU General Public License for more details.
28264 + * You should have received a copy of the GNU General Public License
28265 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
28270 + * There is only one filesystem which implements ->poll operation, currently.
28275 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28278 + struct file *h_file;
28279 + struct super_block *sb;
28281 + /* We should pretend an error happened. */
28282 + mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28283 + sb = file->f_path.dentry->d_sb;
28284 + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28286 + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28287 + if (IS_ERR(h_file)) {
28288 + AuDbg("h_file %ld\n", PTR_ERR(h_file));
28292 + mask = vfs_poll(h_file, pt);
28293 + fput(h_file); /* instead of au_read_post() */
28296 + si_read_unlock(sb);
28297 + if (mask & EPOLLERR)
28298 + AuDbg("mask 0x%x\n", mask);
28301 diff --git a/fs/aufs/posix_acl.c b/fs/aufs/posix_acl.c
28302 new file mode 100644
28303 index 00000000000000..03742bcc66035e
28305 +++ b/fs/aufs/posix_acl.c
28307 +// SPDX-License-Identifier: GPL-2.0
28309 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28311 + * This program is free software; you can redistribute it and/or modify
28312 + * it under the terms of the GNU General Public License as published by
28313 + * the Free Software Foundation; either version 2 of the License, or
28314 + * (at your option) any later version.
28316 + * This program is distributed in the hope that it will be useful,
28317 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28318 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28319 + * GNU General Public License for more details.
28321 + * You should have received a copy of the GNU General Public License
28322 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
28326 + * posix acl operations
28329 +#include <linux/fs.h>
28332 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28334 + struct posix_acl *acl;
28336 + aufs_bindex_t bindex;
28337 + struct inode *h_inode;
28338 + struct super_block *sb;
28340 + acl = ERR_PTR(-ECHILD);
28345 + sb = inode->i_sb;
28346 + si_read_lock(sb, AuLock_FLUSH);
28347 + ii_read_lock_child(inode);
28348 + if (!(sb->s_flags & SB_POSIXACL))
28351 + bindex = au_ibtop(inode);
28352 + h_inode = au_h_iptr(inode, bindex);
28353 + if (unlikely(!h_inode
28354 + || ((h_inode->i_mode & S_IFMT)
28355 + != (inode->i_mode & S_IFMT)))) {
28356 + err = au_busy_or_stale();
28357 + acl = ERR_PTR(err);
28361 + /* always topmost only */
28362 + acl = get_inode_acl(h_inode, type);
28364 + forget_cached_acl(inode, type);
28366 + set_cached_acl(inode, type, acl);
28369 + ii_read_unlock(inode);
28370 + si_read_unlock(sb);
28373 + AuTraceErrPtr(acl);
28377 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28378 + struct dentry *dentry, int type)
28380 + struct posix_acl *acl;
28381 + struct inode *inode;
28383 + inode = d_inode(dentry);
28384 + acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28389 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28390 + struct posix_acl *acl, int type)
28394 + struct inode *inode;
28395 + struct au_sxattr arg = {
28396 + .type = AU_ACL_SET,
28403 + inode = d_inode(dentry);
28404 + IMustLock(inode);
28406 + ssz = au_sxattr(dentry, inode, &arg);
28407 + /* forget even it if succeeds since the branch might set differently */
28408 + forget_cached_acl(inode, type);
28415 diff --git a/fs/aufs/procfs.c b/fs/aufs/procfs.c
28416 new file mode 100644
28417 index 00000000000000..0fca104ac554b3
28419 +++ b/fs/aufs/procfs.c
28421 +// SPDX-License-Identifier: GPL-2.0
28423 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28425 + * This program is free software; you can redistribute it and/or modify
28426 + * it under the terms of the GNU General Public License as published by
28427 + * the Free Software Foundation; either version 2 of the License, or
28428 + * (at your option) any later version.
28430 + * This program is distributed in the hope that it will be useful,
28431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28433 + * GNU General Public License for more details.
28435 + * You should have received a copy of the GNU General Public License
28436 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
28440 + * procfs interfaces
28443 +#include <linux/proc_fs.h>
28446 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28448 + struct au_sbinfo *sbinfo;
28450 + sbinfo = file->private_data;
28452 + au_plink_maint_leave(sbinfo);
28453 + kobject_put(&sbinfo->si_kobj);
28459 +static void au_procfs_plm_write_clean(struct file *file)
28461 + struct au_sbinfo *sbinfo;
28463 + sbinfo = file->private_data;
28465 + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28468 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28471 + struct super_block *sb;
28472 + struct au_sbinfo *sbinfo;
28473 + struct hlist_bl_node *pos;
28476 + if (unlikely(file->private_data))
28480 + /* don't use au_sbilist_lock() here */
28481 + hlist_bl_lock(&au_sbilist);
28482 + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28483 + if (id == sysaufs_si_id(sbinfo)) {
28484 + if (kobject_get_unless_zero(&sbinfo->si_kobj))
28485 + sb = sbinfo->si_sb;
28488 + hlist_bl_unlock(&au_sbilist);
28491 + if (unlikely(!sb))
28494 + err = au_plink_maint_enter(sb);
28496 + /* keep kobject_get() */
28497 + file->private_data = sbinfo;
28499 + kobject_put(&sbinfo->si_kobj);
28505 + * Accept a valid "si=xxxx" only.
28506 + * Once it is accepted successfully, accept "clean" too.
28508 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28509 + size_t count, loff_t *ppos)
28512 + unsigned long id;
28513 + /* last newline is allowed */
28514 + char buf[3 + sizeof(unsigned long) * 2 + 1];
28517 + if (unlikely(!capable(CAP_SYS_ADMIN)))
28521 + if (unlikely(count > sizeof(buf)))
28524 + err = copy_from_user(buf, ubuf, count);
28525 + if (unlikely(err)) {
28532 + if (!strcmp("clean", buf)) {
28533 + au_procfs_plm_write_clean(file);
28534 + goto out_success;
28535 + } else if (unlikely(strncmp("si=", buf, 3)))
28538 + err = kstrtoul(buf + 3, 16, &id);
28539 + if (unlikely(err))
28542 + err = au_procfs_plm_write_si(file, id);
28543 + if (unlikely(err))
28547 + err = count; /* success */
28552 +static const struct proc_ops au_procfs_plm_op = {
28553 + .proc_write = au_procfs_plm_write,
28554 + .proc_release = au_procfs_plm_release
28557 +/* ---------------------------------------------------------------------- */
28559 +static struct proc_dir_entry *au_procfs_dir;
28561 +void au_procfs_fin(void)
28563 + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28564 + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28567 +int __init au_procfs_init(void)
28570 + struct proc_dir_entry *entry;
28573 + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28574 + if (unlikely(!au_procfs_dir))
28577 + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28578 + au_procfs_dir, &au_procfs_plm_op);
28579 + if (unlikely(!entry))
28583 + goto out; /* success */
28587 + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28591 diff --git a/fs/aufs/rdu.c b/fs/aufs/rdu.c
28592 new file mode 100644
28593 index 00000000000000..144251c02c3f31
28595 +++ b/fs/aufs/rdu.c
28597 +// SPDX-License-Identifier: GPL-2.0
28599 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28601 + * This program is free software; you can redistribute it and/or modify
28602 + * it under the terms of the GNU General Public License as published by
28603 + * the Free Software Foundation; either version 2 of the License, or
28604 + * (at your option) any later version.
28606 + * This program is distributed in the hope that it will be useful,
28607 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28608 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28609 + * GNU General Public License for more details.
28611 + * You should have received a copy of the GNU General Public License
28612 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
28616 + * readdir in userspace.
28619 +#include <linux/compat.h>
28620 +#include <linux/fs_stack.h>
28621 +#include <linux/security.h>
28624 +/* bits for struct aufs_rdu.flags */
28625 +#define AuRdu_CALLED 1
28626 +#define AuRdu_CONT (1 << 1)
28627 +#define AuRdu_FULL (1 << 2)
28628 +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
28629 +#define au_fset_rdu(flags, name) \
28630 + do { (flags) |= AuRdu_##name; } while (0)
28631 +#define au_fclr_rdu(flags, name) \
28632 + do { (flags) &= ~AuRdu_##name; } while (0)
28634 +struct au_rdu_arg {
28635 + struct dir_context ctx;
28636 + struct aufs_rdu *rdu;
28637 + union au_rdu_ent_ul ent;
28638 + unsigned long end;
28640 + struct super_block *sb;
28644 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28645 + loff_t offset, u64 h_ino, unsigned int d_type)
28648 + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28649 + struct aufs_rdu *rdu = arg->rdu;
28650 + struct au_rdu_ent ent;
28654 + au_fset_rdu(rdu->cookie.flags, CALLED);
28655 + len = au_rdu_len(nlen);
28656 + if (arg->ent.ul + len < arg->end) {
28658 + ent.bindex = rdu->cookie.bindex;
28659 + ent.type = d_type;
28661 + if (unlikely(nlen > AUFS_MAX_NAMELEN))
28662 + ent.type = DT_UNKNOWN;
28664 + /* unnecessary to support mmap_sem since this is a dir */
28666 + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28668 + if (copy_to_user(arg->ent.e->name, name, nlen))
28670 + /* the terminating NULL */
28671 + if (__put_user(0, arg->ent.e->name + nlen))
28674 + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28675 + arg->ent.ul += len;
28679 + au_fset_rdu(rdu->cookie.flags, FULL);
28681 + rdu->tail = arg->ent;
28685 + /* AuTraceErr(err); */
28689 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28693 + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28695 + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28696 + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28698 + if (unlikely(offset != cookie->h_pos))
28704 + au_fclr_rdu(cookie->flags, CALLED);
28706 + err = vfsub_iterate_dir(h_file, &arg->ctx);
28710 + && au_ftest_rdu(cookie->flags, CALLED)
28711 + && !au_ftest_rdu(cookie->flags, FULL));
28712 + cookie->h_pos = h_file->f_pos;
28719 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28722 + aufs_bindex_t bbot;
28723 + struct au_rdu_arg arg = {
28725 + .actor = au_rdu_fill
28728 + struct dentry *dentry;
28729 + struct inode *inode;
28730 + struct file *h_file;
28731 + struct au_rdu_cookie *cookie = &rdu->cookie;
28733 + /* VERIFY_WRITE */
28734 + err = !access_ok(rdu->ent.e, rdu->sz);
28735 + if (unlikely(err)) {
28741 + rdu->tail = rdu->ent;
28744 + arg.ent = rdu->ent;
28745 + arg.end = arg.ent.ul;
28746 + arg.end += rdu->sz;
28749 + if (unlikely(!file->f_op->iterate_shared))
28752 + err = security_file_permission(file, MAY_READ);
28754 + if (unlikely(err))
28757 + dentry = file->f_path.dentry;
28758 + inode = d_inode(dentry);
28759 + inode_lock_shared(inode);
28761 + arg.sb = inode->i_sb;
28762 + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28763 + if (unlikely(err))
28765 + err = au_alive_dir(dentry);
28766 + if (unlikely(err))
28768 + /* todo: reval? */
28769 + fi_read_lock(file);
28772 + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28773 + && cookie->generation != au_figen(file)))
28778 + rdu->blk = au_sbi(arg.sb)->si_rdblk;
28780 + rdu->blk = au_dir_size(file, /*dentry*/NULL);
28782 + bbot = au_fbtop(file);
28783 + if (cookie->bindex < bbot)
28784 + cookie->bindex = bbot;
28785 + bbot = au_fbbot_dir(file);
28786 + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28787 + for (; !err && cookie->bindex <= bbot;
28788 + cookie->bindex++, cookie->h_pos = 0) {
28789 + h_file = au_hf_dir(file, cookie->bindex);
28793 + au_fclr_rdu(cookie->flags, FULL);
28794 + err = au_rdu_do(h_file, &arg);
28796 + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28799 + AuDbg("rent %llu\n", rdu->rent);
28801 + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28802 + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28803 + au_fset_rdu(cookie->flags, CONT);
28804 + cookie->generation = au_figen(file);
28807 + ii_read_lock_child(inode);
28808 + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28809 + ii_read_unlock(inode);
28812 + fi_read_unlock(file);
28814 + si_read_unlock(arg.sb);
28816 + inode_unlock_shared(inode);
28822 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28826 + unsigned long long nent;
28827 + union au_rdu_ent_ul *u;
28828 + struct au_rdu_ent ent;
28829 + struct super_block *sb;
28832 + nent = rdu->nent;
28834 + sb = file->f_path.dentry->d_sb;
28835 + si_read_lock(sb, AuLock_FLUSH);
28836 + while (nent-- > 0) {
28837 + /* unnecessary to support mmap_sem since this is a dir */
28838 + err = copy_from_user(&ent, u->e, sizeof(ent));
28840 + /* VERIFY_WRITE */
28841 + err = !access_ok(&u->e->ino, sizeof(ino));
28842 + if (unlikely(err)) {
28848 + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28850 + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28852 + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28854 + if (unlikely(err)) {
28859 + err = __put_user(ino, &u->e->ino);
28860 + if (unlikely(err)) {
28865 + u->ul += au_rdu_len(ent.nlen);
28867 + si_read_unlock(sb);
28872 +/* ---------------------------------------------------------------------- */
28874 +static int au_rdu_verify(struct aufs_rdu *rdu)
28876 + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28877 + "%llu, b%d, 0x%x, g%u}\n",
28878 + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28880 + rdu->rent, rdu->shwh, rdu->full,
28881 + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28882 + rdu->cookie.generation);
28884 + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28888 + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28892 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28895 + struct aufs_rdu rdu;
28896 + void __user *p = (void __user *)arg;
28898 + err = copy_from_user(&rdu, p, sizeof(rdu));
28899 + if (unlikely(err)) {
28904 + err = au_rdu_verify(&rdu);
28905 + if (unlikely(err))
28909 + case AUFS_CTL_RDU:
28910 + err = au_rdu(file, &rdu);
28911 + if (unlikely(err))
28914 + e = copy_to_user(p, &rdu, sizeof(rdu));
28915 + if (unlikely(e)) {
28920 + case AUFS_CTL_RDU_INO:
28921 + err = au_rdu_ino(file, &rdu);
28925 + /* err = -ENOTTY; */
28934 +#ifdef CONFIG_COMPAT
28935 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28938 + struct aufs_rdu rdu;
28939 + void __user *p = compat_ptr(arg);
28941 + /* todo: get_user()? */
28942 + err = copy_from_user(&rdu, p, sizeof(rdu));
28943 + if (unlikely(err)) {
28948 + rdu.ent.e = compat_ptr(rdu.ent.ul);
28949 + err = au_rdu_verify(&rdu);
28950 + if (unlikely(err))
28954 + case AUFS_CTL_RDU:
28955 + err = au_rdu(file, &rdu);
28956 + if (unlikely(err))
28959 + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28960 + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28961 + e = copy_to_user(p, &rdu, sizeof(rdu));
28962 + if (unlikely(e)) {
28967 + case AUFS_CTL_RDU_INO:
28968 + err = au_rdu_ino(file, &rdu);
28972 + /* err = -ENOTTY; */
28981 diff --git a/fs/aufs/rwsem.h b/fs/aufs/rwsem.h
28982 new file mode 100644
28983 index 00000000000000..9d9cd50ef15587
28985 +++ b/fs/aufs/rwsem.h
28987 +/* SPDX-License-Identifier: GPL-2.0 */
28989 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28991 + * This program is free software; you can redistribute it and/or modify
28992 + * it under the terms of the GNU General Public License as published by
28993 + * the Free Software Foundation; either version 2 of the License, or
28994 + * (at your option) any later version.
28996 + * This program is distributed in the hope that it will be useful,
28997 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28998 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28999 + * GNU General Public License for more details.
29001 + * You should have received a copy of the GNU General Public License
29002 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
29006 + * simple read-write semaphore wrappers
29009 +#ifndef __AUFS_RWSEM_H__
29010 +#define __AUFS_RWSEM_H__
29014 +#include "debug.h"
29016 +/* in the future, the name 'au_rwsem' will be totally gone */
29017 +#define au_rwsem rw_semaphore
29019 +/* to debug easier, do not make them inlined functions */
29020 +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
29022 +#ifdef CONFIG_LOCKDEP
29023 +/* rwsem_is_locked() is unusable */
29024 +#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29025 + && !lockdep_recursing(current) \
29027 + && !lockdep_is_held_type(rw, 1))
29028 +#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29029 + && !lockdep_recursing(current) \
29031 + && !lockdep_is_held_type(rw, 0))
29032 +#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29033 + && !lockdep_recursing(current) \
29035 + && !lockdep_is_held(rw))
29036 +#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29037 + && !lockdep_recursing(current) \
29039 + && lockdep_is_held(rw))
29041 +#define AuRwMustReadLock(rw) do {} while (0)
29042 +#define AuRwMustWriteLock(rw) do {} while (0)
29043 +#define AuRwMustAnyLock(rw) do {} while (0)
29044 +#define AuRwDestroy(rw) do {} while (0)
29047 +#define au_rw_init(rw) init_rwsem(rw)
29049 +#define au_rw_init_wlock(rw) do { \
29050 + au_rw_init(rw); \
29051 + down_write(rw); \
29054 +#define au_rw_init_wlock_nested(rw, lsc) do { \
29055 + au_rw_init(rw); \
29056 + down_write_nested(rw, lsc); \
29059 +#define au_rw_read_lock(rw) down_read(rw)
29060 +#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29061 +#define au_rw_read_unlock(rw) up_read(rw)
29062 +#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29063 +#define au_rw_write_lock(rw) down_write(rw)
29064 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29065 +#define au_rw_write_unlock(rw) up_write(rw)
29066 +/* why is not _nested version defined? */
29067 +#define au_rw_read_trylock(rw) down_read_trylock(rw)
29068 +#define au_rw_write_trylock(rw) down_write_trylock(rw)
29070 +#endif /* __KERNEL__ */
29071 +#endif /* __AUFS_RWSEM_H__ */
29072 diff --git a/fs/aufs/sbinfo.c b/fs/aufs/sbinfo.c
29073 new file mode 100644
29074 index 00000000000000..dde9f9c17b18db
29076 +++ b/fs/aufs/sbinfo.c
29078 +// SPDX-License-Identifier: GPL-2.0
29080 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29082 + * This program is free software; you can redistribute it and/or modify
29083 + * it under the terms of the GNU General Public License as published by
29084 + * the Free Software Foundation; either version 2 of the License, or
29085 + * (at your option) any later version.
29087 + * This program is distributed in the hope that it will be useful,
29088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29090 + * GNU General Public License for more details.
29092 + * You should have received a copy of the GNU General Public License
29093 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
29097 + * superblock private data
29100 +#include <linux/iversion.h>
29104 + * they are necessary regardless sysfs is disabled.
29106 +void au_si_free(struct kobject *kobj)
29109 + struct au_sbinfo *sbinfo;
29110 + char *locked __maybe_unused; /* debug only */
29112 + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29113 + for (i = 0; i < AuPlink_NHASH; i++)
29114 + AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29115 + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29117 + AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29118 + au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29119 + AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29120 + au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29122 + dbgaufs_si_fin(sbinfo);
29123 + au_rw_write_lock(&sbinfo->si_rwsem);
29124 + au_br_free(sbinfo);
29125 + au_rw_write_unlock(&sbinfo->si_rwsem);
29127 + au_kfree_try_rcu(sbinfo->si_branch);
29128 + mutex_destroy(&sbinfo->si_xib_mtx);
29129 + AuRwDestroy(&sbinfo->si_rwsem);
29131 + au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29132 + /* si_nfiles is waited too */
29133 + au_kfree_rcu(sbinfo);
29136 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29138 + struct au_sbinfo *sbinfo;
29142 + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29143 + if (unlikely(!sbinfo))
29146 + /* will be reallocated separately */
29147 + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29148 + if (unlikely(!sbinfo->si_branch))
29151 + err = sysaufs_si_init(sbinfo);
29153 + dbgaufs_si_null(sbinfo);
29154 + err = dbgaufs_si_init(sbinfo);
29155 + if (unlikely(err))
29156 + kobject_put(&sbinfo->si_kobj);
29158 + if (unlikely(err))
29161 + au_nwt_init(&sbinfo->si_nowait);
29162 + au_rw_init_wlock(&sbinfo->si_rwsem);
29164 + au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29165 + au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29167 + sbinfo->si_bbot = -1;
29168 + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29170 + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29171 + sbinfo->si_wbr_create = AuWbrCreate_Def;
29172 + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29173 + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29175 + au_fhsm_init(sbinfo);
29177 + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29179 + sbinfo->si_xino_jiffy = jiffies;
29180 + sbinfo->si_xino_expire
29181 + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29182 + mutex_init(&sbinfo->si_xib_mtx);
29183 + /* leave si_xib_last_pindex and si_xib_next_bit */
29185 + INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29187 + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29188 + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29189 + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29190 + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29192 + for (i = 0; i < AuPlink_NHASH; i++)
29193 + INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29194 + init_waitqueue_head(&sbinfo->si_plink_wq);
29195 + spin_lock_init(&sbinfo->si_plink_maint_lock);
29197 + INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29199 + /* with getattr by default */
29200 + sbinfo->si_iop_array = aufs_iop;
29202 + /* leave other members for sysaufs and si_mnt. */
29203 + sbinfo->si_sb = sb;
29205 + sb->s_fs_info = sbinfo;
29208 + return sbinfo; /* success */
29211 + au_kfree_try_rcu(sbinfo->si_branch);
29213 + au_kfree_rcu(sbinfo);
29215 + return ERR_PTR(err);
29218 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29221 + struct au_branch **brp;
29223 + AuRwMustWriteLock(&sbinfo->si_rwsem);
29226 + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29227 + if (unlikely(!sz))
29228 + sz = sizeof(*brp);
29229 + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29232 + sbinfo->si_branch = brp;
29239 +/* ---------------------------------------------------------------------- */
29241 +unsigned int au_sigen_inc(struct super_block *sb)
29243 + unsigned int gen;
29244 + struct inode *inode;
29246 + SiMustWriteLock(sb);
29248 + gen = ++au_sbi(sb)->si_generation;
29249 + au_update_digen(sb->s_root);
29250 + inode = d_inode(sb->s_root);
29251 + au_update_iigen(inode, /*half*/0);
29252 + inode_inc_iversion(inode);
29256 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29258 + aufs_bindex_t br_id;
29260 + struct au_sbinfo *sbinfo;
29262 + SiMustWriteLock(sb);
29264 + sbinfo = au_sbi(sb);
29265 + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29266 + br_id = ++sbinfo->si_last_br_id;
29267 + AuDebugOn(br_id < 0);
29268 + if (br_id && au_br_index(sb, br_id) < 0)
29275 +/* ---------------------------------------------------------------------- */
29277 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29278 +int si_read_lock(struct super_block *sb, int flags)
29283 + if (au_ftest_lock(flags, FLUSH))
29284 + au_nwt_flush(&au_sbi(sb)->si_nowait);
29286 + si_noflush_read_lock(sb);
29287 + err = au_plink_maint(sb, flags);
29288 + if (unlikely(err))
29289 + si_read_unlock(sb);
29294 +int si_write_lock(struct super_block *sb, int flags)
29298 + if (au_ftest_lock(flags, FLUSH))
29299 + au_nwt_flush(&au_sbi(sb)->si_nowait);
29301 + si_noflush_write_lock(sb);
29302 + err = au_plink_maint(sb, flags);
29303 + if (unlikely(err))
29304 + si_write_unlock(sb);
29309 +/* dentry and super_block lock. call at entry point */
29310 +int aufs_read_lock(struct dentry *dentry, int flags)
29313 + struct super_block *sb;
29315 + sb = dentry->d_sb;
29316 + err = si_read_lock(sb, flags);
29317 + if (unlikely(err))
29320 + if (au_ftest_lock(flags, DW))
29321 + di_write_lock_child(dentry);
29323 + di_read_lock_child(dentry, flags);
29325 + if (au_ftest_lock(flags, GEN)) {
29326 + err = au_digen_test(dentry, au_sigen(sb));
29327 + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29328 + AuDebugOn(!err && au_dbrange_test(dentry));
29330 + err = au_dbrange_test(dentry);
29331 + if (unlikely(err))
29332 + aufs_read_unlock(dentry, flags);
29339 +void aufs_read_unlock(struct dentry *dentry, int flags)
29341 + if (au_ftest_lock(flags, DW))
29342 + di_write_unlock(dentry);
29344 + di_read_unlock(dentry, flags);
29345 + si_read_unlock(dentry->d_sb);
29348 +void aufs_write_lock(struct dentry *dentry)
29350 + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29351 + di_write_lock_child(dentry);
29354 +void aufs_write_unlock(struct dentry *dentry)
29356 + di_write_unlock(dentry);
29357 + si_write_unlock(dentry->d_sb);
29360 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29363 + unsigned int sigen;
29364 + struct super_block *sb;
29367 + err = si_read_lock(sb, flags);
29368 + if (unlikely(err))
29371 + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29373 + if (au_ftest_lock(flags, GEN)) {
29374 + sigen = au_sigen(sb);
29375 + err = au_digen_test(d1, sigen);
29376 + AuDebugOn(!err && au_dbrange_test(d1));
29378 + err = au_digen_test(d2, sigen);
29379 + AuDebugOn(!err && au_dbrange_test(d2));
29381 + if (unlikely(err))
29382 + aufs_read_and_write_unlock2(d1, d2);
29389 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29391 + di_write_unlock2(d1, d2);
29392 + si_read_unlock(d1->d_sb);
29394 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
29395 new file mode 100644
29396 index 00000000000000..77c6d90808bb3f
29398 +++ b/fs/aufs/super.c
29400 +// SPDX-License-Identifier: GPL-2.0
29402 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29404 + * This program is free software; you can redistribute it and/or modify
29405 + * it under the terms of the GNU General Public License as published by
29406 + * the Free Software Foundation; either version 2 of the License, or
29407 + * (at your option) any later version.
29409 + * This program is distributed in the hope that it will be useful,
29410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29412 + * GNU General Public License for more details.
29414 + * You should have received a copy of the GNU General Public License
29415 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
29419 + * mount and super_block operations
29422 +#include <linux/iversion.h>
29423 +#include <linux/mm.h>
29424 +#include <linux/seq_file.h>
29425 +#include <linux/statfs.h>
29426 +#include <linux/vmalloc.h>
29430 + * super_operations
29432 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29434 + struct au_icntnr *c;
29436 + c = au_cache_alloc_icntnr(sb);
29438 + au_icntnr_init(c);
29439 + inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29440 + c->iinfo.ii_hinode = NULL;
29441 + return &c->vfs_inode;
29446 +static void aufs_destroy_inode(struct inode *inode)
29448 + if (!au_is_bad_inode(inode))
29449 + au_iinfo_fin(inode);
29452 +static void aufs_free_inode(struct inode *inode)
29454 + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29457 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29459 + struct inode *inode;
29462 + inode = iget_locked(sb, ino);
29463 + if (unlikely(!inode)) {
29464 + inode = ERR_PTR(-ENOMEM);
29467 + if (!(inode->i_state & I_NEW))
29470 + err = au_xigen_new(inode);
29472 + err = au_iinfo_init(inode);
29474 + inode_inc_iversion(inode);
29476 + iget_failed(inode);
29477 + inode = ERR_PTR(err);
29481 + /* never return NULL */
29482 + AuDebugOn(!inode);
29483 + AuTraceErrPtr(inode);
29487 +/* lock free root dinfo */
29488 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29491 + aufs_bindex_t bindex, bbot;
29492 + struct path path;
29493 + struct au_hdentry *hdp;
29494 + struct au_branch *br;
29495 + au_br_perm_str_t perm;
29498 + bbot = au_sbbot(sb);
29500 + hdp = au_hdentry(au_di(sb->s_root), bindex);
29501 + for (; !err && bindex <= bbot; bindex++, hdp++) {
29502 + br = au_sbr(sb, bindex);
29503 + path.mnt = au_br_mnt(br);
29504 + path.dentry = hdp->hd_dentry;
29505 + err = au_seq_path(seq, &path);
29507 + au_optstr_br_perm(&perm, br->br_perm);
29508 + seq_printf(seq, "=%s", perm.a);
29509 + if (bindex != bbot)
29510 + seq_putc(seq, ':');
29513 + if (unlikely(err || seq_has_overflowed(seq)))
29519 +static void au_gen_fmt(char *fmt, int len, const char *pat,
29520 + const char *append)
29525 + while (*pat != ':')
29528 + strscpy(p, append, len - (p - fmt));
29529 + AuDebugOn(strlen(fmt) >= len);
29532 +static void au_show_wbr_create(struct seq_file *m, int v,
29533 + struct au_sbinfo *sbinfo)
29537 + struct au_wbr_mfs *mfs;
29539 + AuRwMustAnyLock(&sbinfo->si_rwsem);
29541 + seq_puts(m, ",create=");
29542 + pat = au_optstr_wbr_create(v);
29543 + mfs = &sbinfo->si_wbr_mfs;
29545 + case AuWbrCreate_TDP:
29546 + case AuWbrCreate_RR:
29547 + case AuWbrCreate_MFS:
29548 + case AuWbrCreate_PMFS:
29549 + seq_puts(m, pat);
29551 + case AuWbrCreate_MFSRR:
29552 + case AuWbrCreate_TDMFS:
29553 + case AuWbrCreate_PMFSRR:
29554 + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29555 + seq_printf(m, fmt, mfs->mfsrr_watermark);
29557 + case AuWbrCreate_MFSV:
29558 + case AuWbrCreate_PMFSV:
29559 + au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29560 + seq_printf(m, fmt,
29561 + jiffies_to_msecs(mfs->mfs_expire)
29564 + case AuWbrCreate_MFSRRV:
29565 + case AuWbrCreate_TDMFSV:
29566 + case AuWbrCreate_PMFSRRV:
29567 + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29568 + seq_printf(m, fmt, mfs->mfsrr_watermark,
29569 + jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29576 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29578 +#ifdef CONFIG_SYSFS
29582 + const int len = sizeof(AUFS_XINO_FNAME) - 1;
29583 + aufs_bindex_t bindex, brid;
29584 + struct qstr *name;
29586 + struct dentry *d, *h_root;
29587 + struct au_branch *br;
29589 + AuRwMustAnyLock(&sbinfo->si_rwsem);
29592 + f = au_sbi(sb)->si_xib;
29596 + /* stop printing the default xino path on the first writable branch */
29598 + bindex = au_xi_root(sb, f->f_path.dentry);
29599 + if (bindex >= 0) {
29600 + br = au_sbr_sb(sb, bindex);
29601 + h_root = au_br_dentry(br);
29604 + d = f->f_path.dentry;
29605 + name = &d->d_name;
29606 + /* safe ->d_parent because the file is unlinked */
29607 + if (d->d_parent == h_root
29608 + && name->len == len
29609 + && !memcmp(name->name, AUFS_XINO_FNAME, len))
29612 + seq_puts(seq, ",xino=");
29613 + err = au_xino_path(seq, f);
29620 +/* seq_file will re-call me in case of too long string */
29621 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29624 + unsigned int mnt_flags, v;
29625 + struct super_block *sb;
29626 + struct au_sbinfo *sbinfo;
29628 +#define AuBool(name, str) do { \
29629 + v = au_opt_test(mnt_flags, name); \
29630 + if (v != au_opt_test(AuOpt_Def, name)) \
29631 + seq_printf(m, ",%s" #str, v ? "" : "no"); \
29634 +#define AuStr(name, str) do { \
29635 + v = mnt_flags & AuOptMask_##name; \
29636 + if (v != (AuOpt_Def & AuOptMask_##name)) \
29637 + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29640 +#define AuUInt(name, str, val) do { \
29641 + if (val != AUFS_##name##_DEF) \
29642 + seq_printf(m, "," #str "=%u", val); \
29645 + sb = dentry->d_sb;
29646 + if (sb->s_flags & SB_POSIXACL)
29647 + seq_puts(m, ",acl");
29648 +#if 0 /* reserved for future use */
29649 + if (sb->s_flags & SB_I_VERSION)
29650 + seq_puts(m, ",i_version");
29653 + /* lock free root dinfo */
29654 + si_noflush_read_lock(sb);
29655 + sbinfo = au_sbi(sb);
29656 + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29658 + mnt_flags = au_mntflags(sb);
29659 + if (au_opt_test(mnt_flags, XINO)) {
29660 + err = au_show_xino(m, sb);
29661 + if (unlikely(err))
29664 + seq_puts(m, ",noxino");
29666 + AuBool(TRUNC_XINO, trunc_xino);
29667 + AuStr(UDBA, udba);
29668 + AuBool(SHWH, shwh);
29669 + AuBool(PLINK, plink);
29670 + AuBool(DIO, dio);
29671 + AuBool(DIRPERM1, dirperm1);
29673 + v = sbinfo->si_wbr_create;
29674 + if (v != AuWbrCreate_Def)
29675 + au_show_wbr_create(m, v, sbinfo);
29677 + v = sbinfo->si_wbr_copyup;
29678 + if (v != AuWbrCopyup_Def)
29679 + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29681 + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29682 + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29683 + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29685 + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29687 + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29688 + AuUInt(RDCACHE, rdcache, v);
29690 + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29691 + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29693 + au_fhsm_show(m, sbinfo);
29695 + AuBool(DIRREN, dirren);
29696 + AuBool(SUM, sum);
29697 + /* AuBool(SUM_W, wsum); */
29698 + AuBool(WARN_PERM, warn_perm);
29699 + AuBool(VERBOSE, verbose);
29702 + /* be sure to print "br:" last */
29703 + if (!sysaufs_brs) {
29704 + seq_puts(m, ",br:");
29705 + au_show_brs(m, sb);
29707 + si_read_unlock(sb);
29715 +/* ---------------------------------------------------------------------- */
29717 +/* sum mode which returns the summation for statfs(2) */
29719 +static u64 au_add_till_max(u64 a, u64 b)
29727 + return ULLONG_MAX;
29730 +static u64 au_mul_till_max(u64 a, long mul)
29738 + return ULLONG_MAX;
29741 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29744 + long bsize, factor;
29745 + u64 blocks, bfree, bavail, files, ffree;
29746 + aufs_bindex_t bbot, bindex, i;
29747 + unsigned char shared;
29748 + struct path h_path;
29749 + struct super_block *h_sb;
29752 + bsize = LONG_MAX;
29758 + bbot = au_sbbot(sb);
29759 + for (bindex = 0; bindex <= bbot; bindex++) {
29760 + h_path.mnt = au_sbr_mnt(sb, bindex);
29761 + h_sb = h_path.mnt->mnt_sb;
29763 + for (i = 0; !shared && i < bindex; i++)
29764 + shared = (au_sbr_sb(sb, i) == h_sb);
29768 + /* sb->s_root for NFS is unreliable */
29769 + h_path.dentry = h_path.mnt->mnt_root;
29770 + err = vfs_statfs(&h_path, buf);
29771 + if (unlikely(err))
29774 + if (bsize > buf->f_bsize) {
29776 + * we will reduce bsize, so we have to expand blocks
29777 + * etc. to match them again
29779 + factor = (bsize / buf->f_bsize);
29780 + blocks = au_mul_till_max(blocks, factor);
29781 + bfree = au_mul_till_max(bfree, factor);
29782 + bavail = au_mul_till_max(bavail, factor);
29783 + bsize = buf->f_bsize;
29786 + factor = (buf->f_bsize / bsize);
29787 + blocks = au_add_till_max(blocks,
29788 + au_mul_till_max(buf->f_blocks, factor));
29789 + bfree = au_add_till_max(bfree,
29790 + au_mul_till_max(buf->f_bfree, factor));
29791 + bavail = au_add_till_max(bavail,
29792 + au_mul_till_max(buf->f_bavail, factor));
29793 + files = au_add_till_max(files, buf->f_files);
29794 + ffree = au_add_till_max(ffree, buf->f_ffree);
29797 + buf->f_bsize = bsize;
29798 + buf->f_blocks = blocks;
29799 + buf->f_bfree = bfree;
29800 + buf->f_bavail = bavail;
29801 + buf->f_files = files;
29802 + buf->f_ffree = ffree;
29803 + buf->f_frsize = 0;
29809 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29812 + struct path h_path;
29813 + struct super_block *sb;
29815 + /* lock free root dinfo */
29816 + sb = dentry->d_sb;
29817 + si_noflush_read_lock(sb);
29818 + if (!au_opt_test(au_mntflags(sb), SUM)) {
29819 + /* sb->s_root for NFS is unreliable */
29820 + h_path.mnt = au_sbr_mnt(sb, 0);
29821 + h_path.dentry = h_path.mnt->mnt_root;
29822 + err = vfs_statfs(&h_path, buf);
29824 + err = au_statfs_sum(sb, buf);
29825 + si_read_unlock(sb);
29828 + buf->f_type = AUFS_SUPER_MAGIC;
29829 + buf->f_namelen = AUFS_MAX_NAMELEN;
29830 + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29832 + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29837 +/* ---------------------------------------------------------------------- */
29839 +static int aufs_sync_fs(struct super_block *sb, int wait)
29842 + aufs_bindex_t bbot, bindex;
29843 + struct au_branch *br;
29844 + struct super_block *h_sb;
29847 + si_noflush_read_lock(sb);
29848 + bbot = au_sbbot(sb);
29849 + for (bindex = 0; bindex <= bbot; bindex++) {
29850 + br = au_sbr(sb, bindex);
29851 + if (!au_br_writable(br->br_perm))
29854 + h_sb = au_sbr_sb(sb, bindex);
29855 + e = vfsub_sync_filesystem(h_sb);
29856 + if (unlikely(e && !err))
29858 + /* go on even if an error happens */
29860 + si_read_unlock(sb);
29865 +/* ---------------------------------------------------------------------- */
29867 +/* final actions when unmounting a file system */
29868 +static void aufs_put_super(struct super_block *sb)
29870 + struct au_sbinfo *sbinfo;
29872 + sbinfo = au_sbi(sb);
29874 + kobject_put(&sbinfo->si_kobj);
29877 +/* ---------------------------------------------------------------------- */
29879 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29880 + struct super_block *sb, void *arg)
29883 + unsigned long long n, sz;
29890 + if (*hint > ULLONG_MAX / sizeof(array)) {
29891 + array = ERR_PTR(-EMFILE);
29892 + pr_err("hint %llu\n", *hint);
29896 + sz = sizeof(array) * *hint;
29897 + array = kzalloc(sz, GFP_NOFS);
29898 + if (unlikely(!array))
29899 + array = vzalloc(sz);
29900 + if (unlikely(!array)) {
29901 + array = ERR_PTR(-ENOMEM);
29905 + n = cb(sb, array, *hint, arg);
29906 + AuDebugOn(n > *hint);
29913 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29914 + unsigned long long max __maybe_unused,
29917 + unsigned long long n;
29918 + struct inode **p, *inode;
29919 + struct list_head *head;
29924 + spin_lock(&sb->s_inode_list_lock);
29925 + list_for_each_entry(inode, head, i_sb_list) {
29926 + if (!au_is_bad_inode(inode)
29927 + && au_ii(inode)->ii_btop >= 0) {
29928 + spin_lock(&inode->i_lock);
29929 + if (atomic_read(&inode->i_count)) {
29933 + AuDebugOn(n > max);
29935 + spin_unlock(&inode->i_lock);
29938 + spin_unlock(&sb->s_inode_list_lock);
29943 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29945 + struct au_sbinfo *sbi;
29947 + sbi = au_sbi(sb);
29948 + *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29949 + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29952 +void au_iarray_free(struct inode **a, unsigned long long max)
29954 + unsigned long long ull;
29956 + for (ull = 0; ull < max; ull++)
29961 +/* ---------------------------------------------------------------------- */
29964 + * refresh dentry and inode at remount time.
29966 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29967 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29968 + struct dentry *parent)
29972 + di_write_lock_child(dentry);
29973 + di_read_lock_parent(parent, AuLock_IR);
29974 + err = au_refresh_dentry(dentry, parent);
29975 + if (!err && dir_flags)
29976 + au_hn_reset(d_inode(dentry), dir_flags);
29977 + di_read_unlock(parent, AuLock_IR);
29978 + di_write_unlock(dentry);
29983 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29984 + struct au_sbinfo *sbinfo,
29985 + const unsigned int dir_flags, unsigned int do_idop)
29988 + struct dentry *parent;
29991 + parent = dget_parent(dentry);
29992 + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29993 + if (d_really_is_positive(dentry)) {
29994 + if (!d_is_dir(dentry))
29995 + err = au_do_refresh(dentry, /*dir_flags*/0,
29998 + err = au_do_refresh(dentry, dir_flags, parent);
29999 + if (unlikely(err))
30000 + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30003 + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30004 + AuDbgDentry(dentry);
30010 + au_refresh_dop(dentry, /*force_reval*/0);
30012 + au_refresh_dop(dentry, /*force_reval*/1);
30018 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30020 + int err, i, j, ndentry, e;
30021 + unsigned int sigen;
30022 + struct au_dcsub_pages dpages;
30023 + struct au_dpage *dpage;
30024 + struct dentry **dentries, *d;
30025 + struct au_sbinfo *sbinfo;
30026 + struct dentry *root = sb->s_root;
30027 + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30030 + au_refresh_dop(root, /*force_reval*/0);
30032 + err = au_dpages_init(&dpages, GFP_NOFS);
30033 + if (unlikely(err))
30035 + err = au_dcsub_pages(&dpages, root, NULL, NULL);
30036 + if (unlikely(err))
30039 + sigen = au_sigen(sb);
30040 + sbinfo = au_sbi(sb);
30041 + for (i = 0; i < dpages.ndpage; i++) {
30042 + dpage = dpages.dpages + i;
30043 + dentries = dpage->dentries;
30044 + ndentry = dpage->ndentry;
30045 + for (j = 0; j < ndentry; j++) {
30047 + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30049 + if (unlikely(e && !err))
30051 + /* go on even err */
30056 + au_dpages_free(&dpages);
30061 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30064 + unsigned int sigen;
30065 + unsigned long long max, ull;
30066 + struct inode *inode, **array;
30068 + array = au_iarray_alloc(sb, &max);
30069 + err = PTR_ERR(array);
30070 + if (IS_ERR(array))
30074 + sigen = au_sigen(sb);
30075 + for (ull = 0; ull < max; ull++) {
30076 + inode = array[ull];
30077 + if (unlikely(!inode))
30081 + ii_write_lock_child(inode);
30082 + if (au_iigen(inode, NULL) != sigen) {
30083 + e = au_refresh_hinode_self(inode);
30084 + if (unlikely(e)) {
30085 + au_refresh_iop(inode, /*force_getattr*/1);
30086 + pr_err("error %d, i%lu\n", e, inode->i_ino);
30089 + /* go on even if err */
30092 + if (!e && do_idop)
30093 + au_refresh_iop(inode, /*force_getattr*/0);
30094 + ii_write_unlock(inode);
30097 + au_iarray_free(array, max);
30103 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30106 + unsigned int udba;
30107 + aufs_bindex_t bindex, bbot;
30108 + struct dentry *root;
30109 + struct inode *inode;
30110 + struct au_branch *br;
30111 + struct au_sbinfo *sbi;
30113 + au_sigen_inc(sb);
30114 + sbi = au_sbi(sb);
30115 + au_fclr_si(sbi, FAILED_REFRESH_DIR);
30117 + root = sb->s_root;
30118 + DiMustNoWaiters(root);
30119 + inode = d_inode(root);
30120 + IiMustNoWaiters(inode);
30122 + udba = au_opt_udba(sb);
30123 + bbot = au_sbbot(sb);
30124 + for (bindex = 0; bindex <= bbot; bindex++) {
30125 + br = au_sbr(sb, bindex);
30126 + err = au_hnotify_reset_br(udba, br, br->br_perm);
30127 + if (unlikely(err))
30128 + AuIOErr("hnotify failed on br %d, %d, ignored\n",
30130 + /* go on even if err */
30132 + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30135 + if (au_ftest_si(sbi, NO_DREVAL)) {
30136 + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30137 + sb->s_d_op = &aufs_dop_noreval;
30138 + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30139 + sbi->si_iop_array = aufs_iop_nogetattr;
30141 + AuDebugOn(sb->s_d_op == &aufs_dop);
30142 + sb->s_d_op = &aufs_dop;
30143 + AuDebugOn(sbi->si_iop_array == aufs_iop);
30144 + sbi->si_iop_array = aufs_iop;
30146 + pr_info("reset to %ps and %ps\n",
30147 + sb->s_d_op, sbi->si_iop_array);
30150 + di_write_unlock(root);
30151 + err = au_refresh_d(sb, do_idop);
30152 + e = au_refresh_i(sb, do_idop);
30153 + if (unlikely(e && !err))
30155 + /* aufs_write_lock() calls ..._child() */
30156 + di_write_lock_child(root);
30158 + au_cpup_attr_all(inode, /*force*/1);
30160 + if (unlikely(err))
30161 + AuIOErr("refresh failed, ignored, %d\n", err);
30164 +const struct super_operations aufs_sop = {
30165 + .alloc_inode = aufs_alloc_inode,
30166 + .destroy_inode = aufs_destroy_inode,
30167 + .free_inode = aufs_free_inode,
30168 + /* always deleting, no clearing */
30169 + .drop_inode = generic_delete_inode,
30170 + .show_options = aufs_show_options,
30171 + .statfs = aufs_statfs,
30172 + .put_super = aufs_put_super,
30173 + .sync_fs = aufs_sync_fs
30176 +/* ---------------------------------------------------------------------- */
30178 +int au_alloc_root(struct super_block *sb)
30181 + struct inode *inode;
30182 + struct dentry *root;
30185 + inode = au_iget_locked(sb, AUFS_ROOT_INO);
30186 + err = PTR_ERR(inode);
30187 + if (IS_ERR(inode))
30190 + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30191 + inode->i_fop = &aufs_dir_fop;
30192 + inode->i_mode = S_IFDIR;
30193 + set_nlink(inode, 2);
30194 + unlock_new_inode(inode);
30196 + root = d_make_root(inode);
30197 + if (unlikely(!root))
30199 + err = PTR_ERR(root);
30200 + if (IS_ERR(root))
30203 + err = au_di_init(root);
30205 + sb->s_root = root;
30206 + return 0; /* success */
30214 +/* ---------------------------------------------------------------------- */
30216 +static void aufs_kill_sb(struct super_block *sb)
30218 + struct au_sbinfo *sbinfo;
30219 + struct dentry *root;
30221 + sbinfo = au_sbi(sb);
30225 + au_sbilist_del(sb);
30227 + root = sb->s_root;
30229 + aufs_write_lock(root);
30231 + __si_write_lock(sb);
30234 + if (sbinfo->si_wbr_create_ops->fin)
30235 + sbinfo->si_wbr_create_ops->fin(sb);
30236 + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30237 + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30238 + au_remount_refresh(sb, /*do_idop*/0);
30240 + if (au_opt_test(sbinfo->si_mntflags, PLINK))
30241 + au_plink_put(sb, /*verbose*/1);
30244 + au_dr_opt_flush(sb);
30247 + aufs_write_unlock(root);
30249 + __si_write_unlock(sb);
30251 + sbinfo->si_sb = NULL;
30252 + au_nwt_flush(&sbinfo->si_nowait);
30255 + kill_anon_super(sb);
30258 +struct file_system_type aufs_fs_type = {
30259 + .name = AUFS_FSTYPE,
30260 + /* a race between rename and others */
30261 + .fs_flags = FS_RENAME_DOES_D_MOVE
30263 + /*| FS_ALLOW_IDMAP*/
30265 + .init_fs_context = aufs_fsctx_init,
30266 + .parameters = aufs_fsctx_paramspec,
30267 + .kill_sb = aufs_kill_sb,
30268 + /* no need to __module_get() and module_put(). */
30269 + .owner = THIS_MODULE,
30271 diff --git a/fs/aufs/super.h b/fs/aufs/super.h
30272 new file mode 100644
30273 index 00000000000000..f1d9aedcbca4f3
30275 +++ b/fs/aufs/super.h
30277 +/* SPDX-License-Identifier: GPL-2.0 */
30279 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30281 + * This program is free software; you can redistribute it and/or modify
30282 + * it under the terms of the GNU General Public License as published by
30283 + * the Free Software Foundation; either version 2 of the License, or
30284 + * (at your option) any later version.
30286 + * This program is distributed in the hope that it will be useful,
30287 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30288 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30289 + * GNU General Public License for more details.
30291 + * You should have received a copy of the GNU General Public License
30292 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
30296 + * super_block operations
30299 +#ifndef __AUFS_SUPER_H__
30300 +#define __AUFS_SUPER_H__
30304 +#include <linux/fs.h>
30305 +#include <linux/kobject.h>
30308 +#include "rwsem.h"
30311 +/* policies to select one among multiple writable branches */
30312 +struct au_wbr_copyup_operations {
30313 + int (*copyup)(struct dentry *dentry);
30316 +#define AuWbr_DIR 1 /* target is a dir */
30317 +#define AuWbr_PARENT (1 << 1) /* always require a parent */
30319 +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30320 +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30321 +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30323 +struct au_wbr_create_operations {
30324 + int (*create)(struct dentry *dentry, unsigned int flags);
30325 + int (*init)(struct super_block *sb);
30326 + int (*fin)(struct super_block *sb);
30329 +struct au_wbr_mfs {
30330 + struct mutex mfs_lock; /* protect this structure */
30331 + unsigned long mfs_jiffy;
30332 + unsigned long mfs_expire;
30333 + aufs_bindex_t mfs_bindex;
30335 + unsigned long long mfsrr_bytes;
30336 + unsigned long long mfsrr_watermark;
30339 +#define AuPlink_NHASH 100
30340 +static inline int au_plink_hash(ino_t ino)
30342 + return ino % AuPlink_NHASH;
30345 +/* File-based Hierarchical Storage Management */
30347 +#ifdef CONFIG_AUFS_FHSM
30348 + /* allow only one process who can receive the notification */
30349 + spinlock_t fhsm_spin;
30351 + wait_queue_head_t fhsm_wqh;
30352 + atomic_t fhsm_readable;
30354 + /* these are protected by si_rwsem */
30355 + unsigned long fhsm_expire;
30356 + aufs_bindex_t fhsm_bottom;
30361 +struct au_sbinfo {
30362 + /* nowait tasks in the system-wide workqueue */
30363 + struct au_nowait_tasks si_nowait;
30366 + * tried sb->s_umount, but failed due to the dependency between i_mutex.
30367 + * rwsem for au_sbinfo is necessary.
30369 + struct au_rwsem si_rwsem;
30372 + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30375 + au_lcnt_t si_ninodes, si_nfiles;
30377 + /* branch management */
30378 + unsigned int si_generation;
30380 + /* see AuSi_ flags */
30381 + unsigned char au_si_status;
30383 + aufs_bindex_t si_bbot;
30385 + /* dirty trick to keep br_id plus */
30386 + unsigned int si_last_br_id :
30387 + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30388 + struct au_branch **si_branch;
30390 + /* policy to select a writable branch */
30391 + unsigned char si_wbr_copyup;
30392 + unsigned char si_wbr_create;
30393 + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30394 + struct au_wbr_create_operations *si_wbr_create_ops;
30396 + /* round robin */
30397 + atomic_t si_wbr_rr_next;
30399 + /* most free space */
30400 + struct au_wbr_mfs si_wbr_mfs;
30402 + /* File-based Hierarchical Storage Management */
30403 + struct au_fhsm si_fhsm;
30405 + /* mount flags */
30406 + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30407 + unsigned int si_mntflags;
30409 + /* external inode number (bitmap and translation table) */
30410 + loff_t si_ximaxent; /* max entries in a xino */
30412 + struct file *si_xib;
30413 + struct mutex si_xib_mtx; /* protect xib members */
30414 + unsigned long *si_xib_buf;
30415 + unsigned long si_xib_last_pindex;
30416 + int si_xib_next_bit;
30418 + unsigned long si_xino_jiffy;
30419 + unsigned long si_xino_expire;
30420 + /* reserved for future use */
30421 + /* unsigned long long si_xib_limit; */ /* Max xib file size */
30423 +#ifdef CONFIG_AUFS_EXPORT
30424 + /* i_generation */
30425 + /* todo: make xigen file an array to support many inode numbers */
30426 + struct file *si_xigen;
30427 + atomic_t si_xigen_next;
30430 + /* dirty trick to support atomic_open */
30431 + struct hlist_bl_head si_aopen;
30433 + /* vdir parameters */
30434 + unsigned long si_rdcache; /* max cache time in jiffies */
30435 + unsigned int si_rdblk; /* deblk size */
30436 + unsigned int si_rdhash; /* hash size */
30439 + * If the number of whiteouts are larger than si_dirwh, leave all of
30440 + * them after au_whtmp_ren to reduce the cost of rmdir(2).
30441 + * future fsck.aufs or kernel thread will remove them later.
30442 + * Otherwise, remove all whiteouts and the dir in rmdir(2).
30444 + unsigned int si_dirwh;
30446 + /* pseudo_link list */
30447 + struct hlist_bl_head si_plink[AuPlink_NHASH];
30448 + wait_queue_head_t si_plink_wq;
30449 + spinlock_t si_plink_maint_lock;
30450 + pid_t si_plink_maint_pid;
30453 + struct hlist_bl_head si_files;
30455 + /* with/without getattr, brother of sb->s_d_op */
30456 + const struct inode_operations *si_iop_array;
30459 + * sysfs and lifetime management.
30460 + * this is not a small structure and it may be a waste of memory in case
30461 + * of sysfs is disabled, particularly when many aufs-es are mounted.
30462 + * but using sysfs is majority.
30464 + struct kobject si_kobj;
30465 +#ifdef CONFIG_DEBUG_FS
30466 + struct dentry *si_dbgaufs;
30467 + struct dentry *si_dbgaufs_plink;
30468 + struct dentry *si_dbgaufs_xib;
30469 +#ifdef CONFIG_AUFS_EXPORT
30470 + struct dentry *si_dbgaufs_xigen;
30474 +#ifdef CONFIG_AUFS_SBILIST
30475 + struct hlist_bl_node si_list;
30478 + /* dirty, necessary for unmounting, sysfs and sysrq */
30479 + struct super_block *si_sb;
30482 +/* sbinfo status flags */
30484 + * set true when refresh_dirs() failed at remount time.
30485 + * then try refreshing dirs at access time again.
30486 + * if it is false, refreshing dirs at access time is unnecessary
30488 +#define AuSi_FAILED_REFRESH_DIR 1
30489 +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
30490 +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
30492 +#ifndef CONFIG_AUFS_FHSM
30494 +#define AuSi_FHSM 0
30497 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30498 + unsigned int flag)
30500 + AuRwMustAnyLock(&sbi->si_rwsem);
30501 + return sbi->au_si_status & flag;
30503 +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30504 +#define au_fset_si(sbinfo, name) do { \
30505 + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30506 + (sbinfo)->au_si_status |= AuSi_##name; \
30508 +#define au_fclr_si(sbinfo, name) do { \
30509 + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30510 + (sbinfo)->au_si_status &= ~AuSi_##name; \
30513 +/* ---------------------------------------------------------------------- */
30515 +/* policy to select one among writable branches */
30516 +#define AuWbrCopyup(sbinfo, ...) \
30517 + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30518 +#define AuWbrCreate(sbinfo, ...) \
30519 + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30521 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30522 +#define AuLock_DW 1 /* write-lock dentry */
30523 +#define AuLock_IR (1 << 1) /* read-lock inode */
30524 +#define AuLock_IW (1 << 2) /* write-lock inode */
30525 +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
30526 +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
30527 + /* except RENAME_EXCHANGE */
30528 +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30529 +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
30530 +#define AuLock_GEN (1 << 7) /* test digen/iigen */
30531 +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
30532 +#define au_fset_lock(flags, name) \
30533 + do { (flags) |= AuLock_##name; } while (0)
30534 +#define au_fclr_lock(flags, name) \
30535 + do { (flags) &= ~AuLock_##name; } while (0)
30537 +/* ---------------------------------------------------------------------- */
30540 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30542 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30543 + unsigned long long max, void *arg);
30544 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30545 + struct super_block *sb, void *arg);
30546 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30547 +void au_iarray_free(struct inode **a, unsigned long long max);
30549 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30550 +extern const struct super_operations aufs_sop;
30551 +int au_alloc_root(struct super_block *sb);
30552 +extern struct file_system_type aufs_fs_type;
30555 +void au_si_free(struct kobject *kobj);
30556 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
30557 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30559 +unsigned int au_sigen_inc(struct super_block *sb);
30560 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30562 +int si_read_lock(struct super_block *sb, int flags);
30563 +int si_write_lock(struct super_block *sb, int flags);
30564 +int aufs_read_lock(struct dentry *dentry, int flags);
30565 +void aufs_read_unlock(struct dentry *dentry, int flags);
30566 +void aufs_write_lock(struct dentry *dentry);
30567 +void aufs_write_unlock(struct dentry *dentry);
30568 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30569 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30571 +/* wbr_policy.c */
30572 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30573 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30574 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30575 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30576 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30579 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30581 +#ifdef CONFIG_AUFS_FHSM
30584 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30588 + spin_lock(&fhsm->fhsm_spin);
30589 + pid = fhsm->fhsm_pid;
30590 + spin_unlock(&fhsm->fhsm_spin);
30595 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30596 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30597 +int au_fhsm_fd(struct super_block *sb, int oflags);
30598 +int au_fhsm_br_alloc(struct au_branch *br);
30599 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30600 +void au_fhsm_fin(struct super_block *sb);
30601 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30602 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30603 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30605 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30607 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30608 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30609 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30610 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30611 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30612 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30613 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30614 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30615 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30618 +/* ---------------------------------------------------------------------- */
30620 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30622 + return sb->s_fs_info;
30625 +/* ---------------------------------------------------------------------- */
30627 +#ifdef CONFIG_AUFS_EXPORT
30628 +int au_test_nfsd(void);
30629 +void au_export_init(struct super_block *sb);
30630 +void au_xigen_inc(struct inode *inode);
30631 +int au_xigen_new(struct inode *inode);
30632 +int au_xigen_set(struct super_block *sb, struct path *path);
30633 +void au_xigen_clr(struct super_block *sb);
30635 +static inline int au_busy_or_stale(void)
30637 + if (!au_test_nfsd())
30642 +AuStubInt0(au_test_nfsd, void)
30643 +AuStubVoid(au_export_init, struct super_block *sb)
30644 +AuStubVoid(au_xigen_inc, struct inode *inode)
30645 +AuStubInt0(au_xigen_new, struct inode *inode)
30646 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30647 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30648 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30649 +#endif /* CONFIG_AUFS_EXPORT */
30651 +/* ---------------------------------------------------------------------- */
30653 +#ifdef CONFIG_AUFS_SBILIST
30655 +extern struct hlist_bl_head au_sbilist;
30657 +static inline void au_sbilist_init(void)
30659 + INIT_HLIST_BL_HEAD(&au_sbilist);
30662 +static inline void au_sbilist_add(struct super_block *sb)
30664 + au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30667 +static inline void au_sbilist_del(struct super_block *sb)
30669 + au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30672 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30673 +static inline void au_sbilist_lock(void)
30675 + hlist_bl_lock(&au_sbilist);
30678 +static inline void au_sbilist_unlock(void)
30680 + hlist_bl_unlock(&au_sbilist);
30682 +#define AuGFP_SBILIST GFP_ATOMIC
30684 +AuStubVoid(au_sbilist_lock, void)
30685 +AuStubVoid(au_sbilist_unlock, void)
30686 +#define AuGFP_SBILIST GFP_NOFS
30687 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30689 +AuStubVoid(au_sbilist_init, void)
30690 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30691 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30692 +AuStubVoid(au_sbilist_lock, void)
30693 +AuStubVoid(au_sbilist_unlock, void)
30694 +#define AuGFP_SBILIST GFP_NOFS
30697 +/* ---------------------------------------------------------------------- */
30699 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30702 + * This function is a dynamic '__init' function actually,
30703 + * so the tiny check for si_rwsem is unnecessary.
30705 + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30706 +#ifdef CONFIG_DEBUG_FS
30707 + sbinfo->si_dbgaufs = NULL;
30708 + sbinfo->si_dbgaufs_plink = NULL;
30709 + sbinfo->si_dbgaufs_xib = NULL;
30710 +#ifdef CONFIG_AUFS_EXPORT
30711 + sbinfo->si_dbgaufs_xigen = NULL;
30716 +/* ---------------------------------------------------------------------- */
30718 +/* current->atomic_flags */
30719 +/* this value should never corrupt the ones defined in linux/sched.h */
30720 +#define PFA_AUFS 0x10
30722 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30723 +TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30724 +TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
30726 +static inline int si_pid_test(struct super_block *sb)
30728 + return !!task_test_aufs(current);
30731 +static inline void si_pid_clr(struct super_block *sb)
30733 + AuDebugOn(!task_test_aufs(current));
30734 + task_clear_aufs(current);
30737 +static inline void si_pid_set(struct super_block *sb)
30739 + AuDebugOn(task_test_aufs(current));
30740 + task_set_aufs(current);
30743 +/* ---------------------------------------------------------------------- */
30745 +/* lock superblock. mainly for entry point functions */
30746 +#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30747 +#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30748 +#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30749 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30751 +#define __si_read_trylock_nested(sb) \
30752 + au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30753 +#define __si_write_trylock_nested(sb) \
30754 + au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30757 +#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30758 +#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30759 +#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30761 +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30762 +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30763 +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30765 +static inline void si_noflush_read_lock(struct super_block *sb)
30767 + __si_read_lock(sb);
30771 +static inline int si_noflush_read_trylock(struct super_block *sb)
30775 + locked = __si_read_trylock(sb);
30781 +static inline void si_noflush_write_lock(struct super_block *sb)
30783 + __si_write_lock(sb);
30787 +static inline int si_noflush_write_trylock(struct super_block *sb)
30791 + locked = __si_write_trylock(sb);
30797 +#if 0 /* reserved */
30798 +static inline int si_read_trylock(struct super_block *sb, int flags)
30800 + if (au_ftest_lock(flags, FLUSH))
30801 + au_nwt_flush(&au_sbi(sb)->si_nowait);
30802 + return si_noflush_read_trylock(sb);
30806 +static inline void si_read_unlock(struct super_block *sb)
30809 + __si_read_unlock(sb);
30812 +#if 0 /* reserved */
30813 +static inline int si_write_trylock(struct super_block *sb, int flags)
30815 + if (au_ftest_lock(flags, FLUSH))
30816 + au_nwt_flush(&au_sbi(sb)->si_nowait);
30817 + return si_noflush_write_trylock(sb);
30821 +static inline void si_write_unlock(struct super_block *sb)
30824 + __si_write_unlock(sb);
30827 +#if 0 /* reserved */
30828 +static inline void si_downgrade_lock(struct super_block *sb)
30830 + __si_downgrade_lock(sb);
30834 +/* ---------------------------------------------------------------------- */
30836 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30838 + SiMustAnyLock(sb);
30839 + return au_sbi(sb)->si_bbot;
30842 +static inline unsigned int au_mntflags(struct super_block *sb)
30844 + SiMustAnyLock(sb);
30845 + return au_sbi(sb)->si_mntflags;
30848 +static inline unsigned int au_sigen(struct super_block *sb)
30850 + SiMustAnyLock(sb);
30851 + return au_sbi(sb)->si_generation;
30854 +static inline struct au_branch *au_sbr(struct super_block *sb,
30855 + aufs_bindex_t bindex)
30857 + SiMustAnyLock(sb);
30858 + return au_sbi(sb)->si_branch[0 + bindex];
30861 +static inline loff_t au_xi_maxent(struct super_block *sb)
30863 + SiMustAnyLock(sb);
30864 + return au_sbi(sb)->si_ximaxent;
30867 +#endif /* __KERNEL__ */
30868 +#endif /* __AUFS_SUPER_H__ */
30869 diff --git a/fs/aufs/sysaufs.c b/fs/aufs/sysaufs.c
30870 new file mode 100644
30871 index 00000000000000..ab109d55040c31
30873 +++ b/fs/aufs/sysaufs.c
30875 +// SPDX-License-Identifier: GPL-2.0
30877 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30879 + * This program is free software; you can redistribute it and/or modify
30880 + * it under the terms of the GNU General Public License as published by
30881 + * the Free Software Foundation; either version 2 of the License, or
30882 + * (at your option) any later version.
30884 + * This program is distributed in the hope that it will be useful,
30885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30887 + * GNU General Public License for more details.
30889 + * You should have received a copy of the GNU General Public License
30890 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
30894 + * sysfs interface and lifetime management
30895 + * they are necessary regardless sysfs is disabled.
30898 +#include <linux/random.h>
30901 +unsigned long sysaufs_si_mask;
30902 +struct kset *sysaufs_kset;
30904 +#define AuSiAttr(_name) { \
30905 + .attr = { .name = __stringify(_name), .mode = 0444 }, \
30906 + .show = sysaufs_si_##_name, \
30909 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30910 +struct attribute *sysaufs_si_attrs[] = {
30911 + &sysaufs_si_attr_xi_path.attr,
30914 +ATTRIBUTE_GROUPS(sysaufs_si);
30916 +static const struct sysfs_ops au_sbi_ops = {
30917 + .show = sysaufs_si_show
30920 +static const struct kobj_type au_sbi_ktype = {
30921 + .release = au_si_free,
30922 + .sysfs_ops = &au_sbi_ops,
30923 + .default_groups = sysaufs_si_groups
30926 +/* ---------------------------------------------------------------------- */
30928 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
30932 + sbinfo->si_kobj.kset = sysaufs_kset;
30933 + /* cf. sysaufs_name() */
30934 + err = kobject_init_and_add
30935 + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
30936 + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30941 +void sysaufs_fin(void)
30943 + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30944 + kset_unregister(sysaufs_kset);
30947 +int __init sysaufs_init(void)
30952 + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30953 + } while (!sysaufs_si_mask);
30956 + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30957 + if (unlikely(!sysaufs_kset))
30959 + err = PTR_ERR(sysaufs_kset);
30960 + if (IS_ERR(sysaufs_kset))
30962 + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30963 + if (unlikely(err))
30964 + kset_unregister(sysaufs_kset);
30969 diff --git a/fs/aufs/sysaufs.h b/fs/aufs/sysaufs.h
30970 new file mode 100644
30971 index 00000000000000..3f7efd4510be61
30973 +++ b/fs/aufs/sysaufs.h
30975 +/* SPDX-License-Identifier: GPL-2.0 */
30977 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30979 + * This program is free software; you can redistribute it and/or modify
30980 + * it under the terms of the GNU General Public License as published by
30981 + * the Free Software Foundation; either version 2 of the License, or
30982 + * (at your option) any later version.
30984 + * This program is distributed in the hope that it will be useful,
30985 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30986 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30987 + * GNU General Public License for more details.
30989 + * You should have received a copy of the GNU General Public License
30990 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
30994 + * sysfs interface and mount lifetime management
30997 +#ifndef __SYSAUFS_H__
30998 +#define __SYSAUFS_H__
31002 +#include <linux/sysfs.h>
31003 +#include "module.h"
31005 +struct super_block;
31008 +struct sysaufs_si_attr {
31009 + struct attribute attr;
31010 + int (*show)(struct seq_file *seq, struct super_block *sb);
31013 +/* ---------------------------------------------------------------------- */
31016 +extern unsigned long sysaufs_si_mask;
31017 +extern struct kset *sysaufs_kset;
31018 +extern struct attribute *sysaufs_si_attrs[];
31019 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31020 +int __init sysaufs_init(void);
31021 +void sysaufs_fin(void);
31023 +/* ---------------------------------------------------------------------- */
31025 +/* some people doesn't like to show a pointer in kernel */
31026 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31028 + return sysaufs_si_mask ^ (unsigned long)sbinfo;
31031 +#define SysaufsSiNamePrefix "si_"
31032 +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31033 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31035 + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31036 + sysaufs_si_id(sbinfo));
31040 +#ifdef CONFIG_SYSFS
31042 +extern struct attribute_group *sysaufs_attr_group;
31044 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31045 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31047 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31048 +#ifdef CONFIG_COMPAT
31049 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31052 +void sysaufs_br_init(struct au_branch *br);
31053 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31054 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31056 +#define sysaufs_brs_init() do {} while (0)
31059 +#define sysaufs_attr_group NULL
31061 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31062 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31063 + struct attribute *attr, char *buf)
31064 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31065 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31066 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31068 +static inline void sysaufs_brs_init(void)
31073 +#endif /* CONFIG_SYSFS */
31075 +#endif /* __KERNEL__ */
31076 +#endif /* __SYSAUFS_H__ */
31077 diff --git a/fs/aufs/sysfs.c b/fs/aufs/sysfs.c
31078 new file mode 100644
31079 index 00000000000000..4e8a4cfa9faa22
31081 +++ b/fs/aufs/sysfs.c
31083 +// SPDX-License-Identifier: GPL-2.0
31085 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31087 + * This program is free software; you can redistribute it and/or modify
31088 + * it under the terms of the GNU General Public License as published by
31089 + * the Free Software Foundation; either version 2 of the License, or
31090 + * (at your option) any later version.
31092 + * This program is distributed in the hope that it will be useful,
31093 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31094 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31095 + * GNU General Public License for more details.
31097 + * You should have received a copy of the GNU General Public License
31098 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
31102 + * sysfs interface
31105 +#include <linux/compat.h>
31106 +#include <linux/seq_file.h>
31109 +#ifdef CONFIG_AUFS_FS_MODULE
31110 +/* this entry violates the "one line per file" policy of sysfs */
31111 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31115 + static char *conf =
31116 +/* this file is generated at compiling */
31117 +#include "conf.str"
31120 + err = snprintf(buf, PAGE_SIZE, conf);
31121 + if (unlikely(err >= PAGE_SIZE))
31126 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31129 +static struct attribute *au_attr[] = {
31130 +#ifdef CONFIG_AUFS_FS_MODULE
31131 + &au_config_attr.attr,
31133 + NULL, /* need to NULL terminate the list of attributes */
31136 +static struct attribute_group sysaufs_attr_group_body = {
31140 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31142 +/* ---------------------------------------------------------------------- */
31144 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31148 + SiMustAnyLock(sb);
31151 + if (au_opt_test(au_mntflags(sb), XINO)) {
31152 + err = au_xino_path(seq, au_sbi(sb)->si_xib);
31153 + seq_putc(seq, '\n');
31159 + * the lifetime of branch is independent from the entry under sysfs.
31160 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31163 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31164 + aufs_bindex_t bindex, int idx)
31167 + struct path path;
31168 + struct dentry *root;
31169 + struct au_branch *br;
31170 + au_br_perm_str_t perm;
31172 + AuDbg("b%d\n", bindex);
31175 + root = sb->s_root;
31176 + di_read_lock_parent(root, !AuLock_IR);
31177 + br = au_sbr(sb, bindex);
31180 + case AuBrSysfs_BR:
31181 + path.mnt = au_br_mnt(br);
31182 + path.dentry = au_h_dptr(root, bindex);
31183 + err = au_seq_path(seq, &path);
31185 + au_optstr_br_perm(&perm, br->br_perm);
31186 + seq_printf(seq, "=%s\n", perm.a);
31189 + case AuBrSysfs_BRID:
31190 + seq_printf(seq, "%d\n", br->br_id);
31193 + di_read_unlock(root, !AuLock_IR);
31194 + if (unlikely(err || seq_has_overflowed(seq)))
31200 +/* ---------------------------------------------------------------------- */
31202 +static struct seq_file *au_seq(char *p, ssize_t len)
31204 + struct seq_file *seq;
31206 + seq = kzalloc(sizeof(*seq), GFP_NOFS);
31208 + /* mutex_init(&seq.lock); */
31211 + return seq; /* success */
31214 + seq = ERR_PTR(-ENOMEM);
31218 +#define SysaufsBr_PREFIX "br"
31219 +#define SysaufsBrid_PREFIX "brid"
31221 +/* todo: file size may exceed PAGE_SIZE */
31222 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31228 + aufs_bindex_t bbot;
31229 + struct au_sbinfo *sbinfo;
31230 + struct super_block *sb;
31231 + struct seq_file *seq;
31233 + struct attribute **cattr;
31235 + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31236 + sb = sbinfo->si_sb;
31239 + * prevent a race condition between sysfs and aufs.
31240 + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31241 + * prohibits maintaining the sysfs entries.
31242 + * hew we acquire read lock after sysfs_get_active_two().
31243 + * on the other hand, the remount process may maintain the sysfs/aufs
31244 + * entries after acquiring write lock.
31245 + * it can cause a deadlock.
31246 + * simply we gave up processing read here.
31249 + if (unlikely(!si_noflush_read_trylock(sb)))
31252 + seq = au_seq(buf, PAGE_SIZE);
31253 + err = PTR_ERR(seq);
31257 + name = (void *)attr->name;
31258 + cattr = sysaufs_si_attrs;
31260 + if (!strcmp(name, (*cattr)->name)) {
31261 + err = container_of(*cattr, struct sysaufs_si_attr, attr)
31268 + if (!strncmp(name, SysaufsBrid_PREFIX,
31269 + sizeof(SysaufsBrid_PREFIX) - 1)) {
31270 + idx = AuBrSysfs_BRID;
31271 + name += sizeof(SysaufsBrid_PREFIX) - 1;
31272 + } else if (!strncmp(name, SysaufsBr_PREFIX,
31273 + sizeof(SysaufsBr_PREFIX) - 1)) {
31274 + idx = AuBrSysfs_BR;
31275 + name += sizeof(SysaufsBr_PREFIX) - 1;
31279 + err = kstrtol(name, 10, &l);
31281 + bbot = au_sbbot(sb);
31283 + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31290 + err = seq->count;
31291 + /* sysfs limit */
31292 + if (unlikely(err == PAGE_SIZE))
31295 + au_kfree_rcu(seq);
31297 + si_read_unlock(sb);
31302 +/* ---------------------------------------------------------------------- */
31304 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31308 + aufs_bindex_t bindex, bbot;
31311 + struct seq_file *seq;
31312 + struct au_branch *br;
31314 + si_read_lock(sb, AuLock_FLUSH);
31315 + bbot = au_sbbot(sb);
31321 + buf = (void *)__get_free_page(GFP_NOFS);
31322 + if (unlikely(!buf))
31325 + seq = au_seq(buf, PAGE_SIZE);
31326 + err = PTR_ERR(seq);
31330 + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31331 + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31332 + /* VERIFY_WRITE */
31333 + err = !access_ok(arg, sizeof(*arg));
31334 + if (unlikely(err))
31337 + br = au_sbr(sb, bindex);
31338 + brid = br->br_id;
31339 + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31340 + err = __put_user(brid, &arg->id);
31341 + if (unlikely(err))
31344 + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31345 + err = __put_user(br->br_perm, &arg->perm);
31346 + if (unlikely(err))
31349 + err = au_seq_path(seq, &br->br_path);
31350 + if (unlikely(err))
31352 + seq_putc(seq, '\0');
31353 + if (!seq_has_overflowed(seq)) {
31354 + err = copy_to_user(arg->path, seq->buf, seq->count);
31356 + if (unlikely(err))
31363 + if (unlikely(err))
31367 + au_kfree_rcu(seq);
31369 + free_page((unsigned long)buf);
31371 + si_read_unlock(sb);
31375 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31377 + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31380 +#ifdef CONFIG_COMPAT
31381 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31383 + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31387 +/* ---------------------------------------------------------------------- */
31389 +void sysaufs_br_init(struct au_branch *br)
31392 + struct au_brsysfs *br_sysfs;
31393 + struct attribute *attr;
31395 + br_sysfs = br->br_sysfs;
31396 + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31397 + attr = &br_sysfs->attr;
31398 + sysfs_attr_init(attr);
31399 + attr->name = br_sysfs->name;
31400 + attr->mode = 0444;
31405 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31407 + struct au_branch *br;
31408 + struct kobject *kobj;
31409 + struct au_brsysfs *br_sysfs;
31411 + aufs_bindex_t bbot;
31413 + if (!sysaufs_brs)
31416 + kobj = &au_sbi(sb)->si_kobj;
31417 + bbot = au_sbbot(sb);
31418 + for (; bindex <= bbot; bindex++) {
31419 + br = au_sbr(sb, bindex);
31420 + br_sysfs = br->br_sysfs;
31421 + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31422 + sysfs_remove_file(kobj, &br_sysfs->attr);
31428 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31431 + aufs_bindex_t bbot;
31432 + struct kobject *kobj;
31433 + struct au_branch *br;
31434 + struct au_brsysfs *br_sysfs;
31436 + if (!sysaufs_brs)
31439 + kobj = &au_sbi(sb)->si_kobj;
31440 + bbot = au_sbbot(sb);
31441 + for (; bindex <= bbot; bindex++) {
31442 + br = au_sbr(sb, bindex);
31443 + br_sysfs = br->br_sysfs;
31444 + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31445 + SysaufsBr_PREFIX "%d", bindex);
31446 + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31447 + SysaufsBrid_PREFIX "%d", bindex);
31448 + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31449 + err = sysfs_create_file(kobj, &br_sysfs->attr);
31450 + if (unlikely(err))
31451 + pr_warn("failed %s under sysfs(%d)\n",
31452 + br_sysfs->name, err);
31457 diff --git a/fs/aufs/sysrq.c b/fs/aufs/sysrq.c
31458 new file mode 100644
31459 index 00000000000000..c03d87e414c50f
31461 +++ b/fs/aufs/sysrq.c
31463 +// SPDX-License-Identifier: GPL-2.0
31465 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31467 + * This program is free software; you can redistribute it and/or modify
31468 + * it under the terms of the GNU General Public License as published by
31469 + * the Free Software Foundation; either version 2 of the License, or
31470 + * (at your option) any later version.
31472 + * This program is distributed in the hope that it will be useful,
31473 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31474 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31475 + * GNU General Public License for more details.
31477 + * You should have received a copy of the GNU General Public License
31478 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
31482 + * magic sysrq handler
31485 +/* #include <linux/sysrq.h> */
31486 +#include <linux/writeback.h>
31489 +/* ---------------------------------------------------------------------- */
31491 +static void sysrq_sb(struct super_block *sb)
31494 + struct au_sbinfo *sbinfo;
31495 + struct file *file;
31496 + struct hlist_bl_head *files;
31497 + struct hlist_bl_node *pos;
31498 + struct au_finfo *finfo;
31501 + plevel = au_plevel;
31502 + au_plevel = KERN_WARNING;
31504 + /* since we define pr_fmt, call printk directly */
31505 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31507 + sbinfo = au_sbi(sb);
31508 + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31509 + pr("superblock\n");
31512 +#if 0 /* reserved */
31514 + int err, i, j, ndentry;
31515 + struct au_dcsub_pages dpages;
31516 + struct au_dpage *dpage;
31518 + err = au_dpages_init(&dpages, GFP_ATOMIC);
31519 + if (unlikely(err))
31521 + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31523 + for (i = 0; i < dpages.ndpage; i++) {
31524 + dpage = dpages.dpages + i;
31525 + ndentry = dpage->ndentry;
31526 + for (j = 0; j < ndentry; j++)
31527 + au_dpri_dentry(dpage->dentries[j]);
31529 + au_dpages_free(&dpages);
31533 + pr("isolated inode\n");
31534 + spin_lock(&sb->s_inode_list_lock);
31535 + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31536 + spin_lock(&i->i_lock);
31537 + if (hlist_empty(&i->i_dentry))
31538 + au_dpri_inode(i);
31539 + spin_unlock(&i->i_lock);
31541 + spin_unlock(&sb->s_inode_list_lock);
31544 + files = &au_sbi(sb)->si_files;
31545 + hlist_bl_lock(files);
31546 + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31549 + file = finfo->fi_file;
31550 + mode = file_inode(file)->i_mode;
31551 + if (!special_file(mode))
31552 + au_dpri_file(file);
31554 + hlist_bl_unlock(files);
31558 + au_plevel = plevel;
31561 +/* ---------------------------------------------------------------------- */
31563 +/* module parameter */
31564 +static char *aufs_sysrq_key = "a";
31565 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31566 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31568 +static void au_sysrq(u8 key __maybe_unused)
31570 + struct au_sbinfo *sbinfo;
31571 + struct hlist_bl_node *pos;
31574 + au_sbilist_lock();
31575 + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31576 + sysrq_sb(sbinfo->si_sb);
31577 + au_sbilist_unlock();
31581 +static struct sysrq_key_op au_sysrq_op = {
31582 + .handler = au_sysrq,
31583 + .help_msg = "Aufs",
31584 + .action_msg = "Aufs",
31585 + .enable_mask = SYSRQ_ENABLE_DUMP
31588 +/* ---------------------------------------------------------------------- */
31590 +int __init au_sysrq_init(void)
31596 + key = *aufs_sysrq_key;
31597 + if ('a' <= key && key <= 'z')
31598 + err = register_sysrq_key(key, &au_sysrq_op);
31599 + if (unlikely(err))
31600 + pr_err("err %d, sysrq=%c\n", err, key);
31604 +void au_sysrq_fin(void)
31608 + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31609 + if (unlikely(err))
31610 + pr_err("err %d (ignored)\n", err);
31612 diff --git a/fs/aufs/vdir.c b/fs/aufs/vdir.c
31613 new file mode 100644
31614 index 00000000000000..fdf7644049c5d2
31616 +++ b/fs/aufs/vdir.c
31618 +// SPDX-License-Identifier: GPL-2.0
31620 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31622 + * This program is free software; you can redistribute it and/or modify
31623 + * it under the terms of the GNU General Public License as published by
31624 + * the Free Software Foundation; either version 2 of the License, or
31625 + * (at your option) any later version.
31627 + * This program is distributed in the hope that it will be useful,
31628 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31629 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31630 + * GNU General Public License for more details.
31632 + * You should have received a copy of the GNU General Public License
31633 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
31637 + * virtual or vertical directory
31640 +#include <linux/iversion.h>
31643 +static unsigned int calc_size(int nlen)
31645 + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31648 +static int set_deblk_end(union au_vdir_deblk_p *p,
31649 + union au_vdir_deblk_p *deblk_end)
31651 + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31652 + p->de->de_str.len = 0;
31656 + return -1; /* error */
31659 +/* returns true or false */
31660 +static int is_deblk_end(union au_vdir_deblk_p *p,
31661 + union au_vdir_deblk_p *deblk_end)
31663 + if (calc_size(0) <= deblk_end->deblk - p->deblk)
31664 + return !p->de->de_str.len;
31668 +static unsigned char *last_deblk(struct au_vdir *vdir)
31670 + return vdir->vd_deblk[vdir->vd_nblk - 1];
31673 +/* ---------------------------------------------------------------------- */
31675 +/* estimate the appropriate size for name hash table */
31676 +unsigned int au_rdhash_est(loff_t sz)
31684 + if (sz < AUFS_RDHASH_DEF)
31685 + n = AUFS_RDHASH_DEF;
31686 + /* pr_info("n %u\n", n); */
31691 + * the allocated memory has to be freed by
31692 + * au_nhash_wh_free() or au_nhash_de_free().
31694 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31696 + struct hlist_head *head;
31700 + sz = sizeof(*nhash->nh_head) * num_hash;
31701 + head = kmalloc(sz, gfp);
31703 + nhash->nh_num = num_hash;
31704 + nhash->nh_head = head;
31705 + for (u = 0; u < num_hash; u++)
31706 + INIT_HLIST_HEAD(head++);
31707 + return 0; /* success */
31713 +static void nhash_count(struct hlist_head *head)
31715 +#if 0 /* debugging */
31717 + struct hlist_node *pos;
31720 + hlist_for_each(pos, head)
31722 + pr_info("%lu\n", n);
31726 +static void au_nhash_wh_do_free(struct hlist_head *head)
31728 + struct au_vdir_wh *pos;
31729 + struct hlist_node *node;
31731 + hlist_for_each_entry_safe(pos, node, head, wh_hash)
31732 + au_kfree_rcu(pos);
31735 +static void au_nhash_de_do_free(struct hlist_head *head)
31737 + struct au_vdir_dehstr *pos;
31738 + struct hlist_node *node;
31740 + hlist_for_each_entry_safe(pos, node, head, hash)
31741 + au_cache_free_vdir_dehstr(pos);
31744 +static void au_nhash_do_free(struct au_nhash *nhash,
31745 + void (*free)(struct hlist_head *head))
31748 + struct hlist_head *head;
31750 + n = nhash->nh_num;
31754 + head = nhash->nh_head;
31755 + while (n-- > 0) {
31756 + nhash_count(head);
31759 + au_kfree_try_rcu(nhash->nh_head);
31762 +void au_nhash_wh_free(struct au_nhash *whlist)
31764 + au_nhash_do_free(whlist, au_nhash_wh_do_free);
31767 +static void au_nhash_de_free(struct au_nhash *delist)
31769 + au_nhash_do_free(delist, au_nhash_de_do_free);
31772 +/* ---------------------------------------------------------------------- */
31774 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31778 + unsigned int u, n;
31779 + struct hlist_head *head;
31780 + struct au_vdir_wh *pos;
31783 + n = whlist->nh_num;
31784 + head = whlist->nh_head;
31785 + for (u = 0; u < n; u++, head++)
31786 + hlist_for_each_entry(pos, head, wh_hash)
31787 + if (pos->wh_bindex == btgt && ++num > limit)
31792 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31793 + unsigned char *name,
31794 + unsigned int len)
31797 + /* const unsigned int magic_bit = 12; */
31799 + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31806 + /* v = hash_long(v, magic_bit); */
31807 + v %= nhash->nh_num;
31808 + return nhash->nh_head + v;
31811 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31814 + return str->len == nlen && !memcmp(str->name, name, nlen);
31817 +/* returns found or not */
31818 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31820 + struct hlist_head *head;
31821 + struct au_vdir_wh *pos;
31822 + struct au_vdir_destr *str;
31824 + head = au_name_hash(whlist, name, nlen);
31825 + hlist_for_each_entry(pos, head, wh_hash) {
31826 + str = &pos->wh_str;
31827 + AuDbg("%.*s\n", str->len, str->name);
31828 + if (au_nhash_test_name(str, name, nlen))
31834 +/* returns found(true) or not */
31835 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31837 + struct hlist_head *head;
31838 + struct au_vdir_dehstr *pos;
31839 + struct au_vdir_destr *str;
31841 + head = au_name_hash(delist, name, nlen);
31842 + hlist_for_each_entry(pos, head, hash) {
31844 + AuDbg("%.*s\n", str->len, str->name);
31845 + if (au_nhash_test_name(str, name, nlen))
31851 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31852 + unsigned char d_type)
31854 +#ifdef CONFIG_AUFS_SHWH
31855 + wh->wh_ino = ino;
31856 + wh->wh_type = d_type;
31860 +/* ---------------------------------------------------------------------- */
31862 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31863 + unsigned int d_type, aufs_bindex_t bindex,
31864 + unsigned char shwh)
31867 + struct au_vdir_destr *str;
31868 + struct au_vdir_wh *wh;
31870 + AuDbg("%.*s\n", nlen, name);
31871 + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31874 + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31875 + if (unlikely(!wh))
31879 + wh->wh_bindex = bindex;
31881 + au_shwh_init_wh(wh, ino, d_type);
31882 + str = &wh->wh_str;
31884 + memcpy(str->name, name, nlen);
31885 + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31892 +static int append_deblk(struct au_vdir *vdir)
31895 + unsigned long ul;
31896 + const unsigned int deblk_sz = vdir->vd_deblk_sz;
31897 + union au_vdir_deblk_p p, deblk_end;
31898 + unsigned char **o;
31901 + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31902 + GFP_NOFS, /*may_shrink*/0);
31903 + if (unlikely(!o))
31906 + vdir->vd_deblk = o;
31907 + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31909 + ul = vdir->vd_nblk++;
31910 + vdir->vd_deblk[ul] = p.deblk;
31911 + vdir->vd_last.ul = ul;
31912 + vdir->vd_last.p.deblk = p.deblk;
31913 + deblk_end.deblk = p.deblk + deblk_sz;
31914 + err = set_deblk_end(&p, &deblk_end);
31921 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31922 + unsigned int d_type, struct au_nhash *delist)
31926 + const unsigned int deblk_sz = vdir->vd_deblk_sz;
31927 + union au_vdir_deblk_p p, *room, deblk_end;
31928 + struct au_vdir_dehstr *dehstr;
31930 + p.deblk = last_deblk(vdir);
31931 + deblk_end.deblk = p.deblk + deblk_sz;
31932 + room = &vdir->vd_last.p;
31933 + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31934 + || !is_deblk_end(room, &deblk_end));
31936 + sz = calc_size(nlen);
31937 + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31938 + err = append_deblk(vdir);
31939 + if (unlikely(err))
31942 + p.deblk = last_deblk(vdir);
31943 + deblk_end.deblk = p.deblk + deblk_sz;
31945 + AuDebugOn(room->deblk != p.deblk);
31949 + dehstr = au_cache_alloc_vdir_dehstr();
31950 + if (unlikely(!dehstr))
31953 + dehstr->str = &room->de->de_str;
31954 + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31955 + room->de->de_ino = ino;
31956 + room->de->de_type = d_type;
31957 + room->de->de_str.len = nlen;
31958 + memcpy(room->de->de_str.name, name, nlen);
31961 + room->deblk += sz;
31962 + if (unlikely(set_deblk_end(room, &deblk_end)))
31963 + err = append_deblk(vdir);
31970 +/* ---------------------------------------------------------------------- */
31972 +void au_vdir_free(struct au_vdir *vdir)
31974 + unsigned char **deblk;
31976 + deblk = vdir->vd_deblk;
31977 + while (vdir->vd_nblk--)
31978 + au_kfree_try_rcu(*deblk++);
31979 + au_kfree_try_rcu(vdir->vd_deblk);
31980 + au_cache_free_vdir(vdir);
31983 +static struct au_vdir *alloc_vdir(struct file *file)
31985 + struct au_vdir *vdir;
31986 + struct super_block *sb;
31989 + sb = file->f_path.dentry->d_sb;
31990 + SiMustAnyLock(sb);
31993 + vdir = au_cache_alloc_vdir();
31994 + if (unlikely(!vdir))
31997 + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31998 + if (unlikely(!vdir->vd_deblk))
32001 + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32002 + if (!vdir->vd_deblk_sz) {
32003 + /* estimate the appropriate size for deblk */
32004 + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32005 + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32007 + vdir->vd_nblk = 0;
32008 + vdir->vd_version = 0;
32009 + vdir->vd_jiffy = 0;
32010 + err = append_deblk(vdir);
32012 + return vdir; /* success */
32014 + au_kfree_try_rcu(vdir->vd_deblk);
32017 + au_cache_free_vdir(vdir);
32019 + vdir = ERR_PTR(err);
32023 +static int reinit_vdir(struct au_vdir *vdir)
32026 + union au_vdir_deblk_p p, deblk_end;
32028 + while (vdir->vd_nblk > 1) {
32029 + au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32030 + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32033 + p.deblk = vdir->vd_deblk[0];
32034 + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32035 + err = set_deblk_end(&p, &deblk_end);
32036 + /* keep vd_dblk_sz */
32037 + vdir->vd_last.ul = 0;
32038 + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32039 + vdir->vd_version = 0;
32040 + vdir->vd_jiffy = 0;
32045 +/* ---------------------------------------------------------------------- */
32047 +#define AuFillVdir_CALLED 1
32048 +#define AuFillVdir_WHABLE (1 << 1)
32049 +#define AuFillVdir_SHWH (1 << 2)
32050 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32051 +#define au_fset_fillvdir(flags, name) \
32052 + do { (flags) |= AuFillVdir_##name; } while (0)
32053 +#define au_fclr_fillvdir(flags, name) \
32054 + do { (flags) &= ~AuFillVdir_##name; } while (0)
32056 +#ifndef CONFIG_AUFS_SHWH
32057 +#undef AuFillVdir_SHWH
32058 +#define AuFillVdir_SHWH 0
32061 +struct fillvdir_arg {
32062 + struct dir_context ctx;
32063 + struct file *file;
32064 + struct au_vdir *vdir;
32065 + struct au_nhash delist;
32066 + struct au_nhash whlist;
32067 + aufs_bindex_t bindex;
32068 + unsigned int flags;
32072 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32073 + loff_t offset __maybe_unused, u64 h_ino,
32074 + unsigned int d_type)
32076 + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32077 + char *name = (void *)__name;
32078 + struct super_block *sb;
32080 + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32083 + sb = arg->file->f_path.dentry->d_sb;
32084 + au_fset_fillvdir(arg->flags, CALLED);
32086 + if (nlen <= AUFS_WH_PFX_LEN
32087 + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32088 + if (test_known(&arg->delist, name, nlen)
32089 + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32090 + goto out; /* already exists or whiteouted */
32092 + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32094 + if (unlikely(nlen > AUFS_MAX_NAMELEN))
32095 + d_type = DT_UNKNOWN;
32096 + arg->err = append_de(arg->vdir, name, nlen, ino,
32097 + d_type, &arg->delist);
32099 + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32100 + name += AUFS_WH_PFX_LEN;
32101 + nlen -= AUFS_WH_PFX_LEN;
32102 + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32103 + goto out; /* already whiteouted */
32105 + ino = 0; /* just to suppress a warning */
32107 + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32110 + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32111 + d_type = DT_UNKNOWN;
32112 + arg->err = au_nhash_append_wh
32113 + (&arg->whlist, name, nlen, ino, d_type,
32114 + arg->bindex, shwh);
32120 + arg->vdir->vd_jiffy = jiffies;
32122 + AuTraceErr(arg->err);
32123 + return !arg->err;
32126 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32127 + struct au_nhash *whlist, struct au_nhash *delist)
32129 +#ifdef CONFIG_AUFS_SHWH
32131 + unsigned int nh, u;
32132 + struct hlist_head *head;
32133 + struct au_vdir_wh *pos;
32134 + struct hlist_node *n;
32136 + struct au_vdir_destr *destr;
32138 + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32141 + o = p = (void *)__get_free_page(GFP_NOFS);
32142 + if (unlikely(!p))
32146 + nh = whlist->nh_num;
32147 + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32148 + p += AUFS_WH_PFX_LEN;
32149 + for (u = 0; u < nh; u++) {
32150 + head = whlist->nh_head + u;
32151 + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32152 + destr = &pos->wh_str;
32153 + memcpy(p, destr->name, destr->len);
32154 + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32155 + pos->wh_ino, pos->wh_type, delist);
32156 + if (unlikely(err))
32161 + free_page((unsigned long)o);
32171 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32174 + unsigned int rdhash;
32176 + aufs_bindex_t bbot, bindex, btop;
32177 + unsigned char shwh;
32178 + struct file *hf, *file;
32179 + struct super_block *sb;
32181 + file = arg->file;
32182 + sb = file->f_path.dentry->d_sb;
32183 + SiMustAnyLock(sb);
32185 + rdhash = au_sbi(sb)->si_rdhash;
32187 + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32188 + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32189 + if (unlikely(err))
32191 + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32192 + if (unlikely(err))
32198 + if (au_opt_test(au_mntflags(sb), SHWH)) {
32200 + au_fset_fillvdir(arg->flags, SHWH);
32202 + btop = au_fbtop(file);
32203 + bbot = au_fbbot_dir(file);
32204 + for (bindex = btop; !err && bindex <= bbot; bindex++) {
32205 + hf = au_hf_dir(file, bindex);
32209 + offset = vfsub_llseek(hf, 0, SEEK_SET);
32211 + if (unlikely(offset))
32214 + arg->bindex = bindex;
32215 + au_fclr_fillvdir(arg->flags, WHABLE);
32217 + || (bindex != bbot
32218 + && au_br_whable(au_sbr_perm(sb, bindex))))
32219 + au_fset_fillvdir(arg->flags, WHABLE);
32222 + au_fclr_fillvdir(arg->flags, CALLED);
32224 + err = vfsub_iterate_dir(hf, &arg->ctx);
32227 + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32230 + * dir_relax() may be good for concurrency, but aufs should not
32231 + * use it since it will cause a lockdep problem.
32235 + if (!err && shwh)
32236 + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32238 + au_nhash_wh_free(&arg->whlist);
32241 + au_nhash_de_free(&arg->delist);
32246 +static int read_vdir(struct file *file, int may_read)
32249 + unsigned long expire;
32250 + unsigned char do_read;
32251 + struct fillvdir_arg arg = {
32253 + .actor = fillvdir
32256 + struct inode *inode;
32257 + struct au_vdir *vdir, *allocated;
32260 + inode = file_inode(file);
32261 + IMustLock(inode);
32262 + IiMustWriteLock(inode);
32263 + SiMustAnyLock(inode->i_sb);
32265 + allocated = NULL;
32267 + expire = au_sbi(inode->i_sb)->si_rdcache;
32268 + vdir = au_ivdir(inode);
32271 + vdir = alloc_vdir(file);
32272 + err = PTR_ERR(vdir);
32273 + if (IS_ERR(vdir))
32276 + allocated = vdir;
32277 + } else if (may_read
32278 + && (!inode_eq_iversion(inode, vdir->vd_version)
32279 + || time_after(jiffies, vdir->vd_jiffy + expire))) {
32281 + err = reinit_vdir(vdir);
32282 + if (unlikely(err))
32287 + return 0; /* success */
32291 + err = au_do_read_vdir(&arg);
32293 + /* file->f_pos = 0; */ /* todo: ctx->pos? */
32294 + vdir->vd_version = inode_query_iversion(inode);
32295 + vdir->vd_last.ul = 0;
32296 + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32298 + au_set_ivdir(inode, allocated);
32299 + } else if (allocated)
32300 + au_vdir_free(allocated);
32306 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32309 + unsigned long ul, n;
32310 + const unsigned int deblk_sz = src->vd_deblk_sz;
32312 + AuDebugOn(tgt->vd_nblk != 1);
32315 + if (tgt->vd_nblk < src->vd_nblk) {
32316 + unsigned char **p;
32318 + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32319 + GFP_NOFS, /*may_shrink*/0);
32320 + if (unlikely(!p))
32322 + tgt->vd_deblk = p;
32325 + if (tgt->vd_deblk_sz != deblk_sz) {
32326 + unsigned char *p;
32328 + tgt->vd_deblk_sz = deblk_sz;
32329 + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32330 + /*may_shrink*/1);
32331 + if (unlikely(!p))
32333 + tgt->vd_deblk[0] = p;
32335 + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32336 + tgt->vd_version = src->vd_version;
32337 + tgt->vd_jiffy = src->vd_jiffy;
32339 + n = src->vd_nblk;
32340 + for (ul = 1; ul < n; ul++) {
32341 + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32343 + if (unlikely(!tgt->vd_deblk[ul]))
32347 + tgt->vd_nblk = n;
32348 + tgt->vd_last.ul = tgt->vd_last.ul;
32349 + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32350 + tgt->vd_last.p.deblk += src->vd_last.p.deblk
32351 + - src->vd_deblk[src->vd_last.ul];
32353 + return 0; /* success */
32356 + rerr = reinit_vdir(tgt);
32361 +int au_vdir_init(struct file *file)
32364 + struct inode *inode;
32365 + struct au_vdir *vdir_cache, *allocated;
32367 + /* test file->f_pos here instead of ctx->pos */
32368 + err = read_vdir(file, !file->f_pos);
32369 + if (unlikely(err))
32372 + allocated = NULL;
32373 + vdir_cache = au_fvdir_cache(file);
32374 + if (!vdir_cache) {
32375 + vdir_cache = alloc_vdir(file);
32376 + err = PTR_ERR(vdir_cache);
32377 + if (IS_ERR(vdir_cache))
32379 + allocated = vdir_cache;
32380 + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32381 + /* test file->f_pos here instead of ctx->pos */
32382 + err = reinit_vdir(vdir_cache);
32383 + if (unlikely(err))
32386 + return 0; /* success */
32388 + inode = file_inode(file);
32389 + err = copy_vdir(vdir_cache, au_ivdir(inode));
32391 + file->f_version = inode_query_iversion(inode);
32393 + au_set_fvdir_cache(file, allocated);
32394 + } else if (allocated)
32395 + au_vdir_free(allocated);
32401 +static loff_t calc_offset(struct au_vdir *vdir)
32404 + union au_vdir_deblk_p p;
32406 + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32407 + offset = vdir->vd_last.p.deblk - p.deblk;
32408 + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32412 +/* returns true or false */
32413 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32416 + unsigned int deblk_sz;
32417 + unsigned long ul, n;
32419 + union au_vdir_deblk_p p, deblk_end;
32420 + struct au_vdir *vdir_cache;
32423 + vdir_cache = au_fvdir_cache(file);
32424 + offset = calc_offset(vdir_cache);
32425 + AuDbg("offset %lld\n", offset);
32426 + if (ctx->pos == offset)
32429 + vdir_cache->vd_last.ul = 0;
32430 + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32435 + deblk_sz = vdir_cache->vd_deblk_sz;
32436 + ul = div64_u64(ctx->pos, deblk_sz);
32437 + AuDbg("ul %lu\n", ul);
32438 + if (ul >= vdir_cache->vd_nblk)
32441 + n = vdir_cache->vd_nblk;
32442 + for (; ul < n; ul++) {
32443 + p.deblk = vdir_cache->vd_deblk[ul];
32444 + deblk_end.deblk = p.deblk + deblk_sz;
32446 + offset *= deblk_sz;
32447 + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32450 + l = calc_size(p.de->de_str.len);
32454 + if (!is_deblk_end(&p, &deblk_end)) {
32456 + vdir_cache->vd_last.ul = ul;
32457 + vdir_cache->vd_last.p = p;
32465 + AuDbg("valid %d\n", !valid);
32469 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32471 + unsigned int l, deblk_sz;
32472 + union au_vdir_deblk_p deblk_end;
32473 + struct au_vdir *vdir_cache;
32474 + struct au_vdir_de *de;
32476 + if (!seek_vdir(file, ctx))
32479 + vdir_cache = au_fvdir_cache(file);
32480 + deblk_sz = vdir_cache->vd_deblk_sz;
32482 + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32483 + deblk_end.deblk += deblk_sz;
32484 + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32485 + de = vdir_cache->vd_last.p.de;
32486 + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32487 + de->de_str.len, de->de_str.name, ctx->pos,
32488 + (unsigned long)de->de_ino, de->de_type);
32489 + if (unlikely(!dir_emit(ctx, de->de_str.name,
32490 + de->de_str.len, de->de_ino,
32492 + /* todo: ignore the error caused by udba? */
32493 + /* return err; */
32497 + l = calc_size(de->de_str.len);
32498 + vdir_cache->vd_last.p.deblk += l;
32501 + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32502 + vdir_cache->vd_last.ul++;
32503 + vdir_cache->vd_last.p.deblk
32504 + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32505 + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32514 diff --git a/fs/aufs/vfsub.c b/fs/aufs/vfsub.c
32515 new file mode 100644
32516 index 00000000000000..2fb5f1ac0b31c6
32518 +++ b/fs/aufs/vfsub.c
32520 +// SPDX-License-Identifier: GPL-2.0
32522 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32524 + * This program is free software; you can redistribute it and/or modify
32525 + * it under the terms of the GNU General Public License as published by
32526 + * the Free Software Foundation; either version 2 of the License, or
32527 + * (at your option) any later version.
32529 + * This program is distributed in the hope that it will be useful,
32530 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32531 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32532 + * GNU General Public License for more details.
32534 + * You should have received a copy of the GNU General Public License
32535 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
32539 + * sub-routines for VFS
32542 +#include <linux/mnt_namespace.h>
32543 +#include <linux/nsproxy.h>
32544 +#include <linux/security.h>
32545 +#include <linux/splice.h>
32548 +#ifdef CONFIG_AUFS_BR_FUSE
32549 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32551 + if (!au_test_fuse(h_sb) || !au_userns)
32554 + return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32558 +int vfsub_sync_filesystem(struct super_block *h_sb)
32563 + down_read(&h_sb->s_umount);
32564 + err = sync_filesystem(h_sb);
32565 + up_read(&h_sb->s_umount);
32571 +/* ---------------------------------------------------------------------- */
32573 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32577 + struct super_block *h_sb;
32580 + * Always needs h_path->mnt for LSM or FUSE branch.
32582 + AuDebugOn(!h_path->mnt);
32584 + /* for remote fs, leave work for its getattr or d_revalidate */
32585 + /* for bad i_attr fs, handle them in aufs_getattr() */
32586 + /* still some fs may acquire i_mutex. we need to skip them */
32590 + h_sb = h_path->dentry->d_sb;
32591 + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32593 + err = vfsub_getattr(h_path, &st);
32598 +/* ---------------------------------------------------------------------- */
32600 +struct file *vfsub_dentry_open(struct path *path, int flags)
32602 + return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32606 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32608 + struct file *file;
32611 + file = filp_open(path,
32612 + oflags /* | __FMODE_NONOTIFY */,
32615 + if (IS_ERR(file))
32617 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32624 + * Ideally this function should call VFS:do_last() in order to keep all its
32625 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32626 + * structure such as nameidata. This is a second (or third) best approach.
32627 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32629 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32630 + struct vfsub_aopen_args *args)
32633 + struct au_branch *br = args->br;
32634 + struct file *file = args->file;
32635 + /* copied from linux/fs/namei.c:atomic_open() */
32636 + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32639 + AuDebugOn(!dir->i_op->atomic_open);
32641 + err = au_br_test_oflag(args->open_flag, br);
32642 + if (unlikely(err))
32645 + au_lcnt_inc(&br->br_nfiles);
32646 + file->f_path.dentry = DENTRY_NOT_SET;
32647 + file->f_path.mnt = au_br_mnt(br);
32648 + AuDbg("%ps\n", dir->i_op->atomic_open);
32649 + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32650 + args->create_mode);
32651 + if (unlikely(err < 0)) {
32652 + au_lcnt_dec(&br->br_nfiles);
32656 + /* temporary workaround for nfsv4 branch */
32657 + if (au_test_nfs(dir->i_sb))
32658 + nfs_mark_for_revalidate(dir);
32660 + if (file->f_mode & FMODE_CREATED)
32661 + fsnotify_create(dir, dentry);
32662 + if (!(file->f_mode & FMODE_OPENED)) {
32663 + au_lcnt_dec(&br->br_nfiles);
32667 + /* todo: call VFS:may_open() here */
32668 + /* todo: ima_file_check() too? */
32669 + if (!err && (args->open_flag & __FMODE_EXEC))
32670 + err = deny_write_access(file);
32672 + fsnotify_open(file);
32674 + au_lcnt_dec(&br->br_nfiles);
32675 + /* note that the file is created and still opened */
32681 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32685 + err = kern_path(name, flags, path);
32686 + if (!err && d_is_positive(path->dentry))
32687 + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32691 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32692 + struct path *ppath, int len)
32694 + struct path path;
32696 + path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
32697 + if (IS_ERR(path.dentry))
32699 + if (d_is_positive(path.dentry)) {
32700 + path.mnt = ppath->mnt;
32701 + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32705 + AuTraceErrPtr(path.dentry);
32706 + return path.dentry;
32709 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
32712 + struct path path;
32714 + /* VFS checks it too, but by WARN_ON_ONCE() */
32715 + IMustLock(d_inode(ppath->dentry));
32717 + path.dentry = lookup_one_len(name, ppath->dentry, len);
32718 + if (IS_ERR(path.dentry))
32720 + if (d_is_positive(path.dentry)) {
32721 + path.mnt = ppath->mnt;
32722 + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32726 + AuTraceErrPtr(path.dentry);
32727 + return path.dentry;
32730 +void vfsub_call_lkup_one(void *args)
32732 + struct vfsub_lkup_one_args *a = args;
32733 + *a->errp = vfsub_lkup_one(a->name, a->ppath);
32736 +/* ---------------------------------------------------------------------- */
32738 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32739 + struct dentry *d2, struct au_hinode *hdir2)
32741 + struct dentry *d;
32744 + d = lock_rename(d1, d2);
32746 + au_hn_suspend(hdir1);
32747 + if (hdir1 != hdir2)
32748 + au_hn_suspend(hdir2);
32753 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32754 + struct dentry *d2, struct au_hinode *hdir2)
32756 + au_hn_resume(hdir1);
32757 + if (hdir1 != hdir2)
32758 + au_hn_resume(hdir2);
32760 + unlock_rename(d1, d2);
32764 +/* ---------------------------------------------------------------------- */
32766 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32769 + struct dentry *d;
32770 + struct mnt_idmap *idmap;
32774 + d = path->dentry;
32775 + path->dentry = d->d_parent;
32776 + err = security_path_mknod(path, d, mode, 0);
32777 + path->dentry = d;
32778 + if (unlikely(err))
32780 + idmap = mnt_idmap(path->mnt);
32783 + err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
32786 + struct path tmp = *path;
32789 + vfsub_update_h_iattr(&tmp, &did);
32791 + tmp.dentry = path->dentry->d_parent;
32792 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32801 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32804 + struct dentry *d;
32805 + struct mnt_idmap *idmap;
32809 + d = path->dentry;
32810 + path->dentry = d->d_parent;
32811 + err = security_path_symlink(path, d, symname);
32812 + path->dentry = d;
32813 + if (unlikely(err))
32815 + idmap = mnt_idmap(path->mnt);
32818 + err = vfs_symlink(idmap, dir, path->dentry, symname);
32821 + struct path tmp = *path;
32824 + vfsub_update_h_iattr(&tmp, &did);
32826 + tmp.dentry = path->dentry->d_parent;
32827 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32836 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32839 + struct dentry *d;
32840 + struct mnt_idmap *idmap;
32844 + d = path->dentry;
32845 + path->dentry = d->d_parent;
32846 + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32847 + path->dentry = d;
32848 + if (unlikely(err))
32850 + idmap = mnt_idmap(path->mnt);
32853 + err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
32856 + struct path tmp = *path;
32859 + vfsub_update_h_iattr(&tmp, &did);
32861 + tmp.dentry = path->dentry->d_parent;
32862 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32871 +static int au_test_nlink(struct inode *inode)
32873 + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32875 + if (!au_test_fs_no_limit_nlink(inode->i_sb)
32876 + || inode->i_nlink < link_max)
32881 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32882 + struct inode **delegated_inode)
32885 + struct dentry *d;
32886 + struct mnt_idmap *idmap;
32890 + err = au_test_nlink(d_inode(src_dentry));
32891 + if (unlikely(err))
32894 + /* we don't call may_linkat() */
32895 + d = path->dentry;
32896 + path->dentry = d->d_parent;
32897 + err = security_path_link(src_dentry, path, d);
32898 + path->dentry = d;
32899 + if (unlikely(err))
32901 + idmap = mnt_idmap(path->mnt);
32904 + err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
32907 + struct path tmp = *path;
32910 + /* fuse has different memory inode for the same inumber */
32911 + vfsub_update_h_iattr(&tmp, &did);
32913 + tmp.dentry = path->dentry->d_parent;
32914 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32915 + tmp.dentry = src_dentry;
32916 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32925 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32926 + struct inode *dir, struct path *path,
32927 + struct inode **delegated_inode, unsigned int flags)
32930 + struct renamedata rd;
32931 + struct path tmp = {
32934 + struct dentry *d;
32937 + IMustLock(src_dir);
32939 + d = path->dentry;
32940 + path->dentry = d->d_parent;
32941 + tmp.dentry = src_dentry->d_parent;
32942 + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32943 + path->dentry = d;
32944 + if (unlikely(err))
32947 + rd.old_mnt_idmap = mnt_idmap(path->mnt);
32948 + rd.old_dir = src_dir;
32949 + rd.old_dentry = src_dentry;
32950 + rd.new_mnt_idmap = rd.old_mnt_idmap;
32951 + rd.new_dir = dir;
32952 + rd.new_dentry = path->dentry;
32953 + rd.delegated_inode = delegated_inode;
32954 + rd.flags = flags;
32956 + err = vfs_rename(&rd);
32961 + tmp.dentry = d->d_parent;
32962 + vfsub_update_h_iattr(&tmp, &did);
32964 + tmp.dentry = src_dentry;
32965 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32966 + tmp.dentry = src_dentry->d_parent;
32967 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
32976 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32979 + struct dentry *d;
32980 + struct mnt_idmap *idmap;
32984 + d = path->dentry;
32985 + path->dentry = d->d_parent;
32986 + err = security_path_mkdir(path, d, mode);
32987 + path->dentry = d;
32988 + if (unlikely(err))
32990 + idmap = mnt_idmap(path->mnt);
32993 + err = vfs_mkdir(idmap, dir, path->dentry, mode);
32996 + struct path tmp = *path;
32999 + vfsub_update_h_iattr(&tmp, &did);
33001 + tmp.dentry = path->dentry->d_parent;
33002 + vfsub_update_h_iattr(&tmp, /*did*/NULL);
33011 +int vfsub_rmdir(struct inode *dir, struct path *path)
33014 + struct dentry *d;
33015 + struct mnt_idmap *idmap;
33019 + d = path->dentry;
33020 + path->dentry = d->d_parent;
33021 + err = security_path_rmdir(path, d);
33022 + path->dentry = d;
33023 + if (unlikely(err))
33025 + idmap = mnt_idmap(path->mnt);
33028 + err = vfs_rmdir(idmap, dir, path->dentry);
33031 + struct path tmp = {
33032 + .dentry = path->dentry->d_parent,
33036 + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33043 +/* ---------------------------------------------------------------------- */
33045 +/* todo: support mmap_sem? */
33046 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33052 + err = vfs_read(file, ubuf, count, ppos);
33055 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33059 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33065 + err = kernel_read(file, kbuf, count, ppos);
33069 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33073 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33079 + err = vfs_write(file, ubuf, count, ppos);
33082 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33086 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33091 + err = kernel_write(file, kbuf, count, ppos);
33094 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33098 +int vfsub_flush(struct file *file, fl_owner_t id)
33103 + if (file->f_op->flush) {
33104 + if (!au_test_nfs(file->f_path.dentry->d_sb))
33105 + err = file->f_op->flush(file, id);
33108 + err = file->f_op->flush(file, id);
33112 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33118 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33122 + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33125 + err = iterate_dir(file, ctx);
33128 + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33133 +long vfsub_splice_read(struct file *in, loff_t *ppos,
33134 + struct pipe_inode_info *pipe, size_t len,
33135 + unsigned int flags)
33140 + err = vfs_splice_read(in, ppos, pipe, len, flags);
33142 + file_accessed(in);
33144 + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33148 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33149 + loff_t *ppos, size_t len, unsigned int flags)
33154 + err = do_splice_from(pipe, out, ppos, len, flags);
33157 + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33161 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33165 + /* file can be NULL */
33167 + err = vfs_fsync(file, datasync);
33171 + AuDebugOn(!file);
33172 + path = &file->f_path;
33174 + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33179 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33180 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33181 + struct file *h_file)
33184 + struct inode *h_inode;
33185 + struct super_block *h_sb;
33186 + struct mnt_idmap *h_idmap;
33189 + err = vfsub_truncate(h_path, length);
33193 + h_inode = d_inode(h_path->dentry);
33194 + h_sb = h_inode->i_sb;
33196 + sb_start_write(h_sb);
33198 + err = security_file_truncate(h_file);
33200 + h_idmap = mnt_idmap(h_path->mnt);
33202 + err = do_truncate(h_idmap, h_path->dentry, length, attr,
33207 + sb_end_write(h_sb);
33214 +/* ---------------------------------------------------------------------- */
33216 +struct au_vfsub_mkdir_args {
33218 + struct inode *dir;
33219 + struct path *path;
33223 +static void au_call_vfsub_mkdir(void *args)
33225 + struct au_vfsub_mkdir_args *a = args;
33226 + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33229 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33231 + int err, do_sio, wkq_err;
33232 + struct mnt_idmap *idmap;
33234 + idmap = mnt_idmap(path->mnt);
33235 + do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33238 + err = vfsub_mkdir(dir, path, mode);
33241 + struct au_vfsub_mkdir_args args = {
33247 + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33248 + if (unlikely(wkq_err))
33255 +struct au_vfsub_rmdir_args {
33257 + struct inode *dir;
33258 + struct path *path;
33261 +static void au_call_vfsub_rmdir(void *args)
33263 + struct au_vfsub_rmdir_args *a = args;
33264 + *a->errp = vfsub_rmdir(a->dir, a->path);
33267 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33269 + int err, do_sio, wkq_err;
33270 + struct mnt_idmap *idmap;
33272 + idmap = mnt_idmap(path->mnt);
33273 + do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33276 + err = vfsub_rmdir(dir, path);
33279 + struct au_vfsub_rmdir_args args = {
33284 + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33285 + if (unlikely(wkq_err))
33292 +/* ---------------------------------------------------------------------- */
33294 +struct notify_change_args {
33296 + struct path *path;
33297 + struct iattr *ia;
33298 + struct inode **delegated_inode;
33301 +static void call_notify_change(void *args)
33303 + struct notify_change_args *a = args;
33304 + struct inode *h_inode;
33305 + struct mnt_idmap *idmap;
33307 + h_inode = d_inode(a->path->dentry);
33308 + IMustLock(h_inode);
33310 + *a->errp = -EPERM;
33311 + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33312 + idmap = mnt_idmap(a->path->mnt);
33314 + *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33315 + a->delegated_inode);
33318 + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33320 + AuTraceErr(*a->errp);
33323 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33324 + struct inode **delegated_inode)
33327 + struct notify_change_args args = {
33331 + .delegated_inode = delegated_inode
33334 + call_notify_change(&args);
33339 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33340 + struct inode **delegated_inode)
33342 + int err, wkq_err;
33343 + struct notify_change_args args = {
33347 + .delegated_inode = delegated_inode
33350 + wkq_err = au_wkq_wait(call_notify_change, &args);
33351 + if (unlikely(wkq_err))
33357 +/* ---------------------------------------------------------------------- */
33359 +struct unlink_args {
33361 + struct inode *dir;
33362 + struct path *path;
33363 + struct inode **delegated_inode;
33366 +static void call_unlink(void *args)
33368 + struct unlink_args *a = args;
33369 + struct dentry *d = a->path->dentry;
33370 + struct inode *h_inode;
33371 + struct mnt_idmap *idmap;
33372 + const int stop_sillyrename = (au_test_nfs(d->d_sb)
33373 + && au_dcount(d) == 1);
33375 + IMustLock(a->dir);
33377 + a->path->dentry = d->d_parent;
33378 + *a->errp = security_path_unlink(a->path, d);
33379 + a->path->dentry = d;
33380 + if (unlikely(*a->errp))
33383 + if (!stop_sillyrename)
33386 + if (d_is_positive(d)) {
33387 + h_inode = d_inode(d);
33391 + idmap = mnt_idmap(a->path->mnt);
33393 + *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33396 + struct path tmp = {
33397 + .dentry = d->d_parent,
33398 + .mnt = a->path->mnt
33400 + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33403 + if (!stop_sillyrename)
33408 + AuTraceErr(*a->errp);
33412 + * @dir: must be locked.
33413 + * @dentry: target dentry.
33415 +int vfsub_unlink(struct inode *dir, struct path *path,
33416 + struct inode **delegated_inode, int force)
33419 + struct unlink_args args = {
33423 + .delegated_inode = delegated_inode
33427 + call_unlink(&args);
33431 + wkq_err = au_wkq_wait(call_unlink, &args);
33432 + if (unlikely(wkq_err))
33438 diff --git a/fs/aufs/vfsub.h b/fs/aufs/vfsub.h
33439 new file mode 100644
33440 index 00000000000000..ed218d95770d0c
33442 +++ b/fs/aufs/vfsub.h
33444 +/* SPDX-License-Identifier: GPL-2.0 */
33446 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33448 + * This program is free software; you can redistribute it and/or modify
33449 + * it under the terms of the GNU General Public License as published by
33450 + * the Free Software Foundation; either version 2 of the License, or
33451 + * (at your option) any later version.
33453 + * This program is distributed in the hope that it will be useful,
33454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33456 + * GNU General Public License for more details.
33458 + * You should have received a copy of the GNU General Public License
33459 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
33463 + * sub-routines for VFS
33466 +#ifndef __AUFS_VFSUB_H__
33467 +#define __AUFS_VFSUB_H__
33471 +#include <linux/fs.h>
33472 +#include <linux/mount.h>
33473 +#include <linux/posix_acl.h>
33474 +#include <linux/xattr.h>
33475 +#include "debug.h"
33477 +/* copied from linux/fs/internal.h */
33478 +/* todo: BAD approach!! */
33479 +extern void __mnt_drop_write(struct vfsmount *);
33480 +extern struct file *alloc_empty_file(int, const struct cred *);
33482 +/* ---------------------------------------------------------------------- */
33484 +/* lock subclass for lower inode */
33485 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33486 +/* reduce? gave up. */
33488 + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33489 + AuLsc_I_PARENT, /* lower inode, parent first */
33490 + AuLsc_I_PARENT2, /* copyup dirs */
33491 + AuLsc_I_PARENT3, /* copyup wh */
33497 +/* to debug easier, do not make them inlined functions */
33498 +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
33499 +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
33501 +/* ---------------------------------------------------------------------- */
33503 +static inline void vfsub_drop_nlink(struct inode *inode)
33505 + AuDebugOn(!inode->i_nlink);
33506 + drop_nlink(inode);
33509 +static inline void vfsub_dead_dir(struct inode *inode)
33511 + AuDebugOn(!S_ISDIR(inode->i_mode));
33512 + inode->i_flags |= S_DEAD;
33513 + clear_nlink(inode);
33516 +static inline int vfsub_native_ro(struct inode *inode)
33518 + return sb_rdonly(inode->i_sb)
33519 + || IS_RDONLY(inode)
33520 + /* || IS_APPEND(inode) */
33521 + || IS_IMMUTABLE(inode);
33524 +#ifdef CONFIG_AUFS_BR_FUSE
33525 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33527 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33530 +int vfsub_sync_filesystem(struct super_block *h_sb);
33532 +/* ---------------------------------------------------------------------- */
33534 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33535 +struct file *vfsub_dentry_open(struct path *path, int flags);
33536 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33538 +struct vfsub_aopen_args {
33539 + struct file *file;
33540 + unsigned int open_flag;
33541 + umode_t create_mode;
33542 + struct au_branch *br;
33544 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33545 + struct vfsub_aopen_args *args);
33546 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33548 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33549 + struct path *ppath, int len);
33550 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33553 +struct vfsub_lkup_one_args {
33554 + struct dentry **errp;
33555 + struct qstr *name;
33556 + struct path *ppath;
33559 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33560 + struct path *ppath)
33562 + return vfsub_lookup_one_len(name->name, ppath, name->len);
33565 +void vfsub_call_lkup_one(void *args);
33567 +/* ---------------------------------------------------------------------- */
33569 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33574 + err = mnt_want_write(mnt);
33579 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33582 + mnt_drop_write(mnt);
33586 +#if 0 /* reserved */
33587 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33590 + mnt_drop_write_file(file);
33595 +static inline void vfsub_file_start_write(struct file *file)
33598 + file_start_write(file);
33602 +static inline void vfsub_file_end_write(struct file *file)
33605 + file_end_write(file);
33609 +/* ---------------------------------------------------------------------- */
33612 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33613 + struct dentry *d2, struct au_hinode *hdir2);
33614 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33615 + struct dentry *d2, struct au_hinode *hdir2);
33617 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33619 +int vfsub_symlink(struct inode *dir, struct path *path,
33620 + const char *symname);
33621 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33622 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33623 + struct path *path, struct inode **delegated_inode);
33624 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33625 + struct inode *hdir, struct path *path,
33626 + struct inode **delegated_inode, unsigned int flags);
33627 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33628 +int vfsub_rmdir(struct inode *dir, struct path *path);
33630 +/* ---------------------------------------------------------------------- */
33632 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33634 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33636 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33638 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33640 +int vfsub_flush(struct file *file, fl_owner_t id);
33641 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33643 +static inline loff_t vfsub_f_size_read(struct file *file)
33645 + return i_size_read(file_inode(file));
33648 +static inline unsigned int vfsub_file_flags(struct file *file)
33650 + unsigned int flags;
33652 + spin_lock(&file->f_lock);
33653 + flags = file->f_flags;
33654 + spin_unlock(&file->f_lock);
33659 +static inline int vfsub_file_execed(struct file *file)
33661 + /* todo: direct access f_flags */
33662 + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33665 +#if 0 /* reserved */
33666 +static inline void vfsub_file_accessed(struct file *h_file)
33668 + file_accessed(h_file);
33669 + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33673 +#if 0 /* reserved */
33674 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33675 + struct dentry *h_dentry)
33677 + struct path h_path = {
33678 + .dentry = h_dentry,
33681 + touch_atime(&h_path);
33682 + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33686 +static inline int vfsub_update_time(struct inode *h_inode, int flags)
33688 + return inode_update_time(h_inode, flags);
33689 + /* no vfsub_update_h_iattr() since we don't have struct path */
33692 +#ifdef CONFIG_FS_POSIX_ACL
33693 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
33694 + struct dentry *h_dentry, umode_t h_mode)
33698 + err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
33699 + if (err == -EOPNOTSUPP)
33704 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
33705 + struct dentry *h_dentry, umode_t h_mode);
33708 +long vfsub_splice_read(struct file *in, loff_t *ppos,
33709 + struct pipe_inode_info *pipe, size_t len,
33710 + unsigned int flags);
33711 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33712 + loff_t *ppos, size_t len, unsigned int flags);
33714 +static inline long vfsub_truncate(struct path *path, loff_t length)
33719 + err = vfs_truncate(path, length);
33724 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33725 + struct file *h_file);
33726 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33729 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33732 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33738 + err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33744 +/* copy_file_range(2) is a systemcall */
33745 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33746 + struct file *dst, loff_t dst_pos,
33747 + size_t len, unsigned int flags)
33752 + ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33758 +/* ---------------------------------------------------------------------- */
33760 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33765 + err = vfs_llseek(file, offset, origin);
33770 +/* ---------------------------------------------------------------------- */
33772 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33773 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33774 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33775 + struct inode **delegated_inode);
33776 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33777 + struct inode **delegated_inode);
33778 +int vfsub_unlink(struct inode *dir, struct path *path,
33779 + struct inode **delegated_inode, int force);
33781 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33783 + return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33786 +/* ---------------------------------------------------------------------- */
33788 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
33789 + struct dentry *dentry, const char *name,
33790 + const void *value, size_t size, int flags)
33795 + err = vfs_setxattr(idmap, dentry, name, value, size, flags);
33801 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
33802 + struct dentry *dentry, const char *name)
33807 + err = vfs_removexattr(idmap, dentry, name);
33813 +#ifdef CONFIG_FS_POSIX_ACL
33814 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
33815 + struct dentry *dentry, const char *name,
33816 + struct posix_acl *acl)
33821 + err = vfs_set_acl(idmap, dentry, name, acl);
33827 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
33828 + struct dentry *dentry, const char *name)
33833 + err = vfs_remove_acl(idmap, dentry, name);
33839 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
33840 + const char *name, struct posix_acl *acl);
33841 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
33842 + struct dentry *dentry, const char *name);
33845 +#endif /* __KERNEL__ */
33846 +#endif /* __AUFS_VFSUB_H__ */
33847 diff --git a/fs/aufs/wbr_policy.c b/fs/aufs/wbr_policy.c
33848 new file mode 100644
33849 index 00000000000000..c726f96b7bd538
33851 +++ b/fs/aufs/wbr_policy.c
33853 +// SPDX-License-Identifier: GPL-2.0
33855 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33857 + * This program is free software; you can redistribute it and/or modify
33858 + * it under the terms of the GNU General Public License as published by
33859 + * the Free Software Foundation; either version 2 of the License, or
33860 + * (at your option) any later version.
33862 + * This program is distributed in the hope that it will be useful,
33863 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33864 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33865 + * GNU General Public License for more details.
33867 + * You should have received a copy of the GNU General Public License
33868 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
33872 + * policies for selecting one among multiple writable branches
33875 +#include <linux/statfs.h>
33878 +/* subset of cpup_attr() */
33879 +static noinline_for_stack
33880 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33884 + struct inode *h_isrc;
33886 + h_isrc = d_inode(h_src);
33887 + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33888 + ia.ia_mode = h_isrc->i_mode;
33889 + ia.ia_uid = h_isrc->i_uid;
33890 + ia.ia_gid = h_isrc->i_gid;
33891 + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33892 + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33893 + /* no delegation since it is just created */
33894 + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33896 + /* is this nfs only? */
33897 + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33898 + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33899 + ia.ia_mode = h_isrc->i_mode;
33900 + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33906 +#define AuCpdown_PARENT_OPQ 1
33907 +#define AuCpdown_WHED (1 << 1)
33908 +#define AuCpdown_MADE_DIR (1 << 2)
33909 +#define AuCpdown_DIROPQ (1 << 3)
33910 +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
33911 +#define au_fset_cpdown(flags, name) \
33912 + do { (flags) |= AuCpdown_##name; } while (0)
33913 +#define au_fclr_cpdown(flags, name) \
33914 + do { (flags) &= ~AuCpdown_##name; } while (0)
33916 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33917 + unsigned int *flags)
33920 + struct dentry *opq_dentry;
33922 + opq_dentry = au_diropq_create(dentry, bdst);
33923 + err = PTR_ERR(opq_dentry);
33924 + if (IS_ERR(opq_dentry))
33926 + dput(opq_dentry);
33927 + au_fset_cpdown(*flags, DIROPQ);
33933 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33934 + struct inode *dir, aufs_bindex_t bdst)
33937 + struct path h_path;
33938 + struct au_branch *br;
33940 + br = au_sbr(dentry->d_sb, bdst);
33941 + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33942 + err = PTR_ERR(h_path.dentry);
33943 + if (IS_ERR(h_path.dentry))
33947 + if (d_is_positive(h_path.dentry)) {
33948 + h_path.mnt = au_br_mnt(br);
33949 + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33952 + dput(h_path.dentry);
33958 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33959 + struct au_pin *pin,
33960 + struct dentry *h_parent, void *arg)
33963 + aufs_bindex_t bopq, btop;
33964 + struct path h_path;
33965 + struct dentry *parent;
33966 + struct inode *h_dir, *h_inode, *inode, *dir;
33967 + unsigned int *flags = arg;
33969 + btop = au_dbtop(dentry);
33970 + /* dentry is di-locked */
33971 + parent = dget_parent(dentry);
33972 + dir = d_inode(parent);
33973 + h_dir = d_inode(h_parent);
33974 + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33975 + IMustLock(h_dir);
33977 + err = au_lkup_neg(dentry, bdst, /*wh*/0);
33978 + if (unlikely(err < 0))
33980 + h_path.dentry = au_h_dptr(dentry, bdst);
33981 + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33982 + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
33983 + if (unlikely(err))
33985 + au_fset_cpdown(*flags, MADE_DIR);
33987 + bopq = au_dbdiropq(dentry);
33988 + au_fclr_cpdown(*flags, WHED);
33989 + au_fclr_cpdown(*flags, DIROPQ);
33990 + if (au_dbwh(dentry) == bdst)
33991 + au_fset_cpdown(*flags, WHED);
33992 + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33993 + au_fset_cpdown(*flags, PARENT_OPQ);
33994 + h_inode = d_inode(h_path.dentry);
33995 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
33996 + if (au_ftest_cpdown(*flags, WHED)) {
33997 + err = au_cpdown_dir_opq(dentry, bdst, flags);
33998 + if (unlikely(err)) {
33999 + inode_unlock(h_inode);
34004 + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34005 + inode_unlock(h_inode);
34006 + if (unlikely(err))
34009 + if (au_ftest_cpdown(*flags, WHED)) {
34010 + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34011 + if (unlikely(err))
34015 + inode = d_inode(dentry);
34016 + if (au_ibbot(inode) < bdst)
34017 + au_set_ibbot(inode, bdst);
34018 + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34019 + au_hi_flags(inode, /*isdir*/1));
34020 + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34021 + goto out; /* success */
34025 + if (au_ftest_cpdown(*flags, DIROPQ)) {
34026 + inode_lock_nested(h_inode, AuLsc_I_CHILD);
34027 + rerr = au_diropq_remove(dentry, bdst);
34028 + inode_unlock(h_inode);
34029 + if (unlikely(rerr)) {
34030 + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34031 + dentry, bdst, rerr);
34037 + if (au_ftest_cpdown(*flags, MADE_DIR)) {
34038 + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34039 + if (unlikely(rerr)) {
34040 + AuIOErr("failed removing %pd b%d (%d)\n",
34041 + dentry, bdst, rerr);
34046 + au_set_h_dptr(dentry, bdst, NULL);
34047 + if (au_dbbot(dentry) == bdst)
34048 + au_update_dbbot(dentry);
34054 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34057 + unsigned int flags;
34060 + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34065 +/* ---------------------------------------------------------------------- */
34067 +/* policies for create */
34069 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34071 + int err, i, j, ndentry;
34072 + aufs_bindex_t bopq;
34073 + struct au_dcsub_pages dpages;
34074 + struct au_dpage *dpage;
34075 + struct dentry **dentries, *parent, *d;
34077 + err = au_dpages_init(&dpages, GFP_NOFS);
34078 + if (unlikely(err))
34080 + parent = dget_parent(dentry);
34081 + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34082 + if (unlikely(err))
34086 + for (i = 0; i < dpages.ndpage; i++) {
34087 + dpage = dpages.dpages + i;
34088 + dentries = dpage->dentries;
34089 + ndentry = dpage->ndentry;
34090 + for (j = 0; j < ndentry; j++) {
34092 + di_read_lock_parent2(d, !AuLock_IR);
34093 + bopq = au_dbdiropq(d);
34094 + di_read_unlock(d, !AuLock_IR);
34095 + if (bopq >= 0 && bopq < err)
34102 + au_dpages_free(&dpages);
34107 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34109 + for (; bindex >= 0; bindex--)
34110 + if (!au_br_rdonly(au_sbr(sb, bindex)))
34115 +/* top down parent */
34116 +static int au_wbr_create_tdp(struct dentry *dentry,
34117 + unsigned int flags __maybe_unused)
34120 + aufs_bindex_t btop, bindex;
34121 + struct super_block *sb;
34122 + struct dentry *parent, *h_parent;
34124 + sb = dentry->d_sb;
34125 + btop = au_dbtop(dentry);
34127 + if (!au_br_rdonly(au_sbr(sb, btop)))
34131 + parent = dget_parent(dentry);
34132 + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34133 + h_parent = au_h_dptr(parent, bindex);
34134 + if (!h_parent || d_is_negative(h_parent))
34137 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
34144 + /* bottom up here */
34145 + if (unlikely(err < 0)) {
34146 + err = au_wbr_bu(sb, btop - 1);
34148 + err = au_wbr_nonopq(dentry, err);
34152 + AuDbg("b%d\n", err);
34156 +/* ---------------------------------------------------------------------- */
34158 +/* an exception for the policy other than tdp */
34159 +static int au_wbr_create_exp(struct dentry *dentry)
34162 + aufs_bindex_t bwh, bdiropq;
34163 + struct dentry *parent;
34166 + bwh = au_dbwh(dentry);
34167 + parent = dget_parent(dentry);
34168 + bdiropq = au_dbdiropq(parent);
34170 + if (bdiropq >= 0)
34171 + err = min(bdiropq, bwh);
34174 + AuDbg("%d\n", err);
34175 + } else if (bdiropq >= 0) {
34177 + AuDbg("%d\n", err);
34182 + err = au_wbr_nonopq(dentry, err);
34184 + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34187 + AuDbg("%d\n", err);
34191 +/* ---------------------------------------------------------------------- */
34194 +static int au_wbr_create_init_rr(struct super_block *sb)
34198 + err = au_wbr_bu(sb, au_sbbot(sb));
34199 + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34202 + AuDbg("b%d\n", err);
34206 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34210 + aufs_bindex_t bindex, bbot;
34211 + struct super_block *sb;
34214 + err = au_wbr_create_exp(dentry);
34218 + sb = dentry->d_sb;
34219 + next = &au_sbi(sb)->si_wbr_rr_next;
34220 + bbot = au_sbbot(sb);
34222 + for (bindex = 0; bindex <= bbot; bindex++) {
34223 + if (!au_ftest_wbr(flags, DIR)) {
34224 + err = atomic_dec_return(next) + 1;
34225 + /* modulo for 0 is meaningless */
34226 + if (unlikely(!err))
34227 + err = atomic_dec_return(next) + 1;
34229 + err = atomic_read(next);
34230 + AuDbg("%d\n", err);
34233 + AuDbg("%d\n", err);
34234 + if (!au_br_rdonly(au_sbr(sb, err)))
34240 + err = au_wbr_nonopq(dentry, err);
34243 + AuDbg("%d\n", err);
34247 +/* ---------------------------------------------------------------------- */
34249 +/* most free space */
34250 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34252 + struct super_block *sb;
34253 + struct au_branch *br;
34254 + struct au_wbr_mfs *mfs;
34255 + struct dentry *h_parent;
34256 + aufs_bindex_t bindex, bbot;
34258 + unsigned long long b, bavail;
34259 + struct path h_path;
34260 + /* reduce the stack usage */
34261 + struct kstatfs *st;
34263 + st = kmalloc(sizeof(*st), GFP_NOFS);
34264 + if (unlikely(!st)) {
34265 + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34270 + sb = dentry->d_sb;
34271 + mfs = &au_sbi(sb)->si_wbr_mfs;
34272 + MtxMustLock(&mfs->mfs_lock);
34273 + mfs->mfs_bindex = -EROFS;
34274 + mfs->mfsrr_bytes = 0;
34277 + bbot = au_sbbot(sb);
34279 + bindex = au_dbtop(parent);
34280 + bbot = au_dbtaildir(parent);
34283 + for (; bindex <= bbot; bindex++) {
34285 + h_parent = au_h_dptr(parent, bindex);
34286 + if (!h_parent || d_is_negative(h_parent))
34289 + br = au_sbr(sb, bindex);
34290 + if (au_br_rdonly(br))
34293 + /* sb->s_root for NFS is unreliable */
34294 + h_path.mnt = au_br_mnt(br);
34295 + h_path.dentry = h_path.mnt->mnt_root;
34296 + err = vfs_statfs(&h_path, st);
34297 + if (unlikely(err)) {
34298 + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34302 + /* when the available size is equal, select the lower one */
34303 + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34304 + || sizeof(b) < sizeof(st->f_bsize));
34305 + b = st->f_bavail * st->f_bsize;
34306 + br->br_wbr->wbr_bytes = b;
34307 + if (b >= bavail) {
34309 + mfs->mfs_bindex = bindex;
34310 + mfs->mfs_jiffy = jiffies;
34314 + mfs->mfsrr_bytes = bavail;
34315 + AuDbg("b%d\n", mfs->mfs_bindex);
34316 + au_kfree_rcu(st);
34319 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34322 + struct dentry *parent;
34323 + struct super_block *sb;
34324 + struct au_wbr_mfs *mfs;
34326 + err = au_wbr_create_exp(dentry);
34330 + sb = dentry->d_sb;
34332 + if (au_ftest_wbr(flags, PARENT))
34333 + parent = dget_parent(dentry);
34334 + mfs = &au_sbi(sb)->si_wbr_mfs;
34335 + mutex_lock(&mfs->mfs_lock);
34336 + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34337 + || mfs->mfs_bindex < 0
34338 + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34339 + au_mfs(dentry, parent);
34340 + mutex_unlock(&mfs->mfs_lock);
34341 + err = mfs->mfs_bindex;
34345 + err = au_wbr_nonopq(dentry, err);
34348 + AuDbg("b%d\n", err);
34352 +static int au_wbr_create_init_mfs(struct super_block *sb)
34354 + struct au_wbr_mfs *mfs;
34356 + mfs = &au_sbi(sb)->si_wbr_mfs;
34357 + mutex_init(&mfs->mfs_lock);
34358 + mfs->mfs_jiffy = 0;
34359 + mfs->mfs_bindex = -EROFS;
34364 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34366 + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34370 +/* ---------------------------------------------------------------------- */
34372 +/* top down regardless parent, and then mfs */
34373 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34374 + unsigned int flags __maybe_unused)
34377 + aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34378 + unsigned long long watermark;
34379 + struct super_block *sb;
34380 + struct au_wbr_mfs *mfs;
34381 + struct au_branch *br;
34382 + struct dentry *parent;
34384 + sb = dentry->d_sb;
34385 + mfs = &au_sbi(sb)->si_wbr_mfs;
34386 + mutex_lock(&mfs->mfs_lock);
34387 + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34388 + || mfs->mfs_bindex < 0)
34389 + au_mfs(dentry, /*parent*/NULL);
34390 + watermark = mfs->mfsrr_watermark;
34391 + bmfs = mfs->mfs_bindex;
34392 + mutex_unlock(&mfs->mfs_lock);
34394 + /* another style of au_wbr_create_exp() */
34395 + bwh = au_dbwh(dentry);
34396 + parent = dget_parent(dentry);
34397 + btail = au_dbtaildir(parent);
34398 + if (bwh >= 0 && bwh < btail)
34401 + err = au_wbr_nonopq(dentry, btail);
34402 + if (unlikely(err < 0))
34406 + for (bindex = 0; bindex <= btail; bindex++) {
34407 + br = au_sbr(sb, bindex);
34408 + if (au_br_rdonly(br))
34410 + if (br->br_wbr->wbr_bytes > watermark) {
34421 + AuDbg("b%d\n", err);
34425 +/* ---------------------------------------------------------------------- */
34427 +/* most free space and then round robin */
34428 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34431 + struct au_wbr_mfs *mfs;
34433 + err = au_wbr_create_mfs(dentry, flags);
34435 + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34436 + mutex_lock(&mfs->mfs_lock);
34437 + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34438 + err = au_wbr_create_rr(dentry, flags);
34439 + mutex_unlock(&mfs->mfs_lock);
34442 + AuDbg("b%d\n", err);
34446 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34450 + au_wbr_create_init_mfs(sb); /* ignore */
34451 + err = au_wbr_create_init_rr(sb);
34456 +/* ---------------------------------------------------------------------- */
34458 +/* top down parent and most free space */
34459 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34462 + unsigned long long b;
34463 + aufs_bindex_t bindex, btop, bbot;
34464 + struct super_block *sb;
34465 + struct dentry *parent, *h_parent;
34466 + struct au_branch *br;
34468 + err = au_wbr_create_tdp(dentry, flags);
34469 + if (unlikely(err < 0))
34471 + parent = dget_parent(dentry);
34472 + btop = au_dbtop(parent);
34473 + bbot = au_dbtaildir(parent);
34474 + if (btop == bbot)
34475 + goto out_parent; /* success */
34477 + e2 = au_wbr_create_mfs(dentry, flags);
34479 + goto out_parent; /* success */
34481 + /* when the available size is equal, select upper one */
34482 + sb = dentry->d_sb;
34483 + br = au_sbr(sb, err);
34484 + b = br->br_wbr->wbr_bytes;
34485 + AuDbg("b%d, %llu\n", err, b);
34487 + for (bindex = btop; bindex <= bbot; bindex++) {
34488 + h_parent = au_h_dptr(parent, bindex);
34489 + if (!h_parent || d_is_negative(h_parent))
34492 + br = au_sbr(sb, bindex);
34493 + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34494 + b = br->br_wbr->wbr_bytes;
34496 + AuDbg("b%d, %llu\n", err, b);
34501 + err = au_wbr_nonopq(dentry, err);
34506 + AuDbg("b%d\n", err);
34510 +/* ---------------------------------------------------------------------- */
34513 + * - top down parent
34514 + * - most free space with parent
34515 + * - most free space round-robin regardless parent
34517 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34520 + unsigned long long watermark;
34521 + struct super_block *sb;
34522 + struct au_branch *br;
34523 + struct au_wbr_mfs *mfs;
34525 + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34526 + if (unlikely(err < 0))
34529 + sb = dentry->d_sb;
34530 + br = au_sbr(sb, err);
34531 + mfs = &au_sbi(sb)->si_wbr_mfs;
34532 + mutex_lock(&mfs->mfs_lock);
34533 + watermark = mfs->mfsrr_watermark;
34534 + mutex_unlock(&mfs->mfs_lock);
34535 + if (br->br_wbr->wbr_bytes < watermark)
34536 + /* regardless the parent dir */
34537 + err = au_wbr_create_mfsrr(dentry, flags);
34540 + AuDbg("b%d\n", err);
34544 +/* ---------------------------------------------------------------------- */
34546 +/* policies for copyup */
34548 +/* top down parent */
34549 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34551 + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34554 +/* bottom up parent */
34555 +static int au_wbr_copyup_bup(struct dentry *dentry)
34558 + aufs_bindex_t bindex, btop;
34559 + struct dentry *parent, *h_parent;
34560 + struct super_block *sb;
34563 + sb = dentry->d_sb;
34564 + parent = dget_parent(dentry);
34565 + btop = au_dbtop(parent);
34566 + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34567 + h_parent = au_h_dptr(parent, bindex);
34568 + if (!h_parent || d_is_negative(h_parent))
34571 + if (!au_br_rdonly(au_sbr(sb, bindex))) {
34578 + /* bottom up here */
34579 + if (unlikely(err < 0))
34580 + err = au_wbr_bu(sb, btop - 1);
34582 + AuDbg("b%d\n", err);
34587 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34591 + err = au_wbr_bu(dentry->d_sb, btop);
34592 + AuDbg("b%d\n", err);
34594 + err = au_wbr_nonopq(dentry, err);
34596 + AuDbg("b%d\n", err);
34600 +static int au_wbr_copyup_bu(struct dentry *dentry)
34603 + aufs_bindex_t btop;
34605 + btop = au_dbtop(dentry);
34606 + err = au_wbr_do_copyup_bu(dentry, btop);
34610 +/* ---------------------------------------------------------------------- */
34612 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34613 + [AuWbrCopyup_TDP] = {
34614 + .copyup = au_wbr_copyup_tdp
34616 + [AuWbrCopyup_BUP] = {
34617 + .copyup = au_wbr_copyup_bup
34619 + [AuWbrCopyup_BU] = {
34620 + .copyup = au_wbr_copyup_bu
34624 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34625 + [AuWbrCreate_TDP] = {
34626 + .create = au_wbr_create_tdp
34628 + [AuWbrCreate_RR] = {
34629 + .create = au_wbr_create_rr,
34630 + .init = au_wbr_create_init_rr
34632 + [AuWbrCreate_MFS] = {
34633 + .create = au_wbr_create_mfs,
34634 + .init = au_wbr_create_init_mfs,
34635 + .fin = au_wbr_create_fin_mfs
34637 + [AuWbrCreate_MFSV] = {
34638 + .create = au_wbr_create_mfs,
34639 + .init = au_wbr_create_init_mfs,
34640 + .fin = au_wbr_create_fin_mfs
34642 + [AuWbrCreate_MFSRR] = {
34643 + .create = au_wbr_create_mfsrr,
34644 + .init = au_wbr_create_init_mfsrr,
34645 + .fin = au_wbr_create_fin_mfs
34647 + [AuWbrCreate_MFSRRV] = {
34648 + .create = au_wbr_create_mfsrr,
34649 + .init = au_wbr_create_init_mfsrr,
34650 + .fin = au_wbr_create_fin_mfs
34652 + [AuWbrCreate_TDMFS] = {
34653 + .create = au_wbr_create_tdmfs,
34654 + .init = au_wbr_create_init_mfs,
34655 + .fin = au_wbr_create_fin_mfs
34657 + [AuWbrCreate_TDMFSV] = {
34658 + .create = au_wbr_create_tdmfs,
34659 + .init = au_wbr_create_init_mfs,
34660 + .fin = au_wbr_create_fin_mfs
34662 + [AuWbrCreate_PMFS] = {
34663 + .create = au_wbr_create_pmfs,
34664 + .init = au_wbr_create_init_mfs,
34665 + .fin = au_wbr_create_fin_mfs
34667 + [AuWbrCreate_PMFSV] = {
34668 + .create = au_wbr_create_pmfs,
34669 + .init = au_wbr_create_init_mfs,
34670 + .fin = au_wbr_create_fin_mfs
34672 + [AuWbrCreate_PMFSRR] = {
34673 + .create = au_wbr_create_pmfsrr,
34674 + .init = au_wbr_create_init_mfsrr,
34675 + .fin = au_wbr_create_fin_mfs
34677 + [AuWbrCreate_PMFSRRV] = {
34678 + .create = au_wbr_create_pmfsrr,
34679 + .init = au_wbr_create_init_mfsrr,
34680 + .fin = au_wbr_create_fin_mfs
34683 diff --git a/fs/aufs/whout.c b/fs/aufs/whout.c
34684 new file mode 100644
34685 index 00000000000000..7bd0392eeb875e
34687 +++ b/fs/aufs/whout.c
34689 +// SPDX-License-Identifier: GPL-2.0
34691 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34693 + * This program is free software; you can redistribute it and/or modify
34694 + * it under the terms of the GNU General Public License as published by
34695 + * the Free Software Foundation; either version 2 of the License, or
34696 + * (at your option) any later version.
34698 + * This program is distributed in the hope that it will be useful,
34699 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34700 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34701 + * GNU General Public License for more details.
34703 + * You should have received a copy of the GNU General Public License
34704 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
34708 + * whiteout for logical deletion and opaque directory
34713 +#define WH_MASK 0444
34716 + * If a directory contains this file, then it is opaque. We start with the
34717 + * .wh. flag so that it is blocked by lookup.
34719 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34720 + sizeof(AUFS_WH_DIROPQ) - 1);
34723 + * generate whiteout name, which is NOT terminated by NULL.
34724 + * @name: original d_name.name
34725 + * @len: original d_name.len
34726 + * @wh: whiteout qstr
34727 + * returns zero when succeeds, otherwise error.
34728 + * succeeded value as wh->name should be freed by kfree().
34730 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34734 + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34735 + return -ENAMETOOLONG;
34737 + wh->len = name->len + AUFS_WH_PFX_LEN;
34738 + p = kmalloc(wh->len, GFP_NOFS);
34741 + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34742 + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34749 +/* ---------------------------------------------------------------------- */
34752 + * test if the @wh_name exists under @h_ppath.
34753 + * @try_sio specifies the necessary of super-io.
34755 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
34756 + struct qstr *wh_name, int try_sio)
34759 + struct dentry *wh_dentry;
34762 + wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
34764 + wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
34765 + err = PTR_ERR(wh_dentry);
34766 + if (IS_ERR(wh_dentry)) {
34767 + if (err == -ENAMETOOLONG)
34773 + if (d_is_negative(wh_dentry))
34774 + goto out_wh; /* success */
34777 + if (d_is_reg(wh_dentry))
34778 + goto out_wh; /* success */
34781 + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34782 + wh_dentry, d_inode(wh_dentry)->i_mode);
34791 + * test if the @h_path->dentry sets opaque or not.
34793 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
34796 + struct inode *h_dir;
34798 + h_dir = d_inode(h_path->dentry);
34799 + err = au_wh_test(h_idmap, h_path, &diropq_name,
34800 + au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
34805 + * returns a negative dentry whose name is unique and temporary.
34807 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34808 + struct qstr *prefix)
34810 + struct dentry *dentry;
34812 + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34814 + /* strict atomic_t is unnecessary here */
34815 + static unsigned short cnt;
34817 + struct path h_ppath;
34818 + struct mnt_idmap *h_idmap;
34820 + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34823 + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34824 + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34825 + dentry = ERR_PTR(-ENAMETOOLONG);
34826 + if (unlikely(qs.len > NAME_MAX))
34828 + dentry = ERR_PTR(-ENOMEM);
34829 + name = kmalloc(qs.len + 1, GFP_NOFS);
34830 + if (unlikely(!name))
34834 + /* doubly whiteout-ed */
34835 + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34836 + p = name + AUFS_WH_PFX_LEN * 2;
34837 + memcpy(p, prefix->name, prefix->len);
34838 + p += prefix->len;
34840 + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34842 + h_ppath.dentry = h_parent;
34843 + h_ppath.mnt = au_br_mnt(br);
34844 + h_idmap = au_br_idmap(br);
34846 + for (i = 0; i < 3; i++) {
34847 + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34848 + dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
34849 + if (IS_ERR(dentry) || d_is_negative(dentry))
34853 + /* pr_warn("could not get random name\n"); */
34854 + dentry = ERR_PTR(-EEXIST);
34855 + AuDbg("%.*s\n", AuLNPair(&qs));
34859 + if (name != defname)
34860 + au_kfree_try_rcu(name);
34862 + AuTraceErrPtr(dentry);
34867 + * rename the @h_dentry on @br to the whiteouted temporary name.
34869 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34872 + struct path h_path = {
34873 + .mnt = au_br_mnt(br)
34875 + struct inode *h_dir, *delegated;
34876 + struct dentry *h_parent;
34878 + h_parent = h_dentry->d_parent; /* dir inode is locked */
34879 + h_dir = d_inode(h_parent);
34880 + IMustLock(h_dir);
34882 + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34883 + err = PTR_ERR(h_path.dentry);
34884 + if (IS_ERR(h_path.dentry))
34887 + /* under the same dir, no need to lock_rename() */
34888 + delegated = NULL;
34889 + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34892 + if (unlikely(err == -EWOULDBLOCK)) {
34893 + pr_warn("cannot retry for NFSv4 delegation"
34894 + " for an internal rename\n");
34897 + dput(h_path.dentry);
34904 +/* ---------------------------------------------------------------------- */
34906 + * functions for removing a whiteout
34909 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34912 + struct inode *delegated;
34915 + * forces superio when the dir has a sticky bit.
34916 + * this may be a violation of unix fs semantics.
34918 + force = (h_dir->i_mode & S_ISVTX)
34919 + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34920 + delegated = NULL;
34921 + err = vfsub_unlink(h_dir, h_path, &delegated, force);
34922 + if (unlikely(err == -EWOULDBLOCK)) {
34923 + pr_warn("cannot retry for NFSv4 delegation"
34924 + " for an internal unlink\n");
34930 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34931 + struct dentry *dentry)
34935 + err = do_unlink_wh(h_dir, h_path);
34936 + if (!err && dentry)
34937 + au_set_dbwh(dentry, -1);
34942 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
34945 + struct path h_path;
34948 + h_path.dentry = vfsub_lkup_one(wh, h_ppath);
34949 + if (IS_ERR(h_path.dentry))
34950 + err = PTR_ERR(h_path.dentry);
34952 + if (d_is_reg(h_path.dentry)) {
34953 + h_path.mnt = h_ppath->mnt;
34954 + err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
34956 + dput(h_path.dentry);
34962 +/* ---------------------------------------------------------------------- */
34964 + * initialize/clean whiteout for a branch
34967 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34971 + struct inode *delegated;
34973 + if (d_is_negative(whpath->dentry))
34977 + err = vfsub_rmdir(h_dir, whpath);
34979 + delegated = NULL;
34980 + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34981 + if (unlikely(err == -EWOULDBLOCK)) {
34982 + pr_warn("cannot retry for NFSv4 delegation"
34983 + " for an internal unlink\n");
34987 + if (unlikely(err))
34988 + pr_warn("failed removing %pd (%d), ignored.\n",
34989 + whpath->dentry, err);
34992 +static int test_linkable(struct dentry *h_root)
34994 + struct inode *h_dir = d_inode(h_root);
34996 + if (h_dir->i_op->link)
34999 + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35000 + h_root, au_sbtype(h_root->d_sb));
35001 + return -ENOSYS; /* the branch doesn't have its ->link() */
35004 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35005 +static int au_whdir(struct inode *h_dir, struct path *path)
35010 + if (d_is_negative(path->dentry)) {
35013 + if (au_test_nfs(path->dentry->d_sb))
35015 + err = vfsub_mkdir(h_dir, path, mode);
35016 + } else if (d_is_dir(path->dentry))
35019 + pr_err("unknown %pd exists\n", path->dentry);
35024 +struct au_wh_base {
35025 + const struct qstr *name;
35026 + struct dentry *dentry;
35029 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35030 + struct path *h_path)
35032 + h_path->dentry = base[AuBrWh_BASE].dentry;
35033 + au_wh_clean(h_dir, h_path, /*isdir*/0);
35034 + h_path->dentry = base[AuBrWh_PLINK].dentry;
35035 + au_wh_clean(h_dir, h_path, /*isdir*/1);
35036 + h_path->dentry = base[AuBrWh_ORPH].dentry;
35037 + au_wh_clean(h_dir, h_path, /*isdir*/1);
35041 + * returns tri-state,
35042 + * minus: error, caller should print the message
35044 + * plus: error, caller should NOT print the message
35046 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35047 + int do_plink, struct au_wh_base base[],
35048 + struct path *h_path)
35051 + struct inode *h_dir;
35053 + h_dir = d_inode(h_root);
35054 + h_path->dentry = base[AuBrWh_BASE].dentry;
35055 + au_wh_clean(h_dir, h_path, /*isdir*/0);
35056 + h_path->dentry = base[AuBrWh_PLINK].dentry;
35058 + err = test_linkable(h_root);
35059 + if (unlikely(err)) {
35064 + err = au_whdir(h_dir, h_path);
35065 + if (unlikely(err))
35067 + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35069 + au_wh_clean(h_dir, h_path, /*isdir*/1);
35070 + h_path->dentry = base[AuBrWh_ORPH].dentry;
35071 + err = au_whdir(h_dir, h_path);
35072 + if (unlikely(err))
35074 + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35081 + * for the moment, aufs supports the branch filesystem which does not support
35082 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35083 + * copyup failed. finally, such filesystem will not be used as the writable
35086 + * returns tri-state, see above.
35088 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35089 + int do_plink, struct au_wh_base base[],
35090 + struct path *h_path)
35093 + struct inode *h_dir;
35095 + WbrWhMustWriteLock(wbr);
35097 + err = test_linkable(h_root);
35098 + if (unlikely(err)) {
35104 + * todo: should this create be done in /sbin/mount.aufs helper?
35107 + h_dir = d_inode(h_root);
35108 + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35109 + h_path->dentry = base[AuBrWh_BASE].dentry;
35110 + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35111 + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35114 + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35115 + if (unlikely(err))
35118 + h_path->dentry = base[AuBrWh_PLINK].dentry;
35120 + err = au_whdir(h_dir, h_path);
35121 + if (unlikely(err))
35123 + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35125 + au_wh_clean(h_dir, h_path, /*isdir*/1);
35126 + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35128 + h_path->dentry = base[AuBrWh_ORPH].dentry;
35129 + err = au_whdir(h_dir, h_path);
35130 + if (unlikely(err))
35132 + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35139 + * initialize the whiteout base file/dir for @br.
35141 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35144 + const unsigned char do_plink
35145 + = !!au_opt_test(au_mntflags(sb), PLINK);
35146 + struct inode *h_dir;
35147 + struct path path = br->br_path;
35148 + struct dentry *h_root = path.dentry;
35149 + struct au_wbr *wbr = br->br_wbr;
35150 + static const struct qstr base_name[] = {
35151 + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35152 + sizeof(AUFS_BASE_NAME) - 1),
35153 + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35154 + sizeof(AUFS_PLINKDIR_NAME) - 1),
35155 + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35156 + sizeof(AUFS_ORPHDIR_NAME) - 1)
35158 + struct au_wh_base base[] = {
35159 + [AuBrWh_BASE] = {
35160 + .name = base_name + AuBrWh_BASE,
35163 + [AuBrWh_PLINK] = {
35164 + .name = base_name + AuBrWh_PLINK,
35167 + [AuBrWh_ORPH] = {
35168 + .name = base_name + AuBrWh_ORPH,
35174 + WbrWhMustWriteLock(wbr);
35176 + for (i = 0; i < AuBrWh_Last; i++) {
35177 + /* doubly whiteouted */
35178 + struct dentry *d;
35180 + d = au_wh_lkup(h_root, (void *)base[i].name, br);
35181 + err = PTR_ERR(d);
35185 + base[i].dentry = d;
35187 + && wbr->wbr_wh[i]
35188 + && wbr->wbr_wh[i] != base[i].dentry);
35192 + for (i = 0; i < AuBrWh_Last; i++) {
35193 + dput(wbr->wbr_wh[i]);
35194 + wbr->wbr_wh[i] = NULL;
35198 + if (!au_br_writable(br->br_perm)) {
35199 + h_dir = d_inode(h_root);
35200 + au_wh_init_ro(h_dir, base, &path);
35201 + } else if (!au_br_wh_linkable(br->br_perm)) {
35202 + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35208 + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35214 + goto out; /* success */
35217 + pr_err("an error(%d) on the writable branch %pd(%s)\n",
35218 + err, h_root, au_sbtype(h_root->d_sb));
35220 + for (i = 0; i < AuBrWh_Last; i++)
35221 + dput(base[i].dentry);
35225 +/* ---------------------------------------------------------------------- */
35227 + * whiteouts are all hard-linked usually.
35228 + * when its link count reaches a ceiling, we create a new whiteout base
35229 + * asynchronously.
35232 +struct reinit_br_wh {
35233 + struct super_block *sb;
35234 + struct au_branch *br;
35237 +static void reinit_br_wh(void *arg)
35240 + aufs_bindex_t bindex;
35241 + struct path h_path;
35242 + struct reinit_br_wh *a = arg;
35243 + struct au_wbr *wbr;
35244 + struct inode *dir, *delegated;
35245 + struct dentry *h_root;
35246 + struct au_hinode *hdir;
35249 + wbr = a->br->br_wbr;
35250 + /* big aufs lock */
35251 + si_noflush_write_lock(a->sb);
35252 + if (!au_br_writable(a->br->br_perm))
35254 + bindex = au_br_index(a->sb, a->br->br_id);
35255 + if (unlikely(bindex < 0))
35258 + di_read_lock_parent(a->sb->s_root, AuLock_IR);
35259 + dir = d_inode(a->sb->s_root);
35260 + hdir = au_hi(dir, bindex);
35261 + h_root = au_h_dptr(a->sb->s_root, bindex);
35262 + AuDebugOn(h_root != au_br_dentry(a->br));
35264 + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35265 + wbr_wh_write_lock(wbr);
35266 + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35269 + h_path.dentry = wbr->wbr_whbase;
35270 + h_path.mnt = au_br_mnt(a->br);
35271 + delegated = NULL;
35272 + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35274 + if (unlikely(err == -EWOULDBLOCK)) {
35275 + pr_warn("cannot retry for NFSv4 delegation"
35276 + " for an internal unlink\n");
35280 + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35283 + dput(wbr->wbr_whbase);
35284 + wbr->wbr_whbase = NULL;
35286 + err = au_wh_init(a->br, a->sb);
35287 + wbr_wh_write_unlock(wbr);
35288 + au_hn_inode_unlock(hdir);
35289 + di_read_unlock(a->sb->s_root, AuLock_IR);
35291 + au_fhsm_wrote(a->sb, bindex, /*force*/0);
35295 + atomic_dec(&wbr->wbr_wh_running);
35296 + au_lcnt_dec(&a->br->br_count);
35297 + si_write_unlock(a->sb);
35298 + au_nwt_done(&au_sbi(a->sb)->si_nowait);
35300 + if (unlikely(err))
35301 + AuIOErr("err %d\n", err);
35304 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35306 + int do_dec, wkq_err;
35307 + struct reinit_br_wh *arg;
35310 + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35313 + /* ignore ENOMEM */
35314 + arg = kmalloc(sizeof(*arg), GFP_NOFS);
35317 + * dec(wh_running), kfree(arg) and dec(br_count)
35318 + * in reinit function
35322 + au_lcnt_inc(&br->br_count);
35323 + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35324 + if (unlikely(wkq_err)) {
35325 + atomic_dec(&br->br_wbr->wbr_wh_running);
35326 + au_lcnt_dec(&br->br_count);
35327 + au_kfree_rcu(arg);
35334 + atomic_dec(&br->br_wbr->wbr_wh_running);
35337 +/* ---------------------------------------------------------------------- */
35340 + * create the whiteout @wh.
35342 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35343 + struct dentry *wh)
35346 + struct path h_path = {
35349 + struct au_branch *br;
35350 + struct au_wbr *wbr;
35351 + struct dentry *h_parent;
35352 + struct inode *h_dir, *delegated;
35354 + h_parent = wh->d_parent; /* dir inode is locked */
35355 + h_dir = d_inode(h_parent);
35356 + IMustLock(h_dir);
35358 + br = au_sbr(sb, bindex);
35359 + h_path.mnt = au_br_mnt(br);
35360 + wbr = br->br_wbr;
35361 + wbr_wh_read_lock(wbr);
35362 + if (wbr->wbr_whbase) {
35363 + delegated = NULL;
35364 + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35365 + if (unlikely(err == -EWOULDBLOCK)) {
35366 + pr_warn("cannot retry for NFSv4 delegation"
35367 + " for an internal link\n");
35370 + if (!err || err != -EMLINK)
35373 + /* link count full. re-initialize br_whbase. */
35374 + kick_reinit_br_wh(sb, br);
35377 + /* return this error in this context */
35378 + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35380 + au_fhsm_wrote(sb, bindex, /*force*/0);
35383 + wbr_wh_read_unlock(wbr);
35387 +/* ---------------------------------------------------------------------- */
35390 + * create or remove the diropq.
35392 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35393 + unsigned int flags)
35395 + struct dentry *opq_dentry;
35396 + struct super_block *sb;
35397 + struct au_branch *br;
35398 + struct path h_path;
35401 + sb = dentry->d_sb;
35402 + br = au_sbr(sb, bindex);
35403 + h_path.dentry = au_h_dptr(dentry, bindex);
35404 + h_path.mnt = au_br_mnt(br);
35405 + opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35406 + if (IS_ERR(opq_dentry))
35409 + if (au_ftest_diropq(flags, CREATE)) {
35410 + err = link_or_create_wh(sb, bindex, opq_dentry);
35412 + au_set_dbdiropq(dentry, bindex);
35413 + goto out; /* success */
35416 + h_path.dentry = opq_dentry;
35417 + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35419 + au_set_dbdiropq(dentry, -1);
35421 + dput(opq_dentry);
35422 + opq_dentry = ERR_PTR(err);
35425 + return opq_dentry;
35428 +struct do_diropq_args {
35429 + struct dentry **errp;
35430 + struct dentry *dentry;
35431 + aufs_bindex_t bindex;
35432 + unsigned int flags;
35435 +static void call_do_diropq(void *args)
35437 + struct do_diropq_args *a = args;
35438 + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35441 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35442 + unsigned int flags)
35444 + struct dentry *diropq, *h_dentry;
35445 + struct mnt_idmap *h_idmap;
35447 + h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35448 + h_dentry = au_h_dptr(dentry, bindex);
35449 + if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35450 + MAY_EXEC | MAY_WRITE))
35451 + diropq = do_diropq(dentry, bindex, flags);
35454 + struct do_diropq_args args = {
35456 + .dentry = dentry,
35457 + .bindex = bindex,
35461 + wkq_err = au_wkq_wait(call_do_diropq, &args);
35462 + if (unlikely(wkq_err))
35463 + diropq = ERR_PTR(wkq_err);
35469 +/* ---------------------------------------------------------------------- */
35472 + * lookup whiteout dentry.
35473 + * @h_parent: lower parent dentry which must exist and be locked
35474 + * @base_name: name of dentry which will be whiteouted
35475 + * returns dentry for whiteout.
35477 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35478 + struct au_branch *br)
35481 + struct qstr wh_name;
35482 + struct dentry *wh_dentry;
35483 + struct path h_path;
35485 + err = au_wh_name_alloc(&wh_name, base_name);
35486 + wh_dentry = ERR_PTR(err);
35488 + h_path.dentry = h_parent;
35489 + h_path.mnt = au_br_mnt(br);
35490 + wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35491 + au_kfree_try_rcu(wh_name.name);
35493 + return wh_dentry;
35497 + * link/create a whiteout for @dentry on @bindex.
35499 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35500 + struct dentry *h_parent)
35502 + struct dentry *wh_dentry;
35503 + struct super_block *sb;
35506 + sb = dentry->d_sb;
35507 + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35508 + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35509 + err = link_or_create_wh(sb, bindex, wh_dentry);
35511 + au_set_dbwh(dentry, bindex);
35512 + au_fhsm_wrote(sb, bindex, /*force*/0);
35515 + wh_dentry = ERR_PTR(err);
35519 + return wh_dentry;
35522 +/* ---------------------------------------------------------------------- */
35524 +/* Delete all whiteouts in this directory on branch bindex. */
35525 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35526 + aufs_bindex_t bindex)
35529 + unsigned long ul, n;
35530 + struct qstr wh_name;
35532 + struct hlist_head *head;
35533 + struct au_vdir_wh *pos;
35534 + struct au_vdir_destr *str;
35537 + p = (void *)__get_free_page(GFP_NOFS);
35538 + wh_name.name = p;
35539 + if (unlikely(!wh_name.name))
35543 + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35544 + p += AUFS_WH_PFX_LEN;
35545 + n = whlist->nh_num;
35546 + head = whlist->nh_head;
35547 + for (ul = 0; !err && ul < n; ul++, head++) {
35548 + hlist_for_each_entry(pos, head, wh_hash) {
35549 + if (pos->wh_bindex != bindex)
35552 + str = &pos->wh_str;
35553 + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35554 + memcpy(p, str->name, str->len);
35555 + wh_name.len = AUFS_WH_PFX_LEN + str->len;
35556 + err = unlink_wh_name(h_path, &wh_name);
35561 + AuIOErr("whiteout name too long %.*s\n",
35562 + str->len, str->name);
35567 + free_page((unsigned long)wh_name.name);
35573 +struct del_wh_children_args {
35575 + struct path *h_path;
35576 + struct au_nhash *whlist;
35577 + aufs_bindex_t bindex;
35580 +static void call_del_wh_children(void *args)
35582 + struct del_wh_children_args *a = args;
35583 + *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35586 +/* ---------------------------------------------------------------------- */
35588 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35590 + struct au_whtmp_rmdir *whtmp;
35592 + unsigned int rdhash;
35594 + SiMustAnyLock(sb);
35596 + whtmp = kzalloc(sizeof(*whtmp), gfp);
35597 + if (unlikely(!whtmp)) {
35598 + whtmp = ERR_PTR(-ENOMEM);
35602 + /* no estimation for dir size */
35603 + rdhash = au_sbi(sb)->si_rdhash;
35605 + rdhash = AUFS_RDHASH_DEF;
35606 + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35607 + if (unlikely(err)) {
35608 + au_kfree_rcu(whtmp);
35609 + whtmp = ERR_PTR(err);
35616 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35619 + au_lcnt_dec(&whtmp->br->br_count);
35620 + dput(whtmp->wh_dentry);
35621 + iput(whtmp->dir);
35622 + au_nhash_wh_free(&whtmp->whlist);
35623 + au_kfree_rcu(whtmp);
35627 + * rmdir the whiteouted temporary named dir @h_dentry.
35628 + * @whlist: whiteouted children.
35630 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35631 + struct dentry *wh_dentry, struct au_nhash *whlist)
35634 + unsigned int h_nlink;
35635 + struct path wh_path;
35636 + struct inode *wh_inode, *h_dir;
35637 + struct au_branch *br;
35638 + struct mnt_idmap *h_idmap;
35640 + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35641 + IMustLock(h_dir);
35643 + br = au_sbr(dir->i_sb, bindex);
35644 + wh_path.dentry = wh_dentry;
35645 + wh_path.mnt = au_br_mnt(br);
35646 + h_idmap = au_br_idmap(br);
35647 + wh_inode = d_inode(wh_dentry);
35648 + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35651 + * someone else might change some whiteouts while we were sleeping.
35652 + * it means this whlist may have an obsoleted entry.
35654 + if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
35655 + err = del_wh_children(&wh_path, whlist, bindex);
35658 + struct del_wh_children_args args = {
35660 + .h_path = &wh_path,
35661 + .whlist = whlist,
35665 + wkq_err = au_wkq_wait(call_del_wh_children, &args);
35666 + if (unlikely(wkq_err))
35669 + inode_unlock(wh_inode);
35672 + h_nlink = h_dir->i_nlink;
35673 + err = vfsub_rmdir(h_dir, &wh_path);
35674 + /* some fs doesn't change the parent nlink in some cases */
35675 + h_nlink -= h_dir->i_nlink;
35679 + if (au_ibtop(dir) == bindex) {
35680 + /* todo: dir->i_mutex is necessary */
35681 + au_cpup_attr_timesizes(dir);
35683 + vfsub_drop_nlink(dir);
35685 + return 0; /* success */
35688 + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35692 +static void call_rmdir_whtmp(void *args)
35695 + aufs_bindex_t bindex;
35696 + struct au_whtmp_rmdir *a = args;
35697 + struct super_block *sb;
35698 + struct dentry *h_parent;
35699 + struct inode *h_dir;
35700 + struct au_hinode *hdir;
35702 + /* rmdir by nfsd may cause deadlock with this i_mutex */
35703 + /* inode_lock(a->dir); */
35705 + sb = a->dir->i_sb;
35706 + si_read_lock(sb, !AuLock_FLUSH);
35707 + if (!au_br_writable(a->br->br_perm))
35709 + bindex = au_br_index(sb, a->br->br_id);
35710 + if (unlikely(bindex < 0))
35714 + ii_write_lock_parent(a->dir);
35715 + h_parent = dget_parent(a->wh_dentry);
35716 + h_dir = d_inode(h_parent);
35717 + hdir = au_hi(a->dir, bindex);
35718 + err = vfsub_mnt_want_write(au_br_mnt(a->br));
35719 + if (unlikely(err))
35721 + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35722 + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35725 + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35726 + au_hn_inode_unlock(hdir);
35727 + vfsub_mnt_drop_write(au_br_mnt(a->br));
35731 + ii_write_unlock(a->dir);
35733 + /* inode_unlock(a->dir); */
35734 + au_whtmp_rmdir_free(a);
35735 + si_read_unlock(sb);
35736 + au_nwt_done(&au_sbi(sb)->si_nowait);
35737 + if (unlikely(err))
35738 + AuIOErr("err %d\n", err);
35741 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35742 + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35745 + struct super_block *sb;
35749 + /* all post-process will be done in do_rmdir_whtmp(). */
35751 + args->dir = au_igrab(dir);
35752 + args->br = au_sbr(sb, bindex);
35753 + au_lcnt_inc(&args->br->br_count);
35754 + args->wh_dentry = dget(wh_dentry);
35755 + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35756 + if (unlikely(wkq_err)) {
35757 + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35758 + au_whtmp_rmdir_free(args);
35761 diff --git a/fs/aufs/whout.h b/fs/aufs/whout.h
35762 new file mode 100644
35763 index 00000000000000..a7ae8702e7a5cb
35765 +++ b/fs/aufs/whout.h
35767 +/* SPDX-License-Identifier: GPL-2.0 */
35769 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35771 + * This program is free software; you can redistribute it and/or modify
35772 + * it under the terms of the GNU General Public License as published by
35773 + * the Free Software Foundation; either version 2 of the License, or
35774 + * (at your option) any later version.
35776 + * This program is distributed in the hope that it will be useful,
35777 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35778 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35779 + * GNU General Public License for more details.
35781 + * You should have received a copy of the GNU General Public License
35782 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
35786 + * whiteout for logical deletion and opaque directory
35789 +#ifndef __AUFS_WHOUT_H__
35790 +#define __AUFS_WHOUT_H__
35797 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35798 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35799 + struct qstr *wh_name, int try_sio);
35800 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
35802 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35803 + struct qstr *prefix);
35804 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35805 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35806 + struct dentry *dentry);
35807 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35809 +/* diropq flags */
35810 +#define AuDiropq_CREATE 1
35811 +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
35812 +#define au_fset_diropq(flags, name) \
35813 + do { (flags) |= AuDiropq_##name; } while (0)
35814 +#define au_fclr_diropq(flags, name) \
35815 + do { (flags) &= ~AuDiropq_##name; } while (0)
35817 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35818 + unsigned int flags);
35819 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35820 + struct au_branch *br);
35821 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35822 + struct dentry *h_parent);
35824 +/* real rmdir for the whiteout-ed dir */
35825 +struct au_whtmp_rmdir {
35826 + struct inode *dir;
35827 + struct au_branch *br;
35828 + struct dentry *wh_dentry;
35829 + struct au_nhash whlist;
35832 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35833 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35834 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35835 + struct dentry *wh_dentry, struct au_nhash *whlist);
35836 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35837 + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35839 +/* ---------------------------------------------------------------------- */
35841 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35842 + aufs_bindex_t bindex)
35844 + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35847 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35849 + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35852 +#endif /* __KERNEL__ */
35853 +#endif /* __AUFS_WHOUT_H__ */
35854 diff --git a/fs/aufs/wkq.c b/fs/aufs/wkq.c
35855 new file mode 100644
35856 index 00000000000000..cb71ca3bf51b79
35858 +++ b/fs/aufs/wkq.c
35860 +// SPDX-License-Identifier: GPL-2.0
35862 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35864 + * This program is free software; you can redistribute it and/or modify
35865 + * it under the terms of the GNU General Public License as published by
35866 + * the Free Software Foundation; either version 2 of the License, or
35867 + * (at your option) any later version.
35869 + * This program is distributed in the hope that it will be useful,
35870 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35871 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35872 + * GNU General Public License for more details.
35874 + * You should have received a copy of the GNU General Public License
35875 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
35879 + * workqueue for asynchronous/super-io operations
35880 + * todo: try new credential scheme
35883 +#include <linux/module.h>
35886 +/* internal workqueue named AUFS_WKQ_NAME */
35888 +static struct workqueue_struct *au_wkq;
35890 +struct au_wkinfo {
35891 + struct work_struct wk;
35892 + struct kobject *kobj;
35894 + unsigned int flags; /* see wkq.h */
35896 + au_wkq_func_t func;
35899 +#ifdef CONFIG_LOCKDEP
35901 + struct held_lock **hlock;
35904 + struct completion *comp;
35907 +/* ---------------------------------------------------------------------- */
35909 + * Aufs passes some operations to the workqueue such as the internal copyup.
35910 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35911 + * job run by workqueue depends upon the locks acquired in the other task.
35912 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35913 + * information too. And the job in the workqueue restores the info in order to
35914 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35915 + * correctly and expectedly.
35918 +#ifndef CONFIG_LOCKDEP
35919 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35920 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35921 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35922 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35923 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35925 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35927 + wkinfo->hlock = NULL;
35928 + wkinfo->dont_check = 0;
35935 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35937 + static DEFINE_SPINLOCK(spin);
35940 + struct lock_class_key *key;
35942 + { .name = "&sbinfo->si_rwsem" },
35943 + { .name = "&finfo->fi_rwsem" },
35944 + { .name = "&dinfo->di_rwsem" },
35945 + { .name = "&iinfo->ii_rwsem" }
35950 + /* lockless read from 'set.' see below */
35951 + if (set == ARRAY_SIZE(a)) {
35952 + for (i = 0; i < ARRAY_SIZE(a); i++)
35953 + if (a[i].key == key)
35958 + spin_lock(&spin);
35960 + for (i = 0; i < ARRAY_SIZE(a); i++)
35961 + if (a[i].key == key) {
35962 + spin_unlock(&spin);
35965 + for (i = 0; i < ARRAY_SIZE(a); i++) {
35967 + if (unlikely(a[i].key == key)) { /* rare but possible */
35968 + spin_unlock(&spin);
35973 + if (strstr(a[i].name, name)) {
35975 + * the order of these three lines is important for the
35976 + * lockless read above.
35979 + spin_unlock(&spin);
35981 + /* AuDbg("%d, %s\n", set, name); */
35985 + spin_unlock(&spin);
35994 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35997 + struct task_struct *curr;
35998 + struct held_lock **hl, *held_locks, *p;
36002 + wkinfo->dont_check = lockdep_recursing(curr);
36003 + if (wkinfo->dont_check)
36005 + n = curr->lockdep_depth;
36010 + wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36011 + if (unlikely(!wkinfo->hlock))
36015 +#if 0 /* left for debugging */
36016 + if (0 && au_debug_test())
36017 + lockdep_print_held_locks(curr);
36019 + held_locks = curr->held_locks;
36020 + hl = wkinfo->hlock;
36022 + p = held_locks++;
36023 + if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36032 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36034 + au_kfree_try_rcu(wkinfo->hlock);
36037 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36039 + struct held_lock *p, **hl = wkinfo->hlock;
36042 + if (wkinfo->dont_check)
36046 + while ((p = *hl++)) { /* assignment */
36047 + subclass = lockdep_hlock_class(p)->subclass;
36048 + /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36050 + rwsem_acquire_read(p->instance, subclass, 0,
36051 + /*p->acquire_ip*/_RET_IP_);
36053 + rwsem_acquire(p->instance, subclass, 0,
36054 + /*p->acquire_ip*/_RET_IP_);
36058 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36060 + struct held_lock *p, **hl = wkinfo->hlock;
36062 + if (wkinfo->dont_check)
36066 + while ((p = *hl++)) /* assignment */
36067 + rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36071 +static void wkq_func(struct work_struct *wk)
36073 + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36075 + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36076 + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36078 + au_wkq_lockdep_pre(wkinfo);
36079 + wkinfo->func(wkinfo->args);
36080 + au_wkq_lockdep_post(wkinfo);
36081 + if (au_ftest_wkq(wkinfo->flags, WAIT))
36082 + complete(wkinfo->comp);
36084 + kobject_put(wkinfo->kobj);
36085 + module_put(THIS_MODULE); /* todo: ?? */
36086 + au_kfree_rcu(wkinfo);
36091 + * Since struct completion is large, try allocating it dynamically.
36093 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36095 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36097 + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36099 + init_completion(*comp);
36100 + wkinfo->comp = *comp;
36106 +static void au_wkq_comp_free(struct completion *comp)
36108 + au_kfree_rcu(comp);
36111 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36113 + if (au_ftest_wkq(wkinfo->flags, NEST)) {
36114 + if (au_wkq_test()) {
36115 + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36116 + " due to a dead dir by UDBA,"
36117 + " or async xino write?\n");
36118 + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36121 + au_dbg_verify_kthread();
36123 + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36124 + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36125 + queue_work(au_wkq, &wkinfo->wk);
36127 + INIT_WORK(&wkinfo->wk, wkq_func);
36128 + schedule_work(&wkinfo->wk);
36133 + * Be careful. It is easy to make deadlock happen.
36134 + * processA: lock, wkq and wait
36135 + * processB: wkq and wait, lock in wkq
36138 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36141 + AuWkqCompDeclare(comp);
36142 + struct au_wkinfo wkinfo = {
36148 + err = au_wkq_comp_alloc(&wkinfo, &comp);
36149 + if (unlikely(err))
36151 + err = au_wkq_lockdep_alloc(&wkinfo);
36152 + if (unlikely(err))
36155 + au_wkq_run(&wkinfo);
36156 + /* no timeout, no interrupt */
36157 + wait_for_completion(wkinfo.comp);
36159 + au_wkq_lockdep_free(&wkinfo);
36162 + au_wkq_comp_free(comp);
36164 + destroy_work_on_stack(&wkinfo.wk);
36169 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36170 + * problem in a concurrent umounting.
36172 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36173 + unsigned int flags)
36176 + struct au_wkinfo *wkinfo;
36178 + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36181 + * wkq_func() must free this wkinfo.
36182 + * it highly depends upon the implementation of workqueue.
36185 + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36187 + wkinfo->kobj = &au_sbi(sb)->si_kobj;
36188 + wkinfo->flags = flags & ~AuWkq_WAIT;
36189 + wkinfo->func = func;
36190 + wkinfo->args = args;
36191 + wkinfo->comp = NULL;
36192 + au_wkq_lockdep_init(wkinfo);
36193 + kobject_get(wkinfo->kobj);
36194 + __module_get(THIS_MODULE); /* todo: ?? */
36196 + au_wkq_run(wkinfo);
36199 + au_nwt_done(&au_sbi(sb)->si_nowait);
36205 +/* ---------------------------------------------------------------------- */
36207 +void au_nwt_init(struct au_nowait_tasks *nwt)
36209 + atomic_set(&nwt->nw_len, 0);
36210 + /* smp_mb(); */ /* atomic_set */
36211 + init_waitqueue_head(&nwt->nw_wq);
36214 +void au_wkq_fin(void)
36216 + destroy_workqueue(au_wkq);
36219 +int __init au_wkq_init(void)
36224 + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36225 + if (IS_ERR(au_wkq))
36226 + err = PTR_ERR(au_wkq);
36227 + else if (!au_wkq)
36232 diff --git a/fs/aufs/wkq.h b/fs/aufs/wkq.h
36233 new file mode 100644
36234 index 00000000000000..f8bb8e12395ed5
36236 +++ b/fs/aufs/wkq.h
36238 +/* SPDX-License-Identifier: GPL-2.0 */
36240 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36242 + * This program is free software; you can redistribute it and/or modify
36243 + * it under the terms of the GNU General Public License as published by
36244 + * the Free Software Foundation; either version 2 of the License, or
36245 + * (at your option) any later version.
36247 + * This program is distributed in the hope that it will be useful,
36248 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36249 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36250 + * GNU General Public License for more details.
36252 + * You should have received a copy of the GNU General Public License
36253 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
36257 + * workqueue for asynchronous/super-io operations
36258 + * todo: try new credentials management scheme
36261 +#ifndef __AUFS_WKQ_H__
36262 +#define __AUFS_WKQ_H__
36266 +#include <linux/wait.h>
36268 +struct super_block;
36270 +/* ---------------------------------------------------------------------- */
36273 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36275 +struct au_nowait_tasks {
36277 + wait_queue_head_t nw_wq;
36280 +/* ---------------------------------------------------------------------- */
36282 +typedef void (*au_wkq_func_t)(void *args);
36285 +#define AuWkq_WAIT 1
36286 +#define AuWkq_NEST (1 << 1)
36287 +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
36288 +#define au_fset_wkq(flags, name) \
36289 + do { (flags) |= AuWkq_##name; } while (0)
36290 +#define au_fclr_wkq(flags, name) \
36291 + do { (flags) &= ~AuWkq_##name; } while (0)
36294 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36295 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36296 + unsigned int flags);
36297 +void au_nwt_init(struct au_nowait_tasks *nwt);
36298 +int __init au_wkq_init(void);
36299 +void au_wkq_fin(void);
36301 +/* ---------------------------------------------------------------------- */
36303 +static inline int au_wkq_test(void)
36305 + return current->flags & PF_WQ_WORKER;
36308 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36310 + return au_wkq_do_wait(AuWkq_WAIT, func, args);
36313 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36315 + if (atomic_dec_and_test(&nwt->nw_len))
36316 + wake_up_all(&nwt->nw_wq);
36319 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36321 + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36325 +#endif /* __KERNEL__ */
36326 +#endif /* __AUFS_WKQ_H__ */
36327 diff --git a/fs/aufs/xattr.c b/fs/aufs/xattr.c
36328 new file mode 100644
36329 index 00000000000000..9e08665728aeed
36331 +++ b/fs/aufs/xattr.c
36333 +// SPDX-License-Identifier: GPL-2.0
36335 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36337 + * This program is free software; you can redistribute it and/or modify
36338 + * it under the terms of the GNU General Public License as published by
36339 + * the Free Software Foundation; either version 2 of the License, or
36340 + * (at your option) any later version.
36342 + * This program is distributed in the hope that it will be useful,
36343 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36344 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36345 + * GNU General Public License for more details.
36347 + * You should have received a copy of the GNU General Public License
36348 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
36352 + * handling xattr functions
36355 +#include <linux/fs.h>
36356 +#include <linux/xattr.h>
36359 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36361 + if (!ignore_flags)
36369 + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36374 +#define cmp(brattr, prefix) do { \
36375 + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36376 + XATTR_##prefix##_PREFIX_LEN)) { \
36377 + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36383 + cmp(SEC, SECURITY);
36384 + cmp(SYS, SYSTEM);
36385 + cmp(TR, TRUSTED);
36389 + if (ignore_flags & AuBrAttr_ICEX_OTH)
36396 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36398 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36399 + char *name, char **buf, unsigned int ignore_flags,
36400 + unsigned int verbose)
36404 + struct inode *h_idst;
36405 + struct dentry *h_dst_dentry, *h_src_dentry;
36406 + struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36407 + struct posix_acl *acl;
36409 + is_acl = !!is_posix_acl_xattr(name);
36410 + h_src_idmap = mnt_idmap(h_src->mnt);
36411 + h_src_dentry = h_src->dentry;
36413 + acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36415 + if (unlikely(IS_ERR(acl))) {
36416 + err = PTR_ERR(acl);
36417 + if (err == -ENODATA)
36419 + else if (err == -EOPNOTSUPP
36420 + && au_test_nfs_noacl(d_inode(h_src_dentry)))
36422 + else if (verbose || au_debug_test())
36423 + pr_err("%s, err %d\n", name, err);
36427 + ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36429 + if (unlikely(ssz <= 0)) {
36431 + if (err == -ENODATA)
36433 + else if (err == -EOPNOTSUPP
36434 + && (ignore_flags & au_xattr_out_of_list))
36436 + else if (err && (verbose || au_debug_test()))
36437 + pr_err("%s, err %d\n", name, err);
36442 + /* unlock it temporary */
36443 + h_dst_idmap = mnt_idmap(h_dst->mnt);
36444 + h_dst_dentry = h_dst->dentry;
36445 + h_idst = d_inode(h_dst_dentry);
36446 + inode_unlock(h_idst);
36448 + err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36449 + posix_acl_release(acl);
36451 + err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36452 + ssz, /*flags*/0);
36453 + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36454 + if (unlikely(err)) {
36455 + if (verbose || au_debug_test())
36456 + pr_err("%s, err %d\n", name, err);
36457 + err = au_xattr_ignore(err, name, ignore_flags);
36464 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36465 + unsigned int verbose)
36467 + int err, unlocked;
36469 + struct dentry *h_dst_dentry, *h_src_dentry;
36470 + struct inode *h_isrc, *h_idst;
36471 + char *value, *p, *o, *e;
36473 + /* try stopping to update the source inode while we are referencing */
36474 + /* there should not be the parent-child relationship between them */
36475 + h_dst_dentry = h_dst->dentry;
36476 + h_idst = d_inode(h_dst_dentry);
36477 + h_src_dentry = h_src->dentry;
36478 + h_isrc = d_inode(h_src_dentry);
36479 + inode_unlock(h_idst);
36480 + inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36481 + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36484 + /* some filesystems don't list POSIX ACL, for example tmpfs */
36485 + ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36487 + if (unlikely(err < 0)) {
36489 + if (err == -ENODATA
36490 + || err == -EOPNOTSUPP)
36491 + err = 0; /* ignore */
36500 + p = kmalloc(ssz, GFP_NOFS);
36502 + if (unlikely(!p))
36504 + err = vfs_listxattr(h_src_dentry, p, ssz);
36506 + inode_unlock_shared(h_isrc);
36508 + AuDbg("err %d, ssz %zd\n", err, ssz);
36509 + if (unlikely(err < 0))
36515 + ignore_flags |= au_xattr_out_of_list;
36516 + while (!err && p < e) {
36517 + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36519 + p += strlen(p) + 1;
36521 + au_kfree_try_rcu(value);
36524 + au_kfree_try_rcu(o);
36527 + inode_unlock_shared(h_isrc);
36532 +/* ---------------------------------------------------------------------- */
36534 +static int au_smack_reentering(struct super_block *sb)
36536 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36538 + * as a part of lookup, smack_d_instantiate() is called, and it calls
36539 + * i_op->getxattr(). ouch.
36541 + return si_pid_test(sb);
36552 +struct au_lgxattr {
36560 + const char *name;
36567 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36568 + struct au_lgxattr *arg)
36572 + struct path h_path;
36573 + struct super_block *sb;
36575 + sb = dentry->d_sb;
36576 + reenter = au_smack_reentering(sb);
36578 + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36579 + if (unlikely(err))
36582 + err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36583 + if (unlikely(err))
36585 + if (unlikely(!h_path.dentry))
36586 + /* illegally overlapped or something */
36587 + goto out_di; /* pretending success */
36589 + /* always topmost entry only */
36590 + switch (arg->type) {
36591 + case AU_XATTR_LIST:
36592 + err = vfs_listxattr(h_path.dentry,
36593 + arg->u.list.list, arg->u.list.size);
36595 + case AU_XATTR_GET:
36596 + AuDebugOn(d_is_negative(h_path.dentry));
36597 + err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
36598 + arg->u.get.name, arg->u.get.value,
36599 + arg->u.get.size);
36605 + di_read_unlock(dentry, AuLock_IR);
36608 + si_read_unlock(sb);
36614 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36616 + struct au_lgxattr arg = {
36617 + .type = AU_XATTR_LIST,
36624 + return au_lgxattr(dentry, /*inode*/NULL, &arg);
36627 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36628 + const char *name, void *value, size_t size)
36630 + struct au_lgxattr arg = {
36631 + .type = AU_XATTR_GET,
36639 + return au_lgxattr(dentry, inode, &arg);
36642 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36643 + const char *name, const void *value, size_t size,
36646 + struct au_sxattr arg = {
36647 + .type = AU_XATTR_SET,
36656 + return au_sxattr(dentry, inode, &arg);
36659 +/* ---------------------------------------------------------------------- */
36661 +static int au_xattr_get(const struct xattr_handler *handler,
36662 + struct dentry *dentry, struct inode *inode,
36663 + const char *name, void *buffer, size_t size)
36665 + return au_getxattr(dentry, inode, name, buffer, size);
36668 +static int au_xattr_set(const struct xattr_handler *handler,
36669 + struct mnt_idmap *idmap,
36670 + struct dentry *dentry, struct inode *inode,
36671 + const char *name, const void *value, size_t size,
36674 + return au_setxattr(dentry, inode, name, value, size, flags);
36677 +static const struct xattr_handler au_xattr_handler = {
36680 + .get = au_xattr_get,
36681 + .set = au_xattr_set
36684 +static const struct xattr_handler *au_xattr_handlers[] = {
36685 + &au_xattr_handler, /* must be last */
36689 +void au_xattr_init(struct super_block *sb)
36691 + sb->s_xattr = au_xattr_handlers;
36693 diff --git a/fs/aufs/xino.c b/fs/aufs/xino.c
36694 new file mode 100644
36695 index 00000000000000..e1db09b3541d1b
36697 +++ b/fs/aufs/xino.c
36699 +// SPDX-License-Identifier: GPL-2.0
36701 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36703 + * This program is free software; you can redistribute it and/or modify
36704 + * it under the terms of the GNU General Public License as published by
36705 + * the Free Software Foundation; either version 2 of the License, or
36706 + * (at your option) any later version.
36708 + * This program is distributed in the hope that it will be useful,
36709 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36710 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36711 + * GNU General Public License for more details.
36713 + * You should have received a copy of the GNU General Public License
36714 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
36718 + * external inode number translation table and bitmap
36720 + * things to consider
36722 + * + au_xino object
36723 + * + XINO files (xino, xib, xigen)
36724 + * + dynamic debugfs entries (xiN)
36725 + * + static debugfs entries (xib, xigen)
36726 + * + static sysfs entry (xi_path)
36727 + * - several entry points to handle them.
36728 + * + mount(2) without xino option (default)
36729 + * + mount(2) with xino option
36730 + * + mount(2) with noxino option
36732 + * + remount with add/del branches
36733 + * + remount with xino/noxino options
36736 +#include <linux/seq_file.h>
36737 +#include <linux/statfs.h>
36740 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36741 + aufs_bindex_t bbot,
36742 + struct super_block *h_sb)
36744 + /* todo: try binary-search if the branches are many */
36745 + for (; btop <= bbot; btop++)
36746 + if (h_sb == au_sbr_sb(sb, btop))
36752 + * find another branch who is on the same filesystem of the specified
36753 + * branch{@btgt}. search until @bbot.
36755 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36756 + aufs_bindex_t bbot)
36758 + aufs_bindex_t bindex;
36759 + struct super_block *tgt_sb;
36761 + tgt_sb = au_sbr_sb(sb, btgt);
36762 + bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36764 + bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36769 +/* ---------------------------------------------------------------------- */
36772 + * stop unnecessary notify events at creating xino files
36775 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36777 + aufs_bindex_t bfound, bindex, bbot;
36778 + struct dentry *parent;
36779 + struct au_branch *br;
36782 + parent = dentry->d_parent; /* safe d_parent access */
36783 + bbot = au_sbbot(sb);
36784 + for (bindex = 0; bindex <= bbot; bindex++) {
36785 + br = au_sbr(sb, bindex);
36786 + if (au_br_dentry(br) == parent) {
36792 + AuDbg("bfound b%d\n", bfound);
36796 +struct au_xino_lock_dir {
36797 + struct au_hinode *hdir;
36798 + struct dentry *parent;
36799 + struct inode *dir;
36802 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36803 + unsigned int lsc)
36805 + struct dentry *parent;
36806 + struct inode *dir;
36808 + parent = dget_parent(dentry);
36809 + dir = d_inode(parent);
36810 + inode_lock_nested(dir, lsc);
36811 +#if 0 /* it should not happen */
36812 + spin_lock(&dentry->d_lock);
36813 + if (unlikely(dentry->d_parent != parent)) {
36814 + spin_unlock(&dentry->d_lock);
36815 + inode_unlock(dir);
36820 + spin_unlock(&dentry->d_lock);
36827 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36828 + struct au_xino_lock_dir *ldir)
36830 + aufs_bindex_t bindex;
36832 + ldir->hdir = NULL;
36833 + bindex = au_xi_root(sb, xipath->dentry);
36834 + if (bindex >= 0) {
36835 + /* rw branch root */
36836 + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36837 + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36840 + ldir->parent = au_dget_parent_lock(xipath->dentry,
36842 + ldir->dir = d_inode(ldir->parent);
36846 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36849 + au_hn_inode_unlock(ldir->hdir);
36851 + inode_unlock(ldir->dir);
36852 + dput(ldir->parent);
36856 +/* ---------------------------------------------------------------------- */
36859 + * create and set a new xino file
36861 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36864 + struct file *file;
36865 + struct dentry *h_parent, *d;
36866 + struct inode *h_dir, *inode;
36868 + static DEFINE_MUTEX(mtx);
36871 + * at mount-time, and the xino file is the default path,
36872 + * hnotify is disabled so we have no notify events to ignore.
36873 + * when a user specified the xino, we cannot get au_hdir to be ignored.
36876 + mutex_lock(&mtx);
36877 + file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36878 + /* | __FMODE_NONOTIFY */,
36880 + if (IS_ERR(file)) {
36882 + mutex_unlock(&mtx);
36884 + pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36888 + /* keep file count */
36890 + d = file->f_path.dentry;
36891 + h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36893 + mutex_unlock(&mtx);
36894 + /* mnt_want_write() is unnecessary here */
36895 + h_dir = d_inode(h_parent);
36896 + inode = file_inode(file);
36897 + /* no delegation since it is just created */
36898 + if (inode->i_nlink)
36899 + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36901 + inode_unlock(h_dir);
36903 + if (unlikely(err)) {
36905 + pr_err("unlink %s(%d)\n", fpath, err);
36910 + if (unlikely(sb && sb == d->d_sb)) {
36912 + pr_err("%s must be outside\n", fpath);
36915 + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36917 + pr_err("xino doesn't support %s(%s)\n",
36918 + fpath, au_sbtype(d->d_sb));
36921 + return file; /* success */
36925 + file = ERR_PTR(err);
36930 + * create a new xinofile at the same place/path as @base.
36932 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36933 + struct file *copy_src)
36935 + struct file *file;
36936 + struct dentry *dentry;
36937 + struct inode *dir, *delegated;
36938 + struct qstr *name;
36939 + struct path ppath, path;
36940 + int err, do_unlock;
36941 + struct au_xino_lock_dir ldir;
36944 + au_xino_lock_dir(sb, base, &ldir);
36945 + dentry = base->dentry;
36946 + ppath.dentry = dentry->d_parent; /* dir inode is locked */
36947 + ppath.mnt = base->mnt;
36948 + dir = d_inode(ppath.dentry);
36951 + name = &dentry->d_name;
36952 + path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
36953 + if (IS_ERR(path.dentry)) {
36954 + file = (void *)path.dentry;
36955 + pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36959 + /* no need to mnt_want_write() since we call dentry_open() later */
36960 + err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
36961 + if (unlikely(err)) {
36962 + file = ERR_PTR(err);
36963 + pr_err("%pd create err %d\n", dentry, err);
36967 + path.mnt = base->mnt;
36968 + file = vfsub_dentry_open(&path,
36969 + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36970 + /* | __FMODE_NONOTIFY */);
36971 + if (IS_ERR(file)) {
36972 + pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36976 + delegated = NULL;
36977 + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36978 + au_xino_unlock_dir(&ldir);
36980 + if (unlikely(err == -EWOULDBLOCK)) {
36981 + pr_warn("cannot retry for NFSv4 delegation"
36982 + " for an internal unlink\n");
36985 + if (unlikely(err)) {
36986 + pr_err("%pd unlink err %d\n", dentry, err);
36991 + /* no one can touch copy_src xino */
36992 + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36993 + if (unlikely(err)) {
36994 + pr_err("%pd copy err %d\n", dentry, err);
36998 + goto out_dput; /* success */
37002 + file = ERR_PTR(err);
37004 + dput(path.dentry);
37007 + au_xino_unlock_dir(&ldir);
37011 +struct file *au_xino_file1(struct au_xino *xi)
37013 + struct file *file;
37014 + unsigned int u, nfile;
37017 + nfile = xi->xi_nfile;
37018 + for (u = 0; u < nfile; u++) {
37019 + file = xi->xi_file[u];
37027 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37038 + if (idx < xi->xi_nfile) {
37039 + f = xi->xi_file[idx];
37043 + p = au_kzrealloc(xi->xi_file,
37044 + sizeof(*xi->xi_file) * xi->xi_nfile,
37045 + sizeof(*xi->xi_file) * (idx + 1),
37046 + GFP_NOFS, /*may_shrink*/0);
37048 + MtxMustLock(&xi->xi_mtx);
37050 + xi->xi_nfile = idx + 1;
37058 + xi->xi_file[idx] = file;
37065 + * if @xinew->xi is not set, then create new xigen file.
37067 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37069 + struct file *file;
37072 + SiMustAnyLock(sb);
37074 + file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37075 + if (IS_ERR(file)) {
37076 + err = PTR_ERR(file);
37077 + pr_err("%s[%d], err %d\n",
37078 + xinew->xi ? "xino" : "xigen",
37079 + xinew->idx, err);
37084 + err = au_xino_file_set(xinew->xi, xinew->idx, file);
37087 + /* todo: make xigen file an array */
37088 + /* err = au_xigen_file_set(sb, xinew->idx, file); */
37091 + if (unlikely(err))
37092 + file = ERR_PTR(err);
37098 +/* ---------------------------------------------------------------------- */
37101 + * truncate xino files
37103 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37104 + int idx, struct kstatfs *st)
37108 + struct file *file, *new_xino;
37109 + struct au_xi_new xinew = {
37114 + xinew.xi = au_sbr(sb, bindex)->br_xino;
37115 + file = au_xino_file(xinew.xi, idx);
37119 + xinew.base = &file->f_path;
37120 + err = vfs_statfs(xinew.base, st);
37121 + if (unlikely(err)) {
37122 + AuErr1("statfs err %d, ignored\n", err);
37127 + blocks = file_inode(file)->i_blocks;
37128 + pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37129 + bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37131 + xinew.copy_src = file;
37132 + new_xino = au_xi_new(sb, &xinew);
37133 + if (IS_ERR(new_xino)) {
37134 + err = PTR_ERR(new_xino);
37135 + pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37139 + err = vfs_statfs(&new_xino->f_path, st);
37141 + pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37142 + bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37143 + st->f_bfree, st->f_blocks);
37145 + AuErr1("statfs err %d, ignored\n", err);
37153 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37156 + unsigned long jiffy;
37157 + aufs_bindex_t bbot;
37158 + struct kstatfs *st;
37159 + struct au_branch *br;
37160 + struct au_xino *xi;
37163 + st = kmalloc(sizeof(*st), GFP_NOFS);
37164 + if (unlikely(!st))
37168 + bbot = au_sbbot(sb);
37169 + if (unlikely(bindex < 0 || bbot < bindex))
37174 + br = au_sbr(sb, bindex);
37175 + xi = br->br_xino;
37176 + for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37177 + err = au_xino_do_trunc(sb, bindex, i, st);
37179 + au_sbi(sb)->si_xino_jiffy = jiffy;
37182 + au_kfree_rcu(st);
37187 +struct xino_do_trunc_args {
37188 + struct super_block *sb;
37189 + struct au_branch *br;
37193 +static void xino_do_trunc(void *_args)
37195 + struct xino_do_trunc_args *args = _args;
37196 + struct super_block *sb;
37197 + struct au_branch *br;
37198 + struct inode *dir;
37200 + aufs_bindex_t bindex;
37204 + dir = d_inode(sb->s_root);
37208 + si_noflush_write_lock(sb);
37209 + ii_read_lock_parent(dir);
37210 + bindex = au_br_index(sb, br->br_id);
37211 + err = au_xino_trunc(sb, bindex, idx);
37212 + ii_read_unlock(dir);
37213 + if (unlikely(err))
37214 + pr_warn("err b%d, (%d)\n", bindex, err);
37215 + atomic_dec(&br->br_xino->xi_truncating);
37216 + au_lcnt_dec(&br->br_count);
37217 + si_write_unlock(sb);
37218 + au_nwt_done(&au_sbi(sb)->si_nowait);
37219 + au_kfree_rcu(args);
37223 + * returns the index in the xi_file array whose corresponding file is necessary
37224 + * to truncate, or -1 which means no need to truncate.
37226 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37230 + struct kstatfs st;
37231 + struct au_sbinfo *sbinfo;
37232 + struct au_xino *xi;
37233 + struct file *file;
37235 + /* todo: si_xino_expire and the ratio should be customizable */
37236 + sbinfo = au_sbi(sb);
37237 + if (time_before(jiffies,
37238 + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37241 + /* truncation border */
37242 + xi = br->br_xino;
37243 + for (u = 0; u < xi->xi_nfile; u++) {
37244 + file = au_xino_file(xi, u);
37248 + err = vfs_statfs(&file->f_path, &st);
37249 + if (unlikely(err)) {
37250 + AuErr1("statfs err %d, ignored\n", err);
37253 + if (div64_u64(st.f_bfree * 100, st.f_blocks)
37254 + >= AUFS_XINO_DEF_TRUNC)
37261 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37264 + struct xino_do_trunc_args *args;
37267 + idx = xino_trunc_test(sb, br);
37271 + if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37274 + /* lock and kfree() will be called in trunc_xino() */
37275 + args = kmalloc(sizeof(*args), GFP_NOFS);
37276 + if (unlikely(!args)) {
37277 + AuErr1("no memory\n");
37281 + au_lcnt_inc(&br->br_count);
37285 + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37287 + return; /* success */
37289 + pr_err("wkq %d\n", wkq_err);
37290 + au_lcnt_dec(&br->br_count);
37291 + au_kfree_rcu(args);
37294 + atomic_dec(&br->br_xino->xi_truncating);
37297 +/* ---------------------------------------------------------------------- */
37299 +struct au_xi_calc {
37304 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37305 + struct au_xi_calc *calc)
37309 + maxent = au_xi_maxent(sb);
37310 + calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37311 + calc->pos *= sizeof(ino_t);
37314 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37315 + struct au_xi_calc *calc)
37318 + struct file *file;
37319 + struct au_xino *xi = br->br_xino;
37320 + struct au_xi_new xinew = {
37324 + SiMustAnyLock(sb);
37330 + mutex_lock(&xi->xi_mtx);
37331 + file = au_xino_file(xi, calc->idx);
37335 + file = au_xino_file(xi, /*idx*/-1);
37336 + AuDebugOn(!file);
37337 + xinew.idx = calc->idx;
37338 + xinew.base = &file->f_path;
37339 + /* xinew.copy_src = NULL; */
37340 + file = au_xi_new(sb, &xinew);
37341 + if (IS_ERR(file))
37342 + err = PTR_ERR(file);
37345 + mutex_unlock(&xi->xi_mtx);
37350 +struct au_xino_do_new_async_args {
37351 + struct super_block *sb;
37352 + struct au_branch *br;
37353 + struct au_xi_calc calc;
37357 +struct au_xi_writing {
37358 + struct hlist_bl_node node;
37359 + ino_t h_ino, ino;
37362 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37365 +static void au_xino_call_do_new_async(void *args)
37367 + struct au_xino_do_new_async_args *a = args;
37368 + struct au_branch *br;
37369 + struct super_block *sb;
37370 + struct au_sbinfo *sbi;
37371 + struct inode *root;
37372 + struct file *file;
37373 + struct au_xi_writing *del, *p;
37374 + struct hlist_bl_head *hbl;
37375 + struct hlist_bl_node *pos;
37380 + sbi = au_sbi(sb);
37381 + si_noflush_read_lock(sb);
37382 + root = d_inode(sb->s_root);
37383 + ii_read_lock_child(root);
37384 + err = au_xino_do_new_async(sb, br, &a->calc);
37385 + if (unlikely(err)) {
37386 + AuIOErr("err %d\n", err);
37390 + file = au_xino_file(br->br_xino, a->calc.idx);
37391 + AuDebugOn(!file);
37392 + err = au_xino_do_write(file, &a->calc, a->ino);
37393 + if (unlikely(err)) {
37394 + AuIOErr("err %d\n", err);
37399 + hbl = &br->br_xino->xi_writing;
37400 + hlist_bl_lock(hbl);
37401 + au_hbl_for_each(pos, hbl) {
37402 + p = container_of(pos, struct au_xi_writing, node);
37403 + if (p->ino == a->ino) {
37405 + hlist_bl_del(&p->node);
37409 + hlist_bl_unlock(hbl);
37410 + au_kfree_rcu(del);
37413 + au_lcnt_dec(&br->br_count);
37414 + ii_read_unlock(root);
37415 + si_read_unlock(sb);
37416 + au_nwt_done(&sbi->si_nowait);
37421 + * create a new xino file asynchronously
37423 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37424 + struct au_xi_calc *calc, ino_t ino)
37427 + struct au_xino_do_new_async_args *arg;
37430 + arg = kmalloc(sizeof(*arg), GFP_NOFS);
37431 + if (unlikely(!arg))
37436 + arg->calc = *calc;
37438 + au_lcnt_inc(&br->br_count);
37439 + err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37440 + if (unlikely(err)) {
37441 + pr_err("wkq %d\n", err);
37442 + au_lcnt_dec(&br->br_count);
37443 + au_kfree_rcu(arg);
37451 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37452 + * at the position of @h_ino.
37454 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37459 + struct au_xi_calc calc;
37460 + struct au_sbinfo *sbinfo;
37461 + struct file *file;
37462 + struct au_xino *xi;
37463 + struct hlist_bl_head *hbl;
37464 + struct hlist_bl_node *pos;
37465 + struct au_xi_writing *p;
37468 + if (!au_opt_test(au_mntflags(sb), XINO))
37469 + return 0; /* no xino */
37472 + au_xi_calc(sb, h_ino, &calc);
37473 + xi = au_sbr(sb, bindex)->br_xino;
37474 + file = au_xino_file(xi, calc.idx);
37476 + hbl = &xi->xi_writing;
37477 + hlist_bl_lock(hbl);
37478 + au_hbl_for_each(pos, hbl) {
37479 + p = container_of(pos, struct au_xi_writing, node);
37480 + if (p->h_ino == h_ino) {
37481 + AuDbg("hi%llu, i%llu, found\n",
37482 + (u64)p->h_ino, (u64)p->ino);
37487 + hlist_bl_unlock(hbl);
37489 + } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37490 + return 0; /* no xino */
37492 + sbinfo = au_sbi(sb);
37493 + sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37494 + if (sz == sizeof(*ino))
37495 + return 0; /* success */
37498 + if (unlikely(sz >= 0)) {
37500 + AuIOErr("xino read error (%zd)\n", sz);
37505 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37510 + sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37511 + if (sz == sizeof(ino))
37512 + return 0; /* success */
37514 + AuIOErr("write failed (%zd)\n", sz);
37519 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37520 + * at the position of @h_ino.
37521 + * even if @ino is zero, it is written to the xinofile and means no entry.
37522 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37523 + * try truncating it.
37525 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37529 + unsigned int mnt_flags;
37530 + struct au_xi_calc calc;
37531 + struct file *file;
37532 + struct au_branch *br;
37533 + struct au_xino *xi;
37534 + struct au_xi_writing *p;
37536 + SiMustAnyLock(sb);
37538 + mnt_flags = au_mntflags(sb);
37539 + if (!au_opt_test(mnt_flags, XINO))
37542 + au_xi_calc(sb, h_ino, &calc);
37543 + br = au_sbr(sb, bindex);
37544 + xi = br->br_xino;
37545 + file = au_xino_file(xi, calc.idx);
37547 + /* store the inum pair into the list */
37548 + p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37549 + p->h_ino = h_ino;
37551 + au_hbl_add(&p->node, &xi->xi_writing);
37553 + /* create and write a new xino file asynchronously */
37554 + err = au_xino_new_async(sb, br, &calc, ino);
37556 + return 0; /* success */
37560 + err = au_xino_do_write(file, &calc, ino);
37562 + br = au_sbr(sb, bindex);
37563 + if (au_opt_test(mnt_flags, TRUNC_XINO)
37564 + && au_test_fs_trunc_xino(au_br_sb(br)))
37565 + xino_try_trunc(sb, br);
37566 + return 0; /* success */
37570 + AuIOErr("write failed (%d)\n", err);
37574 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37577 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37578 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37582 + const int prevent_endless = 10;
37586 + err = vfsub_read_k(file, kbuf, size, pos);
37587 + if (err == -EINTR
37588 + && !au_wkq_test()
37589 + && fatal_signal_pending(current)) {
37590 + err = xino_fread_wkq(file, kbuf, size, pos);
37591 + BUG_ON(err == -EINTR);
37593 + } while (i++ < prevent_endless
37594 + && (err == -EAGAIN || err == -EINTR));
37596 +#if 0 /* reserved for future use */
37598 + fsnotify_access(file->f_path.dentry);
37604 +struct xino_fread_args {
37606 + struct file *file;
37612 +static void call_xino_fread(void *args)
37614 + struct xino_fread_args *a = args;
37615 + *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37618 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37623 + struct xino_fread_args args = {
37631 + wkq_err = au_wkq_wait(call_xino_fread, &args);
37632 + if (unlikely(wkq_err))
37638 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37641 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37646 + const int prevent_endless = 10;
37650 + err = vfsub_write_k(file, kbuf, size, pos);
37651 + if (err == -EINTR
37652 + && !au_wkq_test()
37653 + && fatal_signal_pending(current)) {
37654 + err = xino_fwrite_wkq(file, kbuf, size, pos);
37655 + BUG_ON(err == -EINTR);
37657 + } while (i++ < prevent_endless
37658 + && (err == -EAGAIN || err == -EINTR));
37660 +#if 0 /* reserved for future use */
37662 + fsnotify_modify(file->f_path.dentry);
37668 +struct do_xino_fwrite_args {
37670 + struct file *file;
37676 +static void call_do_xino_fwrite(void *args)
37678 + struct do_xino_fwrite_args *a = args;
37679 + *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37682 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37687 + struct do_xino_fwrite_args args = {
37696 + * it breaks RLIMIT_FSIZE and normal user's limit,
37697 + * users should care about quota and real 'filesystem full.'
37699 + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37700 + if (unlikely(wkq_err))
37706 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37710 + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37712 + err = do_xino_fwrite(file, buf, size, pos);
37716 + err = xino_fwrite_wkq(file, buf, size, pos);
37723 +/* ---------------------------------------------------------------------- */
37726 + * inode number bitmap
37728 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37729 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37733 + AuDebugOn(bit < 0 || page_bits <= bit);
37734 + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37738 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37740 + AuDebugOn(ino < AUFS_FIRST_INO);
37741 + ino -= AUFS_FIRST_INO;
37742 + *pindex = ino / page_bits;
37743 + *bit = ino % page_bits;
37746 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37751 + struct au_sbinfo *sbinfo;
37752 + struct file *xib;
37753 + unsigned long *p;
37755 + sbinfo = au_sbi(sb);
37756 + MtxMustLock(&sbinfo->si_xib_mtx);
37757 + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37758 + || !au_opt_test(sbinfo->si_mntflags, XINO));
37760 + if (pindex == sbinfo->si_xib_last_pindex)
37763 + xib = sbinfo->si_xib;
37764 + p = sbinfo->si_xib_buf;
37765 + pos = sbinfo->si_xib_last_pindex;
37766 + pos *= PAGE_SIZE;
37767 + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37768 + if (unlikely(sz != PAGE_SIZE))
37772 + pos *= PAGE_SIZE;
37773 + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37774 + sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37776 + memset(p, 0, PAGE_SIZE);
37777 + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37779 + if (sz == PAGE_SIZE) {
37780 + sbinfo->si_xib_last_pindex = pindex;
37781 + return 0; /* success */
37785 + AuIOErr1("write failed (%zd)\n", sz);
37792 +static void au_xib_clear_bit(struct inode *inode)
37795 + unsigned long pindex;
37796 + struct super_block *sb;
37797 + struct au_sbinfo *sbinfo;
37799 + AuDebugOn(inode->i_nlink);
37801 + sb = inode->i_sb;
37802 + xib_calc_bit(inode->i_ino, &pindex, &bit);
37803 + AuDebugOn(page_bits <= bit);
37804 + sbinfo = au_sbi(sb);
37805 + mutex_lock(&sbinfo->si_xib_mtx);
37806 + err = xib_pindex(sb, pindex);
37808 + clear_bit(bit, sbinfo->si_xib_buf);
37809 + sbinfo->si_xib_next_bit = bit;
37811 + mutex_unlock(&sbinfo->si_xib_mtx);
37814 +/* ---------------------------------------------------------------------- */
37817 + * truncate a xino bitmap file
37821 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37825 + unsigned long pindex;
37826 + loff_t pos, pend;
37827 + struct au_sbinfo *sbinfo;
37829 + unsigned long *p;
37832 + sbinfo = au_sbi(sb);
37833 + MtxMustLock(&sbinfo->si_xib_mtx);
37834 + p = sbinfo->si_xib_buf;
37835 + pend = vfsub_f_size_read(file);
37837 + while (pos < pend) {
37838 + sz = xino_fread(file, page, PAGE_SIZE, &pos);
37840 + if (unlikely(sz <= 0))
37844 + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37845 + if (unlikely(*ino < AUFS_FIRST_INO))
37848 + xib_calc_bit(*ino, &pindex, &bit);
37849 + AuDebugOn(page_bits <= bit);
37850 + err = xib_pindex(sb, pindex);
37862 +static int xib_restore(struct super_block *sb)
37865 + unsigned int nfile;
37866 + aufs_bindex_t bindex, bbot;
37868 + struct au_branch *br;
37869 + struct au_xino *xi;
37870 + struct file *file;
37873 + page = (void *)__get_free_page(GFP_NOFS);
37874 + if (unlikely(!page))
37878 + bbot = au_sbbot(sb);
37879 + for (bindex = 0; !err && bindex <= bbot; bindex++)
37880 + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37881 + br = au_sbr(sb, bindex);
37882 + xi = br->br_xino;
37883 + nfile = xi->xi_nfile;
37884 + for (i = 0; i < nfile; i++) {
37885 + file = au_xino_file(xi, i);
37887 + err = do_xib_restore(sb, file, page);
37890 + AuDbg("skip shared b%d\n", bindex);
37891 + free_page((unsigned long)page);
37897 +int au_xib_trunc(struct super_block *sb)
37902 + struct au_sbinfo *sbinfo;
37903 + unsigned long *p;
37904 + struct file *file;
37906 + SiMustWriteLock(sb);
37909 + sbinfo = au_sbi(sb);
37910 + if (!au_opt_test(sbinfo->si_mntflags, XINO))
37913 + file = sbinfo->si_xib;
37914 + if (vfsub_f_size_read(file) <= PAGE_SIZE)
37917 + file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37918 + err = PTR_ERR(file);
37919 + if (IS_ERR(file))
37921 + fput(sbinfo->si_xib);
37922 + sbinfo->si_xib = file;
37924 + p = sbinfo->si_xib_buf;
37925 + memset(p, 0, PAGE_SIZE);
37927 + sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
37928 + if (unlikely(sz != PAGE_SIZE)) {
37930 + AuIOErr("err %d\n", err);
37936 + mutex_lock(&sbinfo->si_xib_mtx);
37937 + /* mnt_want_write() is unnecessary here */
37938 + err = xib_restore(sb);
37939 + mutex_unlock(&sbinfo->si_xib_mtx);
37945 +/* ---------------------------------------------------------------------- */
37947 +struct au_xino *au_xino_alloc(unsigned int nfile)
37949 + struct au_xino *xi;
37951 + xi = kzalloc(sizeof(*xi), GFP_NOFS);
37952 + if (unlikely(!xi))
37954 + xi->xi_nfile = nfile;
37955 + xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
37956 + if (unlikely(!xi->xi_file))
37959 + xi->xi_nondir.total = 8; /* initial size */
37960 + xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37962 + if (unlikely(!xi->xi_nondir.array))
37965 + spin_lock_init(&xi->xi_nondir.spin);
37966 + init_waitqueue_head(&xi->xi_nondir.wqh);
37967 + mutex_init(&xi->xi_mtx);
37968 + INIT_HLIST_BL_HEAD(&xi->xi_writing);
37969 + atomic_set(&xi->xi_truncating, 0);
37970 + kref_init(&xi->xi_kref);
37971 + goto out; /* success */
37974 + au_kfree_try_rcu(xi->xi_file);
37976 + au_kfree_rcu(xi);
37982 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
37985 + struct au_xino *xi;
37988 + xi = au_xino_alloc(idx + 1);
37989 + if (unlikely(!xi)) {
37996 + xi->xi_file[idx] = file;
37997 + AuDebugOn(br->br_xino);
37998 + br->br_xino = xi;
38004 +static void au_xino_release(struct kref *kref)
38006 + struct au_xino *xi;
38008 + unsigned long ul;
38009 + struct hlist_bl_head *hbl;
38010 + struct hlist_bl_node *pos, *n;
38011 + struct au_xi_writing *p;
38013 + xi = container_of(kref, struct au_xino, xi_kref);
38014 + for (i = 0; i < xi->xi_nfile; i++)
38015 + if (xi->xi_file[i])
38016 + fput(xi->xi_file[i]);
38017 + for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38018 + AuDebugOn(xi->xi_nondir.array[i]);
38019 + mutex_destroy(&xi->xi_mtx);
38020 + hbl = &xi->xi_writing;
38021 + ul = au_hbl_count(hbl);
38022 + if (unlikely(ul)) {
38023 + pr_warn("xi_writing %lu\n", ul);
38024 + hlist_bl_lock(hbl);
38025 + hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38026 + hlist_bl_del(&p->node);
38027 + /* kmemleak reported au_kfree_rcu() doesn't free it */
38030 + hlist_bl_unlock(hbl);
38032 + au_kfree_try_rcu(xi->xi_file);
38033 + au_kfree_try_rcu(xi->xi_nondir.array);
38034 + au_kfree_rcu(xi);
38037 +int au_xino_put(struct au_branch *br)
38040 + struct au_xino *xi;
38043 + xi = br->br_xino;
38045 + br->br_xino = NULL;
38046 + ret = kref_put(&xi->xi_kref, au_xino_release);
38052 +/* ---------------------------------------------------------------------- */
38055 + * xino mount option handlers
38059 +static void xino_clear_xib(struct super_block *sb)
38061 + struct au_sbinfo *sbinfo;
38063 + SiMustWriteLock(sb);
38065 + sbinfo = au_sbi(sb);
38066 + if (sbinfo->si_xib)
38067 + fput(sbinfo->si_xib);
38068 + sbinfo->si_xib = NULL;
38069 + if (sbinfo->si_xib_buf)
38070 + free_page((unsigned long)sbinfo->si_xib_buf);
38071 + sbinfo->si_xib_buf = NULL;
38074 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38078 + struct au_sbinfo *sbinfo;
38079 + struct file *file;
38080 + struct super_block *xi_sb;
38082 + SiMustWriteLock(sb);
38084 + sbinfo = au_sbi(sb);
38085 + file = au_xino_create2(sb, path, sbinfo->si_xib);
38086 + err = PTR_ERR(file);
38087 + if (IS_ERR(file))
38089 + if (sbinfo->si_xib)
38090 + fput(sbinfo->si_xib);
38091 + sbinfo->si_xib = file;
38092 + xi_sb = file_inode(file)->i_sb;
38093 + sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38094 + if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38096 + pr_err("s_maxbytes(%llu) on %s is too small\n",
38097 + (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38100 + sbinfo->si_ximaxent /= sizeof(ino_t);
38103 + if (!sbinfo->si_xib_buf)
38104 + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38105 + if (unlikely(!sbinfo->si_xib_buf))
38108 + sbinfo->si_xib_last_pindex = 0;
38109 + sbinfo->si_xib_next_bit = 0;
38110 + if (vfsub_f_size_read(file) < PAGE_SIZE) {
38112 + err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38113 + if (unlikely(err != PAGE_SIZE))
38117 + goto out; /* success */
38120 + if (sbinfo->si_xib_buf)
38121 + free_page((unsigned long)sbinfo->si_xib_buf);
38122 + sbinfo->si_xib_buf = NULL;
38126 + fput(sbinfo->si_xib);
38127 + sbinfo->si_xib = NULL;
38133 +/* xino for each branch */
38134 +static void xino_clear_br(struct super_block *sb)
38136 + aufs_bindex_t bindex, bbot;
38137 + struct au_branch *br;
38139 + bbot = au_sbbot(sb);
38140 + for (bindex = 0; bindex <= bbot; bindex++) {
38141 + br = au_sbr(sb, bindex);
38147 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38148 + aufs_bindex_t bshared)
38150 + struct au_branch *brshared;
38152 + brshared = au_sbr(sb, bshared);
38153 + AuDebugOn(!brshared->br_xino);
38154 + AuDebugOn(!brshared->br_xino->xi_file);
38155 + if (br->br_xino != brshared->br_xino) {
38156 + au_xino_get(brshared);
38158 + br->br_xino = brshared->br_xino;
38162 +struct au_xino_do_set_br {
38163 + struct au_branch *br;
38165 + aufs_bindex_t bshared;
38168 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38169 + struct au_xino_do_set_br *args)
38172 + struct au_xi_calc calc;
38173 + struct file *file;
38174 + struct au_branch *br;
38175 + struct au_xi_new xinew = {
38180 + xinew.xi = br->br_xino;
38181 + au_xi_calc(sb, args->h_ino, &calc);
38182 + xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38183 + if (args->bshared >= 0)
38184 + /* shared xino */
38185 + au_xino_set_br_shared(sb, br, args->bshared);
38186 + else if (!xinew.xi) {
38188 + err = au_xino_init(br, calc.idx, xinew.copy_src);
38189 + if (unlikely(err))
38193 + /* force re-creating */
38194 + xinew.xi = br->br_xino;
38195 + xinew.idx = calc.idx;
38196 + mutex_lock(&xinew.xi->xi_mtx);
38197 + file = au_xi_new(sb, &xinew);
38198 + mutex_unlock(&xinew.xi->xi_mtx);
38199 + err = PTR_ERR(file);
38200 + if (IS_ERR(file))
38202 + AuDebugOn(!file);
38204 + err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38205 + if (unlikely(err))
38213 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38216 + aufs_bindex_t bindex, bbot;
38217 + struct au_xino_do_set_br args;
38218 + struct inode *inode;
38220 + SiMustWriteLock(sb);
38222 + bbot = au_sbbot(sb);
38223 + inode = d_inode(sb->s_root);
38224 + for (bindex = 0; bindex <= bbot; bindex++) {
38225 + args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38226 + args.br = au_sbr(sb, bindex);
38227 + args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38228 + err = au_xino_do_set_br(sb, path, &args);
38229 + if (unlikely(err))
38237 +void au_xino_clr(struct super_block *sb)
38239 + struct au_sbinfo *sbinfo;
38241 + au_xigen_clr(sb);
38242 + xino_clear_xib(sb);
38243 + xino_clear_br(sb);
38244 + dbgaufs_brs_del(sb, 0);
38245 + sbinfo = au_sbi(sb);
38246 + /* lvalue, do not call au_mntflags() */
38247 + au_opt_clr(sbinfo->si_mntflags, XINO);
38250 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38253 + struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38254 + struct qstr *dname, *cur_name;
38255 + struct file *cur_xino;
38256 + struct au_sbinfo *sbinfo;
38257 + struct path *path, *cur_path;
38259 + SiMustWriteLock(sb);
38262 + sbinfo = au_sbi(sb);
38263 + path = &xiopt->file->f_path;
38264 + dentry = path->dentry;
38265 + parent = dget_parent(dentry);
38268 + cur_xino = sbinfo->si_xib;
38270 + cur_path = &cur_xino->f_path;
38271 + cur_dentry = cur_path->dentry;
38272 + cur_parent = dget_parent(cur_dentry);
38273 + cur_name = &cur_dentry->d_name;
38274 + dname = &dentry->d_name;
38275 + skip = (cur_parent == parent
38276 + && au_qstreq(dname, cur_name));
38277 + dput(cur_parent);
38283 + au_opt_set(sbinfo->si_mntflags, XINO);
38284 + err = au_xino_set_xib(sb, path);
38285 + /* si_x{read,write} are set */
38287 + err = au_xigen_set(sb, path);
38289 + err = au_xino_set_br(sb, path);
38291 + dbgaufs_brs_add(sb, 0, /*topdown*/1);
38292 + goto out; /* success */
38296 + AuIOErr("failed setting xino(%d).\n", err);
38305 + * create a xinofile at the default place/path.
38307 +struct file *au_xino_def(struct super_block *sb)
38309 + struct file *file;
38311 + struct au_branch *br;
38312 + struct super_block *h_sb;
38313 + struct path path;
38314 + aufs_bindex_t bbot, bindex, bwr;
38317 + bbot = au_sbbot(sb);
38319 + for (bindex = 0; bindex <= bbot; bindex++) {
38320 + br = au_sbr(sb, bindex);
38321 + if (au_br_writable(br->br_perm)
38322 + && !au_test_fs_bad_xino(au_br_sb(br))) {
38329 + file = ERR_PTR(-ENOMEM);
38330 + page = (void *)__get_free_page(GFP_NOFS);
38331 + if (unlikely(!page))
38333 + path.mnt = au_br_mnt(br);
38334 + path.dentry = au_h_dptr(sb->s_root, bwr);
38335 + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38336 + file = (void *)p;
38337 + if (!IS_ERR(p)) {
38338 + strcat(p, "/" AUFS_XINO_FNAME);
38339 + AuDbg("%s\n", p);
38340 + file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38342 + free_page((unsigned long)page);
38344 + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38346 + if (IS_ERR(file))
38348 + h_sb = file->f_path.dentry->d_sb;
38349 + if (unlikely(au_test_fs_bad_xino(h_sb))) {
38350 + pr_err("xino doesn't support %s(%s)\n",
38351 + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38353 + file = ERR_PTR(-EINVAL);
38361 +/* ---------------------------------------------------------------------- */
38364 + * initialize the xinofile for the specified branch @br
38365 + * at the place/path where @base_file indicates.
38366 + * test whether another branch is on the same filesystem or not,
38367 + * if found then share the xinofile with another branch.
38369 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38370 + struct path *base)
38373 + struct au_xino_do_set_br args = {
38378 + args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38380 + err = au_xino_do_set_br(sb, base, &args);
38381 + if (unlikely(err))
38387 +/* ---------------------------------------------------------------------- */
38390 + * get an unused inode number from bitmap
38392 +ino_t au_xino_new_ino(struct super_block *sb)
38395 + unsigned long *p, pindex, ul, pend;
38396 + struct au_sbinfo *sbinfo;
38397 + struct file *file;
38398 + int free_bit, err;
38400 + if (!au_opt_test(au_mntflags(sb), XINO))
38401 + return iunique(sb, AUFS_FIRST_INO);
38403 + sbinfo = au_sbi(sb);
38404 + mutex_lock(&sbinfo->si_xib_mtx);
38405 + p = sbinfo->si_xib_buf;
38406 + free_bit = sbinfo->si_xib_next_bit;
38407 + if (free_bit < page_bits && !test_bit(free_bit, p))
38408 + goto out; /* success */
38409 + free_bit = find_first_zero_bit(p, page_bits);
38410 + if (free_bit < page_bits)
38411 + goto out; /* success */
38413 + pindex = sbinfo->si_xib_last_pindex;
38414 + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38415 + err = xib_pindex(sb, ul);
38416 + if (unlikely(err))
38418 + free_bit = find_first_zero_bit(p, page_bits);
38419 + if (free_bit < page_bits)
38420 + goto out; /* success */
38423 + file = sbinfo->si_xib;
38424 + pend = vfsub_f_size_read(file) / PAGE_SIZE;
38425 + for (ul = pindex + 1; ul <= pend; ul++) {
38426 + err = xib_pindex(sb, ul);
38427 + if (unlikely(err))
38429 + free_bit = find_first_zero_bit(p, page_bits);
38430 + if (free_bit < page_bits)
38431 + goto out; /* success */
38436 + set_bit(free_bit, p);
38437 + sbinfo->si_xib_next_bit = free_bit + 1;
38438 + pindex = sbinfo->si_xib_last_pindex;
38439 + mutex_unlock(&sbinfo->si_xib_mtx);
38440 + ino = xib_calc_ino(pindex, free_bit);
38441 + AuDbg("i%lu\n", (unsigned long)ino);
38444 + mutex_unlock(&sbinfo->si_xib_mtx);
38449 +/* for s_op->delete_inode() */
38450 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38453 + unsigned int mnt_flags;
38454 + aufs_bindex_t bindex, bbot, bi;
38455 + unsigned char try_trunc;
38456 + struct au_iinfo *iinfo;
38457 + struct super_block *sb;
38458 + struct au_hinode *hi;
38459 + struct inode *h_inode;
38460 + struct au_branch *br;
38461 + struct au_xi_calc calc;
38462 + struct file *file;
38464 + AuDebugOn(au_is_bad_inode(inode));
38466 + sb = inode->i_sb;
38467 + mnt_flags = au_mntflags(sb);
38468 + if (!au_opt_test(mnt_flags, XINO)
38469 + || inode->i_ino == AUFS_ROOT_INO)
38473 + au_xigen_inc(inode);
38474 + au_xib_clear_bit(inode);
38477 + iinfo = au_ii(inode);
38478 + bindex = iinfo->ii_btop;
38482 + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38483 + hi = au_hinode(iinfo, bindex);
38484 + bbot = iinfo->ii_bbot;
38485 + for (; bindex <= bbot; bindex++, hi++) {
38486 + h_inode = hi->hi_inode;
38488 + || (!unlinked && h_inode->i_nlink))
38491 + /* inode may not be revalidated */
38492 + bi = au_br_index(sb, hi->hi_id);
38496 + br = au_sbr(sb, bi);
38497 + au_xi_calc(sb, h_inode->i_ino, &calc);
38498 + file = au_xino_file(br->br_xino, calc.idx);
38499 + if (IS_ERR_OR_NULL(file))
38502 + err = au_xino_do_write(file, &calc, /*ino*/0);
38503 + if (!err && try_trunc
38504 + && au_test_fs_trunc_xino(au_br_sb(br)))
38505 + xino_try_trunc(sb, br);
38509 +/* ---------------------------------------------------------------------- */
38511 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38513 + int found, total, i;
38516 + total = xi->xi_nondir.total;
38517 + for (i = 0; i < total; i++) {
38518 + if (xi->xi_nondir.array[i] != h_ino)
38527 +static int au_xinondir_expand(struct au_xino *xi)
38532 + BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38535 + sz = xi->xi_nondir.total * sizeof(ino_t);
38536 + if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38538 + p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38539 + /*may_shrink*/0);
38541 + xi->xi_nondir.array = p;
38542 + xi->xi_nondir.total <<= 1;
38543 + AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38551 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38552 + ino_t h_ino, int idx)
38554 + struct au_xino *xi;
38556 + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38557 + xi = au_sbr(sb, bindex)->br_xino;
38558 + AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38560 + spin_lock(&xi->xi_nondir.spin);
38561 + AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38562 + xi->xi_nondir.array[idx] = 0;
38563 + spin_unlock(&xi->xi_nondir.spin);
38564 + wake_up_all(&xi->xi_nondir.wqh);
38567 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38570 + int err, found, empty;
38571 + struct au_xino *xi;
38575 + if (!au_opt_test(au_mntflags(sb), XINO))
38576 + goto out; /* no xino */
38578 + xi = au_sbr(sb, bindex)->br_xino;
38581 + spin_lock(&xi->xi_nondir.spin);
38582 + found = au_xinondir_find(xi, h_ino);
38583 + if (found == -1) {
38584 + empty = au_xinondir_find(xi, /*h_ino*/0);
38585 + if (empty == -1) {
38586 + empty = xi->xi_nondir.total;
38587 + err = au_xinondir_expand(xi);
38588 + if (unlikely(err))
38591 + xi->xi_nondir.array[empty] = h_ino;
38594 + spin_unlock(&xi->xi_nondir.spin);
38595 + wait_event(xi->xi_nondir.wqh,
38596 + xi->xi_nondir.array[found] != h_ino);
38601 + spin_unlock(&xi->xi_nondir.spin);
38606 +/* ---------------------------------------------------------------------- */
38608 +int au_xino_path(struct seq_file *seq, struct file *file)
38612 + err = au_seq_path(seq, &file->f_path);
38613 + if (unlikely(err))
38616 +#define Deleted "\\040(deleted)"
38617 + seq->count -= sizeof(Deleted) - 1;
38618 + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38619 + sizeof(Deleted) - 1));
38625 diff --git a/fs/dcache.c b/fs/dcache.c
38626 index c82ae731df9af7..411c9acbdca754 100644
38629 @@ -1345,7 +1345,7 @@ enum d_walk_ret {
38631 * The @enter() callbacks are called with d_lock held.
38633 -static void d_walk(struct dentry *parent, void *data,
38634 +void d_walk(struct dentry *parent, void *data,
38635 enum d_walk_ret (*enter)(void *, struct dentry *))
38637 struct dentry *this_parent;
38638 @@ -1450,6 +1450,7 @@ static void d_walk(struct dentry *parent, void *data,
38642 +EXPORT_SYMBOL_GPL(d_walk);
38644 struct check_mount {
38645 struct vfsmount *mnt;
38646 @@ -3051,6 +3052,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
38648 write_sequnlock(&rename_lock);
38650 +EXPORT_SYMBOL_GPL(d_exchange);
38653 * d_ancestor - search for an ancestor
38654 diff --git a/fs/exec.c b/fs/exec.c
38655 index 4aa19b24f2810a..013bc5896529ca 100644
38658 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
38659 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
38660 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
38662 +EXPORT_SYMBOL_GPL(path_noexec);
38664 #ifdef CONFIG_USELIB
38666 diff --git a/fs/fcntl.c b/fs/fcntl.c
38667 index c80a6acad742fb..e21a0fcff53801 100644
38672 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
38674 -static int setfl(int fd, struct file * filp, unsigned int arg)
38675 +int setfl(int fd, struct file * filp, unsigned int arg)
38677 struct inode * inode = file_inode(filp);
38679 @@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
38681 if (filp->f_op->check_flags)
38682 error = filp->f_op->check_flags(arg);
38683 + if (!error && filp->f_op->setfl)
38684 + error = filp->f_op->setfl(filp, arg);
38688 @@ -85,6 +87,7 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
38692 +EXPORT_SYMBOL_GPL(setfl);
38694 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
38696 diff --git a/fs/file_table.c b/fs/file_table.c
38697 index de4a2915bfd494..475f3f9c787607 100644
38698 --- a/fs/file_table.c
38699 +++ b/fs/file_table.c
38700 @@ -235,6 +235,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
38702 return ERR_PTR(-ENFILE);
38704 +EXPORT_SYMBOL_GPL(alloc_empty_file);
38707 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
38708 diff --git a/fs/namespace.c b/fs/namespace.c
38709 index fbf0e596fcd30c..1b942a6b72fa9f 100644
38710 --- a/fs/namespace.c
38711 +++ b/fs/namespace.c
38712 @@ -489,6 +489,7 @@ void mnt_put_write_access_file(struct file *file)
38713 if (!(file->f_mode & FMODE_WRITER))
38714 mnt_put_write_access(file->f_path.mnt);
38716 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
38718 void mnt_drop_write_file(struct file *file)
38720 @@ -874,6 +875,13 @@ static inline int check_mnt(struct mount *mnt)
38721 return mnt->mnt_ns == current->nsproxy->mnt_ns;
38724 +/* for aufs, CONFIG_AUFS_BR_FUSE */
38725 +int is_current_mnt_ns(struct vfsmount *mnt)
38727 + return check_mnt(real_mount(mnt));
38729 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
38732 * vfsmount lock must be held for write
38734 @@ -2148,6 +2156,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
38738 +EXPORT_SYMBOL_GPL(iterate_mounts);
38740 static void lock_mnt_tree(struct mount *mnt)
38742 diff --git a/fs/notify/group.c b/fs/notify/group.c
38743 index 1de6631a3925ee..3008eb37a18d46 100644
38744 --- a/fs/notify/group.c
38745 +++ b/fs/notify/group.c
38746 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
38748 refcount_inc(&group->refcnt);
38750 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
38753 * Drop a reference to a group. Free it if it's through.
38754 diff --git a/fs/open.c b/fs/open.c
38755 index 3494a9cd8046c4..dc6a1779c44d8c 100644
38758 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
38759 inode_unlock(dentry->d_inode);
38762 +EXPORT_SYMBOL_GPL(do_truncate);
38764 long vfs_truncate(const struct path *path, loff_t length)
38766 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
38767 index c6e7ebc637562d..d7ccfd90976464 100644
38768 --- a/fs/proc/nommu.c
38769 +++ b/fs/proc/nommu.c
38770 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
38771 file = region->vm_file;
38774 - struct inode *inode = file_inode(region->vm_file);
38775 + struct inode *inode;
38777 + file = vmr_pr_or_file(region);
38778 + inode = file_inode(file);
38779 dev = inode->i_sb->s_dev;
38780 ino = inode->i_ino;
38782 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
38783 index 435b61054b5b9e..e67bb5fc412535 100644
38784 --- a/fs/proc/task_mmu.c
38785 +++ b/fs/proc/task_mmu.c
38786 @@ -273,7 +273,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
38787 const char *name = NULL;
38790 - struct inode *inode = file_inode(vma->vm_file);
38791 + struct inode *inode;
38793 + file = vma_pr_or_file(vma);
38794 + inode = file_inode(file);
38795 dev = inode->i_sb->s_dev;
38796 ino = inode->i_ino;
38797 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
38798 @@ -2682,7 +2685,7 @@ static int show_numa_map(struct seq_file *m, void *v)
38799 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
38800 struct vm_area_struct *vma = v;
38801 struct numa_maps *md = &numa_priv->md;
38802 - struct file *file = vma->vm_file;
38803 + struct file *file = vma_pr_or_file(vma);
38804 struct mm_struct *mm = vma->vm_mm;
38806 struct mempolicy *pol;
38807 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
38808 index bce67453300031..b12b5a75c799ca 100644
38809 --- a/fs/proc/task_nommu.c
38810 +++ b/fs/proc/task_nommu.c
38811 @@ -137,7 +137,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
38812 file = vma->vm_file;
38815 - struct inode *inode = file_inode(vma->vm_file);
38816 + struct inode *inode;
38818 + file = vma_pr_or_file(vma);
38819 + inode = file_inode(file);
38820 dev = inode->i_sb->s_dev;
38821 ino = inode->i_ino;
38822 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
38823 diff --git a/fs/read_write.c b/fs/read_write.c
38824 index 4771701c896bad..c79270aba79246 100644
38825 --- a/fs/read_write.c
38826 +++ b/fs/read_write.c
38827 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
38828 inc_syscr(current);
38831 +EXPORT_SYMBOL_GPL(vfs_read);
38833 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
38835 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
38836 file_end_write(file);
38839 +EXPORT_SYMBOL_GPL(vfs_write);
38841 /* file_ppos returns &file->f_pos or NULL if file is stream */
38842 static inline loff_t *file_ppos(struct file *file)
38843 diff --git a/fs/splice.c b/fs/splice.c
38844 index d983d375ff1130..7ce1f1bc42684d 100644
38847 @@ -925,13 +925,14 @@ static int warn_unsupported(struct file *file, const char *op)
38849 * Attempt to initiate a splice from pipe to file.
38851 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
38852 - loff_t *ppos, size_t len, unsigned int flags)
38853 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
38854 + loff_t *ppos, size_t len, unsigned int flags)
38856 if (unlikely(!out->f_op->splice_write))
38857 return warn_unsupported(out, "write");
38858 return out->f_op->splice_write(pipe, out, ppos, len, flags);
38860 +EXPORT_SYMBOL_GPL(do_splice_from);
38863 * Indicate to the caller that there was a premature EOF when reading from the
38864 diff --git a/fs/xattr.c b/fs/xattr.c
38865 index 09d9276034335f..890daee95a7bf3 100644
38868 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
38869 *xattr_value = value;
38872 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
38875 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
38876 diff --git a/include/linux/fs.h b/include/linux/fs.h
38877 index 98b7a7a8c42e36..7bf7c5d0b4c9d8 100644
38878 --- a/include/linux/fs.h
38879 +++ b/include/linux/fs.h
38880 @@ -1102,6 +1102,7 @@ extern void fasync_free(struct fasync_struct *);
38881 /* can be called from interrupts */
38882 extern void kill_fasync(struct fasync_struct **, int, int);
38884 +extern int setfl(int fd, struct file *filp, unsigned int arg);
38885 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
38886 extern int f_setown(struct file *filp, int who, int force);
38887 extern void f_delown(struct file *filp);
38888 @@ -1936,6 +1937,7 @@ struct file_operations {
38889 int (*lock) (struct file *, int, struct file_lock *);
38890 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
38891 int (*check_flags)(int);
38892 + int (*setfl)(struct file *, unsigned long);
38893 int (*flock) (struct file *, int, struct file_lock *);
38894 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
38895 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
38896 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
38897 index dc2844b071c2c2..069ffb776c2c12 100644
38898 --- a/include/linux/lockdep.h
38899 +++ b/include/linux/lockdep.h
38900 @@ -249,6 +249,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
38901 return lock->key == key;
38904 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
38909 diff --git a/include/linux/mm.h b/include/linux/mm.h
38910 index da5219b48d5229..ba625a96957fed 100644
38911 --- a/include/linux/mm.h
38912 +++ b/include/linux/mm.h
38913 @@ -2420,6 +2420,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
38914 static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
38915 unsigned long addr);
38917 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
38918 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
38919 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
38921 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
38922 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
38924 +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
38926 +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
38928 +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
38929 +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
38931 +#ifndef CONFIG_MMU
38932 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
38933 +extern void vmr_do_fput(struct vm_region *, const char[], int);
38935 +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
38937 +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
38938 +#endif /* !CONFIG_MMU */
38942 +#define vma_file_update_time(vma) file_update_time((vma)->vm_file)
38943 +#define vma_pr_or_file(vma) (vma)->vm_file
38944 +#define vma_get_file(vma) get_file((vma)->vm_file)
38945 +#define vma_fput(vma) fput((vma)->vm_file)
38947 +#ifndef CONFIG_MMU
38948 +#define vmr_pr_or_file(region) (region)->vm_file
38949 +#define vmr_fput(region) fput((region)->vm_file)
38950 +#endif /* !CONFIG_MMU */
38952 +#endif /* CONFIG_AUFS_FS */
38954 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
38955 void *buf, int len, unsigned int gup_flags);
38956 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
38957 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
38958 index 957ce38768b2a8..cc556e47ea1727 100644
38959 --- a/include/linux/mm_types.h
38960 +++ b/include/linux/mm_types.h
38961 @@ -527,6 +527,9 @@ struct vm_region {
38962 unsigned long vm_top; /* region allocated to here */
38963 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
38964 struct file *vm_file; /* the backing file or NULL */
38965 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
38966 + struct file *vm_prfile; /* the virtual backing file or NULL */
38969 int vm_usage; /* region usage count (access under nommu_region_sem) */
38970 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
38971 @@ -688,6 +691,9 @@ struct vm_area_struct {
38972 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
38974 struct file * vm_file; /* File we map to (can be NULL). */
38975 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
38976 + struct file *vm_prfile; /* shadow of vm_file */
38978 void * vm_private_data; /* was vm_pte (shared mem) */
38980 #ifdef CONFIG_ANON_VMA_NAME
38981 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
38982 index 8f882f5881e879..6b9808f0984351 100644
38983 --- a/include/linux/mnt_namespace.h
38984 +++ b/include/linux/mnt_namespace.h
38985 @@ -7,12 +7,15 @@ struct mnt_namespace;
38987 struct user_namespace;
38991 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
38992 struct user_namespace *, struct fs_struct *);
38993 extern void put_mnt_ns(struct mnt_namespace *ns);
38994 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
38996 +extern int is_current_mnt_ns(struct vfsmount *mnt);
38998 extern const struct file_operations proc_mounts_operations;
38999 extern const struct file_operations proc_mountinfo_operations;
39000 extern const struct file_operations proc_mountstats_operations;
39001 diff --git a/include/linux/splice.h b/include/linux/splice.h
39002 index 6c461573434dde..7416cf375ad126 100644
39003 --- a/include/linux/splice.h
39004 +++ b/include/linux/splice.h
39005 @@ -99,4 +99,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
39007 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
39008 extern const struct pipe_buf_operations default_pipe_buf_ops;
39010 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
39011 + loff_t *ppos, size_t len, unsigned int flags);
39012 +extern long do_splice_to(struct file *in, loff_t *ppos,
39013 + struct pipe_inode_info *pipe, size_t len,
39014 + unsigned int flags);
39016 diff --git a/include/uapi/linux/aufs_type.h b/include/uapi/linux/aufs_type.h
39017 new file mode 100644
39018 index 00000000000000..207ee8ae216206
39020 +++ b/include/uapi/linux/aufs_type.h
39022 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39024 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39026 + * This program is free software; you can redistribute it and/or modify
39027 + * it under the terms of the GNU General Public License as published by
39028 + * the Free Software Foundation; either version 2 of the License, or
39029 + * (at your option) any later version.
39031 + * This program is distributed in the hope that it will be useful,
39032 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39033 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39034 + * GNU General Public License for more details.
39036 + * You should have received a copy of the GNU General Public License
39037 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
39040 +#ifndef __AUFS_TYPE_H__
39041 +#define __AUFS_TYPE_H__
39043 +#define AUFS_NAME "aufs"
39047 + * define it before including all other headers.
39048 + * sched.h may use pr_* macros before defining "current", so define the
39049 + * no-current version first, and re-define later.
39051 +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39052 +#include <linux/sched.h>
39054 +#define pr_fmt(fmt) \
39055 + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39056 + (int)sizeof(current->comm), current->comm, current->pid
39057 +#include <linux/limits.h>
39059 +#include <stdint.h>
39060 +#include <sys/types.h>
39061 +#include <limits.h>
39062 +#endif /* __KERNEL__ */
39064 +#define AUFS_VERSION "6.x-rcN-20231106"
39066 +/* todo? move this to linux-2.6.19/include/magic.h */
39067 +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39069 +/* ---------------------------------------------------------------------- */
39072 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39073 +typedef int8_t aufs_bindex_t;
39074 +#define AUFS_BRANCH_MAX 127
39076 +typedef int16_t aufs_bindex_t;
39077 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39078 +#define AUFS_BRANCH_MAX 511
39079 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39080 +#define AUFS_BRANCH_MAX 1023
39081 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39082 +#define AUFS_BRANCH_MAX 32767
39086 +#ifndef AUFS_BRANCH_MAX
39087 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39089 +#endif /* __KERNEL__ */
39091 +/* ---------------------------------------------------------------------- */
39093 +#define AUFS_FSTYPE AUFS_NAME
39095 +#define AUFS_ROOT_INO 2
39096 +#define AUFS_FIRST_INO 11
39098 +#define AUFS_WH_PFX ".wh."
39099 +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
39100 +#define AUFS_WH_TMP_LEN 4
39101 +/* a limit for rmdir/rename a dir and copyup */
39102 +#define AUFS_MAX_NAMELEN (NAME_MAX \
39103 + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
39105 + - AUFS_WH_TMP_LEN) /* hex */
39106 +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
39107 +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
39108 +#define AUFS_XINO_DEF_SEC 30 /* seconds */
39109 +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
39110 +#define AUFS_DIRWH_DEF 3
39111 +#define AUFS_RDCACHE_DEF 10 /* seconds */
39112 +#define AUFS_RDCACHE_MAX 3600 /* seconds */
39113 +#define AUFS_RDBLK_DEF 512 /* bytes */
39114 +#define AUFS_RDHASH_DEF 32
39115 +#define AUFS_WKQ_NAME AUFS_NAME "d"
39116 +#define AUFS_MFS_DEF_SEC 30 /* seconds */
39117 +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
39118 +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
39119 +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
39121 +/* pseudo-link maintenace under /proc */
39122 +#define AUFS_PLINK_MAINT_NAME "plink_maint"
39123 +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
39124 +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39126 +/* dirren, renamed dir */
39127 +#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
39128 +#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
39129 +/* whiteouted doubly */
39130 +#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
39131 +#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39133 +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
39134 +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
39136 +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
39137 +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
39138 +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
39140 +/* doubly whiteouted */
39141 +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39142 +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39143 +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39145 +/* branch permissions and attributes */
39146 +#define AUFS_BRPERM_RW "rw"
39147 +#define AUFS_BRPERM_RO "ro"
39148 +#define AUFS_BRPERM_RR "rr"
39149 +#define AUFS_BRATTR_COO_REG "coo_reg"
39150 +#define AUFS_BRATTR_COO_ALL "coo_all"
39151 +#define AUFS_BRATTR_FHSM "fhsm"
39152 +#define AUFS_BRATTR_UNPIN "unpin"
39153 +#define AUFS_BRATTR_ICEX "icex"
39154 +#define AUFS_BRATTR_ICEX_SEC "icexsec"
39155 +#define AUFS_BRATTR_ICEX_SYS "icexsys"
39156 +#define AUFS_BRATTR_ICEX_TR "icextr"
39157 +#define AUFS_BRATTR_ICEX_USR "icexusr"
39158 +#define AUFS_BRATTR_ICEX_OTH "icexoth"
39159 +#define AUFS_BRRATTR_WH "wh"
39160 +#define AUFS_BRWATTR_NLWH "nolwh"
39161 +#define AUFS_BRWATTR_MOO "moo"
39163 +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39164 +#define AuBrPerm_RO (1 << 1) /* readonly */
39165 +#define AuBrPerm_RR (1 << 2) /* natively readonly */
39166 +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39168 +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39169 +#define AuBrAttr_COO_ALL (1 << 4)
39170 +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39172 +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39173 +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
39174 + branch. meaningless since
39175 + linux-3.18-rc1 */
39177 +/* ignore error in copying XATTR */
39178 +#define AuBrAttr_ICEX_SEC (1 << 7)
39179 +#define AuBrAttr_ICEX_SYS (1 << 8)
39180 +#define AuBrAttr_ICEX_TR (1 << 9)
39181 +#define AuBrAttr_ICEX_USR (1 << 10)
39182 +#define AuBrAttr_ICEX_OTH (1 << 11)
39183 +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39184 + | AuBrAttr_ICEX_SYS \
39185 + | AuBrAttr_ICEX_TR \
39186 + | AuBrAttr_ICEX_USR \
39187 + | AuBrAttr_ICEX_OTH)
39189 +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
39190 +#define AuBrRAttr_Mask AuBrRAttr_WH
39192 +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39193 +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
39194 +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39196 +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39198 +/* #warning test userspace */
39200 +#ifndef CONFIG_AUFS_FHSM
39201 +#undef AuBrAttr_FHSM
39202 +#define AuBrAttr_FHSM 0
39204 +#ifndef CONFIG_AUFS_XATTR
39205 +#undef AuBrAttr_ICEX
39206 +#define AuBrAttr_ICEX 0
39207 +#undef AuBrAttr_ICEX_SEC
39208 +#define AuBrAttr_ICEX_SEC 0
39209 +#undef AuBrAttr_ICEX_SYS
39210 +#define AuBrAttr_ICEX_SYS 0
39211 +#undef AuBrAttr_ICEX_TR
39212 +#define AuBrAttr_ICEX_TR 0
39213 +#undef AuBrAttr_ICEX_USR
39214 +#define AuBrAttr_ICEX_USR 0
39215 +#undef AuBrAttr_ICEX_OTH
39216 +#define AuBrAttr_ICEX_OTH 0
39220 +/* the longest combination */
39221 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39222 +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39223 + "+" AUFS_BRATTR_COO_REG \
39224 + "+" AUFS_BRATTR_FHSM \
39225 + "+" AUFS_BRATTR_UNPIN \
39226 + "+" AUFS_BRATTR_ICEX_SEC \
39227 + "+" AUFS_BRATTR_ICEX_SYS \
39228 + "+" AUFS_BRATTR_ICEX_USR \
39229 + "+" AUFS_BRATTR_ICEX_OTH \
39230 + "+" AUFS_BRWATTR_NLWH)
39233 + char a[AuBrPermStrSz];
39234 +} au_br_perm_str_t;
39236 +static inline int au_br_writable(int brperm)
39238 + return brperm & AuBrPerm_RW;
39241 +static inline int au_br_whable(int brperm)
39243 + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39246 +static inline int au_br_wh_linkable(int brperm)
39248 + return !(brperm & AuBrWAttr_NoLinkWH);
39251 +static inline int au_br_cmoo(int brperm)
39253 + return brperm & AuBrAttr_CMOO_Mask;
39256 +static inline int au_br_fhsm(int brperm)
39258 + return brperm & AuBrAttr_FHSM;
39261 +/* ---------------------------------------------------------------------- */
39265 + /* readdir in userspace */
39269 + AuCtl_WBR_FD, /* pathconf wrapper */
39270 + AuCtl_IBUSY, /* busy inode */
39271 + AuCtl_MVDOWN, /* move-down */
39272 + AuCtl_BR, /* info about branches */
39273 + AuCtl_FHSM_FD /* connection for fhsm */
39276 +/* borrowed from linux/include/linux/kernel.h */
39278 +#ifdef _GNU_SOURCE
39279 +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
39281 +#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39283 +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39286 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39288 +#define __aligned(x) __attribute__((aligned(x)))
39293 +#define __packed __attribute__((packed))
39297 +struct au_rdu_cookie {
39302 + uint32_t generation;
39305 +struct au_rdu_ent {
39314 +static inline int au_rdu_len(int nlen)
39316 + /* include the terminating NULL */
39317 + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39318 + sizeof(uint64_t));
39321 +union au_rdu_ent_ul {
39322 + struct au_rdu_ent __user *e;
39334 + uint64_t sz; /* AuCtl_RDU */
39335 + uint64_t nent; /* AuCtl_RDU_INO */
39337 + union au_rdu_ent_ul ent;
39338 + uint16_t verify[AufsCtlRduV_End];
39340 + /* input/output */
39344 + union au_rdu_ent_ul tail;
39345 + /* number of entries which were added in a single call */
39350 + struct au_rdu_cookie cookie;
39353 +/* ---------------------------------------------------------------------- */
39355 +/* dirren. the branch is identified by the filename who contains this */
39356 +struct au_drinfo {
39359 + uint8_t oldnamelen;
39360 + uint64_t _padding;
39362 + uint8_t oldname[];
39365 +struct au_drinfo_fdata {
39367 + struct au_drinfo drinfo;
39370 +#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39372 +#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39374 +/* ---------------------------------------------------------------------- */
39376 +struct aufs_wbr_fd {
39381 +/* ---------------------------------------------------------------------- */
39383 +struct aufs_ibusy {
39384 + uint64_t ino, h_ino;
39388 +/* ---------------------------------------------------------------------- */
39390 +/* error code for move-down */
39391 +/* the actual message strings are implemented in aufs-util.git */
39393 + EAU_MVDOWN_OPAQUE = 1,
39394 + EAU_MVDOWN_WHITEOUT,
39395 + EAU_MVDOWN_UPPER,
39396 + EAU_MVDOWN_BOTTOM,
39397 + EAU_MVDOWN_NOUPPER,
39398 + EAU_MVDOWN_NOLOWERBR,
39402 +/* flags for move-down */
39403 +#define AUFS_MVDOWN_DMSG 1
39404 +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39405 +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39406 +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39407 +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39408 +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39409 +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39410 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39411 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
39412 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39413 +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39414 +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39415 +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
39417 +/* index for move-down */
39419 + AUFS_MVDOWN_UPPER,
39420 + AUFS_MVDOWN_LOWER,
39421 + AUFS_MVDOWN_NARRAY
39425 + * additional info of move-down
39426 + * number of free blocks and inodes.
39427 + * subset of struct kstatfs, but smaller and always 64bit.
39429 +struct aufs_stfs {
39430 + uint64_t f_blocks;
39431 + uint64_t f_bavail;
39432 + uint64_t f_files;
39433 + uint64_t f_ffree;
39436 +struct aufs_stbr {
39437 + int16_t brid; /* optional input */
39438 + int16_t bindex; /* output */
39439 + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39442 +struct aufs_mvdown {
39443 + uint32_t flags; /* input/output */
39444 + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39445 + int8_t au_errno; /* output */
39448 +/* ---------------------------------------------------------------------- */
39450 +union aufs_brinfo {
39451 + /* PATH_MAX may differ between kernel-space and user-space */
39452 + char _spacer[4096];
39460 +/* ---------------------------------------------------------------------- */
39462 +#define AuCtlType 'A'
39463 +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39464 +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39465 +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39466 + struct aufs_wbr_fd)
39467 +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39468 +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39469 + struct aufs_mvdown)
39470 +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39471 +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39473 +#endif /* __AUFS_TYPE_H__ */
39474 diff --git a/kernel/fork.c b/kernel/fork.c
39475 index 10917c3e1f0366..6018e512fb9c32 100644
39476 --- a/kernel/fork.c
39477 +++ b/kernel/fork.c
39478 @@ -731,7 +731,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
39480 struct address_space *mapping = file->f_mapping;
39483 + vma_get_file(tmp);
39484 i_mmap_lock_write(mapping);
39485 if (vma_is_shared_maywrite(tmp))
39486 mapping_allow_writable(mapping);
39487 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
39488 index 151bd3de59363a..d0ac4fc69a31dc 100644
39489 --- a/kernel/locking/lockdep.c
39490 +++ b/kernel/locking/lockdep.c
39491 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
39492 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
39493 DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
39495 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
39496 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
39498 unsigned int class_idx = hlock->class_idx;
39500 @@ -239,6 +239,8 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
39502 return lock_classes + class_idx;
39504 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
39505 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
39507 #ifdef CONFIG_LOCK_STAT
39508 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
39509 diff --git a/kernel/task_work.c b/kernel/task_work.c
39510 index 95a7e1b7f1dab2..5053670775d3ce 100644
39511 --- a/kernel/task_work.c
39512 +++ b/kernel/task_work.c
39513 @@ -183,3 +183,4 @@ void task_work_run(void)
39517 +EXPORT_SYMBOL_GPL(task_work_run);
39518 diff --git a/mm/Makefile b/mm/Makefile
39519 index 33873c8aedb399..ae036592066aa6 100644
39522 @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
39523 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
39524 obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
39525 obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
39527 diff --git a/mm/filemap.c b/mm/filemap.c
39528 index ad5b4aa049a380..e855d6c72168fe 100644
39531 @@ -3584,7 +3584,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
39532 vm_fault_t ret = VM_FAULT_LOCKED;
39534 sb_start_pagefault(mapping->host->i_sb);
39535 - file_update_time(vmf->vma->vm_file);
39536 + vma_file_update_time(vmf->vma);
39538 if (folio->mapping != mapping) {
39539 folio_unlock(folio);
39540 diff --git a/mm/mmap.c b/mm/mmap.c
39541 index aa82eec17489ca..a37484d87ff0b3 100644
39544 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
39545 if (vma->vm_ops && vma->vm_ops->close)
39546 vma->vm_ops->close(vma);
39548 - fput(vma->vm_file);
39550 mpol_put(vma_policy(vma));
39552 __vm_area_free(vma);
39553 @@ -554,7 +554,7 @@ static inline void vma_complete(struct vma_prepare *vp,
39555 uprobe_munmap(vp->remove, vp->remove->vm_start,
39556 vp->remove->vm_end);
39558 + vma_fput(vp->vma);
39560 if (vp->remove->anon_vma)
39561 anon_vma_merge(vp->vma, vp->remove);
39562 @@ -2390,7 +2390,7 @@ static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
39563 goto out_free_mpol;
39566 - get_file(new->vm_file);
39567 + vma_get_file(new);
39569 if (new->vm_ops && new->vm_ops->open)
39570 new->vm_ops->open(new);
39571 @@ -2888,7 +2888,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
39572 * and cause general protection fault
39575 - fput(vma->vm_file);
39579 /* Update vm_flags to pick up the change. */
39580 @@ -2983,7 +2983,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
39582 if (file || vma->vm_file) {
39583 unmap_and_free_vma:
39584 - fput(vma->vm_file);
39586 vma->vm_file = NULL;
39588 vma_iter_set(&vmi, vma->vm_end);
39589 @@ -3045,6 +3045,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
39590 unsigned long populate = 0;
39591 unsigned long ret = -EINVAL;
39593 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
39594 + struct file *prfile;
39597 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
39598 current->comm, current->pid);
39599 @@ -3103,10 +3106,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
39600 if (vma->vm_flags & VM_LOCKED)
39601 flags |= MAP_LOCKED;
39603 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
39604 + vma_get_file(vma);
39605 + file = vma->vm_file;
39606 + prfile = vma->vm_prfile;
39607 + ret = do_mmap(vma->vm_file, start, size,
39608 + prot, flags, /*vm_flags*/0, pgoff, &populate, NULL);
39609 + if (!IS_ERR_VALUE(ret) && file && prfile) {
39610 + struct vm_area_struct *new_vma;
39612 + new_vma = find_vma(mm, ret);
39613 + if (!new_vma->vm_prfile)
39614 + new_vma->vm_prfile = prfile;
39616 + get_file(prfile);
39619 + * two fput()s instead of vma_fput(vma),
39620 + * coz vma may not be available anymore.
39626 file = get_file(vma->vm_file);
39627 ret = do_mmap(vma->vm_file, start, size,
39628 prot, flags, 0, pgoff, &populate, NULL);
39630 +#endif /* CONFIG_AUFS_FS */
39632 mmap_write_unlock(mm);
39634 @@ -3450,7 +3477,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
39635 if (anon_vma_clone(new_vma, vma))
39636 goto out_free_mempol;
39637 if (new_vma->vm_file)
39638 - get_file(new_vma->vm_file);
39639 + vma_get_file(new_vma);
39640 if (new_vma->vm_ops && new_vma->vm_ops->open)
39641 new_vma->vm_ops->open(new_vma);
39642 if (vma_link(mm, new_vma))
39643 @@ -3464,7 +3491,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
39644 new_vma->vm_ops->close(new_vma);
39646 if (new_vma->vm_file)
39647 - fput(new_vma->vm_file);
39648 + vma_fput(new_vma);
39650 unlink_anon_vmas(new_vma);
39652 diff --git a/mm/nommu.c b/mm/nommu.c
39653 index b6dc558d314408..d96655486468e4 100644
39656 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
39657 up_write(&nommu_region_sem);
39659 if (region->vm_file)
39660 - fput(region->vm_file);
39661 + vmr_fput(region);
39663 /* IO memory and memory shared directly out of the pagecache
39664 * from ramfs/tmpfs mustn't be released here */
39665 @@ -603,7 +603,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
39666 if (vma->vm_ops && vma->vm_ops->close)
39667 vma->vm_ops->close(vma);
39669 - fput(vma->vm_file);
39671 put_nommu_region(vma->vm_region);
39674 @@ -1135,7 +1135,7 @@ unsigned long do_mmap(struct file *file,
39675 goto error_just_free;
39678 - fput(region->vm_file);
39679 + vmr_fput(region);
39680 kmem_cache_free(vm_region_jar, region);
39683 @@ -1221,10 +1221,10 @@ unsigned long do_mmap(struct file *file,
39685 vma_iter_free(&vmi);
39686 if (region->vm_file)
39687 - fput(region->vm_file);
39688 + vmr_fput(region);
39689 kmem_cache_free(vm_region_jar, region);
39691 - fput(vma->vm_file);
39696 diff --git a/mm/prfile.c b/mm/prfile.c
39697 new file mode 100644
39698 index 00000000000000..8f820a2353646f
39702 +// SPDX-License-Identifier: GPL-2.0
39704 + * Mainly for aufs which mmap(2) different file and wants to print different
39705 + * path in /proc/PID/maps.
39706 + * Call these functions via macros defined in linux/mm.h.
39708 + * See Documentation/filesystems/aufs/design/06mmap.txt
39710 + * Copyright (c) 2014-2022 Junjro R. Okajima
39711 + * Copyright (c) 2014 Ian Campbell
39714 +#include <linux/mm.h>
39715 +#include <linux/file.h>
39716 +#include <linux/fs.h>
39718 +/* #define PRFILE_TRACE */
39719 +static inline void prfile_trace(struct file *f, struct file *pr,
39720 + const char func[], int line, const char func2[])
39722 +#ifdef PRFILE_TRACE
39724 + pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
39728 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
39731 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
39733 + prfile_trace(f, pr, func, line, __func__);
39734 + file_update_time(f);
39736 + file_update_time(pr);
39739 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
39742 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
39744 + prfile_trace(f, pr, func, line, __func__);
39745 + return (f && pr) ? pr : f;
39748 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
39750 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
39752 + prfile_trace(f, pr, func, line, __func__);
39758 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
39760 + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
39762 + prfile_trace(f, pr, func, line, __func__);
39768 +#ifndef CONFIG_MMU
39769 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
39772 + struct file *f = region->vm_file, *pr = region->vm_prfile;
39774 + prfile_trace(f, pr, func, line, __func__);
39775 + return (f && pr) ? pr : f;
39778 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
39780 + struct file *f = region->vm_file, *pr = region->vm_prfile;
39782 + prfile_trace(f, pr, func, line, __func__);
39787 +#endif /* !CONFIG_MMU */
39788 diff --git a/security/security.c b/security/security.c
39789 index dcb3e7014f9bdd..a7135660dd2c70 100644
39790 --- a/security/security.c
39791 +++ b/security/security.c
39792 @@ -1750,6 +1750,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
39794 return call_int_hook(path_rmdir, 0, dir, dentry);
39796 +EXPORT_SYMBOL_GPL(security_path_rmdir);
39799 * security_path_unlink() - Check if removing a hard link is allowed
39800 @@ -1785,6 +1786,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
39802 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
39804 +EXPORT_SYMBOL_GPL(security_path_symlink);
39807 * security_path_link - Check if creating a hard link is allowed
39808 @@ -1803,6 +1805,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
39810 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
39812 +EXPORT_SYMBOL_GPL(security_path_link);
39815 * security_path_rename() - Check if renaming a file is allowed
39816 @@ -1864,6 +1867,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
39818 return call_int_hook(path_chmod, 0, path, mode);
39820 +EXPORT_SYMBOL_GPL(security_path_chmod);
39823 * security_path_chown() - Check if changing the file's owner/group is allowed
39824 @@ -1881,6 +1885,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
39826 return call_int_hook(path_chown, 0, path, uid, gid);
39828 +EXPORT_SYMBOL_GPL(security_path_chown);
39831 * security_path_chroot() - Check if changing the root directory is allowed
39832 @@ -2110,6 +2115,7 @@ int security_inode_permission(struct inode *inode, int mask)
39834 return call_int_hook(inode_permission, 0, inode, mask);
39836 +EXPORT_SYMBOL_GPL(security_inode_permission);
39839 * security_inode_setattr() - Check if setting file attributes is allowed
39840 @@ -2588,6 +2594,7 @@ int security_file_permission(struct file *file, int mask)
39842 return fsnotify_perm(file, mask);
39844 +EXPORT_SYMBOL_GPL(security_file_permission);
39847 * security_file_alloc() - Allocate and init a file's LSM blob
39848 @@ -2854,6 +2861,7 @@ int security_file_truncate(struct file *file)
39850 return call_int_hook(file_truncate, 0, file);
39852 +EXPORT_SYMBOL_GPL(security_file_truncate);
39855 * security_task_alloc() - Allocate a task's LSM blob