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