~ubuntu-branches/ubuntu/precise/aufs/precise

« back to all changes in this revision

Viewing changes to fs/aufs/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2007-05-09 15:29:28 UTC
  • Revision ID: james.westby@ubuntu.com-20070509152928-4sywrmkifvz0bq02
Tags: upstream-0+20070509
ImportĀ upstreamĀ versionĀ 0+20070509

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005, 2006, 2007 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
/* $Id: misc.c,v 1.30 2007/04/23 00:56:45 sfjro Exp $ */
 
20
 
 
21
//#include <linux/fs.h>
 
22
//#include <linux/namei.h>
 
23
//#include <linux/mm.h>
 
24
//#include <asm/uaccess.h>
 
25
#include "aufs.h"
 
26
 
 
27
void *au_kzrealloc(void *p, int nused, int new_sz, gfp_t gfp)
 
28
{
 
29
        void *q;
 
30
 
 
31
        LKTRTrace("p %p, nused %d, sz %d, ksize %d\n",
 
32
                  p, nused, new_sz, ksize(p));
 
33
        DEBUG_ON(new_sz <= 0);
 
34
        if (new_sz <= nused)
 
35
                return p;
 
36
        if (new_sz <= ksize(p)) {
 
37
                memset(p + nused, 0, new_sz - nused);
 
38
                return p;
 
39
        }
 
40
 
 
41
        q = kmalloc(new_sz, gfp);
 
42
        //q = NULL;
 
43
        if (unlikely(!q))
 
44
                return NULL;
 
45
        memcpy(q, p, nused);
 
46
        memset(q + nused, 0, new_sz - nused);
 
47
        //smp_mb();
 
48
        kfree(p);
 
49
        return q;
 
50
}
 
51
 
 
52
/* ---------------------------------------------------------------------- */
 
53
 
 
54
// todo: make it inline
 
55
struct nameidata *fake_dm(struct nameidata *fake_nd, struct nameidata *nd,
 
56
                          struct super_block *sb, aufs_bindex_t bindex)
 
57
{
 
58
        LKTRTrace("nd %p, b%d\n", nd, bindex);
 
59
 
 
60
        if (!nd)
 
61
                return NULL;
 
62
 
 
63
        fake_nd->dentry = NULL;
 
64
        fake_nd->mnt = NULL;
 
65
 
 
66
#ifndef CONFIG_AUFS_FAKE_DM
 
67
        DiMustAnyLock(nd->dentry);
 
68
 
 
69
        if (bindex <= dbend(nd->dentry))
 
70
                fake_nd->dentry = au_h_dptr_i(nd->dentry, bindex);
 
71
        if (fake_nd->dentry) {
 
72
                dget(fake_nd->dentry);
 
73
                fake_nd->mnt = sbr_mnt(sb, bindex);
 
74
                DEBUG_ON(!fake_nd->mnt);
 
75
                mntget(fake_nd->mnt);
 
76
        } else
 
77
                fake_nd = ERR_PTR(-ENOENT);
 
78
#endif
 
79
 
 
80
        TraceErrPtr(fake_nd);
 
81
        return fake_nd;
 
82
}
 
83
 
 
84
void fake_dm_release(struct nameidata *fake_nd)
 
85
{
 
86
#ifndef CONFIG_AUFS_FAKE_DM
 
87
        if (fake_nd) {
 
88
                mntput(fake_nd->mnt);
 
89
                dput(fake_nd->dentry);
 
90
        }
 
91
#endif
 
92
}
 
93
 
 
94
/* ---------------------------------------------------------------------- */
 
95
 
 
96
int au_copy_file(struct file *dst, struct file *src, loff_t len,
 
97
                 struct super_block *sb, int *sparse)
 
