Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / xino.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  * external inode number translation table and bitmap
21  */
22
23 #include <linux/seq_file.h>
24 #include "aufs.h"
25
26 /* todo: unnecessary to support mmap_sem since kernel-space? */
27 ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
28                    loff_t *pos)
29 {
30         ssize_t err;
31         mm_segment_t oldfs;
32         union {
33                 void *k;
34                 char __user *u;
35         } buf;
36
37         buf.k = kbuf;
38         oldfs = get_fs();
39         set_fs(KERNEL_DS);
40         do {
41                 /* todo: signal_pending? */
42                 err = func(file, buf.u, size, pos);
43         } while (err == -EAGAIN || err == -EINTR);
44         set_fs(oldfs);
45
46 #if 0 /* reserved for future use */
47         if (err > 0)
48                 fsnotify_access(file->f_dentry);
49 #endif
50
51         return err;
52 }
53
54 /* ---------------------------------------------------------------------- */
55
56 static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
57                               size_t size, loff_t *pos)
58 {
59         ssize_t err;
60         mm_segment_t oldfs;
61         union {
62                 void *k;
63                 const char __user *u;
64         } buf;
65
66         buf.k = kbuf;
67         oldfs = get_fs();
68         set_fs(KERNEL_DS);
69         do {
70                 /* todo: signal_pending? */
71                 err = func(file, buf.u, size, pos);
72         } while (err == -EAGAIN || err == -EINTR);
73         set_fs(oldfs);
74
75 #if 0 /* reserved for future use */
76         if (err > 0)
77                 fsnotify_modify(file->f_dentry);
78 #endif
79
80         return err;
81 }
82
83 struct do_xino_fwrite_args {
84         ssize_t *errp;
85         au_writef_t func;
86         struct file *file;
87         void *buf;
88         size_t size;
89         loff_t *pos;
90 };
91
92 static void call_do_xino_fwrite(void *args)
93 {
94         struct do_xino_fwrite_args *a = args;
95         *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
96 }
97
98 ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
99                     loff_t *pos)
100 {
101         ssize_t err;
102
103         /* todo: signal block and no wkq? */
104         if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
105                 lockdep_off();
106                 err = do_xino_fwrite(func, file, buf, size, pos);
107                 lockdep_on();
108         } else {
109                 /*
110                  * it breaks RLIMIT_FSIZE and normal user's limit,
111                  * users should care about quota and real 'filesystem full.'
112                  */
113                 int wkq_err;
114                 struct do_xino_fwrite_args args = {
115                         .errp   = &err,
116                         .func   = func,
117                         .file   = file,
118                         .buf    = buf,
119                         .size   = size,
120                         .pos    = pos
121                 };
122
123                 wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
124                 if (unlikely(wkq_err))
125                         err = wkq_err;
126         }
127
128         return err;
129 }
130
131 /* ---------------------------------------------------------------------- */
132
133 /*
134  * create a new xinofile at the same place/path as @base_file.
135  */
136 struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
137 {
138         struct file *file;
139         struct dentry *base, *parent;
140         struct inode *dir;
141         struct qstr *name;
142         struct path path;
143         int err;
144
145         base = base_file->f_dentry;
146         parent = base->d_parent; /* dir inode is locked */
147         dir = parent->d_inode;
148         IMustLock(dir);
149
150         file = ERR_PTR(-EINVAL);
151         name = &base->d_name;
152         path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
153         if (IS_ERR(path.dentry)) {
154                 file = (void *)path.dentry;
155                 pr_err("%.*s lookup err %ld\n",
156                        AuLNPair(name), PTR_ERR(path.dentry));
157                 goto out;
158         }
159
160         /* no need to mnt_want_write() since we call dentry_open() later */
161         err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
162         if (unlikely(err)) {
163                 file = ERR_PTR(err);
164                 pr_err("%.*s create err %d\n", AuLNPair(name), err);
165                 goto out_dput;
166         }
167
168         path.mnt = base_file->f_vfsmnt;
169         file = vfsub_dentry_open(&path,
170                                  O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
171                                  /* | __FMODE_NONOTIFY */);
172         if (IS_ERR(file)) {
173                 pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
174                 goto out_dput;
175         }
176
177         err = vfsub_unlink(dir, &file->f_path, /*force*/0);
178         if (unlikely(err)) {
179                 pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
180                 goto out_fput;
181         }
182
183         if (copy_src) {
184                 /* no one can touch copy_src xino */
185                 err = au_copy_file(file, copy_src,
186                                    i_size_read(copy_src->f_dentry->d_inode));
187                 if (unlikely(err)) {
188                         pr_err("%.*s copy err %d\n", AuLNPair(name), err);
189                         goto out_fput;
190                 }
191         }
192         goto out_dput; /* success */
193
194 out_fput:
195         fput(file);
196         file = ERR_PTR(err);
197 out_dput:
198         dput(path.dentry);
199 out:
200         return file;
201 }
202
203 struct au_xino_lock_dir {
204         struct au_hinode *hdir;
205         struct dentry *parent;
206         struct mutex *mtx;
207 };
208
209 static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
210                              struct au_xino_lock_dir *ldir)
211 {
212         aufs_bindex_t brid, bindex;
213
214         ldir->hdir = NULL;
215         bindex = -1;
216         brid = au_xino_brid(sb);
217         if (brid >= 0)
218                 bindex = au_br_index(sb, brid);
219         if (bindex >= 0) {
220                 ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
221                 au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
222         } else {
223                 ldir->parent = dget_parent(xino->f_dentry);
224                 ldir->mtx = &ldir->parent->d_inode->i_mutex;
225                 mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
226         }
227 }
228
229 static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
230 {
231         if (ldir->hdir)
232                 au_hn_imtx_unlock(ldir->hdir);
233         else {
234                 mutex_unlock(ldir->mtx);
235                 dput(ldir->parent);
236         }
237 }
238
239 /* ---------------------------------------------------------------------- */
240
241 /* trucate xino files asynchronously */
242
243 int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
244 {
245         int err;
246         aufs_bindex_t bi, bend;
247         struct au_branch *br;
248         struct file *new_xino, *file;
249         struct super_block *h_sb;
250         struct au_xino_lock_dir ldir;
251
252         err = -EINVAL;
253         bend = au_sbend(sb);
254         if (unlikely(bindex < 0 || bend < bindex))
255                 goto out;
256         br = au_sbr(sb, bindex);
257         file = br->br_xino.xi_file;
258         if (!file)
259                 goto out;
260
261         au_xino_lock_dir(sb, file, &ldir);
262         /* mnt_want_write() is unnecessary here */
263         new_xino = au_xino_create2(file, file);
264         au_xino_unlock_dir(&ldir);
265         err = PTR_ERR(new_xino);
266         if (IS_ERR(new_xino))
267                 goto out;
268         err = 0;
269         fput(file);
270         br->br_xino.xi_file = new_xino;
271
272         h_sb = br->br_mnt->mnt_sb;
273         for (bi = 0; bi <= bend; bi++) {
274                 if (unlikely(bi == bindex))
275                         continue;
276                 br = au_sbr(sb, bi);
277                 if (br->br_mnt->mnt_sb != h_sb)
278                         continue;
279
280                 fput(br->br_xino.xi_file);
281                 br->br_xino.xi_file = new_xino;
282                 get_file(new_xino);
283         }
284
285 out:
286         return err;
287 }
288
289 struct xino_do_trunc_args {
290         struct super_block *sb;
291         struct au_branch *br;
292 };
293
294 static void xino_do_trunc(void *_args)
295 {
296         struct xino_do_trunc_args *args = _args;
297         struct super_block *sb;
298         struct au_branch *br;
299         struct inode *dir;
300         int err;
301         aufs_bindex_t bindex;
302
303         err = 0;
304         sb = args->sb;
305         dir = sb->s_root->d_inode;
306         br = args->br;
307
308         si_noflush_write_lock(sb);
309         ii_read_lock_parent(dir);
310         bindex = au_br_index(sb, br->br_id);
311         err = au_xino_trunc(sb, bindex);
312         if (!err
313             && br->br_xino.xi_file->f_dentry->d_inode->i_blocks
314             >= br->br_xino_upper)
315                 br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
316
317         ii_read_unlock(dir);
318         if (unlikely(err))
319                 pr_warning("err b%d, (%d)\n", bindex, err);
320         atomic_dec(&br->br_xino_running);
321         atomic_dec(&br->br_count);
322         si_write_unlock(sb);
323         au_nwt_done(&au_sbi(sb)->si_nowait);
324         kfree(args);
325 }
326
327 static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
328 {
329         struct xino_do_trunc_args *args;
330         int wkq_err;
331
332         if (br->br_xino.xi_file->f_dentry->d_inode->i_blocks
333             < br->br_xino_upper)
334                 return;
335
336         if (atomic_inc_return(&br->br_xino_running) > 1)
337                 goto out;
338
339         /* lock and kfree() will be called in trunc_xino() */
340         args = kmalloc(sizeof(*args), GFP_NOFS);
341         if (unlikely(!args)) {
342                 AuErr1("no memory\n");
343                 goto out_args;
344         }
345
346         atomic_inc(&br->br_count);
347         args->sb = sb;
348         args->br = br;
349         wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
350         if (!wkq_err)
351                 return; /* success */
352
353         pr_err("wkq %d\n", wkq_err);
354         atomic_dec(&br->br_count);
355
356 out_args:
357         kfree(args);
358 out:
359         atomic_dec(&br->br_xino_running);
360 }
361
362 /* ---------------------------------------------------------------------- */
363
364 static int au_xino_do_write(au_writef_t write, struct file *file,
365                             ino_t h_ino, ino_t ino)
366 {
367         loff_t pos;
368         ssize_t sz;
369
370         pos = h_ino;
371         if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
372                 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
373                 return -EFBIG;
374         }
375         pos *= sizeof(ino);
376         sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
377         if (sz == sizeof(ino))
378                 return 0; /* success */
379
380         AuIOErr("write failed (%zd)\n", sz);
381         return -EIO;
382 }
383
384 /*
385  * write @ino to the xinofile for the specified branch{@sb, @bindex}
386  * at the position of @h_ino.
387  * even if @ino is zero, it is written to the xinofile and means no entry.
388  * if the size of the xino file on a specific filesystem exceeds the watermark,
389  * try truncating it.
390  */
391 int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
392                   ino_t ino)
393 {
394         int err;
395         unsigned int mnt_flags;
396         struct au_branch *br;
397
398         BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
399                      || ((loff_t)-1) > 0);
400         SiMustAnyLock(sb);
401
402         mnt_flags = au_mntflags(sb);
403         if (!au_opt_test(mnt_flags, XINO))
404                 return 0;
405
406         br = au_sbr(sb, bindex);
407         err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
408                                h_ino, ino);
409         if (!err) {
410                 if (au_opt_test(mnt_flags, TRUNC_XINO)
411                     && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
412                         xino_try_trunc(sb, br);
413                 return 0; /* success */
414         }
415
416         AuIOErr("write failed (%d)\n", err);
417         return -EIO;
418 }
419
420 /* ---------------------------------------------------------------------- */
421
422 /* aufs inode number bitmap */
423
424 static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
425 static ino_t xib_calc_ino(unsigned long pindex, int bit)
426 {
427         ino_t ino;
428
429         AuDebugOn(bit < 0 || page_bits <= bit);
430         ino = AUFS_FIRST_INO + pindex * page_bits + bit;
431         return ino;
432 }
433
434 static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
435 {
436         AuDebugOn(ino < AUFS_FIRST_INO);
437         ino -= AUFS_FIRST_INO;
438         *pindex = ino / page_bits;
439         *bit = ino % page_bits;
440 }
441
442 static int xib_pindex(struct super_block *sb, unsigned long pindex)
443 {
444         int err;
445         loff_t pos;
446         ssize_t sz;
447         struct au_sbinfo *sbinfo;
448         struct file *xib;
449         unsigned long *p;
450
451         sbinfo = au_sbi(sb);
452         MtxMustLock(&sbinfo->si_xib_mtx);
453         AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
454                   || !au_opt_test(sbinfo->si_mntflags, XINO));
455
456         if (pindex == sbinfo->si_xib_last_pindex)
457                 return 0;
458
459         xib = sbinfo->si_xib;
460         p = sbinfo->si_xib_buf;
461         pos = sbinfo->si_xib_last_pindex;
462         pos *= PAGE_SIZE;
463         sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
464         if (unlikely(sz != PAGE_SIZE))
465                 goto out;
466
467         pos = pindex;
468         pos *= PAGE_SIZE;
469         if (i_size_read(xib->f_dentry->d_inode) >= pos + PAGE_SIZE)
470                 sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
471         else {
472                 memset(p, 0, PAGE_SIZE);
473                 sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
474         }
475         if (sz == PAGE_SIZE) {
476                 sbinfo->si_xib_last_pindex = pindex;
477                 return 0; /* success */
478         }
479
480 out:
481         AuIOErr1("write failed (%zd)\n", sz);
482         err = sz;
483         if (sz >= 0)
484                 err = -EIO;
485         return err;
486 }
487
488 /* ---------------------------------------------------------------------- */
489
490 static void au_xib_clear_bit(struct inode *inode)
491 {
492         int err, bit;
493         unsigned long pindex;
494         struct super_block *sb;
495         struct au_sbinfo *sbinfo;
496
497         AuDebugOn(inode->i_nlink);
498
499         sb = inode->i_sb;
500         xib_calc_bit(inode->i_ino, &pindex, &bit);
501         AuDebugOn(page_bits <= bit);
502         sbinfo = au_sbi(sb);
503         mutex_lock(&sbinfo->si_xib_mtx);
504         err = xib_pindex(sb, pindex);
505         if (!err) {
506                 clear_bit(bit, sbinfo->si_xib_buf);
507                 sbinfo->si_xib_next_bit = bit;
508         }
509         mutex_unlock(&sbinfo->si_xib_mtx);
510 }
511
512 /* for s_op->delete_inode() */
513 void au_xino_delete_inode(struct inode *inode, const int unlinked)
514 {
515         int err;
516         unsigned int mnt_flags;
517         aufs_bindex_t bindex, bend, bi;
518         unsigned char try_trunc;
519         struct au_iinfo *iinfo;
520         struct super_block *sb;
521         struct au_hinode *hi;
522         struct inode *h_inode;
523         struct au_branch *br;
524         au_writef_t xwrite;
525
526         sb = inode->i_sb;
527         mnt_flags = au_mntflags(sb);
528         if (!au_opt_test(mnt_flags, XINO)
529             || inode->i_ino == AUFS_ROOT_INO)
530                 return;
531
532         if (unlinked) {
533                 au_xigen_inc(inode);
534                 au_xib_clear_bit(inode);
535         }
536
537         iinfo = au_ii(inode);
538         if (!iinfo)
539                 return;
540
541         bindex = iinfo->ii_bstart;
542         if (bindex < 0)
543                 return;
544
545         xwrite = au_sbi(sb)->si_xwrite;
546         try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
547         hi = iinfo->ii_hinode + bindex;
548         bend = iinfo->ii_bend;
549         for (; bindex <= bend; bindex++, hi++) {
550                 h_inode = hi->hi_inode;
551                 if (!h_inode
552                     || (!unlinked && h_inode->i_nlink))
553                         continue;
554
555                 /* inode may not be revalidated */
556                 bi = au_br_index(sb, hi->hi_id);
557                 if (bi < 0)
558                         continue;
559
560                 br = au_sbr(sb, bi);
561                 err = au_xino_do_write(xwrite, br->br_xino.xi_file,
562                                        h_inode->i_ino, /*ino*/0);
563                 if (!err && try_trunc
564                     && au_test_fs_trunc_xino(br->br_mnt->mnt_sb))
565                         xino_try_trunc(sb, br);
566         }
567 }
568
569 /* get an unused inode number from bitmap */
570 ino_t au_xino_new_ino(struct super_block *sb)
571 {
572         ino_t ino;
573         unsigned long *p, pindex, ul, pend;
574         struct au_sbinfo *sbinfo;
575         struct file *file;
576         int free_bit, err;
577
578         if (!au_opt_test(au_mntflags(sb), XINO))
579                 return iunique(sb, AUFS_FIRST_INO);
580
581         sbinfo = au_sbi(sb);
582         mutex_lock(&sbinfo->si_xib_mtx);
583         p = sbinfo->si_xib_buf;
584         free_bit = sbinfo->si_xib_next_bit;
585         if (free_bit < page_bits && !test_bit(free_bit, p))
586                 goto out; /* success */
587         free_bit = find_first_zero_bit(p, page_bits);
588         if (free_bit < page_bits)
589                 goto out; /* success */
590
591         pindex = sbinfo->si_xib_last_pindex;
592         for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
593                 err = xib_pindex(sb, ul);
594                 if (unlikely(err))
595                         goto out_err;
596                 free_bit = find_first_zero_bit(p, page_bits);
597                 if (free_bit < page_bits)
598                         goto out; /* success */
599         }
600
601         file = sbinfo->si_xib;
602         pend = i_size_read(file->f_dentry->d_inode) / PAGE_SIZE;
603         for (ul = pindex + 1; ul <= pend; ul++) {
604                 err = xib_pindex(sb, ul);
605                 if (unlikely(err))
606                         goto out_err;
607                 free_bit = find_first_zero_bit(p, page_bits);
608                 if (free_bit < page_bits)
609                         goto out; /* success */
610         }
611         BUG();
612
613 out:
614         set_bit(free_bit, p);
615         sbinfo->si_xib_next_bit = free_bit + 1;
616         pindex = sbinfo->si_xib_last_pindex;
617         mutex_unlock(&sbinfo->si_xib_mtx);
618         ino = xib_calc_ino(pindex, free_bit);
619         AuDbg("i%lu\n", (unsigned long)ino);
620         return ino;
621 out_err:
622         mutex_unlock(&sbinfo->si_xib_mtx);
623         AuDbg("i0\n");
624         return 0;
625 }
626
627 /*
628  * read @ino from xinofile for the specified branch{@sb, @bindex}
629  * at the position of @h_ino.
630  * if @ino does not exist and @do_new is true, get new one.
631  */
632 int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
633                  ino_t *ino)
634 {
635         int err;
636         ssize_t sz;
637         loff_t pos;
638         struct file *file;
639         struct au_sbinfo *sbinfo;
640
641         *ino = 0;
642         if (!au_opt_test(au_mntflags(sb), XINO))
643                 return 0; /* no xino */
644
645         err = 0;
646         sbinfo = au_sbi(sb);
647         pos = h_ino;
648         if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
649                 AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
650                 return -EFBIG;
651         }
652         pos *= sizeof(*ino);
653
654         file = au_sbr(sb, bindex)->br_xino.xi_file;
655         if (i_size_read(file->f_dentry->d_inode) < pos + sizeof(*ino))
656                 return 0; /* no ino */
657
658         sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
659         if (sz == sizeof(*ino))
660                 return 0; /* success */
661
662         err = sz;
663         if (unlikely(sz >= 0)) {
664                 err = -EIO;
665                 AuIOErr("xino read error (%zd)\n", sz);
666         }
667
668         return err;
669 }
670
671 /* ---------------------------------------------------------------------- */
672
673 /* create and set a new xino file */
674
675 struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
676 {
677         struct file *file;
678         struct dentry *h_parent, *d;
679         struct inode *h_dir;
680         int err;
681
682         /*
683          * at mount-time, and the xino file is the default path,
684          * hnotify is disabled so we have no notify events to ignore.
685          * when a user specified the xino, we cannot get au_hdir to be ignored.
686          */
687         file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
688                                /* | __FMODE_NONOTIFY */,
689                                S_IRUGO | S_IWUGO);
690         if (IS_ERR(file)) {
691                 if (!silent)
692                         pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
693                 return file;
694         }
695
696         /* keep file count */
697         h_parent = dget_parent(file->f_dentry);
698         h_dir = h_parent->d_inode;
699         mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
700         /* mnt_want_write() is unnecessary here */
701         err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
702         mutex_unlock(&h_dir->i_mutex);
703         dput(h_parent);
704         if (unlikely(err)) {
705                 if (!silent)
706                         pr_err("unlink %s(%d)\n", fname, err);
707                 goto out;
708         }
709
710         err = -EINVAL;
711         d = file->f_dentry;
712         if (unlikely(sb == d->d_sb)) {
713                 if (!silent)
714                         pr_err("%s must be outside\n", fname);
715                 goto out;
716         }
717         if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
718                 if (!silent)
719                         pr_err("xino doesn't support %s(%s)\n",
720                                fname, au_sbtype(d->d_sb));
721                 goto out;
722         }
723         return file; /* success */
724
725 out:
726         fput(file);
727         file = ERR_PTR(err);
728         return file;
729 }
730
731 /*
732  * find another branch who is on the same filesystem of the specified
733  * branch{@btgt}. search until @bend.
734  */
735 static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
736                         aufs_bindex_t bend)
737 {
738         aufs_bindex_t bindex;
739         struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
740
741         for (bindex = 0; bindex < btgt; bindex++)
742                 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
743                         return bindex;
744         for (bindex++; bindex <= bend; bindex++)
745                 if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
746                         return bindex;
747         return -1;
748 }
749
750 /* ---------------------------------------------------------------------- */
751
752 /*
753  * initialize the xinofile for the specified branch @br
754  * at the place/path where @base_file indicates.
755  * test whether another branch is on the same filesystem or not,
756  * if @do_test is true.
757  */
758 int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
759                struct file *base_file, int do_test)
760 {
761         int err;
762         ino_t ino;
763         aufs_bindex_t bend, bindex;
764         struct au_branch *shared_br, *b;
765         struct file *file;
766         struct super_block *tgt_sb;
767
768         shared_br = NULL;
769         bend = au_sbend(sb);
770         if (do_test) {
771                 tgt_sb = br->br_mnt->mnt_sb;
772                 for (bindex = 0; bindex <= bend; bindex++) {
773                         b = au_sbr(sb, bindex);
774                         if (tgt_sb == b->br_mnt->mnt_sb) {
775                                 shared_br = b;
776                                 break;
777                         }
778                 }
779         }
780
781         if (!shared_br || !shared_br->br_xino.xi_file) {
782                 struct au_xino_lock_dir ldir;
783
784                 au_xino_lock_dir(sb, base_file, &ldir);
785                 /* mnt_want_write() is unnecessary here */
786                 file = au_xino_create2(base_file, NULL);
787                 au_xino_unlock_dir(&ldir);
788                 err = PTR_ERR(file);
789                 if (IS_ERR(file))
790                         goto out;
791                 br->br_xino.xi_file = file;
792         } else {
793                 br->br_xino.xi_file = shared_br->br_xino.xi_file;
794                 get_file(br->br_xino.xi_file);
795         }
796
797         ino = AUFS_ROOT_INO;
798         err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
799                                h_ino, ino);
800         if (unlikely(err)) {
801                 fput(br->br_xino.xi_file);
802                 br->br_xino.xi_file = NULL;
803         }
804
805 out:
806         return err;
807 }
808
809 /* ---------------------------------------------------------------------- */
810
811 /* trucate a xino bitmap file */
812
813 /* todo: slow */
814 static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
815 {
816         int err, bit;
817         ssize_t sz;
818         unsigned long pindex;
819         loff_t pos, pend;
820         struct au_sbinfo *sbinfo;
821         au_readf_t func;
822         ino_t *ino;
823         unsigned long *p;
824
825         err = 0;
826         sbinfo = au_sbi(sb);
827         MtxMustLock(&sbinfo->si_xib_mtx);
828         p = sbinfo->si_xib_buf;
829         func = sbinfo->si_xread;
830         pend = i_size_read(file->f_dentry->d_inode);
831         pos = 0;
832         while (pos < pend) {
833                 sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
834                 err = sz;
835                 if (unlikely(sz <= 0))
836                         goto out;
837
838                 err = 0;
839                 for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
840                         if (unlikely(*ino < AUFS_FIRST_INO))
841                                 continue;
842
843                         xib_calc_bit(*ino, &pindex, &bit);
844                         AuDebugOn(page_bits <= bit);
845                         err = xib_pindex(sb, pindex);
846                         if (!err)
847                                 set_bit(bit, p);
848                         else
849                                 goto out;
850                 }
851         }
852
853 out:
854         return err;
855 }
856
857 static int xib_restore(struct super_block *sb)
858 {
859         int err;
860         aufs_bindex_t bindex, bend;
861         void *page;
862
863         err = -ENOMEM;
864         page = (void *)__get_free_page(GFP_NOFS);
865         if (unlikely(!page))
866                 goto out;
867
868         err = 0;
869         bend = au_sbend(sb);
870         for (bindex = 0; !err && bindex <= bend; bindex++)
871                 if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
872                         err = do_xib_restore
873                                 (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
874                 else
875                         AuDbg("b%d\n", bindex);
876         free_page((unsigned long)page);
877
878 out:
879         return err;
880 }
881
882 int au_xib_trunc(struct super_block *sb)
883 {
884         int err;
885         ssize_t sz;
886         loff_t pos;
887         struct au_xino_lock_dir ldir;
888         struct au_sbinfo *sbinfo;
889         unsigned long *p;
890         struct file *file;
891
892         SiMustWriteLock(sb);
893
894         err = 0;
895         sbinfo = au_sbi(sb);
896         if (!au_opt_test(sbinfo->si_mntflags, XINO))
897                 goto out;
898
899         file = sbinfo->si_xib;
900         if (i_size_read(file->f_dentry->d_inode) <= PAGE_SIZE)
901                 goto out;
902
903         au_xino_lock_dir(sb, file, &ldir);
904         /* mnt_want_write() is unnecessary here */
905         file = au_xino_create2(sbinfo->si_xib, NULL);
906         au_xino_unlock_dir(&ldir);
907         err = PTR_ERR(file);
908         if (IS_ERR(file))
909                 goto out;
910         fput(sbinfo->si_xib);
911         sbinfo->si_xib = file;
912
913         p = sbinfo->si_xib_buf;
914         memset(p, 0, PAGE_SIZE);
915         pos = 0;
916         sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
917         if (unlikely(sz != PAGE_SIZE)) {
918                 err = sz;
919                 AuIOErr("err %d\n", err);
920                 if (sz >= 0)
921                         err = -EIO;
922                 goto out;
923         }
924
925         mutex_lock(&sbinfo->si_xib_mtx);
926         /* mnt_want_write() is unnecessary here */
927         err = xib_restore(sb);
928         mutex_unlock(&sbinfo->si_xib_mtx);
929
930 out:
931         return err;
932 }
933
934 /* ---------------------------------------------------------------------- */
935
936 /*
937  * xino mount option handlers
938  */
939 static au_readf_t find_readf(struct file *h_file)
940 {
941         const struct file_operations *fop = h_file->f_op;
942
943         if (fop) {
944                 if (fop->read)
945                         return fop->read;
946                 if (fop->aio_read)
947                         return do_sync_read;
948         }
949         return ERR_PTR(-ENOSYS);
950 }
951
952 static au_writef_t find_writef(struct file *h_file)
953 {
954         const struct file_operations *fop = h_file->f_op;
955
956         if (fop) {
957                 if (fop->write)
958                         return fop->write;
959                 if (fop->aio_write)
960                         return do_sync_write;
961         }
962         return ERR_PTR(-ENOSYS);
963 }
964
965 /* xino bitmap */
966 static void xino_clear_xib(struct super_block *sb)
967 {
968         struct au_sbinfo *sbinfo;
969
970         SiMustWriteLock(sb);
971
972         sbinfo = au_sbi(sb);
973         sbinfo->si_xread = NULL;
974         sbinfo->si_xwrite = NULL;
975         if (sbinfo->si_xib)
976                 fput(sbinfo->si_xib);
977         sbinfo->si_xib = NULL;
978         free_page((unsigned long)sbinfo->si_xib_buf);
979         sbinfo->si_xib_buf = NULL;
980 }
981
982 static int au_xino_set_xib(struct super_block *sb, struct file *base)
983 {
984         int err;
985         loff_t pos;
986         struct au_sbinfo *sbinfo;
987         struct file *file;
988
989         SiMustWriteLock(sb);
990
991         sbinfo = au_sbi(sb);
992         file = au_xino_create2(base, sbinfo->si_xib);
993         err = PTR_ERR(file);
994         if (IS_ERR(file))
995                 goto out;
996         if (sbinfo->si_xib)
997                 fput(sbinfo->si_xib);
998         sbinfo->si_xib = file;
999         sbinfo->si_xread = find_readf(file);
1000         sbinfo->si_xwrite = find_writef(file);
1001
1002         err = -ENOMEM;
1003         if (!sbinfo->si_xib_buf)
1004                 sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
1005         if (unlikely(!sbinfo->si_xib_buf))
1006                 goto out_unset;
1007
1008         sbinfo->si_xib_last_pindex = 0;
1009         sbinfo->si_xib_next_bit = 0;
1010         if (i_size_read(file->f_dentry->d_inode) < PAGE_SIZE) {
1011                 pos = 0;
1012                 err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
1013                                   PAGE_SIZE, &pos);
1014                 if (unlikely(err != PAGE_SIZE))
1015                         goto out_free;
1016         }
1017         err = 0;
1018         goto out; /* success */
1019
1020 out_free:
1021         free_page((unsigned long)sbinfo->si_xib_buf);
1022         sbinfo->si_xib_buf = NULL;
1023         if (err >= 0)
1024                 err = -EIO;
1025 out_unset:
1026         fput(sbinfo->si_xib);
1027         sbinfo->si_xib = NULL;
1028         sbinfo->si_xread = NULL;
1029         sbinfo->si_xwrite = NULL;
1030 out:
1031         return err;
1032 }
1033
1034 /* xino for each branch */
1035 static void xino_clear_br(struct super_block *sb)
1036 {
1037         aufs_bindex_t bindex, bend;
1038         struct au_branch *br;
1039
1040         bend = au_sbend(sb);
1041         for (bindex = 0; bindex <= bend; bindex++) {
1042                 br = au_sbr(sb, bindex);
1043                 if (!br || !br->br_xino.xi_file)
1044                         continue;
1045
1046                 fput(br->br_xino.xi_file);
1047                 br->br_xino.xi_file = NULL;
1048         }
1049 }
1050
1051 static int au_xino_set_br(struct super_block *sb, struct file *base)
1052 {
1053         int err;
1054         ino_t ino;
1055         aufs_bindex_t bindex, bend, bshared;
1056         struct {
1057                 struct file *old, *new;
1058         } *fpair, *p;
1059         struct au_branch *br;
1060         struct inode *inode;
1061         au_writef_t writef;
1062
1063         SiMustWriteLock(sb);
1064
1065         err = -ENOMEM;
1066         bend = au_sbend(sb);
1067         fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
1068         if (unlikely(!fpair))
1069                 goto out;
1070
1071         inode = sb->s_root->d_inode;
1072         ino = AUFS_ROOT_INO;
1073         writef = au_sbi(sb)->si_xwrite;
1074         for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1075                 br = au_sbr(sb, bindex);
1076                 bshared = is_sb_shared(sb, bindex, bindex - 1);
1077                 if (bshared >= 0) {
1078                         /* shared xino */
1079                         *p = fpair[bshared];
1080                         get_file(p->new);
1081                 }
1082
1083                 if (!p->new) {
1084                         /* new xino */
1085                         p->old = br->br_xino.xi_file;
1086                         p->new = au_xino_create2(base, br->br_xino.xi_file);
1087                         err = PTR_ERR(p->new);
1088                         if (IS_ERR(p->new)) {
1089                                 p->new = NULL;
1090                                 goto out_pair;
1091                         }
1092                 }
1093
1094                 err = au_xino_do_write(writef, p->new,
1095                                        au_h_iptr(inode, bindex)->i_ino, ino);
1096                 if (unlikely(err))
1097                         goto out_pair;
1098         }
1099
1100         for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
1101                 br = au_sbr(sb, bindex);
1102                 if (br->br_xino.xi_file)
1103                         fput(br->br_xino.xi_file);
1104                 get_file(p->new);
1105                 br->br_xino.xi_file = p->new;
1106         }
1107
1108 out_pair:
1109         for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
1110                 if (p->new)
1111                         fput(p->new);
1112                 else
1113                         break;
1114         kfree(fpair);
1115 out:
1116         return err;
1117 }
1118
1119 void au_xino_clr(struct super_block *sb)
1120 {
1121         struct au_sbinfo *sbinfo;
1122
1123         au_xigen_clr(sb);
1124         xino_clear_xib(sb);
1125         xino_clear_br(sb);
1126         sbinfo = au_sbi(sb);
1127         /* lvalue, do not call au_mntflags() */
1128         au_opt_clr(sbinfo->si_mntflags, XINO);
1129 }
1130
1131 int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
1132 {
1133         int err, skip;
1134         struct dentry *parent, *cur_parent;
1135         struct qstr *dname, *cur_name;
1136         struct file *cur_xino;
1137         struct inode *dir;
1138         struct au_sbinfo *sbinfo;
1139
1140         SiMustWriteLock(sb);
1141
1142         err = 0;
1143         sbinfo = au_sbi(sb);
1144         parent = dget_parent(xino->file->f_dentry);
1145         if (remount) {
1146                 skip = 0;
1147                 dname = &xino->file->f_dentry->d_name;
1148                 cur_xino = sbinfo->si_xib;
1149                 if (cur_xino) {
1150                         cur_parent = dget_parent(cur_xino->f_dentry);
1151                         cur_name = &cur_xino->f_dentry->d_name;
1152                         skip = (cur_parent == parent
1153                                 && dname->len == cur_name->len
1154                                 && !memcmp(dname->name, cur_name->name,
1155                                            dname->len));
1156                         dput(cur_parent);
1157                 }
1158                 if (skip)
1159                         goto out;
1160         }
1161
1162         au_opt_set(sbinfo->si_mntflags, XINO);
1163         dir = parent->d_inode;
1164         mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
1165         /* mnt_want_write() is unnecessary here */
1166         err = au_xino_set_xib(sb, xino->file);
1167         if (!err)
1168                 err = au_xigen_set(sb, xino->file);
1169         if (!err)
1170                 err = au_xino_set_br(sb, xino->file);
1171         mutex_unlock(&dir->i_mutex);
1172         if (!err)
1173                 goto out; /* success */
1174
1175         /* reset all */
1176         AuIOErr("failed creating xino(%d).\n", err);
1177
1178 out:
1179         dput(parent);
1180         return err;
1181 }
1182
1183 /* ---------------------------------------------------------------------- */
1184
1185 /*
1186  * create a xinofile at the default place/path.
1187  */
1188 struct file *au_xino_def(struct super_block *sb)
1189 {
1190         struct file *file;
1191         char *page, *p;
1192         struct au_branch *br;
1193         struct super_block *h_sb;
1194         struct path path;
1195         aufs_bindex_t bend, bindex, bwr;
1196
1197         br = NULL;
1198         bend = au_sbend(sb);
1199         bwr = -1;
1200         for (bindex = 0; bindex <= bend; bindex++) {
1201                 br = au_sbr(sb, bindex);
1202                 if (au_br_writable(br->br_perm)
1203                     && !au_test_fs_bad_xino(br->br_mnt->mnt_sb)) {
1204                         bwr = bindex;
1205                         break;
1206                 }
1207         }
1208
1209         if (bwr >= 0) {
1210                 file = ERR_PTR(-ENOMEM);
1211                 page = __getname_gfp(GFP_NOFS);
1212                 if (unlikely(!page))
1213                         goto out;
1214                 path.mnt = br->br_mnt;
1215                 path.dentry = au_h_dptr(sb->s_root, bwr);
1216                 p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
1217                 file = (void *)p;
1218                 if (!IS_ERR(p)) {
1219                         strcat(p, "/" AUFS_XINO_FNAME);
1220                         AuDbg("%s\n", p);
1221                         file = au_xino_create(sb, p, /*silent*/0);
1222                         if (!IS_ERR(file))
1223                                 au_xino_brid_set(sb, br->br_id);
1224                 }
1225                 __putname(page);
1226         } else {
1227                 file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
1228                 if (IS_ERR(file))
1229                         goto out;
1230                 h_sb = file->f_dentry->d_sb;
1231                 if (unlikely(au_test_fs_bad_xino(h_sb))) {
1232                         pr_err("xino doesn't support %s(%s)\n",
1233                                AUFS_XINO_DEFPATH, au_sbtype(h_sb));
1234                         fput(file);
1235                         file = ERR_PTR(-EINVAL);
1236                 }
1237                 if (!IS_ERR(file))
1238                         au_xino_brid_set(sb, -1);
1239         }
1240
1241 out:
1242         return file;
1243 }
1244
1245 /* ---------------------------------------------------------------------- */
1246
1247 int au_xino_path(struct seq_file *seq, struct file *file)
1248 {
1249         int err;
1250
1251         err = au_seq_path(seq, &file->f_path);
1252         if (unlikely(err < 0))
1253                 goto out;
1254
1255         err = 0;
1256 #define Deleted "\\040(deleted)"
1257         seq->count -= sizeof(Deleted) - 1;
1258         AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
1259                          sizeof(Deleted) - 1));
1260 #undef Deleted
1261
1262 out:
1263         return err;
1264 }