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

« back to all changes in this revision

Viewing changes to fs/aufs25/misc.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
 * $Id: misc.c,v 1.4 2008/05/04 23:52:29 sfjro Exp $
 
21
 */
 
22
 
 
23
//#include <linux/fs.h>
 
24
//#include <linux/namei.h>
 
25
//#include <linux/mm.h>
 
26
//#include <asm/uaccess.h>
 
27
#include "aufs.h"
 
28
 
 
29
void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
 
30
{
 
31
        void *q;
 
32
 
 
33
        LKTRTrace("p %p, nused %d, sz %d\n", p, nused, new_sz);
 
34
        AuDebugOn(new_sz <= 0);
 
35
        if (new_sz <= nused)
 
36
                return p;
 
37
 
 
38
        q = krealloc(p, new_sz, gfp);
 
39
        if (q)
 
40
                memset(q + nused, 0, new_sz - nused);
 
41
        return q;
 
42
}
 
43
 
 
44
/* ---------------------------------------------------------------------- */
 
45
 
 
46
struct nameidata *au_dup_nd(struct au_sbinfo *sbinfo, struct nameidata *dst,
 
47
                            struct nameidata *src)
 
48
{
 
49
        LKTRTrace("src %p\n", src);
 
50
 
 
51
        if (src) {
 
52
                *dst = *src;
 
53
                dst->flags &= ~LOOKUP_PARENT;
 
54
                if (unlikely(sbinfo->si_wbr_create != AuWbrCreate_TDP)) {
 
55
                        dst->flags &= ~LOOKUP_CREATE;
 
56
                        dst->intent.open.flags &= ~O_CREAT;
 
57
                }
 
58
        } else
 
59
                dst = NULL;
 
60
 
 
61
        return dst;
 
62
}
 
63
 
 
64
#ifdef CONFIG_AUFS_FAKE_DM
 
65
struct nameidata *au_fake_dm(struct nameidata *fake_nd, struct nameidata *nd,
 
66
                             struct super_block *sb, aufs_bindex_t bindex)
 
67
{
 
68
        LKTRTrace("nd %p, b%d\n", nd, bindex);
 
69
 
 
70
        if (!nd)
 
71
                return NULL;
 
72
 
 
73
        fake_nd->path.dentry = NULL;
 
74
        fake_nd->path.mnt = NULL;
 
75
        return fake_nd;
 
76
}
 
77
 
 
78
void au_fake_dm_release(struct nameidata *fake_nd)
 
79
{
 
80
        /* empty */
 
81
}
 
82
 
 
83
int au_h_create(struct inode *h_dir, struct dentry *h_dentry, int mode,
 
84
                int dlgt, struct nameidata *nd, struct vfsmount *nfsmnt)
 
85
{
 
86
        int err;
 
87
 
 
88
        LKTRTrace("hi%lu, %.*s, 0%o, nd %d, nfsmnt %d\n",
 
89
                  h_dir->i_ino, AuDLNPair(h_dentry), mode, !!nd, !!nfsmnt);
 
90
 
 
91
        err = -ENOSYS;
 
92
        if (!nfsmnt)
 
93
                err = vfsub_create(h_dir, h_dentry, mode, /*nd*/NULL, dlgt);
 
94
 
 
95
        AuTraceErr(err);
 
96
        return err;
 
97
}
 
98
#else
 
99
struct nameidata *au_fake_dm(struct nameidata *fake_nd, struct nameidata *nd,
 
100
                             struct super_block *sb, aufs_bindex_t bindex)
 
101
{
 
102
        LKTRTrace("nd %p, b%d\n", nd, bindex);
 
103
 
 
104
        if (!nd)
 
105
                return NULL;
 
106
 
 
107
        DiMustAnyLock(nd->path.dentry);
 
108
 
 
109
        fake_nd->path.dentry = NULL;
 
110
        fake_nd->path.mnt = NULL;
 
111
 
 
112
        if (bindex <= au_dbend(nd->path.dentry))
 
113
                fake_nd->path.dentry = au_h_dptr(nd->path.dentry, bindex);
 
114
        if (fake_nd->path.dentry) {
 
115
                fake_nd->path.mnt = au_sbr_mnt(sb, bindex);
 
116
                AuDebugOn(!fake_nd->path.mnt);
 
117
                path_get(&fake_nd->path);
 
118
        } else
 
119
                fake_nd = ERR_PTR(-ENOENT);
 
120
 
 
121
        AuTraceErrPtr(fake_nd);
 
122
        return fake_nd;
 
123
}
 
