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