Whamcloud - gitweb
e2fsck: clean up xattr checking code
[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         int name_index;
333         char *name;
334         char *short_name;
335         void *value;
336         unsigned int value_len;
337         ext2_ino_t ea_ino;
338 };
339
340 struct ext2_xattr_handle {
341         errcode_t magic;
342         ext2_filsys fs;
343         struct ext2_xattr *attrs;
344         int capacity;
345         int count;
346         int ibody_count;
347         ext2_ino_t ino;
348         unsigned int flags;
349 };
350
351 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
352                                       unsigned int expandby)
353 {
354         struct ext2_xattr *new_attrs;
355         errcode_t err;
356
357         err = ext2fs_get_arrayzero(h->capacity + expandby,
358                                    sizeof(struct ext2_xattr), &new_attrs);
359         if (err)
360                 return err;
361
362         memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
363         ext2fs_free_mem(&h->attrs);
364         h->capacity += expandby;
365         h->attrs = new_attrs;
366
367         return 0;
368 }
369
370 struct ea_name_index {
371         int index;
372         const char *name;
373 };
374
375 /* Keep these names sorted in order of decreasing specificity. */
376 static struct ea_name_index ea_names[] = {
377         {10, "gnu."},
378         {3, "system.posix_acl_default"},
379         {2, "system.posix_acl_access"},
380         {8, "system.richacl"},
381         {6, "security."},
382         {4, "trusted."},
383         {7, "system."},
384         {1, "user."},
385         {0, NULL},
386 };
387
388 static const char *find_ea_prefix(int index)
389 {
390         struct ea_name_index *e;
391
392         for (e = ea_names; e->name; e++)
393                 if (e->index == index)
394                         return e->name;
395
396         return NULL;
397 }
398
399 static int find_ea_index(const char *fullname, const char **name, int *index)
400 {
401         struct ea_name_index *e;
402
403         for (e = ea_names; e->name; e++) {
404                 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
405                         *name = fullname + strlen(e->name);
406                         *index = e->index;
407                         return 1;
408                 }
409         }
410         return 0;
411 }
412
413 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
414                                struct ext2_inode_large *inode)
415 {
416         struct ext2_ext_attr_header *header;
417         void *block_buf = NULL;
418         blk64_t blk;
419         errcode_t err;
420         struct ext2_inode_large i;
421
422         /* Read inode? */
423         if (inode == NULL) {
424                 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
425                                              sizeof(struct ext2_inode_large));
426                 if (err)
427                         return err;
428                 inode = &i;
429         }
430
431         /* Do we already have an EA block? */
432         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
433         if (blk == 0)
434                 return 0;
435
436         /* Find block, zero it, write back */
437         if ((blk < fs->super->s_first_data_block) ||
438             (blk >= ext2fs_blocks_count(fs->super))) {
439                 err = EXT2_ET_BAD_EA_BLOCK_NUM;
440                 goto out;
441         }
442
443         err = ext2fs_get_mem(fs->blocksize, &block_buf);
444         if (err)
445                 goto out;
446
447         err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
448         if (err)
449                 goto out2;
450
451         /* We only know how to deal with v2 EA blocks */
452         header = (struct ext2_ext_attr_header *) block_buf;
453         if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
454                 err = EXT2_ET_BAD_EA_HEADER;
455                 goto out2;
456         }
457
458         header->h_refcount--;
459         err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
460         if (err)
461                 goto out2;
462
463         /* Erase link to block */
464         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
465         if (header->h_refcount == 0)
466                 ext2fs_block_alloc_stats2(fs, blk, -1);
467         err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
468         if (err)
469                 goto out2;
470
471         /* Write inode? */
472         if (inode == &i) {
473                 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
474                                               sizeof(struct ext2_inode_large));
475                 if (err)
476                         goto out2;
477         }
478
479 out2:
480         ext2fs_free_mem(&block_buf);
481 out:
482         return err;
483 }
484
485 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
486                                          struct ext2_inode_large *inode)
487 {
488         struct ext2_ext_attr_header *header;
489         void *block_buf = NULL;
490         blk64_t blk, goal;
491         errcode_t err;
492
493         /* Do we already have an EA block? */
494         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
495         if (blk != 0) {
496                 if ((blk < fs->super->s_first_data_block) ||
497                     (blk >= ext2fs_blocks_count(fs->super))) {
498                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
499                         goto out;
500                 }
501
502                 err = ext2fs_get_mem(fs->blocksize, &block_buf);
503                 if (err)
504                         goto out;
505
506                 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
507                 if (err)
508                         goto out2;
509
510                 /* We only know how to deal with v2 EA blocks */
511                 header = (struct ext2_ext_attr_header *) block_buf;
512                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
513                         err = EXT2_ET_BAD_EA_HEADER;
514                         goto out2;
515                 }
516
517                 /* Single-user block.  We're done here. */
518                 if (header->h_refcount == 1)
519                         goto out2;
520
521                 /* We need to CoW the block. */
522                 header->h_refcount--;
523                 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
524                 if (err)
525                         goto out2;
526         } else {
527                 /* No block, we must increment i_blocks */
528                 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
529                                              1);
530                 if (err)
531                         goto out;
532         }
533
534         /* Allocate a block */
535         goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
536         err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
537         if (err)
538                 goto out2;
539         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
540 out2:
541         if (block_buf)
542                 ext2fs_free_mem(&block_buf);
543 out:
544         return err;
545 }
546
547
548 static inline int
549 posix_acl_xattr_count(size_t size)
550 {
551         if (size < sizeof(posix_acl_xattr_header))
552                 return -1;
553         size -= sizeof(posix_acl_xattr_header);
554         if (size % sizeof(posix_acl_xattr_entry))
555                 return -1;
556         return size / sizeof(posix_acl_xattr_entry);
557 }
558
559 /*
560  * The lgetxattr function returns data formatted in the POSIX extended
561  * attribute format.  The on-disk format uses a more compact encoding.
562  * See the ext4_acl_to_disk in fs/ext4/acl.c.
563  */
564 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
565                                                   void *out_buf, size_t *size_out)
566 {
567         const posix_acl_xattr_header *header =
568                 (const posix_acl_xattr_header*) value;
569         const posix_acl_xattr_entry *end, *entry =
570                 (const posix_acl_xattr_entry *)(header+1);
571         ext4_acl_header *ext_acl;
572         size_t s;
573         char *e;
574
575         int count;
576
577         if (!value)
578                 return EINVAL;
579         if (size < sizeof(posix_acl_xattr_header))
580                 return ENOMEM;
581         if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
582                 return EINVAL;
583
584         count = posix_acl_xattr_count(size);
585         ext_acl = out_buf;
586         ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
587
588         if (count <= 0)
589                 return EINVAL;
590
591         e = (char *) out_buf + sizeof(ext4_acl_header);
592         s = sizeof(ext4_acl_header);
593         for (end = entry + count; entry != end;entry++) {
594                 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
595                 disk_entry->e_tag = entry->e_tag;
596                 disk_entry->e_perm = entry->e_perm;
597
598                 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
599                         case ACL_USER_OBJ:
600                         case ACL_GROUP_OBJ:
601                         case ACL_MASK:
602                         case ACL_OTHER:
603                                 e += sizeof(ext4_acl_entry_short);
604                                 s += sizeof(ext4_acl_entry_short);
605                                 break;
606                         case ACL_USER:
607                         case ACL_GROUP:
608                                 disk_entry->e_id = entry->e_id;
609                                 e += sizeof(ext4_acl_entry);
610                                 s += sizeof(ext4_acl_entry);
611                                 break;
612                         default:
613                                 return EINVAL;
614                 }
615         }
616         *size_out = s;
617         return 0;
618 }
619
620 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
621                                                   void **out_buf, size_t *size_out)
622 {
623         posix_acl_xattr_header *header;
624         posix_acl_xattr_entry *entry;
625         const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
626         errcode_t err;
627         const char *cp;
628         char *out;
629
630         if ((!value) ||
631             (size < sizeof(ext4_acl_header)) ||
632             (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
633                 return EINVAL;
634
635         err = ext2fs_get_mem(size * 2, &out);
636         if (err)
637                 return err;
638
639         header = (posix_acl_xattr_header *) out;
640         header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
641         entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
642
643         cp = (const char *) value + sizeof(ext4_acl_header);
644         size -= sizeof(ext4_acl_header);
645
646         while (size > 0) {
647                 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
648
649                 entry->e_tag = disk_entry->e_tag;
650                 entry->e_perm = disk_entry->e_perm;
651
652                 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
653                         case ACL_USER_OBJ:
654                         case ACL_GROUP_OBJ:
655                         case ACL_MASK:
656                         case ACL_OTHER:
657                                 entry->e_id = 0;
658                                 cp += sizeof(ext4_acl_entry_short);
659                                 size -= sizeof(ext4_acl_entry_short);
660                                 break;
661                         case ACL_USER:
662                         case ACL_GROUP:
663                                 entry->e_id = disk_entry->e_id;
664                                 cp += sizeof(ext4_acl_entry);
665                                 size -= sizeof(ext4_acl_entry);
666                                 break;
667                         default:
668                                 ext2fs_free_mem(&out);
669                                 return EINVAL;
670                 }
671                 entry++;
672         }
673         *out_buf = out;
674         *size_out = ((char *) entry - out);
675         return 0;
676 }
677
678 static errcode_t
679 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
680                        void *entries_start, unsigned int storage_size,
681                        unsigned int value_offset_correction, int write_hash)
682 {
683         struct ext2_xattr *x;
684         struct ext2_ext_attr_entry *e = entries_start;
685         char *end = (char *) entries_start + storage_size;
686         unsigned int value_size;
687         errcode_t err;
688
689         memset(entries_start, 0, storage_size);
690         for (x = attrs; x < attrs + count; x++) {
691                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
692                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
693
694                 /* Fill out e appropriately */
695                 e->e_name_len = strlen(x->short_name);
696                 e->e_name_index = x->name_index;
697
698                 e->e_value_size = x->value_len;
699                 e->e_value_inum = x->ea_ino;
700
701                 /* Store name */
702                 memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
703                 if (x->ea_ino) {
704                         e->e_value_offs = 0;
705                 } else {
706                         end -= value_size;
707                         e->e_value_offs = end - (char *) entries_start +
708                                                 value_offset_correction;
709                         memcpy(end, x->value, e->e_value_size);
710                 }
711
712                 if (write_hash || x->ea_ino) {
713                         err = ext2fs_ext_attr_hash_entry2(fs, e,
714                                                           x->ea_ino ? 0 : end,
715                                                           &e->e_hash);
716                         if (err)
717                                 return err;
718                 } else
719                         e->e_hash = 0;
720
721                 e = EXT2_EXT_ATTR_NEXT(e);
722                 *(__u32 *)e = 0;
723         }
724         return 0;
725 }
726
727 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
728 {
729         ext2_filsys fs = handle->fs;
730         const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
731         struct ext2_inode_large *inode;
732         char *start, *block_buf = NULL;
733         struct ext2_ext_attr_header *header;
734         __u32 ea_inode_magic;
735         blk64_t blk;
736         unsigned int storage_size;
737         unsigned int i;
738         errcode_t err;
739
740         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
741         i = inode_size;
742         if (i < sizeof(*inode))
743                 i = sizeof(*inode);
744         err = ext2fs_get_memzero(i, &inode);
745         if (err)
746                 return err;
747
748         err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
749                                      inode_size);
750         if (err)
751                 goto out;
752
753         /* If extra_isize isn't set, we need to set it now */
754         if (inode->i_extra_isize == 0 &&
755             inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
756                 char *p = (char *)inode;
757                 size_t extra = fs->super->s_want_extra_isize;
758
759                 if (extra == 0)
760                         extra = sizeof(__u32);
761                 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
762                 inode->i_extra_isize = extra;
763         }
764         if (inode->i_extra_isize & 3) {
765                 err = EXT2_ET_INODE_CORRUPTED;
766                 goto out;
767         }
768
769         /* Does the inode have space for EA? */
770         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
771             inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
772                                                                 sizeof(__u32))
773                 goto write_ea_block;
774
775         /* Write the inode EA */
776         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
777         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
778                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
779         storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
780                                 inode->i_extra_isize - sizeof(__u32);
781         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
782                                 inode->i_extra_isize + sizeof(__u32);
783
784         err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
785                                      start, storage_size, 0, 0);
786         if (err)
787                 goto out;
788 write_ea_block:
789         /* Are we done? */
790         if (handle->ibody_count == handle->count &&
791             !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
792                 goto skip_ea_block;
793
794         /* Write the EA block */
795         err = ext2fs_get_memzero(fs->blocksize, &block_buf);
796         if (err)
797                 goto out;
798
799         storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
800         start = block_buf + sizeof(struct ext2_ext_attr_header);
801
802         err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
803                                      handle->count - handle->ibody_count, start,
804                                      storage_size, start - block_buf, 1);
805         if (err)
806                 goto out2;
807
808         /* Write a header on the EA block */
809         header = (struct ext2_ext_attr_header *) block_buf;
810         header->h_magic = EXT2_EXT_ATTR_MAGIC;
811         header->h_refcount = 1;
812         header->h_blocks = 1;
813
814         /* Get a new block for writing */
815         err = prep_ea_block_for_write(fs, handle->ino, inode);
816         if (err)
817                 goto out2;
818
819         /* Finally, write the new EA block */
820         blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
821         err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
822         if (err)
823                 goto out2;
824
825 skip_ea_block:
826         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
827         if (!block_buf && blk) {
828                 /* xattrs shrunk, free the block */
829                 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
830                 if (err)
831                         goto out;
832         }
833
834         /* Write the inode */
835         err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
836                                       inode_size);
837         if (err)
838                 goto out2;
839
840 out2:
841         ext2fs_free_mem(&block_buf);
842 out:
843         ext2fs_free_mem(&inode);
844         return err;
845 }
846
847 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
848                                          struct ext2_inode_large *inode,
849                                          struct ext2_ext_attr_entry *entries,
850                                          unsigned int storage_size,
851                                          char *value_start)
852 {
853         struct ext2_xattr *x;
854         struct ext2_ext_attr_entry *entry, *end;
855         const char *prefix;
856         unsigned int remain, prefix_len;
857         errcode_t err;
858         unsigned int values_size = storage_size +
859                         ((char *)entries - value_start);
860
861         /* find the end */
862         end = entries;
863         remain = storage_size;
864         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
865                !EXT2_EXT_IS_LAST_ENTRY(end)) {
866
867                 /* header eats this space */
868                 remain -= sizeof(struct ext2_ext_attr_entry);
869
870                 /* is attribute name valid? */
871                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
872                         return EXT2_ET_EA_BAD_NAME_LEN;
873
874                 /* attribute len eats this space */
875                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
876                 end = EXT2_EXT_ATTR_NEXT(end);
877         }
878
879         entry = entries;
880         remain = storage_size;
881         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
882                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
883
884                 /* Allocate space for more attrs? */
885                 if (handle->count == handle->capacity) {
886                         err = ext2fs_xattrs_expand(handle, 4);
887                         if (err)
888                                 return err;
889                 }
890
891                 x = handle->attrs + handle->count;
892
893                 /* header eats this space */
894                 remain -= sizeof(struct ext2_ext_attr_entry);
895
896                 /* attribute len eats this space */
897                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
898
899                 /* Extract name */
900                 prefix = find_ea_prefix(entry->e_name_index);
901                 prefix_len = (prefix ? strlen(prefix) : 0);
902                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
903                                          &x->name);
904                 if (err)
905                         return err;
906                 if (prefix)
907                         memcpy(x->name, prefix, prefix_len);
908                 if (entry->e_name_len)
909                         memcpy(x->name + prefix_len,
910                                (char *)entry + sizeof(*entry),
911                                entry->e_name_len);
912                 x->short_name = x->name + prefix_len;
913                 x->name_index = entry->e_name_index;
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 ((__u64) 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 char *short_name,
1342                                     int index, const void *value,
1343                                     size_t value_len, int in_inode)
1344 {
1345         ext2_ino_t ea_ino = 0;
1346         void *new_value = NULL;
1347         char *new_name = NULL;
1348         int name_len;
1349         errcode_t ret;
1350
1351         if (!x->name) {
1352                 name_len = strlen(name);
1353                 ret = ext2fs_get_mem(name_len + 1, &new_name);
1354                 if (ret)
1355                         goto fail;
1356                 memcpy(new_name, name, name_len + 1);
1357         }
1358
1359         ret = ext2fs_get_mem(value_len, &new_value);
1360         if (ret)
1361                 goto fail;
1362         memcpy(new_value, value, value_len);
1363
1364         if (in_inode) {
1365                 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1366                 if (ret)
1367                         goto fail;
1368         }
1369
1370         if (x->ea_ino) {
1371                 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1372                 if (ret)
1373                         goto fail;
1374         }
1375
1376         if (!x->name) {
1377                 x->name = new_name;
1378                 x->short_name = new_name + (short_name  - name);
1379         }
1380         x->name_index = index;
1381
1382         if (x->value)
1383                 ext2fs_free_mem(&x->value);
1384         x->value = new_value;
1385         x->value_len = value_len;
1386         x->ea_ino = ea_ino;
1387         return 0;
1388 fail:
1389         if (new_name)
1390                 ext2fs_free_mem(&new_name);
1391         if (new_value)
1392                 ext2fs_free_mem(&new_value);
1393         if (ea_ino)
1394                 xattr_inode_dec_ref(fs, ea_ino);
1395         return ret;
1396 }
1397
1398 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1399                                const char *shortname, int name_idx)
1400 {
1401         struct ext2_xattr *x;
1402         int i;
1403         int shortname_len, x_shortname_len;
1404
1405         shortname_len = strlen(shortname);
1406
1407         for (i = 0, x = attrs; i < count; i++, x++) {
1408                 if (name_idx < x->name_index)
1409                         break;
1410                 if (name_idx > x->name_index)
1411                         continue;
1412
1413                 x_shortname_len = strlen(x->short_name);
1414                 if (shortname_len < x_shortname_len)
1415                         break;
1416                 if (shortname_len > x_shortname_len)
1417                         continue;
1418
1419                 if (memcmp(shortname, x->short_name, 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 = 0;
1435         const char *shortname = name;
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                                          shortname, name_idx, value,
1463                                          value_len, in_inode);
1464                 if (ret)
1465                         return ret;
1466                 if (h->ibody_count <= old_idx) {
1467                         /* Move entry from block to the end of ibody. */
1468                         tmp = h->attrs[old_idx];
1469                         memmove(h->attrs + h->ibody_count + 1,
1470                                 h->attrs + h->ibody_count,
1471                                 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1472                         h->attrs[h->ibody_count] = tmp;
1473                         h->ibody_count++;
1474                 }
1475                 return 0;
1476         }
1477
1478         if (h->ibody_count <= old_idx) {
1479                 block_free += EXT2_EXT_ATTR_LEN(name_len);
1480                 if (!h->attrs[old_idx].ea_ino)
1481                         block_free +=
1482                                 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1483         }
1484
1485         if (needed > block_free)
1486                 return EXT2_ET_EA_NO_SPACE;
1487
1488         if (old_idx >= 0) {
1489                 /* Update the existing entry. */
1490                 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1491                                          shortname, name_idx, value,
1492                                          value_len, in_inode);
1493                 if (ret)
1494                         return ret;
1495                 if (old_idx < h->ibody_count) {
1496                         /*
1497                          * Move entry from ibody to the block. Note that
1498                          * entries in the block are sorted.
1499                          */
1500                         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1501                                                       h->count - h->ibody_count,
1502                                                       shortname, name_idx);
1503                         new_idx += h->ibody_count - 1;
1504                         tmp = h->attrs[old_idx];
1505                         memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1506                                 (new_idx - old_idx) * sizeof(*h->attrs));
1507                         h->attrs[new_idx] = tmp;
1508                         h->ibody_count--;
1509                 }
1510                 return 0;
1511         }
1512
1513         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1514                                       h->count - h->ibody_count,
1515                                       shortname, name_idx);
1516         new_idx += h->ibody_count;
1517         add_to_ibody = 0;
1518
1519 add_new:
1520         if (h->count == h->capacity) {
1521                 ret = ext2fs_xattrs_expand(h, 4);
1522                 if (ret)
1523                         return ret;
1524         }
1525
1526         ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
1527                                  name_idx, value, value_len, in_inode);
1528         if (ret)
1529                 return ret;
1530
1531         tmp = h->attrs[h->count];
1532         memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1533                 (h->count - new_idx)*sizeof(*h->attrs));
1534         h->attrs[new_idx] = tmp;
1535         if (add_to_ibody)
1536                 h->ibody_count++;
1537         h->count++;
1538         return 0;
1539 }
1540
1541 static int space_used(struct ext2_xattr *attrs, int count)
1542 {
1543         int total = 0;
1544         struct ext2_xattr *x;
1545         int i, len;
1546
1547         for (i = 0, x = attrs; i < count; i++, x++) {
1548                 len = strlen(x->short_name);
1549                 total += EXT2_EXT_ATTR_LEN(len);
1550                 if (!x->ea_ino)
1551                         total += EXT2_EXT_ATTR_SIZE(x->value_len);
1552         }
1553         return total;
1554 }
1555
1556 /*
1557  * The minimum size of EA value when you start storing it in an external inode
1558  * size of block - size of header - size of 1 entry - 4 null bytes
1559  */
1560 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1561         ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1562
1563 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1564                            const char *name,
1565                            const void *value,
1566                            size_t value_len)
1567 {
1568         ext2_filsys fs = h->fs;
1569         const int inode_size = EXT2_INODE_SIZE(fs->super);
1570         struct ext2_inode_large *inode = NULL;
1571         struct ext2_xattr *x;
1572         char *new_value;
1573         int ibody_free, block_free;
1574         int in_inode = 0;
1575         int old_idx = -1;
1576         int extra_isize;
1577         errcode_t ret;
1578
1579         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1580
1581         ret = ext2fs_get_mem(value_len, &new_value);
1582         if (ret)
1583                 return ret;
1584         if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1585             ((strcmp(name, "system.posix_acl_default") == 0) ||
1586              (strcmp(name, "system.posix_acl_access") == 0))) {
1587                 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1588                                                        new_value, &value_len);
1589                 if (ret)
1590                         goto out;
1591         } else if (value_len)
1592                 memcpy(new_value, value, value_len);
1593
1594         /* Imitate kernel behavior by skipping update if value is the same. */
1595         for (x = h->attrs; x < h->attrs + h->count; x++) {
1596                 if (!strcmp(x->name, name)) {
1597                         if (!x->ea_ino && x->value_len == value_len &&
1598                             (!value_len ||
1599                              !memcmp(x->value, new_value, value_len))) {
1600                                 ret = 0;
1601                                 goto out;
1602                         }
1603                         old_idx = x - h->attrs;
1604                         break;
1605                 }
1606         }
1607
1608         ret = ext2fs_get_memzero(inode_size, &inode);
1609         if (ret)
1610                 goto out;
1611         ret = ext2fs_read_inode_full(fs, h->ino,
1612                                      (struct ext2_inode *)inode,
1613                                      inode_size);
1614         if (ret)
1615                 goto out;
1616         if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1617                 extra_isize = inode->i_extra_isize;
1618                 if (extra_isize == 0) {
1619                         extra_isize = fs->super->s_want_extra_isize;
1620                         if (extra_isize == 0)
1621                                 extra_isize = sizeof(__u32);
1622                 }
1623                 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1624                 ibody_free -= extra_isize;
1625                 /* Extended attribute magic and final null entry. */
1626                 ibody_free -= sizeof(__u32) * 2;
1627                 ibody_free -= space_used(h->attrs, h->ibody_count);
1628         } else
1629                 ibody_free = 0;
1630
1631         /* Inline data can only go to ibody. */
1632         if (strcmp(name, "system.data") == 0) {
1633                 if (h->ibody_count <= old_idx) {
1634                         ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1635                         goto out;
1636                 }
1637                 ret = xattr_array_update(h, name, new_value, value_len,
1638                                          ibody_free,
1639                                          0 /* block_free */, old_idx,
1640                                          0 /* in_inode */);
1641                 if (ret)
1642                         goto out;
1643                 goto write_out;
1644         }
1645
1646         block_free = fs->blocksize;
1647         block_free -= sizeof(struct ext2_ext_attr_header);
1648         /* Final null entry. */
1649         block_free -= sizeof(__u32);
1650         block_free -= space_used(h->attrs + h->ibody_count,
1651                                  h->count - h->ibody_count);
1652
1653         if (ext2fs_has_feature_ea_inode(fs->super) &&
1654             value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1655                 in_inode = 1;
1656
1657         ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1658                                  block_free, old_idx, in_inode);
1659         if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1660             ext2fs_has_feature_ea_inode(fs->super))
1661                 ret = xattr_array_update(h, name, new_value, value_len,
1662                         ibody_free, block_free, old_idx, 1 /* in_inode */);
1663         if (ret)
1664                 goto out;
1665
1666 write_out:
1667         ret = ext2fs_xattrs_write(h);
1668 out:
1669         if (inode)
1670                 ext2fs_free_mem(&inode);
1671         ext2fs_free_mem(&new_value);
1672         return ret;
1673 }
1674
1675 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1676                               const char *key)
1677 {
1678         struct ext2_xattr *x;
1679         struct ext2_xattr *end = handle->attrs + handle->count;
1680
1681         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1682         for (x = handle->attrs; x < end; x++) {
1683                 if (strcmp(x->name, key) == 0) {
1684                         ext2fs_free_mem(&x->name);
1685                         ext2fs_free_mem(&x->value);
1686                         if (x->ea_ino)
1687                                 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1688                         memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1689                         memset(end - 1, 0, sizeof(*end));
1690                         if (x < handle->attrs + handle->ibody_count)
1691                                 handle->ibody_count--;
1692                         handle->count--;
1693                         return ext2fs_xattrs_write(handle);
1694                 }
1695         }
1696
1697         /* no key found, success! */
1698         return 0;
1699 }
1700
1701 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1702                              struct ext2_xattr_handle **handle)
1703 {
1704         struct ext2_xattr_handle *h;
1705         errcode_t err;
1706
1707         if (!ext2fs_has_feature_xattr(fs->super) &&
1708             !ext2fs_has_feature_inline_data(fs->super))
1709                 return EXT2_ET_MISSING_EA_FEATURE;
1710
1711         err = ext2fs_get_memzero(sizeof(*h), &h);
1712         if (err)
1713                 return err;
1714
1715         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1716         h->capacity = 4;
1717         err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1718                                    &h->attrs);
1719         if (err) {
1720                 ext2fs_free_mem(&h);
1721                 return err;
1722         }
1723         h->count = 0;
1724         h->ino = ino;
1725         h->fs = fs;
1726         *handle = h;
1727         return 0;
1728 }
1729
1730 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1731 {
1732         struct ext2_xattr_handle *h = *handle;
1733
1734         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1735         xattrs_free_keys(h);
1736         ext2fs_free_mem(&h->attrs);
1737         ext2fs_free_mem(handle);
1738         return 0;
1739 }
1740
1741 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1742 {
1743         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1744         *count = handle->count;
1745         return 0;
1746 }
1747
1748 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1749                               unsigned int *new_flags, unsigned int *old_flags)
1750 {
1751         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1752         if (old_flags)
1753                 *old_flags = handle->flags;
1754         if (new_flags)
1755                 handle->flags = *new_flags;
1756         return 0;
1757 }
1758
1759 struct ext2_attr_info {
1760         int name_index;
1761         const char *name;
1762         const char *value;
1763         int value_len;
1764 };
1765
1766 struct ext2_attr_search {
1767         struct ext2_ext_attr_entry *first;
1768         char *base;
1769         char *end;
1770         struct ext2_ext_attr_entry *here;
1771         int not_found;
1772 };
1773
1774 struct ext2_attr_ibody_find {
1775         ext2_ino_t ino;
1776         struct ext2_attr_search s;
1777 };
1778
1779 struct ext2_attr_block_find {
1780         struct ext2_attr_search s;
1781         char *block;
1782 };
1783
1784 void ext2fs_attr_shift_entries(struct ext2_ext_attr_entry *entry,
1785                                int value_offs_shift, char *to,
1786                                char *from, int n)
1787 {
1788         struct ext2_ext_attr_entry *last = entry;
1789
1790         /* Adjust the value offsets of the entries */
1791         for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1792                 if (!last->e_value_inum && last->e_value_size) {
1793                         last->e_value_offs = last->e_value_offs +
1794                                                         value_offs_shift;
1795                 }
1796         }
1797         /* Shift the entries by n bytes and zero freed space in inode */
1798         memmove(to, from, n);
1799         if (to > from)
1800                 memset(from, 0, to - from);
1801 }
1802
1803 /*
1804  * This function returns the free space present in the inode or the EA block.
1805  * total is number of bytes taken up by the EA entries and is used to shift
1806  * the EAs in ext2fs_expand_extra_isize().
1807  */
1808 int ext2fs_attr_free_space(struct ext2_ext_attr_entry *last,
1809                            int *min_offs, char *base, int *total)
1810 {
1811         for (; !EXT2_EXT_IS_LAST_ENTRY(last); last = EXT2_EXT_ATTR_NEXT(last)) {
1812                 *total += EXT2_EXT_ATTR_LEN(last->e_name_len);
1813                 if (!last->e_value_inum && last->e_value_size) {
1814                         int offs = last->e_value_offs;
1815                         if (offs < *min_offs)
1816                                 *min_offs = offs;
1817                 }
1818         }
1819
1820         return *min_offs - ((char *)last - base) - sizeof(__u32);
1821 }
1822
1823 static errcode_t ext2fs_attr_check_names(struct ext2_ext_attr_entry *entry,
1824                                          char *end)
1825 {
1826         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1827                 struct ext2_ext_attr_entry *next = EXT2_EXT_ATTR_NEXT(entry);
1828                 if ((char *)next >= end)
1829                         return EXT2_ET_EA_BAD_ENTRIES;
1830                 entry = next;
1831         }
1832         return 0;
1833 }
1834
1835 /* The unused parameter used to be the blocksize, but with in-inode xattrs
1836  * the xattr storage area size depends on where the xattrs are kept.  Keep
1837  * this parameter for API/ABI compatibility, but it is not needed. */
1838 static errcode_t ext2fs_attr_find_entry(struct ext2_ext_attr_entry **pentry,
1839                                         int name_index, const char *name,
1840                                         int unused, int sorted)
1841 {
1842         struct ext2_ext_attr_entry *entry;
1843         int name_len;
1844         int cmp = 1;
1845
1846         if (name == NULL)
1847                 return EXT2_ET_EA_BAD_NAME;
1848
1849         name_len = strlen(name);
1850         entry = *pentry;
1851         for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
1852                 entry = EXT2_EXT_ATTR_NEXT(entry)) {
1853                 cmp = name_index - entry->e_name_index;
1854                 if (!cmp)
1855                         cmp = name_len - entry->e_name_len;
1856                 if (!cmp)
1857                         cmp = memcmp(name, entry->e_name, name_len);
1858                 if (cmp <= 0 && (sorted || cmp == 0))
1859                         break;
1860         }
1861         *pentry = entry;
1862
1863         return cmp ? EXT2_ET_EA_NAME_NOT_FOUND : 0;
1864 }
1865
1866 static errcode_t ext2fs_attr_block_find(ext2_filsys fs,struct ext2_inode *inode,
1867                                         struct ext2_attr_info *i,
1868                                         struct ext2_attr_block_find *bs)
1869 {
1870         struct ext2_ext_attr_header *header;
1871         errcode_t error;
1872
1873         if (inode->i_file_acl) {
1874                 /* The inode already has an extended attribute block. */
1875                 error = ext2fs_get_mem(fs->blocksize, &bs->block);
1876                 if (error)
1877                         return error;
1878                 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, bs->block);
1879                 if (error)
1880                         goto cleanup;
1881
1882                 header = BHDR(bs->block);
1883                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1884                         error = EXT2_ET_EA_BAD_MAGIC;
1885                         goto cleanup;
1886                 }
1887
1888                 /* Find the named attribute. */
1889                 bs->s.base = bs->block;
1890                 bs->s.first = (struct ext2_ext_attr_entry *)(header + 1);
1891                 bs->s.end = bs->block + fs->blocksize;
1892                 bs->s.here = bs->s.first;
1893                 error = ext2fs_attr_find_entry(&bs->s.here, i->name_index,
1894                                                i->name, fs->blocksize, 1);
1895                 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1896                         goto cleanup;
1897                 bs->s.not_found = error;
1898         }
1899         error = 0;
1900
1901 cleanup:
1902         if (error && bs->block)
1903                 ext2fs_free_mem(&bs->block);
1904         return error;
1905 }
1906
1907 static errcode_t ext2fs_attr_ibody_find(ext2_filsys fs,
1908                                         struct ext2_inode_large *inode,
1909                                         struct ext2_attr_info *i,
1910                                         struct ext2_attr_ibody_find *is)
1911 {
1912         errcode_t error;
1913
1914         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1915                 return 0;
1916
1917         if (inode->i_extra_isize == 0)
1918                 return 0;
1919
1920         is->s.first = &IHDR(inode)->h_first_entry[0];
1921         is->s.base = (char *)is->s.first;
1922         is->s.here = is->s.first;
1923         is->s.end = (char *)inode + EXT2_INODE_SIZE(fs->super);
1924         if (IHDR(inode)->h_magic == EXT2_EXT_ATTR_MAGIC) {
1925                 error = ext2fs_attr_check_names(is->s.first, is->s.end);
1926                 if (error)
1927                         return error;
1928                 /* Find the named attribute. */
1929                 error = ext2fs_attr_find_entry(&is->s.here, i->name_index,
1930                                                i->name, is->s.end -
1931                                                (char *)is->s.base, 0);
1932                 if (error && error != EXT2_ET_EA_NAME_NOT_FOUND)
1933                         return error;
1934                 is->s.not_found = error;
1935         }
1936
1937         return 0;
1938 }
1939
1940 static errcode_t ext2fs_attr_set_entry(ext2_filsys fs, struct ext2_attr_info *i,
1941                                        struct ext2_attr_search *s)
1942 {
1943         struct ext2_ext_attr_entry *last;
1944         int free, min_offs = s->end - s->base, name_len = strlen(i->name);
1945
1946         /* Compute min_offs and last. */
1947         for (last = s->first; !EXT2_EXT_IS_LAST_ENTRY(last);
1948              last = EXT2_EXT_ATTR_NEXT(last)) {
1949                 if (!last->e_value_inum && last->e_value_size) {
1950                         int offs = last->e_value_offs;
1951
1952                         if (offs < min_offs)
1953                                 min_offs = offs;
1954                 }
1955         }
1956         free = min_offs - ((char *)last - s->base) - sizeof(__u32);
1957
1958         if (!s->not_found) {
1959                 if (!s->here->e_value_inum && s->here->e_value_size) {
1960                         int size = s->here->e_value_size;
1961                         free += EXT2_EXT_ATTR_SIZE(size);
1962                 }
1963                 free += EXT2_EXT_ATTR_LEN(name_len);
1964         }
1965         if (i->value) {
1966                 if (free < EXT2_EXT_ATTR_LEN(name_len) +
1967                            EXT2_EXT_ATTR_SIZE(i->value_len))
1968                         return EXT2_ET_EA_NO_SPACE;
1969         }
1970
1971         if (i->value && s->not_found) {
1972                 /* Insert the new name. */
1973                 int size = EXT2_EXT_ATTR_LEN(name_len);
1974                 int rest = (char *)last - (char *)s->here + sizeof(__u32);
1975
1976                 memmove((char *)s->here + size, s->here, rest);
1977                 memset(s->here, 0, size);
1978                 s->here->e_name_index = i->name_index;
1979                 s->here->e_name_len = name_len;
1980                 memcpy(s->here->e_name, i->name, name_len);
1981         } else {
1982                 if (!s->here->e_value_inum && s->here->e_value_size) {
1983                         char *first_val = s->base + min_offs;
1984                         int offs = s->here->e_value_offs;
1985                         char *val = s->base + offs;
1986                         int size = EXT2_EXT_ATTR_SIZE(s->here->e_value_size);
1987
1988                         if (i->value &&
1989                             size == EXT2_EXT_ATTR_SIZE(i->value_len)) {
1990                                 /* The old and the new value have the same
1991                                    size. Just replace. */
1992                                 s->here->e_value_size = i->value_len;
1993                                 memset(val + size - EXT2_EXT_ATTR_PAD, 0,
1994                                        EXT2_EXT_ATTR_PAD); /* Clear pad bytes */
1995                                 memcpy(val, i->value, i->value_len);
1996                                 return 0;
1997                         }
1998
1999                         /* Remove the old value. */
2000                         memmove(first_val + size, first_val, val - first_val);
2001                         memset(first_val, 0, size);
2002                         s->here->e_value_size = 0;
2003                         s->here->e_value_offs = 0;
2004                         min_offs += size;
2005
2006                         /* Adjust all value offsets. */
2007                         last = s->first;
2008                         while (!EXT2_EXT_IS_LAST_ENTRY(last)) {
2009                                 int o = last->e_value_offs;
2010
2011                                 if (!last->e_value_inum &&
2012                                     last->e_value_size && o < offs)
2013                                         last->e_value_offs = o + size;
2014                                 last = EXT2_EXT_ATTR_NEXT(last);
2015                         }
2016                 }
2017                 if (!i->value) {
2018                         /* Remove the old name. */
2019                         int size = EXT2_EXT_ATTR_LEN(name_len);
2020
2021                         last = ENTRY((char *)last - size);
2022                         memmove((char *)s->here, (char *)s->here + size,
2023                                 (char *)last - (char *)s->here + sizeof(__u32));
2024                         memset(last, 0, size);
2025                 }
2026         }
2027
2028         if (i->value) {
2029                 /* Insert the new value. */
2030                 s->here->e_value_size = i->value_len;
2031                 if (i->value_len) {
2032                         int size = EXT2_EXT_ATTR_SIZE(i->value_len);
2033                         char *val = s->base + min_offs - size;
2034
2035                         s->here->e_value_offs = min_offs - size;
2036                         memset(val + size - EXT2_EXT_ATTR_PAD, 0,
2037                                EXT2_EXT_ATTR_PAD); /* Clear the pad bytes. */
2038                         memcpy(val, i->value, i->value_len);
2039                 }
2040         }
2041
2042         return 0;
2043 }
2044
2045 static errcode_t ext2fs_attr_block_set(ext2_filsys fs, struct ext2_inode *inode,
2046                                        struct ext2_attr_info *i,
2047                                        struct ext2_attr_block_find *bs)
2048 {
2049         struct ext2_attr_search *s = &bs->s;
2050         char *new_buf = NULL, *old_block = NULL;
2051         blk_t blk;
2052         int clear_flag = 0;
2053         errcode_t error;
2054
2055         if (i->value && i->value_len > fs->blocksize)
2056                 return EXT2_ET_EA_NO_SPACE;
2057
2058         if (s->base) {
2059                 if (BHDR(s->base)->h_refcount != 1) {
2060                         int offset = (char *)s->here - bs->block;
2061
2062                         /* Decrement the refcount of the shared block */
2063                         old_block = s->base;
2064                         BHDR(s->base)->h_refcount -= 1;
2065
2066                         error = ext2fs_get_mem(fs->blocksize, &s->base);
2067                         if (error)
2068                                 goto cleanup;
2069                         clear_flag = 1;
2070                         memcpy(s->base, bs->block, fs->blocksize);
2071                         s->first = ENTRY(BHDR(s->base)+1);
2072                         BHDR(s->base)->h_refcount = 1;
2073                         s->here = ENTRY(s->base + offset);
2074                         s->end = s->base + fs->blocksize;
2075                 }
2076         } else {
2077                 error = ext2fs_get_mem(fs->blocksize, &s->base);
2078                 if (error)
2079                         goto cleanup;
2080                 clear_flag = 1;
2081                 memset(s->base, 0, fs->blocksize);
2082                 BHDR(s->base)->h_magic = EXT2_EXT_ATTR_MAGIC;
2083                 BHDR(s->base)->h_blocks = 1;
2084                 BHDR(s->base)->h_refcount = 1;
2085                 s->first = ENTRY(BHDR(s->base)+1);
2086                 s->here = ENTRY(BHDR(s->base)+1);
2087                 s->end = s->base + fs->blocksize;
2088         }
2089
2090         error = ext2fs_attr_set_entry(fs, i, s);
2091         if (error)
2092                 goto cleanup;
2093
2094         if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2095                 ext2fs_attr_rehash(BHDR(s->base), s->here);
2096
2097         if (!EXT2_EXT_IS_LAST_ENTRY(s->first)) {
2098                 if (bs->block && bs->block == s->base) {
2099                         /* We are modifying this block in-place */
2100                         new_buf = bs->block;
2101                         blk = inode->i_file_acl;
2102                         error = ext2fs_write_ext_attr(fs, blk, s->base);
2103                         if (error)
2104                                 goto cleanup;
2105                 } else {
2106                         /* We need to allocate a new block */
2107                         error = ext2fs_new_block(fs, 0, 0, &blk);
2108                         if (error)
2109                                 goto cleanup;
2110                         ext2fs_block_alloc_stats(fs, blk, 1);
2111                         error = ext2fs_write_ext_attr(fs, blk, s->base);
2112                         if (error)
2113                                 goto cleanup;
2114                         new_buf = s->base;
2115                         if (old_block) {
2116                                 BHDR(s->base)->h_refcount -= 1;
2117                                 error = ext2fs_write_ext_attr(fs,
2118                                                               inode->i_file_acl,
2119                                                               s->base);
2120                                 if (error)
2121                                         goto cleanup;
2122                         }
2123                 }
2124         }
2125
2126         /* Update the i_blocks if we added a new EA block */
2127         if (!inode->i_file_acl && new_buf)
2128                 inode->i_blocks += fs->blocksize / 512;
2129         /* Update the inode. */
2130         inode->i_file_acl = new_buf ? blk : 0;
2131
2132 cleanup:
2133         if (clear_flag)
2134                 ext2fs_free_mem(&s->base);
2135         return 0;
2136 }
2137
2138 static errcode_t ext2fs_attr_ibody_set(ext2_filsys fs,
2139                                        struct ext2_inode_large *inode,
2140                                        struct ext2_attr_info *i,
2141                                        struct ext2_attr_ibody_find *is)
2142 {
2143         struct ext2_attr_search *s = &is->s;
2144         errcode_t error;
2145
2146         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
2147                 return EXT2_ET_EA_NO_SPACE;
2148
2149         error = ext2fs_attr_set_entry(fs, i, s);
2150         if (error)
2151                 return error;
2152
2153         if (!EXT2_EXT_IS_LAST_ENTRY(s->first))
2154                 IHDR(inode)->h_magic = EXT2_EXT_ATTR_MAGIC;
2155         else
2156                 IHDR(inode)->h_magic = 0;
2157
2158         return ext2fs_write_inode_full(fs, is->ino, (struct ext2_inode *)inode,
2159                                        EXT2_INODE_SIZE(fs->super));
2160 }
2161
2162 static struct {
2163         char str[28];
2164         int len;
2165 } ext2_attr_index_prefix[] = {
2166         [EXT2_ATTR_INDEX_USER] = { EXT2_ATTR_INDEX_USER_PREFIX,
2167                                    sizeof(EXT2_ATTR_INDEX_USER_PREFIX) },
2168         [EXT2_ATTR_INDEX_POSIX_ACL_ACCESS] = {
2169                 EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX,
2170                 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_ACCESS_PREFIX) },
2171         [EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT] = {
2172                 EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX,
2173                 sizeof(EXT2_ATTR_INDEX_POSIX_ACL_DEFAULT_PREFIX) },
2174         [EXT2_ATTR_INDEX_TRUSTED] = { EXT2_ATTR_INDEX_TRUSTED_PREFIX,
2175                                       sizeof(EXT2_ATTR_INDEX_TRUSTED_PREFIX) },
2176         [EXT2_ATTR_INDEX_LUSTRE] = { EXT2_ATTR_INDEX_LUSTRE_PREFIX,
2177                                      sizeof(EXT2_ATTR_INDEX_LUSTRE_PREFIX) },
2178         [EXT2_ATTR_INDEX_SECURITY] = { EXT2_ATTR_INDEX_SECURITY_PREFIX,
2179                                        sizeof(EXT2_ATTR_INDEX_SECURITY_PREFIX)},
2180         { "", 0 }
2181 };
2182
2183 errcode_t ext2fs_attr_set(ext2_filsys fs, ext2_ino_t ino,
2184                           struct ext2_inode *inode,
2185                           int name_index, const char *name, const char *value,
2186                           int value_len, int flags)
2187 {
2188         struct ext2_inode_large *inode_large = NULL;
2189         struct ext2_attr_info i = {
2190                 .name_index = name_index,
2191                 .name = name,
2192                 .value = value,
2193                 .value_len = value_len,
2194         };
2195         struct ext2_attr_ibody_find is = {
2196                 .ino = ino,
2197                 .s = { .not_found = -ENODATA, },
2198         };
2199         struct ext2_attr_block_find bs = {
2200                 .s = { .not_found = -ENODATA, },
2201         };
2202         errcode_t error;
2203
2204         if (!name)
2205                 return EXT2_ET_EA_BAD_NAME;
2206         if (strlen(name) > 255)
2207                 return EXT2_ET_EA_NAME_TOO_BIG;
2208
2209         /* If the prefix is still present, skip it */
2210         if (strncmp(name, ext2_attr_index_prefix[name_index].str,
2211                     ext2_attr_index_prefix[name_index].len) == 0)
2212                 i.name += ext2_attr_index_prefix[name_index].len;
2213
2214         if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
2215                 inode_large = (struct ext2_inode_large *)inode;
2216
2217                 error = ext2fs_attr_ibody_find(fs, inode_large, &i, &is);
2218                 if (error)
2219                         goto cleanup;
2220         }
2221         if (is.s.not_found) {
2222                 error = ext2fs_attr_block_find(fs, inode, &i, &bs);
2223                 if (error)
2224                         goto cleanup;
2225         }
2226
2227         if (is.s.not_found && bs.s.not_found) {
2228                 error = EXT2_ET_EA_NAME_NOT_FOUND;
2229                 if (flags & XATTR_REPLACE)
2230                         goto cleanup;
2231                 error = 0;
2232                 if (!value)
2233                         goto cleanup;
2234         } else {
2235                 error = EXT2_ET_EA_NAME_EXISTS;
2236                 if (flags & XATTR_CREATE)
2237                         goto cleanup;
2238         }
2239
2240         if (!value) {
2241                 if (!is.s.not_found &&
2242                     (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE))
2243                         error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2244                 else if (!bs.s.not_found)
2245                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2246         } else {
2247                 if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
2248                         error = ext2fs_attr_ibody_set(fs, inode_large, &i, &is);
2249                 if (!error && !bs.s.not_found) {
2250                         i.value = NULL;
2251                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2252                 } else if (error == EXT2_ET_EA_NO_SPACE) {
2253                         error = ext2fs_attr_block_set(fs, inode, &i, &bs);
2254                         if (error)
2255                                 goto cleanup;
2256                         if (!is.s.not_found) {
2257                                 i.value = NULL;
2258                                 if (EXT2_INODE_SIZE(fs->super) >
2259                                     EXT2_GOOD_OLD_INODE_SIZE)
2260                                         error = ext2fs_attr_ibody_set(fs,
2261                                                         inode_large, &i, &is);
2262                         }
2263                 }
2264         }
2265
2266 cleanup:
2267         return error;
2268 }
2269
2270 static errcode_t ext2fs_attr_check_block(ext2_filsys fs, char *buffer)
2271 {
2272         if (BHDR(buffer)->h_magic != (EXT2_EXT_ATTR_MAGIC) ||
2273             BHDR(buffer)->h_blocks != 1)
2274                 return EXT2_ET_EA_BAD_MAGIC;
2275
2276         return ext2fs_attr_check_names((struct ext2_ext_attr_entry *)
2277                                        (BHDR(buffer) + 1),
2278                                        buffer + fs->blocksize);
2279 }
2280
2281 static errcode_t ext2fs_attr_block_get(ext2_filsys fs, struct ext2_inode *inode,
2282                                        int name_index, const char *name,
2283                                        void *buffer, size_t buffer_size,
2284                                        int *easize)
2285 {
2286         struct ext2_ext_attr_header *header = NULL;
2287         struct ext2_ext_attr_entry *entry;
2288         char *block_buf = NULL;
2289         errcode_t error;
2290
2291         error = EXT2_ET_EA_NAME_NOT_FOUND;
2292         if (!inode->i_file_acl)
2293                 goto cleanup;
2294
2295         error = ext2fs_get_mem(fs->blocksize, &block_buf);
2296         if (error)
2297                 return error;
2298         error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2299         if (error)
2300                 goto cleanup;
2301
2302         error = ext2fs_attr_check_block(fs, block_buf);
2303         if (error)
2304                 goto cleanup;
2305
2306         header = BHDR(block_buf);
2307         entry = (struct ext2_ext_attr_entry *)(header + 1);
2308         error = ext2fs_attr_find_entry(&entry, name_index, name,
2309                                        fs->blocksize, 1);
2310         if (error)
2311                 goto cleanup;
2312         if (easize)
2313                 *easize = entry->e_value_size;
2314         if (buffer) {
2315                 if (entry->e_value_size > buffer_size) {
2316                         error = EXT2_ET_EA_TOO_BIG;
2317                         goto cleanup;
2318                 }
2319                 memcpy(buffer, block_buf + entry->e_value_offs,
2320                        entry->e_value_size);
2321                 error = 0;
2322         }
2323
2324 cleanup:
2325         if (block_buf)
2326                 ext2fs_free_mem(&block_buf);
2327         return error;
2328 }
2329
2330 static errcode_t ext2fs_attr_check_ibody(ext2_filsys fs,
2331                                          struct ext2_inode_large *inode)
2332 {
2333         const int inode_size = EXT2_INODE_SIZE(fs->super);
2334
2335         if (inode_size == EXT2_GOOD_OLD_INODE_SIZE)
2336                 return EXT2_ET_EA_NAME_NOT_FOUND;
2337
2338         if (IHDR(inode)->h_magic != EXT2_EXT_ATTR_MAGIC)
2339                 return EXT2_ET_EA_BAD_MAGIC;
2340
2341         return ext2fs_attr_check_names(&IHDR(inode)->h_first_entry[0],
2342                                        (char *)inode + inode_size);
2343 }
2344
2345
2346 static errcode_t ext2fs_attr_ibody_get(ext2_filsys fs,
2347                                        struct ext2_inode_large *inode,
2348                                        int name_index, const char *name,
2349                                        void *buffer, size_t buffer_size,
2350                                        int *easize)
2351 {
2352         struct ext2_ext_attr_entry *entry;
2353         int error;
2354
2355         error = ext2fs_attr_check_ibody(fs, inode);
2356         if (error)
2357                 return error;
2358
2359         entry = &IHDR(inode)->h_first_entry[0];
2360
2361         error = ext2fs_attr_find_entry(&entry, name_index, name,
2362                                 (char *)inode + EXT2_INODE_SIZE(fs->super) -
2363                                 (char *)entry, 0);
2364         if (error)
2365                 goto cleanup;
2366         if (easize)
2367                 *easize = entry->e_value_size;
2368         if (buffer) {
2369                 if (entry->e_value_size > buffer_size) {
2370                         error = EXT2_ET_EA_TOO_BIG;
2371                         goto cleanup;
2372                 }
2373                 memcpy(buffer, (char *)&IHDR(inode)->h_first_entry[0] +
2374                                entry->e_value_offs, entry->e_value_size);
2375         }
2376
2377 cleanup:
2378         return error;
2379 }
2380
2381
2382 errcode_t ext2fs_attr_get(ext2_filsys fs, struct ext2_inode *inode,
2383                           int name_index, const char *name, char *buffer,
2384                           size_t buffer_size, int *easize)
2385 {
2386         errcode_t error;
2387
2388         error = ext2fs_attr_ibody_get(fs, (struct ext2_inode_large *)inode,
2389                                       name_index, name, buffer, buffer_size,
2390                                       easize);
2391         if (error == EXT2_ET_EA_NAME_NOT_FOUND || error == EXT2_ET_EA_BAD_MAGIC)
2392                 error = ext2fs_attr_block_get(fs, inode, name_index, name,
2393                                               buffer, buffer_size, easize);
2394
2395         return error;
2396 }
2397
2398 int ext2fs_attr_get_next_attr(struct ext2_ext_attr_entry *entry, int name_index,
2399                               char *buffer, int buffer_size, int start)
2400 {
2401         const int prefix_len = ext2_attr_index_prefix[name_index].len;
2402         int total_len;
2403
2404         if (!start && !EXT2_EXT_IS_LAST_ENTRY(entry))
2405                 entry = EXT2_EXT_ATTR_NEXT(entry);
2406
2407         for (; !EXT2_EXT_IS_LAST_ENTRY(entry);
2408              entry = EXT2_EXT_ATTR_NEXT(entry)) {
2409                 if (!name_index)
2410                         break;
2411                 if (name_index == entry->e_name_index)
2412                         break;
2413         }
2414         if (EXT2_EXT_IS_LAST_ENTRY(entry))
2415                 return 0;
2416
2417         total_len = prefix_len + entry->e_name_len + 1;
2418         if (buffer && total_len <= buffer_size) {
2419                 memcpy(buffer, ext2_attr_index_prefix[name_index].str,
2420                        prefix_len);
2421                 memcpy(buffer + prefix_len, entry->e_name, entry->e_name_len);
2422                 buffer[prefix_len + entry->e_name_len] = '\0';
2423         }
2424
2425         return total_len;
2426 }
2427
2428 errcode_t ext2fs_expand_extra_isize(ext2_filsys fs, ext2_ino_t ino,
2429                                     struct ext2_inode_large *inode,
2430                                     int new_extra_isize, int *ret,
2431                                     int *needed_size)
2432 {
2433         struct ext2_inode *inode_buf = NULL;
2434         struct ext2_ext_attr_header *header = NULL;
2435         struct ext2_ext_attr_entry *entry = NULL, *last = NULL;
2436         struct ext2_attr_ibody_find is = {
2437                 .ino = ino,
2438                 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2439         };
2440         struct ext2_attr_block_find bs = {
2441                 .s = { .not_found = EXT2_ET_EA_NO_SPACE, },
2442         };
2443         char *start, *end, *block_buf = NULL, *buffer =NULL, *b_entry_name=NULL;
2444         int total_ino = 0, total_blk, free, offs, tried_min_extra_isize = 0;
2445         int s_min_extra_isize = fs->super->s_min_extra_isize;
2446         errcode_t error = 0;
2447
2448         if (needed_size)
2449                 *needed_size = new_extra_isize;
2450         error = ext2fs_get_mem(fs->blocksize, &block_buf);
2451         if (error)
2452                 return error;
2453
2454         if (inode == NULL) {
2455                 error = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super), &inode_buf);
2456                 if (error)
2457                         goto cleanup;
2458
2459                 error = ext2fs_read_inode_full(fs, ino, inode_buf,
2460                                                EXT2_INODE_SIZE(fs->super));
2461                 if (error)
2462                         goto cleanup;
2463
2464                 inode = (struct ext2_inode_large *)inode_buf;
2465         }
2466
2467 retry:
2468         if (inode->i_extra_isize >= new_extra_isize)
2469                 goto cleanup;
2470
2471         start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2472         /* No extended attributes present */
2473         if (IHDR(inode)->h_magic != EXT2_EXT_ATTR_MAGIC) {
2474                 memset(start, 0,
2475                        EXT2_INODE_SIZE(fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
2476                        inode->i_extra_isize);
2477                 inode->i_extra_isize = new_extra_isize;
2478                 if (needed_size)
2479                         *needed_size = 0;
2480                 goto write_inode;
2481         }
2482
2483         start += sizeof(__u32);
2484         end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2485         last = entry = (struct ext2_ext_attr_entry *)start;
2486         offs = end - start;
2487         /* Consider space takenup by magic number */
2488         total_ino = sizeof(__u32);
2489         free = ext2fs_attr_free_space(last, &offs, start, &total_ino);
2490
2491         /* Enough free space available in the inode for expansion */
2492         if (free >= new_extra_isize) {
2493                 ext2fs_attr_shift_entries(entry,
2494                                         inode->i_extra_isize - new_extra_isize,
2495                                         (char *)inode +
2496                                         EXT2_GOOD_OLD_INODE_SIZE +
2497                                         new_extra_isize,
2498                                         start - sizeof(__u32), total_ino);
2499                 inode->i_extra_isize = new_extra_isize;
2500                 if (needed_size)
2501                         *needed_size = 0;
2502                 goto write_inode;
2503         }
2504
2505         if (inode->i_file_acl) {
2506                 error = ext2fs_read_ext_attr(fs, inode->i_file_acl, block_buf);
2507                 if (error)
2508                         goto cleanup;
2509
2510                 header = BHDR(block_buf);
2511                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
2512                         error = EXT2_ET_EA_BAD_MAGIC;
2513                         goto cleanup;
2514                 }
2515                 end = block_buf + fs->blocksize;
2516                 last = entry = (struct ext2_ext_attr_entry *)(header+1);
2517                 start = (char *)entry;
2518                 offs = end - start;
2519                 free = ext2fs_attr_free_space(last, &offs, start, &total_blk);
2520                 if (free < new_extra_isize) {
2521                         if (!tried_min_extra_isize && s_min_extra_isize) {
2522                                 tried_min_extra_isize++;
2523                                 new_extra_isize = s_min_extra_isize;
2524                                 goto retry;
2525                         }
2526                         if (ret)
2527                                 *ret = EXT2_EXPAND_EISIZE_NOSPC;
2528                         error = EXT2_ET_EA_NO_SPACE;
2529                         goto cleanup;
2530                 }
2531         } else {
2532                 if (ret && *ret == EXT2_EXPAND_EISIZE_UNSAFE) {
2533                         *ret = EXT2_EXPAND_EISIZE_NEW_BLOCK;
2534                         error = 0;
2535                         goto cleanup;
2536                 }
2537                 free = fs->blocksize;
2538         }
2539
2540         while (new_extra_isize > 0) {
2541                 int offs, size, entry_size;
2542                 struct ext2_ext_attr_entry *small_entry = NULL;
2543                 struct ext2_attr_info i = {
2544                         .value = NULL,
2545                         .value_len = 0,
2546                 };
2547                 unsigned int total_size, shift_bytes, temp = ~0U, extra_isize=0;
2548
2549                 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2550                                 inode->i_extra_isize + sizeof(__u32);
2551                 end = (char *)inode + EXT2_INODE_SIZE(fs->super);
2552                 last = (struct ext2_ext_attr_entry *)start;
2553
2554                 /* Find the entry best suited to be pushed into EA block */
2555                 entry = NULL;
2556                 for (; !EXT2_EXT_IS_LAST_ENTRY(last);
2557                         last = EXT2_EXT_ATTR_NEXT(last)) {
2558                         total_size = EXT2_EXT_ATTR_SIZE(last->e_value_size) +
2559                                         EXT2_EXT_ATTR_LEN(last->e_name_len);
2560                         if (total_size <= free && total_size < temp) {
2561                                 if (total_size < new_extra_isize) {
2562                                         small_entry = last;
2563                                 } else {
2564                                         entry = last;
2565                                         temp = total_size;
2566                                 }
2567                         }
2568                 }
2569
2570                 if (entry == NULL) {
2571                         if (small_entry) {
2572                                 entry = small_entry;
2573                         } else {
2574                                 if (!tried_min_extra_isize &&
2575                                     s_min_extra_isize) {
2576                                         tried_min_extra_isize++;
2577                                         new_extra_isize = s_min_extra_isize;
2578                                         goto retry;
2579                                 }
2580                                 if (ret)
2581                                         *ret = EXT2_EXPAND_EISIZE_NOSPC;
2582                                 error = EXT2_ET_EA_NO_SPACE;
2583                                 goto cleanup;
2584                         }
2585                 }
2586                 offs = entry->e_value_offs;
2587                 size = entry->e_value_size;
2588                 entry_size = EXT2_EXT_ATTR_LEN(entry->e_name_len);
2589                 i.name_index = entry->e_name_index;
2590                 error = ext2fs_get_mem(EXT2_EXT_ATTR_SIZE(size), &buffer);
2591                 if (error)
2592                         goto cleanup;
2593                 error = ext2fs_get_mem(entry->e_name_len + 1, &b_entry_name);
2594                 if (error)
2595                         goto cleanup;
2596                 /* Save the entry name and the entry value */
2597                 memcpy((char *)buffer, (char *)start + offs,
2598                        EXT2_EXT_ATTR_SIZE(size));
2599                 memcpy((char *)b_entry_name, (char *)entry->e_name,
2600                        entry->e_name_len);
2601                 b_entry_name[entry->e_name_len] = '\0';
2602                 i.name = b_entry_name;
2603
2604                 error = ext2fs_attr_ibody_find(fs, inode, &i, &is);
2605                 if (error)
2606                         goto cleanup;
2607
2608                 error = ext2fs_attr_set_entry(fs, &i, &is.s);
2609                 if (error)
2610                         goto cleanup;
2611
2612                 entry = (struct ext2_ext_attr_entry *)start;
2613                 if (entry_size + EXT2_EXT_ATTR_SIZE(size) >= new_extra_isize)
2614                         shift_bytes = new_extra_isize;
2615                 else
2616                         shift_bytes = entry_size + EXT2_EXT_ATTR_SIZE(size);
2617                 ext2fs_attr_shift_entries(entry,
2618                                         inode->i_extra_isize - shift_bytes,
2619                                         (char *)inode +EXT2_GOOD_OLD_INODE_SIZE+
2620                                         extra_isize + shift_bytes,
2621                                         start - sizeof(__u32),
2622                                         total_ino - entry_size);
2623
2624                 extra_isize += shift_bytes;
2625                 new_extra_isize -= shift_bytes;
2626                 if (needed_size)
2627                         *needed_size = new_extra_isize;
2628                 inode->i_extra_isize = extra_isize;
2629
2630                 i.name = b_entry_name;
2631                 i.value = buffer;
2632                 i.value_len = size;
2633                 error = ext2fs_attr_block_find(fs, (struct ext2_inode *)inode,
2634                                                &i, &bs);
2635                 if (error)
2636                         goto cleanup;
2637
2638                 /* Add entry which was removed from the inode into the block */
2639                 error = ext2fs_attr_block_set(fs, (struct ext2_inode *)inode,
2640                                               &i, &bs);
2641                 if (error)
2642                         goto cleanup;
2643         }
2644
2645 write_inode:
2646         error = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)inode,
2647                                         EXT2_INODE_SIZE(fs->super));
2648 cleanup:
2649         if (inode_buf)
2650                 ext2fs_free_mem(&inode_buf);
2651         if (block_buf)
2652                 ext2fs_free_mem(&block_buf);
2653         if (buffer)
2654                 ext2fs_free_mem(&buffer);
2655         if (b_entry_name)
2656                 ext2fs_free_mem(&b_entry_name);
2657
2658         return error;
2659 }