~ubuntu-branches/ubuntu/vivid/aufs/vivid

« back to all changes in this revision

Viewing changes to fs/aufs25/xino.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2008-05-06 18:35:50 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080506183550-0b6c974kkgc46oeh
Tags: 0+20080506-1
* New upstream release, supports Kernel 2.6.25 (Closes: #479717)
* Fix building with older Kernels (Closes: #475042)
* Update the patches 01, 04 and 07 to also patch fs/aufs25

Show diffs side-by-side

added added

removed removed

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