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