Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-inode-version-2.6.18-vanilla.patch
1 Index: linux-2.6.18/fs/ext3/inode.c
2 ===================================================================
3 --- linux-2.6.18.orig/fs/ext3/inode.c
4 +++ linux-2.6.18/fs/ext3/inode.c
5 @@ -2703,6 +2703,13 @@ void ext3_read_inode(struct inode * inod
6         EXT3_INODE_GET_XTIME(i_atime, inode, raw_inode);
7         EXT3_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
8  
9 +       ei->i_fs_version = le32_to_cpu(raw_inode->i_disk_version);
10 +       if (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
11 +               if (EXT3_FITS_IN_INODE(raw_inode, ei, i_version_hi))
12 +                       ei->i_fs_version |= (__u64)(le32_to_cpu(raw_inode->i_version_hi))
13 +                                                                        << 32;
14 +       }
15 +
16         if (S_ISREG(inode->i_mode)) {
17                 inode->i_op = &ext3_file_inode_operations;
18                 inode->i_fop = &ext3_file_operations;
19 @@ -2841,8 +2848,14 @@ static int ext3_do_update_inode(handle_t
20         } else for (block = 0; block < EXT3_N_BLOCKS; block++)
21                 raw_inode->i_block[block] = ei->i_data[block];
22  
23 -       if (ei->i_extra_isize)
24 +       raw_inode->i_disk_version = cpu_to_le32(ei->i_fs_version);
25 +       if (ei->i_extra_isize) {
26 +               if (EXT3_FITS_IN_INODE(raw_inode, ei, i_version_hi)) {
27 +                       raw_inode->i_version_hi = cpu_to_le32(ei->i_fs_version
28 +                                                                       >> 32);
29 +               }
30                 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
31 +       }
32  
33         BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
34         rc = ext3_journal_dirty_metadata(handle, bh);
35 @@ -3116,10 +3129,32 @@ ext3_reserve_inode_write(handle_t *handl
36  int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
37  {
38         struct ext3_iloc iloc;
39 -       int err;
40 +       int err, ret;
41 +       static int expand_message;
42  
43         might_sleep();
44         err = ext3_reserve_inode_write(handle, inode, &iloc);
45 +       if (EXT3_I(inode)->i_extra_isize <
46 +           EXT3_SB(inode->i_sb)->s_want_extra_isize &&
47 +           !(EXT3_I(inode)->i_state & EXT3_STATE_NO_EXPAND)) {
48 +               /* We need extra buffer credits since we may write into EA block
49 +                * with this same handle */
50 +               if ((ext3_journal_extend(handle,
51 +                            EXT3_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
52 +                       ret = ext3_expand_extra_isize(inode,
53 +                                       EXT3_SB(inode->i_sb)->s_want_extra_isize,
54 +                                       iloc, handle);
55 +                       if (ret) {
56 +                               EXT3_I(inode)->i_state |= EXT3_STATE_NO_EXPAND;
57 +                               if (!expand_message) {
58 +                                       ext3_warning(inode->i_sb, __FUNCTION__,
59 +                                       "Unable to expand inode %lu. Delete some"
60 +                                       " EAs or run e2fsck.", inode->i_ino);
61 +                                       expand_message = 1;
62 +                               }
63 +                       }
64 +               }
65 +       }
66         if (!err)
67                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
68         return err;
69 Index: linux-2.6.18/include/linux/ext3_fs.h
70 ===================================================================
71 --- linux-2.6.18.orig/include/linux/ext3_fs.h
72 +++ linux-2.6.18/include/linux/ext3_fs.h
73 @@ -201,6 +201,7 @@ struct ext3_group_desc
74  #define EXT3_STATE_JDATA               0x00000001 /* journaled data exists */
75  #define EXT3_STATE_NEW                 0x00000002 /* inode is newly created */
76  #define EXT3_STATE_XATTR               0x00000004 /* has in-inode xattrs */
77 +#define EXT3_STATE_NO_EXPAND           0x00000008 /* No space for expansion */
78  
79  /* Used to pass group descriptor data when online resize is done */
80  struct ext3_new_group_input {
81 @@ -277,7 +278,7 @@ struct ext3_inode {
82         __le32  i_flags;        /* File flags */
83         union {
84                 struct {
85 -                       __u32  l_i_reserved1;
86 +                       __u32  l_i_version;
87                 } linux1;
88                 struct {
89                         __u32  h_i_translator;
90 @@ -322,6 +323,7 @@ struct ext3_inode {
91         __le32  i_atime_extra;  /* extra Access time      (nsec << 2 | epoch) */
92         __le32  i_crtime;       /* File Creation time */
93         __le32  i_crtime_extra; /* extra File Creation time (nsec << 2 | epoch) */
94 +       __le32  i_version_hi;   /* high 32 bits for 64-bit version */
95  };
96  
97  #define i_size_high    i_dir_acl
98 @@ -384,6 +386,8 @@ do {                                                                               \
99                                        raw_inode->xtime ## _extra);            \
100  } while (0)
101  
102 +#define i_disk_version osd1.linux1.l_i_version
103 +
104  #if defined(__KERNEL__) || defined(__linux__)
105  #define i_reserved1    osd1.linux1.l_i_reserved1
106  #define i_frag         osd2.linux2.l_i_frag
107 Index: linux-2.6.18/include/linux/ext3_fs_i.h
108 ===================================================================
109 --- linux-2.6.18.orig/include/linux/ext3_fs_i.h
110 +++ linux-2.6.18/include/linux/ext3_fs_i.h
111 @@ -21,6 +21,8 @@
112  #include <linux/seqlock.h>
113  #include <linux/mutex.h>
114  
115 +#define HAVE_DISK_INODE_VERSION
116 +
117  /* data type for block offset of block group */
118  typedef int ext3_grpblk_t;
119  
120 @@ -147,6 +149,8 @@ struct ext3_inode_info {
121         struct timespec i_crtime;
122  
123         void *i_filterdata;
124 +
125 +       __u64 i_fs_version;
126  };
127  
128  #endif /* _LINUX_EXT3_FS_I */
129 Index: linux-2.6.18/fs/ext3/xattr.c
130 ===================================================================
131 --- linux-2.6.18.orig/fs/ext3/xattr.c
132 +++ linux-2.6.18/fs/ext3/xattr.c
133 @@ -505,6 +505,20 @@ ext3_xattr_release_block(handle_t *handl
134         }
135  }
136  
137 +static inline size_t ext3_xattr_free_space(struct ext3_xattr_entry *last,
138 +                                   size_t *min_offs, void *base, int *total)
139 +{
140 +       for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
141 +               *total += EXT3_XATTR_LEN(last->e_name_len);
142 +               if (!last->e_value_block && last->e_value_size) {
143 +                       size_t offs = le16_to_cpu(last->e_value_offs);
144 +                       if (offs < *min_offs)
145 +                               *min_offs = offs;
146 +               }
147 +       }
148 +       return (*min_offs - ((void *)last - base) - sizeof(__u32));
149 +}
150 +
151  struct ext3_xattr_info {
152         int name_index;
153         const char *name;
154 @@ -1008,6 +1022,8 @@ ext3_xattr_set_handle(handle_t *handle, 
155         if (!error) {
156                 ext3_xattr_update_super_block(handle, inode->i_sb);
157                 inode->i_ctime = ext3_current_time(inode);
158 +               if (!value)
159 +                       EXT3_I(inode)->i_state &= ~EXT3_STATE_NO_EXPAND;
160                 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
161                 /*
162                  * The bh is consumed by ext3_mark_iloc_dirty, even with
163 @@ -1060,6 +1076,249 @@ retry:
164         return error;
165  }
166  
167 +static void ext3_xattr_shift_entries(struct ext3_xattr_entry *entry,
168 +                                    int value_offs_shift, void *to,
169 +                                    void *from, size_t n, int blocksize)
170 +{
171 +       struct ext3_xattr_entry *last = entry;
172 +       int new_offs;
173 +
174 +       /* Adjust the value offsets of the entries */
175 +       for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
176 +               if (!last->e_value_block && last->e_value_size) {
177 +                       new_offs = le16_to_cpu(last->e_value_offs) +
178 +                                                       value_offs_shift;
179 +                       BUG_ON(new_offs + le32_to_cpu(last->e_value_size) >
180 +                              blocksize);
181 +                       last->e_value_offs = cpu_to_le16(new_offs);
182 +               }
183 +       }
184 +       /* Shift the entries by n bytes */
185 +       memmove(to, from, n);
186 +}
187 +
188 +/* Expand an inode by new_extra_isize bytes.
189 + * Returns 0 on success or negative error number on failure.
190 + */
191 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
192 +                           struct ext3_iloc iloc, handle_t *handle)
193 +{
194 +       struct ext3_inode *raw_inode;
195 +       struct ext3_xattr_ibody_header *header;
196 +       struct ext3_xattr_entry *entry, *last, *first;
197 +       struct buffer_head *bh = NULL;
198 +       struct ext3_xattr_ibody_find *is = NULL;
199 +       struct ext3_xattr_block_find *bs = NULL;
200 +       char *buffer = NULL, *b_entry_name = NULL;
201 +       size_t min_offs, free;
202 +       int total_ino, total_blk;
203 +       void *base, *start, *end;
204 +       int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
205 +       int s_min_extra_isize = EXT3_SB(inode->i_sb)->s_es->s_min_extra_isize;
206 +
207 +       down_write(&EXT3_I(inode)->xattr_sem);
208 +
209 +retry:
210 +       if (EXT3_I(inode)->i_extra_isize >= new_extra_isize) {
211 +               up_write(&EXT3_I(inode)->xattr_sem);
212 +               return 0;
213 +       }
214 +
215 +       raw_inode = ext3_raw_inode(&iloc);
216 +
217 +       header = IHDR(inode, raw_inode);
218 +       entry = IFIRST(header);
219 +
220 +       /* No extended attributes present */
221 +       if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR) ||
222 +           header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC)) {
223 +               memset((void *)raw_inode + EXT3_GOOD_OLD_INODE_SIZE, 0,
224 +                      new_extra_isize);
225 +               EXT3_I(inode)->i_extra_isize = new_extra_isize;
226 +               goto cleanup;
227 +       }
228 +
229 +       /*
230 +        * Check if enough free space is available in the inode to shift the
231 +        * entries ahead by new_extra_isize.
232 +        */
233 +
234 +       base = start = entry;
235 +       end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
236 +       min_offs = end - base;
237 +       last = entry;
238 +       total_ino = sizeof(struct ext3_xattr_ibody_header);
239 +
240 +       free = ext3_xattr_free_space(last, &min_offs, base, &total_ino);
241 +       if (free >= new_extra_isize) {
242 +               entry = IFIRST(header);
243 +               ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
244 +                               new_extra_isize, (void *)raw_inode +
245 +                               EXT3_GOOD_OLD_INODE_SIZE + new_extra_isize,
246 +                               (void *)header, total_ino,
247 +                               inode->i_sb->s_blocksize);
248 +               EXT3_I(inode)->i_extra_isize = new_extra_isize;
249 +               error = 0;
250 +               goto cleanup;
251 +       }
252 +
253 +       /*
254 +        * Enough free space isn't available in the inode, check if
255 +        * EA block can hold new_extra_isize bytes.
256 +        */
257 +       if (EXT3_I(inode)->i_file_acl) {
258 +               bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
259 +               error = -EIO;
260 +               if (!bh)
261 +                       goto cleanup;
262 +               if (ext3_xattr_check_block(bh)) {
263 +                       ext3_error(inode->i_sb, __FUNCTION__,
264 +                               "inode %lu: bad block "E3FSBLK, inode->i_ino,
265 +                               EXT3_I(inode)->i_file_acl);
266 +                       error = -EIO;
267 +                       goto cleanup;
268 +               }
269 +               base = BHDR(bh);
270 +               first = BFIRST(bh);
271 +               end = bh->b_data + bh->b_size;
272 +               min_offs = end - base;
273 +               free = ext3_xattr_free_space(first, &min_offs, base,
274 +                                            &total_blk);
275 +               if (free < new_extra_isize) {
276 +                       if (!tried_min_extra_isize && s_min_extra_isize) {
277 +                               tried_min_extra_isize++;
278 +                               new_extra_isize = s_min_extra_isize;
279 +                               goto retry;
280 +                       }
281 +                       error = -1;
282 +                       goto cleanup;
283 +               }
284 +       } else {
285 +               free = inode->i_sb->s_blocksize;
286 +       }
287 +
288 +       while (new_extra_isize > 0) {
289 +               size_t offs, size, entry_size;
290 +               struct ext3_xattr_entry *small_entry = NULL;
291 +               struct ext3_xattr_info i = {
292 +                       .value = NULL,
293 +                       .value_len = 0,
294 +               };
295 +               unsigned int total_size, shift_bytes, temp = ~0U;
296 +
297 +               is = (struct ext3_xattr_ibody_find *) kmalloc(sizeof(struct
298 +                                        ext3_xattr_ibody_find), GFP_KERNEL);
299 +               bs = (struct ext3_xattr_block_find *) kmalloc(sizeof(struct
300 +                                        ext3_xattr_block_find), GFP_KERNEL);
301 +               memset((void *)is, 0, sizeof(struct ext3_xattr_ibody_find));
302 +               memset((void *)bs, 0, sizeof(struct ext3_xattr_block_find));
303 +
304 +               is->s.not_found = bs->s.not_found = -ENODATA;
305 +               is->iloc.bh = NULL;
306 +               bs->bh = NULL;
307 +
308 +               last = IFIRST(header);
309 +               /* Find the entry best suited to be pushed into EA block */
310 +               entry = NULL;
311 +               for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
312 +                       total_size = EXT3_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
313 +                                       EXT3_XATTR_LEN(last->e_name_len);
314 +                       if (total_size <= free && total_size < temp) {
315 +                               if (total_size < new_extra_isize) {
316 +                                       small_entry = last;
317 +                               } else {
318 +                                       entry = last;
319 +                                       temp = total_size;
320 +                               }
321 +                       }
322 +               }
323 +
324 +               if (entry == NULL) {
325 +                       if (small_entry) {
326 +                               entry = small_entry;
327 +                       } else {
328 +                               if (!tried_min_extra_isize &&
329 +                                   s_min_extra_isize) {
330 +                                       tried_min_extra_isize++;
331 +                                       new_extra_isize = s_min_extra_isize;
332 +                                       goto retry;
333 +                               }
334 +                               error = -1;
335 +                               goto cleanup;
336 +                       }
337 +               }
338 +               offs = le16_to_cpu(entry->e_value_offs);
339 +               size = le32_to_cpu(entry->e_value_size);
340 +               entry_size = EXT3_XATTR_LEN(entry->e_name_len);
341 +               i.name_index = entry->e_name_index,
342 +               buffer = kmalloc(EXT3_XATTR_SIZE(size), GFP_KERNEL);
343 +               b_entry_name = kmalloc(entry->e_name_len + 1, GFP_KERNEL);
344 +               /* Save the entry name and the entry value */
345 +               memcpy((void *)buffer, (void *)IFIRST(header) + offs,
346 +                      EXT3_XATTR_SIZE(size));
347 +               memcpy((void *)b_entry_name, (void *)entry->e_name,
348 +                      entry->e_name_len);
349 +               b_entry_name[entry->e_name_len] = '\0';
350 +               i.name = b_entry_name;
351 +
352 +               error = ext3_get_inode_loc(inode, &is->iloc);
353 +               if (error)
354 +                       goto cleanup;
355 +
356 +               error = ext3_xattr_ibody_find(inode, &i, is);
357 +               if (error)
358 +                       goto cleanup;
359 +
360 +               /* Remove the chosen entry from the inode */
361 +               error = ext3_xattr_ibody_set(handle, inode, &i, is);
362 +
363 +               entry = IFIRST(header);
364 +               if (entry_size + EXT3_XATTR_SIZE(size) >= new_extra_isize)
365 +                       shift_bytes = new_extra_isize;
366 +               else
367 +                       shift_bytes = entry_size + size;
368 +               /* Adjust the offsets and shift the remaining entries ahead */
369 +               ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
370 +                       shift_bytes, (void *)raw_inode +
371 +                       EXT3_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
372 +                       (void *)header, total_ino - entry_size,
373 +                       inode->i_sb->s_blocksize);
374 +
375 +               extra_isize += shift_bytes;
376 +               new_extra_isize -= shift_bytes;
377 +               EXT3_I(inode)->i_extra_isize = extra_isize;
378 +
379 +               i.name = b_entry_name;
380 +               i.value = buffer;
381 +               i.value_len = cpu_to_le32(size);
382 +               error = ext3_xattr_block_find(inode, &i, bs);
383 +               if (error)
384 +                       goto cleanup;
385 +
386 +               /* Add entry which was removed from the inode into the block */
387 +               error = ext3_xattr_block_set(handle, inode, &i, bs);
388 +               if (error)
389 +                       goto cleanup;
390 +       }
391 +
392 +cleanup:
393 +       if (b_entry_name)
394 +               kfree(b_entry_name);
395 +       if (buffer)
396 +               kfree(buffer);
397 +       if (is) {
398 +               brelse(is->iloc.bh);
399 +               kfree(is);
400 +       }
401 +       if (bs)
402 +               kfree(bs);
403 +       brelse(bh);
404 +       up_write(&EXT3_I(inode)->xattr_sem);
405 +       return error;
406 +}
407 +
408 +
409 +
410  /*
411   * ext3_xattr_delete_inode()
412   *
413 Index: linux-2.6.18/fs/ext3/xattr.h
414 ===================================================================
415 --- linux-2.6.18.orig/fs/ext3/xattr.h
416 +++ linux-2.6.18/fs/ext3/xattr.h
417 @@ -74,6 +74,9 @@ extern int ext3_xattr_set_handle(handle_
418  extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
419  extern void ext3_xattr_put_super(struct super_block *);
420  
421 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
422 +                           struct ext3_iloc iloc, handle_t *handle);
423 +
424  extern int init_ext3_xattr(void);
425  extern void exit_ext3_xattr(void);
426