~ubuntu-branches/ubuntu/raring/aufs/raring

« back to all changes in this revision

Viewing changes to fs/aufs25/inode.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2008-05-06 18:35:50 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20080506183550-7y7mrzkzkh2tjlfu
Tags: upstream-0+20080506
ImportĀ upstreamĀ versionĀ 0+20080506

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
 * inode functions
 
21
 *
 
22
 * $Id: inode.c,v 1.3 2008/04/28 03:04:12 sfjro Exp $
 
23
 */
 
24
 
 
25
#include "aufs.h"
 
26
 
 
27
int au_refresh_hinode_self(struct inode *inode)
 
28
{
 
29
        int err, new_sz, update;
 
30
        struct inode *first;
 
31
        struct au_hinode *p, *q, tmp;
 
32
        struct super_block *sb;
 
33
        struct au_iinfo *iinfo;
 
34
        aufs_bindex_t bindex, bend, new_bindex;
 
35
 
 
36
        LKTRTrace("i%lu\n", inode->i_ino);
 
37
        IiMustWriteLock(inode);
 
38
 
 
39
        err = -ENOMEM;
 
40
        update = 0;
 
41
        sb = inode->i_sb;
 
42
        bend = au_sbend(sb);
 
43
        new_sz = sizeof(*iinfo->ii_hinode) * (bend + 1);
 
44
        iinfo = au_ii(inode);
 
45
        p = au_kzrealloc(iinfo->ii_hinode, sizeof(*p) * (iinfo->ii_bend + 1),
 
46
                         new_sz, GFP_KERNEL);
 
47
        //p = NULL;
 
48
        if (unlikely(!p))
 
49
                goto out;
 
50
 
 
51
        iinfo->ii_hinode = p;
 
52
        p = iinfo->ii_hinode + iinfo->ii_bstart;
 
53
        first = p->hi_inode;
 
54
        err = 0;
 
55
        for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
 
56
             bindex++, p++) {
 
57
                if (!p->hi_inode)
 
58
                        continue;
 
59
 
 
60
                new_bindex = au_br_index(sb, p->hi_id);
 
61
                if (new_bindex == bindex)
 
62
                        continue;
 
63
                if (new_bindex < 0) {
 
64
                        update++;
 
65
                        au_hiput(p);
 
66
                        p->hi_inode = NULL;
 
67
                        continue;
 
68
                }
 
69
 
 
70
                if (new_bindex < iinfo->ii_bstart)
 
71
                        iinfo->ii_bstart = new_bindex;
 
72
                if (iinfo->ii_bend < new_bindex)
 
73
                        iinfo->ii_bend = new_bindex;
 
74
                /* swap two hidden inode, and loop again */
 
75
                q = iinfo->ii_hinode + new_bindex;
 
76
                tmp = *q;
 
77
                *q = *p;
 
78
                *p = tmp;
 
79
                if (tmp.hi_inode) {
 
80
                        bindex--;
 
81
                        p--;
 
82
                }
 
83
        }
 
84
        au_update_brange(inode, /*do_put_zero*/0);
 
85
 
 
86
        if (unlikely(err))
 
87
                goto out;
 
88
 
 
89
        if (1 || first != au_h_iptr(inode, iinfo->ii_bstart))
 
90
                au_cpup_attr_all(inode);
 
91
        if (update && S_ISDIR(inode->i_mode))
 
92
                inode->i_version++;
 
93
        au_update_iigen(inode);
 
94
 
 
95
 out:
 
96
        AuTraceErr(err);
 
97
        return err;
 
98
}
 
99
 
 
100
int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
 
