Whamcloud - gitweb
- need couple routines to be accessible from modules for size-on-mds feature
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-in-ea-2.6.7.patch
1 Index: linux-2.6.7/fs/ext3/extents-in-ea.c
2 ===================================================================
3 --- linux-2.6.7.orig/fs/ext3/extents-in-ea.c    2003-01-30 18:24:37.000000000 +0800
4 +++ linux-2.6.7/fs/ext3/extents-in-ea.c 2004-08-12 14:31:13.000000000 +0800
5 @@ -0,0 +1,224 @@
6 +/*
7 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
8 + * Written by Alex Tomas <alex@clusterfs.com>
9 + *
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
13 + *
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 + * GNU General Public License for more details.
18 + *
19 + * You should have received a copy of the GNU General Public Licens
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
22 + */
23 +
24 +#include <linux/module.h>
25 +#include <linux/fs.h>
26 +#include <linux/time.h>
27 +#include <linux/ext3_jbd.h>
28 +#include <linux/jbd.h>
29 +#include <linux/smp_lock.h>
30 +#include <linux/highuid.h>
31 +#include <linux/pagemap.h>
32 +#include <linux/quotaops.h>
33 +#include <linux/string.h>
34 +#include <linux/ext3_extents.h>
35 +#include <linux/ext3_xattr.h>
36 +#include <linux/slab.h>
37 +#include <asm/uaccess.h>
38 +
39 +static int ext3_get_ea_write_access(handle_t *handle, void *buffer)
40 +{
41 +       struct buffer_head *bh = (struct buffer_head *) buffer;
42 +       return ext3_journal_get_write_access(handle, bh);
43 +}
44 +
45 +static int ext3_mark_ea_buffer_dirty(handle_t *handle, void *buffer)
46 +{
47 +       struct buffer_head *bh = (struct buffer_head *) buffer;
48 +       ext3_journal_dirty_metadata(handle, bh);
49 +       return 0;
50 +}
51 +
52 +static struct ext3_extents_helpers ext3_ea_helpers = {
53 +       .get_write_access       = ext3_get_ea_write_access,
54 +       .mark_buffer_dirty      = ext3_mark_ea_buffer_dirty,
55 +       .mergable               = NULL,
56 +       .new_block              = NULL,
57 +       .remove_extent          = NULL,
58 +       .remove_extent_credits  = NULL,
59 +};
60 +
61 +int ext3_init_tree_in_ea_desc(struct ext3_extents_tree *tree,
62 +                               struct inode *inode, int name_index,
63 +                               const char *eaname)
64 +{
65 +       struct buffer_head *bh;
66 +       int offset, err, size;
67 +
68 +       err = ext3_xattr_get_ea_loc(inode, name_index, eaname,
69 +                                       &bh, &offset, &size);
70 +       if (err)
71 +               return err;
72 +
73 +       EXT_ASSERT(bh);
74 +       EXT_ASSERT(size >= sizeof(struct ext3_extent_header)
75 +                               + sizeof(struct ext3_extent));
76 +       tree->inode = inode;
77 +       tree->root = (void *) bh->b_data + offset;
78 +       tree->buffer_len = size;
79 +       tree->buffer = (void *) bh;
80 +       tree->ops = &ext3_ea_helpers;
81 +       tree->cex = NULL;       /* FIXME: add cache store later */
82 +       return 0;
83 +}
84 +
85 +void ext3_release_tree_in_ea_desc(struct ext3_extents_tree *tree)
86 +{
87 +       struct buffer_head *bh;
88 +
89 +       bh = (struct buffer_head *) tree->buffer;
90 +       EXT_ASSERT(bh);
91 +       brelse(bh);
92 +}
93 +
94 +int ext3_init_tree_in_ea(struct inode *inode, int name_index,
95 +                               const char *eaname, int size)
96 +{
97 +       struct ext3_extents_tree tree;
98 +       handle_t *handle;
99 +       char *root;
100 +       int err;
101 +
102 +       root = kmalloc(size, GFP_USER);
103 +       if (!root)
104 +               return -ENOMEM;
105 +       memset(root, 0, size);
106 +
107 +       /* first, create ea to store root of the tree */
108 +       handle = ext3_journal_start(inode, EXT3_ALLOC_NEEDED + 3);
109 +       if (IS_ERR(handle))
110 +               return PTR_ERR(handle);
111 +       if ((err = ext3_xattr_set(inode, name_index,
112 +                                 eaname, root, size, 0)))
113 +               goto out;
114 +       if ((err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname)))
115 +               goto out;
116 +       err = ext3_extent_tree_init(handle, &tree);
117 +       ext3_release_tree_in_ea_desc(&tree);
118 +out:
119 +       ext3_journal_stop(handle, inode);
120 +       kfree(root);
121 +       return err;
122 +}
123 +
124 +static int
125 +ext3_ext_in_ea_new_extent(struct ext3_extents_tree *tree,
126 +                       struct ext3_ext_path *path,
127 +                       struct ext3_extent *newex, int exist)
128 +{
129 +       struct inode *inode = tree->inode;
130 +       handle_t *handle;
131 +       int needed, err;
132 +       unsigned long tgen;
133 +
134 +       if (exist)
135 +               return EXT_CONTINUE;
136 +
137 +       tgen = EXT_GENERATION(tree);
138 +       needed = ext3_ext_calc_credits_for_insert(tree, path);
139 +       up(&EXT3_I(inode)->truncate_sem);
140 +       handle = ext3_journal_start(tree->inode, needed + 10);
141 +       if (IS_ERR(handle)) {
142 +               down_write(&EXT3_I(inode)->truncate_sem);
143 +               return PTR_ERR(handle);
144 +       }
145 +
146 +       if (tgen != EXT_GENERATION(tree)) {
147 +               /* the tree has changed. so path can be invalid at moment */
148 +               ext3_journal_stop(handle, inode);
149 +               down_write(&EXT3_I(inode)->truncate_sem);
150 +               return EXT_REPEAT;
151 +       }
152 +
153 +       down_write(&EXT3_I(inode)->truncate_sem);
154 +
155 +       /* insert new extent */
156 +       newex->ee_start = 0;
157 +       err = ext3_ext_insert_extent(handle, tree, path, newex);
158 +       if (!err)
159 +               ext3_journal_stop(handle, tree->inode);
160 +
161 +       return err;
162 +}
163 +
164 +int ext3_ext_in_ea_alloc_space(struct inode *inode, int name_index,
165 +                               const char *eaname, unsigned long from,
166 +                               unsigned long num)
167 +{
168 +       struct ext3_extents_tree tree;
169 +       int err;
170 +
171 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
172 +       if (err == 0) {
173 +               down_write(&EXT3_I(inode)->truncate_sem);       
174 +               err = ext3_ext_walk_space(&tree, from, num,
175 +                                               ext3_ext_in_ea_new_extent);
176 +               ext3_release_tree_in_ea_desc(&tree);
177 +               up_write(&EXT3_I(inode)->truncate_sem);
178 +       }
179 +       return err;
180 +}
181 +
182 +int ext3_ext_in_ea_remove_space(struct inode *inode, int name_index,
183 +                               const char *eaname, unsigned long from,
184 +                               unsigned long num)
185 +{
186 +       struct ext3_extents_tree tree;
187 +       int err;
188 +
189 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
190 +       if (err == 0) {
191 +               err = ext3_ext_remove_space(&tree, from, num);
192 +               ext3_release_tree_in_ea_desc(&tree);
193 +       }
194 +       return err;
195 +}
196 +
197 +int ext3_ext_in_ea_presence(struct inode *inode, int name_index,
198 +                               const char *eaname, unsigned long block)
199 +{
200 +       struct ext3_extents_tree tree;
201 +       struct ext3_ext_path *path;
202 +       struct ext3_extent *ex;
203 +       int err, depth;
204 +
205 +       err = ext3_init_tree_in_ea_desc(&tree, inode, name_index, eaname);
206 +       if (err)
207 +               return err;
208 +
209 +       /* find extent for this block */
210 +       path = ext3_ext_find_extent(&tree, block, NULL);
211 +       if (IS_ERR(path)) {
212 +               err = PTR_ERR(path);
213 +               goto out;
214 +       }
215 +
216 +       depth = EXT_DEPTH(&tree);
217 +       ex = path[depth].p_ext;
218 +       if (!ex) {
219 +               /* there is no extent yet */
220 +               goto out;
221 +       }
222 +
223 +       if (block >= ex->ee_block && block < ex->ee_block + ex->ee_len)
224 +               err = 1;
225 +out:
226 +       ext3_release_tree_in_ea_desc(&tree);
227 +       return err;
228 +}
229 +
230 Index: linux-2.6.7/fs/ext3/Makefile
231 ===================================================================
232 --- linux-2.6.7.orig/fs/ext3/Makefile   2004-08-12 13:42:21.000000000 +0800
233 +++ linux-2.6.7/fs/ext3/Makefile        2004-08-12 13:42:23.000000000 +0800
234 @@ -7,6 +7,6 @@
235  ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
236            ioctl.o namei.o super.o symlink.o hash.o extents.o
237  
238 -ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
239 +ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o extents-in-ea.o
240  ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
241  ext3-$(CONFIG_EXT3_FS_SECURITY)         += xattr_security.o
242 Index: linux-2.6.7/fs/ext3/xattr.c
243 ===================================================================
244 --- linux-2.6.7.orig/fs/ext3/xattr.c    2004-08-12 13:04:19.000000000 +0800
245 +++ linux-2.6.7/fs/ext3/xattr.c 2004-08-12 13:43:10.000000000 +0800
246 @@ -680,7 +680,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 @@ -727,6 +728,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 @@ -747,7 +757,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 @@ -790,6 +801,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 @@ -984,7 +1001,8 @@
296         down_write(&EXT3_I(inode)->xattr_sem);
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 @@ -993,7 +1011,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 @@ -1050,6 +1068,35 @@
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 +       /* try to find attribute in inode body */
334 +       err = ext3_xattr_ibody_find(inode, name_index, name,
335 +                                       &entry, &free1, bh, offset);
336 +       if (err == -ENOENT) {
337 +               /* there is no such attribute in inode body */
338 +               /* try to find attribute in dedicated block */
339 +               err = ext3_xattr_block_find(inode, name_index, name,
340 +                                               &entry, &free2, bh, offset);
341 +       }
342 +       if (err == 0 && size)
343 +               *size = le32_to_cpu(entry.e_value_size);
344 +       return err;
345 +}
346 +
347  /*
348   * ext3_xattr_block_set()
349   *
350 Index: linux-2.6.7/fs/ext3/xattr.h
351 ===================================================================
352 --- linux-2.6.7.orig/fs/ext3/xattr.h    2004-08-12 13:04:19.000000000 +0800
353 +++ linux-2.6.7/fs/ext3/xattr.h 2004-08-12 13:42:23.000000000 +0800
354 @@ -80,6 +80,7 @@
355  extern int ext3_xattr_set_handle(handle_t *, struct inode *, int, const char *,const void *,size_t,int);
356  extern int ext3_xattr_block_set(handle_t *, struct inode *, int, const char *,const void *,size_t,int);
357  
358 +extern int ext3_xattr_get_ea_loc(struct inode *, int, const char *, struct buffer_head **, int *, int *);
359  extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
360  extern void ext3_xattr_put_super(struct super_block *);
361