98
{
 
99
        int err, all_zero, dlgt;
 
100
        unsigned long blksize;
 
101
        char *buf;
 
102
        /* reduce stack space */
 
103
        struct iattr *ia;
 
104
 
 
105
        LKTRTrace("%.*s, %.*s\n",
 
106
                  DLNPair(dst->f_dentry), DLNPair(src->f_dentry));
 
107
        DEBUG_ON(!(dst->f_mode & FMODE_WRITE));
 
108
        IMustLock(dst->f_dentry->d_parent->d_inode);
 
109
 
 
110
        err = -ENOMEM;
 
111
        blksize = dst->f_dentry->d_sb->s_blocksize;
 
112
        if (!blksize || PAGE_SIZE < blksize)
 
113
                blksize = PAGE_SIZE;
 
114
        LKTRTrace("blksize %lu\n", blksize);
 
115
        buf = kmalloc(blksize, GFP_KERNEL);
 
116
        //buf = NULL;
 
117
        if (unlikely(!buf))
 
118
                goto out;
 
119
        ia = kmalloc(sizeof(*ia), GFP_KERNEL);
 
120
        if (unlikely(!ia))
 
121
                goto out_buf;
 
122
 
 
123
        dlgt = need_dlgt(sb);
 
124
        err = all_zero = 0;
 
125
        dst->f_pos = src->f_pos = 0;
 
126
        while (len) {
 
127
                size_t sz, rbytes, wbytes;
 
128
                char *p;
 
129
                int i;
 
130
 
 
131
                LKTRTrace("len %lld\n", len);
 
132
                sz = blksize;
 
133
                if (len < blksize)
 
134
                        sz = len;
 
135
 
 
136
                /* support LSM and notify */
 
137
                rbytes = 0;
 
138
                while (!rbytes || err == -EAGAIN || err == -EINTR)
 
139
                        err = rbytes = vfsub_read_k(src, buf, sz, &src->f_pos,
 
140
                                                    dlgt);
 
141
                if (unlikely(err < 0))
 
142
                        break;
 
143
 
 
144
                all_zero = 0;
 
145
                if (len >= rbytes && rbytes == blksize) {
 
146
                        all_zero = 1;
 
147
                        p = buf;
 
148
                        for (i = 0; all_zero && i < rbytes; i++)
 
149
                                all_zero = !*p++;
 
150
                }
 
151
                if (!all_zero) {
 
152
                        wbytes = rbytes;
 
153
                        p = buf;
 
154
                        while (wbytes) {
 
155
                                size_t b;
 
156
                                /* support LSM and notify */
 
157
                                err = b = vfsub_write_k(dst, p, wbytes,
 
158
                                                        &dst->f_pos, dlgt);
 
159
                                if (unlikely(err == -EAGAIN || err == -EINTR))
 
160
                                        continue;
 
161
                                if (unlikely(err < 0))
 
162
                                        break;
 
163
                                wbytes -= b;
 
164
                                p += b;
 
165
                        }
 
166
                } else {
 
167
                        loff_t res;
 
168
                        LKTRLabel(hole);
 
169
                        *sparse = 1;
 
170
                        err = res = vfsub_llseek(dst, rbytes, SEEK_CUR);
 
171
                        if (unlikely(res < 0))
 
172
                                break;
 
173
                }
 
174
                len -= rbytes;
 
175
                err = 0;
 
176
        }
 
177
 
 
178
        /* the last block may be a hole */
 
179
        if (unlikely(!err && all_zero)) {
 
180
                struct dentry *h_d = dst->f_dentry;
 
181
                struct inode *h_i = h_d->d_inode;
 
182
 
 
183
                LKTRLabel(last hole);
 
184
                do {
 
185
                        err = vfsub_write_k(dst, "\0", 1, &dst->f_pos, dlgt);
 
186
                } while (err == -EAGAIN || err == -EINTR);
 
187
                if (err == 1) {
 
188
                        ia->ia_size = dst->f_pos;
 
189
                        ia->ia_valid = ATTR_SIZE | ATTR_FILE;
 
190
                        ia->ia_file = dst;
 
191
                        hi_lock_child2(h_i);
 
192
                        err = vfsub_notify_change(h_d, ia, dlgt);
 
193
                        i_unlock(h_i);
 
194
                }
 
195
        }
 
196
 
 
197
        kfree(ia);
 
198
 out_buf:
 
199
        kfree(buf);
 
200
 out:
 
201
        TraceErr(err);
 
202
        return err;
 
203
}
 
204
 
 
205
/* ---------------------------------------------------------------------- */
 
206
 
 
207
int test_ro(struct super_block *sb, aufs_bindex_t bindex, struct inode *inode)
 
208
{
 
209
        int err;
 
210
 
 
211
        err = br_rdonly(stobr(sb, bindex));
 
212
        if (!err && inode) {
 
213
                struct inode *hi = au_h_iptr_i(inode, bindex);
 
214
                if (hi)
 
215
                        err = IS_IMMUTABLE(hi) ? -EROFS : 0;
 
216
        }
 
217
        return err;
 
218
}
 
219
 
 
220
int au_test_perm(struct inode *hidden_inode, int mask, int dlgt)
 
221
{
 
222
        if (!current->fsuid)
 
223
                return 0;
 
224
        if (unlikely(au_is_nfs(hidden_inode->i_sb)
 
225
                     && (mask & MAY_WRITE)
 
226
                     && S_ISDIR(hidden_inode->i_mode)))
 
227
                mask |= MAY_READ; /* force permission check */
 
228
        return vfsub_permission(hidden_inode, mask, NULL, dlgt);
 
229
}