Whamcloud - gitweb
libext2fs: check ea value offset when loading
[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
25 #include "ext2fs.h"
26
27 #define NAME_HASH_SHIFT 5
28 #define VALUE_HASH_SHIFT 16
29
30 /*
31  * ext2_xattr_hash_entry()
32  *
33  * Compute the hash of an extended attribute.
34  */
35 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
36 {
37         __u32 hash = 0;
38         char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
39         int n;
40
41         for (n = 0; n < entry->e_name_len; n++) {
42                 hash = (hash << NAME_HASH_SHIFT) ^
43                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
44                        *name++;
45         }
46
47         /* The hash needs to be calculated on the data in little-endian. */
48         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
49                 __u32 *value = (__u32 *)data;
50                 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
51                          EXT2_EXT_ATTR_PAD_BITS; n; n--) {
52                         hash = (hash << VALUE_HASH_SHIFT) ^
53                                (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
54                                ext2fs_le32_to_cpu(*value++);
55                 }
56         }
57
58         return hash;
59 }
60
61 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
62 {
63         if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
64              header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
65             header->h_blocks != 1)
66                 return EXT2_ET_BAD_EA_HEADER;
67
68         return 0;
69 }
70
71 #undef NAME_HASH_SHIFT
72 #undef VALUE_HASH_SHIFT
73
74 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
75                                 ext2_ino_t inum)
76 {
77         int             csum_failed = 0;
78         errcode_t       retval;
79
80         retval = io_channel_read_blk64(fs->io, block, 1, buf);
81         if (retval)
82                 return retval;
83
84         if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
85             !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
86                 csum_failed = 1;
87
88 #ifdef WORDS_BIGENDIAN
89         ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
90 #endif
91
92         retval = check_ext_attr_header(buf);
93         if (retval == 0 && csum_failed)
94                 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
95
96         return retval;
97 }
98
99 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
100 {
101         return ext2fs_read_ext_attr3(fs, block, buf, 0);
102 }
103
104 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
105 {
106         return ext2fs_read_ext_attr2(fs, block, buf);
107 }
108
109 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
110                                  ext2_ino_t inum)
111 {
112         errcode_t       retval;
113         char            *write_buf;
114
115 #ifdef WORDS_BIGENDIAN
116         retval = ext2fs_get_mem(fs->blocksize, &write_buf);
117         if (retval)
118                 return retval;
119         ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
120 #else
121         write_buf = (char *) inbuf;
122 #endif
123
124         retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
125                         (struct ext2_ext_attr_header *)write_buf);
126         if (retval)
127                 return retval;
128
129         retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
130 #ifdef WORDS_BIGENDIAN
131         ext2fs_free_mem(&write_buf);
132 #endif
133         if (!retval)
134                 ext2fs_mark_changed(fs);
135         return retval;
136 }
137
138 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
139 {
140         return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
141 }
142
143 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
144 {
145         return ext2fs_write_ext_attr2(fs, block, inbuf);
146 }
147
148 /*
149  * This function adjusts the reference count of the EA block.
150  */
151 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
152                                     char *block_buf, int adjust,
153                                     __u32 *newcount, ext2_ino_t inum)
154 {
155         errcode_t       retval;
156         struct ext2_ext_attr_header *header;
157         char    *buf = 0;
158
159         if ((blk >= ext2fs_blocks_count(fs->super)) ||
160             (blk < fs->super->s_first_data_block))
161                 return EXT2_ET_BAD_EA_BLOCK_NUM;
162
163         if (!block_buf) {
164                 retval = ext2fs_get_mem(fs->blocksize, &buf);
165                 if (retval)
166                         return retval;
167                 block_buf = buf;
168         }
169
170         retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
171         if (retval)
172                 goto errout;
173
174         header = (struct ext2_ext_attr_header *) block_buf;
175         header->h_refcount += adjust;
176         if (newcount)
177                 *newcount = header->h_refcount;
178
179         retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
180         if (retval)
181                 goto errout;
182
183 errout:
184         if (buf)
185                 ext2fs_free_mem(&buf);
186         return retval;
187 }
188
189 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
190                                     char *block_buf, int adjust,
191                                     __u32 *newcount)
192 {
193         return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
194                                           newcount, 0);
195 }
196
197 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
198                                         char *block_buf, int adjust,
199                                         __u32 *newcount)
200 {
201         return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
202                                           newcount);
203 }
204
205 /* Manipulate the contents of extended attribute regions */
206 struct ext2_xattr {
207         char *name;
208         void *value;
209         size_t value_len;
210 };
211
212 struct ext2_xattr_handle {
213         errcode_t magic;
214         ext2_filsys fs;
215         struct ext2_xattr *attrs;
216         size_t length, count;
217         ext2_ino_t ino;
218         int dirty;
219 };
220
221 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
222                                       unsigned int expandby)
223 {
224         struct ext2_xattr *new_attrs;
225         errcode_t err;
226
227         err = ext2fs_get_arrayzero(h->length + expandby,
228                                    sizeof(struct ext2_xattr), &new_attrs);
229         if (err)
230                 return err;
231
232         memcpy(new_attrs, h->attrs, h->length * sizeof(struct ext2_xattr));
233         ext2fs_free_mem(&h->attrs);
234         h->length += expandby;
235         h->attrs = new_attrs;
236
237         return 0;
238 }
239
240 struct ea_name_index {
241         int index;
242         const char *name;
243 };
244
245 /* Keep these names sorted in order of decreasing specificity. */
246 static struct ea_name_index ea_names[] = {
247         {3, "system.posix_acl_default"},
248         {2, "system.posix_acl_access"},
249         {8, "system.richacl"},
250         {6, "security."},
251         {4, "trusted."},
252         {7, "system."},
253         {1, "user."},
254         {0, NULL},
255 };
256
257 static void move_inline_data_to_front(struct ext2_xattr_handle *h)
258 {
259         struct ext2_xattr *x;
260         struct ext2_xattr tmp;
261
262         for (x = h->attrs + 1; x < h->attrs + h->length; x++) {
263                 if (!x->name)
264                         continue;
265
266                 if (strcmp(x->name, "system.data") == 0) {
267                         memcpy(&tmp, x, sizeof(tmp));
268                         memcpy(x, h->attrs, sizeof(tmp));
269                         memcpy(h->attrs, &tmp, sizeof(tmp));
270                         return;
271                 }
272         }
273 }
274
275 static const char *find_ea_prefix(int index)
276 {
277         struct ea_name_index *e;
278
279         for (e = ea_names; e->name; e++)
280                 if (e->index == index)
281                         return e->name;
282
283         return NULL;
284 }
285
286 static int find_ea_index(const char *fullname, char **name, int *index)
287 {
288         struct ea_name_index *e;
289
290         for (e = ea_names; e->name; e++) {
291                 if (memcmp(fullname, e->name, strlen(e->name)) == 0) {
292                         *name = (char *)fullname + strlen(e->name);
293                         *index = e->index;
294                         return 1;
295                 }
296         }
297         return 0;
298 }
299
300 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
301                                struct ext2_inode_large *inode)
302 {
303         struct ext2_ext_attr_header *header;
304         void *block_buf = NULL;
305         blk64_t blk;
306         errcode_t err;
307         struct ext2_inode_large i;
308
309         /* Read inode? */
310         if (inode == NULL) {
311                 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
312                                              sizeof(struct ext2_inode_large));
313                 if (err)
314                         return err;
315                 inode = &i;
316         }
317
318         /* Do we already have an EA block? */
319         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
320         if (blk == 0)
321                 return 0;
322
323         /* Find block, zero it, write back */
324         if ((blk < fs->super->s_first_data_block) ||
325             (blk >= ext2fs_blocks_count(fs->super))) {
326                 err = EXT2_ET_BAD_EA_BLOCK_NUM;
327                 goto out;
328         }
329
330         err = ext2fs_get_mem(fs->blocksize, &block_buf);
331         if (err)
332                 goto out;
333
334         err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
335         if (err)
336                 goto out2;
337
338         /* We only know how to deal with v2 EA blocks */
339         header = (struct ext2_ext_attr_header *) block_buf;
340         if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
341                 err = EXT2_ET_BAD_EA_HEADER;
342                 goto out2;
343         }
344
345         header->h_refcount--;
346         err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
347         if (err)
348                 goto out2;
349
350         /* Erase link to block */
351         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
352         if (header->h_refcount == 0)
353                 ext2fs_block_alloc_stats2(fs, blk, -1);
354         err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
355         if (err)
356                 goto out2;
357
358         /* Write inode? */
359         if (inode == &i) {
360                 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
361                                               sizeof(struct ext2_inode_large));
362                 if (err)
363                         goto out2;
364         }
365
366 out2:
367         ext2fs_free_mem(&block_buf);
368 out:
369         return err;
370 }
371
372 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
373                                          struct ext2_inode_large *inode)
374 {
375         struct ext2_ext_attr_header *header;
376         void *block_buf = NULL;
377         dgrp_t grp;
378         blk64_t blk, goal;
379         errcode_t err;
380
381         /* Do we already have an EA block? */
382         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
383         if (blk != 0) {
384                 if ((blk < fs->super->s_first_data_block) ||
385                     (blk >= ext2fs_blocks_count(fs->super))) {
386                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
387                         goto out;
388                 }
389
390                 err = ext2fs_get_mem(fs->blocksize, &block_buf);
391                 if (err)
392                         goto out;
393
394                 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
395                 if (err)
396                         goto out2;
397
398                 /* We only know how to deal with v2 EA blocks */
399                 header = (struct ext2_ext_attr_header *) block_buf;
400                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
401                         err = EXT2_ET_BAD_EA_HEADER;
402                         goto out2;
403                 }
404
405                 /* Single-user block.  We're done here. */
406                 if (header->h_refcount == 1)
407                         goto out2;
408
409                 /* We need to CoW the block. */
410                 header->h_refcount--;
411                 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
412                 if (err)
413                         goto out2;
414         } else {
415                 /* No block, we must increment i_blocks */
416                 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
417                                              1);
418                 if (err)
419                         goto out;
420         }
421
422         /* Allocate a block */
423         grp = ext2fs_group_of_ino(fs, ino);
424         goal = ext2fs_inode_table_loc(fs, grp);
425         err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
426         if (err)
427                 goto out2;
428         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
429 out2:
430         if (block_buf)
431                 ext2fs_free_mem(&block_buf);
432 out:
433         return err;
434 }
435
436
437 static errcode_t write_xattrs_to_buffer(struct ext2_xattr_handle *handle,
438                                         struct ext2_xattr **pos,
439                                         void *entries_start,
440                                         unsigned int storage_size,
441                                         unsigned int value_offset_correction)
442 {
443         struct ext2_xattr *x = *pos;
444         struct ext2_ext_attr_entry *e = entries_start;
445         void *end = entries_start + storage_size;
446         char *shortname;
447         unsigned int entry_size, value_size;
448         int idx, ret;
449
450         memset(entries_start, 0, storage_size);
451         /* For all remaining x...  */
452         for (; x < handle->attrs + handle->length; x++) {
453                 if (!x->name)
454                         continue;
455
456                 /* Calculate index and shortname position */
457                 shortname = x->name;
458                 ret = find_ea_index(x->name, &shortname, &idx);
459
460                 /* Calculate entry and value size */
461                 entry_size = (sizeof(*e) + strlen(shortname) +
462                               EXT2_EXT_ATTR_PAD - 1) &
463                              ~(EXT2_EXT_ATTR_PAD - 1);
464                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
465                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
466
467                 /*
468                  * Would entry collide with value?
469                  * Note that we must leave sufficient room for a (u32)0 to
470                  * mark the end of the entries.
471                  */
472                 if ((void *)e + entry_size + sizeof(__u32) > end - value_size)
473                         break;
474
475                 /* Fill out e appropriately */
476                 e->e_name_len = strlen(shortname);
477                 e->e_name_index = (ret ? idx : 0);
478                 e->e_value_offs = end - value_size - (void *)entries_start +
479                                 value_offset_correction;
480                 e->e_value_block = 0;
481                 e->e_value_size = x->value_len;
482
483                 /* Store name and value */
484                 end -= value_size;
485                 memcpy((void *)e + sizeof(*e), shortname, e->e_name_len);
486                 memcpy(end, x->value, e->e_value_size);
487
488                 e->e_hash = ext2fs_ext_attr_hash_entry(e, end);
489
490                 e = EXT2_EXT_ATTR_NEXT(e);
491                 *(__u32 *)e = 0;
492         }
493         *pos = x;
494
495         return 0;
496 }
497
498 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
499 {
500         struct ext2_xattr *x;
501         struct ext2_inode_large *inode;
502         void *start, *block_buf = NULL;
503         struct ext2_ext_attr_header *header;
504         __u32 ea_inode_magic;
505         blk64_t blk;
506         unsigned int storage_size;
507         unsigned int i;
508         errcode_t err;
509
510         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
511         i = EXT2_INODE_SIZE(handle->fs->super);
512         if (i < sizeof(*inode))
513                 i = sizeof(*inode);
514         err = ext2fs_get_memzero(i, &inode);
515         if (err)
516                 return err;
517
518         err = ext2fs_read_inode_full(handle->fs, handle->ino,
519                                      (struct ext2_inode *)inode,
520                                      EXT2_INODE_SIZE(handle->fs->super));
521         if (err)
522                 goto out;
523
524         move_inline_data_to_front(handle);
525
526         x = handle->attrs;
527         /* Does the inode have size for EA? */
528         if (EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
529                                                   inode->i_extra_isize +
530                                                   sizeof(__u32))
531                 goto write_ea_block;
532
533         /* Write the inode EA */
534         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
535         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
536                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
537         storage_size = EXT2_INODE_SIZE(handle->fs->super) -
538                 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
539                 sizeof(__u32);
540         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
541                 inode->i_extra_isize + sizeof(__u32);
542
543         err = write_xattrs_to_buffer(handle, &x, start, storage_size, 0);
544         if (err)
545                 goto out;
546
547         /* Are we done? */
548         if (x == handle->attrs + handle->length)
549                 goto skip_ea_block;
550
551 write_ea_block:
552         /* Write the EA block */
553         err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
554         if (err)
555                 goto out;
556
557         storage_size = handle->fs->blocksize -
558                 sizeof(struct ext2_ext_attr_header);
559         start = block_buf + sizeof(struct ext2_ext_attr_header);
560
561         err = write_xattrs_to_buffer(handle, &x, start, storage_size,
562                                      (void *)start - block_buf);
563         if (err)
564                 goto out2;
565
566         if (x < handle->attrs + handle->length) {
567                 err = EXT2_ET_EA_NO_SPACE;
568                 goto out2;
569         }
570
571         /* Write a header on the EA block */
572         header = block_buf;
573         header->h_magic = EXT2_EXT_ATTR_MAGIC;
574         header->h_refcount = 1;
575         header->h_blocks = 1;
576
577         /* Get a new block for writing */
578         err = prep_ea_block_for_write(handle->fs, handle->ino, inode);
579         if (err)
580                 goto out2;
581
582         /* Finally, write the new EA block */
583         blk = ext2fs_file_acl_block(handle->fs,
584                                     (struct ext2_inode *)inode);
585         err = ext2fs_write_ext_attr3(handle->fs, blk, block_buf,
586                                      handle->ino);
587         if (err)
588                 goto out2;
589
590 skip_ea_block:
591         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
592         if (!block_buf && blk) {
593                 /* xattrs shrunk, free the block */
594                 err = ext2fs_free_ext_attr(handle->fs, handle->ino, inode);
595                 if (err)
596                         goto out;
597         }
598
599         /* Write the inode */
600         err = ext2fs_write_inode_full(handle->fs, handle->ino,
601                                       (struct ext2_inode *)inode,
602                                       EXT2_INODE_SIZE(handle->fs->super));
603         if (err)
604                 goto out2;
605
606 out2:
607         ext2fs_free_mem(&block_buf);
608 out:
609         ext2fs_free_mem(&inode);
610         handle->dirty = 0;
611         return err;
612 }
613
614 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
615                                          struct ext2_ext_attr_entry *entries,
616                                          unsigned int storage_size,
617                                          void *value_start,
618                                          size_t *nr_read)
619 {
620         struct ext2_xattr *x;
621         struct ext2_ext_attr_entry *entry, *end;
622         const char *prefix;
623         unsigned int remain, prefix_len;
624         errcode_t err;
625         unsigned int values_size = storage_size +
626                         ((char *)entries - (char *)value_start);
627
628         x = handle->attrs;
629         while (x->name)
630                 x++;
631
632         /* find the end */
633         end = entries;
634         remain = storage_size;
635         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
636                !EXT2_EXT_IS_LAST_ENTRY(end)) {
637
638                 /* header eats this space */
639                 remain -= sizeof(struct ext2_ext_attr_entry);
640
641                 /* is attribute name valid? */
642                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
643                         return EXT2_ET_EA_BAD_NAME_LEN;
644
645                 /* attribute len eats this space */
646                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
647                 end = EXT2_EXT_ATTR_NEXT(end);
648         }
649
650         entry = entries;
651         remain = storage_size;
652         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
653                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
654                 __u32 hash;
655
656                 /* header eats this space */
657                 remain -= sizeof(struct ext2_ext_attr_entry);
658
659                 /* attribute len eats this space */
660                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
661
662                 /* check value size */
663                 if (entry->e_value_size > remain)
664                         return EXT2_ET_EA_BAD_VALUE_SIZE;
665
666                 if (entry->e_value_offs + entry->e_value_size > values_size)
667                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
668
669                 if (entry->e_value_size > 0 &&
670                     value_start + entry->e_value_offs <
671                     (void *)end + sizeof(__u32))
672                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
673
674                 /* e_value_block must be 0 in inode's ea */
675                 if (entry->e_value_block != 0)
676                         return EXT2_ET_BAD_EA_BLOCK_NUM;
677
678                 hash = ext2fs_ext_attr_hash_entry(entry, value_start +
679                                                          entry->e_value_offs);
680
681                 /* e_hash may be 0 in older inode's ea */
682                 if (entry->e_hash != 0 && entry->e_hash != hash)
683                         return EXT2_ET_BAD_EA_HASH;
684
685                 remain -= entry->e_value_size;
686
687                 /* Allocate space for more attrs? */
688                 if (x == handle->attrs + handle->length) {
689                         err = ext2fs_xattrs_expand(handle, 4);
690                         if (err)
691                                 return err;
692                         x = handle->attrs + handle->length - 4;
693                 }
694
695                 /* Extract name/value */
696                 prefix = find_ea_prefix(entry->e_name_index);
697                 prefix_len = (prefix ? strlen(prefix) : 0);
698                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
699                                          &x->name);
700                 if (err)
701                         return err;
702                 if (prefix)
703                         memcpy(x->name, prefix, prefix_len);
704                 if (entry->e_name_len)
705                         memcpy(x->name + prefix_len,
706                                (void *)entry + sizeof(*entry),
707                                entry->e_name_len);
708
709                 err = ext2fs_get_mem(entry->e_value_size, &x->value);
710                 if (err)
711                         return err;
712                 x->value_len = entry->e_value_size;
713                 memcpy(x->value, value_start + entry->e_value_offs,
714                        entry->e_value_size);
715                 x++;
716                 (*nr_read)++;
717                 entry = EXT2_EXT_ATTR_NEXT(entry);
718         }
719
720         return 0;
721 }
722
723 static void xattrs_free_keys(struct ext2_xattr_handle *h)
724 {
725         struct ext2_xattr *a = h->attrs;
726         size_t i;
727
728         for (i = 0; i < h->length; i++) {
729                 if (a[i].name)
730                         ext2fs_free_mem(&a[i].name);
731                 if (a[i].value)
732                         ext2fs_free_mem(&a[i].value);
733         }
734         h->count = 0;
735 }
736
737 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
738 {
739         struct ext2_inode_large *inode;
740         struct ext2_ext_attr_header *header;
741         __u32 ea_inode_magic;
742         unsigned int storage_size;
743         void *start, *block_buf = NULL;
744         blk64_t blk;
745         int i;
746         errcode_t err;
747
748         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
749         i = EXT2_INODE_SIZE(handle->fs->super);
750         if (i < sizeof(*inode))
751                 i = sizeof(*inode);
752         err = ext2fs_get_memzero(i, &inode);
753         if (err)
754                 return err;
755
756         err = ext2fs_read_inode_full(handle->fs, handle->ino,
757                                      (struct ext2_inode *)inode,
758                                      EXT2_INODE_SIZE(handle->fs->super));
759         if (err)
760                 goto out;
761
762         xattrs_free_keys(handle);
763
764         /* Does the inode have size for EA? */
765         if (EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
766                                                   inode->i_extra_isize +
767                                                   sizeof(__u32))
768                 goto read_ea_block;
769
770         /* Look for EA in the inode */
771         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
772                inode->i_extra_isize, sizeof(__u32));
773         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
774                 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
775                         EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
776                         sizeof(__u32);
777                 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
778                         inode->i_extra_isize + sizeof(__u32);
779
780                 err = read_xattrs_from_buffer(handle, start, storage_size,
781                                               start, &handle->count);
782                 if (err)
783                         goto out;
784         }
785
786 read_ea_block:
787         /* Look for EA in a separate EA block */
788         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
789         if (blk != 0) {
790                 if ((blk < handle->fs->super->s_first_data_block) ||
791                     (blk >= ext2fs_blocks_count(handle->fs->super))) {
792                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
793                         goto out;
794                 }
795
796                 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
797                 if (err)
798                         goto out;
799
800                 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
801                                             handle->ino);
802                 if (err)
803                         goto out3;
804
805                 /* We only know how to deal with v2 EA blocks */
806                 header = (struct ext2_ext_attr_header *) block_buf;
807                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
808                         err = EXT2_ET_BAD_EA_HEADER;
809                         goto out3;
810                 }
811
812                 /* Read EAs */
813                 storage_size = handle->fs->blocksize -
814                         sizeof(struct ext2_ext_attr_header);
815                 start = block_buf + sizeof(struct ext2_ext_attr_header);
816                 err = read_xattrs_from_buffer(handle, start, storage_size,
817                                               block_buf, &handle->count);
818                 if (err)
819                         goto out3;
820
821                 ext2fs_free_mem(&block_buf);
822         }
823
824         ext2fs_free_mem(&block_buf);
825         ext2fs_free_mem(&inode);
826         return 0;
827
828 out3:
829         ext2fs_free_mem(&block_buf);
830 out:
831         ext2fs_free_mem(&inode);
832         return err;
833 }
834
835 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
836                                 int (*func)(char *name, char *value,
837                                             size_t value_len, void *data),
838                                 void *data)
839 {
840         struct ext2_xattr *x;
841         int ret;
842
843         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
844         for (x = h->attrs; x < h->attrs + h->length; x++) {
845                 if (!x->name)
846                         continue;
847
848                 ret = func(x->name, x->value, x->value_len, data);
849                 if (ret & XATTR_CHANGED)
850                         h->dirty = 1;
851                 if (ret & XATTR_ABORT)
852                         return 0;
853         }
854
855         return 0;
856 }
857
858 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
859                            void **value, size_t *value_len)
860 {
861         struct ext2_xattr *x;
862         void *val;
863         errcode_t err;
864
865         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
866         for (x = h->attrs; x < h->attrs + h->length; x++) {
867                 if (!x->name)
868                         continue;
869
870                 if (strcmp(x->name, key) == 0) {
871                         err = ext2fs_get_mem(x->value_len, &val);
872                         if (err)
873                                 return err;
874                         memcpy(val, x->value, x->value_len);
875                         *value = val;
876                         *value_len = x->value_len;
877                         return 0;
878                 }
879         }
880
881         return EXT2_ET_EA_KEY_NOT_FOUND;
882 }
883
884 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
885                                       size_t *size)
886 {
887         struct ext2_ext_attr_entry *entry;
888         struct ext2_inode_large *inode;
889         __u32 ea_inode_magic;
890         unsigned int minoff;
891         void *start;
892         int i;
893         errcode_t err;
894
895         i = EXT2_INODE_SIZE(fs->super);
896         if (i < sizeof(*inode))
897                 i = sizeof(*inode);
898         err = ext2fs_get_memzero(i, &inode);
899         if (err)
900                 return err;
901
902         err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
903                                      EXT2_INODE_SIZE(fs->super));
904         if (err)
905                 goto out;
906
907         /* Does the inode have size for EA? */
908         if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
909                                                   inode->i_extra_isize +
910                                                   sizeof(__u32)) {
911                 err = EXT2_ET_INLINE_DATA_NO_SPACE;
912                 goto out;
913         }
914
915         minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
916         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
917                inode->i_extra_isize, sizeof(__u32));
918         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
919                 /* has xattrs.  calculate the size */
920                 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
921                         inode->i_extra_isize + sizeof(__u32);
922                 entry = start;
923                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
924                         if (!entry->e_value_block && entry->e_value_size) {
925                                 unsigned int offs = entry->e_value_offs;
926                                 if (offs < minoff)
927                                         minoff = offs;
928                         }
929                         entry = EXT2_EXT_ATTR_NEXT(entry);
930                 }
931                 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
932         } else {
933                 /* no xattr.  return a maximum size */
934                 *size = EXT2_EXT_ATTR_SIZE(minoff -
935                                            EXT2_EXT_ATTR_LEN(strlen("data")) -
936                                            EXT2_EXT_ATTR_ROUND - sizeof(__u32));
937         }
938
939 out:
940         ext2fs_free_mem(&inode);
941         return err;
942 }
943
944 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
945                            const char *key,
946                            const void *value,
947                            size_t value_len)
948 {
949         struct ext2_xattr *x, *last_empty;
950         char *new_value;
951         errcode_t err;
952
953         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
954         last_empty = NULL;
955         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
956                 if (!x->name) {
957                         last_empty = x;
958                         continue;
959                 }
960
961                 /* Replace xattr */
962                 if (strcmp(x->name, key) == 0) {
963                         err = ext2fs_get_mem(value_len, &new_value);
964                         if (err)
965                                 return err;
966                         memcpy(new_value, value, value_len);
967                         ext2fs_free_mem(&x->value);
968                         x->value = new_value;
969                         x->value_len = value_len;
970                         handle->dirty = 1;
971                         return 0;
972                 }
973         }
974
975         /* Add attr to empty slot */
976         if (last_empty) {
977                 err = ext2fs_get_mem(strlen(key) + 1, &last_empty->name);
978                 if (err)
979                         return err;
980                 strcpy(last_empty->name, key);
981
982                 err = ext2fs_get_mem(value_len, &last_empty->value);
983                 if (err)
984                         return err;
985                 memcpy(last_empty->value, value, value_len);
986                 last_empty->value_len = value_len;
987                 handle->dirty = 1;
988                 handle->count++;
989                 return 0;
990         }
991
992         /* Expand array, append slot */
993         err = ext2fs_xattrs_expand(handle, 4);
994         if (err)
995                 return err;
996
997         x = handle->attrs + handle->length - 4;
998         err = ext2fs_get_mem(strlen(key) + 1, &x->name);
999         if (err)
1000                 return err;
1001         strcpy(x->name, key);
1002
1003         err = ext2fs_get_mem(value_len, &x->value);
1004         if (err)
1005                 return err;
1006         memcpy(x->value, value, value_len);
1007         x->value_len = value_len;
1008         handle->dirty = 1;
1009         handle->count++;
1010         return 0;
1011 }
1012
1013 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1014                               const char *key)
1015 {
1016         struct ext2_xattr *x;
1017
1018         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1019         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
1020                 if (!x->name)
1021                         continue;
1022
1023                 if (strcmp(x->name, key) == 0) {
1024                         ext2fs_free_mem(&x->name);
1025                         ext2fs_free_mem(&x->value);
1026                         x->value_len = 0;
1027                         handle->dirty = 1;
1028                         handle->count--;
1029                         return 0;
1030                 }
1031         }
1032
1033         /* no key found, success! */
1034         return 0;
1035 }
1036
1037 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1038                              struct ext2_xattr_handle **handle)
1039 {
1040         struct ext2_xattr_handle *h;
1041         errcode_t err;
1042
1043         if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
1044                                      EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1045             !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1046                                      EXT4_FEATURE_INCOMPAT_INLINE_DATA))
1047                 return EXT2_ET_MISSING_EA_FEATURE;
1048
1049         err = ext2fs_get_memzero(sizeof(*h), &h);
1050         if (err)
1051                 return err;
1052
1053         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1054         h->length = 4;
1055         err = ext2fs_get_arrayzero(h->length, sizeof(struct ext2_xattr),
1056                                    &h->attrs);
1057         if (err) {
1058                 ext2fs_free_mem(&h);
1059                 return err;
1060         }
1061         h->count = 0;
1062         h->ino = ino;
1063         h->fs = fs;
1064         *handle = h;
1065         return 0;
1066 }
1067
1068 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1069 {
1070         struct ext2_xattr_handle *h = *handle;
1071         errcode_t err;
1072
1073         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1074         if (h->dirty) {
1075                 err = ext2fs_xattrs_write(h);
1076                 if (err)
1077                         return err;
1078         }
1079
1080         xattrs_free_keys(h);
1081         ext2fs_free_mem(&h->attrs);
1082         ext2fs_free_mem(handle);
1083         return 0;
1084 }
1085
1086 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1087 {
1088         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1089         *count = handle->count;
1090         return 0;
1091 }