101
{
 
102
        int err, update, isdir;
 
103
        struct inode *first;
 
104
        struct au_hinode *p;
 
105
        struct super_block *sb;
 
106
        struct au_iinfo *iinfo;
 
107
        aufs_bindex_t bindex, bend;
 
108
        unsigned int flags;
 
109
 
 
110
        LKTRTrace("%.*s\n", AuDLNPair(dentry));
 
111
        IiMustWriteLock(inode);
 
112
 
 
113
        err = au_refresh_hinode_self(inode);
 
114
        if (unlikely(err))
 
115
                goto out;
 
116
 
 
117
        sb = dentry->d_sb;
 
118
        bend = au_sbend(sb);
 
119
        iinfo = au_ii(inode);
 
120
        update = 0;
 
121
        p = iinfo->ii_hinode + iinfo->ii_bstart;
 
122
        first = p->hi_inode;
 
123
        isdir = S_ISDIR(inode->i_mode);
 
124
        flags = au_hi_flags(inode, isdir);
 
125
        bend = au_dbend(dentry);
 
126
        for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
 
127
                struct inode *hi;
 
128
                struct dentry *hd;
 
129
 
 
130
                hd = au_h_dptr(dentry, bindex);
 
131
                if (!hd || !hd->d_inode)
 
132
                        continue;
 
133
 
 
134
                if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
 
135
                        hi = au_h_iptr(inode, bindex);
 
136
                        if (hi) {
 
137
                                if (hi == hd->d_inode)
 
138
                                        continue;
 
139
                                err = -ESTALE;
 
140
                                break;
 
141
                        }
 
142
                }
 
143
                if (bindex < iinfo->ii_bstart)
 
144
                        iinfo->ii_bstart = bindex;
 
145
                if (iinfo->ii_bend < bindex)
 
146
                        iinfo->ii_bend = bindex;
 
147
                au_set_h_iptr(inode, bindex, igrab(hd->d_inode), flags);
 
148
                update++;
 
149
        }
 
150
        au_update_brange(inode, /*do_put_zero*/0);
 
151
 
 
152
        if (unlikely(err))
 
153
                goto out;
 
154
 
 
155
        if (1 || first != au_h_iptr(inode, iinfo->ii_bstart))
 
156
                au_cpup_attr_all(inode);
 
157
        if (update && isdir)
 
158
                inode->i_version++;
 
159
        au_update_iigen(inode);
 
160
 
 
161
 out:
 
162
        AuTraceErr(err);
 
163
        return err;
 
164
}
 
165
 
 
166
static int set_inode(struct inode *inode, struct dentry *dentry)
 
167
{
 
168
        int err, isdir;
 
169
        struct dentry *h_dentry;
 
170
        struct inode *h_inode;
 
171
        umode_t mode;
 
172
        aufs_bindex_t bindex, bstart, btail;
 
173
        struct au_iinfo *iinfo;
 
174
        unsigned int flags;
 
175
 
 
176
        LKTRTrace("i%lu, %.*s\n", inode->i_ino, AuDLNPair(dentry));
 
177
        AuDebugOn(!(inode->i_state & I_NEW));
 
178
        IiMustWriteLock(inode);
 
179
        bstart = au_dbstart(dentry);
 
180
        h_dentry = au_h_dptr(dentry, bstart);
 
181
        AuDebugOn(!h_dentry);
 
182
        h_inode = h_dentry->d_inode;
 
183
        AuDebugOn(!h_inode);
 
184
 
 
185
        err = 0;
 
186
        isdir = 0;
 
187
        mode = h_inode->i_mode;
 
188
        switch (mode & S_IFMT) {
 
189
        case S_IFREG:
 
190
                btail = au_dbtail(dentry);
 
191
                break;
 
192
        case S_IFDIR:
 
193
                isdir = 1;
 
194
                btail = au_dbtaildir(dentry);
 
195
                inode->i_op = &aufs_dir_iop;
 
196
                inode->i_fop = &aufs_dir_fop;
 
197
                break;
 
198
        case S_IFLNK:
 
199
                btail = au_dbtail(dentry);
 
200
                inode->i_op = &aufs_symlink_iop;
 
201
                break;
 
202
        case S_IFBLK:
 
203
        case S_IFCHR:
 
204
        case S_IFIFO:
 
205
        case S_IFSOCK:
 
206
                btail = au_dbtail(dentry);
 
207
                init_special_inode(inode, mode,
 
208
                                   au_h_rdev(h_inode, /*h_mnt*/NULL, h_dentry));
 
209
                break;
 
210
        default:
 
211
                AuIOErr("Unknown file type 0%o\n", mode);
 
212
                err = -EIO;
 
213
                goto out;
 
214
        }
 
215
 
 
216
        flags = au_hi_flags(inode, isdir);
 
217
        iinfo = au_ii(inode);
 
218
        iinfo->ii_bstart = bstart;
 
219
        iinfo->ii_bend = btail;
 
220
        for (bindex = bstart; bindex <= btail; bindex++) {
 
221
                h_dentry = au_h_dptr(dentry, bindex);
 
222
                if (!h_dentry)
 
223
                        continue;
 
224
                AuDebugOn(!h_dentry->d_inode);
 
225
                au_set_h_iptr(inode, bindex, igrab(h_dentry->d_inode), flags);
 
226
        }
 
227
        au_cpup_attr_all(inode);
 
228
 
 
229
 out:
 
230
        AuTraceErr(err);
 
231
        return err;
 
232
}
 
