Whamcloud - gitweb
Branch b1_8
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-inode-version-2.6-sles10.patch
1 Index: linux-2.6.16-sles10/fs/ext3/inode.c
2 ===================================================================
3 --- linux-2.6.16-sles10.orig/fs/ext3/inode.c
4 +++ linux-2.6.16-sles10/fs/ext3/inode.c
5 @@ -2561,6 +2561,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 @@ -2699,8 +2706,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 @@ -2974,10 +2987,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.16-sles10/include/linux/ext3_fs.h
70 ===================================================================
71 --- linux-2.6.16-sles10.orig/include/linux/ext3_fs.h
72 +++ linux-2.6.16-sles10/include/linux/ext3_fs.h
73 @@ -227,6 +227,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 @@ -303,7 +304,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 @@ -348,6 +349,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 @@ -410,6 +412,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.16-sles10/include/linux/ext3_fs_i.h
108 ===================================================================
109 --- linux-2.6.16-sles10.orig/include/linux/ext3_fs_i.h
110 +++ linux-2.6.16-sles10/include/linux/ext3_fs_i.h
111 @@ -20,6 +20,8 @@
112  #include <linux/rbtree.h>
113  #include <linux/seqlock.h>
114  
115 +#define HAVE_DISK_INODE_VERSION
116 +
117  struct ext3_reserve_window {
118         __u32                   _rsv_start;     /* First byte reserved */
119         __u32                   _rsv_end;       /* Last byte reserved or 0 */
120 @@ -142,6 +144,8 @@ struct ext3_inode_info {
121         spinlock_t i_prealloc_lock;
122  
123         void *i_filterdata;
124 +
125 +       __u64 i_fs_version;
126  };
127  
128  #endif /* _LINUX_EXT3_FS_I */
129 Index: linux-2.6.16-sles10/fs/ext3/xattr.c
130 ===================================================================
131 --- linux-2.6.16-sles10.orig/fs/ext3/xattr.c
132 +++ linux-2.6.16-sles10/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 @@ -944,13 +958,18 @@ ext3_xattr_set_handle(handle_t *handle, 
155         struct ext3_xattr_block_find bs = {
156                 .s = { .not_found = -ENODATA, },
157         };
158 +       unsigned long no_expand;
159         int error;
160  
161         if (!name)
162                 return -EINVAL;
163         if (strlen(name) > 255)
164                 return -ERANGE;
165 +
166         down_write(&EXT3_I(inode)->xattr_sem);
167 +       no_expand = EXT3_I(inode)->i_state & EXT3_STATE_NO_EXPAND;
168 +       EXT3_I(inode)->i_state |= EXT3_STATE_NO_EXPAND;
169 +
170         error = ext3_get_inode_loc(inode, &is.iloc);
171         if (error)
172                 goto cleanup;
173 @@ -1007,7 +1026,10 @@ ext3_xattr_set_handle(handle_t *handle, 
174         if (!error) {
175                 ext3_xattr_update_super_block(handle, inode->i_sb);
176                 inode->i_ctime = ext3_current_time(inode);
177 +
178                 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
179 +               if (!value)
180 +                       EXT3_I(inode)->i_state &= ~EXT3_STATE_NO_EXPAND;
181                 /*
182                  * The bh is consumed by ext3_mark_iloc_dirty, even with
183                  * error != 0.
184 @@ -1020,6 +1042,8 @@ ext3_xattr_set_handle(handle_t *handle, 
185  cleanup:
186         brelse(is.iloc.bh);
187         brelse(bs.bh);
188 +       if (no_expand == 0)
189 +               EXT3_I(inode)->i_state &= ~EXT3_STATE_NO_EXPAND;
190         up_write(&EXT3_I(inode)->xattr_sem);
191         return error;
192  }
193 @@ -1059,6 +1083,249 @@ retry:
194         return error;
195  }
196  
197 +static void ext3_xattr_shift_entries(struct ext3_xattr_entry *entry,
198 +                                    int value_offs_shift, void *to,
199 +                                    void *from, size_t n, int blocksize)
200 +{
201 +       struct ext3_xattr_entry *last = entry;
202 +       int new_offs;
203 +
204 +       /* Adjust the value offsets of the entries */
205 +       for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
206 +               if (!last->e_value_block && last->e_value_size) {
207 +                       new_offs = le16_to_cpu(last->e_value_offs) +
208 +                                                       value_offs_shift;
209 +                       BUG_ON(new_offs + le32_to_cpu(last->e_value_size) >
210 +                              blocksize);
211 +                       last->e_value_offs = cpu_to_le16(new_offs);
212 +               }
213 +       }
214 +       /* Shift the entries by n bytes */
215 +       memmove(to, from, n);
216 +}
217 +
218 +/* Expand an inode by new_extra_isize bytes.
219 + * Returns 0 on success or negative error number on failure.
220 + */
221 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
222 +                           struct ext3_iloc iloc, handle_t *handle)
223 +{
224 +       struct ext3_inode *raw_inode;
225 +       struct ext3_xattr_ibody_header *header;
226 +       struct ext3_xattr_entry *entry, *last, *first;
227 +       struct buffer_head *bh = NULL;
228 +       struct ext3_xattr_ibody_find *is = NULL;
229 +       struct ext3_xattr_block_find *bs = NULL;
230 +       char *buffer = NULL, *b_entry_name = NULL;
231 +       size_t min_offs, free;
232 +       int total_ino, total_blk;
233 +       void *base, *start, *end;
234 +       int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
235 +       int s_min_extra_isize = EXT3_SB(inode->i_sb)->s_es->s_min_extra_isize;
236 +
237 +       down_write(&EXT3_I(inode)->xattr_sem);
238 +
239 +retry:
240 +       if (EXT3_I(inode)->i_extra_isize >= new_extra_isize) {
241 +               up_write(&EXT3_I(inode)->xattr_sem);
242 +               return 0;
243 +       }
244 +
245 +       raw_inode = ext3_raw_inode(&iloc);
246 +
247 +       header = IHDR(inode, raw_inode);
248 +       entry = IFIRST(header);
249 +
250 +       /* No extended attributes present */
251 +       if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR) ||
252 +           header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC)) {
253 +               memset((void *)raw_inode + EXT3_GOOD_OLD_INODE_SIZE, 0,
254 +                      new_extra_isize);
255 +               EXT3_I(inode)->i_extra_isize = new_extra_isize;
256 +               goto cleanup;
257 +       }
258 +
259 +       /*
260 +        * Check if enough free space is available in the inode to shift the
261 +        * entries ahead by new_extra_isize.
262 +        */
263 +
264 +       base = start = entry;
265 +       end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
266 +       min_offs = end - base;
267 +       last = entry;
268 +       total_ino = sizeof(struct ext3_xattr_ibody_header);
269 +
270 +       free = ext3_xattr_free_space(last, &min_offs, base, &total_ino);
271 +       if (free >= new_extra_isize) {
272 +               entry = IFIRST(header);
273 +               ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
274 +                               new_extra_isize, (void *)raw_inode +
275 +                               EXT3_GOOD_OLD_INODE_SIZE + new_extra_isize,
276 +                               (void *)header, total_ino,
277 +                               inode->i_sb->s_blocksize);
278 +               EXT3_I(inode)->i_extra_isize = new_extra_isize;
279 +               error = 0;
280 +               goto cleanup;
281 +       }
282 +
283 +       /*
284 +        * Enough free space isn't available in the inode, check if
285 +        * EA block can hold new_extra_isize bytes.
286 +        */
287 +       if (EXT3_I(inode)->i_file_acl) {
288 +               bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
289 +               error = -EIO;
290 +               if (!bh)
291 +                       goto cleanup;
292 +               if (ext3_xattr_check_block(bh)) {
293 +                       ext3_error(inode->i_sb, __FUNCTION__,
294 +                               "inode %lu: bad block %d", inode->i_ino,
295 +                               EXT3_I(inode)->i_file_acl);
296 +                       error = -EIO;
297 +                       goto cleanup;
298 +               }
299 +               base = BHDR(bh);
300 +               first = BFIRST(bh);
301 +               end = bh->b_data + bh->b_size;
302 +               min_offs = end - base;
303 +               free = ext3_xattr_free_space(first, &min_offs, base,
304 +                                            &total_blk);
305 +               if (free < new_extra_isize) {
306 +                       if (!tried_min_extra_isize && s_min_extra_isize) {
307 +                               tried_min_extra_isize++;
308 +                               new_extra_isize = s_min_extra_isize;
309 +                               goto retry;
310 +                       }
311 +                       error = -1;
312 +                       goto cleanup;
313 +               }
314 +       } else {
315 +               free = inode->i_sb->s_blocksize;
316 +       }
317 +
318 +       while (new_extra_isize > 0) {
319 +               size_t offs, size, entry_size;
320 +               struct ext3_xattr_entry *small_entry = NULL;
321 +               struct ext3_xattr_info i = {
322 +                       .value = NULL,
323 +                       .value_len = 0,
324 +               };
325 +               unsigned int total_size, shift_bytes, temp = ~0U;
326 +
327 +               is = (struct ext3_xattr_ibody_find *) kmalloc(sizeof(struct
328 +                                        ext3_xattr_ibody_find), GFP_KERNEL);
329 +               bs = (struct ext3_xattr_block_find *) kmalloc(sizeof(struct
330 +                                        ext3_xattr_block_find), GFP_KERNEL);
331 +               memset((void *)is, 0, sizeof(struct ext3_xattr_ibody_find));
332 +               memset((void *)bs, 0, sizeof(struct ext3_xattr_block_find));
333 +
334 +               is->s.not_found = bs->s.not_found = -ENODATA;
335 +               is->iloc.bh = NULL;
336 +               bs->bh = NULL;
337 +
338 +               last = IFIRST(header);
339 +               /* Find the entry best suited to be pushed into EA block */
340 +               entry = NULL;
341 +               for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
342 +                       total_size = EXT3_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
343 +                                       EXT3_XATTR_LEN(last->e_name_len);
344 +                       if (total_size <= free && total_size < temp) {
345 +                               if (total_size < new_extra_isize) {
346 +                                       small_entry = last;
347 +                               } else {
348 +                                       entry = last;
349 +                                       temp = total_size;
350 +                               }
351 +                       }
352 +               }
353 +
354 +               if (entry == NULL) {
355 +                       if (small_entry) {
356 +                               entry = small_entry;
357 +                       } else {
358 +                               if (!tried_min_extra_isize &&
359 +                                   s_min_extra_isize) {
360 +                                       tried_min_extra_isize++;
361 +                                       new_extra_isize = s_min_extra_isize;
362 +                                       goto retry;
363 +                               }
364 +                               error = -1;
365 +                               goto cleanup;
366 +                       }
367 +               }
368 +               offs = le16_to_cpu(entry->e_value_offs);
369 +               size = le32_to_cpu(entry->e_value_size);
370 +               entry_size = EXT3_XATTR_LEN(entry->e_name_len);
371 +               i.name_index = entry->e_name_index,
372 +               buffer = kmalloc(EXT3_XATTR_SIZE(size), GFP_KERNEL);
373 +               b_entry_name = kmalloc(entry->e_name_len + 1, GFP_KERNEL);
374 +               /* Save the entry name and the entry value */
375 +               memcpy((void *)buffer, (void *)IFIRST(header) + offs,
376 +                      EXT3_XATTR_SIZE(size));
377 +               memcpy((void *)b_entry_name, (void *)entry->e_name,
378 +                      entry->e_name_len);
379 +               b_entry_name[entry->e_name_len] = '\0';
380 +               i.name = b_entry_name;
381 +
382 +               error = ext3_get_inode_loc(inode, &is->iloc);
383 +               if (error)
384 +                       goto cleanup;
385 +
386 +               error = ext3_xattr_ibody_find(inode, &i, is);
387 +               if (error)
388 +                       goto cleanup;
389 +
390 +               /* Remove the chosen entry from the inode */
391 +               error = ext3_xattr_ibody_set(handle, inode, &i, is);
392 +
393 +               entry = IFIRST(header);
394 +               if (entry_size + EXT3_XATTR_SIZE(size) >= new_extra_isize)
395 +                       shift_bytes = new_extra_isize;
396 +               else
397 +                       shift_bytes = entry_size + size;
398 +               /* Adjust the offsets and shift the remaining entries ahead */
399 +               ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
400 +                       shift_bytes, (void *)raw_inode +
401 +                       EXT3_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
402 +                       (void *)header, total_ino - entry_size,
403 +                       inode->i_sb->s_blocksize);
404 +
405 +               extra_isize += shift_bytes;
406 +               new_extra_isize -= shift_bytes;
407 +               EXT3_I(inode)->i_extra_isize = extra_isize;
408 +
409 +               i.name = b_entry_name;
410 +               i.value = buffer;
411 +               i.value_len = cpu_to_le32(size);
412 +               error = ext3_xattr_block_find(inode, &i, bs);
413 +               if (error)
414 +                       goto cleanup;
415 +
416 +               /* Add entry which was removed from the inode into the block */
417 +               error = ext3_xattr_block_set(handle, inode, &i, bs);
418 +               if (error)
419 +                       goto cleanup;
420 +       }
421 +
422 +cleanup:
423 +       if (b_entry_name)
424 +               kfree(b_entry_name);
425 +       if (buffer)
426 +               kfree(buffer);
427 +       if (is) {
428 +               brelse(is->iloc.bh);
429 +               kfree(is);
430 +       }
431 +       if (bs)
432 +               kfree(bs);
433 +       brelse(bh);
434 +       up_write(&EXT3_I(inode)->xattr_sem);
435 +       return error;
436 +}
437 +
438 +
439 +
440  /*
441   * ext3_xattr_delete_inode()
442   *
443 Index: linux-2.6.16-sles10/fs/ext3/xattr.h
444 ===================================================================
445 --- linux-2.6.16-sles10.orig/fs/ext3/xattr.h
446 +++ linux-2.6.16-sles10/fs/ext3/xattr.h
447 @@ -75,6 +75,9 @@ extern int ext3_xattr_set_handle(handle_
448  extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
449  extern void ext3_xattr_put_super(struct super_block *);
450  
451 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
452 +                           struct ext3_iloc iloc, handle_t *handle);
453 +
454  extern int init_ext3_xattr(void);
455  extern void exit_ext3_xattr(void);
456  
457 Index: linux-2.6.16-sles10/fs/ext3/ialloc.c
458 ===================================================================
459 --- linux-2.6.16-sles10.orig/fs/ext3/ialloc.c
460 +++ linux-2.6.16-sles10/fs/ext3/ialloc.c
461 @@ -750,6 +750,7 @@ got:
462         ei->i_dtime = 0;
463         ei->i_block_alloc_info = NULL;
464         ei->i_block_group = group;
465 +       ei->i_fs_version = 0;
466  
467         ext3_set_inode_flags(inode);
468         if (IS_DIRSYNC(inode))