2 * Copyright (C) 2005-2017 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * directory operations
22 #include <linux/fs_stack.h>
25 void au_add_nlink(struct inode
*dir
, struct inode
*h_dir
)
29 AuDebugOn(!S_ISDIR(dir
->i_mode
) || !S_ISDIR(h_dir
->i_mode
));
32 nlink
+= h_dir
->i_nlink
- 2;
33 if (h_dir
->i_nlink
< 2)
35 smp_mb(); /* for i_nlink */
36 /* 0 can happen in revaliding */
37 set_nlink(dir
, nlink
);
40 void au_sub_nlink(struct inode
*dir
, struct inode
*h_dir
)
44 AuDebugOn(!S_ISDIR(dir
->i_mode
) || !S_ISDIR(h_dir
->i_mode
));
47 nlink
-= h_dir
->i_nlink
- 2;
48 if (h_dir
->i_nlink
< 2)
50 smp_mb(); /* for i_nlink */
51 /* nlink == 0 means the branch-fs is broken */
52 set_nlink(dir
, nlink
);
55 loff_t
au_dir_size(struct file
*file
, struct dentry
*dentry
)
58 aufs_bindex_t bindex
, bbot
;
60 struct dentry
*h_dentry
;
64 AuDebugOn(!d_is_dir(file
->f_path
.dentry
));
66 bbot
= au_fbbot_dir(file
);
67 for (bindex
= au_fbtop(file
);
68 bindex
<= bbot
&& sz
< KMALLOC_MAX_SIZE
;
70 h_file
= au_hf_dir(file
, bindex
);
71 if (h_file
&& file_inode(h_file
))
72 sz
+= vfsub_f_size_read(h_file
);
76 AuDebugOn(!d_is_dir(dentry
));
78 bbot
= au_dbtaildir(dentry
);
79 for (bindex
= au_dbtop(dentry
);
80 bindex
<= bbot
&& sz
< KMALLOC_MAX_SIZE
;
82 h_dentry
= au_h_dptr(dentry
, bindex
);
83 if (h_dentry
&& d_is_positive(h_dentry
))
84 sz
+= i_size_read(d_inode(h_dentry
));
87 if (sz
< KMALLOC_MAX_SIZE
)
88 sz
= roundup_pow_of_two(sz
);
89 if (sz
> KMALLOC_MAX_SIZE
)
90 sz
= KMALLOC_MAX_SIZE
;
91 else if (sz
< NAME_MAX
) {
92 BUILD_BUG_ON(AUFS_RDBLK_DEF
< NAME_MAX
);
98 struct au_dir_ts_arg
{
99 struct dentry
*dentry
;
103 static void au_do_dir_ts(void *arg
)
105 struct au_dir_ts_arg
*a
= arg
;
108 struct inode
*dir
, *h_dir
;
109 struct super_block
*sb
;
110 struct au_branch
*br
;
111 struct au_hinode
*hdir
;
113 aufs_bindex_t btop
, bindex
;
115 sb
= a
->dentry
->d_sb
;
116 if (d_really_is_negative(a
->dentry
))
118 /* no dir->i_mutex lock */
119 aufs_read_lock(a
->dentry
, AuLock_DW
); /* noflush */
121 dir
= d_inode(a
->dentry
);
122 btop
= au_ibtop(dir
);
123 bindex
= au_br_index(sb
, a
->brid
);
127 br
= au_sbr(sb
, bindex
);
128 h_path
.dentry
= au_h_dptr(a
->dentry
, bindex
);
131 h_path
.mnt
= au_br_mnt(br
);
132 au_dtime_store(&dt
, a
->dentry
, &h_path
);
134 br
= au_sbr(sb
, btop
);
135 if (!au_br_writable(br
->br_perm
))
137 h_path
.dentry
= au_h_dptr(a
->dentry
, btop
);
138 h_path
.mnt
= au_br_mnt(br
);
139 err
= vfsub_mnt_want_write(h_path
.mnt
);
142 hdir
= au_hi(dir
, btop
);
143 au_hn_inode_lock_nested(hdir
, AuLsc_I_PARENT
);
144 h_dir
= au_h_iptr(dir
, btop
);
146 && timespec_compare(&h_dir
->i_mtime
, &dt
.dt_mtime
) < 0) {
147 dt
.dt_h_path
= h_path
;
148 au_dtime_revert(&dt
);
150 au_hn_inode_unlock(hdir
);
151 vfsub_mnt_drop_write(h_path
.mnt
);
152 au_cpup_attr_timesizes(dir
);
155 aufs_read_unlock(a
->dentry
, AuLock_DW
);
158 au_nwt_done(&au_sbi(sb
)->si_nowait
);
162 void au_dir_ts(struct inode
*dir
, aufs_bindex_t bindex
)
166 struct au_dir_ts_arg
*arg
;
167 struct dentry
*dentry
;
168 struct super_block
*sb
;
172 dentry
= d_find_any_alias(dir
);
175 btop
= au_ibtop(dir
);
176 if (btop
== bindex
) {
177 au_cpup_attr_timesizes(dir
);
181 perm
= au_sbr_perm(sb
, btop
);
182 if (!au_br_writable(perm
))
185 arg
= kmalloc(sizeof(*arg
), GFP_NOFS
);
189 arg
->dentry
= dget(dentry
); /* will be dput-ted by au_do_dir_ts() */
190 arg
->brid
= au_sbr_id(sb
, bindex
);
191 wkq_err
= au_wkq_nowait(au_do_dir_ts
, arg
, sb
, /*flags*/0);
192 if (unlikely(wkq_err
)) {
193 pr_err("wkq %d\n", wkq_err
);
202 /* ---------------------------------------------------------------------- */
204 static int reopen_dir(struct file
*file
)
208 aufs_bindex_t bindex
, btail
, btop
;
209 struct dentry
*dentry
, *h_dentry
;
212 /* open all lower dirs */
213 dentry
= file
->f_path
.dentry
;
214 btop
= au_dbtop(dentry
);
215 for (bindex
= au_fbtop(file
); bindex
< btop
; bindex
++)
216 au_set_h_fptr(file
, bindex
, NULL
);
217 au_set_fbtop(file
, btop
);
219 btail
= au_dbtaildir(dentry
);
220 for (bindex
= au_fbbot_dir(file
); btail
< bindex
; bindex
--)
221 au_set_h_fptr(file
, bindex
, NULL
);
222 au_set_fbbot_dir(file
, btail
);
224 flags
= vfsub_file_flags(file
);
225 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
226 h_dentry
= au_h_dptr(dentry
, bindex
);
229 h_file
= au_hf_dir(file
, bindex
);
233 h_file
= au_h_open(dentry
, bindex
, flags
, file
, /*force_wr*/0);
234 err
= PTR_ERR(h_file
);
236 goto out
; /* close all? */
237 au_set_h_fptr(file
, bindex
, h_file
);
239 au_update_figen(file
);
240 /* todo: necessary? */
241 /* file->f_ra = h_file->f_ra; */
248 static int do_open_dir(struct file
*file
, int flags
, struct file
*h_file
)
251 aufs_bindex_t bindex
, btail
;
252 struct dentry
*dentry
, *h_dentry
;
253 struct vfsmount
*mnt
;
255 FiMustWriteLock(file
);
259 mnt
= file
->f_path
.mnt
;
260 dentry
= file
->f_path
.dentry
;
261 file
->f_version
= d_inode(dentry
)->i_version
;
262 bindex
= au_dbtop(dentry
);
263 au_set_fbtop(file
, bindex
);
264 btail
= au_dbtaildir(dentry
);
265 au_set_fbbot_dir(file
, btail
);
266 for (; !err
&& bindex
<= btail
; bindex
++) {
267 h_dentry
= au_h_dptr(dentry
, bindex
);
271 err
= vfsub_test_mntns(mnt
, h_dentry
->d_sb
);
274 h_file
= au_h_open(dentry
, bindex
, flags
, file
, /*force_wr*/0);
275 if (IS_ERR(h_file
)) {
276 err
= PTR_ERR(h_file
);
279 au_set_h_fptr(file
, bindex
, h_file
);
281 au_update_figen(file
);
282 /* todo: necessary? */
283 /* file->f_ra = h_file->f_ra; */
285 return 0; /* success */
288 for (bindex
= au_fbtop(file
); bindex
<= btail
; bindex
++)
289 au_set_h_fptr(file
, bindex
, NULL
);
290 au_set_fbtop(file
, -1);
291 au_set_fbbot_dir(file
, -1);
296 static int aufs_open_dir(struct inode
*inode __maybe_unused
,
300 struct super_block
*sb
;
301 struct au_fidir
*fidir
;
304 sb
= file
->f_path
.dentry
->d_sb
;
305 si_read_lock(sb
, AuLock_FLUSH
);
306 fidir
= au_fidir_alloc(sb
);
308 struct au_do_open_args args
= {
312 err
= au_do_open(file
, &args
);
320 static int aufs_release_dir(struct inode
*inode __maybe_unused
,
323 struct au_vdir
*vdir_cache
;
324 struct au_finfo
*finfo
;
325 struct au_fidir
*fidir
;
327 aufs_bindex_t bindex
, bbot
;
330 fidir
= finfo
->fi_hdir
;
332 au_hbl_del(&finfo
->fi_hlist
,
333 &au_sbi(file
->f_path
.dentry
->d_sb
)->si_files
);
334 vdir_cache
= fidir
->fd_vdir_cache
; /* lock-free */
336 au_vdir_free(vdir_cache
);
338 bindex
= finfo
->fi_btop
;
340 hf
= fidir
->fd_hfile
+ bindex
;
342 * calls fput() instead of filp_close(),
343 * since no dnotify or lock for the lower file.
345 bbot
= fidir
->fd_bbot
;
346 for (; bindex
<= bbot
; bindex
++, hf
++)
348 au_hfput(hf
, /*execed*/0);
351 finfo
->fi_hdir
= NULL
;
357 /* ---------------------------------------------------------------------- */
359 static int au_do_flush_dir(struct file
*file
, fl_owner_t id
)
362 aufs_bindex_t bindex
, bbot
;
366 bbot
= au_fbbot_dir(file
);
367 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
368 h_file
= au_hf_dir(file
, bindex
);
370 err
= vfsub_flush(h_file
, id
);
375 static int aufs_flush_dir(struct file
*file
, fl_owner_t id
)
377 return au_do_flush(file
, id
, au_do_flush_dir
);
380 /* ---------------------------------------------------------------------- */
382 static int au_do_fsync_dir_no_file(struct dentry
*dentry
, int datasync
)
385 aufs_bindex_t bbot
, bindex
;
387 struct super_block
*sb
;
391 inode
= d_inode(dentry
);
393 bbot
= au_dbbot(dentry
);
394 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= bbot
; bindex
++) {
397 if (au_test_ro(sb
, bindex
, inode
))
399 h_path
.dentry
= au_h_dptr(dentry
, bindex
);
403 h_path
.mnt
= au_sbr_mnt(sb
, bindex
);
404 err
= vfsub_fsync(NULL
, &h_path
, datasync
);
410 static int au_do_fsync_dir(struct file
*file
, int datasync
)
413 aufs_bindex_t bbot
, bindex
;
415 struct super_block
*sb
;
418 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1, /*fi_lsc*/0);
422 inode
= file_inode(file
);
424 bbot
= au_fbbot_dir(file
);
425 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
426 h_file
= au_hf_dir(file
, bindex
);
427 if (!h_file
|| au_test_ro(sb
, bindex
, inode
))
430 err
= vfsub_fsync(h_file
, &h_file
->f_path
, datasync
);
440 static int aufs_fsync_dir(struct file
*file
, loff_t start
, loff_t end
,
444 struct dentry
*dentry
;
446 struct super_block
*sb
;
449 dentry
= file
->f_path
.dentry
;
450 inode
= d_inode(dentry
);
453 si_noflush_read_lock(sb
);
455 err
= au_do_fsync_dir(file
, datasync
);
457 di_write_lock_child(dentry
);
458 err
= au_do_fsync_dir_no_file(dentry
, datasync
);
460 au_cpup_attr_timesizes(inode
);
461 di_write_unlock(dentry
);
463 fi_write_unlock(file
);
470 /* ---------------------------------------------------------------------- */
472 static int aufs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
475 struct dentry
*dentry
;
476 struct inode
*inode
, *h_inode
;
477 struct super_block
*sb
;
479 AuDbg("%pD, ctx{%pf, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
481 dentry
= file
->f_path
.dentry
;
482 inode
= d_inode(dentry
);
486 si_read_lock(sb
, AuLock_FLUSH
);
487 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1, /*fi_lsc*/0);
490 err
= au_alive_dir(dentry
);
492 err
= au_vdir_init(file
);
493 di_downgrade_lock(dentry
, AuLock_IR
);
497 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
498 if (!au_test_nfsd()) {
499 err
= au_vdir_fill_de(file
, ctx
);
500 fsstack_copy_attr_atime(inode
, h_inode
);
503 * nfsd filldir may call lookup_one_len(), vfs_getattr(),
504 * encode_fh() and others.
506 atomic_inc(&h_inode
->i_count
);
507 di_read_unlock(dentry
, AuLock_IR
);
509 err
= au_vdir_fill_de(file
, ctx
);
510 fsstack_copy_attr_atime(inode
, h_inode
);
511 fi_write_unlock(file
);
519 di_read_unlock(dentry
, AuLock_IR
);
520 fi_write_unlock(file
);
526 /* ---------------------------------------------------------------------- */
528 #define AuTestEmpty_WHONLY 1
529 #define AuTestEmpty_CALLED (1 << 1)
530 #define AuTestEmpty_SHWH (1 << 2)
531 #define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
532 #define au_fset_testempty(flags, name) \
533 do { (flags) |= AuTestEmpty_##name; } while (0)
534 #define au_fclr_testempty(flags, name) \
535 do { (flags) &= ~AuTestEmpty_##name; } while (0)
537 #ifndef CONFIG_AUFS_SHWH
538 #undef AuTestEmpty_SHWH
539 #define AuTestEmpty_SHWH 0
542 struct test_empty_arg
{
543 struct dir_context ctx
;
544 struct au_nhash
*whlist
;
547 aufs_bindex_t bindex
;
550 static int test_empty_cb(struct dir_context
*ctx
, const char *__name
,
551 int namelen
, loff_t offset __maybe_unused
, u64 ino
,
554 struct test_empty_arg
*arg
= container_of(ctx
, struct test_empty_arg
,
556 char *name
= (void *)__name
;
559 au_fset_testempty(arg
->flags
, CALLED
);
562 && (namelen
== 1 || (name
[1] == '.' && namelen
== 2)))
563 goto out
; /* success */
565 if (namelen
<= AUFS_WH_PFX_LEN
566 || memcmp(name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
)) {
567 if (au_ftest_testempty(arg
->flags
, WHONLY
)
568 && !au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
569 arg
->err
= -ENOTEMPTY
;
573 name
+= AUFS_WH_PFX_LEN
;
574 namelen
-= AUFS_WH_PFX_LEN
;
575 if (!au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
576 arg
->err
= au_nhash_append_wh
577 (arg
->whlist
, name
, namelen
, ino
, d_type
, arg
->bindex
,
578 au_ftest_testempty(arg
->flags
, SHWH
));
582 AuTraceErr(arg
->err
);
586 static int do_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
591 h_file
= au_h_open(dentry
, arg
->bindex
,
592 O_RDONLY
| O_NONBLOCK
| O_DIRECTORY
| O_LARGEFILE
,
593 /*file*/NULL
, /*force_wr*/0);
594 err
= PTR_ERR(h_file
);
599 if (!au_opt_test(au_mntflags(dentry
->d_sb
), UDBA_NONE
)
600 && !file_inode(h_file
)->i_nlink
)
605 au_fclr_testempty(arg
->flags
, CALLED
);
607 err
= vfsub_iterate_dir(h_file
, &arg
->ctx
);
610 } while (!err
&& au_ftest_testempty(arg
->flags
, CALLED
));
614 au_sbr_put(dentry
->d_sb
, arg
->bindex
);
619 struct do_test_empty_args
{
621 struct dentry
*dentry
;
622 struct test_empty_arg
*arg
;
625 static void call_do_test_empty(void *args
)
627 struct do_test_empty_args
*a
= args
;
628 *a
->errp
= do_test_empty(a
->dentry
, a
->arg
);
631 static int sio_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
634 struct dentry
*h_dentry
;
635 struct inode
*h_inode
;
637 h_dentry
= au_h_dptr(dentry
, arg
->bindex
);
638 h_inode
= d_inode(h_dentry
);
639 /* todo: i_mode changes anytime? */
640 vfsub_inode_lock_shared_nested(h_inode
, AuLsc_I_CHILD
);
641 err
= au_test_h_perm_sio(h_inode
, MAY_EXEC
| MAY_READ
);
642 inode_unlock_shared(h_inode
);
644 err
= do_test_empty(dentry
, arg
);
646 struct do_test_empty_args args
= {
651 unsigned int flags
= arg
->flags
;
653 wkq_err
= au_wkq_wait(call_do_test_empty
, &args
);
654 if (unlikely(wkq_err
))
662 int au_test_empty_lower(struct dentry
*dentry
)
666 aufs_bindex_t bindex
, btop
, btail
;
667 struct au_nhash whlist
;
668 struct test_empty_arg arg
= {
670 .actor
= test_empty_cb
673 int (*test_empty
)(struct dentry
*dentry
, struct test_empty_arg
*arg
);
675 SiMustAnyLock(dentry
->d_sb
);
677 rdhash
= au_sbi(dentry
->d_sb
)->si_rdhash
;
679 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, dentry
));
680 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
685 arg
.whlist
= &whlist
;
686 btop
= au_dbtop(dentry
);
687 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
688 au_fset_testempty(arg
.flags
, SHWH
);
689 test_empty
= do_test_empty
;
690 if (au_opt_test(au_mntflags(dentry
->d_sb
), DIRPERM1
))
691 test_empty
= sio_test_empty
;
693 err
= test_empty(dentry
, &arg
);
697 au_fset_testempty(arg
.flags
, WHONLY
);
698 btail
= au_dbtaildir(dentry
);
699 for (bindex
= btop
+ 1; !err
&& bindex
<= btail
; bindex
++) {
700 struct dentry
*h_dentry
;
702 h_dentry
= au_h_dptr(dentry
, bindex
);
703 if (h_dentry
&& d_is_positive(h_dentry
)) {
705 err
= test_empty(dentry
, &arg
);
710 au_nhash_wh_free(&whlist
);
715 int au_test_empty(struct dentry
*dentry
, struct au_nhash
*whlist
)
718 struct test_empty_arg arg
= {
720 .actor
= test_empty_cb
723 aufs_bindex_t bindex
, btail
;
727 arg
.flags
= AuTestEmpty_WHONLY
;
728 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
729 au_fset_testempty(arg
.flags
, SHWH
);
730 btail
= au_dbtaildir(dentry
);
731 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= btail
; bindex
++) {
732 struct dentry
*h_dentry
;
734 h_dentry
= au_h_dptr(dentry
, bindex
);
735 if (h_dentry
&& d_is_positive(h_dentry
)) {
737 err
= sio_test_empty(dentry
, &arg
);
744 /* ---------------------------------------------------------------------- */
746 const struct file_operations aufs_dir_fop
= {
747 .owner
= THIS_MODULE
,
748 .llseek
= default_llseek
,
749 .read
= generic_read_dir
,
750 .iterate_shared
= aufs_iterate_shared
,
751 .unlocked_ioctl
= aufs_ioctl_dir
,
753 .compat_ioctl
= aufs_compat_ioctl_dir
,
755 .open
= aufs_open_dir
,
756 .release
= aufs_release_dir
,
757 .flush
= aufs_flush_dir
,
758 .fsync
= aufs_fsync_dir