233
 
 
234
/* successful returns with iinfo write_locked */
 
235
//todo: return with unlocked?
 
236
static int reval_inode(struct inode *inode, struct dentry *dentry, int *matched)
 
237
{
 
238
        int err;
 
239
        struct inode *h_inode, *h_dinode;
 
240
        aufs_bindex_t bindex, bend;
 
241
#if 0
 
242
        const int hinotify = au_opt_test(au_mntflags(inode->i_sb),
 
243
                                         UDBA_INOTIFY);
 
244
#endif
 
245
 
 
246
        LKTRTrace("i%lu, %.*s\n", inode->i_ino, AuDLNPair(dentry));
 
247
 
 
248
        *matched = 0;
 
249
 
 
250
        /*
 
251
         * before this function, if aufs got any iinfo lock, it must be only
 
252
         * one, the parent dir.
 
253
         * it can happen by UDBA and the obsoleted inode number.
 
254
         */
 
255
        err = -EIO;
 
256
        if (unlikely(inode->i_ino == parent_ino(dentry)))
 
257
                goto out;
 
258
 
 
259
        err = 0;
 
260
        h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
 
261
        mutex_lock_nested(&inode->i_mutex, AuLsc_I_CHILD);
 
262
        ii_write_lock_new(inode);
 
263
        bend = au_ibend(inode);
 
264
        for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
 
265
                h_inode = au_h_iptr(inode, bindex);
 
266
                if (h_inode && h_inode == h_dinode) {
 
267
                        //&& (ibs != bstart || !au_test_higen(inode, h_inode)));
 
268
                        *matched = 1;
 
269
                        err = 0;
 
270
                        if (unlikely(au_iigen(inode) != au_digen(dentry)))
 
271
                                err = au_refresh_hinode(inode, dentry);
 
272
                        break;
 
273
                }
 
274
        }
 
275
        if (unlikely(err))
 
276
                ii_write_unlock(inode);
 
277
        mutex_unlock(&inode->i_mutex);
 
278
 
 
279
 out:
 
280
        AuTraceErr(err);
 
281
        return err;
 
282
}
 
283
 
 
284
/* successful returns with iinfo write_locked */
 
285
//todo: return with unlocked?
 
286
struct inode *au_new_inode(struct dentry *dentry)
 
