Whamcloud - gitweb
e2fsck: add support for expanding the inode size
[tools/e2fsprogs.git] / lib / ext2fs / ext_attr.c
1 /*
2  * ext_attr.c --- extended attribute blocks
3  *
4  * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5  *
6  * Copyright (C) 2002 Theodore Ts'o.
7  *
8  * %Begin-Header%
9  * This file may be redistributed under the terms of the GNU Library
10  * General Public License, version 2.
11  * %End-Header%
12  */
13
14 #include "config.h"
15 #include <stdio.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <string.h>
20 #include <time.h>
21 #include <errno.h>
22 #if defined HAVE_SYS_XATTR_H
23 #include <sys/xattr.h>
24 #elif defined HAVE_ATTR_XATTR_H
25 #include <attr/xattr.h>
26 #endif
27
28 #include "ext2_fs.h"
29 #include "ext2_ext_attr.h"
30 #include "ext4_acl.h"
31
32 #include "ext2fs.h"
33
34 static errcode_t read_ea_inode_hash(ext2_filsys fs, ext2_ino_t ino, __u32 *hash)
35 {
36         struct ext2_inode inode;
37         errcode_t retval;
38
39         retval = ext2fs_read_inode(fs, ino, &inode);
40         if (retval)
41                 return retval;
42         *hash = ext2fs_get_ea_inode_hash(&inode);
43         return 0;
44 }
45
46 #define NAME_HASH_SHIFT 5
47 #define VALUE_HASH_SHIFT 16
48
49 /*
50  * ext2_xattr_hash_entry()
51  *
52  * Compute the hash of an extended attribute.
53  */
54 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
55 {
56         __u32 hash = 0;
57         char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
58         int n;
59
60         for (n = 0; n < entry->e_name_len; n++) {
61                 hash = (hash << NAME_HASH_SHIFT) ^
62                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
63                        *name++;
64         }
65
66         /* The hash needs to be calculated on the data in little-endian. */
67         if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
68                 __u32 *value = (__u32 *)data;
69                 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
70                          EXT2_EXT_ATTR_PAD_BITS; n; n--) {
71                         hash = (hash << VALUE_HASH_SHIFT) ^
72                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
73                                ext2fs_le32_to_cpu(*value++);
74                 }
75         }
76
77         return hash;
78 }
79
80 /*
81  * ext2fs_ext_attr_hash_entry2()
82  *
83  * Compute the hash of an extended attribute.
84  * This version of the function supports hashing entries that reference
85  * external inodes (ea_inode feature).
86  */
87 errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
88                                       struct ext2_ext_attr_entry *entry,
89                                       void *data, __u32 *hash)
90 {
91         *hash = ext2fs_ext_attr_hash_entry(entry, data);
92
93         if (entry->e_value_inum) {
94                 __u32 ea_inode_hash;
95                 errcode_t retval;
96
97                 retval = read_ea_inode_hash(fs, entry->e_value_inum,
98                                             &ea_inode_hash);
99                 if (retval)
100                         return retval;
101
102                 *hash = (*hash << VALUE_HASH_SHIFT) ^
103                         (*hash >> (8*sizeof(*hash) - VALUE_HASH_SHIFT)) ^
104                         ea_inode_hash;
105         }
106         return 0;
107 }
108
109 #undef NAME_HASH_SHIFT
110 #undef VALUE_HASH_SHIFT
111
112 #define BLOCK_HASH_SHIFT 16
113
114 /* Mirrors ext4_xattr_rehash() implementation in kernel. */
115 void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
116                                   struct ext2_ext_attr_entry *end)
117 {
118         struct ext2_ext_attr_entry *here;
119         __u32 hash = 0;
120
121         here = (struct ext2_ext_attr_entry *)(header+1);
122         while (here < end && !EXT2_EXT_IS_LAST_ENTRY(here)) {
123                 if (!here->e_hash) {
124                         /* Block is not shared if an entry's hash value == 0 */
125                         hash = 0;
126                         break;
127                 }
128                 hash = (hash << BLOCK_HASH_SHIFT) ^
129                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
130                        here->e_hash;
131                 here = EXT2_EXT_ATTR_NEXT(here);
132         }
133         header->h_hash = hash;
134 }
135
136 /*
137  * Re-compute the extended attribute hash value after an entry has changed.
138  */
139 static void ext2fs_attr_rehash(struct ext2_ext_attr_header *header,
140                                 struct ext2_ext_attr_entry *entry)
141 {
142         struct ext2_ext_attr_entry *here;
143         __u32 hash = 0;
144
145         entry->e_hash = ext2fs_ext_attr_hash_entry(entry, (char *)header +
146                                                    entry->e_value_offs);
147
148         here = ENTRY(header+1);
149         while (!EXT2_EXT_IS_LAST_ENTRY(here)) {
150                 if (!here->e_hash) {
151                         /* Block is not shared if an entry's hash value == 0 */
152                         hash = 0;
153                         break;
154                 }
155                 hash = (hash << BLOCK_HASH_SHIFT) ^
156                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
157                        here->e_hash;
158                 here = EXT2_EXT_ATTR_NEXT(here);
159         }
160         header->h_hash = hash;
161 }
162
163 #undef BLOCK_HASH_SHIFT
164
165 __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode)
166 {
167         return inode->i_atime;
168 }
169
170 void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash)
171 {
172         inode->i_atime = hash;
173 }
174
175 __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode)
176 {
177         return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
178 }
179
180 void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count)
181 {
182         inode->i_ctime = (__u32)(ref_count >> 32);
183         inode->osd1.linux1.l_i_version = (__u32)ref_count;
184 }
185
186 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
187 {
188         if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
189              header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
190             header->h_blocks != 1)
191                 return EXT2_ET_BAD_EA_HEADER;
192
193         return 0;
194 }
195
196 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
197                                 ext2_ino_t inum)
198 {
199         int             csum_failed = 0;
200         errcode_t       retval;
201
202         retval = io_channel_read_blk64(fs->io, block, 1, buf);
203         if (retval)
204                 return retval;
205
206         if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
207             !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
208                 csum_failed = 1;
209
210 #ifdef WORDS_BIGENDIAN
211         ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
212 #endif
213
214         retval = check_ext_attr_header(buf);
215         if (retval == 0 && csum_failed)
216                 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
217
218         return retval;
219 }
220
221 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
222 {
223         return ext2fs_read_ext_attr3(fs, block, buf, 0);
224 }
225
226 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
227 {
228         return ext2fs_read_ext_attr2(fs, block, buf);
229 }
230
231 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
232                                  ext2_ino_t inum)
233 {
234         errcode_t       retval;
235         char            *write_buf;
236
237 #ifdef WORDS_BIGENDIAN
238         retval = ext2fs_get_mem(fs->blocksize, &write_buf);
239         if (retval)
240                 return retval;
241         ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
242 #else
243         write_buf = (char *) inbuf;
244 #endif
245
246         retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
247                         (struct ext2_ext_attr_header *)write_buf);
248         if (retval)
249                 return retval;
250
251         retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
252 #ifdef WORDS_BIGENDIAN
253         ext2fs_free_mem(&write_buf);
254 #endif
255         if (!retval)
256                 ext2fs_mark_changed(fs);
257         return retval;
258 }
259
260 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
261 {
262         return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
263 }
264
265 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
266 {
267         return ext2fs_write_ext_attr2(fs, block, inbuf);
268 }
269
270 /*
271  * This function adjusts the reference count of the EA block.
272  */
273 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
274                                     char *block_buf, int adjust,
275                                     __u32 *newcount, ext2_ino_t inum)
276 {
277         errcode_t       retval;
278         struct ext2_ext_attr_header *header;
279         char    *buf = 0;
280
281         if ((blk >= ext2fs_blocks_count(fs->super)) ||
282             (blk < fs->super->s_first_data_block))
283                 return EXT2_ET_BAD_EA_BLOCK_NUM;
284
285         if (!block_buf) {
286                 retval = ext2fs_get_mem(fs->blocksize, &buf);
287                 if (retval)
288                         return retval;
289                 block_buf = buf;
290         }
291
292         retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
293         if (retval)
294                 goto errout;
295
296         header = BHDR(block_buf);
297         if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
298                 return EXT2_ET_EA_BAD_MAGIC;
299
300         header->h_refcount += adjust;
301         if (newcount)
302                 *newcount = header->h_refcount;
303
304         retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
305         if (retval)
306                 goto errout;
307
308 errout:
309         if (buf)
310                 ext2fs_free_mem(&buf);
311         return retval;
312 }
313
314 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
315                                     char *block_buf, int adjust,
316                                     __u32 *newcount)
317 {
318         return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
319                                           newcount, 0);
320 }
321
322 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
323                                         char *block_buf, int adjust,
324                                         __u32 *newcount)
325 {
326         return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
327                                           newcount);
328 }
329
330 /* Manipulate the contents of extended attribute regions */
331 struct ext2_xattr {
332         char *name;
333         void *value;
334         unsigned int value_len;
335         ext2_ino_t ea_ino;
336 };
337
338 struct ext2_xattr_handle {
339         errcode_t magic;
340         ext2_filsys fs;
341         struct ext2_xattr *attrs;
342         int capacity;
343         int count;
344         int ibody_count;
345         ext2_ino_t ino;
346         unsigned int flags;
347 };
348
349 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
350                                       unsigned int expandby)
351 {
352         struct ext2_xattr *new_attrs;
353         errcode_t err;
354
355         err = ext2fs_get_arrayzero(h->capacity + expandby,
356                                    sizeof(struct ext2_xattr), &new_attrs);
357         if (err)
358                 return err;
359
360         memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
361         ext2fs_free_mem(&h->attrs);
362         h->capacity += expandby;
363         h->attrs = new_attrs;
364
365         return 0;
366 }
367
368 struct ea_name_index {
369         int index;
370         const char *name;
371 };
372
373 /* Keep these names sorted in order of decreasing specificity. */
374 static struct ea_name_index ea_names[] = {
375         {3, "system.posix_acl_default"},
376         {2, "system.posix_acl_access"},
377         {8, "system.richacl"},
378         {6, "security."},
379         {4, "trusted."},
380         {7, "system."},
381         {1, "user."},
382         {0, NULL},
383 };
384
385 static const char *find_ea_prefix(int index)
386 {
387         struct ea_name_index *e;
388
389         for (e = ea_names; e->name; e++)
390                 if (e->index == index)
391                         return e->name;
392
393         return NULL;
394 }
395
396 static int find_ea_index(const char *fullname, const char **name, int *index)
397 {
398         struct ea_name_index *e;
399
400         for (e = ea_names; e->name; e++) {
401                 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
402                         *name = fullname + strlen(e->name);
403                         *index = e->index;
404                         return 1;
405                 }
406         }
407         return 0;
408 }
409
410 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
411                                struct ext2_inode_large *inode)
412 {
413         struct ext2_ext_attr_header *header;
414         void *block_buf = NULL;
415         blk64_t blk;
416         errcode_t err;
417         struct ext2_inode_large i;
418
419         /* Read inode? */
420         if (inode == NULL) {
421                 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
422                                              sizeof(struct ext2_inode_large));
423                 if (err)
424                         return err;
425                 inode = &i;
426         }
427
428         /* Do we already have an EA block? */
429         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
430         if (blk == 0)
431                 return 0;
432
433         /* Find block, zero it, write back */
434         if ((blk < fs->super->s_first_data_block) ||
435             (blk >= ext2fs_blocks_count(fs->super))) {
436                 err = EXT2_ET_BAD_EA_BLOCK_NUM;
437                 goto out;
438         }
439
440         err = ext2fs_get_mem(fs->blocksize, &block_buf);
441         if (err)
442                 goto out;
443
444         err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
445         if (err)
446                 goto out2;
447
448         /* We only know how to deal with v2 EA blocks */
449         header = (struct ext2_ext_attr_header *) block_buf;
450         if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
451                 err = EXT2_ET_BAD_EA_HEADER;
452                 goto out2;
453         }
454
455         header->h_refcount--;
456         err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
457         if (err)
458                 goto out2;
459
460         /* Erase link to block */
461         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
462         if (header->h_refcount == 0)
463                 ext2fs_block_alloc_stats2(fs, blk, -1);
464         err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
465         if (err)
466                 goto out2;
467
468         /* Write inode? */
469         if (inode == &i) {
470                 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
471                                               sizeof(struct ext2_inode_large));
472                 if (err)
473                         goto out2;
474         }
475
476 out2:
477         ext2fs_free_mem(&block_buf);
478 out:
479         return err;
480 }
481
482 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
483                                          struct ext2_inode_large *inode)
484 {
485         struct ext2_ext_attr_header *header;
486         void *block_buf = NULL;
487         blk64_t blk, goal;
488         errcode_t err;
489
490         /* Do we already have an EA block? */
491         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
492         if (blk != 0) {
493                 if ((blk < fs->super->s_first_data_block) ||
494                     (blk >= ext2fs_blocks_count(fs->super))) {
495                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
496                         goto out;
497                 }
498
499                 err = ext2fs_get_mem(fs->blocksize, &block_buf);
500                 if (err)
501                         goto out;
502
503                 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
504                 if (err)
505                         goto out2;
506
507                 /* We only know how to deal with v2 EA blocks */
508                 header = (struct ext2_ext_attr_header *) block_buf;
509                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
510                         err = EXT2_ET_BAD_EA_HEADER;
511                         goto out2;
512                 }
513
514                 /* Single-user block.  We're done here. */
515                 if (header->h_refcount == 1)
516                         goto out2;
517
518                 /* We need to CoW the block. */
519                 header->h_refcount--;
520                 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
521                 if (err)
522                         goto out2;
523         } else {
524                 /* No block, we must increment i_blocks */
525                 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
526                                              1);
527                 if (err)
528                         goto out;
529         }
530
531         /* Allocate a block */
532         goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
533         err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
534         if (err)
535                 goto out2;
536         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
537 out2:
538         if (block_buf)
539                 ext2fs_free_mem(&block_buf);
540 out:
541         return err;
542 }
543
544
545 static inline int
546 posix_acl_xattr_count(size_t size)
547 {
548         if (size < sizeof(posix_acl_xattr_header))
549                 return -1;
550         size -= sizeof(posix_acl_xattr_header);
551         if (size % sizeof(posix_acl_xattr_entry))
552                 return -1;
553         return size / sizeof(posix_acl_xattr_entry);
554 }
555
556 /*
557  * The lgetxattr function returns data formatted in the POSIX extended
558  * attribute format.  The on-disk format uses a more compact encoding.
559  * See the ext4_acl_to_disk in fs/ext4/acl.c.
560  */
561 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
562                                                   void *out_buf, size_t *size_out)
563 {
564         const posix_acl_xattr_header *header =
565                 (const posix_acl_xattr_header*) value;
566         const posix_acl_xattr_entry *end, *entry =
567                 (const posix_acl_xattr_entry *)(header+1);
568         ext4_acl_header *ext_acl;
569         size_t s;
570         char *e;
571
572         int count;
573
574         if (!value)
575                 return EINVAL;
576         if (size < sizeof(posix_acl_xattr_header))
577                 return ENOMEM;
578         if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
579                 return EINVAL;
580
581         count = posix_acl_xattr_count(size);
582         ext_acl = out_buf;
583         ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
584
585         if (count <= 0)
586                 return EINVAL;
587
588         e = (char *) out_buf + sizeof(ext4_acl_header);
589         s = sizeof(ext4_acl_header);
590         for (end = entry + count; entry != end;entry++) {
591                 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
592                 disk_entry->e_tag = ext2fs_cpu_to_le16(entry->e_tag);
593                 disk_entry->e_perm = ext2fs_cpu_to_le16(entry->e_perm);
594
595                 switch(entry->e_tag) {
596                         case ACL_USER_OBJ:
597                         case ACL_GROUP_OBJ:
598                         case ACL_MASK:
599                         case ACL_OTHER:
600                                 e += sizeof(ext4_acl_entry_short);
601                                 s += sizeof(ext4_acl_entry_short);
602                                 break;
603                         case ACL_USER:
604                         case ACL_GROUP:
605                                 disk_entry->e_id =  ext2fs_cpu_to_le32(entry->e_id);
606                                 e += sizeof(ext4_acl_entry);
607                                 s += sizeof(ext4_acl_entry);
608                                 break;
609                 }
610         }
611         *size_out = s;
612         return 0;
613 }
614
615 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
616                                                   void **out_buf, size_t *size_out)
617 {
618         posix_acl_xattr_header *header;
619         posix_acl_xattr_entry *entry;
620         const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
621         errcode_t err;
622         const char *cp;
623         char *out;
624
625         if ((!value) ||
626             (size < sizeof(ext4_acl_header)) ||
627             (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
628                 return EINVAL;
629
630         err = ext2fs_get_mem(size * 2, &out);
631         if (err)
632                 return err;
633
634         header = (posix_acl_xattr_header *) out;
635         header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
636         entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
637
638         cp = (const char *) value + sizeof(ext4_acl_header);
639         size -= sizeof(ext4_acl_header);
640
641         while (size > 0) {
642                 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
643
644                 entry->e_tag = ext2fs_le16_to_cpu(disk_entry->e_tag);
645                 entry->e_perm = ext2fs_le16_to_cpu(disk_entry->e_perm);
646
647                 switch(entry->e_tag) {
648                         case ACL_USER_OBJ:
649                         case ACL_GROUP_OBJ:
650                         case ACL_MASK:
651                         case ACL_OTHER:
652                                 entry->e_id = 0;
653                                 cp += sizeof(ext4_acl_entry_short);
654                                 size -= sizeof(ext4_acl_entry_short);
655                                 break;
656                         case ACL_USER:
657                         case ACL_GROUP:
658                                 entry->e_id = ext2fs_le32_to_cpu(disk_entry->e_id);
659                                 cp += sizeof(ext4_acl_entry);
660                                 size -= sizeof(ext4_acl_entry);
661                                 break;
662                 default:
663                         ext2fs_free_mem(&out);
664                         return EINVAL;
665                         break;
666                 }
667                 entry++;
668         }
669         *out_buf = out;
670         *size_out = ((char *) entry - out);
671         return 0;
672 }
673
674 static errcode_t
675 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
676                        void *entries_start, unsigned int storage_size,
677                        unsigned int value_offset_correction, int write_hash)
678 {
679         struct ext2_xattr *x;
680         struct ext2_ext_attr_entry *e = entries_start;
681         char *end = (char *) entries_start + storage_size;
682         const char *shortname;
683         unsigned int value_size;
684         int idx, ret;
685         errcode_t err;
686
687         memset(entries_start, 0, storage_size);
688         for (x = attrs; x < attrs + count; x++) {
689                 /* Calculate index and shortname position */
690                 shortname = x->name;
691                 ret = find_ea_index(x->name, &shortname, &idx);
692
693                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
694                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
695
696                 /* Fill out e appropriately */
697                 e->e_name_len = strlen(shortname);
698                 e->e_name_index = (ret ? idx : 0);
699
700                 e->e_value_size = x->value_len;
701                 e->e_value_inum = x->ea_ino;
702
703                 /* Store name */
704                 memcpy((char *)e + sizeof(*e), shortname, e->e_name_len);
705                 if (x->ea_ino) {
706                         e->e_value_offs = 0;
707                 } else {
708                         end -= value_size;
709                         e->e_value_offs = end - (char *) entries_start +
710                                                 value_offset_correction;
711                         memcpy(end, x->value, e->e_value_size);
712                 }
713
714                 if (write_hash || x->ea_ino) {
715                         err = ext2fs_ext_attr_hash_entry2(fs, e,
716                                                           x->ea_ino ? 0 : end,
717                                                           &e->e_hash);
718                         if (err)
719                                 return err;
720                 } else
721                         e->e_hash = 0;
722
723                 e = EXT2_EXT_ATTR_NEXT(e);
724                 *(__u32 *)e = 0;
725         }
726         return 0;
727 }
728
729 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
730 {
731         ext2_filsys fs = handle->fs;
732         const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
733         struct ext2_inode_large *inode;
734         char *start, *block_buf = NULL;
735         struct ext2_ext_attr_header *header;
736         __u32 ea_inode_magic;
737         blk64_t blk;
738         unsigned int storage_size;
739         unsigned int i;
740         errcode_t err;
741
742         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
743         i = inode_size;
744         if (i < sizeof(*inode))
745                 i = sizeof(*inode);
746         err = ext2fs_get_memzero(i, &inode);
747         if (err)
748                 return err;
749
750         err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
751                                      inode_size);
752         if (err)
753                 goto out;
754
755         /* If extra_isize isn't set, we need to set it now */
756         if (inode->i_extra_isize == 0 &&
757             inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
758                 char *p = (char *)inode;
759                 size_t extra = fs->super->s_want_extra_isize;
760
761                 if (extra == 0)
762                         extra = sizeof(__u32);
763                 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
764                 inode->i_extra_isize = extra;
765         }
766         if (inode->i_extra_isize & 3) {
767                 err = EXT2_ET_INODE_CORRUPTED;
768                 goto out;
769         }
770
771         /* Does the inode have space for EA? */
772         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
773             inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
774                                                                 sizeof(__u32))
775                 goto write_ea_block;
776
777         /* Write the inode EA */
778         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
779         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
780                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
781         storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
782                                 inode->i_extra_isize - sizeof(__u32);
783         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
784                                 inode->i_extra_isize + sizeof(__u32);
785
786         err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
787                                      start, storage_size, 0, 0);
788         if (err)
789                 goto out;
790 write_ea_block:
791         /* Are we done? */
792         if (handle->ibody_count == handle->count &&
793             !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
794                 goto skip_ea_block;
795
796         /* Write the EA block */
797         err = ext2fs_get_memzero(fs->blocksize, &block_buf);
798         if (err)
799                 goto out;
800
801         storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
802         start = block_buf + sizeof(struct ext2_ext_attr_header);
803
804         err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
805                                      handle->count - handle->ibody_count, start,
806                                      storage_size, start - block_buf, 1);
807         if (err)
808                 goto out2;
809
810         /* Write a header on the EA block */
811         header = (struct ext2_ext_attr_header *) block_buf;
812         header->h_magic = EXT2_EXT_ATTR_MAGIC;
813         header->h_refcount = 1;
814         header->h_blocks = 1;
815
816         /* Get a new block for writing */
817         err = prep_ea_block_for_write(fs, handle->ino, inode);
818         if (err)
819                 goto out2;
820
821         /* Finally, write the new EA block */
822         blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
823         err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
824         if (err)
825                 goto out2;
826
827 skip_ea_block:
828         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
829         if (!block_buf && blk) {
830                 /* xattrs shrunk, free the block */
831                 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
832                 if (err)
833                         goto out;
834         }
835
836         /* Write the inode */
837         err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
838                                       inode_size);
839         if (err)
840                 goto out2;
841
842 out2:
843         ext2fs_free_mem(&block_buf);
844 out:
845         ext2fs_free_mem(&inode);
846         return err;
847 }
848
849 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
850                                          struct ext2_inode_large *inode,
851                                          struct ext2_ext_attr_entry *entries,
852                                          unsigned int storage_size,
853                                          char *value_start)
854 {
855         struct ext2_xattr *x;
856         struct ext2_ext_attr_entry *entry, *end;
857         const char *prefix;
858         unsigned int remain, prefix_len;
859         errcode_t err;
860         unsigned int values_size = storage_size +
861                         ((char *)entries - value_start);
862
863         /* find the end */
864         end = entries;
865         remain = storage_size;
866         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
867                !EXT2_EXT_IS_LAST_ENTRY(end)) {
868
869                 /* header eats this space */
870                 remain -= sizeof(struct ext2_ext_attr_entry);
871
872                 /* is attribute name valid? */
873                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
874                         return EXT2_ET_EA_BAD_NAME_LEN;
875
876                 /* attribute len eats this space */
877                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
878                 end = EXT2_EXT_ATTR_NEXT(end);
879         }
880
881         entry = entries;
882         remain = storage_size;
883         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
884                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
885
886                 /* Allocate space for more attrs? */
887                 if (handle->count == handle->capacity) {
888                         err = ext2fs_xattrs_expand(handle, 4);
889                         if (err)
890                                 return err;
891                 }
892
893                 x = handle->attrs + handle->count;
894
895                 /* header eats this space */
896                 remain -= sizeof(struct ext2_ext_attr_entry);
897
898                 /* attribute len eats this space */
899                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
900
901                 /* Extract name */
902                 prefix = find_ea_prefix(entry->e_name_index);
903                 prefix_len = (prefix ? strlen(prefix) : 0);
904                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
905                                          &x->name);
906                 if (err)
907                         return err;
908                 if (prefix)
909                         memcpy(x->name, prefix, prefix_len);
910                 if (entry->e_name_len)
911                         memcpy(x->name + prefix_len,
912                                (char *)entry + sizeof(*entry),
913                                entry->e_name_len);
914
915                 /* Check & copy value */
916                 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
917                     entry->e_value_inum != 0)
918                         return EXT2_ET_BAD_EA_BLOCK_NUM;
919
920                 if (entry->e_value_inum == 0) {
921                         if (entry->e_value_size > remain)
922                                 return EXT2_ET_EA_BAD_VALUE_SIZE;
923
924                         if (entry->e_value_offs + entry->e_value_size > values_size)
925                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
926
927                         if (entry->e_value_size > 0 &&
928                             value_start + entry->e_value_offs <
929                             (char *)end + sizeof(__u32))
930                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
931
932                         remain -= entry->e_value_size;
933
934                         err = ext2fs_get_mem(entry->e_value_size, &x->value);
935                         if (err)
936                                 return err;
937                         memcpy(x->value, value_start + entry->e_value_offs,
938                                entry->e_value_size);
939                 } else {
940                         struct ext2_inode *ea_inode;
941                         ext2_file_t ea_file;
942
943                         if (entry->e_value_offs != 0)
944                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
945
946                         if (entry->e_value_size > (64 * 1024))
947                                 return EXT2_ET_EA_BAD_VALUE_SIZE;
948
949                         err = ext2fs_get_mem(entry->e_value_size, &x->value);
950                         if (err)
951                                 return err;
952
953                         err = ext2fs_file_open(handle->fs, entry->e_value_inum,
954                                                0, &ea_file);
955                         if (err)
956                                 return err;
957
958                         ea_inode = ext2fs_file_get_inode(ea_file);
959                         if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
960                             !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
961                             ea_inode->i_links_count == 0)
962                                 err = EXT2_ET_EA_INODE_CORRUPTED;
963                         else if (ext2fs_file_get_size(ea_file) !=
964                             entry->e_value_size)
965                                 err = EXT2_ET_EA_BAD_VALUE_SIZE;
966                         else
967                                 err = ext2fs_file_read(ea_file, x->value,
968                                                        entry->e_value_size, 0);
969                         ext2fs_file_close(ea_file);
970                         if (err)
971                                 return err;
972                 }
973
974                 x->ea_ino = entry->e_value_inum;
975                 x->value_len = entry->e_value_size;
976
977                 /* e_hash may be 0 in older inode's ea */
978                 if (entry->e_hash != 0) {
979                         __u32 hash;
980                         void *data = (entry->e_value_inum != 0) ?
981                                         0 : value_start + entry->e_value_offs;
982
983                         err = ext2fs_ext_attr_hash_entry2(handle->fs, entry,
984                                                           data, &hash);
985                         if (err)
986                                 return err;
987                         if (entry->e_hash != hash) {
988                                 struct ext2_inode child;
989
990                                 /* Check whether this is an old Lustre-style
991                                  * ea_inode reference.
992                                  */
993                                 err = ext2fs_read_inode(handle->fs,
994                                                         entry->e_value_inum,
995                                                         &child);
996                                 if (err)
997                                         return err;
998                                 if (child.i_mtime != handle->ino ||
999                                     child.i_generation != inode->i_generation)
1000                                         return EXT2_ET_BAD_EA_HASH;
1001                         }
1002                 }
1003
1004                 handle->count++;
1005                 entry = EXT2_EXT_ATTR_NEXT(entry);
1006         }
1007
1008         return 0;
1009 }
1010
1011 static void xattrs_free_keys(struct ext2_xattr_handle *h)
1012 {
1013         struct ext2_xattr *a = h->attrs;
1014         int i;
1015
1016         for (i = 0; i < h->capacity; i++) {
1017                 if (a[i].name)
1018                         ext2fs_free_mem(&a[i].name);
1019                 if (a[i].value)
1020                         ext2fs_free_mem(&a[i].value);
1021         }
1022         h->count = 0;
1023         h->ibody_count = 0;
1024 }
1025
1026 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
1027 {
1028         struct ext2_inode_large *inode;
1029         struct ext2_ext_attr_header *header;
1030         __u32 ea_inode_magic;
1031         unsigned int storage_size;
1032         char *start, *block_buf = NULL;
1033         blk64_t blk;
1034         size_t i;
1035         errcode_t err;
1036
1037         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1038         i = EXT2_INODE_SIZE(handle->fs->super);
1039         if (i < sizeof(*inode))
1040                 i = sizeof(*inode);
1041         err = ext2fs_get_memzero(i, &inode);
1042         if (err)
1043                 return err;
1044
1045         err = ext2fs_read_inode_full(handle->fs, handle->ino,
1046                                      (struct ext2_inode *)inode,
1047                                      EXT2_INODE_SIZE(handle->fs->super));
1048         if (err)
1049                 goto out;
1050
1051         xattrs_free_keys(handle);
1052
1053         /* Does the inode have space for EA? */
1054         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1055             EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1056                                                   inode->i_extra_isize +
1057                                                   sizeof(__u32))
1058                 goto read_ea_block;
1059         if (inode->i_extra_isize & 3) {
1060                 err = EXT2_ET_INODE_CORRUPTED;
1061                 goto out;
1062         }
1063
1064         /* Look for EA in the inode */
1065         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1066                inode->i_extra_isize, sizeof(__u32));
1067         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1068                 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1069                         EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1070                         sizeof(__u32);
1071                 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1072                         inode->i_extra_isize + sizeof(__u32);
1073
1074                 err = read_xattrs_from_buffer(handle, inode,
1075                                         (struct ext2_ext_attr_entry *) start,
1076                                         storage_size, start);
1077                 if (err)
1078                         goto out;
1079
1080                 handle->ibody_count = handle->count;
1081         }
1082
1083 read_ea_block:
1084         /* Look for EA in a separate EA block */
1085         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
1086         if (blk != 0) {
1087                 if ((blk < handle->fs->super->s_first_data_block) ||
1088                     (blk >= ext2fs_blocks_count(handle->fs->super))) {
1089                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
1090                         goto out;
1091                 }
1092
1093                 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1094                 if (err)
1095                         goto out;
1096
1097                 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1098                                             handle->ino);
1099                 if (err)
1100                         goto out3;
1101
1102                 /* We only know how to deal with v2 EA blocks */
1103                 header = (struct ext2_ext_attr_header *) block_buf;
1104                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1105                         err = EXT2_ET_BAD_EA_HEADER;
1106                         goto out3;
1107                 }
1108
1109                 /* Read EAs */
1110                 storage_size = handle->fs->blocksize -
1111                         sizeof(struct ext2_ext_attr_header);
1112                 start = block_buf + sizeof(struct ext2_ext_attr_header);
1113                 err = read_xattrs_from_buffer(handle, inode,
1114                                         (struct ext2_ext_attr_entry *) start,
1115                                         storage_size, block_buf);
1116                 if (err)
1117                         goto out3;
1118
1119                 ext2fs_free_mem(&block_buf);
1120         }
1121
1122         ext2fs_free_mem(&block_buf);
1123         ext2fs_free_mem(&inode);
1124         return 0;
1125
1126 out3:
1127         ext2fs_free_mem(&block_buf);
1128 out:
1129         ext2fs_free_mem(&inode);
1130         return err;
1131 }
1132
1133 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1134                                 int (*func)(char *name, char *value,
1135                                             size_t value_len, void *data),
1136                                 void *data)
1137 {
1138         struct ext2_xattr *x;
1139         int dirty = 0;
1140         int ret;
1141
1142         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1143         for (x = h->attrs; x < h->attrs + h->count; x++) {
1144                 ret = func(x->name, x->value, x->value_len, data);
1145                 if (ret & XATTR_CHANGED)
1146                         dirty = 1;
1147                 if (ret & XATTR_ABORT)
1148                         break;
1149         }
1150
1151         if (dirty)
1152                 return ext2fs_xattrs_write(h);
1153         return 0;
1154 }
1155
1156 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1157                            void **value, size_t *value_len)
1158 {
1159         struct ext2_xattr *x;
1160         char *val;
1161         errcode_t err;
1162
1163         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1164         for (x = h->attrs; x < h->attrs + h->count; x++) {
1165                 if (strcmp(x->name, key))
1166                         continue;
1167
1168                 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1169                     ((strcmp(key, "system.posix_acl_default") == 0) ||
1170                      (strcmp(key, "system.posix_acl_access") == 0))) {
1171                         err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1172                                                                value, value_len);
1173                         return err;
1174                 } else {
1175                         err = ext2fs_get_mem(x->value_len, &val);
1176                         if (err)
1177                                 return err;
1178                         memcpy(val, x->value, x->value_len);
1179                         *value = val;
1180                         *value_len = x->value_len;
1181                         return 0;
1182                 }
1183         }
1184
1185         return EXT2_ET_EA_KEY_NOT_FOUND;
1186 }
1187
1188 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1189                                       size_t *size)
1190 {
1191         struct ext2_ext_attr_entry *entry;
1192         struct ext2_inode_large *inode;
1193         __u32 ea_inode_magic;
1194         unsigned int minoff;
1195         char *start;
1196         size_t i;
1197         errcode_t err;
1198
1199         i = EXT2_INODE_SIZE(fs->super);
1200         if (i < sizeof(*inode))
1201                 i = sizeof(*inode);
1202         err = ext2fs_get_memzero(i, &inode);
1203         if (err)
1204                 return err;
1205
1206         err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1207                                      EXT2_INODE_SIZE(fs->super));
1208         if (err)
1209                 goto out;
1210
1211         /* Does the inode have size for EA? */
1212         if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1213                                                   inode->i_extra_isize +
1214                                                   sizeof(__u32)) {
1215                 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1216                 goto out;
1217         }
1218
1219         minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1220         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1221                inode->i_extra_isize, sizeof(__u32));
1222         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1223                 /* has xattrs.  calculate the size */
1224                 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1225                         inode->i_extra_isize + sizeof(__u32);
1226                 entry = (struct ext2_ext_attr_entry *) start;
1227                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1228                         if (!entry->e_value_inum && entry->e_value_size) {
1229                                 unsigned int offs = entry->e_value_offs;
1230                                 if (offs < minoff)
1231                                         minoff = offs;
1232                         }
1233                         entry = EXT2_EXT_ATTR_NEXT(entry);
1234                 }
1235                 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1236         } else {
1237                 /* no xattr.  return a maximum size */
1238                 *size = EXT2_EXT_ATTR_SIZE(minoff -
1239                                            EXT2_EXT_ATTR_LEN(strlen("data")) -
1240                                            EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1241         }
1242
1243 out:
1244         ext2fs_free_mem(&inode);
1245         return err;
1246 }
1247
1248 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1249                                        size_t value_len, ext2_ino_t *ea_ino)
1250 {
1251         struct ext2_inode inode;
1252         ext2_ino_t ino;
1253         ext2_file_t file;
1254         __u32 hash;
1255         errcode_t ret;
1256
1257         ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1258         if (ret)
1259                 return ret;
1260
1261         memset(&inode, 0, sizeof(inode));
1262         inode.i_flags |= EXT4_EA_INODE_FL;
1263         if (ext2fs_has_feature_extents(fs->super))
1264                 inode.i_flags |= EXT4_EXTENTS_FL;
1265         inode.i_size = 0;
1266         inode.i_mode = LINUX_S_IFREG | 0600;
1267         inode.i_links_count = 1;
1268         ret = ext2fs_write_new_inode(fs, ino, &inode);
1269         if (ret)
1270                 return ret;
1271         /*
1272          * ref_count and hash utilize inode's i_*time fields.
1273          * ext2fs_write_new_inode() call above initializes these fields with
1274          * current time. That's why ref count and hash updates are done
1275          * separately below.
1276          */
1277         ext2fs_set_ea_inode_ref(&inode, 1);
1278         hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1279         ext2fs_set_ea_inode_hash(&inode, hash);
1280
1281         ret = ext2fs_write_inode(fs, ino, &inode);
1282         if (ret)
1283                 return ret;
1284
1285         ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1286         if (ret)
1287                 return ret;
1288         ret = ext2fs_file_write(file, value, value_len, NULL);
1289         ext2fs_file_close(file);
1290         if (ret)
1291                 return ret;
1292
1293         ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1294
1295         *ea_ino = ino;
1296         return 0;
1297 }
1298
1299 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1300 {
1301         struct ext2_inode_large inode;
1302         __u64 ref_count;
1303         errcode_t ret;
1304
1305         ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1306                                      sizeof(inode));
1307         if (ret)
1308                 goto out;
1309
1310         ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1311         ref_count--;
1312         ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1313
1314         if (ref_count)
1315                 goto write_out;
1316
1317         inode.i_links_count = 0;
1318         inode.i_dtime = fs->now ? fs->now : time(0);
1319
1320         ret = ext2fs_free_ext_attr(fs, ino, &inode);
1321         if (ret)
1322                 goto write_out;
1323
1324         if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1325                 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1326                                    0, ~0ULL);
1327                 if (ret)
1328                         goto out;
1329         }
1330
1331         ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1332
1333 write_out:
1334         ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1335                                       sizeof(inode));
1336 out:
1337         return ret;
1338 }
1339
1340 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1341                                     const char *name, const void *value,
1342                                     size_t value_len, int in_inode)
1343 {
1344         ext2_ino_t ea_ino = 0;
1345         void *new_value = NULL;
1346         char *new_name = NULL;
1347         int name_len;
1348         errcode_t ret;
1349
1350         if (!x->name) {
1351                 name_len = strlen(name);
1352                 ret = ext2fs_get_mem(name_len + 1, &new_name);
1353                 if (ret)
1354                         goto fail;
1355                 memcpy(new_name, name, name_len + 1);
1356         }
1357
1358         ret = ext2fs_get_mem(value_len, &new_value);
1359         if (ret)
1360                 goto fail;
1361         memcpy(new_value, value, value_len);
1362
1363         if (in_inode) {
1364                 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1365                 if (ret)
1366                         goto fail;
1367         }
1368
1369         if (x->ea_ino) {
1370                 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1371                 if (ret)
1372                         goto fail;
1373         }
1374
1375         if (!x->name)
1376                 x->name = new_name;
1377
1378         if (x->value)
1379                 ext2fs_free_mem(&x->value);
1380         x->value = new_value;
1381         x->value_len = value_len;
1382         x->ea_ino = ea_ino;
1383         return 0;
1384 fail:
1385         if (new_name)
1386                 ext2fs_free_mem(&new_name);
1387         if (new_value)
1388                 ext2fs_free_mem(&new_value);
1389         if (ea_ino)
1390                 xattr_inode_dec_ref(fs, ea_ino);
1391         return ret;
1392 }
1393
1394 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1395                                const char *name)
1396 {
1397         struct ext2_xattr *x;
1398         int i;
1399         const char *shortname, *x_shortname;
1400         int name_idx, x_name_idx;
1401         int shortname_len, x_shortname_len;
1402
1403         find_ea_index(name, &shortname, &name_idx);
1404         shortname_len = strlen(shortname);
1405
1406         for (i = 0, x = attrs; i < count; i++, x++) {
1407                 find_ea_index(x->name, &x_shortname, &x_name_idx);
1408                 if (name_idx < x_name_idx)
1409                         break;
1410                 if (name_idx > x_name_idx)
1411                         continue;
1412
1413                 x_shortname_len = strlen(x_shortname);
1414                 if (shortname_len < x_shortname_len)
1415                         break;
1416                 if (shortname_len > x_shortname_len)
1417                         continue;
1418
1419                 if (memcmp(shortname, x_shortname, shortname_len) <= 0)
1420                         break;
1421         }
1422         return i;
1423 }
1424
1425 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1426                                     const char *name,
1427                                     const void *value, size_t value_len,
1428                                     int ibody_free, int block_free,
1429                                     int old_idx, int in_inode)
1430 {
1431         struct ext2_xattr tmp;
1432         int add_to_ibody;
1433         int needed;
1434         int name_len, name_idx;
1435         const char *shortname;
1436         int new_idx;
1437         int ret;
1438
1439         find_ea_index(name, &shortname, &name_idx);
1440         name_len = strlen(shortname);
1441
1442         needed = EXT2_EXT_ATTR_LEN(name_len);
1443         if (!in_inode)
1444                 needed += EXT2_EXT_ATTR_SIZE(value_len);
1445
1446         if (old_idx >= 0 && old_idx < h->ibody_count) {
1447                 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1448                 if (!h->attrs[old_idx].ea_ino)
1449                         ibody_free += EXT2_EXT_ATTR_SIZE(
1450                                                 h->attrs[old_idx].value_len);
1451         }
1452
1453         if (needed <= ibody_free) {
1454                 if (old_idx < 0) {
1455                         new_idx = h->ibody_count;
1456                         add_to_ibody = 1;
1457                         goto add_new;
1458                 }
1459
1460                 /* Update the existing entry. */
1461                 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1462                                          value, value_len, in_inode);
1463                 if (ret)
1464                         return ret;
1465                 if (h->ibody_count <= old_idx) {
1466                         /* Move entry from block to the end of ibody. */
1467                         tmp = h->attrs[old_idx];
1468                         memmove(h->attrs + h->ibody_count + 1,
1469                                 h->attrs + h->ibody_count,
1470                                 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1471                         h->attrs[h->ibody_count] = tmp;
1472                         h->ibody_count++;
1473                 }
1474                 return 0;
1475         }
1476
1477         if (h->ibody_count <= old_idx) {
1478                 block_free += EXT2_EXT_ATTR_LEN(name_len);
1479                 if (!h->attrs[old_idx].ea_ino)
1480                         block_free +=
1481                                 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1482         }
1483
1484         if (needed > block_free)
1485                 return EXT2_ET_EA_NO_SPACE;
1486
1487         if (old_idx >= 0) {
1488                 /* Update the existing entry. */
1489                 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1490                                          value, value_len, in_inode);
1491                 if (ret)
1492                         return ret;
1493                 if (old_idx < h->ibody_count) {
1494                         /*
1495                          * Move entry from ibody to the block. Note that
1496                          * entries in the block are sorted.
1497                          */
1498                         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1499                                 h->count - h->ibody_count, name);
1500                         new_idx += h->ibody_count - 1;
1501                         tmp = h->attrs[old_idx];
1502                         memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1503                                 (new_idx - old_idx) * sizeof(*h->attrs));
1504                         h->attrs[new_idx] = tmp;
1505                         h->ibody_count--;
1506                 }
1507                 return 0;
1508         }
1509
1510         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1511                                       h->count - h->ibody_count, name);
1512         new_idx += h->ibody_count;
1513         add_to_ibody = 0;
1514
1515 add_new:
1516         if (h->count == h->capacity) {
1517                 ret = ext2fs_xattrs_expand(h, 4);
1518                 if (ret)
1519                         return ret;
1520         }
1521
1522         ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, value,
1523                                  value_len, in_inode);
1524         if (ret)
1525                 return ret;
1526
1527         tmp = h->attrs[h->count];
1528         memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1529                 (h->count - new_idx)*sizeof(*h->attrs));
1530         h->attrs[new_idx] = tmp;
1531         if (add_to_ibody)
1532                 h->ibody_count++;
1533         h->count++;
1534         return 0;
1535 }
1536
1537 static int space_used(struct ext2_xattr *attrs, int count)
1538 {
1539         int total = 0;
1540         struct ext2_xattr *x;
1541         const char *shortname;
1542         int i, len, name_idx;
1543
1544         for (i = 0, x = attrs; i < count; i++, x++) {
1545                 find_ea_index(x->name, &shortname, &name_idx);
1546                 len = strlen(shortname);
1547                 total += EXT2_EXT_ATTR_LEN(len);
1548                 if (!x->ea_ino)
1549                         total += EXT2_EXT_ATTR_SIZE(x->value_len);
1550         }
1551         return total;
1552 }
1553
1554 /*
1555  * The minimum size of EA value when you start storing it in an external inode
1556  * size of block - size of header - size of 1 entry - 4 null bytes
1557  */
1558 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1559         ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1560
1561 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1562                            const char *name,
1563                            const void *value,
1564                            size_t value_len)
1565 {
1566         ext2_filsys fs = h->fs;
1567         const int inode_size = EXT2_INODE_SIZE(fs->super);
1568         struct ext2_inode_large *inode = NULL;
1569         struct ext2_xattr *x;
1570         char *new_value;
1571         int ibody_free, block_free;
1572         int in_inode = 0;
1573         int old_idx = -1;
1574         int extra_isize;
1575         errcode_t ret;
1576
1577         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1578
1579         ret = ext2fs_get_mem(value_len, &new_value);
1580         if (ret)
1581                 return ret;
1582         if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1583             ((strcmp(name, "system.posix_acl_default") == 0) ||
1584              (strcmp(name, "system.posix_acl_access") == 0))) {
1585                 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1586                                                        new_value, &value_len);
1587                 if (ret)
1588                         goto out;
1589         } else
1590                 memcpy(new_value, value, value_len);
1591
1592         /* Imitate kernel behavior by skipping update if value is the same. */
1593         for (x = h->attrs; x < h->attrs + h->count; x++) {
1594                 if (!strcmp(x->name, name)) {
1595                         if (!x->ea_ino && x->value_len == value_len &&
1596                             !memcmp(x->value, new_value, value_len)) {
1597                                 ret = 0;
1598                                 goto out;
1599                         }
1600                         old_idx = x - h->attrs;
1601                         break;
1602                 }
1603         }
1604
1605         ret = ext2fs_get_memzero(inode_size, &inode);
1606         if (ret)
1607                 goto out;
1608         ret = ext2fs_read_inode_full(fs, h->ino,
1609                                      (struct ext2_inode *)inode,
1610                                      inode_size);
1611         if (ret)
1612                 goto out;
1613         if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1614                 extra_isize = inode->i_extra_isize;
1615                 if (extra_isize == 0) {
1616                         extra_isize = fs->super->s_want_extra_isize;
1617                         if (extra_isize == 0)
1618                                 extra_isize = sizeof(__u32);
1619                 }
1620                 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1621                 ibody_free -= extra_isize;
1622                 /* Extended attribute magic and final null entry. */
1623                 ibody_free -= sizeof(__u32) * 2;
1624                 ibody_free -= space_used(h->attrs, h->ibody_count);
1625         } else
1626                 ibody_free = 0;
1627
1628         /* Inline data can only go to ibody. */
1629         if (strcmp(name, "system.data") == 0) {
1630                 if (h->ibody_count <= old_idx) {
1631                         ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1632                         goto out;
1633                 }
1634                 ret = xattr_array_update(h, name, value, value_len, ibody_free,
1635                                          0 /* block_free */, old_idx,
1636                                          0 /* in_inode */);
1637                 if (ret)
1638                         goto out;
1639                 goto write_out;
1640         }
1641
1642         block_free = fs->blocksize;
1643         block_free -= sizeof(struct ext2_ext_attr_header);
1644         /* Final null entry. */
1645         block_free -= sizeof(__u32);
1646         block_free -= space_used(h->attrs + h->ibody_count,
1647                                  h->count - h->ibody_count);
1648
1649         if (ext2fs_has_feature_ea_inode(fs->super) &&
1650             value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1651                 in_inode = 1;
1652
1653         ret = xattr_array_update(h, name, value, value_len, ibody_free,
1654                                  block_free, old_idx, in_inode);
1655         if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1656             ext2fs_has_feature_ea_inode(fs->super))
1657                 ret = xattr_array_update(h, name, value, value_len, ibody_free,
1658                                  block_free, old_idx, 1 /* in_inode */);
1659         if (ret)
1660                 goto out;
1661
1662 write_out:
1663         ret = ext2fs_xattrs_write(h);
1664 out:
1665         if (inode)
1666                 ext2fs_free_mem(&inode);
1667         ext2fs_free_mem(&new_value);
1668         return ret;
1669 }
1670
1671 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1672                               const char *key)
1673 {
1674         struct ext2_xattr *x;
1675         struct ext2_xattr *end = handle->attrs + handle->count;
1676
1677         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1678         for (x = handle->attrs; x < end; x++) {
1679                 if (strcmp(x->name, key) == 0) {
1680                         ext2fs_free_mem(&x->name);
1681                         ext2fs_free_mem(&x->value);
1682                         if (x->ea_ino)
1683                                 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1684                         memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1685                         memset(end - 1, 0, sizeof(*end));
1686                         if (x < handle->attrs + handle->ibody_count)
1687                                 handle->ibody_count--;
1688                         handle->count--;
1689                         return ext2fs_xattrs_write(handle);
1690                 }
1691         }
1692
1693         /* no key found, success! */
1694         return 0;
1695 }
1696
1697 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1698                              struct ext2_xattr_handle **handle)
1699 {
1700         struct ext2_xattr_handle *h;
1701         errcode_t err;
1702
1703         if (!ext2fs_has_feature_xattr(fs->super) &&
1704             !ext2fs_has_feature_inline_data(fs->super))
1705                 return EXT2_ET_MISSING_EA_FEATURE;
1706
1707         err = ext2fs_get_memzero(sizeof(*h), &h);
1708         if (err)
1709                 return err;
1710
1711         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1712         h->capacity = 4;
1713         err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1714                                    &h->attrs);
1715         if (err) {
1716                 ext2fs_free_mem(&h);
1717                 return err;
1718         }
1719         h->count = 0;
1720         h->ino = ino;
1721         h->fs = fs;
1722         *handle = h;
1723         return 0;
1724 }
1725
1726 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1727 {
1728         struct ext2_xattr_handle *h = *handle;
1729
1730         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1731         xattrs_free_keys(h);
1732         ext2fs_free_mem(&h->attrs);
1733         ext2fs_free_mem(handle);
1734         return 0;
1735 }
1736
1737 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1738 {
1739         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1740         *count = handle->count;
1741         return 0;
1742 }
1743
1744 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1745                               unsigned int *new_flags, unsigned int *old_flags)
1746 {
1747         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1748         if (old_flags)
1749                 *old_flags = handle->flags;
1750         if (new_flags)
1751                 handle->flags = *new_flags;
1752         return 0;
1753 }
1754
1755 struct ext2_attr_info {
1756         int name_index;
1757         const char *name;
1758         const char *value;
1759         int value_len;
1760 };
1761
1762 struct ext2_attr_search {
1763         struct ext2_ext_attr_entry *first;
1764         char *base;
1765         char *end;
1766         struct ext2_ext_attr_entry *here;
1767         int not_found;
1768 };
1769
1770 struct ext2_attr_ibody_find {
1771         ext2_ino_t ino;
1772         struct ext2_attr_search s;
1773 };
1774
1775 struct ext2_attr_block_find {
1776         struct ext2_attr_search s;
1777         char *block;
1778 };
1779
1780 void ext2fs_attr_shift_entries(struct ext2_ext_attr_entry *entry,
1781                                int value_offs_shift, char *to,
1782                                char *from, int n)
1783 {
1784         struct ext2_ext_attr_entry *last = entry;
1785
1786         /* Adjust the value offsets of the entries */
1787         for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1788                 if (!last->e_value_inum && last->e_value_size) {
1789                         last->e_value_offs = last->e_value_offs +
1790                                                         value_offs_shift;
1791                 }
1792         }
1793         /* Shift the entries by n bytes and zero freed space in inode */
1794         memmove(to, from, n);
1795         if (to > from)
1796                 memset(from, 0, to - from);
1797 }
1798
1799 /*
1800  * This function returns the free space present in the inode or the EA block.
1801  * total is number of bytes taken up by the EA entries and is used to shift
1802  * the EAs in ext2fs_expand_extra_isize().
1803  */
1804 int ext2fs_attr_free_space(struct ext2_ext_attr_entry *last,
1805                            int *min_offs, char *base, int *total)
1806 {
1807         for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1808                 *total += EXT2_EXT_ATTR_LEN(last->e_name_len);
1809                 if (!last->e_value_inum && last->e_value_size) {
1810                         int offs = last->e_value_offs;
1811                         if (offs < *min_offs)
1812                                 *min_offs = offs;
1813                 }
1814         }
1815
1816         return *min_offs - ((char *)last - base) - sizeof(__u32);
1817 }
1818
1819 static errcode_t ext2fs_attr_check_names(struct ext2_ext_attr_entry *entry,
1820                                          char *end)
1821 {
1822         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1823                 struct ext2_ext_attr_entry *next = EXT2_EXT_ATTR_NEXT(entry);
1824                 if ((char *)next >= end)
1825                         return EXT2_ET_EA_BAD_ENTRIES;
1826                 entry = next;
1827         }
1828         return 0;
1829 }
1830
1831 /* The unused parameter used to be the blocksize, but with in-inode xattrs
1832  * the xattr storage area size depends on where the xattrs are kept.  Keep
1833  * this parameter for API/ABI compatibility, but it is not needed. */
1834 static errcode_t ext2fs_attr_find_entry(struct ext2_ext_attr_entry **pentry,
1835                                         int name_index, const char *name,
1836                                         int unused, int sorted)
1837 {
1838         struct ext2_ext_attr_entry *entry;
1839         int name_len;
1840         int cmp = 1;
1841
1842         if (name == NULL)
1843                 return EXT2_ET_EA_BAD_NAME;
1844
1845         name_len = strlen(name);
1846         entry = *pentry;
1847         for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
1848                 entry = EXT2_EXT_ATTR_NEXT(entry)) {
1849                 cmp = name_index - entry->e_name_index;
1850                 if (!cmp)
1851                         cmp = name_len - entry->e_name_len;
1852                 if (!cmp)
1853                         cmp = memcmp(name, entry->e_name, name_len);
1854                 if (cmp <= 0 && (sorted || cmp == 0))
1855                         break;
1856         }
1857         *pentry = entry;
1858
1859         return cmp ? EXT2_ET_EA_NAME_NOT_FOUND : 0;
1860 }
1861
1862 static errcode_t ext2fs_attr_block_find(ext2_filsys fs,struct ext2_inode *inode,
1863                                         struct ext2_attr_info *i,
1864                                         struct ext2_attr_block_find *bs)
1865 {
1866         struct ext2_ext_attr_header *header;
1867         errcode_t error;
1868
1869         if (inode->i_file_acl) {
1870                 /* The inode already has an extended attribute block. */
1871                 error = ext2fs_get_mem(fs->blocksize, &bs->block);
1872                 if (error)
1873                         return error;
1874                 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, bs->block);
1875                 if (error)
1876                         goto cleanup;
1877
1878                 header = BHDR(bs->block);
1879                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1880                         error = EXT2_ET_EA_BAD_MAGIC;
1881                         goto cleanup;
1882                 }
1883
1884                 /* Find the named attribute. */
1885                 bs->s.base = bs->block;
1886                 bs->s.first = (struct ext2_ext_attr_entry *)(header + 1);
1887                 bs->s.end = bs->block + fs->blocksize;
1888                 bs->s.here = bs->s.first;
1889                 error = ext2fs_attr_find_entry(&bs->s.here, i->name_index,
1890                                                i->name, fs->blocksize, 1);
1891                 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1892                         goto cleanup;
1893                 bs->s.not_found = error;
1894         }
1895         error = 0;
1896
1897 cleanup:
1898         if (error && bs->block)
1899                 ext2fs_free_mem(&bs->block);
1900         return error;
1901 }
1902
1903 static errcode_t ext2fs_attr_ibody_find(ext2_filsys fs,
1904                                         struct ext2_inode_large *inode,
1905                                         struct ext2_attr_info *i,
1906                                         struct ext2_attr_ibody_find *is)
1907 {
1908         __u32 *eamagic;
1909         char *start;
1910         errcode_t error;
1911
1912         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1913                 return 0;
1914
1915         if (inode->i_extra_isize == 0)
1916                 return 0;
1917         eamagic = IHDR(inode);
1918
1919         start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
1920                         inode->i_extra_isize + sizeof(__u32);
1921         is->s.first = (struct ext2_ext_attr_entry *)start;
1922         is->s.base = start;
1923         is->s.here = is->s.first;
1924         is->s.end = (char *)inode + EXT2_INODE_SIZE(fs->super);
1925         if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
1926                 error = ext2fs_attr_check_names((struct ext2_ext_attr_entry *)
1927                                                 start, is->s.end);
1928                 if (error)
1929                         return error;
1930                 /* Find the named attribute. */
1931                 error = ext2fs_attr_find_entry(&is->s.here, i->name_index,
1932                                                i->name, is->s.end -
1933                                                (char *)is->s.base, 0);
1934                 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1935                         return error;
1936                 is->s.not_found = error;
1937         }
1938
1939         return 0;
1940 }
1941
1942 static errcode_t ext2fs_attr_set_entry(ext2_filsys fs, struct ext2_attr_info *i,
1943                                        struct ext2_attr_search *s)
1944 {
1945         struct ext2_ext_attr_entry *last;
1946         int free, min_offs = s->end - s->base, name_len = strlen(i->name);
1947
1948         /* Compute min_offs and last. */
1949         for (last = s->first; !EXT2_EXT_IS_LAST_ENTRY(last);
1950              last = EXT2_EXT_ATTR_NEXT(last)) {
1951                 if (!last->e_value_inum && last->e_value_size) {
1952                         int offs = last->e_value_offs;
1953
1954                         if (offs < min_offs)
1955                                 min_offs = offs;
1956                 }
1957         }
1958         free = min_offs - ((char *)last - s->base) - sizeof(__u32);
1959
1960         if (!s->not_found) {
1961                 if (!s->here->e_value_inum && s->here->e_value_size) {
1962                         int size = s->here->e_value_size;
1963                         free += EXT2_EXT_ATTR_SIZE(size);
1964                 }
1965                 free += EXT2_EXT_ATTR_LEN(name_len);
1966         }
1967         if (i->value) {
1968                 if (free < EXT2_EXT_ATTR_LEN(name_len) +
1969                            EXT2_EXT_ATTR_SIZE(i->value_len))
1970                         return EXT2_ET_EA_NO_SPACE;
1971         }
1972
1973         if (i->value && s->not_found) {
1974                 /* Insert the new name. */
1975                 int size = EXT2_EXT_ATTR_LEN(name_len);
1976                 int rest = (char *)last - (char *)s->here + sizeof(__u32);
1977
1978                 memmove((char *)s->here + size, s->here, rest);
1979                 memset(s->here, 0, size);
1980                 s->here->e_name_index = i->name_index;
1981                 s->here->e_name_len = name_len;
1982                 memcpy(s->here->e_name, i->name, name_len);
1983         } else {
1984                 if (!s->here->e_value_inum && s->here->e_value_size) {
1985                         char *first_val = s->base + min_offs;
1986                         int offs = s->here->e_value_offs;
1987                         char *val = s->base + offs;
1988                         int size = EXT2_EXT_ATTR_SIZE(s->here->e_value_size);
1989
1990                         if (i->value &&
1991                             size == EXT2_EXT_ATTR_SIZE(i->value_len)) {
1992                                 /* The old and the new value have the same
1993                                    size. Just replace. */
1994                                 s->here->e_value_size = i->value_len;
1995                                 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
1996                                        EXT2_EXT_ATTR_PAD); /* Clear pad bytes */
1997                                 memcpy(val, i->value, i->value_len);
1998                                 return 0;
1999                         }
2000
2001                         /* Remove the old value. */
2002                         memmove(first_val + size, first_val, val - first_val);
2003                         memset(first_val, 0, size);
2004                         s->here->e_value_size = 0;
2005                         s->here->e_value_offs = 0;
2006                         min_offs += size;
2007
2008                         /* Adjust all value offsets. */
2009                         last = s->first;
2010                         while (!EXT2_EXT_IS_LAST_ENTRY(last)) {
2011                                 int o = last->e_value_offs;
2012
2013                                 if (!last->e_value_inum &&
2014                                     last->e_value_size && o < offs)
2015                                         last->e_value_offs = o + size;
2016                                 last = EXT2_EXT_ATTR_NEXT(last);
2017                         }
2018                 }
2019                 if (!i->value) {
2020                         /* Remove the old name. */
2021                         int size = EXT2_EXT_ATTR_LEN(name_len);
2022
2023                         last = ENTRY((char *)last - size);
2024                         memmove((char *)s->here, (char *)s->here + size,
2025                                 (char *)last - (char *)s->here + sizeof(__u32));
2026                         memset(last, 0, size);
2027                 }
2028         }
2029
2030         if (i->value) {
2031                 /* Insert the new value. */
2032                 s->here->e_value_size = i->value_len;
2033                 if (i->value_len) {
2034                         int size = EXT2_EXT_ATTR_SIZE(i->value_len);
2035                         char *val = s->base + min_offs - size;
2036
2037                         s->here->e_value_offs = min_offs - size;
2038                         memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2039                                EXT2_EXT_ATTR_PAD); /* Clear the pad bytes. */
2040                         memcpy(val, i->value, i->value_len);
2041                 }
2042         }
2043
2044         return 0;
2045 }
2046
2047 static errcode_t ext2fs_attr_block_set(ext2_filsys fs, struct ext2_inode *inode,
2048                                        struct ext2_attr_info *i,
2049                                        struct ext2_attr_block_find *bs)
2050 {
2051         struct ext2_attr_search *s = &bs->s;
2052         char *new_buf = NULL, *old_block = NULL;
2053         blk_t blk;
2054         int clear_flag = 0;
2055         errcode_t error;
2056
2057         if (i->value && i->value_len > fs->blocksize)
2058                 return EXT2_ET_EA_NO_SPACE;
2059
2060         if (s->base) {
2061                 if (BHDR(s->base)->h_refcount != 1) {
2062                         int offset = (char *)s->here - bs->block;
2063
2064                         /* Decrement the refcount of the shared block */
2065                         old_block = s->base;
2066                         BHDR(s->base)->h_refcount -= 1;
2067
2068                         error = ext2fs_get_mem(fs->blocksize, &s->base);
2069                         if (error)
2070                                 goto cleanup;
2071                         clear_flag = 1;
2072                         memcpy(s->base, bs->block, fs->blocksize);
2073                         s->first = ENTRY(BHDR(s->base)+1);
2074                         BHDR(s->base)->h_refcount = 1;
2075                         s->here = ENTRY(s->base + offset);
2076                         s->end = s->base + fs->blocksize;
2077                 }
2078         } else {
2079                 error = ext2fs_get_mem(fs->blocksize, &s->base);
2080                 if (error)
2081                         goto cleanup;
2082                 clear_flag = 1;
2083                 memset(s->base, 0, fs->blocksize);
2084                 BHDR(s->base)->h_magic = EXT2_EXT_ATTR_MAGIC;
2085                 BHDR(s->base)->h_blocks = 1;
2086                 BHDR(s->base)->h_refcount = 1;
2087                 s->first = ENTRY(BHDR(s->base)+1);
2088                 s->here = ENTRY(BHDR(s->base)+1);
2089                 s->end = s->base + fs->blocksize;
2090         }
2091
2092         error = ext2fs_attr_set_entry(fs, i, s);
2093         if (error)
2094                 goto cleanup;
2095
2096         if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2097                 ext2fs_attr_rehash(BHDR(s->base), s->here);
2098
2099         if (!EXT2_EXT_IS_LAST_ENTRY(s->first)) {
2100                 if (bs->block && bs->block == s->base) {
2101                         /* We are modifying this block in-place */
2102                         new_buf = bs->block;
2103                         blk = inode->i_file_acl;
2104                         error = ext2fs_write_ext_attr(fs, blk, s->base);
2105                         if (error)
2106                                 goto cleanup;
2107                 } else {
2108                         /* We need to allocate a new block */
2109                         error = ext2fs_new_block(fs, 0, 0, &blk);
2110                         if (error)
2111                                 goto cleanup;
2112                         ext2fs_block_alloc_stats(fs, blk, 1);
2113                         error = ext2fs_write_ext_attr(fs, blk, s->base);
2114                         if (error)
2115                                 goto cleanup;
2116                         new_buf = s->base;
2117                         if (old_block) {
2118                                 BHDR(s->base)->h_refcount -= 1;
2119                                 error = ext2fs_write_ext_attr(fs,
2120                                                               inode->i_file_acl,
2121                                                               s->base);
2122                                 if (error)
2123                                         goto cleanup;
2124                         }
2125                 }
2126         }
2127
2128         /* Update the i_blocks if we added a new EA block */
2129         if (!inode->i_file_acl && new_buf)
2130                 inode->i_blocks += fs->blocksize / 512;
2131         /* Update the inode. */
2132         inode->i_file_acl = new_buf ? blk : 0;
2133
2134 cleanup:
2135         if (clear_flag)
2136                 ext2fs_free_mem(&s->base);
2137         return 0;
2138 }
2139
2140 static errcode_t ext2fs_attr_ibody_set(ext2_filsys fs,
2141                                        struct ext2_inode_large *inode,
2142                                        struct ext2_attr_info *i,
2143                                        struct ext2_attr_ibody_find *is)
2144 {
2145         __u32 *eamagic;
2146         struct ext2_attr_search *s = &is->s;
2147         errcode_t error;
2148
2149         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2150                 return EXT2_ET_EA_NO_SPACE;
2151
2152         error = ext2fs_attr_set_entry(fs, i, s);
2153         if (error)
2154                 return error;
2155
2156         eamagic = IHDR(inode);
2157         if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2158                 *eamagic = EXT2_EXT_ATTR_MAGIC;
2159         else
2160                 *eamagic = 0;
2161
2162         return ext2fs_write_inode_full(fs, is->ino, (struct ext2_inode *)inode,
2163                                        EXT2_INODE_SIZE(fs->super));
2164 }
2165
2166 static struct {
2167         char str[28];
2168         int len;
2169 } ext2_attr_index_prefix[] = {
2170         [EXT2_ATTR_INDEX_USER] = { EXT2_ATTR_INDEX_USER_PREFIX,
2171                                    sizeof(EXT2_ATTR_INDEX_USER_PREFIX) },
2172         [EXT2_ATTR_INDEX_POSIX_ACL_ACCESS] = {
2173                 EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX,
2174                 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX) },
2175         [EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT] = {
2176                 EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX,
2177                 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX) },
2178         [EXT2_ATTR_INDEX_TRUSTED] = { EXT2_ATTR_INDEX_TRUSTED_PREFIX,
2179                                       sizeof(EXT2_ATTR_INDEX_TRUSTED_PREFIX) },
2180         [EXT2_ATTR_INDEX_LUSTRE] = { EXT2_ATTR_INDEX_LUSTRE_PREFIX,
2181                                      sizeof(EXT2_ATTR_INDEX_LUSTRE_PREFIX) },
2182         [EXT2_ATTR_INDEX_SECURITY] = { EXT2_ATTR_INDEX_SECURITY_PREFIX,
2183                                        sizeof(EXT2_ATTR_INDEX_SECURITY_PREFIX)},
2184         { "", 0 }
2185 };
2186
2187 errcode_t ext2fs_attr_set(ext2_filsys fs, ext2_ino_t ino,
2188                           struct ext2_inode *inode,
2189                           int name_index, const char *name, const char *value,
2190                           int value_len, int flags)
2191 {
2192         struct ext2_inode_large *inode_large = NULL;
2193         struct ext2_attr_info i = {
2194                 .name_index = name_index,
2195                 .name = name,
2196                 .value = value,
2197                 .value_len = value_len,
2198         };
2199         struct ext2_attr_ibody_find is = {
2200                 .ino = ino,
2201                 .s = { .not_found = -ENODATA, },
2202         };
2203         struct ext2_attr_block_find bs = {
2204                 .s = { .not_found = -ENODATA, },
2205         };
2206         errcode_t error;
2207
2208         if (!name)
2209                 return EXT2_ET_EA_BAD_NAME;
2210         if (strlen(name) > 255)
2211                 return EXT2_ET_EA_NAME_TOO_BIG;
2212
2213         /* If the prefix is still present, skip it */
2214         if (strncmp(name, ext2_attr_index_prefix[name_index].str,
2215                     ext2_attr_index_prefix[name_index].len) == 0)
2216                 i.name += ext2_attr_index_prefix[name_index].len;
2217
2218         if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
2219                 inode_large = (struct ext2_inode_large *)inode;
2220
2221                 error = ext2fs_attr_ibody_find(fs, inode_large, &i, &is);
2222                 if (error)
2223                         goto cleanup;
2224         }
2225         if (is.s.not_found) {
2226                 error = ext2fs_attr_block_find(fs, inode, &i, &bs);
2227                 if (error)
2228                         goto cleanup;
2229         }
2230
2231         if (is.s.not_found && bs.s.not_found) {
2232                 error = EXT2_ET_EA_NAME_NOT_FOUND;
2233                 if (flags & XATTR_REPLACE)
2234                         goto cleanup;
2235                 error = 0;
2236                 if (!value)
2237                         goto cleanup;
2238         } else {
2239                 error = EXT2_ET_EA_NAME_EXISTS;
2240                 if (flags & XATTR_CREATE)
2241                         goto cleanup;
2242         }
2243
2244         if (!value) {
2245                 if (!is.s.not_found &&
2246                     (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE))
2247                         error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2248                 else if (!bs.s.not_found)
2249                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2250         } else {
2251                 if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
2252                         error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2253                 if (!error && !bs.s.not_found) {
2254                         i.value = NULL;
2255                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2256                 } else if (error == EXT2_ET_EA_NO_SPACE) {
2257                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2258                         if (error)
2259                                 goto cleanup;
2260                         if (!is.s.not_found) {
2261                                 i.value = NULL;
2262                                 if (EXT2_INODE_SIZE(fs->super) >
2263                                     EXT2_GOOD_OLD_INODE_SIZE)
2264                                         error = ext2fs_attr_ibody_set(fs,
2265                                                         inode_large, &i, &is);
2266                         }
2267                 }
2268         }
2269
2270 cleanup:
2271         return error;
2272 }
2273
2274 static errcode_t ext2fs_attr_check_block(ext2_filsys fs, char *buffer)
2275 {
2276         if (BHDR(buffer)->h_magic != (EXT2_EXT_ATTR_MAGIC) ||
2277             BHDR(buffer)->h_blocks != 1)
2278                 return EXT2_ET_EA_BAD_MAGIC;
2279
2280         return ext2fs_attr_check_names((struct ext2_ext_attr_entry *)
2281                                        (BHDR(buffer) + 1),
2282                                        buffer + fs->blocksize);
2283 }
2284
2285 static errcode_t ext2fs_attr_block_get(ext2_filsys fs, struct ext2_inode *inode,
2286                                        int name_index, const char *name,
2287                                        void *buffer, size_t buffer_size,
2288                                        int *easize)
2289 {
2290         struct ext2_ext_attr_header *header = NULL;
2291         struct ext2_ext_attr_entry *entry;
2292         char *block_buf = NULL;
2293         errcode_t error;
2294
2295         error = EXT2_ET_EA_NAME_NOT_FOUND;
2296         if (!inode->i_file_acl)
2297                 goto cleanup;
2298
2299         error = ext2fs_get_mem(fs->blocksize, &block_buf);
2300         if (error)
2301                 return error;
2302         error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2303         if (error)
2304                 goto cleanup;
2305
2306         error = ext2fs_attr_check_block(fs, block_buf);
2307         if (error)
2308                 goto cleanup;
2309
2310         header = BHDR(block_buf);
2311         entry = (struct ext2_ext_attr_entry *)(header + 1);
2312         error = ext2fs_attr_find_entry(&entry, name_index, name,
2313                                        fs->blocksize, 1);
2314         if (error)
2315                 goto cleanup;
2316         if (easize)
2317                 *easize = entry->e_value_size;
2318         if (buffer) {
2319                 if (entry->e_value_size > buffer_size) {
2320                         error = EXT2_ET_EA_TOO_BIG;
2321                         goto cleanup;
2322                 }
2323                 memcpy(buffer, block_buf + entry->e_value_offs,
2324                        entry->e_value_size);
2325         }
2326
2327 cleanup:
2328         if (block_buf)
2329                 ext2fs_free_mem(&block_buf);
2330         return error;
2331 }
2332
2333 static errcode_t ext2fs_attr_ibody_get(ext2_filsys fs,
2334                                        struct ext2_inode_large *inode,
2335                                        int name_index, const char *name,
2336                                        void *buffer, size_t buffer_size,
2337                                        int *easize)
2338 {
2339         struct ext2_ext_attr_entry *entry;
2340         int error;
2341         char *end, *start;
2342         __u32 *eamagic;
2343
2344         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2345                 return EXT2_ET_EA_NAME_NOT_FOUND;
2346
2347         eamagic = IHDR(inode);
2348         error = ext2fs_attr_check_block(fs, buffer);
2349         if (error)
2350                 return error;
2351
2352         start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2353                         inode->i_extra_isize + sizeof(__u32);
2354         entry = (struct ext2_ext_attr_entry *)start;
2355         end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2356         error = ext2fs_attr_check_names(entry, end);
2357         if (error)
2358                 goto cleanup;
2359         error = ext2fs_attr_find_entry(&entry, name_index, name,
2360                                        end - (char *)entry, 0);
2361         if (error)
2362                 goto cleanup;
2363         if (easize)
2364                 *easize = entry->e_value_size;
2365         if (buffer) {
2366                 if (entry->e_value_size > buffer_size) {
2367                         error = EXT2_ET_EA_TOO_BIG;
2368                         goto cleanup;
2369                 }
2370                 memcpy(buffer, start + entry->e_value_offs,entry->e_value_size);
2371         }
2372
2373 cleanup:
2374         return error;
2375 }
2376
2377
2378 errcode_t ext2fs_attr_get(ext2_filsys fs, struct ext2_inode *inode,
2379                           int name_index, const char *name, char *buffer,
2380                           size_t buffer_size, int *easize)
2381 {
2382         errcode_t error;
2383
2384         error = ext2fs_attr_ibody_get(fs, (struct ext2_inode_large *)inode,
2385                                       name_index, name, buffer, buffer_size,
2386                                       easize);
2387         if (error == EXT2_ET_EA_NAME_NOT_FOUND || error == EXT2_ET_EA_BAD_MAGIC)
2388                 error = ext2fs_attr_block_get(fs, inode, name_index, name,
2389                                               buffer, buffer_size, easize);
2390
2391         return error;
2392 }
2393
2394 int ext2fs_attr_get_next_attr(struct ext2_ext_attr_entry *entry, int name_index,
2395                               char *buffer, int buffer_size, int start)
2396 {
2397         const int prefix_len = ext2_attr_index_prefix[name_index].len;
2398         int total_len;
2399
2400         if (!start && !EXT2_EXT_IS_LAST_ENTRY(entry))
2401                 entry = EXT2_EXT_ATTR_NEXT(entry);
2402
2403         for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
2404              entry = EXT2_EXT_ATTR_NEXT(entry)) {
2405                 if (!name_index)
2406                         break;
2407                 if (name_index == entry->e_name_index)
2408                         break;
2409         }
2410         if (EXT2_EXT_IS_LAST_ENTRY(entry))
2411                 return 0;
2412
2413         total_len = prefix_len + entry->e_name_len + 1;
2414         if (buffer && total_len <= buffer_size) {
2415                 memcpy(buffer, ext2_attr_index_prefix[name_index].str,
2416                        prefix_len);
2417                 memcpy(buffer + prefix_len, entry->e_name, entry->e_name_len);
2418                 buffer[prefix_len + entry->e_name_len] = '\0';
2419         }
2420
2421         return total_len;
2422 }
2423
2424 errcode_t ext2fs_expand_extra_isize(ext2_filsys fs, ext2_ino_t ino,
2425                                     struct ext2_inode_large *inode,
2426                                     int new_extra_isize, int *ret,
2427                                     int *needed_size)
2428 {
2429         struct ext2_inode *inode_buf = NULL;
2430         __u32 *eamagic = NULL;
2431         struct ext2_ext_attr_header *header = NULL;
2432         struct ext2_ext_attr_entry *entry = NULL, *last = NULL;
2433         struct ext2_attr_ibody_find is = {
2434                 .ino = ino,
2435                 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2436         };
2437         struct ext2_attr_block_find bs = {
2438                 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2439         };
2440         char *start, *end, *block_buf = NULL, *buffer =NULL, *b_entry_name=NULL;
2441         int total_ino = 0, total_blk, free, offs, tried_min_extra_isize = 0;
2442         int s_min_extra_isize = fs->super->s_min_extra_isize;
2443         errcode_t error = 0;
2444
2445         if (needed_size)
2446                 *needed_size = new_extra_isize;
2447         error = ext2fs_get_mem(fs->blocksize, &block_buf);
2448         if (error)
2449                 return error;
2450
2451         if (inode == NULL) {
2452                 error = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode_buf);
2453                 if (error)
2454                         goto cleanup;
2455
2456                 error = ext2fs_read_inode_full(fs, ino, inode_buf,
2457                                                EXT2_INODE_SIZE(fs->super));
2458                 if (error)
2459                         goto cleanup;
2460
2461                 inode = (struct ext2_inode_large *)inode_buf;
2462         }
2463
2464 retry:
2465         if (inode->i_extra_isize >= new_extra_isize)
2466                 goto cleanup;
2467
2468         eamagic = IHDR(inode);
2469         start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2470         /* No extended attributes present */
2471         if (*eamagic != EXT2_EXT_ATTR_MAGIC) {
2472                 memset(start, 0,
2473                        EXT2_INODE_SIZE(fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
2474                        inode->i_extra_isize);
2475                 inode->i_extra_isize = new_extra_isize;
2476                 if (needed_size)
2477                         *needed_size = 0;
2478                 goto write_inode;
2479         }
2480
2481         start += sizeof(__u32);
2482         end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2483         last = entry = (struct ext2_ext_attr_entry *)start;
2484         offs = end - start;
2485         /* Consider space takenup by magic number */
2486         total_ino = sizeof(__u32);
2487         free = ext2fs_attr_free_space(last, &offs, start, &total_ino);
2488
2489         /* Enough free space available in the inode for expansion */
2490         if (free >= new_extra_isize) {
2491                 ext2fs_attr_shift_entries(entry,
2492                                         inode->i_extra_isize - new_extra_isize,
2493                                         (char *)inode +
2494                                         EXT2_GOOD_OLD_INODE_SIZE +
2495                                         new_extra_isize,
2496                                         start - sizeof(__u32), total_ino);
2497                 inode->i_extra_isize = new_extra_isize;
2498                 if (needed_size)
2499                         *needed_size = 0;
2500                 goto write_inode;
2501         }
2502
2503         if (inode->i_file_acl) {
2504                 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2505                 if (error)
2506                         goto cleanup;
2507
2508                 header = BHDR(block_buf);
2509                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
2510                         error = EXT2_ET_EA_BAD_MAGIC;
2511                         goto cleanup;
2512                 }
2513                 end = block_buf + fs->blocksize;
2514                 last = entry = (struct ext2_ext_attr_entry *)(header+1);
2515                 start = (char *)entry;
2516                 offs = end - start;
2517                 free = ext2fs_attr_free_space(last, &offs, start, &total_blk);
2518                 if (free < new_extra_isize) {
2519                         if (!tried_min_extra_isize && s_min_extra_isize) {
2520                                 tried_min_extra_isize++;
2521                                 new_extra_isize = s_min_extra_isize;
2522                                 goto retry;
2523                         }
2524                         if (ret)
2525                                 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2526                         error = EXT2_ET_EA_NO_SPACE;
2527                         goto cleanup;
2528                 }
2529         } else {
2530                 if (ret && *ret == EXT2_EXPAND_EISIZE_UNSAFE) {
2531                         *ret = EXT2_EXPAND_EISIZE_NEW_BLOCK;
2532                         error = 0;
2533                         goto cleanup;
2534                 }
2535                 free = fs->blocksize;
2536         }
2537
2538         while (new_extra_isize > 0) {
2539                 int offs, size, entry_size;
2540                 struct ext2_ext_attr_entry *small_entry = NULL;
2541                 struct ext2_attr_info i = {
2542                         .value = NULL,
2543                         .value_len = 0,
2544                 };
2545                 unsigned int total_size, shift_bytes, temp = ~0U, extra_isize=0;
2546
2547                 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2548                                 inode->i_extra_isize + sizeof(__u32);
2549                 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2550                 last = (struct ext2_ext_attr_entry *)start;
2551
2552                 /* Find the entry best suited to be pushed into EA block */
2553                 entry = NULL;
2554                 for (; !EXT2_EXT_IS_LAST_ENTRY(last);
2555                         last = EXT2_EXT_ATTR_NEXT(last)) {
2556                         total_size = EXT2_EXT_ATTR_SIZE(last->e_value_size) +
2557                                         EXT2_EXT_ATTR_LEN(last->e_name_len);
2558                         if (total_size <= free && total_size < temp) {
2559                                 if (total_size < new_extra_isize) {
2560                                         small_entry = last;
2561                                 } else {
2562                                         entry = last;
2563                                         temp = total_size;
2564                                 }
2565                         }
2566                 }
2567
2568                 if (entry == NULL) {
2569                         if (small_entry) {
2570                                 entry = small_entry;
2571                         } else {
2572                                 if (!tried_min_extra_isize &&
2573                                     s_min_extra_isize) {
2574                                         tried_min_extra_isize++;
2575                                         new_extra_isize = s_min_extra_isize;
2576                                         goto retry;
2577                                 }
2578                                 if (ret)
2579                                         *ret = EXT2_EXPAND_EISIZE_NOSPC;
2580                                 error = EXT2_ET_EA_NO_SPACE;
2581                                 goto cleanup;
2582                         }
2583                 }
2584                 offs = entry->e_value_offs;
2585                 size = entry->e_value_size;
2586                 entry_size = EXT2_EXT_ATTR_LEN(entry->e_name_len);
2587                 i.name_index = entry->e_name_index;
2588                 error = ext2fs_get_mem(EXT2_EXT_ATTR_SIZE(size), &buffer);
2589                 if (error)
2590                         goto cleanup;
2591                 error = ext2fs_get_mem(entry->e_name_len + 1, &b_entry_name);
2592                 if (error)
2593                         goto cleanup;
2594                 /* Save the entry name and the entry value */
2595                 memcpy((char *)buffer, (char *)start + offs,
2596                        EXT2_EXT_ATTR_SIZE(size));
2597                 memcpy((char *)b_entry_name, (char *)entry->e_name,
2598                        entry->e_name_len);
2599                 b_entry_name[entry->e_name_len] = '\0';
2600                 i.name = b_entry_name;
2601
2602                 error = ext2fs_attr_ibody_find(fs, inode, &i, &is);
2603                 if (error)
2604                         goto cleanup;
2605
2606                 error = ext2fs_attr_set_entry(fs, &i, &is.s);
2607                 if (error)
2608                         goto cleanup;
2609
2610                 entry = (struct ext2_ext_attr_entry *)start;
2611                 if (entry_size + EXT2_EXT_ATTR_SIZE(size) >= new_extra_isize)
2612                         shift_bytes = new_extra_isize;
2613                 else
2614                         shift_bytes = entry_size + EXT2_EXT_ATTR_SIZE(size);
2615                 ext2fs_attr_shift_entries(entry,
2616                                         inode->i_extra_isize - shift_bytes,
2617                                         (char *)inode +EXT2_GOOD_OLD_INODE_SIZE+
2618                                         extra_isize + shift_bytes,
2619                                         start - sizeof(__u32),
2620                                         total_ino - entry_size);
2621
2622                 extra_isize += shift_bytes;
2623                 new_extra_isize -= shift_bytes;
2624                 if (needed_size)
2625                         *needed_size = new_extra_isize;
2626                 inode->i_extra_isize = extra_isize;
2627
2628                 i.name = b_entry_name;
2629                 i.value = buffer;
2630                 i.value_len = size;
2631                 error = ext2fs_attr_block_find(fs, (struct ext2_inode *)inode,
2632                                                &i, &bs);
2633                 if (error)
2634                         goto cleanup;
2635
2636                 /* Add entry which was removed from the inode into the block */
2637                 error = ext2fs_attr_block_set(fs, (struct ext2_inode *)inode,
2638                                               &i, &bs);
2639                 if (error)
2640                         goto cleanup;
2641         }
2642
2643 write_inode:
2644         error = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)inode,
2645                                         EXT2_INODE_SIZE(fs->super));
2646 cleanup:
2647         if (inode_buf)
2648                 ext2fs_free_mem(&inode_buf);
2649         if (block_buf)
2650                 ext2fs_free_mem(&block_buf);
2651         if (buffer)
2652                 ext2fs_free_mem(&buffer);
2653         if (b_entry_name)
2654                 ext2fs_free_mem(&b_entry_name);
2655
2656         return error;
2657 }