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 @@ -2690,6 +2690,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);
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))
16 if (S_ISREG(inode->i_mode)) {
17 inode->i_op = &ext3_file_inode_operations;
18 inode->i_fop = &ext3_file_operations;
19 @@ -2828,8 +2835,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];
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
30 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
33 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
34 rc = ext3_journal_dirty_metadata(handle, bh);
35 @@ -3103,10 +3116,32 @@ ext3_reserve_inode_write(handle_t *handl
36 int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
38 struct ext3_iloc iloc;
41 + static int expand_message;
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,
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);
67 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
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 @@ -224,6 +224,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 */
79 /* Used to pass group descriptor data when online resize is done */
80 struct ext3_new_group_input {
81 @@ -297,7 +298,7 @@ struct ext3_inode {
82 __le32 i_flags; /* File flags */
85 - __u32 l_i_reserved1;
90 @@ -342,6 +343,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 */
97 #define i_size_high i_dir_acl
98 @@ -404,6 +406,8 @@ do { \
99 raw_inode->xtime ## _extra); \
102 +#define i_disk_version osd1.linux1.l_i_version
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
112 #include <linux/seqlock.h>
113 #include <linux/mutex.h>
115 +#define HAVE_DISK_INODE_VERSION
117 /* data type for block offset of block group */
118 typedef int ext3_grpblk_t;
120 @@ -162,6 +164,8 @@ struct ext3_inode_info {
122 struct list_head i_prealloc_list;
123 spinlock_t i_prealloc_lock;
125 + __u64 i_fs_version;
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
137 +static inline size_t ext3_xattr_free_space(struct ext3_xattr_entry *last,
138 + size_t *min_offs, void *base, int *total)
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)
148 + return (*min_offs - ((void *)last - base) - sizeof(__u32));
151 struct ext3_xattr_info {
154 @@ -945,13 +959,18 @@ ext3_xattr_set_handle(handle_t *handle,
155 struct ext3_xattr_block_find bs = {
156 .s = { .not_found = -ENODATA, },
158 + unsigned long no_expand;
163 if (strlen(name) > 255)
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;
170 error = ext3_get_inode_loc(inode, &is.iloc);
173 @@ -1009,6 +1028,8 @@ ext3_xattr_set_handle(handle_t *handle,
174 ext3_xattr_update_super_block(handle, inode->i_sb);
175 inode->i_ctime = ext3_current_time(inode);
176 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
178 + EXT3_I(inode)->i_state &= ~EXT3_STATE_NO_EXPAND;
180 * The bh is consumed by ext3_mark_iloc_dirty, even with
182 @@ -1021,6 +1042,8 @@ ext3_xattr_set_handle(handle_t *handle,
186 + if (no_expand == 0)
187 + EXT3_I(inode)->i_state &= ~EXT3_STATE_NO_EXPAND;
188 up_write(&EXT3_I(inode)->xattr_sem);
191 @@ -1060,6 +1083,249 @@ retry:
195 +static void ext3_xattr_shift_entries(struct ext3_xattr_entry *entry,
196 + int value_offs_shift, void *to,
197 + void *from, size_t n, int blocksize)
199 + struct ext3_xattr_entry *last = entry;
202 + /* Adjust the value offsets of the entries */
203 + for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
204 + if (!last->e_value_block && last->e_value_size) {
205 + new_offs = le16_to_cpu(last->e_value_offs) +
207 + BUG_ON(new_offs + le32_to_cpu(last->e_value_size) >
209 + last->e_value_offs = cpu_to_le16(new_offs);
212 + /* Shift the entries by n bytes */
213 + memmove(to, from, n);
216 +/* Expand an inode by new_extra_isize bytes.
217 + * Returns 0 on success or negative error number on failure.
219 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
220 + struct ext3_iloc iloc, handle_t *handle)
222 + struct ext3_inode *raw_inode;
223 + struct ext3_xattr_ibody_header *header;
224 + struct ext3_xattr_entry *entry, *last, *first;
225 + struct buffer_head *bh = NULL;
226 + struct ext3_xattr_ibody_find *is = NULL;
227 + struct ext3_xattr_block_find *bs = NULL;
228 + char *buffer = NULL, *b_entry_name = NULL;
229 + size_t min_offs, free;
230 + int total_ino, total_blk;
231 + void *base, *start, *end;
232 + int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
233 + int s_min_extra_isize = EXT3_SB(inode->i_sb)->s_es->s_min_extra_isize;
235 + down_write(&EXT3_I(inode)->xattr_sem);
238 + if (EXT3_I(inode)->i_extra_isize >= new_extra_isize) {
239 + up_write(&EXT3_I(inode)->xattr_sem);
243 + raw_inode = ext3_raw_inode(&iloc);
245 + header = IHDR(inode, raw_inode);
246 + entry = IFIRST(header);
248 + /* No extended attributes present */
249 + if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR) ||
250 + header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC)) {
251 + memset((void *)raw_inode + EXT3_GOOD_OLD_INODE_SIZE, 0,
253 + EXT3_I(inode)->i_extra_isize = new_extra_isize;
258 + * Check if enough free space is available in the inode to shift the
259 + * entries ahead by new_extra_isize.
262 + base = start = entry;
263 + end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
264 + min_offs = end - base;
266 + total_ino = sizeof(struct ext3_xattr_ibody_header);
268 + free = ext3_xattr_free_space(last, &min_offs, base, &total_ino);
269 + if (free >= new_extra_isize) {
270 + entry = IFIRST(header);
271 + ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
272 + new_extra_isize, (void *)raw_inode +
273 + EXT3_GOOD_OLD_INODE_SIZE + new_extra_isize,
274 + (void *)header, total_ino,
275 + inode->i_sb->s_blocksize);
276 + EXT3_I(inode)->i_extra_isize = new_extra_isize;
282 + * Enough free space isn't available in the inode, check if
283 + * EA block can hold new_extra_isize bytes.
285 + if (EXT3_I(inode)->i_file_acl) {
286 + bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
290 + if (ext3_xattr_check_block(bh)) {
291 + ext3_error(inode->i_sb, __FUNCTION__,
292 + "inode %lu: bad block "E3FSBLK, inode->i_ino,
293 + EXT3_I(inode)->i_file_acl);
298 + first = BFIRST(bh);
299 + end = bh->b_data + bh->b_size;
300 + min_offs = end - base;
301 + free = ext3_xattr_free_space(first, &min_offs, base,
303 + if (free < new_extra_isize) {
304 + if (!tried_min_extra_isize && s_min_extra_isize) {
305 + tried_min_extra_isize++;
306 + new_extra_isize = s_min_extra_isize;
313 + free = inode->i_sb->s_blocksize;
316 + while (new_extra_isize > 0) {
317 + size_t offs, size, entry_size;
318 + struct ext3_xattr_entry *small_entry = NULL;
319 + struct ext3_xattr_info i = {
323 + unsigned int total_size, shift_bytes, temp = ~0U;
325 + is = (struct ext3_xattr_ibody_find *) kmalloc(sizeof(struct
326 + ext3_xattr_ibody_find), GFP_KERNEL);
327 + bs = (struct ext3_xattr_block_find *) kmalloc(sizeof(struct
328 + ext3_xattr_block_find), GFP_KERNEL);
329 + memset((void *)is, 0, sizeof(struct ext3_xattr_ibody_find));
330 + memset((void *)bs, 0, sizeof(struct ext3_xattr_block_find));
332 + is->s.not_found = bs->s.not_found = -ENODATA;
333 + is->iloc.bh = NULL;
336 + last = IFIRST(header);
337 + /* Find the entry best suited to be pushed into EA block */
339 + for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
340 + total_size = EXT3_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
341 + EXT3_XATTR_LEN(last->e_name_len);
342 + if (total_size <= free && total_size < temp) {
343 + if (total_size < new_extra_isize) {
344 + small_entry = last;
352 + if (entry == NULL) {
354 + entry = small_entry;
356 + if (!tried_min_extra_isize &&
357 + s_min_extra_isize) {
358 + tried_min_extra_isize++;
359 + new_extra_isize = s_min_extra_isize;
366 + offs = le16_to_cpu(entry->e_value_offs);
367 + size = le32_to_cpu(entry->e_value_size);
368 + entry_size = EXT3_XATTR_LEN(entry->e_name_len);
369 + i.name_index = entry->e_name_index,
370 + buffer = kmalloc(EXT3_XATTR_SIZE(size), GFP_KERNEL);
371 + b_entry_name = kmalloc(entry->e_name_len + 1, GFP_KERNEL);
372 + /* Save the entry name and the entry value */
373 + memcpy((void *)buffer, (void *)IFIRST(header) + offs,
374 + EXT3_XATTR_SIZE(size));
375 + memcpy((void *)b_entry_name, (void *)entry->e_name,
376 + entry->e_name_len);
377 + b_entry_name[entry->e_name_len] = '\0';
378 + i.name = b_entry_name;
380 + error = ext3_get_inode_loc(inode, &is->iloc);
384 + error = ext3_xattr_ibody_find(inode, &i, is);
388 + /* Remove the chosen entry from the inode */
389 + error = ext3_xattr_ibody_set(handle, inode, &i, is);
391 + entry = IFIRST(header);
392 + if (entry_size + EXT3_XATTR_SIZE(size) >= new_extra_isize)
393 + shift_bytes = new_extra_isize;
395 + shift_bytes = entry_size + size;
396 + /* Adjust the offsets and shift the remaining entries ahead */
397 + ext3_xattr_shift_entries(entry, EXT3_I(inode)->i_extra_isize -
398 + shift_bytes, (void *)raw_inode +
399 + EXT3_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
400 + (void *)header, total_ino - entry_size,
401 + inode->i_sb->s_blocksize);
403 + extra_isize += shift_bytes;
404 + new_extra_isize -= shift_bytes;
405 + EXT3_I(inode)->i_extra_isize = extra_isize;
407 + i.name = b_entry_name;
409 + i.value_len = cpu_to_le32(size);
410 + error = ext3_xattr_block_find(inode, &i, bs);
414 + /* Add entry which was removed from the inode into the block */
415 + error = ext3_xattr_block_set(handle, inode, &i, bs);
422 + kfree(b_entry_name);
426 + brelse(is->iloc.bh);
432 + up_write(&EXT3_I(inode)->xattr_sem);
439 * ext3_xattr_delete_inode()
441 Index: linux-2.6.18/fs/ext3/xattr.h
442 ===================================================================
443 --- linux-2.6.18.orig/fs/ext3/xattr.h
444 +++ linux-2.6.18/fs/ext3/xattr.h
445 @@ -74,6 +74,9 @@ extern int ext3_xattr_set_handle(handle_
446 extern void ext3_xattr_delete_inode(handle_t *, struct inode *);
447 extern void ext3_xattr_put_super(struct super_block *);
449 +int ext3_expand_extra_isize(struct inode *inode, int new_extra_isize,
450 + struct ext3_iloc iloc, handle_t *handle);
452 extern int init_ext3_xattr(void);
453 extern void exit_ext3_xattr(void);
455 Index: linux-2.6.18/fs/ext3/ialloc.c
456 ===================================================================
457 --- linux-2.6.18.orig/fs/ext3/ialloc.c
458 +++ linux-2.6.18/fs/ext3/ialloc.c
459 @@ -751,6 +751,7 @@ got:
461 ei->i_block_alloc_info = NULL;
462 ei->i_block_group = group;
463 + ei->i_fs_version = 0;
465 ext3_set_inode_flags(inode);
466 if (IS_DIRSYNC(inode))