Whamcloud - gitweb
libext2fs: add gnu.translator support
[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
22 #include "ext2_fs.h"
23 #include "ext2_ext_attr.h"
24 #include "ext4_acl.h"
25
26 #include "ext2fs.h"
27
28 static errcode_t read_ea_inode_hash(ext2_filsys fs, ext2_ino_t ino, __u32 *hash)
29 {
30         struct ext2_inode inode;
31         errcode_t retval;
32
33         retval = ext2fs_read_inode(fs, ino, &inode);
34         if (retval)
35                 return retval;
36         *hash = ext2fs_get_ea_inode_hash(&inode);
37         return 0;
38 }
39
40 #define NAME_HASH_SHIFT 5
41 #define VALUE_HASH_SHIFT 16
42
43 /*
44  * ext2_xattr_hash_entry()
45  *
46  * Compute the hash of an extended attribute.
47  */
48 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
49 {
50         __u32 hash = 0;
51         char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
52         int n;
53
54         for (n = 0; n < entry->e_name_len; n++) {
55                 hash = (hash << NAME_HASH_SHIFT) ^
56                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
57                        *name++;
58         }
59
60         /* The hash needs to be calculated on the data in little-endian. */
61         if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
62                 __u32 *value = (__u32 *)data;
63                 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
64                          EXT2_EXT_ATTR_PAD_BITS; n; n--) {
65                         hash = (hash << VALUE_HASH_SHIFT) ^
66                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
67                                ext2fs_le32_to_cpu(*value++);
68                 }
69         }
70
71         return hash;
72 }
73
74 /*
75  * ext2fs_ext_attr_hash_entry2()
76  *
77  * Compute the hash of an extended attribute.
78  * This version of the function supports hashing entries that reference
79  * external inodes (ea_inode feature).
80  */
81 errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
82                                       struct ext2_ext_attr_entry *entry,
83                                       void *data, __u32 *hash)
84 {
85         *hash = ext2fs_ext_attr_hash_entry(entry, data);
86
87         if (entry->e_value_inum) {
88                 __u32 ea_inode_hash;
89                 errcode_t retval;
90
91                 retval = read_ea_inode_hash(fs, entry->e_value_inum,
92                                             &ea_inode_hash);
93                 if (retval)
94                         return retval;
95
96                 *hash = (*hash << VALUE_HASH_SHIFT) ^
97                         (*hash >> (8*sizeof(*hash) - VALUE_HASH_SHIFT)) ^
98                         ea_inode_hash;
99         }
100         return 0;
101 }
102
103 #undef NAME_HASH_SHIFT
104 #undef VALUE_HASH_SHIFT
105
106 #define BLOCK_HASH_SHIFT 16
107
108 /* Mirrors ext4_xattr_rehash() implementation in kernel. */
109 void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
110                                   struct ext2_ext_attr_entry *end)
111 {
112         struct ext2_ext_attr_entry *here;
113         __u32 hash = 0;
114
115         here = (struct ext2_ext_attr_entry *)(header+1);
116         while (here < end && !EXT2_EXT_IS_LAST_ENTRY(here)) {
117                 if (!here->e_hash) {
118                         /* Block is not shared if an entry's hash value == 0 */
119                         hash = 0;
120                         break;
121                 }
122                 hash = (hash << BLOCK_HASH_SHIFT) ^
123                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
124                        here->e_hash;
125                 here = EXT2_EXT_ATTR_NEXT(here);
126         }
127         header->h_hash = hash;
128 }
129
130 #undef BLOCK_HASH_SHIFT
131
132 __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode)
133 {
134         return inode->i_atime;
135 }
136
137 void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash)
138 {
139         inode->i_atime = hash;
140 }
141
142 __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode)
143 {
144         return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
145 }
146
147 void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count)
148 {
149         inode->i_ctime = (__u32)(ref_count >> 32);
150         inode->osd1.linux1.l_i_version = (__u32)ref_count;
151 }
152
153 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
154 {
155         if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
156              header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
157             header->h_blocks != 1)
158                 return EXT2_ET_BAD_EA_HEADER;
159
160         return 0;
161 }
162
163 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
164                                 ext2_ino_t inum)
165 {
166         int             csum_failed = 0;
167         errcode_t       retval;
168
169         retval = io_channel_read_blk64(fs->io, block, 1, buf);
170         if (retval)
171                 return retval;
172
173         if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
174             !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
175                 csum_failed = 1;
176
177 #ifdef WORDS_BIGENDIAN
178         ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
179 #endif
180
181         retval = check_ext_attr_header(buf);
182         if (retval == 0 && csum_failed)
183                 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
184
185         return retval;
186 }
187
188 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
189 {
190         return ext2fs_read_ext_attr3(fs, block, buf, 0);
191 }
192
193 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
194 {
195         return ext2fs_read_ext_attr2(fs, block, buf);
196 }
197
198 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
199                                  ext2_ino_t inum)
200 {
201         errcode_t       retval;
202         char            *write_buf;
203
204 #ifdef WORDS_BIGENDIAN
205         retval = ext2fs_get_mem(fs->blocksize, &write_buf);
206         if (retval)
207                 return retval;
208         ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
209 #else
210         write_buf = (char *) inbuf;
211 #endif
212
213         retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
214                         (struct ext2_ext_attr_header *)write_buf);
215         if (retval)
216                 return retval;
217
218         retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
219 #ifdef WORDS_BIGENDIAN
220         ext2fs_free_mem(&write_buf);
221 #endif
222         if (!retval)
223                 ext2fs_mark_changed(fs);
224         return retval;
225 }
226
227 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
228 {
229         return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
230 }
231
232 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
233 {
234         return ext2fs_write_ext_attr2(fs, block, inbuf);
235 }
236
237 /*
238  * This function adjusts the reference count of the EA block.
239  */
240 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
241                                     char *block_buf, int adjust,
242                                     __u32 *newcount, ext2_ino_t inum)
243 {
244         errcode_t       retval;
245         struct ext2_ext_attr_header *header;
246         char    *buf = 0;
247
248         if ((blk >= ext2fs_blocks_count(fs->super)) ||
249             (blk < fs->super->s_first_data_block))
250                 return EXT2_ET_BAD_EA_BLOCK_NUM;
251
252         if (!block_buf) {
253                 retval = ext2fs_get_mem(fs->blocksize, &buf);
254                 if (retval)
255                         return retval;
256                 block_buf = buf;
257         }
258
259         retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
260         if (retval)
261                 goto errout;
262
263         header = (struct ext2_ext_attr_header *) block_buf;
264         header->h_refcount += adjust;
265         if (newcount)
266                 *newcount = header->h_refcount;
267
268         retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
269         if (retval)
270                 goto errout;
271
272 errout:
273         if (buf)
274                 ext2fs_free_mem(&buf);
275         return retval;
276 }
277
278 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
279                                     char *block_buf, int adjust,
280                                     __u32 *newcount)
281 {
282         return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
283                                           newcount, 0);
284 }
285
286 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
287                                         char *block_buf, int adjust,
288                                         __u32 *newcount)
289 {
290         return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
291                                           newcount);
292 }
293
294 /* Manipulate the contents of extended attribute regions */
295 struct ext2_xattr {
296         char *name;
297         void *value;
298         unsigned int value_len;
299         ext2_ino_t ea_ino;
300 };
301
302 struct ext2_xattr_handle {
303         errcode_t magic;
304         ext2_filsys fs;
305         struct ext2_xattr *attrs;
306         int capacity;
307         int count;
308         int ibody_count;
309         ext2_ino_t ino;
310         unsigned int flags;
311 };
312
313 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
314                                       unsigned int expandby)
315 {
316         struct ext2_xattr *new_attrs;
317         errcode_t err;
318
319         err = ext2fs_get_arrayzero(h->capacity + expandby,
320                                    sizeof(struct ext2_xattr), &new_attrs);
321         if (err)
322                 return err;
323
324         memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
325         ext2fs_free_mem(&h->attrs);
326         h->capacity += expandby;
327         h->attrs = new_attrs;
328
329         return 0;
330 }
331
332 struct ea_name_index {
333         int index;
334         const char *name;
335 };
336
337 /* Keep these names sorted in order of decreasing specificity. */
338 static struct ea_name_index ea_names[] = {
339         {10, "gnu."},
340         {3, "system.posix_acl_default"},
341         {2, "system.posix_acl_access"},
342         {8, "system.richacl"},
343         {6, "security."},
344         {4, "trusted."},
345         {7, "system."},
346         {1, "user."},
347         {0, NULL},
348 };
349
350 static const char *find_ea_prefix(int index)
351 {
352         struct ea_name_index *e;
353
354         for (e = ea_names; e->name; e++)
355                 if (e->index == index)
356                         return e->name;
357
358         return NULL;
359 }
360
361 static int find_ea_index(const char *fullname, const char **name, int *index)
362 {
363         struct ea_name_index *e;
364
365         for (e = ea_names; e->name; e++) {
366                 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
367                         *name = fullname + strlen(e->name);
368                         *index = e->index;
369                         return 1;
370                 }
371         }
372         return 0;
373 }
374
375 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
376                                struct ext2_inode_large *inode)
377 {
378         struct ext2_ext_attr_header *header;
379         void *block_buf = NULL;
380         blk64_t blk;
381         errcode_t err;
382         struct ext2_inode_large i;
383
384         /* Read inode? */
385         if (inode == NULL) {
386                 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
387                                              sizeof(struct ext2_inode_large));
388                 if (err)
389                         return err;
390                 inode = &i;
391         }
392
393         /* Do we already have an EA block? */
394         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
395         if (blk == 0)
396                 return 0;
397
398         /* Find block, zero it, write back */
399         if ((blk < fs->super->s_first_data_block) ||
400             (blk >= ext2fs_blocks_count(fs->super))) {
401                 err = EXT2_ET_BAD_EA_BLOCK_NUM;
402                 goto out;
403         }
404
405         err = ext2fs_get_mem(fs->blocksize, &block_buf);
406         if (err)
407                 goto out;
408
409         err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
410         if (err)
411                 goto out2;
412
413         /* We only know how to deal with v2 EA blocks */
414         header = (struct ext2_ext_attr_header *) block_buf;
415         if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
416                 err = EXT2_ET_BAD_EA_HEADER;
417                 goto out2;
418         }
419
420         header->h_refcount--;
421         err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
422         if (err)
423                 goto out2;
424
425         /* Erase link to block */
426         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
427         if (header->h_refcount == 0)
428                 ext2fs_block_alloc_stats2(fs, blk, -1);
429         err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
430         if (err)
431                 goto out2;
432
433         /* Write inode? */
434         if (inode == &i) {
435                 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
436                                               sizeof(struct ext2_inode_large));
437                 if (err)
438                         goto out2;
439         }
440
441 out2:
442         ext2fs_free_mem(&block_buf);
443 out:
444         return err;
445 }
446
447 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
448                                          struct ext2_inode_large *inode)
449 {
450         struct ext2_ext_attr_header *header;
451         void *block_buf = NULL;
452         blk64_t blk, goal;
453         errcode_t err;
454
455         /* Do we already have an EA block? */
456         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
457         if (blk != 0) {
458                 if ((blk < fs->super->s_first_data_block) ||
459                     (blk >= ext2fs_blocks_count(fs->super))) {
460                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
461                         goto out;
462                 }
463
464                 err = ext2fs_get_mem(fs->blocksize, &block_buf);
465                 if (err)
466                         goto out;
467
468                 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
469                 if (err)
470                         goto out2;
471
472                 /* We only know how to deal with v2 EA blocks */
473                 header = (struct ext2_ext_attr_header *) block_buf;
474                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
475                         err = EXT2_ET_BAD_EA_HEADER;
476                         goto out2;
477                 }
478
479                 /* Single-user block.  We're done here. */
480                 if (header->h_refcount == 1)
481                         goto out2;
482
483                 /* We need to CoW the block. */
484                 header->h_refcount--;
485                 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
486                 if (err)
487                         goto out2;
488         } else {
489                 /* No block, we must increment i_blocks */
490                 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
491                                              1);
492                 if (err)
493                         goto out;
494         }
495
496         /* Allocate a block */
497         goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
498         err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
499         if (err)
500                 goto out2;
501         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
502 out2:
503         if (block_buf)
504                 ext2fs_free_mem(&block_buf);
505 out:
506         return err;
507 }
508
509
510 static inline int
511 posix_acl_xattr_count(size_t size)
512 {
513         if (size < sizeof(posix_acl_xattr_header))
514                 return -1;
515         size -= sizeof(posix_acl_xattr_header);
516         if (size % sizeof(posix_acl_xattr_entry))
517                 return -1;
518         return size / sizeof(posix_acl_xattr_entry);
519 }
520
521 /*
522  * The lgetxattr function returns data formatted in the POSIX extended
523  * attribute format.  The on-disk format uses a more compact encoding.
524  * See the ext4_acl_to_disk in fs/ext4/acl.c.
525  */
526 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
527                                                   void *out_buf, size_t *size_out)
528 {
529         const posix_acl_xattr_header *header =
530                 (const posix_acl_xattr_header*) value;
531         const posix_acl_xattr_entry *end, *entry =
532                 (const posix_acl_xattr_entry *)(header+1);
533         ext4_acl_header *ext_acl;
534         size_t s;
535         char *e;
536
537         int count;
538
539         if (!value)
540                 return EINVAL;
541         if (size < sizeof(posix_acl_xattr_header))
542                 return ENOMEM;
543         if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
544                 return EINVAL;
545
546         count = posix_acl_xattr_count(size);
547         ext_acl = out_buf;
548         ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
549
550         if (count <= 0)
551                 return EINVAL;
552
553         e = (char *) out_buf + sizeof(ext4_acl_header);
554         s = sizeof(ext4_acl_header);
555         for (end = entry + count; entry != end;entry++) {
556                 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
557                 disk_entry->e_tag = ext2fs_cpu_to_le16(entry->e_tag);
558                 disk_entry->e_perm = ext2fs_cpu_to_le16(entry->e_perm);
559
560                 switch(entry->e_tag) {
561                         case ACL_USER_OBJ:
562                         case ACL_GROUP_OBJ:
563                         case ACL_MASK:
564                         case ACL_OTHER:
565                                 e += sizeof(ext4_acl_entry_short);
566                                 s += sizeof(ext4_acl_entry_short);
567                                 break;
568                         case ACL_USER:
569                         case ACL_GROUP:
570                                 disk_entry->e_id =  ext2fs_cpu_to_le32(entry->e_id);
571                                 e += sizeof(ext4_acl_entry);
572                                 s += sizeof(ext4_acl_entry);
573                                 break;
574                 }
575         }
576         *size_out = s;
577         return 0;
578 }
579
580 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
581                                                   void **out_buf, size_t *size_out)
582 {
583         posix_acl_xattr_header *header;
584         posix_acl_xattr_entry *entry;
585         const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
586         errcode_t err;
587         const char *cp;
588         char *out;
589
590         if ((!value) ||
591             (size < sizeof(ext4_acl_header)) ||
592             (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
593                 return EINVAL;
594
595         err = ext2fs_get_mem(size * 2, &out);
596         if (err)
597                 return err;
598
599         header = (posix_acl_xattr_header *) out;
600         header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
601         entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
602
603         cp = (const char *) value + sizeof(ext4_acl_header);
604         size -= sizeof(ext4_acl_header);
605
606         while (size > 0) {
607                 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
608
609                 entry->e_tag = ext2fs_le16_to_cpu(disk_entry->e_tag);
610                 entry->e_perm = ext2fs_le16_to_cpu(disk_entry->e_perm);
611
612                 switch(entry->e_tag) {
613                         case ACL_USER_OBJ:
614                         case ACL_GROUP_OBJ:
615                         case ACL_MASK:
616                         case ACL_OTHER:
617                                 entry->e_id = 0;
618                                 cp += sizeof(ext4_acl_entry_short);
619                                 size -= sizeof(ext4_acl_entry_short);
620                                 break;
621                         case ACL_USER:
622                         case ACL_GROUP:
623                                 entry->e_id = ext2fs_le32_to_cpu(disk_entry->e_id);
624                                 cp += sizeof(ext4_acl_entry);
625                                 size -= sizeof(ext4_acl_entry);
626                                 break;
627                 default:
628                         ext2fs_free_mem(&out);
629                         return EINVAL;
630                         break;
631                 }
632                 entry++;
633         }
634         *out_buf = out;
635         *size_out = ((char *) entry - out);
636         return 0;
637 }
638
639 static errcode_t
640 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
641                        void *entries_start, unsigned int storage_size,
642                        unsigned int value_offset_correction, int write_hash)
643 {
644         struct ext2_xattr *x;
645         struct ext2_ext_attr_entry *e = entries_start;
646         char *end = (char *) entries_start + storage_size;
647         const char *shortname;
648         unsigned int value_size;
649         int idx, ret;
650         errcode_t err;
651
652         memset(entries_start, 0, storage_size);
653         for (x = attrs; x < attrs + count; x++) {
654                 /* Calculate index and shortname position */
655                 shortname = x->name;
656                 ret = find_ea_index(x->name, &shortname, &idx);
657
658                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
659                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
660
661                 /* Fill out e appropriately */
662                 e->e_name_len = strlen(shortname);
663                 e->e_name_index = (ret ? idx : 0);
664
665                 e->e_value_size = x->value_len;
666                 e->e_value_inum = x->ea_ino;
667
668                 /* Store name */
669                 memcpy((char *)e + sizeof(*e), shortname, e->e_name_len);
670                 if (x->ea_ino) {
671                         e->e_value_offs = 0;
672                 } else {
673                         end -= value_size;
674                         e->e_value_offs = end - (char *) entries_start +
675                                                 value_offset_correction;
676                         memcpy(end, x->value, e->e_value_size);
677                 }
678
679                 if (write_hash || x->ea_ino) {
680                         err = ext2fs_ext_attr_hash_entry2(fs, e,
681                                                           x->ea_ino ? 0 : end,
682                                                           &e->e_hash);
683                         if (err)
684                                 return err;
685                 } else
686                         e->e_hash = 0;
687
688                 e = EXT2_EXT_ATTR_NEXT(e);
689                 *(__u32 *)e = 0;
690         }
691         return 0;
692 }
693
694 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
695 {
696         ext2_filsys fs = handle->fs;
697         const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
698         struct ext2_inode_large *inode;
699         char *start, *block_buf = NULL;
700         struct ext2_ext_attr_header *header;
701         __u32 ea_inode_magic;
702         blk64_t blk;
703         unsigned int storage_size;
704         unsigned int i;
705         errcode_t err;
706
707         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
708         i = inode_size;
709         if (i < sizeof(*inode))
710                 i = sizeof(*inode);
711         err = ext2fs_get_memzero(i, &inode);
712         if (err)
713                 return err;
714
715         err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
716                                      inode_size);
717         if (err)
718                 goto out;
719
720         /* If extra_isize isn't set, we need to set it now */
721         if (inode->i_extra_isize == 0 &&
722             inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
723                 char *p = (char *)inode;
724                 size_t extra = fs->super->s_want_extra_isize;
725
726                 if (extra == 0)
727                         extra = sizeof(__u32);
728                 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
729                 inode->i_extra_isize = extra;
730         }
731         if (inode->i_extra_isize & 3) {
732                 err = EXT2_ET_INODE_CORRUPTED;
733                 goto out;
734         }
735
736         /* Does the inode have space for EA? */
737         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
738             inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
739                                                                 sizeof(__u32))
740                 goto write_ea_block;
741
742         /* Write the inode EA */
743         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
744         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
745                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
746         storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
747                                 inode->i_extra_isize - sizeof(__u32);
748         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
749                                 inode->i_extra_isize + sizeof(__u32);
750
751         err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
752                                      start, storage_size, 0, 0);
753         if (err)
754                 goto out;
755 write_ea_block:
756         /* Are we done? */
757         if (handle->ibody_count == handle->count &&
758             !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
759                 goto skip_ea_block;
760
761         /* Write the EA block */
762         err = ext2fs_get_memzero(fs->blocksize, &block_buf);
763         if (err)
764                 goto out;
765
766         storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
767         start = block_buf + sizeof(struct ext2_ext_attr_header);
768
769         err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
770                                      handle->count - handle->ibody_count, start,
771                                      storage_size, start - block_buf, 1);
772         if (err)
773                 goto out2;
774
775         /* Write a header on the EA block */
776         header = (struct ext2_ext_attr_header *) block_buf;
777         header->h_magic = EXT2_EXT_ATTR_MAGIC;
778         header->h_refcount = 1;
779         header->h_blocks = 1;
780
781         /* Get a new block for writing */
782         err = prep_ea_block_for_write(fs, handle->ino, inode);
783         if (err)
784                 goto out2;
785
786         /* Finally, write the new EA block */
787         blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
788         err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
789         if (err)
790                 goto out2;
791
792 skip_ea_block:
793         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
794         if (!block_buf && blk) {
795                 /* xattrs shrunk, free the block */
796                 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
797                 if (err)
798                         goto out;
799         }
800
801         /* Write the inode */
802         err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
803                                       inode_size);
804         if (err)
805                 goto out2;
806
807 out2:
808         ext2fs_free_mem(&block_buf);
809 out:
810         ext2fs_free_mem(&inode);
811         return err;
812 }
813
814 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
815                                          struct ext2_inode_large *inode,
816                                          struct ext2_ext_attr_entry *entries,
817                                          unsigned int storage_size,
818                                          char *value_start)
819 {
820         struct ext2_xattr *x;
821         struct ext2_ext_attr_entry *entry, *end;
822         const char *prefix;
823         unsigned int remain, prefix_len;
824         errcode_t err;
825         unsigned int values_size = storage_size +
826                         ((char *)entries - value_start);
827
828         /* find the end */
829         end = entries;
830         remain = storage_size;
831         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
832                !EXT2_EXT_IS_LAST_ENTRY(end)) {
833
834                 /* header eats this space */
835                 remain -= sizeof(struct ext2_ext_attr_entry);
836
837                 /* is attribute name valid? */
838                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
839                         return EXT2_ET_EA_BAD_NAME_LEN;
840
841                 /* attribute len eats this space */
842                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
843                 end = EXT2_EXT_ATTR_NEXT(end);
844         }
845
846         entry = entries;
847         remain = storage_size;
848         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
849                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
850
851                 /* Allocate space for more attrs? */
852                 if (handle->count == handle->capacity) {
853                         err = ext2fs_xattrs_expand(handle, 4);
854                         if (err)
855                                 return err;
856                 }
857
858                 x = handle->attrs + handle->count;
859
860                 /* header eats this space */
861                 remain -= sizeof(struct ext2_ext_attr_entry);
862
863                 /* attribute len eats this space */
864                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
865
866                 /* Extract name */
867                 prefix = find_ea_prefix(entry->e_name_index);
868                 prefix_len = (prefix ? strlen(prefix) : 0);
869                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
870                                          &x->name);
871                 if (err)
872                         return err;
873                 if (prefix)
874                         memcpy(x->name, prefix, prefix_len);
875                 if (entry->e_name_len)
876                         memcpy(x->name + prefix_len,
877                                (char *)entry + sizeof(*entry),
878                                entry->e_name_len);
879
880                 /* Check & copy value */
881                 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
882                     entry->e_value_inum != 0)
883                         return EXT2_ET_BAD_EA_BLOCK_NUM;
884
885                 if (entry->e_value_inum == 0) {
886                         if (entry->e_value_size > remain)
887                                 return EXT2_ET_EA_BAD_VALUE_SIZE;
888
889                         if (entry->e_value_offs + entry->e_value_size > values_size)
890                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
891
892                         if (entry->e_value_size > 0 &&
893                             value_start + entry->e_value_offs <
894                             (char *)end + sizeof(__u32))
895                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
896
897                         remain -= entry->e_value_size;
898
899                         err = ext2fs_get_mem(entry->e_value_size, &x->value);
900                         if (err)
901                                 return err;
902                         memcpy(x->value, value_start + entry->e_value_offs,
903                                entry->e_value_size);
904                 } else {
905                         struct ext2_inode *ea_inode;
906                         ext2_file_t ea_file;
907
908                         if (entry->e_value_offs != 0)
909                                 return EXT2_ET_EA_BAD_VALUE_OFFSET;
910
911                         if (entry->e_value_size > (64 * 1024))
912                                 return EXT2_ET_EA_BAD_VALUE_SIZE;
913
914                         err = ext2fs_get_mem(entry->e_value_size, &x->value);
915                         if (err)
916                                 return err;
917
918                         err = ext2fs_file_open(handle->fs, entry->e_value_inum,
919                                                0, &ea_file);
920                         if (err)
921                                 return err;
922
923                         ea_inode = ext2fs_file_get_inode(ea_file);
924                         if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
925                             !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
926                             ea_inode->i_links_count == 0)
927                                 err = EXT2_ET_EA_INODE_CORRUPTED;
928                         else if ((__u64) ext2fs_file_get_size(ea_file) !=
929                                  entry->e_value_size)
930                                 err = EXT2_ET_EA_BAD_VALUE_SIZE;
931                         else
932                                 err = ext2fs_file_read(ea_file, x->value,
933                                                        entry->e_value_size, 0);
934                         ext2fs_file_close(ea_file);
935                         if (err)
936                                 return err;
937                 }
938
939                 x->ea_ino = entry->e_value_inum;
940                 x->value_len = entry->e_value_size;
941
942                 /* e_hash may be 0 in older inode's ea */
943                 if (entry->e_hash != 0) {
944                         __u32 hash;
945                         void *data = (entry->e_value_inum != 0) ?
946                                         0 : value_start + entry->e_value_offs;
947
948                         err = ext2fs_ext_attr_hash_entry2(handle->fs, entry,
949                                                           data, &hash);
950                         if (err)
951                                 return err;
952                         if (entry->e_hash != hash) {
953                                 struct ext2_inode child;
954
955                                 /* Check whether this is an old Lustre-style
956                                  * ea_inode reference.
957                                  */
958                                 err = ext2fs_read_inode(handle->fs,
959                                                         entry->e_value_inum,
960                                                         &child);
961                                 if (err)
962                                         return err;
963                                 if (child.i_mtime != handle->ino ||
964                                     child.i_generation != inode->i_generation)
965                                         return EXT2_ET_BAD_EA_HASH;
966                         }
967                 }
968
969                 handle->count++;
970                 entry = EXT2_EXT_ATTR_NEXT(entry);
971         }
972
973         return 0;
974 }
975
976 static void xattrs_free_keys(struct ext2_xattr_handle *h)
977 {
978         struct ext2_xattr *a = h->attrs;
979         int i;
980
981         for (i = 0; i < h->capacity; i++) {
982                 if (a[i].name)
983                         ext2fs_free_mem(&a[i].name);
984                 if (a[i].value)
985                         ext2fs_free_mem(&a[i].value);
986         }
987         h->count = 0;
988         h->ibody_count = 0;
989 }
990
991 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
992 {
993         struct ext2_inode_large *inode;
994         struct ext2_ext_attr_header *header;
995         __u32 ea_inode_magic;
996         unsigned int storage_size;
997         char *start, *block_buf = NULL;
998         blk64_t blk;
999         size_t i;
1000         errcode_t err;
1001
1002         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1003         i = EXT2_INODE_SIZE(handle->fs->super);
1004         if (i < sizeof(*inode))
1005                 i = sizeof(*inode);
1006         err = ext2fs_get_memzero(i, &inode);
1007         if (err)
1008                 return err;
1009
1010         err = ext2fs_read_inode_full(handle->fs, handle->ino,
1011                                      (struct ext2_inode *)inode,
1012                                      EXT2_INODE_SIZE(handle->fs->super));
1013         if (err)
1014                 goto out;
1015
1016         xattrs_free_keys(handle);
1017
1018         /* Does the inode have space for EA? */
1019         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1020             EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1021                                                   inode->i_extra_isize +
1022                                                   sizeof(__u32))
1023                 goto read_ea_block;
1024         if (inode->i_extra_isize & 3) {
1025                 err = EXT2_ET_INODE_CORRUPTED;
1026                 goto out;
1027         }
1028
1029         /* Look for EA in the inode */
1030         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1031                inode->i_extra_isize, sizeof(__u32));
1032         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1033                 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1034                         EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1035                         sizeof(__u32);
1036                 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1037                         inode->i_extra_isize + sizeof(__u32);
1038
1039                 err = read_xattrs_from_buffer(handle, inode,
1040                                         (struct ext2_ext_attr_entry *) start,
1041                                         storage_size, start);
1042                 if (err)
1043                         goto out;
1044
1045                 handle->ibody_count = handle->count;
1046         }
1047
1048 read_ea_block:
1049         /* Look for EA in a separate EA block */
1050         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
1051         if (blk != 0) {
1052                 if ((blk < handle->fs->super->s_first_data_block) ||
1053                     (blk >= ext2fs_blocks_count(handle->fs->super))) {
1054                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
1055                         goto out;
1056                 }
1057
1058                 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1059                 if (err)
1060                         goto out;
1061
1062                 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1063                                             handle->ino);
1064                 if (err)
1065                         goto out3;
1066
1067                 /* We only know how to deal with v2 EA blocks */
1068                 header = (struct ext2_ext_attr_header *) block_buf;
1069                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1070                         err = EXT2_ET_BAD_EA_HEADER;
1071                         goto out3;
1072                 }
1073
1074                 /* Read EAs */
1075                 storage_size = handle->fs->blocksize -
1076                         sizeof(struct ext2_ext_attr_header);
1077                 start = block_buf + sizeof(struct ext2_ext_attr_header);
1078                 err = read_xattrs_from_buffer(handle, inode,
1079                                         (struct ext2_ext_attr_entry *) start,
1080                                         storage_size, block_buf);
1081                 if (err)
1082                         goto out3;
1083
1084                 ext2fs_free_mem(&block_buf);
1085         }
1086
1087         ext2fs_free_mem(&block_buf);
1088         ext2fs_free_mem(&inode);
1089         return 0;
1090
1091 out3:
1092         ext2fs_free_mem(&block_buf);
1093 out:
1094         ext2fs_free_mem(&inode);
1095         return err;
1096 }
1097
1098 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1099                                 int (*func)(char *name, char *value,
1100                                             size_t value_len, void *data),
1101                                 void *data)
1102 {
1103         struct ext2_xattr *x;
1104         int dirty = 0;
1105         int ret;
1106
1107         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1108         for (x = h->attrs; x < h->attrs + h->count; x++) {
1109                 ret = func(x->name, x->value, x->value_len, data);
1110                 if (ret & XATTR_CHANGED)
1111                         dirty = 1;
1112                 if (ret & XATTR_ABORT)
1113                         break;
1114         }
1115
1116         if (dirty)
1117                 return ext2fs_xattrs_write(h);
1118         return 0;
1119 }
1120
1121 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1122                            void **value, size_t *value_len)
1123 {
1124         struct ext2_xattr *x;
1125         char *val;
1126         errcode_t err;
1127
1128         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1129         for (x = h->attrs; x < h->attrs + h->count; x++) {
1130                 if (strcmp(x->name, key))
1131                         continue;
1132
1133                 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1134                     ((strcmp(key, "system.posix_acl_default") == 0) ||
1135                      (strcmp(key, "system.posix_acl_access") == 0))) {
1136                         err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1137                                                                value, value_len);
1138                         return err;
1139                 } else {
1140                         err = ext2fs_get_mem(x->value_len, &val);
1141                         if (err)
1142                                 return err;
1143                         memcpy(val, x->value, x->value_len);
1144                         *value = val;
1145                         *value_len = x->value_len;
1146                         return 0;
1147                 }
1148         }
1149
1150         return EXT2_ET_EA_KEY_NOT_FOUND;
1151 }
1152
1153 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1154                                       size_t *size)
1155 {
1156         struct ext2_ext_attr_entry *entry;
1157         struct ext2_inode_large *inode;
1158         __u32 ea_inode_magic;
1159         unsigned int minoff;
1160         char *start;
1161         size_t i;
1162         errcode_t err;
1163
1164         i = EXT2_INODE_SIZE(fs->super);
1165         if (i < sizeof(*inode))
1166                 i = sizeof(*inode);
1167         err = ext2fs_get_memzero(i, &inode);
1168         if (err)
1169                 return err;
1170
1171         err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1172                                      EXT2_INODE_SIZE(fs->super));
1173         if (err)
1174                 goto out;
1175
1176         /* Does the inode have size for EA? */
1177         if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1178                                                   inode->i_extra_isize +
1179                                                   sizeof(__u32)) {
1180                 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1181                 goto out;
1182         }
1183
1184         minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1185         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1186                inode->i_extra_isize, sizeof(__u32));
1187         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1188                 /* has xattrs.  calculate the size */
1189                 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1190                         inode->i_extra_isize + sizeof(__u32);
1191                 entry = (struct ext2_ext_attr_entry *) start;
1192                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1193                         if (!entry->e_value_inum && entry->e_value_size) {
1194                                 unsigned int offs = entry->e_value_offs;
1195                                 if (offs < minoff)
1196                                         minoff = offs;
1197                         }
1198                         entry = EXT2_EXT_ATTR_NEXT(entry);
1199                 }
1200                 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1201         } else {
1202                 /* no xattr.  return a maximum size */
1203                 *size = EXT2_EXT_ATTR_SIZE(minoff -
1204                                            EXT2_EXT_ATTR_LEN(strlen("data")) -
1205                                            EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1206         }
1207
1208 out:
1209         ext2fs_free_mem(&inode);
1210         return err;
1211 }
1212
1213 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1214                                        size_t value_len, ext2_ino_t *ea_ino)
1215 {
1216         struct ext2_inode inode;
1217         ext2_ino_t ino;
1218         ext2_file_t file;
1219         __u32 hash;
1220         errcode_t ret;
1221
1222         ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1223         if (ret)
1224                 return ret;
1225
1226         memset(&inode, 0, sizeof(inode));
1227         inode.i_flags |= EXT4_EA_INODE_FL;
1228         if (ext2fs_has_feature_extents(fs->super))
1229                 inode.i_flags |= EXT4_EXTENTS_FL;
1230         inode.i_size = 0;
1231         inode.i_mode = LINUX_S_IFREG | 0600;
1232         inode.i_links_count = 1;
1233         ret = ext2fs_write_new_inode(fs, ino, &inode);
1234         if (ret)
1235                 return ret;
1236         /*
1237          * ref_count and hash utilize inode's i_*time fields.
1238          * ext2fs_write_new_inode() call above initializes these fields with
1239          * current time. That's why ref count and hash updates are done
1240          * separately below.
1241          */
1242         ext2fs_set_ea_inode_ref(&inode, 1);
1243         hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1244         ext2fs_set_ea_inode_hash(&inode, hash);
1245
1246         ret = ext2fs_write_inode(fs, ino, &inode);
1247         if (ret)
1248                 return ret;
1249
1250         ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1251         if (ret)
1252                 return ret;
1253         ret = ext2fs_file_write(file, value, value_len, NULL);
1254         ext2fs_file_close(file);
1255         if (ret)
1256                 return ret;
1257
1258         ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1259
1260         *ea_ino = ino;
1261         return 0;
1262 }
1263
1264 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1265 {
1266         struct ext2_inode_large inode;
1267         __u64 ref_count;
1268         errcode_t ret;
1269
1270         ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1271                                      sizeof(inode));
1272         if (ret)
1273                 goto out;
1274
1275         ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1276         ref_count--;
1277         ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1278
1279         if (ref_count)
1280                 goto write_out;
1281
1282         inode.i_links_count = 0;
1283         inode.i_dtime = fs->now ? fs->now : time(0);
1284
1285         ret = ext2fs_free_ext_attr(fs, ino, &inode);
1286         if (ret)
1287                 goto write_out;
1288
1289         if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1290                 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1291                                    0, ~0ULL);
1292                 if (ret)
1293                         goto out;
1294         }
1295
1296         ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1297
1298 write_out:
1299         ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1300                                       sizeof(inode));
1301 out:
1302         return ret;
1303 }
1304
1305 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1306                                     const char *name, const void *value,
1307                                     size_t value_len, int in_inode)
1308 {
1309         ext2_ino_t ea_ino = 0;
1310         void *new_value = NULL;
1311         char *new_name = NULL;
1312         int name_len;
1313         errcode_t ret;
1314
1315         if (!x->name) {
1316                 name_len = strlen(name);
1317                 ret = ext2fs_get_mem(name_len + 1, &new_name);
1318                 if (ret)
1319                         goto fail;
1320                 memcpy(new_name, name, name_len + 1);
1321         }
1322
1323         ret = ext2fs_get_mem(value_len, &new_value);
1324         if (ret)
1325                 goto fail;
1326         memcpy(new_value, value, value_len);
1327
1328         if (in_inode) {
1329                 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1330                 if (ret)
1331                         goto fail;
1332         }
1333
1334         if (x->ea_ino) {
1335                 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1336                 if (ret)
1337                         goto fail;
1338         }
1339
1340         if (!x->name)
1341                 x->name = new_name;
1342
1343         if (x->value)
1344                 ext2fs_free_mem(&x->value);
1345         x->value = new_value;
1346         x->value_len = value_len;
1347         x->ea_ino = ea_ino;
1348         return 0;
1349 fail:
1350         if (new_name)
1351                 ext2fs_free_mem(&new_name);
1352         if (new_value)
1353                 ext2fs_free_mem(&new_value);
1354         if (ea_ino)
1355                 xattr_inode_dec_ref(fs, ea_ino);
1356         return ret;
1357 }
1358
1359 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1360                                const char *name)
1361 {
1362         struct ext2_xattr *x;
1363         int i;
1364         const char *shortname, *x_shortname;
1365         int name_idx, x_name_idx;
1366         int shortname_len, x_shortname_len;
1367
1368         find_ea_index(name, &shortname, &name_idx);
1369         shortname_len = strlen(shortname);
1370
1371         for (i = 0, x = attrs; i < count; i++, x++) {
1372                 find_ea_index(x->name, &x_shortname, &x_name_idx);
1373                 if (name_idx < x_name_idx)
1374                         break;
1375                 if (name_idx > x_name_idx)
1376                         continue;
1377
1378                 x_shortname_len = strlen(x_shortname);
1379                 if (shortname_len < x_shortname_len)
1380                         break;
1381                 if (shortname_len > x_shortname_len)
1382                         continue;
1383
1384                 if (memcmp(shortname, x_shortname, shortname_len) <= 0)
1385                         break;
1386         }
1387         return i;
1388 }
1389
1390 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1391                                     const char *name,
1392                                     const void *value, size_t value_len,
1393                                     int ibody_free, int block_free,
1394                                     int old_idx, int in_inode)
1395 {
1396         struct ext2_xattr tmp;
1397         int add_to_ibody;
1398         int needed;
1399         int name_len, name_idx;
1400         const char *shortname;
1401         int new_idx;
1402         int ret;
1403
1404         find_ea_index(name, &shortname, &name_idx);
1405         name_len = strlen(shortname);
1406
1407         needed = EXT2_EXT_ATTR_LEN(name_len);
1408         if (!in_inode)
1409                 needed += EXT2_EXT_ATTR_SIZE(value_len);
1410
1411         if (old_idx >= 0 && old_idx < h->ibody_count) {
1412                 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1413                 if (!h->attrs[old_idx].ea_ino)
1414                         ibody_free += EXT2_EXT_ATTR_SIZE(
1415                                                 h->attrs[old_idx].value_len);
1416         }
1417
1418         if (needed <= ibody_free) {
1419                 if (old_idx < 0) {
1420                         new_idx = h->ibody_count;
1421                         add_to_ibody = 1;
1422                         goto add_new;
1423                 }
1424
1425                 /* Update the existing entry. */
1426                 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1427                                          value, value_len, in_inode);
1428                 if (ret)
1429                         return ret;
1430                 if (h->ibody_count <= old_idx) {
1431                         /* Move entry from block to the end of ibody. */
1432                         tmp = h->attrs[old_idx];
1433                         memmove(h->attrs + h->ibody_count + 1,
1434                                 h->attrs + h->ibody_count,
1435                                 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1436                         h->attrs[h->ibody_count] = tmp;
1437                         h->ibody_count++;
1438                 }
1439                 return 0;
1440         }
1441
1442         if (h->ibody_count <= old_idx) {
1443                 block_free += EXT2_EXT_ATTR_LEN(name_len);
1444                 if (!h->attrs[old_idx].ea_ino)
1445                         block_free +=
1446                                 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1447         }
1448
1449         if (needed > block_free)
1450                 return EXT2_ET_EA_NO_SPACE;
1451
1452         if (old_idx >= 0) {
1453                 /* Update the existing entry. */
1454                 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1455                                          value, value_len, in_inode);
1456                 if (ret)
1457                         return ret;
1458                 if (old_idx < h->ibody_count) {
1459                         /*
1460                          * Move entry from ibody to the block. Note that
1461                          * entries in the block are sorted.
1462                          */
1463                         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1464                                 h->count - h->ibody_count, name);
1465                         new_idx += h->ibody_count - 1;
1466                         tmp = h->attrs[old_idx];
1467                         memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1468                                 (new_idx - old_idx) * sizeof(*h->attrs));
1469                         h->attrs[new_idx] = tmp;
1470                         h->ibody_count--;
1471                 }
1472                 return 0;
1473         }
1474
1475         new_idx = xattr_find_position(h->attrs + h->ibody_count,
1476                                       h->count - h->ibody_count, name);
1477         new_idx += h->ibody_count;
1478         add_to_ibody = 0;
1479
1480 add_new:
1481         if (h->count == h->capacity) {
1482                 ret = ext2fs_xattrs_expand(h, 4);
1483                 if (ret)
1484                         return ret;
1485         }
1486
1487         ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, value,
1488                                  value_len, in_inode);
1489         if (ret)
1490                 return ret;
1491
1492         tmp = h->attrs[h->count];
1493         memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1494                 (h->count - new_idx)*sizeof(*h->attrs));
1495         h->attrs[new_idx] = tmp;
1496         if (add_to_ibody)
1497                 h->ibody_count++;
1498         h->count++;
1499         return 0;
1500 }
1501
1502 static int space_used(struct ext2_xattr *attrs, int count)
1503 {
1504         int total = 0;
1505         struct ext2_xattr *x;
1506         const char *shortname;
1507         int i, len, name_idx;
1508
1509         for (i = 0, x = attrs; i < count; i++, x++) {
1510                 find_ea_index(x->name, &shortname, &name_idx);
1511                 len = strlen(shortname);
1512                 total += EXT2_EXT_ATTR_LEN(len);
1513                 if (!x->ea_ino)
1514                         total += EXT2_EXT_ATTR_SIZE(x->value_len);
1515         }
1516         return total;
1517 }
1518
1519 /*
1520  * The minimum size of EA value when you start storing it in an external inode
1521  * size of block - size of header - size of 1 entry - 4 null bytes
1522  */
1523 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1524         ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1525
1526 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1527                            const char *name,
1528                            const void *value,
1529                            size_t value_len)
1530 {
1531         ext2_filsys fs = h->fs;
1532         const int inode_size = EXT2_INODE_SIZE(fs->super);
1533         struct ext2_inode_large *inode = NULL;
1534         struct ext2_xattr *x;
1535         char *new_value;
1536         int ibody_free, block_free;
1537         int in_inode = 0;
1538         int old_idx = -1;
1539         int extra_isize;
1540         errcode_t ret;
1541
1542         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1543
1544         ret = ext2fs_get_mem(value_len, &new_value);
1545         if (ret)
1546                 return ret;
1547         if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1548             ((strcmp(name, "system.posix_acl_default") == 0) ||
1549              (strcmp(name, "system.posix_acl_access") == 0))) {
1550                 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1551                                                        new_value, &value_len);
1552                 if (ret)
1553                         goto out;
1554         } else if (value_len)
1555                 memcpy(new_value, value, value_len);
1556
1557         /* Imitate kernel behavior by skipping update if value is the same. */
1558         for (x = h->attrs; x < h->attrs + h->count; x++) {
1559                 if (!strcmp(x->name, name)) {
1560                         if (!x->ea_ino && x->value_len == value_len &&
1561                             (!value_len ||
1562                              !memcmp(x->value, new_value, value_len))) {
1563                                 ret = 0;
1564                                 goto out;
1565                         }
1566                         old_idx = x - h->attrs;
1567                         break;
1568                 }
1569         }
1570
1571         ret = ext2fs_get_memzero(inode_size, &inode);
1572         if (ret)
1573                 goto out;
1574         ret = ext2fs_read_inode_full(fs, h->ino,
1575                                      (struct ext2_inode *)inode,
1576                                      inode_size);
1577         if (ret)
1578                 goto out;
1579         if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1580                 extra_isize = inode->i_extra_isize;
1581                 if (extra_isize == 0) {
1582                         extra_isize = fs->super->s_want_extra_isize;
1583                         if (extra_isize == 0)
1584                                 extra_isize = sizeof(__u32);
1585                 }
1586                 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1587                 ibody_free -= extra_isize;
1588                 /* Extended attribute magic and final null entry. */
1589                 ibody_free -= sizeof(__u32) * 2;
1590                 ibody_free -= space_used(h->attrs, h->ibody_count);
1591         } else
1592                 ibody_free = 0;
1593
1594         /* Inline data can only go to ibody. */
1595         if (strcmp(name, "system.data") == 0) {
1596                 if (h->ibody_count <= old_idx) {
1597                         ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1598                         goto out;
1599                 }
1600                 ret = xattr_array_update(h, name, new_value, value_len,
1601                                          ibody_free,
1602                                          0 /* block_free */, old_idx,
1603                                          0 /* in_inode */);
1604                 if (ret)
1605                         goto out;
1606                 goto write_out;
1607         }
1608
1609         block_free = fs->blocksize;
1610         block_free -= sizeof(struct ext2_ext_attr_header);
1611         /* Final null entry. */
1612         block_free -= sizeof(__u32);
1613         block_free -= space_used(h->attrs + h->ibody_count,
1614                                  h->count - h->ibody_count);
1615
1616         if (ext2fs_has_feature_ea_inode(fs->super) &&
1617             value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1618                 in_inode = 1;
1619
1620         ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1621                                  block_free, old_idx, in_inode);
1622         if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1623             ext2fs_has_feature_ea_inode(fs->super))
1624                 ret = xattr_array_update(h, name, new_value, value_len,
1625                         ibody_free, block_free, old_idx, 1 /* in_inode */);
1626         if (ret)
1627                 goto out;
1628
1629 write_out:
1630         ret = ext2fs_xattrs_write(h);
1631 out:
1632         if (inode)
1633                 ext2fs_free_mem(&inode);
1634         ext2fs_free_mem(&new_value);
1635         return ret;
1636 }
1637
1638 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1639                               const char *key)
1640 {
1641         struct ext2_xattr *x;
1642         struct ext2_xattr *end = handle->attrs + handle->count;
1643
1644         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1645         for (x = handle->attrs; x < end; x++) {
1646                 if (strcmp(x->name, key) == 0) {
1647                         ext2fs_free_mem(&x->name);
1648                         ext2fs_free_mem(&x->value);
1649                         if (x->ea_ino)
1650                                 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1651                         memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1652                         memset(end - 1, 0, sizeof(*end));
1653                         if (x < handle->attrs + handle->ibody_count)
1654                                 handle->ibody_count--;
1655                         handle->count--;
1656                         return ext2fs_xattrs_write(handle);
1657                 }
1658         }
1659
1660         /* no key found, success! */
1661         return 0;
1662 }
1663
1664 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1665                              struct ext2_xattr_handle **handle)
1666 {
1667         struct ext2_xattr_handle *h;
1668         errcode_t err;
1669
1670         if (!ext2fs_has_feature_xattr(fs->super) &&
1671             !ext2fs_has_feature_inline_data(fs->super))
1672                 return EXT2_ET_MISSING_EA_FEATURE;
1673
1674         err = ext2fs_get_memzero(sizeof(*h), &h);
1675         if (err)
1676                 return err;
1677
1678         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1679         h->capacity = 4;
1680         err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1681                                    &h->attrs);
1682         if (err) {
1683                 ext2fs_free_mem(&h);
1684                 return err;
1685         }
1686         h->count = 0;
1687         h->ino = ino;
1688         h->fs = fs;
1689         *handle = h;
1690         return 0;
1691 }
1692
1693 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1694 {
1695         struct ext2_xattr_handle *h = *handle;
1696
1697         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1698         xattrs_free_keys(h);
1699         ext2fs_free_mem(&h->attrs);
1700         ext2fs_free_mem(handle);
1701         return 0;
1702 }
1703
1704 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1705 {
1706         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1707         *count = handle->count;
1708         return 0;
1709 }
1710
1711 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1712                               unsigned int *new_flags, unsigned int *old_flags)
1713 {
1714         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1715         if (old_flags)
1716                 *old_flags = handle->flags;
1717         if (new_flags)
1718                 handle->flags = *new_flags;
1719         return 0;
1720 }