124
 
 
125
void au_fake_dm_release(struct nameidata *fake_nd)
 
126
{
 
127
        if (fake_nd)
 
128
                path_put(&fake_nd->path);
 
129
}
 
130
 
 
131
int au_h_create(struct inode *h_dir, struct dentry *h_dentry, int mode,
 
132
                int dlgt, struct nameidata *nd, struct vfsmount *nfsmnt)
 
133
{
 
134
        int err;
 
135
 
 
136
        LKTRTrace("hi%lu, %.*s, 0%o, nd %d, nfsmnt %d\n",
 
137
                  h_dir->i_ino, AuDLNPair(h_dentry), mode, !!nd, !!nfsmnt);
 
138
 
 
139
        err = -ENOSYS;
 
140
        if (!nfsmnt)
 
141
                err = vfsub_create(h_dir, h_dentry, mode, /*nd*/NULL, dlgt);
 
142
        else {
 
143
                struct nameidata fake_nd;
 
144
 
 
145
                if (nd)
 
146
                        fake_nd = *nd;
 
147
                else
 
148
                        memset(&fake_nd, 0, sizeof(fake_nd));
 
149
                fake_nd.path.dentry = h_dentry;
 
150
                fake_nd.path.mnt = nfsmnt;
 
151
                path_get(&fake_nd.path);
 
152
                fake_nd.flags = LOOKUP_CREATE;
 
153
                fake_nd.intent.open.flags = O_CREAT | FMODE_READ;
 
154
                fake_nd.intent.open.create_mode = mode;
 
155
 
 
156
                err = vfsub_create(h_dir, h_dentry, mode, &fake_nd, dlgt);
 
157
                path_put(&fake_nd.path);
 
158
        }
 
159
 
 
160
        AuTraceErr(err);
 
161
        return err;
 
162
}
 
163
#endif /* CONFIG_AUFS_FAKE_DM */
 
164
 
 
165
/* ---------------------------------------------------------------------- */
 
166
 
 
167
int au_copy_file(struct file *dst, struct file *src, loff_t len,
 
168
                 struct super_block *sb)
 
