2 * Copyright (C) 2005-2016 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
);
159 au_delayed_kfree(arg
);
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
);
195 au_delayed_kfree(arg
);
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
);
314 au_delayed_kfree(fidir
);
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 delayed
= (current
->flags
& PF_KTHREAD
) || in_interrupt();
332 fidir
= finfo
->fi_hdir
;
334 au_sphl_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
, delayed
);
340 bindex
= finfo
->fi_btop
;
342 execed
= vfsub_file_execed(file
);
343 hf
= fidir
->fd_hfile
+ bindex
;
345 * calls fput() instead of filp_close(),
346 * since no dnotify or lock for the lower file.
348 bbot
= fidir
->fd_bbot
;
349 for (; bindex
<= bbot
; bindex
++, hf
++)
351 au_hfput(hf
, execed
);
353 au_delayed_kfree(fidir
);
354 finfo
->fi_hdir
= NULL
;
356 au_finfo_fin(file
, delayed
);
360 /* ---------------------------------------------------------------------- */
362 static int au_do_flush_dir(struct file
*file
, fl_owner_t id
)
365 aufs_bindex_t bindex
, bbot
;
369 bbot
= au_fbbot_dir(file
);
370 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
371 h_file
= au_hf_dir(file
, bindex
);
373 err
= vfsub_flush(h_file
, id
);
378 static int aufs_flush_dir(struct file
*file
, fl_owner_t id
)
380 return au_do_flush(file
, id
, au_do_flush_dir
);
383 /* ---------------------------------------------------------------------- */
385 static int au_do_fsync_dir_no_file(struct dentry
*dentry
, int datasync
)
388 aufs_bindex_t bbot
, bindex
;
390 struct super_block
*sb
;
394 inode
= d_inode(dentry
);
396 bbot
= au_dbbot(dentry
);
397 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= bbot
; bindex
++) {
400 if (au_test_ro(sb
, bindex
, inode
))
402 h_path
.dentry
= au_h_dptr(dentry
, bindex
);
406 h_path
.mnt
= au_sbr_mnt(sb
, bindex
);
407 err
= vfsub_fsync(NULL
, &h_path
, datasync
);
413 static int au_do_fsync_dir(struct file
*file
, int datasync
)
416 aufs_bindex_t bbot
, bindex
;
418 struct super_block
*sb
;
421 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1);
425 inode
= file_inode(file
);
427 bbot
= au_fbbot_dir(file
);
428 for (bindex
= au_fbtop(file
); !err
&& bindex
<= bbot
; bindex
++) {
429 h_file
= au_hf_dir(file
, bindex
);
430 if (!h_file
|| au_test_ro(sb
, bindex
, inode
))
433 err
= vfsub_fsync(h_file
, &h_file
->f_path
, datasync
);
443 static int aufs_fsync_dir(struct file
*file
, loff_t start
, loff_t end
,
447 struct dentry
*dentry
;
449 struct super_block
*sb
;
452 dentry
= file
->f_path
.dentry
;
453 inode
= d_inode(dentry
);
456 si_noflush_read_lock(sb
);
458 err
= au_do_fsync_dir(file
, datasync
);
460 di_write_lock_child(dentry
);
461 err
= au_do_fsync_dir_no_file(dentry
, datasync
);
463 au_cpup_attr_timesizes(inode
);
464 di_write_unlock(dentry
);
466 fi_write_unlock(file
);
473 /* ---------------------------------------------------------------------- */
475 static int aufs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
478 struct dentry
*dentry
;
479 struct inode
*inode
, *h_inode
;
480 struct super_block
*sb
;
482 AuDbg("%pD, ctx{%pf, %llu}\n", file
, ctx
->actor
, ctx
->pos
);
484 dentry
= file
->f_path
.dentry
;
485 inode
= d_inode(dentry
);
489 si_read_lock(sb
, AuLock_FLUSH
);
490 err
= au_reval_and_lock_fdi(file
, reopen_dir
, /*wlock*/1);
493 err
= au_alive_dir(dentry
);
495 err
= au_vdir_init(file
);
496 di_downgrade_lock(dentry
, AuLock_IR
);
500 h_inode
= au_h_iptr(inode
, au_ibtop(inode
));
501 if (!au_test_nfsd()) {
502 err
= au_vdir_fill_de(file
, ctx
);
503 fsstack_copy_attr_atime(inode
, h_inode
);
506 * nfsd filldir may call lookup_one_len(), vfs_getattr(),
507 * encode_fh() and others.
509 atomic_inc(&h_inode
->i_count
);
510 di_read_unlock(dentry
, AuLock_IR
);
512 err
= au_vdir_fill_de(file
, ctx
);
513 fsstack_copy_attr_atime(inode
, h_inode
);
514 fi_write_unlock(file
);
522 di_read_unlock(dentry
, AuLock_IR
);
523 fi_write_unlock(file
);
529 /* ---------------------------------------------------------------------- */
531 #define AuTestEmpty_WHONLY 1
532 #define AuTestEmpty_CALLED (1 << 1)
533 #define AuTestEmpty_SHWH (1 << 2)
534 #define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
535 #define au_fset_testempty(flags, name) \
536 do { (flags) |= AuTestEmpty_##name; } while (0)
537 #define au_fclr_testempty(flags, name) \
538 do { (flags) &= ~AuTestEmpty_##name; } while (0)
540 #ifndef CONFIG_AUFS_SHWH
541 #undef AuTestEmpty_SHWH
542 #define AuTestEmpty_SHWH 0
545 struct test_empty_arg
{
546 struct dir_context ctx
;
547 struct au_nhash
*whlist
;
550 aufs_bindex_t bindex
;
553 static int test_empty_cb(struct dir_context
*ctx
, const char *__name
,
554 int namelen
, loff_t offset __maybe_unused
, u64 ino
,
557 struct test_empty_arg
*arg
= container_of(ctx
, struct test_empty_arg
,
559 char *name
= (void *)__name
;
562 au_fset_testempty(arg
->flags
, CALLED
);
565 && (namelen
== 1 || (name
[1] == '.' && namelen
== 2)))
566 goto out
; /* success */
568 if (namelen
<= AUFS_WH_PFX_LEN
569 || memcmp(name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
)) {
570 if (au_ftest_testempty(arg
->flags
, WHONLY
)
571 && !au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
572 arg
->err
= -ENOTEMPTY
;
576 name
+= AUFS_WH_PFX_LEN
;
577 namelen
-= AUFS_WH_PFX_LEN
;
578 if (!au_nhash_test_known_wh(arg
->whlist
, name
, namelen
))
579 arg
->err
= au_nhash_append_wh
580 (arg
->whlist
, name
, namelen
, ino
, d_type
, arg
->bindex
,
581 au_ftest_testempty(arg
->flags
, SHWH
));
585 AuTraceErr(arg
->err
);
589 static int do_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
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 au_sbr_put(dentry
->d_sb
, arg
->bindex
);
622 struct do_test_empty_args
{
624 struct dentry
*dentry
;
625 struct test_empty_arg
*arg
;
628 static void call_do_test_empty(void *args
)
630 struct do_test_empty_args
*a
= args
;
631 *a
->errp
= do_test_empty(a
->dentry
, a
->arg
);
634 static int sio_test_empty(struct dentry
*dentry
, struct test_empty_arg
*arg
)
637 struct dentry
*h_dentry
;
638 struct inode
*h_inode
;
640 h_dentry
= au_h_dptr(dentry
, arg
->bindex
);
641 h_inode
= d_inode(h_dentry
);
642 /* todo: i_mode changes anytime? */
643 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
644 err
= au_test_h_perm_sio(h_inode
, MAY_EXEC
| MAY_READ
);
645 inode_unlock(h_inode
);
647 err
= do_test_empty(dentry
, arg
);
649 struct do_test_empty_args args
= {
654 unsigned int flags
= arg
->flags
;
656 wkq_err
= au_wkq_wait(call_do_test_empty
, &args
);
657 if (unlikely(wkq_err
))
665 int au_test_empty_lower(struct dentry
*dentry
)
669 aufs_bindex_t bindex
, btop
, btail
;
670 struct au_nhash whlist
;
671 struct test_empty_arg arg
= {
673 .actor
= test_empty_cb
676 int (*test_empty
)(struct dentry
*dentry
, struct test_empty_arg
*arg
);
678 SiMustAnyLock(dentry
->d_sb
);
680 rdhash
= au_sbi(dentry
->d_sb
)->si_rdhash
;
682 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, dentry
));
683 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
688 arg
.whlist
= &whlist
;
689 btop
= au_dbtop(dentry
);
690 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
691 au_fset_testempty(arg
.flags
, SHWH
);
692 test_empty
= do_test_empty
;
693 if (au_opt_test(au_mntflags(dentry
->d_sb
), DIRPERM1
))
694 test_empty
= sio_test_empty
;
696 err
= test_empty(dentry
, &arg
);
700 au_fset_testempty(arg
.flags
, WHONLY
);
701 btail
= au_dbtaildir(dentry
);
702 for (bindex
= btop
+ 1; !err
&& bindex
<= btail
; bindex
++) {
703 struct dentry
*h_dentry
;
705 h_dentry
= au_h_dptr(dentry
, bindex
);
706 if (h_dentry
&& d_is_positive(h_dentry
)) {
708 err
= test_empty(dentry
, &arg
);
713 au_nhash_wh_free(&whlist
);
718 int au_test_empty(struct dentry
*dentry
, struct au_nhash
*whlist
)
721 struct test_empty_arg arg
= {
723 .actor
= test_empty_cb
726 aufs_bindex_t bindex
, btail
;
730 arg
.flags
= AuTestEmpty_WHONLY
;
731 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
))
732 au_fset_testempty(arg
.flags
, SHWH
);
733 btail
= au_dbtaildir(dentry
);
734 for (bindex
= au_dbtop(dentry
); !err
&& bindex
<= btail
; bindex
++) {
735 struct dentry
*h_dentry
;
737 h_dentry
= au_h_dptr(dentry
, bindex
);
738 if (h_dentry
&& d_is_positive(h_dentry
)) {
740 err
= sio_test_empty(dentry
, &arg
);
747 /* ---------------------------------------------------------------------- */
749 const struct file_operations aufs_dir_fop
= {
750 .owner
= THIS_MODULE
,
751 .llseek
= default_llseek
,
752 .read
= generic_read_dir
,
753 .iterate_shared
= aufs_iterate_shared
,
754 .unlocked_ioctl
= aufs_ioctl_dir
,
756 .compat_ioctl
= aufs_compat_ioctl_dir
,
758 .open
= aufs_open_dir
,
759 .release
= aufs_release_dir
,
760 .flush
= aufs_flush_dir
,
761 .fsync
= aufs_fsync_dir