~ubuntu-branches/ubuntu/karmic/linux-mvl-dove/karmic-proposed

« back to all changes in this revision

Viewing changes to ubuntu/aufs/xino.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bader
  • Date: 2010-03-10 22:24:12 UTC
  • mto: (15.1.2 karmic-security)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20100310222412-k86m3r53jw0je7x1
Tags: upstream-2.6.31
ImportĀ upstreamĀ versionĀ 2.6.31

Show diffs side-by-side

added added

removed removed

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