Whamcloud - gitweb
b=3550
[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 13:24:37.000000000 +0300
4 +++ linux-2.4.24/fs/ext3/extents-in-ea.c        2004-08-06 03:54:47.000000000 +0400
5 @@ -0,0 +1,223 @@
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 +static struct ext3_extents_helpers ext3_ea_helpers = {
52 +       .get_write_access       = ext3_get_ea_write_access,
53 +       .mark_buffer_dirty      = ext3_mark_ea_buffer_dirty,
54 +       .mergable               = NULL,
55 +       .new_block              = NULL,
56 +       .remove_extent          = NULL,
57 +       .remove_extent_credits  = NULL,
58 +};
59 +
60 +int ext3_init_tree_in_ea_desc(struct ext3_extents_tree *tree,
61 +                               struct inode *inode, int name_index,
62 +                               const char *eaname)
63 +{
64 +       struct buffer_head *bh;
65 +       int offset, err, size;
66 +
67 +       err = ext3_xattr_get_ea_loc(inode, name_index, eaname,
68 +                                       &bh, &offset, &size);
69 +       if (err)
70 +               return err;
71 +
72 +       EXT_ASSERT(bh);
73 +       EXT_ASSERT(size >= sizeof(struct ext3_extent_header)
74 +                               + sizeof(struct ext3_extent));
75 +       tree->inode = inode;
76 +       tree->root = (void *) bh->b_data + offset;
77 +       tree->buffer_len = size;
78 +       tree->buffer = (void *) bh;
79 +       tree->ops = &ext3_ea_helpers;
80 +       tree->cex = NULL;       /* FIXME: add cache store later */
81 +       return 0;
82 +}
83 +
84 +void ext3_release_tree_in_ea_desc(struct ext3_extents_tree *tree)
85 +{
86 +       struct buffer_head *bh;
87 +
88 +       bh = (struct buffer_head *) tree->buffer;
89 +       EXT_ASSERT(bh);
90 +       brelse(bh);
91 +}
92 +
93 +int ext3_init_tree_in_ea(struct inode *inode, int name_index,
94 +                               const char *eaname, int size)
95 +{
96 +       struct ext3_extents_tree tree;
97 +       handle_t *handle;
98 +       char *root;
99 +       int err;
100 +
101 +       root = kmalloc(size, GFP_USER);
102 +       if (!root)
103 +               return -ENOMEM;
104 +       memset(root, 0, size);
105 +
106 +       /* first, create ea to store root of the tree */
107 +       handle = ext3_journal_start(inode, EXT3_ALLOC_NEEDED + 3);
108 +       if (IS_ERR(handle))
109 +               return PTR_ERR(handle);
110 +       if ((err = ext3_xattr_set(handle, inode, name_index,
111 +                                       eaname, root, size, 0)))
112 +               goto out;
113 +       if ((err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname)))
114 +               goto out;
115 +       err = ext3_extent_tree_init(handle, &tree);
116 +       ext3_release_tree_in_ea_desc(&tree);
117 +out:
118 +       ext3_journal_stop(handle, inode);
119 +       kfree(root);
120 +       return err;
121 +}
122 +
123 +static int
124 +ext3_ext_in_ea_new_extent(struct ext3_extents_tree *tree,
125 +                       struct ext3_ext_path *path,
126 +                       struct ext3_extent *newex, int exist)
127 +{
128 +       struct inode *inode = tree->inode;
129 +       handle_t *handle;
130 +       int needed, err;
131 +       unsigned long tgen;
132 +
133 +       if (exist)
134 +               return EXT_CONTINUE;
135 +
136 +       tgen = EXT_GENERATION(tree);
137 +       needed = ext3_ext_calc_credits_for_insert(tree, path);
138 +       up_write(&EXT3_I(inode)->truncate_sem);
139 +       handle = ext3_journal_start(tree->inode, needed + 10);
140 +       if (IS_ERR(handle)) {
141 +               down_write(&EXT3_I(inode)->truncate_sem);
142 +               return PTR_ERR(handle);
143 +       }
144 +
145 +       if (tgen != EXT_GENERATION(tree)) {
146 +               /* the tree has changed. so path can be invalid at moment */
147 +               ext3_journal_stop(handle, inode);
148 +               down_write(&EXT3_I(inode)->truncate_sem);
149 +               return EXT_REPEAT;
150 +       }
151 +
152 +       down_write(&EXT3_I(inode)->truncate_sem);
153 +
154 +       /* insert new extent */
155 +       newex->e_start = 0;
156 +       err = ext3_ext_insert_extent(handle, tree, path, newex);
157 +       if (!err)
158 +               ext3_journal_stop(handle, tree->inode);
159 +
160 +       return err;
161 +}
162 +
163 +int ext3_ext_in_ea_alloc_space(struct inode *inode, int name_index,
164 +                               const char *eaname, unsigned long from,
165 +                               unsigned long num)
166 +{
167 +       struct ext3_extents_tree tree;
168 +       int err;
169 +
170 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
171 +       if (err == 0) {
172 +               down_write(&EXT3_I(inode)->truncate_sem);       
173 +               err = ext3_ext_walk_space(&tree, from, num,
174 +                                               ext3_ext_in_ea_new_extent);
175 +               ext3_release_tree_in_ea_desc(&tree);
176 +               up_write(&EXT3_I(inode)->truncate_sem);
177 +       }
178 +       return err;
179 +}
180 +
181 +int ext3_ext_in_ea_remove_space(struct inode *inode, int name_index,
182 +                               const char *eaname, unsigned long from,
183 +                               unsigned long num)
184 +{
185 +       struct ext3_extents_tree tree;
186 +       int err;
187 +
188 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
189 +       if (err == 0) {
190 +               err = ext3_ext_remove_space(&tree, from, num);
191 +               ext3_release_tree_in_ea_desc(&tree);
192 +       }
193 +       return err;
194 +}
195 +
196 +int ext3_ext_in_ea_presence(struct inode *inode, int name_index,
197 +                               const char *eaname, unsigned long block)
198 +{
199 +       struct ext3_extents_tree tree;
200 +       struct ext3_ext_path *path;
201 +       struct ext3_extent *ex;
202 +       int err, depth;
203 +
204 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
205 +       if (err)
206 +               return err;
207 +
208 +       /* find extent for this block */
209 +       path = ext3_ext_find_extent(&tree, block, NULL);
210 +       if (IS_ERR(path)) {
211 +               err = PTR_ERR(path);
212 +               goto out;
213 +       }
214 +
215 +       depth = EXT_DEPTH(&tree);
216 +       ex = path[depth].p_ext;
217 +       if (!ex) {
218 +               /* there is no extent yet */
219 +               goto out;
220 +       }
221 +
222 +       if (block >= ex->e_block && block < ex->e_block + ex->e_num)
223 +               err = 1;
224 +out:
225 +       ext3_release_tree_in_ea_desc(&tree);
226 +       return err;
227 +}
228 +
229 Index: linux-2.4.24/fs/ext3/Makefile
230 ===================================================================
231 --- linux-2.4.24.orig/fs/ext3/Makefile  2004-08-06 02:43:24.000000000 +0400
232 +++ linux-2.4.24/fs/ext3/Makefile       2004-08-06 03:52:43.000000000 +0400
233 @@ -19,7 +19,7 @@
234  obj-m    := $(O_TARGET)
235  
236  export-objs += xattr.o
237 -obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o
238 +obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o extents-in-ea.o
239  obj-$(CONFIG_EXT3_FS_XATTR_USER) += xattr_user.o
240  
241  include $(TOPDIR)/Rules.make
242 Index: linux-2.4.24/fs/ext3/xattr.c
243 ===================================================================
244 --- linux-2.4.24.orig/fs/ext3/xattr.c   2004-08-06 00:47:18.000000000 +0400
245 +++ linux-2.4.24/fs/ext3/xattr.c        2004-08-06 03:52:43.000000000 +0400
246 @@ -771,7 +771,8 @@
247   */
248  int
249  ext3_xattr_ibody_find(struct inode *inode, int name_index,
250 -               const char *name, struct ext3_xattr_entry *rentry, int *free)
251 +               const char *name, struct ext3_xattr_entry *rentry, int *free,
252 +               struct buffer_head **bh, int *offset)
253  {
254         struct ext3_xattr_entry *last;
255         struct ext3_inode *raw_inode;
256 @@ -818,6 +819,15 @@
257                     name_len == last->e_name_len &&
258                     !memcmp(name, last->e_name, name_len)) {
259                         memcpy(rentry, last, sizeof(struct ext3_xattr_entry));
260 +                       if (offset) {
261 +                               void *voff;
262 +                               voff = start + le16_to_cpu(last->e_value_offs);
263 +                               *offset = voff - (void *) iloc.bh->b_data;
264 +                       }
265 +                       if (bh) {
266 +                               get_bh(iloc.bh);        
267 +                               *bh = iloc.bh;
268 +                       }
269                         ret = 0;
270                 } else {
271                         *free -= EXT3_XATTR_LEN(last->e_name_len);
272 @@ -838,7 +848,8 @@
273   */
274  int
275  ext3_xattr_block_find(struct inode *inode, int name_index, const char *name,
276 -              struct ext3_xattr_entry *rentry, int *free)
277 +              struct ext3_xattr_entry *rentry, int *free,
278 +              struct buffer_head **tbh, int *offset)
279  {
280         struct buffer_head *bh = NULL;
281         struct ext3_xattr_entry *entry;
282 @@ -881,6 +892,12 @@
283                     memcmp(name, entry->e_name, name_len) == 0) {
284                         memcpy(rentry, entry, sizeof(struct ext3_xattr_entry));
285                         error = 0;
286 +                       if (offset)
287 +                               *offset = le16_to_cpu(entry->e_value_offs);
288 +                       if (tbh) {
289 +                               get_bh(bh);     
290 +                               *tbh = bh;
291 +                       }
292                 } else {
293                         *free -= EXT3_XATTR_LEN(entry->e_name_len);
294                         *free -= le32_to_cpu(entry->e_value_size);
295 @@ -1073,7 +1090,8 @@
296                 return -ERANGE;
297  
298         /* try to find attribute in inode body */
299 -       err = ext3_xattr_ibody_find(inode, name_index, name, &entry, &free1);
300 +       err = ext3_xattr_ibody_find(inode, name_index, name,
301 +                                       &entry, &free1, NULL, NULL);
302         if (err == 0) {
303                 /* found EA in inode */
304                 found = 1;
305 @@ -1082,7 +1100,7 @@
306                 /* there is no such attribute in inode body */
307                 /* try to find attribute in dedicated block */
308                 err = ext3_xattr_block_find(inode, name_index, name,
309 -                                               &entry, &free2);
310 +                                               &entry, &free2, NULL, NULL);
311                 if (err != 0 && err != -ENOENT) {
312                         /* not found EA in block */
313                         goto finish;    
314 @@ -1138,6 +1156,38 @@
315         return err;
316  }
317  
318 +int ext3_xattr_get_ea_loc(struct inode *inode, int name_index,
319 +                               const char *name, struct buffer_head **bh,
320 +                               int *offset, int *size)
321 +{
322 +       int free1 = -1, free2 = -1, err, name_len;
323 +       struct ext3_xattr_entry entry;
324 +       
325 +       ea_idebug(inode, "name=%d.%s", name_index, name);
326 +
327 +       if (name == NULL)
328 +               return -EINVAL;
329 +       name_len = strlen(name);
330 +       if (name_len > 255)
331 +               return -ERANGE;
332 +
333 +       down(&ext3_xattr_sem);
334 +
335 +       /* try to find attribute in inode body */
336 +       err = ext3_xattr_ibody_find(inode, name_index, name,
337 +                                       &entry, &free1, bh, offset);
338 +       if (err == -ENOENT) {
339 +               /* there is no such attribute in inode body */
340 +               /* try to find attribute in dedicated block */
341 +               err = ext3_xattr_block_find(inode, name_index, name,
342 +                                               &entry, &free2, bh, offset);
343 +       }
344 +       if (err == 0 && size)
345 +               *size = le32_to_cpu(entry.e_value_size);
346 +       up(&ext3_xattr_sem);
347 +       return err;
348 +}
349 +
350  /*
351   * ext3_xattr_block_set()
352   *
353 Index: linux-2.4.24/include/linux/ext3_xattr.h
354 ===================================================================
355 --- linux-2.4.24.orig/include/linux/ext3_xattr.h        2004-08-06 00:47:18.000000000 +0400
356 +++ linux-2.4.24/include/linux/ext3_xattr.h     2004-08-06 03:52:43.000000000 +0400
357 @@ -80,6 +80,7 @@
358  extern int ext3_xattr_get(struct inode *, int, const char *, void *, size_t);
359  extern int ext3_xattr_list(struct inode *, char *, size_t);
360  extern int ext3_xattr_set(handle_t *handle, struct inode *, int, const char *, const void *, size_t, int);
361 +extern int ext3_xattr_get_ea_loc(struct inode *, int, const char *, struct buffer_head **, int *, int *);
362  
363  extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
364  extern void ext3_xattr_put_super(struct super_block *);