2 * Copyright (C) 2005-2013 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)
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)
51 set_nlink(dir, nlink);
54 loff_t au_dir_size(struct file *file, struct dentry *dentry)
57 aufs_bindex_t bindex, bend;
59 struct dentry *h_dentry;
63 AuDebugOn(!file->f_dentry);
64 AuDebugOn(!file->f_dentry->d_inode);
65 AuDebugOn(!S_ISDIR(file->f_dentry->d_inode->i_mode));
67 bend = au_fbend_dir(file);
68 for (bindex = au_fbstart(file);
69 bindex <= bend && sz < KMALLOC_MAX_SIZE;
71 h_file = au_hf_dir(file, bindex);
74 && h_file->f_dentry->d_inode)
75 sz += i_size_read(h_file->f_dentry->d_inode);
79 AuDebugOn(!dentry->d_inode);
80 AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
82 bend = au_dbtaildir(dentry);
83 for (bindex = au_dbstart(dentry);
84 bindex <= bend && sz < KMALLOC_MAX_SIZE;
86 h_dentry = au_h_dptr(dentry, bindex);
87 if (h_dentry && h_dentry->d_inode)
88 sz += i_size_read(h_dentry->d_inode);
91 if (sz < KMALLOC_MAX_SIZE)
92 sz = roundup_pow_of_two(sz);
93 if (sz > KMALLOC_MAX_SIZE)
94 sz = KMALLOC_MAX_SIZE;
95 else if (sz < NAME_MAX) {
96 BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
102 /* ---------------------------------------------------------------------- */
104 static int reopen_dir(struct file *file)
108 aufs_bindex_t bindex, btail, bstart;
109 struct dentry *dentry, *h_dentry;
112 /* open all lower dirs */
113 dentry = file->f_dentry;
114 bstart = au_dbstart(dentry);
115 for (bindex = au_fbstart(file); bindex < bstart; bindex++)
116 au_set_h_fptr(file, bindex, NULL);
117 au_set_fbstart(file, bstart);
119 btail = au_dbtaildir(dentry);
120 for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
121 au_set_h_fptr(file, bindex, NULL);
122 au_set_fbend_dir(file, btail);
124 flags = vfsub_file_flags(file);
125 for (bindex = bstart; bindex <= btail; bindex++) {
126 h_dentry = au_h_dptr(dentry, bindex);
129 h_file = au_hf_dir(file, bindex);
133 h_file = au_h_open(dentry, bindex, flags, file);
134 err = PTR_ERR(h_file);
136 goto out; /* close all? */
137 au_set_h_fptr(file, bindex, h_file);
139 au_update_figen(file);
140 /* todo: necessary? */
141 /* file->f_ra = h_file->f_ra; */
148 static int do_open_dir(struct file *file, int flags)
151 aufs_bindex_t bindex, btail;
152 struct dentry *dentry, *h_dentry;
155 FiMustWriteLock(file);
157 dentry = file->f_dentry;
158 err = au_alive_dir(dentry);
162 file->f_version = dentry->d_inode->i_version;
163 bindex = au_dbstart(dentry);
164 au_set_fbstart(file, bindex);
165 btail = au_dbtaildir(dentry);
166 au_set_fbend_dir(file, btail);
167 for (; !err && bindex <= btail; bindex++) {
168 h_dentry = au_h_dptr(dentry, bindex);
172 h_file = au_h_open(dentry, bindex, flags, file);
173 if (IS_ERR(h_file)) {
174 err = PTR_ERR(h_file);
177 au_set_h_fptr(file, bindex, h_file);
179 au_update_figen(file);
180 /* todo: necessary? */
181 /* file->f_ra = h_file->f_ra; */
183 return 0; /* success */
186 for (bindex = au_fbstart(file); bindex <= btail; bindex++)
187 au_set_h_fptr(file, bindex, NULL);
188 au_set_fbstart(file, -1);
189 au_set_fbend_dir(file, -1);
195 static int aufs_open_dir(struct inode *inode __maybe_unused,
199 struct super_block *sb;
200 struct au_fidir *fidir;
203 sb = file->f_dentry->d_sb;
204 si_read_lock(sb, AuLock_FLUSH);
205 fidir = au_fidir_alloc(sb);
207 err = au_do_open(file, do_open_dir, fidir);
215 static int aufs_release_dir(struct inode *inode __maybe_unused,
218 struct au_vdir *vdir_cache;
219 struct au_finfo *finfo;
220 struct au_fidir *fidir;
221 aufs_bindex_t bindex, bend;
224 fidir = finfo->fi_hdir;
226 /* remove me from sb->s_files */
227 file_sb_list_del(file);
229 vdir_cache = fidir->fd_vdir_cache; /* lock-free */
231 au_vdir_free(vdir_cache);
233 bindex = finfo->fi_btop;
236 * calls fput() instead of filp_close(),
237 * since no dnotify or lock for the lower file.
239 bend = fidir->fd_bbot;
240 for (; bindex <= bend; bindex++)
241 au_set_h_fptr(file, bindex, NULL);
244 finfo->fi_hdir = NULL;
250 /* ---------------------------------------------------------------------- */
252 static int au_do_flush_dir(struct file *file, fl_owner_t id)
255 aufs_bindex_t bindex, bend;
259 bend = au_fbend_dir(file);
260 for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
261 h_file = au_hf_dir(file, bindex);
263 err = vfsub_flush(h_file, id);
268 static int aufs_flush_dir(struct file *file, fl_owner_t id)
270 return au_do_flush(file, id, au_do_flush_dir);
273 /* ---------------------------------------------------------------------- */
275 static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
278 aufs_bindex_t bend, bindex;
280 struct super_block *sb;
284 inode = dentry->d_inode;
286 bend = au_dbend(dentry);
287 for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
290 if (au_test_ro(sb, bindex, inode))
292 h_path.dentry = au_h_dptr(dentry, bindex);
296 h_path.mnt = au_sbr_mnt(sb, bindex);
297 err = vfsub_fsync(NULL, &h_path, datasync);
303 static int au_do_fsync_dir(struct file *file, int datasync)
306 aufs_bindex_t bend, bindex;
308 struct super_block *sb;
311 err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
315 sb = file->f_dentry->d_sb;
316 inode = file->f_dentry->d_inode;
317 bend = au_fbend_dir(file);
318 for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
319 h_file = au_hf_dir(file, bindex);
320 if (!h_file || au_test_ro(sb, bindex, inode))
323 err = vfsub_fsync(h_file, &h_file->f_path, datasync);
333 static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
337 struct dentry *dentry;
338 struct super_block *sb;
342 dentry = file->f_dentry;
343 mtx = &dentry->d_inode->i_mutex;
346 si_noflush_read_lock(sb);
348 err = au_do_fsync_dir(file, datasync);
350 di_write_lock_child(dentry);
351 err = au_do_fsync_dir_no_file(dentry, datasync);
353 au_cpup_attr_timesizes(dentry->d_inode);
354 di_write_unlock(dentry);
356 fi_write_unlock(file);
363 /* ---------------------------------------------------------------------- */
365 static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
368 struct dentry *dentry;
369 struct inode *inode, *h_inode;
370 struct super_block *sb;
372 dentry = file->f_dentry;
373 inode = dentry->d_inode;
377 si_read_lock(sb, AuLock_FLUSH);
378 err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
381 err = au_alive_dir(dentry);
383 err = au_vdir_init(file);
384 di_downgrade_lock(dentry, AuLock_IR);
388 h_inode = au_h_iptr(inode, au_ibstart(inode));
389 if (!au_test_nfsd()) {
390 err = au_vdir_fill_de(file, dirent, filldir);
391 fsstack_copy_attr_atime(inode, h_inode);
394 * nfsd filldir may call lookup_one_len(), vfs_getattr(),
395 * encode_fh() and others.
397 atomic_inc(&h_inode->i_count);
398 di_read_unlock(dentry, AuLock_IR);
400 err = au_vdir_fill_de(file, dirent, filldir);
401 fsstack_copy_attr_atime(inode, h_inode);
402 fi_write_unlock(file);
410 di_read_unlock(dentry, AuLock_IR);
411 fi_write_unlock(file);
417 /* ---------------------------------------------------------------------- */
419 #define AuTestEmpty_WHONLY 1
420 #define AuTestEmpty_CALLED (1 << 1)
421 #define AuTestEmpty_SHWH (1 << 2)
422 #define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
423 #define au_fset_testempty(flags, name) \
424 do { (flags) |= AuTestEmpty_##name; } while (0)
425 #define au_fclr_testempty(flags, name) \
426 do { (flags) &= ~AuTestEmpty_##name; } while (0)
428 #ifndef CONFIG_AUFS_SHWH
429 #undef AuTestEmpty_SHWH
430 #define AuTestEmpty_SHWH 0
433 struct test_empty_arg {
434 struct au_nhash *whlist;
437 aufs_bindex_t bindex;
440 static int test_empty_cb(void *__arg, const char *__name, int namelen,
441 loff_t offset __maybe_unused, u64 ino,
444 struct test_empty_arg *arg = __arg;
445 char *name = (void *)__name;
448 au_fset_testempty(arg->flags, CALLED);
451 && (namelen == 1 || (name[1] == '.' && namelen == 2)))
452 goto out; /* success */
454 if (namelen <= AUFS_WH_PFX_LEN
455 || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
456 if (au_ftest_testempty(arg->flags, WHONLY)
457 && !au_nhash_test_known_wh(arg->whlist, name, namelen))
458 arg->err = -ENOTEMPTY;
462 name += AUFS_WH_PFX_LEN;
463 namelen -= AUFS_WH_PFX_LEN;
464 if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
465 arg->err = au_nhash_append_wh
466 (arg->whlist, name, namelen, ino, d_type, arg->bindex,
467 au_ftest_testempty(arg->flags, SHWH));
471 AuTraceErr(arg->err);
475 static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
480 h_file = au_h_open(dentry, arg->bindex,
481 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
483 err = PTR_ERR(h_file);
488 if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
489 && !h_file->f_dentry->d_inode->i_nlink)
494 au_fclr_testempty(arg->flags, CALLED);
496 err = vfsub_readdir(h_file, test_empty_cb, arg);
499 } while (!err && au_ftest_testempty(arg->flags, CALLED));
503 au_sbr_put(dentry->d_sb, arg->bindex);
508 struct do_test_empty_args {
510 struct dentry *dentry;
511 struct test_empty_arg *arg;
514 static void call_do_test_empty(void *args)
516 struct do_test_empty_args *a = args;
517 *a->errp = do_test_empty(a->dentry, a->arg);
520 static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
523 struct dentry *h_dentry;
524 struct inode *h_inode;
526 h_dentry = au_h_dptr(dentry, arg->bindex);
527 h_inode = h_dentry->d_inode;
528 /* todo: i_mode changes anytime? */
529 mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
530 err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
531 mutex_unlock(&h_inode->i_mutex);
533 err = do_test_empty(dentry, arg);
535 struct do_test_empty_args args = {
540 unsigned int flags = arg->flags;
542 wkq_err = au_wkq_wait(call_do_test_empty, &args);
543 if (unlikely(wkq_err))
551 int au_test_empty_lower(struct dentry *dentry)
555 aufs_bindex_t bindex, bstart, btail;
556 struct au_nhash whlist;
557 struct test_empty_arg arg;
559 SiMustAnyLock(dentry->d_sb);
561 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
563 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
564 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
569 arg.whlist = &whlist;
570 bstart = au_dbstart(dentry);
571 if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
572 au_fset_testempty(arg.flags, SHWH);
574 err = do_test_empty(dentry, &arg);
578 au_fset_testempty(arg.flags, WHONLY);
579 btail = au_dbtaildir(dentry);
580 for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
581 struct dentry *h_dentry;
583 h_dentry = au_h_dptr(dentry, bindex);
584 if (h_dentry && h_dentry->d_inode) {
586 err = do_test_empty(dentry, &arg);
591 au_nhash_wh_free(&whlist);
596 int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
599 struct test_empty_arg arg;
600 aufs_bindex_t bindex, btail;
604 arg.flags = AuTestEmpty_WHONLY;
605 if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
606 au_fset_testempty(arg.flags, SHWH);
607 btail = au_dbtaildir(dentry);
608 for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
609 struct dentry *h_dentry;
611 h_dentry = au_h_dptr(dentry, bindex);
612 if (h_dentry && h_dentry->d_inode) {
614 err = sio_test_empty(dentry, &arg);
621 /* ---------------------------------------------------------------------- */
623 const struct file_operations aufs_dir_fop = {
624 .owner = THIS_MODULE,
625 .llseek = default_llseek,
626 .read = generic_read_dir,
627 .readdir = aufs_readdir,
628 .unlocked_ioctl = aufs_ioctl_dir,
630 .compat_ioctl = aufs_compat_ioctl_dir,
632 .open = aufs_open_dir,
633 .release = aufs_release_dir,
634 .flush = aufs_flush_dir,
635 .fsync = aufs_fsync_dir