Whamcloud - gitweb
8d9b80f8cf79643fbb41f8a2151f3f9572fe296e
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-in-ea-2.4.20.patch
1 Index: linux-2.4.24/fs/ext3/extents-in-ea.c
2 ===================================================================
3 --- linux-2.4.24.orig/fs/ext3/extents-in-ea.c   2003-01-30 18:24:37.000000000 +0800
4 +++ linux-2.4.24/fs/ext3/extents-in-ea.c        2004-06-24 21:53:00.000000000 +0800
5 @@ -0,0 +1,219 @@
6 +/*
7 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
8 + *
9 + * This program is free software; you can redistribute it and/or modify
10 + * it under the terms of the GNU General Public License version 2 as
11 + * published by the Free Software Foundation.
12 + *
13 + * This program is distributed in the hope that it will be useful,
14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 + * GNU General Public License for more details.
17 + *
18 + * You should have received a copy of the GNU General Public Licens
19 + * along with this program; if not, write to the Free Software
20 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
21 + */
22 +
23 +#include <linux/module.h>
24 +#include <linux/fs.h>
25 +#include <linux/time.h>
26 +#include <linux/ext3_jbd.h>
27 +#include <linux/jbd.h>
28 +#include <linux/smp_lock.h>
29 +#include <linux/highuid.h>
30 +#include <linux/pagemap.h>
31 +#include <linux/quotaops.h>
32 +#include <linux/string.h>
33 +#include <linux/ext3_extents.h>
34 +#include <linux/ext3_xattr.h>
35 +#include <linux/slab.h>
36 +#include <asm/uaccess.h>
37 +
38 +static int ext3_get_ea_write_access(handle_t *handle, void *buffer)
39 +{
40 +       struct buffer_head *bh = (struct buffer_head *) buffer;
41 +       return ext3_journal_get_write_access(handle, bh);
42 +}
43 +
44 +static int ext3_mark_ea_buffer_dirty(handle_t *handle, void *buffer)
45 +{
46 +       struct buffer_head *bh = (struct buffer_head *) buffer;
47 +       ext3_journal_dirty_metadata(handle, bh);
48 +       return 0;
49 +}
50 +
51 +int ext3_init_tree_in_ea_desc(struct ext3_extents_tree *tree,
52 +                               struct inode *inode, int name_index,
53 +                               const char *eaname)
54 +{
55 +       struct buffer_head *bh;
56 +       int offset, err, size;
57 +
58 +       err = ext3_xattr_get_ea_loc(inode, name_index, eaname,
59 +                                       &bh, &offset, &size);
60 +       if (err)
61 +               return err;
62 +
63 +       EXT_ASSERT(bh);
64 +       EXT_ASSERT(size >= sizeof(struct ext3_extent_header)
65 +                               + sizeof(struct ext3_extent));
66 +       tree->inode = inode;
67 +       tree->root = (void *) bh->b_data + offset;
68 +       tree->buffer_len = size;
69 +       tree->buffer = (void *) bh;
70 +       tree->get_write_access = ext3_get_ea_write_access;
71 +       tree->mark_buffer_dirty = ext3_mark_ea_buffer_dirty;
72 +       tree->mergable = NULL;
73 +       tree->remove_extent = NULL;
74 +       tree->remove_extent_credits = NULL;
75 +       tree->new_block = NULL;
76 +       tree->cex = NULL;       /* FIXME: add cache store later */
77 +       return 0;
78 +}
79 +
80 +void ext3_release_tree_in_ea_desc(struct ext3_extents_tree *tree)
81 +{
82 +       struct buffer_head *bh;
83 +
84 +       bh = (struct buffer_head *) tree->buffer;
85 +       EXT_ASSERT(bh);
86 +       brelse(bh);
87 +}
88 +
89 +int ext3_init_tree_in_ea(struct inode *inode, int name_index,
90 +                               const char *eaname, int size)
91 +{
92 +       struct ext3_extents_tree tree;
93 +       handle_t *handle;
94 +       char *root;
95 +       int err;
96 +
97 +       root = kmalloc(size, GFP_USER);
98 +       if (!root)
99 +               return -ENOMEM;
100 +       memset(root, 0, size);
101 +
102 +       /* first, create ea to store root of the tree */
103 +       handle = ext3_journal_start(inode, EXT3_ALLOC_NEEDED + 3);
104 +       if (IS_ERR(handle))
105 +               return PTR_ERR(handle);
106 +       if ((err = ext3_xattr_set(handle, inode, name_index,
107 +                                       eaname, root, size, 0)))
108 +               goto out;
109 +       if ((err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname)))
110 +               goto out;
111 +       err = ext3_extent_tree_init(handle, &tree);
112 +       ext3_release_tree_in_ea_desc(&tree);
113 +out:
114 +       ext3_journal_stop(handle, inode);
115 +       kfree(root);
116 +       return err;
117 +}
118 +
119 +static int
120 +ext3_ext_in_ea_new_extent(struct ext3_extents_tree *tree,
121 +                       struct ext3_ext_path *path,
122 +                       struct ext3_extent *newex, int exist)
123 +{
124 +       struct inode *inode = tree->inode;
125 +       handle_t *handle;
126 +       int needed, err;
127 +       unsigned long tgen;
128 +
129 +       if (exist)
130 +               return EXT_CONTINUE;
131 +
132 +       tgen = EXT_GENERATION(tree);
133 +       needed = ext3_ext_calc_credits_for_insert(tree, path);
134 +       up_write(&EXT3_I(inode)->truncate_sem);
135 +       handle = ext3_journal_start(tree->inode, needed + 10);
136 +       if (IS_ERR(handle)) {
137 +               down_write(&EXT3_I(inode)->truncate_sem);
138 +               return PTR_ERR(handle);
139 +       }
140 +
141 +       if (tgen != EXT_GENERATION(tree)) {
142 +               /* the tree has changed. so path can be invalid at moment */
143 +               ext3_journal_stop(handle, inode);
144 +               down_write(&EXT3_I(inode)->truncate_sem);
145 +               return EXT_REPEAT;
146 +       }
147 +
148 +       down_write(&EXT3_I(inode)->truncate_sem);
149 +
150 +       /* insert new extent */
151 +       newex->e_start = 0;
152 +       err = ext3_ext_insert_extent(handle, tree, path, newex);
153 +       if (!err)
154 +               ext3_journal_stop(handle, tree->inode);
155 +
156 +       return err;
157 +}
158 +
159 +int ext3_ext_in_ea_alloc_space(struct inode *inode, int name_index,
160 +                               const char *eaname, unsigned long from,
161 +                               unsigned long num)
162 +{
163 +       struct ext3_extents_tree tree;
164 +       int err;
165 +
166 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
167 +       if (err == 0) {
168 +               down_write(&EXT3_I(inode)->truncate_sem);       
169 +               err = ext3_ext_walk_space(&tree, from, num,
170 +                                               ext3_ext_in_ea_new_extent);
171 +               ext3_release_tree_in_ea_desc(&tree);
172 +               up_write(&EXT3_I(inode)->truncate_sem);
173 +       }
174 +       return err;
175 +}
176 +
177 +int ext3_ext_in_ea_remove_space(struct inode *inode, int name_index,
178 +                               const char *eaname, unsigned long from,
179 +                               unsigned long num)
180 +{
181 +       struct ext3_extents_tree tree;
182 +       int err;
183 +
184 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
185 +       if (err == 0) {
186 +               err = ext3_ext_remove_space(&tree, from, num);
187 +               ext3_release_tree_in_ea_desc(&tree);
188 +       }
189 +       return err;
190 +}
191 +
192 +int ext3_ext_in_ea_presence(struct inode *inode, int name_index,
193 +                               const char *eaname, unsigned long block)
194 +{
195 +       struct ext3_extents_tree tree;
196 +       struct ext3_ext_path *path;
197 +       struct ext3_extent *ex;
198 +       int err, depth;
199 +
200 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
201 +       if (err)
202 +               return err;
203 +
204 +       /* find extent for this block */
205 +       path = ext3_ext_find_extent(&tree, block, NULL);
206 +       if (IS_ERR(path)) {
207 +               err = PTR_ERR(path);
208 +               goto out;
209 +       }
210 +
211 +       depth = EXT_DEPTH(&tree);
212 +       ex = path[depth].p_ext;
213 +       if (!ex) {
214 +               /* there is no extent yet */
215 +               goto out;
216 +       }
217 +
218 +       if (block >= ex->e_block && block < ex->e_block + ex->e_num)
219 +               err = 1;
220 +out:
221 +       ext3_release_tree_in_ea_desc(&tree);
222 +       return err;
223 +}
224 +
225 Index: linux-2.4.24/fs/ext3/Makefile
226 ===================================================================
227 --- linux-2.4.24.orig/fs/ext3/Makefile  2004-06-09 11:31:06.000000000 +0800
228 +++ linux-2.4.24/fs/ext3/Makefile       2004-06-24 21:36:29.000000000 +0800
229 @@ -19,7 +19,7 @@
230  obj-m    := $(O_TARGET)
231  
232  export-objs += xattr.o
233 -obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o
234 +obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o extents-in-ea.o
235  obj-$(CONFIG_EXT3_FS_XATTR_USER) += xattr_user.o
236  
237  include $(TOPDIR)/Rules.make
238 Index: linux-2.4.24/fs/ext3/xattr.c
239 ===================================================================
240 --- linux-2.4.24.orig/fs/ext3/xattr.c   2004-06-09 11:31:06.000000000 +0800
241 +++ linux-2.4.24/fs/ext3/xattr.c        2004-06-24 21:36:29.000000000 +0800
242 @@ -771,7 +771,8 @@
243   */
244  int
245  ext3_xattr_ibody_find(struct inode *inode, int name_index,
246 -               const char *name, struct ext3_xattr_entry *rentry, int *free)
247 +               const char *name, struct ext3_xattr_entry *rentry, int *free,
248 +               struct buffer_head **bh, int *offset)
249  {
250         struct ext3_xattr_entry *last;
251         struct ext3_inode *raw_inode;
252 @@ -818,6 +819,15 @@
253                     name_len == last->e_name_len &&
254                     !memcmp(name, last->e_name, name_len)) {
255                         memcpy(rentry, last, sizeof(struct ext3_xattr_entry));
256 +                       if (offset) {
257 +                               void *voff;
258 +                               voff = start + le16_to_cpu(last->e_value_offs);
259 +                               *offset = voff - (void *) iloc.bh->b_data;
260 +                       }
261 +                       if (bh) {
262 +                               get_bh(iloc.bh);        
263 +                               *bh = iloc.bh;
264 +                       }
265                         ret = 0;
266                 } else {
267                         *free -= EXT3_XATTR_LEN(last->e_name_len);
268 @@ -838,7 +848,8 @@
269   */
270  int
271  ext3_xattr_block_find(struct inode *inode, int name_index, const char *name,
272 -              struct ext3_xattr_entry *rentry, int *free)
273 +              struct ext3_xattr_entry *rentry, int *free,
274 +              struct buffer_head **tbh, int *offset)
275  {
276         struct buffer_head *bh = NULL;
277         struct ext3_xattr_entry *entry;
278 @@ -881,6 +892,12 @@
279                     memcmp(name, entry->e_name, name_len) == 0) {
280                         memcpy(rentry, entry, sizeof(struct ext3_xattr_entry));
281                         error = 0;
282 +                       if (offset)
283 +                               *offset = le16_to_cpu(entry->e_value_offs);
284 +                       if (tbh) {
285 +                               get_bh(bh);     
286 +                               *tbh = bh;
287 +                       }
288                 } else {
289                         *free -= EXT3_XATTR_LEN(entry->e_name_len);
290                         *free -= le32_to_cpu(entry->e_value_size);
291 @@ -1073,7 +1090,8 @@
292                 return -ERANGE;
293  
294         /* try to find attribute in inode body */
295 -       err = ext3_xattr_ibody_find(inode, name_index, name, &entry, &free1);
296 +       err = ext3_xattr_ibody_find(inode, name_index, name,
297 +                                       &entry, &free1, NULL, NULL);
298         if (err == 0) {
299                 /* found EA in inode */
300                 found = 1;
301 @@ -1082,7 +1100,7 @@
302                 /* there is no such attribute in inode body */
303                 /* try to find attribute in dedicated block */
304                 err = ext3_xattr_block_find(inode, name_index, name,
305 -                                               &entry, &free2);
306 +                                               &entry, &free2, NULL, NULL);
307                 if (err != 0 && err != -ENOENT) {
308                         /* not found EA in block */
309                         goto finish;    
310 @@ -1138,6 +1156,38 @@
311         return err;
312  }
313  
314 +int ext3_xattr_get_ea_loc(struct inode *inode, int name_index,
315 +                               const char *name, struct buffer_head **bh,
316 +                               int *offset, int *size)
317 +{
318 +       int free1 = -1, free2 = -1, err, name_len;
319 +       struct ext3_xattr_entry entry;
320 +       
321 +       ea_idebug(inode, "name=%d.%s", name_index, name);
322 +
323 +       if (name == NULL)
324 +               return -EINVAL;
325 +       name_len = strlen(name);
326 +       if (name_len > 255)
327 +               return -ERANGE;
328 +
329 +       down(&ext3_xattr_sem);
330 +
331 +       /* try to find attribute in inode body */
332 +       err = ext3_xattr_ibody_find(inode, name_index, name,
333 +                                       &entry, &free1, bh, offset);
334 +       if (err == -ENOENT) {
335 +               /* there is no such attribute in inode body */
336 +               /* try to find attribute in dedicated block */
337 +               err = ext3_xattr_block_find(inode, name_index, name,
338 +                                               &entry, &free2, bh, offset);
339 +       }
340 +       if (err == 0 && size)
341 +               *size = le32_to_cpu(entry.e_value_size);
342 +       up(&ext3_xattr_sem);
343 +       return err;
344 +}
345 +
346  /*
347   * ext3_xattr_block_set()
348   *
349 Index: linux-2.4.24/include/linux/ext3_xattr.h
350 ===================================================================
351 --- linux-2.4.24.orig/include/linux/ext3_xattr.h        2004-06-09 11:31:06.000000000 +0800
352 +++ linux-2.4.24/include/linux/ext3_xattr.h     2004-06-24 21:36:29.000000000 +0800
353 @@ -80,6 +80,7 @@
354  extern int ext3_xattr_get(struct inode *, int, const char *, void *, size_t);
355  extern int ext3_xattr_list(struct inode *, char *, size_t);
356  extern int ext3_xattr_set(handle_t *handle, struct inode *, int, const char *, const void *, size_t, int);
357 +extern int ext3_xattr_get_ea_loc(struct inode *, int, const char *, struct buffer_head **, int *, int *);
358  
359  extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
360  extern void ext3_xattr_put_super(struct super_block *);