update aufs to it's latest standalone 3.2 branch
[pandora-kernel.git] / fs / aufs / dir.c
1 /*
2  * Copyright (C) 2005-2013 Junjiro R. Okajima
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /*
20  * directory operations
21  */
22
23 #include <linux/fs_stack.h>
24 #include "aufs.h"
25
26 void au_add_nlink(struct inode *dir, struct inode *h_dir)
27 {
28         unsigned int nlink;
29
30         AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
31
32         nlink = dir->i_nlink;
33         nlink += h_dir->i_nlink - 2;
34         if (h_dir->i_nlink < 2)
35                 nlink += 2;
36         smp_mb();
37         set_nlink(dir, nlink);
38 }
39
40 void au_sub_nlink(struct inode *dir, struct inode *h_dir)
41 {
42         unsigned int nlink;
43
44         AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
45
46         nlink = dir->i_nlink;
47         nlink -= h_dir->i_nlink - 2;
48         if (h_dir->i_nlink < 2)
49                 nlink -= 2;
50         smp_mb();
51         set_nlink(dir, nlink);
52 }
53
54 loff_t au_dir_size(struct file *file, struct dentry *dentry)
55 {
56         loff_t sz;
57         aufs_bindex_t bindex, bend;
58         struct file *h_file;
59         struct dentry *h_dentry;
60
61         sz = 0;
62         if (file) {
63                 AuDebugOn(!file->f_dentry);
64                 AuDebugOn(!file->f_dentry->d_inode);
65                 AuDebugOn(!S_ISDIR(file->f_dentry->d_inode->i_mode));
66
67                 bend = au_fbend_dir(file);
68                 for (bindex = au_fbstart(file);
69                      bindex <= bend && sz < KMALLOC_MAX_SIZE;
70                      bindex++) {
71                         h_file = au_hf_dir(file, bindex);
72                         if (h_file
73                             && h_file->f_dentry
74                             && h_file->f_dentry->d_inode)
75                                 sz += i_size_read(h_file->f_dentry->d_inode);
76                 }
77         } else {
78                 AuDebugOn(!dentry);
79                 AuDebugOn(!dentry->d_inode);
80                 AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
81
82                 bend = au_dbtaildir(dentry);
83                 for (bindex = au_dbstart(dentry);
84                      bindex <= bend && sz < KMALLOC_MAX_SIZE;
85                      bindex++) {
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);
89                 }
90         }
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);
97                 sz = AUFS_RDBLK_DEF;
98         }
99         return sz;
100 }
101
102 /* ---------------------------------------------------------------------- */
103
104 static int reopen_dir(struct file *file)
105 {
106         int err;
107         unsigned int flags;
108         aufs_bindex_t bindex, btail, bstart;
109         struct dentry *dentry, *h_dentry;
110         struct file *h_file;
111
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);
118
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);
123
124         flags = vfsub_file_flags(file);
125         for (bindex = bstart; bindex <= btail; bindex++) {
126                 h_dentry = au_h_dptr(dentry, bindex);
127                 if (!h_dentry)
128                         continue;
129                 h_file = au_hf_dir(file, bindex);
130                 if (h_file)
131                         continue;
132
133                 h_file = au_h_open(dentry, bindex, flags, file);
134                 err = PTR_ERR(h_file);
135                 if (IS_ERR(h_file))
136                         goto out; /* close all? */
137                 au_set_h_fptr(file, bindex, h_file);
138         }
139         au_update_figen(file);
140         /* todo: necessary? */
141         /* file->f_ra = h_file->f_ra; */
142         err = 0;
143
144 out:
145         return err;
146 }
147
148 static int do_open_dir(struct file *file, int flags)
149 {
150         int err;
151         aufs_bindex_t bindex, btail;
152         struct dentry *dentry, *h_dentry;
153         struct file *h_file;
154
155         FiMustWriteLock(file);
156
157         dentry = file->f_dentry;
158         err = au_alive_dir(dentry);
159         if (unlikely(err))
160                 goto out;
161
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);
169                 if (!h_dentry)
170                         continue;
171
172                 h_file = au_h_open(dentry, bindex, flags, file);
173                 if (IS_ERR(h_file)) {
174                         err = PTR_ERR(h_file);
175                         break;
176                 }
177                 au_set_h_fptr(file, bindex, h_file);
178         }
179         au_update_figen(file);
180         /* todo: necessary? */
181         /* file->f_ra = h_file->f_ra; */
182         if (!err)
183                 return 0; /* success */
184
185         /* close all */
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);
190
191 out:
192         return err;
193 }
194
195 static int aufs_open_dir(struct inode *inode __maybe_unused,
196                          struct file *file)
197 {
198         int err;
199         struct super_block *sb;
200         struct au_fidir *fidir;
201
202         err = -ENOMEM;
203         sb = file->f_dentry->d_sb;
204         si_read_lock(sb, AuLock_FLUSH);
205         fidir = au_fidir_alloc(sb);
206         if (fidir) {
207                 err = au_do_open(file, do_open_dir, fidir);
208                 if (unlikely(err))
209                         kfree(fidir);
210         }
211         si_read_unlock(sb);
212         return err;
213 }
214
215 static int aufs_release_dir(struct inode *inode __maybe_unused,
216                             struct file *file)
217 {
218         struct au_vdir *vdir_cache;
219         struct au_finfo *finfo;
220         struct au_fidir *fidir;
221         aufs_bindex_t bindex, bend;
222
223         finfo = au_fi(file);
224         fidir = finfo->fi_hdir;
225         if (fidir) {
226                 /* remove me from sb->s_files */
227                 file_sb_list_del(file);
228
229                 vdir_cache = fidir->fd_vdir_cache; /* lock-free */
230                 if (vdir_cache)
231                         au_vdir_free(vdir_cache);
232
233                 bindex = finfo->fi_btop;
234                 if (bindex >= 0) {
235                         /*
236                          * calls fput() instead of filp_close(),
237                          * since no dnotify or lock for the lower file.
238                          */
239                         bend = fidir->fd_bbot;
240                         for (; bindex <= bend; bindex++)
241                                 au_set_h_fptr(file, bindex, NULL);
242                 }
243                 kfree(fidir);
244                 finfo->fi_hdir = NULL;
245         }
246         au_finfo_fin(file);
247         return 0;
248 }
249
250 /* ---------------------------------------------------------------------- */
251
252 static int au_do_flush_dir(struct file *file, fl_owner_t id)
253 {
254         int err;
255         aufs_bindex_t bindex, bend;
256         struct file *h_file;
257
258         err = 0;
259         bend = au_fbend_dir(file);
260         for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
261                 h_file = au_hf_dir(file, bindex);
262                 if (h_file)
263                         err = vfsub_flush(h_file, id);
264         }
265         return err;
266 }
267
268 static int aufs_flush_dir(struct file *file, fl_owner_t id)
269 {
270         return au_do_flush(file, id, au_do_flush_dir);
271 }
272
273 /* ---------------------------------------------------------------------- */
274
275 static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
276 {
277         int err;
278         aufs_bindex_t bend, bindex;
279         struct inode *inode;
280         struct super_block *sb;
281
282         err = 0;
283         sb = dentry->d_sb;
284         inode = dentry->d_inode;
285         IMustLock(inode);
286         bend = au_dbend(dentry);
287         for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
288                 struct path h_path;
289
290                 if (au_test_ro(sb, bindex, inode))
291                         continue;
292                 h_path.dentry = au_h_dptr(dentry, bindex);
293                 if (!h_path.dentry)
294                         continue;
295
296                 h_path.mnt = au_sbr_mnt(sb, bindex);
297                 err = vfsub_fsync(NULL, &h_path, datasync);
298         }
299
300         return err;
301 }
302
303 static int au_do_fsync_dir(struct file *file, int datasync)
304 {
305         int err;
306         aufs_bindex_t bend, bindex;
307         struct file *h_file;
308         struct super_block *sb;
309         struct inode *inode;
310
311         err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
312         if (unlikely(err))
313                 goto out;
314
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))
321                         continue;
322
323                 err = vfsub_fsync(h_file, &h_file->f_path, datasync);
324         }
325
326 out:
327         return err;
328 }
329
330 /*
331  * @file may be NULL
332  */
333 static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
334                           int datasync)
335 {
336         int err;
337         struct dentry *dentry;
338         struct super_block *sb;
339         struct mutex *mtx;
340
341         err = 0;
342         dentry = file->f_dentry;
343         mtx = &dentry->d_inode->i_mutex;
344         mutex_lock(mtx);
345         sb = dentry->d_sb;
346         si_noflush_read_lock(sb);
347         if (file)
348                 err = au_do_fsync_dir(file, datasync);
349         else {
350                 di_write_lock_child(dentry);
351                 err = au_do_fsync_dir_no_file(dentry, datasync);
352         }
353         au_cpup_attr_timesizes(dentry->d_inode);
354         di_write_unlock(dentry);
355         if (file)
356                 fi_write_unlock(file);
357
358         si_read_unlock(sb);
359         mutex_unlock(mtx);
360         return err;
361 }
362
363 /* ---------------------------------------------------------------------- */
364
365 static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
366 {
367         int err;
368         struct dentry *dentry;
369         struct inode *inode, *h_inode;
370         struct super_block *sb;
371
372         dentry = file->f_dentry;
373         inode = dentry->d_inode;
374         IMustLock(inode);
375
376         sb = dentry->d_sb;
377         si_read_lock(sb, AuLock_FLUSH);
378         err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
379         if (unlikely(err))
380                 goto out;
381         err = au_alive_dir(dentry);
382         if (!err)
383                 err = au_vdir_init(file);
384         di_downgrade_lock(dentry, AuLock_IR);
385         if (unlikely(err))
386                 goto out_unlock;
387
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);
392         } else {
393                 /*
394                  * nfsd filldir may call lookup_one_len(), vfs_getattr(),
395                  * encode_fh() and others.
396                  */
397                 atomic_inc(&h_inode->i_count);
398                 di_read_unlock(dentry, AuLock_IR);
399                 si_read_unlock(sb);
400                 err = au_vdir_fill_de(file, dirent, filldir);
401                 fsstack_copy_attr_atime(inode, h_inode);
402                 fi_write_unlock(file);
403                 iput(h_inode);
404
405                 AuTraceErr(err);
406                 return err;
407         }
408
409 out_unlock:
410         di_read_unlock(dentry, AuLock_IR);
411         fi_write_unlock(file);
412 out:
413         si_read_unlock(sb);
414         return err;
415 }
416
417 /* ---------------------------------------------------------------------- */
418
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)
427
428 #ifndef CONFIG_AUFS_SHWH
429 #undef AuTestEmpty_SHWH
430 #define AuTestEmpty_SHWH        0
431 #endif
432
433 struct test_empty_arg {
434         struct au_nhash *whlist;
435         unsigned int flags;
436         int err;
437         aufs_bindex_t bindex;
438 };
439
440 static int test_empty_cb(void *__arg, const char *__name, int namelen,
441                          loff_t offset __maybe_unused, u64 ino,
442                          unsigned int d_type)
443 {
444         struct test_empty_arg *arg = __arg;
445         char *name = (void *)__name;
446
447         arg->err = 0;
448         au_fset_testempty(arg->flags, CALLED);
449         /* smp_mb(); */
450         if (name[0] == '.'
451             && (namelen == 1 || (name[1] == '.' && namelen == 2)))
452                 goto out; /* success */
453
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;
459                 goto out;
460         }
461
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));
468
469 out:
470         /* smp_mb(); */
471         AuTraceErr(arg->err);
472         return arg->err;
473 }
474
475 static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
476 {
477         int err;
478         struct file *h_file;
479
480         h_file = au_h_open(dentry, arg->bindex,
481                            O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
482                            /*file*/NULL);
483         err = PTR_ERR(h_file);
484         if (IS_ERR(h_file))
485                 goto out;
486
487         err = 0;
488         if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
489             && !h_file->f_dentry->d_inode->i_nlink)
490                 goto out_put;
491
492         do {
493                 arg->err = 0;
494                 au_fclr_testempty(arg->flags, CALLED);
495                 /* smp_mb(); */
496                 err = vfsub_readdir(h_file, test_empty_cb, arg);
497                 if (err >= 0)
498                         err = arg->err;
499         } while (!err && au_ftest_testempty(arg->flags, CALLED));
500
501 out_put:
502         fput(h_file);
503         au_sbr_put(dentry->d_sb, arg->bindex);
504 out:
505         return err;
506 }
507
508 struct do_test_empty_args {
509         int *errp;
510         struct dentry *dentry;
511         struct test_empty_arg *arg;
512 };
513
514 static void call_do_test_empty(void *args)
515 {
516         struct do_test_empty_args *a = args;
517         *a->errp = do_test_empty(a->dentry, a->arg);
518 }
519
520 static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
521 {
522         int err, wkq_err;
523         struct dentry *h_dentry;
524         struct inode *h_inode;
525
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);
532         if (!err)
533                 err = do_test_empty(dentry, arg);
534         else {
535                 struct do_test_empty_args args = {
536                         .errp   = &err,
537                         .dentry = dentry,
538                         .arg    = arg
539                 };
540                 unsigned int flags = arg->flags;
541
542                 wkq_err = au_wkq_wait(call_do_test_empty, &args);
543                 if (unlikely(wkq_err))
544                         err = wkq_err;
545                 arg->flags = flags;
546         }
547
548         return err;
549 }
550
551 int au_test_empty_lower(struct dentry *dentry)
552 {
553         int err;
554         unsigned int rdhash;
555         aufs_bindex_t bindex, bstart, btail;
556         struct au_nhash whlist;
557         struct test_empty_arg arg;
558
559         SiMustAnyLock(dentry->d_sb);
560
561         rdhash = au_sbi(dentry->d_sb)->si_rdhash;
562         if (!rdhash)
563                 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
564         err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
565         if (unlikely(err))
566                 goto out;
567
568         arg.flags = 0;
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);
573         arg.bindex = bstart;
574         err = do_test_empty(dentry, &arg);
575         if (unlikely(err))
576                 goto out_whlist;
577
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;
582
583                 h_dentry = au_h_dptr(dentry, bindex);
584                 if (h_dentry && h_dentry->d_inode) {
585                         arg.bindex = bindex;
586                         err = do_test_empty(dentry, &arg);
587                 }
588         }
589
590 out_whlist:
591         au_nhash_wh_free(&whlist);
592 out:
593         return err;
594 }
595
596 int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
597 {
598         int err;
599         struct test_empty_arg arg;
600         aufs_bindex_t bindex, btail;
601
602         err = 0;
603         arg.whlist = whlist;
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;
610
611                 h_dentry = au_h_dptr(dentry, bindex);
612                 if (h_dentry && h_dentry->d_inode) {
613                         arg.bindex = bindex;
614                         err = sio_test_empty(dentry, &arg);
615                 }
616         }
617
618         return err;
619 }
620
621 /* ---------------------------------------------------------------------- */
622
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,
629 #ifdef CONFIG_COMPAT
630         .compat_ioctl   = aufs_compat_ioctl_dir,
631 #endif
632         .open           = aufs_open_dir,
633         .release        = aufs_release_dir,
634         .flush          = aufs_flush_dir,
635         .fsync          = aufs_fsync_dir
636 };