287
{
 
288
        struct inode *inode, *h_inode;
 
289
        struct dentry *h_dentry;
 
290
        ino_t h_ino;
 
291
        struct super_block *sb;
 
292
        int err, match;
 
293
        aufs_bindex_t bstart;
 
294
        struct au_xino_entry xinoe;
 
295
 
 
296
        LKTRTrace("%.*s\n", AuDLNPair(dentry));
 
297
        sb = dentry->d_sb;
 
298
        bstart = au_dbstart(dentry);
 
299
        h_dentry = au_h_dptr(dentry, bstart);
 
300
        AuDebugOn(!h_dentry);
 
301
        h_inode = h_dentry->d_inode;
 
302
        AuDebugOn(!h_inode);
 
303
 
 
304
        h_ino = h_inode->i_ino;
 
305
        err = au_xino_read(sb, bstart, h_ino, &xinoe);
 
306
        //err = -1;
 
307
        inode = ERR_PTR(err);
 
308
        if (unlikely(err))
 
309
                goto out;
 
310
 new_ino:
 
311
        if (!xinoe.ino) {
 
312
                xinoe.ino = au_xino_new_ino(sb);
 
313
                if (!xinoe.ino) {
 
314
                        inode = ERR_PTR(-EIO);
 
315
                        goto out;
 
316
                }
 
317
        }
 
318
 
 
319
        LKTRTrace("i%lu\n", xinoe.ino);
 
320
        inode = au_iget_locked(sb, xinoe.ino);
 
321
        err = PTR_ERR(inode);
 
322
        if (IS_ERR(inode))
 
323
                goto out;
 
324
 
 
325
        LKTRTrace("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
 
326
        if (inode->i_state & I_NEW) {
 
327
                ii_write_lock_new(inode);
 
328
                err = set_inode(inode, dentry);
 
329
                //err = -1;
 
330
                unlock_new_inode(inode);
 
331
                if (!err)
 
332
                        goto out; /* success */
 
333
                iget_failed(inode);
 
334
                ii_write_unlock(inode);
 
335
                goto out_iput;
 
336
        } else {
 
337
                //todo: remove this
 
338
                AuDebugOn(inode->i_state & I_LOCK);
 
339
                err = reval_inode(inode, dentry, &match);
 
340
                if (!err)
 
341
                        goto out; /* success */
 
342
                else if (match)
 
343
                        goto out_iput;
 
344
        }
 
345
 
 
346
        if (unlikely(au_test_unique_ino(h_dentry, h_ino)))
 
347
                AuWarn1("Un-notified UDBA or repeatedly renamed dir,"
 
348
                        " b%d, %s, %.*s, hi%lu, i%lu.\n",
 
349
                        bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
 
350
                        h_ino, xinoe.ino);
 
351
#if 0 // debug
 
352
        {
 
353
                static unsigned char c;
 
354
                if (!c++) {
 
355
                        struct dentry *d = d_find_alias(inode);
 
356
                        au_debug_on();
 
357
                        DbgDentry(dentry);
 
358
                        DbgInode(inode);
 
359
                        if (d) {
 
360
                                DbgDentry(d);
 
361
                                dput(d);
 
362
                        }
 
363
                        au_debug_off();
 
364
                }
 
365
        }
 
366
#endif
 
367
        xinoe.ino = 0;
 
368
        err = au_xino_write0(sb, bstart, h_ino, 0);
 
369
        if (!err) {
 
370
                iput(inode);
 
371
                goto new_ino;
 
372
        }
 
373
        /* force noxino? */
 
374
 
 
375
 out_iput:
 
376
        iput(inode);
 
377
        inode = ERR_PTR(err);
 
378
 out:
 
379
        AuTraceErrPtr(inode);
 
380
        return inode;
 
381
}
 
382
 
 
383
/* ---------------------------------------------------------------------- */
 
384
 
 
385
int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
 
386
               struct inode *inode)
 
387
{
 
388
        int err;
 
389
 
 
390
        err = au_br_rdonly(au_sbr(sb, bindex));
 
391
 
 
392
        /* pseudo-link after flushed may out of bounds */
 
393
        if (!err
 
394
            && inode
 
395
            && au_ibstart(inode) <= bindex
 
396
            && bindex <= au_ibend(inode)) {
 
397
                /*
 
398
                 * permission check is unnecessary since vfsub routine
 
399
                 * will be called later
 
400
                 */
 
401
                struct inode *hi = au_h_iptr(inode, bindex);
 
402
                if (hi)
 
403
                        err = IS_IMMUTABLE(hi) ? -EROFS : 0;
 
404
        }
 
405
 
 
406
        AuTraceErr(err);
 
407
        return err;
 
408
}
 
409
 
 
410
int au_test_h_perm(struct inode *h_inode, int mask, int dlgt)
 
411
{
 
412
        if (!current->fsuid)
 
413
                return 0;
 
414
        //todo: fake nameidata
 
415
        return vfsub_permission(h_inode, mask, NULL, dlgt);
 
416
}
 
417
 
 
418
int au_test_h_perm_sio(struct inode *h_inode, int mask, int dlgt)
 
419
{
 
420
        if (unlikely(au_test_nfs(h_inode->i_sb)
 
421
                     && (mask & MAY_WRITE)
 
422
                     && S_ISDIR(h_inode->i_mode)))
 
423
                mask |= MAY_READ; /* force permission check */
 
424
        return au_test_h_perm(h_inode, mask, dlgt);
 
425
}