169
{
 
170
        int err, all_zero;
 
171
        unsigned long blksize;
 
172
        char *buf;
 
173
        struct vfsub_args vargs;
 
174
        /* reduce stack space */
 
175
        struct iattr *ia;
 
176
 
 
177
        LKTRTrace("%.*s, %.*s\n",
 
178
                  AuDLNPair(dst->f_dentry), AuDLNPair(src->f_dentry));
 
179
        AuDebugOn(!(dst->f_mode & FMODE_WRITE));
 
180
#ifdef CONFIG_AUFS_DEBUG
 
181
        {
 
182
                struct dentry *parent;
 
183
                parent = dget_parent(dst->f_dentry);
 
184
                IMustLock(parent->d_inode);
 
185
                dput(parent);
 
186
        }
 
187
#endif
 
188
 
 
189
        err = -ENOMEM;
 
190
        blksize = dst->f_dentry->d_sb->s_blocksize;
 
191
        if (!blksize || PAGE_SIZE < blksize)
 
192
                blksize = PAGE_SIZE;
 
193
        LKTRTrace("blksize %lu\n", blksize);
 
194
        buf = kmalloc(blksize, GFP_TEMPORARY);
 
195
        //buf = NULL;
 
196
        if (unlikely(!buf))
 
197
                goto out;
 
198
        ia = kmalloc(sizeof(*ia), GFP_TEMPORARY);
 
199
        if (unlikely(!ia))
 
200
                goto out_buf;
 
201
 
 
202
#ifdef CONFIG_AUFS_DEBUG
 
203
        if (len > (1 << 22))
 
204
                AuWarn("copying a large file %Ld\n", (long long)len);
 
205
#endif
 
206
        vfsub_args_init(&vargs, NULL, au_opt_test_dlgt(au_mntflags(sb)), 0);
 
207
        err = 0;
 
208
        all_zero = 0;
 
209
        src->f_pos = 0;
 
210
        dst->f_pos = 0;
 
211
        while (len) {
 
212
                size_t sz, rbytes, wbytes, i;
 
213
                char *p;
 
214
 
 
215
                LKTRTrace("len %lld\n", len);
 
216
                sz = blksize;
 
217
                if (len < blksize)
 
218
                        sz = len;
 
219
 
 
220
                /* support LSM and notify */
 
221
                rbytes = 0;
 
222
                // signal_pending
 
223
                while (!rbytes || err == -EAGAIN || err == -EINTR) {
 
224
                        rbytes = vfsub_read_k(src, buf, sz, &src->f_pos,
 
225
                                              vfsub_ftest(vargs.flags, DLGT));
 
226
                        err = rbytes;
 
227
                }
 
228
                if (unlikely(err < 0))
 
229
                        break;
 
230
 
 
231
                all_zero = 0;
 
232
                if (len >= rbytes && rbytes == blksize) {
 
233
                        //todo: try bitmap or memcmp()/get_zeroed_page()
 
234
                        unsigned long *ulp;
 
235
                        size_t n;
 
236
 
 
237
                        all_zero = 1;
 
238
                        ulp = (void *)buf;
 
239
                        n = rbytes / sizeof(*ulp);
 
240
                        i = n;
 
241
                        while (n-- > 0 && all_zero)
 
242
                                all_zero = !*ulp++;
 
243
                        p = (void *)ulp;
 
244
                        i *= sizeof(*ulp);
 
245
                        for (; all_zero && i < rbytes; i++)
 
246
                                all_zero = !*p++;
 
247
                }
 
248
                if (!all_zero) {
 
249
                        wbytes = rbytes;
 
250
                        p = buf;
 
251
                        while (wbytes) {
 
252
                                size_t b;
 
253
                                /* support LSM and notify */
 
254
                                b = vfsub_write_k(dst, p, wbytes, &dst->f_pos,
 
255
                                                  &vargs);
 
256
                                err = b;
 
257
                                // signal_pending
 
258
                                if (unlikely(err == -EAGAIN || err == -EINTR))
 
259
                                        continue;
 
260
                                if (unlikely(err < 0))
 
261
                                        break;
 
262
                                wbytes -= b;
 
263
                                p += b;
 
264
                        }
 
265
                } else {
 
266
                        loff_t res;
 
267
                        LKTRLabel(hole);
 
268
                        res = vfsub_llseek(dst, rbytes, SEEK_CUR);
 
269
                        err = res;
 
270
                        if (unlikely(res < 0))
 
271
                                break;
 
272
                }
 
273
                len -= rbytes;
 
274
                err = 0;
 
275
        }
 
276
 
 
277
        /* the last block may be a hole */
 
278
        if (unlikely(!err && all_zero)) {
 
279
                struct dentry *h_d = dst->f_dentry;
 
280
                struct inode *h_i = h_d->d_inode;
 
281
 
 
282
                LKTRLabel(last hole);
 
283
                do {
 
284
                        // signal_pending
 
285
                        err = vfsub_write_k(dst, "\0", 1, &dst->f_pos, &vargs);
 
286
                } while (err == -EAGAIN || err == -EINTR);
 
287
                if (err == 1) {
 
288
                        ia->ia_size = dst->f_pos;
 
289
                        ia->ia_valid = ATTR_SIZE | ATTR_FILE;
 
290
                        ia->ia_file = dst;
 
291
                        mutex_lock_nested(&h_i->i_mutex, AuLsc_I_CHILD2);
 
292
                        err = vfsub_notify_change(h_d, ia, &vargs);
 
293
                        mutex_unlock(&h_i->i_mutex);
 
294
                }
 
295
        }
 
296
 
 
297
        kfree(ia);
 
298
 out_buf:
 
299
        kfree(buf);
 
300
 out:
 
301
        AuTraceErr(err);
 
302
        return err;
 
303
}