1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2021 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * directory operations
23 #include <linux/fs_stack.h>
24 #include <linux/iversion.h>
27 void au_add_nlink(struct inode
*dir
, struct inode
*h_dir
)
31 AuDebugOn(!S_ISDIR(dir
->i_mode
) || !S_ISDIR(h_dir
->i_mode
));
34 nlink
+= h_dir
->i_nlink
- 2;
35 if (h_dir
->i_nlink
< 2)
37 smp_mb(); /* for i_nlink */
38 /* 0 can happen in revaliding */
39 set_nlink(dir
, nlink
);
42 void au_sub_nlink(struct inode
*dir
, struct inode
*h_dir
)
46 AuDebugOn(!S_ISDIR(dir
->i_mode
) || !S_ISDIR(h_dir
->i_mode
));
49 nlink
-= h_dir
->i_nlink
- 2;
50 if (h_dir
->i_nlink
< 2)
52 smp_mb(); /* for i_nlink */
53 /* nlink == 0 means the branch-fs is broken */
54 set_nlink(dir
, nlink
);
57 loff_t
au_dir_size(struct file
*file
, struct dentry
*dentry
)
60 aufs_bindex_t bindex
, bbot
;
62 struct dentry
*h_dentry
;
66 AuDebugOn(!d_is_dir(file
->f_path
.dentry
));
68 bbot
= au_fbbot_dir(file
);
69 for (bindex
= au_fbtop(file
);
70 bindex
<= bbot
&& sz
< KMALLOC_MAX_SIZE
;
72 h_file
= au_hf_dir(file
, bindex
);
73 if (h_file
&& file_inode(h_file
))
74 sz
+= vfsub_f_size_read(h_file
);
78 AuDebugOn(!d_is_dir(dentry
));
80 bbot
= au_dbtaildir(dentry
);
81 for (bindex
= au_dbtop(dentry
);
82 bindex
<= bbot
&& sz
< KMALLOC_MAX_SIZE
;
84 h_dentry
= au_h_dptr(dentry
, bindex
);
85 if (h_dentry
&& d_is_positive(h_dentry
))
86 sz
+= i_size_read(d_inode(h_dentry
));
89 if (sz
< KMALLOC_MAX_SIZE
)
90 sz
= roundup_pow_of_two(sz
);
91 if (sz
> KMALLOC_MAX_SIZE
)
92 sz
= KMALLOC_MAX_SIZE
;
93 else if (sz
< NAME_MAX
) {
94 BUILD_BUG_ON(AUFS_RDBLK_DEF
< NAME_MAX
);
100 struct au_dir_ts_arg
{
101 struct dentry
*dentry
;
105 static void au_do_dir_ts(void *arg
)
107 struct au_dir_ts_arg
*a
= arg
;
110 struct inode
*dir
, *h_dir
;
111 struct super_block
*sb
;
112 struct au_branch
*br
;
113 struct au_hinode
*hdir
;
115 aufs_bindex_t btop
, bindex
;
117 sb
= a
->dentry
->d_sb
;
118 if (d_really_is_negative(a
->dentry
))
120 /* no dir->i_mutex lock */
121 aufs_read_lock(a
->dentry
, AuLock_DW
); /* noflush */
123 dir
= d_inode(a
->dentry
);
124 btop
= au_ibtop(dir
);
125 bindex
= au_br_index(sb
, a
->brid
);
129 br
= au_sbr(sb
, bindex
);
130 h_path
.dentry
= au_h_dptr(a
->dentry
, bindex
);
133 h_path
.mnt
= au_br_mnt(br
);
134 au_dtime_store(&dt
, a
->dentry
, &h_path
);
136 br
= au_sbr(sb
, btop
);
137 if (!au_br_writable(br
->br_perm
))
139 h_path
.dentry
= au_h_dptr(a
->dentry
, btop
);
140 h_path
.mnt
= au_br_mnt(br
);
141 err
= vfsub_mnt_want_write(h_path
.mnt
);
144 hdir
= au_hi(dir
, btop
);
145 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
146 h_dir
= au_h_iptr(dir
, btop
);
148 && timespec64_compare(&h_dir
->i_mtime
, &dt
.dt_mtime
) < 0) {
149 dt
.dt_h_path
= h_path
;
150 au_dtime_revert(&dt
);
152 au_hn_inode_unlock(hdir
);
153 vfsub_mnt_drop_write(h_path
.mnt
);
154 au_cpup_attr_timesizes(dir
);
157 aufs_read_unlock(a
->dentry
, AuLock_DW
);
160 au_nwt_done(&au_sbi(sb
)->si_nowait
);
161 au_kfree_try_rcu(arg
);
164 void au_dir_ts(struct inode
*dir
, aufs_bindex_t bindex
)
168 struct au_dir_ts_arg
*arg
;
169 struct dentry
*dentry
;
170 struct super_block
*sb
;
174 dentry
= d_find_any_alias(dir
);
177 btop
= au_ibtop(dir
);
178 if (btop
== bindex
) {
179 au_cpup_attr_timesizes(dir
);
183 perm
= au_sbr_perm(sb
, btop
);
184 if (!au_br_writable(perm
))
187 arg
= kmalloc(sizeof(*arg
), GFP_NOFS
);
191 arg
->dentry
= dget(dentry
); /* will be dput-ted by au_do_dir_ts() */
192 arg
->brid
= au_sbr_id(sb
, bindex
);
193 wkq_err
= au_wkq_nowait(au_do_dir_ts
, arg
, sb
, /*flags*/0);
194 if (unlikely(wkq_err
)) {
195 pr_err("wkq %d\n", wkq_err
);
197 au_kfree_try_rcu(arg
);
204 /* ---------------------------------------------------------------------- */
206 static int reopen_dir(struct file
*file
)
210 aufs_bindex_t bindex
, btail
, btop
;
211 struct dentry
*dentry
, *h_dentry
;
214 /* open all lower dirs */
215 dentry
= file
->f_path
.dentry
;
216 btop
= au_dbtop(dentry
);
217 for (bindex
= au_fbtop(file
); bindex
< btop
; bindex
++)
218 au_set_h_fptr(file
, bindex
, NULL
);
219 au_set_fbtop(file
, btop
);
221 btail
= au_dbtaildir(dentry
);
222 for (bindex
= au_fbbot_dir(file
); btail
< bindex
; bindex
--)
223 au_set_h_fptr(file
, bindex
, NULL
);
224 au_set_fbbot_dir(file
, btail
);
226 flags
= vfsub_file_flags(file
);
227 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
228 h_dentry
= au_h_dptr(dentry
, bindex
);
231 h_file
= au_hf_dir(file
, bindex
);
235 h_file
= au_h_open(dentry
, bindex
, flags
, file
, /*force_wr*/0);
236 err
= PTR_ERR(h_file
);
238 goto out
; /* close all? */
239 au_set_h_fptr(file
, bindex
, h_file
);
241 au_update_figen(file
);
242 /* todo: necessary? */
243 /* file->f_ra = h_file->f_ra; */
250 static int do_open_dir(struct file
*file
, int flags
, struct file
*h_file
)
253 aufs_bindex_t bindex
, btail
;
254 struct dentry
*dentry
, *h_dentry
;
255 struct vfsmount
*mnt
;
257 FiMustWriteLock(file
);
261 mnt
= file
->f_path
.mnt
;
262 dentry
= file
->f_path
.dentry
;
263 file
->f_version
= inode_query_iversion(d_inode(dentry
));
264 bindex
= au_dbtop(dentry
);
265 au_set_fbtop(file
, bindex
);
266 btail
= au_dbtaildir(dentry
);
267 au_set_fbbot_dir(file
, btail
);
268 for (; !err
&& bindex
<= btail
; bindex
++) {
269 h_dentry
= au_h_dptr(dentry
, bindex
);
273 err
= vfsub_test_mntns(mnt
, h_dentry
->d_sb
);
276 h_file
= au_h_open(dentry
, bindex
, flags
, file
, /*force_wr*/0);
277 if (IS_ERR(h_file
)) {
278 err
= PTR_ERR(h_file
);
281 au_set_h_fptr(file
, bindex
, h_file
);
283 au_update_figen(file
);
284 /* todo: necessary? */
285 /* file->f_ra = h_file->f_ra; */
287 return 0; /* success */
290 for (bindex
= au_fbtop(file
); bindex
<= btail
; bindex
++)
291 au_set_h_fptr(file
, bindex
, NULL
);
292 au_set_fbtop(file
, -1);
293 au_set_fbbot_dir(file
, -1);
298 static int aufs_open_dir(struct inode
*inode __maybe_unused
,
302 struct super_block
*sb
;
303 struct au_fidir
*fidir
;
306 sb
= file
->f_path
.dentry
->d_sb
;
307 si_read_lock(sb
, AuLock_FLUSH
);
308 fidir
= au_fidir_alloc(sb
);
310 struct au_do_open_args args
= {
314 err
= au_do_open(file
, &args
);
322 static int aufs_release_dir(struct inode
*inode __maybe_unused
,
325 struct au_vdir
*vdir_cache
;
326 struct au_finfo
*finfo
;
327 struct au_fidir
*fidir
;
329 aufs_bindex_t bindex
, bbot
;
332 fidir
= finfo
->fi_hdir
;
334 au_hbl_del(&finfo
->fi_hlist
,
335 &au_sbi(file
->f_path
.dentry
->d_sb
)->si_files
);
336 vdir_cache
= fidir
->fd_vdir_cache
; /* lock-free */
338 au_vdir_free(vdir_cache
);
340 bindex
= finfo
->fi_btop
;
342 hf
= fidir
->fd_hfile
+ bindex
;
344 * calls fput() instead of filp_close(),
345 * since no dnotify or lock for the lower file.
347 bbot
= fidir
->fd_bbot
;
348 for (; bindex
<= bbot
; bindex
++, hf
++)
350 au_hfput(hf
, /*execed*/0);
353 finfo
->fi_hdir
= NULL
;
359 /* ---------------------------------------------------------------------- */
361 static int au_do_flush_dir(struct file
*file
, fl_owner_t id
)
364 aufs_bindex_t bindex
, bbot
;
368 bbot
= au_fbbot_dir(file
);
369 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
370 h_file
= au_hf_dir(file
, bindex
);
372 err
= vfsub_flush(h_file
, id
);
377 static int aufs_flush_dir(struct file
*file
, fl_owner_t id
)
379 return au_do_flush(file
, id
, au_do_flush_dir
);
382 /* ---------------------------------------------------------------------- */
384 static int au_do_fsync_dir_no_file(struct dentry
*dentry
, int datasync
)
387 aufs_bindex_t bbot
, bindex
;
389 struct super_block
*sb
;
393 inode
= d_inode(dentry
);
395 bbot
= au_dbbot(dentry
);
396 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= bbot
; bindex
++) {
399 if (au_test_ro(sb
, bindex
, inode
))
401 h_path
.dentry
= au_h_dptr(dentry
, bindex
);
405 h_path
.mnt
= au_sbr_mnt(sb
, bindex
);
406 err
= vfsub_fsync(NULL
, &h_path
, datasync
);
412 static int au_do_fsync_dir(struct file
*file
, int datasync
)
415 aufs_bindex_t bbot
, bindex
;
417 struct super_block
*sb
;
420 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1, /*fi_lsc*/0);
424 inode
= file_inode(file
);
426 bbot
= au_fbbot_dir(file
);
427 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
428 h_file
= au_hf_dir(file
, bindex
);
429 if (!h_file
|| au_test_ro(sb
, bindex
, inode
))
432 err
= vfsub_fsync(h_file
, &h_file
->f_path
, datasync
);
442 static int aufs_fsync_dir(struct file
*file
, loff_t start
, loff_t end
,
446 struct dentry
*dentry
;
448 struct super_block
*sb
;
451 dentry
= file
->f_path
.dentry
;
452 inode
= d_inode(dentry
);
455 si_noflush_read_lock(sb
);
457 err
= au_do_fsync_dir(file
, datasync
);
459 di_write_lock_child(dentry
);
460 err
= au_do_fsync_dir_no_file(dentry
, datasync
);
462 au_cpup_attr_timesizes(inode
);
463 di_write_unlock(dentry
);
465 fi_write_unlock(file
);
472 /* ---------------------------------------------------------------------- */
474 static int aufs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
477 struct dentry
*dentry
;
478 struct inode
*inode
, *h_inode
;
479 struct super_block
*sb
;
481 AuDbg("%pD, ctx{%ps, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
483 dentry
= file
->f_path
.dentry
;
484 inode
= d_inode(dentry
);
488 si_read_lock(sb
, AuLock_FLUSH
);
489 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1, /*fi_lsc*/0);
492 err
= au_alive_dir(dentry
);
494 err
= au_vdir_init(file
);
495 di_downgrade_lock(dentry
, AuLock_IR
);
499 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
500 if (!au_test_nfsd()) {
501 err
= au_vdir_fill_de(file
, ctx
);
502 fsstack_copy_attr_atime(inode
, h_inode
);
505 * nfsd filldir may call lookup_one_len(), vfs_getattr(),
506 * encode_fh() and others.
508 atomic_inc(&h_inode
->i_count
);
509 di_read_unlock(dentry
, AuLock_IR
);
511 err
= au_vdir_fill_de(file
, ctx
);
512 fsstack_copy_attr_atime(inode
, h_inode
);
513 fi_write_unlock(file
);
521 di_read_unlock(dentry
, AuLock_IR
);
522 fi_write_unlock(file
);
528 /* ---------------------------------------------------------------------- */
530 #define AuTestEmpty_WHONLY 1
531 #define AuTestEmpty_CALLED (1 << 1)
532 #define AuTestEmpty_SHWH (1 << 2)
533 #define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
534 #define au_fset_testempty(flags, name) \
535 do { (flags) |= AuTestEmpty_##name; } while (0)
536 #define au_fclr_testempty(flags, name) \
537 do { (flags) &= ~AuTestEmpty_##name; } while (0)
539 #ifndef CONFIG_AUFS_SHWH
540 #undef AuTestEmpty_SHWH
541 #define AuTestEmpty_SHWH 0
544 struct test_empty_arg
{
545 struct dir_context ctx
;
546 struct au_nhash
*whlist
;
549 aufs_bindex_t bindex
;
552 static int test_empty_cb(struct dir_context
*ctx
, const char *__name
,
553 int namelen
, loff_t offset __maybe_unused
, u64 ino
,
556 struct test_empty_arg
*arg
= container_of(ctx
, struct test_empty_arg
,
558 char *name
= (void *)__name
;
561 au_fset_testempty(arg
->flags
, CALLED
);
564 && (namelen
== 1 || (name
[1] == '.' && namelen
== 2)))
565 goto out
; /* success */
567 if (namelen
<= AUFS_WH_PFX_LEN
568 || memcmp(name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
)) {
569 if (au_ftest_testempty(arg
->flags
, WHONLY
)
570 && !au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
571 arg
->err
= -ENOTEMPTY
;
575 name
+= AUFS_WH_PFX_LEN
;
576 namelen
-= AUFS_WH_PFX_LEN
;
577 if (!au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
578 arg
->err
= au_nhash_append_wh
579 (arg
->whlist
, name
, namelen
, ino
, d_type
, arg
->bindex
,
580 au_ftest_testempty(arg
->flags
, SHWH
));
584 AuTraceErr(arg
->err
);
588 static int do_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
592 struct au_branch
*br
;
594 h_file
= au_h_open(dentry
, arg
->bindex
,
595 O_RDONLY
| O_NONBLOCK
| O_DIRECTORY
| O_LARGEFILE
,
596 /*file*/NULL
, /*force_wr*/0);
597 err
= PTR_ERR(h_file
);
602 if (!au_opt_test(au_mntflags(dentry
->d_sb
), UDBA_NONE
)
603 && !file_inode(h_file
)->i_nlink
)
608 au_fclr_testempty(arg
->flags
, CALLED
);
610 err
= vfsub_iterate_dir(h_file
, &arg
->ctx
);
613 } while (!err
&& au_ftest_testempty(arg
->flags
, CALLED
));
617 br
= au_sbr(dentry
->d_sb
, arg
->bindex
);
618 au_lcnt_dec(&br
->br_nfiles
);
623 struct do_test_empty_args
{
625 struct dentry
*dentry
;
626 struct test_empty_arg
*arg
;
629 static void call_do_test_empty(void *args
)
631 struct do_test_empty_args
*a
= args
;
632 *a
->errp
= do_test_empty(a
->dentry
, a
->arg
);
635 static int sio_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
638 struct dentry
*h_dentry
;
639 struct inode
*h_inode
;
640 struct user_namespace
*h_userns
;
642 h_userns
= au_sbr_userns(dentry
->d_sb
, arg
->bindex
);
643 h_dentry
= au_h_dptr(dentry
, arg
->bindex
);
644 h_inode
= d_inode(h_dentry
);
645 /* todo: i_mode changes anytime? */
646 inode_lock_shared_nested(h_inode
, AuLsc_I_CHILD
);
647 err
= au_test_h_perm_sio(h_userns
, h_inode
, MAY_EXEC
| MAY_READ
);
648 inode_unlock_shared(h_inode
);
650 err
= do_test_empty(dentry
, arg
);
652 struct do_test_empty_args args
= {
657 unsigned int flags
= arg
->flags
;
659 wkq_err
= au_wkq_wait(call_do_test_empty
, &args
);
660 if (unlikely(wkq_err
))
668 int au_test_empty_lower(struct dentry
*dentry
)
672 aufs_bindex_t bindex
, btop
, btail
;
673 struct au_nhash whlist
;
674 struct test_empty_arg arg
= {
676 .actor
= test_empty_cb
679 int (*test_empty
)(struct dentry
*dentry
, struct test_empty_arg
*arg
);
681 SiMustAnyLock(dentry
->d_sb
);
683 rdhash
= au_sbi(dentry
->d_sb
)->si_rdhash
;
685 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, dentry
));
686 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
691 arg
.whlist
= &whlist
;
692 btop
= au_dbtop(dentry
);
693 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
694 au_fset_testempty(arg
.flags
, SHWH
);
695 test_empty
= do_test_empty
;
696 if (au_opt_test(au_mntflags(dentry
->d_sb
), DIRPERM1
))
697 test_empty
= sio_test_empty
;
699 err
= test_empty(dentry
, &arg
);
703 au_fset_testempty(arg
.flags
, WHONLY
);
704 btail
= au_dbtaildir(dentry
);
705 for (bindex
= btop
+ 1; !err
&& bindex
<= btail
; bindex
++) {
706 struct dentry
*h_dentry
;
708 h_dentry
= au_h_dptr(dentry
, bindex
);
709 if (h_dentry
&& d_is_positive(h_dentry
)) {
711 err
= test_empty(dentry
, &arg
);
716 au_nhash_wh_free(&whlist
);
721 int au_test_empty(struct dentry
*dentry
, struct au_nhash
*whlist
)
724 struct test_empty_arg arg
= {
726 .actor
= test_empty_cb
729 aufs_bindex_t bindex
, btail
;
733 arg
.flags
= AuTestEmpty_WHONLY
;
734 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
735 au_fset_testempty(arg
.flags
, SHWH
);
736 btail
= au_dbtaildir(dentry
);
737 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= btail
; bindex
++) {
738 struct dentry
*h_dentry
;
740 h_dentry
= au_h_dptr(dentry
, bindex
);
741 if (h_dentry
&& d_is_positive(h_dentry
)) {
743 err
= sio_test_empty(dentry
, &arg
);
750 /* ---------------------------------------------------------------------- */
752 const struct file_operations aufs_dir_fop
= {
753 .owner
= THIS_MODULE
,
754 .llseek
= default_llseek
,
755 .read
= generic_read_dir
,
756 .iterate_shared
= aufs_iterate_shared
,
757 .unlocked_ioctl
= aufs_ioctl_dir
,
759 .compat_ioctl
= aufs_compat_ioctl_dir
,
761 .open
= aufs_open_dir
,
762 .release
= aufs_release_dir
,
763 .flush
= aufs_flush_dir
,
764 .fsync
= aufs_fsync_dir