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