2 * Copyright (C) 2005-2012 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * directory operations
23 #include <linux/fs_stack.h>
26 void au_add_nlink(struct inode *dir, struct inode *h_dir)
30 AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
33 nlink += h_dir->i_nlink - 2;
34 if (h_dir->i_nlink < 2)
36 set_nlink(dir, nlink);
39 void au_sub_nlink(struct inode *dir, struct inode *h_dir)
43 AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
46 nlink -= h_dir->i_nlink - 2;
47 if (h_dir->i_nlink < 2)
49 set_nlink(dir, nlink);
52 loff_t au_dir_size(struct file *file, struct dentry *dentry)
55 aufs_bindex_t bindex, bend;
57 struct dentry *h_dentry;
61 AuDebugOn(!file->f_dentry);
62 AuDebugOn(!file->f_dentry->d_inode);
63 AuDebugOn(!S_ISDIR(file->f_dentry->d_inode->i_mode));
65 bend = au_fbend_dir(file);
66 for (bindex = au_fbstart(file);
67 bindex <= bend && sz < KMALLOC_MAX_SIZE;
69 h_file = au_hf_dir(file, bindex);
72 && h_file->f_dentry->d_inode)
73 sz += i_size_read(h_file->f_dentry->d_inode);
77 AuDebugOn(!dentry->d_inode);
78 AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
80 bend = au_dbtaildir(dentry);
81 for (bindex = au_dbstart(dentry);
82 bindex <= bend && sz < KMALLOC_MAX_SIZE;
84 h_dentry = au_h_dptr(dentry, bindex);
85 if (h_dentry && h_dentry->d_inode)
86 sz += i_size_read(h_dentry->d_inode);
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 /* ---------------------------------------------------------------------- */
102 static int reopen_dir(struct file *file)
106 aufs_bindex_t bindex, btail, bstart;
107 struct dentry *dentry, *h_dentry;
110 /* open all lower dirs */
111 dentry = file->f_dentry;
112 bstart = au_dbstart(dentry);
113 for (bindex = au_fbstart(file); bindex < bstart; bindex++)
114 au_set_h_fptr(file, bindex, NULL);
115 au_set_fbstart(file, bstart);
117 btail = au_dbtaildir(dentry);
118 for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
119 au_set_h_fptr(file, bindex, NULL);
120 au_set_fbend_dir(file, btail);
122 flags = vfsub_file_flags(file);
123 for (bindex = bstart; bindex <= btail; bindex++) {
124 h_dentry = au_h_dptr(dentry, bindex);
127 h_file = au_hf_dir(file, bindex);
131 h_file = au_h_open(dentry, bindex, flags, file);
132 err = PTR_ERR(h_file);
134 goto out; /* close all? */
135 au_set_h_fptr(file, bindex, h_file);
137 au_update_figen(file);
138 /* todo: necessary? */
139 /* file->f_ra = h_file->f_ra; */
146 static int do_open_dir(struct file *file, int flags)
149 aufs_bindex_t bindex, btail;
150 struct dentry *dentry, *h_dentry;
153 FiMustWriteLock(file);
155 dentry = file->f_dentry;
156 err = au_alive_dir(dentry);
160 file->f_version = dentry->d_inode->i_version;
161 bindex = au_dbstart(dentry);
162 au_set_fbstart(file, bindex);
163 btail = au_dbtaildir(dentry);
164 au_set_fbend_dir(file, btail);
165 for (; !err && bindex <= btail; bindex++) {
166 h_dentry = au_h_dptr(dentry, bindex);
170 h_file = au_h_open(dentry, bindex, flags, file);
171 if (IS_ERR(h_file)) {
172 err = PTR_ERR(h_file);
175 au_set_h_fptr(file, bindex, h_file);
177 au_update_figen(file);
178 /* todo: necessary? */
179 /* file->f_ra = h_file->f_ra; */
181 return 0; /* success */
184 for (bindex = au_fbstart(file); bindex <= btail; bindex++)
185 au_set_h_fptr(file, bindex, NULL);
186 au_set_fbstart(file, -1);
187 au_set_fbend_dir(file, -1);
193 static int aufs_open_dir(struct inode *inode __maybe_unused,
197 struct super_block *sb;
198 struct au_fidir *fidir;
201 sb = file->f_dentry->d_sb;
202 si_read_lock(sb, AuLock_FLUSH);
203 fidir = au_fidir_alloc(sb);
205 err = au_do_open(file, do_open_dir, fidir);
213 static int aufs_release_dir(struct inode *inode __maybe_unused,
216 struct au_vdir *vdir_cache;
217 struct au_finfo *finfo;
218 struct au_fidir *fidir;
219 aufs_bindex_t bindex, bend;
222 fidir = finfo->fi_hdir;
224 /* remove me from sb->s_files */
225 file_sb_list_del(file);
227 vdir_cache = fidir->fd_vdir_cache; /* lock-free */
229 au_vdir_free(vdir_cache);
231 bindex = finfo->fi_btop;
234 * calls fput() instead of filp_close(),
235 * since no dnotify or lock for the lower file.
237 bend = fidir->fd_bbot;
238 for (; bindex <= bend; bindex++)
239 au_set_h_fptr(file, bindex, NULL);
242 finfo->fi_hdir = NULL;
248 /* ---------------------------------------------------------------------- */
250 static int au_do_flush_dir(struct file *file, fl_owner_t id)
253 aufs_bindex_t bindex, bend;
257 bend = au_fbend_dir(file);
258 for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
259 h_file = au_hf_dir(file, bindex);
261 err = vfsub_flush(h_file, id);
266 static int aufs_flush_dir(struct file *file, fl_owner_t id)
268 return au_do_flush(file, id, au_do_flush_dir);
271 /* ---------------------------------------------------------------------- */
273 static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
276 aufs_bindex_t bend, bindex;
278 struct super_block *sb;
282 inode = dentry->d_inode;
284 bend = au_dbend(dentry);
285 for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
288 if (au_test_ro(sb, bindex, inode))
290 h_path.dentry = au_h_dptr(dentry, bindex);
294 h_path.mnt = au_sbr_mnt(sb, bindex);
295 err = vfsub_fsync(NULL, &h_path, datasync);
301 static int au_do_fsync_dir(struct file *file, int datasync)
304 aufs_bindex_t bend, bindex;
306 struct super_block *sb;
309 err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
313 sb = file->f_dentry->d_sb;
314 inode = file->f_dentry->d_inode;
315 bend = au_fbend_dir(file);
316 for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
317 h_file = au_hf_dir(file, bindex);
318 if (!h_file || au_test_ro(sb, bindex, inode))
321 err = vfsub_fsync(h_file, &h_file->f_path, datasync);
331 static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
335 struct dentry *dentry;
336 struct super_block *sb;
340 dentry = file->f_dentry;
341 mtx = &dentry->d_inode->i_mutex;
344 si_noflush_read_lock(sb);
346 err = au_do_fsync_dir(file, datasync);
348 di_write_lock_child(dentry);
349 err = au_do_fsync_dir_no_file(dentry, datasync);
351 au_cpup_attr_timesizes(dentry->d_inode);
352 di_write_unlock(dentry);
354 fi_write_unlock(file);
361 /* ---------------------------------------------------------------------- */
363 static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
366 struct dentry *dentry;
367 struct inode *inode, *h_inode;
368 struct super_block *sb;
370 dentry = file->f_dentry;
371 inode = dentry->d_inode;
375 si_read_lock(sb, AuLock_FLUSH);
376 err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
379 err = au_alive_dir(dentry);
381 err = au_vdir_init(file);
382 di_downgrade_lock(dentry, AuLock_IR);
386 h_inode = au_h_iptr(inode, au_ibstart(inode));
387 if (!au_test_nfsd()) {
388 err = au_vdir_fill_de(file, dirent, filldir);
389 fsstack_copy_attr_atime(inode, h_inode);
392 * nfsd filldir may call lookup_one_len(), vfs_getattr(),
393 * encode_fh() and others.
395 atomic_inc(&h_inode->i_count);
396 di_read_unlock(dentry, AuLock_IR);
398 err = au_vdir_fill_de(file, dirent, filldir);
399 fsstack_copy_attr_atime(inode, h_inode);
400 fi_write_unlock(file);
408 di_read_unlock(dentry, AuLock_IR);
409 fi_write_unlock(file);
415 /* ---------------------------------------------------------------------- */
417 #define AuTestEmpty_WHONLY 1
418 #define AuTestEmpty_CALLED (1 << 1)
419 #define AuTestEmpty_SHWH (1 << 2)
420 #define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
421 #define au_fset_testempty(flags, name) \
422 do { (flags) |= AuTestEmpty_##name; } while (0)
423 #define au_fclr_testempty(flags, name) \
424 do { (flags) &= ~AuTestEmpty_##name; } while (0)
426 #ifndef CONFIG_AUFS_SHWH
427 #undef AuTestEmpty_SHWH
428 #define AuTestEmpty_SHWH 0
431 struct test_empty_arg {
432 struct au_nhash *whlist;
435 aufs_bindex_t bindex;
438 static int test_empty_cb(void *__arg, const char *__name, int namelen,
439 loff_t offset __maybe_unused, u64 ino,
442 struct test_empty_arg *arg = __arg;
443 char *name = (void *)__name;
446 au_fset_testempty(arg->flags, CALLED);
449 && (namelen == 1 || (name[1] == '.' && namelen == 2)))
450 goto out; /* success */
452 if (namelen <= AUFS_WH_PFX_LEN
453 || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
454 if (au_ftest_testempty(arg->flags, WHONLY)
455 && !au_nhash_test_known_wh(arg->whlist, name, namelen))
456 arg->err = -ENOTEMPTY;
460 name += AUFS_WH_PFX_LEN;
461 namelen -= AUFS_WH_PFX_LEN;
462 if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
463 arg->err = au_nhash_append_wh
464 (arg->whlist, name, namelen, ino, d_type, arg->bindex,
465 au_ftest_testempty(arg->flags, SHWH));
469 AuTraceErr(arg->err);
473 static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
478 h_file = au_h_open(dentry, arg->bindex,
479 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
481 err = PTR_ERR(h_file);
486 if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
487 && !h_file->f_dentry->d_inode->i_nlink)
492 au_fclr_testempty(arg->flags, CALLED);
494 err = vfsub_readdir(h_file, test_empty_cb, arg);
497 } while (!err && au_ftest_testempty(arg->flags, CALLED));
501 au_sbr_put(dentry->d_sb, arg->bindex);
506 struct do_test_empty_args {
508 struct dentry *dentry;
509 struct test_empty_arg *arg;
512 static void call_do_test_empty(void *args)
514 struct do_test_empty_args *a = args;
515 *a->errp = do_test_empty(a->dentry, a->arg);
518 static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
521 struct dentry *h_dentry;
522 struct inode *h_inode;
524 h_dentry = au_h_dptr(dentry, arg->bindex);
525 h_inode = h_dentry->d_inode;
526 /* todo: i_mode changes anytime? */
527 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
528 err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
529 mutex_unlock(&h_inode->i_mutex);
531 err = do_test_empty(dentry, arg);
533 struct do_test_empty_args args = {
538 unsigned int flags = arg->flags;
540 wkq_err = au_wkq_wait(call_do_test_empty, &args);
541 if (unlikely(wkq_err))
549 int au_test_empty_lower(struct dentry *dentry)
553 aufs_bindex_t bindex, bstart, btail;
554 struct au_nhash whlist;
555 struct test_empty_arg arg;
557 SiMustAnyLock(dentry->d_sb);
559 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
561 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
562 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
567 arg.whlist = &whlist;
568 bstart = au_dbstart(dentry);
569 if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
570 au_fset_testempty(arg.flags, SHWH);
572 err = do_test_empty(dentry, &arg);
576 au_fset_testempty(arg.flags, WHONLY);
577 btail = au_dbtaildir(dentry);
578 for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
579 struct dentry *h_dentry;
581 h_dentry = au_h_dptr(dentry, bindex);
582 if (h_dentry && h_dentry->d_inode) {
584 err = do_test_empty(dentry, &arg);
589 au_nhash_wh_free(&whlist);
594 int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
597 struct test_empty_arg arg;
598 aufs_bindex_t bindex, btail;
602 arg.flags = AuTestEmpty_WHONLY;
603 if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
604 au_fset_testempty(arg.flags, SHWH);
605 btail = au_dbtaildir(dentry);
606 for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
607 struct dentry *h_dentry;
609 h_dentry = au_h_dptr(dentry, bindex);
610 if (h_dentry && h_dentry->d_inode) {
612 err = sio_test_empty(dentry, &arg);
619 /* ---------------------------------------------------------------------- */
621 const struct file_operations aufs_dir_fop = {
622 .owner = THIS_MODULE,
623 .llseek = default_llseek,
624 .read = generic_read_dir,
625 .readdir = aufs_readdir,
626 .unlocked_ioctl = aufs_ioctl_dir,
628 .compat_ioctl = aufs_compat_ioctl_dir,
630 .open = aufs_open_dir,
631 .release = aufs_release_dir,
632 .flush = aufs_flush_dir,
633 .fsync = aufs_fsync_dir