Whamcloud - gitweb
libext2fs: zero hash in ibody extended attributes
[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 /* Push empty attributes to the end and inlinedata to the front. */
258 static int attr_compare(const void *a, const void *b)
259 {
260         const struct ext2_xattr *xa = a, *xb = b;
261
262         if (xa->name == NULL)
263                 return +1;
264         else if (xb->name == NULL)
265                 return -1;
266         else if (!strcmp(xa->name, "system.data"))
267                 return -1;
268         else if (!strcmp(xb->name, "system.data"))
269                 return +1;
270         return 0;
271 }
272
273 static const char *find_ea_prefix(int index)
274 {
275         struct ea_name_index *e;
276
277         for (e = ea_names; e->name; e++)
278                 if (e->index == index)
279                         return e->name;
280
281         return NULL;
282 }
283
284 static int find_ea_index(const char *fullname, char **name, int *index)
285 {
286         struct ea_name_index *e;
287
288         for (e = ea_names; e->name; e++) {
289                 if (memcmp(fullname, e->name, strlen(e->name)) == 0) {
290                         *name = (char *)fullname + strlen(e->name);
291                         *index = e->index;
292                         return 1;
293                 }
294         }
295         return 0;
296 }
297
298 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
299                                struct ext2_inode_large *inode)
300 {
301         struct ext2_ext_attr_header *header;
302         void *block_buf = NULL;
303         blk64_t blk;
304         errcode_t err;
305         struct ext2_inode_large i;
306
307         /* Read inode? */
308         if (inode == NULL) {
309                 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
310                                              sizeof(struct ext2_inode_large));
311                 if (err)
312                         return err;
313                 inode = &i;
314         }
315
316         /* Do we already have an EA block? */
317         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
318         if (blk == 0)
319                 return 0;
320
321         /* Find block, zero it, write back */
322         if ((blk < fs->super->s_first_data_block) ||
323             (blk >= ext2fs_blocks_count(fs->super))) {
324                 err = EXT2_ET_BAD_EA_BLOCK_NUM;
325                 goto out;
326         }
327
328         err = ext2fs_get_mem(fs->blocksize, &block_buf);
329         if (err)
330                 goto out;
331
332         err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
333         if (err)
334                 goto out2;
335
336         /* We only know how to deal with v2 EA blocks */
337         header = (struct ext2_ext_attr_header *) block_buf;
338         if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
339                 err = EXT2_ET_BAD_EA_HEADER;
340                 goto out2;
341         }
342
343         header->h_refcount--;
344         err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
345         if (err)
346                 goto out2;
347
348         /* Erase link to block */
349         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
350         if (header->h_refcount == 0)
351                 ext2fs_block_alloc_stats2(fs, blk, -1);
352         err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
353         if (err)
354                 goto out2;
355
356         /* Write inode? */
357         if (inode == &i) {
358                 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
359                                               sizeof(struct ext2_inode_large));
360                 if (err)
361                         goto out2;
362         }
363
364 out2:
365         ext2fs_free_mem(&block_buf);
366 out:
367         return err;
368 }
369
370 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
371                                          struct ext2_inode_large *inode)
372 {
373         struct ext2_ext_attr_header *header;
374         void *block_buf = NULL;
375         blk64_t blk, goal;
376         errcode_t err;
377
378         /* Do we already have an EA block? */
379         blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
380         if (blk != 0) {
381                 if ((blk < fs->super->s_first_data_block) ||
382                     (blk >= ext2fs_blocks_count(fs->super))) {
383                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
384                         goto out;
385                 }
386
387                 err = ext2fs_get_mem(fs->blocksize, &block_buf);
388                 if (err)
389                         goto out;
390
391                 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
392                 if (err)
393                         goto out2;
394
395                 /* We only know how to deal with v2 EA blocks */
396                 header = (struct ext2_ext_attr_header *) block_buf;
397                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
398                         err = EXT2_ET_BAD_EA_HEADER;
399                         goto out2;
400                 }
401
402                 /* Single-user block.  We're done here. */
403                 if (header->h_refcount == 1)
404                         goto out2;
405
406                 /* We need to CoW the block. */
407                 header->h_refcount--;
408                 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
409                 if (err)
410                         goto out2;
411         } else {
412                 /* No block, we must increment i_blocks */
413                 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
414                                              1);
415                 if (err)
416                         goto out;
417         }
418
419         /* Allocate a block */
420         goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
421         err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
422         if (err)
423                 goto out2;
424         ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
425 out2:
426         if (block_buf)
427                 ext2fs_free_mem(&block_buf);
428 out:
429         return err;
430 }
431
432
433 static errcode_t write_xattrs_to_buffer(struct ext2_xattr_handle *handle,
434                                         struct ext2_xattr **pos,
435                                         void *entries_start,
436                                         unsigned int storage_size,
437                                         unsigned int value_offset_correction,
438                                         int write_hash)
439 {
440         struct ext2_xattr *x = *pos;
441         struct ext2_ext_attr_entry *e = entries_start;
442         void *end = entries_start + storage_size;
443         char *shortname;
444         unsigned int entry_size, value_size;
445         int idx, ret;
446
447         memset(entries_start, 0, storage_size);
448         /* For all remaining x...  */
449         for (; x < handle->attrs + handle->length; x++) {
450                 if (!x->name)
451                         continue;
452
453                 /* Calculate index and shortname position */
454                 shortname = x->name;
455                 ret = find_ea_index(x->name, &shortname, &idx);
456
457                 /* Calculate entry and value size */
458                 entry_size = (sizeof(*e) + strlen(shortname) +
459                               EXT2_EXT_ATTR_PAD - 1) &
460                              ~(EXT2_EXT_ATTR_PAD - 1);
461                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
462                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
463
464                 /*
465                  * Would entry collide with value?
466                  * Note that we must leave sufficient room for a (u32)0 to
467                  * mark the end of the entries.
468                  */
469                 if ((void *)e + entry_size + sizeof(__u32) > end - value_size)
470                         break;
471
472                 /* Fill out e appropriately */
473                 e->e_name_len = strlen(shortname);
474                 e->e_name_index = (ret ? idx : 0);
475                 e->e_value_offs = end - value_size - (void *)entries_start +
476                                 value_offset_correction;
477                 e->e_value_block = 0;
478                 e->e_value_size = x->value_len;
479
480                 /* Store name and value */
481                 end -= value_size;
482                 memcpy((void *)e + sizeof(*e), shortname, e->e_name_len);
483                 memcpy(end, x->value, e->e_value_size);
484
485                 if (write_hash)
486                         e->e_hash = ext2fs_ext_attr_hash_entry(e, end);
487                 else
488                         e->e_hash = 0;
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         /* If extra_isize isn't set, we need to set it now */
525         if (inode->i_extra_isize == 0 &&
526             EXT2_INODE_SIZE(handle->fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
527                 char *p = (char *)inode;
528                 size_t extra = handle->fs->super->s_want_extra_isize;
529
530                 if (extra == 0)
531                         extra = sizeof(__u32);
532                 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
533                 inode->i_extra_isize = extra;
534         }
535
536         /*
537          * Force the inlinedata attr to the front and the empty entries
538          * to the end.
539          */
540         x = handle->attrs;
541         qsort(x, handle->length, sizeof(struct ext2_xattr), attr_compare);
542
543         /* Does the inode have space for EA? */
544         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
545             EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
546                                                   inode->i_extra_isize +
547                                                   sizeof(__u32))
548                 goto write_ea_block;
549
550         /* Write the inode EA */
551         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
552         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
553                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
554         storage_size = EXT2_INODE_SIZE(handle->fs->super) -
555                 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
556                 sizeof(__u32);
557         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
558                 inode->i_extra_isize + sizeof(__u32);
559
560         err = write_xattrs_to_buffer(handle, &x, start, storage_size, 0, 0);
561         if (err)
562                 goto out;
563
564 write_ea_block:
565         /* Are we done? */
566         if (x >= handle->attrs + handle->count)
567                 goto skip_ea_block;
568
569         /* Write the EA block */
570         err = ext2fs_get_memzero(handle->fs->blocksize, &block_buf);
571         if (err)
572                 goto out;
573
574         storage_size = handle->fs->blocksize -
575                 sizeof(struct ext2_ext_attr_header);
576         start = block_buf + sizeof(struct ext2_ext_attr_header);
577
578         err = write_xattrs_to_buffer(handle, &x, start, storage_size,
579                                      (void *)start - block_buf, 1);
580         if (err)
581                 goto out2;
582
583         if (x < handle->attrs + handle->length) {
584                 err = EXT2_ET_EA_NO_SPACE;
585                 goto out2;
586         }
587
588         /* Write a header on the EA block */
589         header = block_buf;
590         header->h_magic = EXT2_EXT_ATTR_MAGIC;
591         header->h_refcount = 1;
592         header->h_blocks = 1;
593
594         /* Get a new block for writing */
595         err = prep_ea_block_for_write(handle->fs, handle->ino, inode);
596         if (err)
597                 goto out2;
598
599         /* Finally, write the new EA block */
600         blk = ext2fs_file_acl_block(handle->fs,
601                                     (struct ext2_inode *)inode);
602         err = ext2fs_write_ext_attr3(handle->fs, blk, block_buf,
603                                      handle->ino);
604         if (err)
605                 goto out2;
606
607 skip_ea_block:
608         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
609         if (!block_buf && blk) {
610                 /* xattrs shrunk, free the block */
611                 err = ext2fs_free_ext_attr(handle->fs, handle->ino, inode);
612                 if (err)
613                         goto out;
614         }
615
616         /* Write the inode */
617         err = ext2fs_write_inode_full(handle->fs, handle->ino,
618                                       (struct ext2_inode *)inode,
619                                       EXT2_INODE_SIZE(handle->fs->super));
620         if (err)
621                 goto out2;
622
623 out2:
624         ext2fs_free_mem(&block_buf);
625 out:
626         ext2fs_free_mem(&inode);
627         handle->dirty = 0;
628         return err;
629 }
630
631 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
632                                          struct ext2_ext_attr_entry *entries,
633                                          unsigned int storage_size,
634                                          void *value_start,
635                                          size_t *nr_read)
636 {
637         struct ext2_xattr *x;
638         struct ext2_ext_attr_entry *entry, *end;
639         const char *prefix;
640         unsigned int remain, prefix_len;
641         errcode_t err;
642         unsigned int values_size = storage_size +
643                         ((char *)entries - (char *)value_start);
644
645         x = handle->attrs;
646         while (x->name)
647                 x++;
648
649         /* find the end */
650         end = entries;
651         remain = storage_size;
652         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
653                !EXT2_EXT_IS_LAST_ENTRY(end)) {
654
655                 /* header eats this space */
656                 remain -= sizeof(struct ext2_ext_attr_entry);
657
658                 /* is attribute name valid? */
659                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
660                         return EXT2_ET_EA_BAD_NAME_LEN;
661
662                 /* attribute len eats this space */
663                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
664                 end = EXT2_EXT_ATTR_NEXT(end);
665         }
666
667         entry = entries;
668         remain = storage_size;
669         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
670                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
671                 __u32 hash;
672
673                 /* header eats this space */
674                 remain -= sizeof(struct ext2_ext_attr_entry);
675
676                 /* attribute len eats this space */
677                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
678
679                 /* check value size */
680                 if (entry->e_value_size > remain)
681                         return EXT2_ET_EA_BAD_VALUE_SIZE;
682
683                 if (entry->e_value_offs + entry->e_value_size > values_size)
684                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
685
686                 if (entry->e_value_size > 0 &&
687                     value_start + entry->e_value_offs <
688                     (void *)end + sizeof(__u32))
689                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
690
691                 /* e_value_block must be 0 in inode's ea */
692                 if (entry->e_value_block != 0)
693                         return EXT2_ET_BAD_EA_BLOCK_NUM;
694
695                 hash = ext2fs_ext_attr_hash_entry(entry, value_start +
696                                                          entry->e_value_offs);
697
698                 /* e_hash may be 0 in older inode's ea */
699                 if (entry->e_hash != 0 && entry->e_hash != hash)
700                         return EXT2_ET_BAD_EA_HASH;
701
702                 remain -= entry->e_value_size;
703
704                 /* Allocate space for more attrs? */
705                 if (x == handle->attrs + handle->length) {
706                         err = ext2fs_xattrs_expand(handle, 4);
707                         if (err)
708                                 return err;
709                         x = handle->attrs + handle->length - 4;
710                 }
711
712                 /* Extract name/value */
713                 prefix = find_ea_prefix(entry->e_name_index);
714                 prefix_len = (prefix ? strlen(prefix) : 0);
715                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
716                                          &x->name);
717                 if (err)
718                         return err;
719                 if (prefix)
720                         memcpy(x->name, prefix, prefix_len);
721                 if (entry->e_name_len)
722                         memcpy(x->name + prefix_len,
723                                (void *)entry + sizeof(*entry),
724                                entry->e_name_len);
725
726                 err = ext2fs_get_mem(entry->e_value_size, &x->value);
727                 if (err)
728                         return err;
729                 x->value_len = entry->e_value_size;
730                 memcpy(x->value, value_start + entry->e_value_offs,
731                        entry->e_value_size);
732                 x++;
733                 (*nr_read)++;
734                 entry = EXT2_EXT_ATTR_NEXT(entry);
735         }
736
737         return 0;
738 }
739
740 static void xattrs_free_keys(struct ext2_xattr_handle *h)
741 {
742         struct ext2_xattr *a = h->attrs;
743         size_t i;
744
745         for (i = 0; i < h->length; i++) {
746                 if (a[i].name)
747                         ext2fs_free_mem(&a[i].name);
748                 if (a[i].value)
749                         ext2fs_free_mem(&a[i].value);
750         }
751         h->count = 0;
752 }
753
754 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
755 {
756         struct ext2_inode_large *inode;
757         struct ext2_ext_attr_header *header;
758         __u32 ea_inode_magic;
759         unsigned int storage_size;
760         void *start, *block_buf = NULL;
761         blk64_t blk;
762         int i;
763         errcode_t err;
764
765         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
766         i = EXT2_INODE_SIZE(handle->fs->super);
767         if (i < sizeof(*inode))
768                 i = sizeof(*inode);
769         err = ext2fs_get_memzero(i, &inode);
770         if (err)
771                 return err;
772
773         err = ext2fs_read_inode_full(handle->fs, handle->ino,
774                                      (struct ext2_inode *)inode,
775                                      EXT2_INODE_SIZE(handle->fs->super));
776         if (err)
777                 goto out;
778
779         xattrs_free_keys(handle);
780
781         /* Does the inode have space for EA? */
782         if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
783             EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
784                                                   inode->i_extra_isize +
785                                                   sizeof(__u32))
786                 goto read_ea_block;
787
788         /* Look for EA in the inode */
789         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
790                inode->i_extra_isize, sizeof(__u32));
791         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
792                 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
793                         EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
794                         sizeof(__u32);
795                 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
796                         inode->i_extra_isize + sizeof(__u32);
797
798                 err = read_xattrs_from_buffer(handle, start, storage_size,
799                                               start, &handle->count);
800                 if (err)
801                         goto out;
802         }
803
804 read_ea_block:
805         /* Look for EA in a separate EA block */
806         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
807         if (blk != 0) {
808                 if ((blk < handle->fs->super->s_first_data_block) ||
809                     (blk >= ext2fs_blocks_count(handle->fs->super))) {
810                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
811                         goto out;
812                 }
813
814                 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
815                 if (err)
816                         goto out;
817
818                 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
819                                             handle->ino);
820                 if (err)
821                         goto out3;
822
823                 /* We only know how to deal with v2 EA blocks */
824                 header = (struct ext2_ext_attr_header *) block_buf;
825                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
826                         err = EXT2_ET_BAD_EA_HEADER;
827                         goto out3;
828                 }
829
830                 /* Read EAs */
831                 storage_size = handle->fs->blocksize -
832                         sizeof(struct ext2_ext_attr_header);
833                 start = block_buf + sizeof(struct ext2_ext_attr_header);
834                 err = read_xattrs_from_buffer(handle, start, storage_size,
835                                               block_buf, &handle->count);
836                 if (err)
837                         goto out3;
838
839                 ext2fs_free_mem(&block_buf);
840         }
841
842         ext2fs_free_mem(&block_buf);
843         ext2fs_free_mem(&inode);
844         return 0;
845
846 out3:
847         ext2fs_free_mem(&block_buf);
848 out:
849         ext2fs_free_mem(&inode);
850         return err;
851 }
852
853 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
854                                 int (*func)(char *name, char *value,
855                                             size_t value_len, void *data),
856                                 void *data)
857 {
858         struct ext2_xattr *x;
859         int ret;
860
861         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
862         for (x = h->attrs; x < h->attrs + h->length; x++) {
863                 if (!x->name)
864                         continue;
865
866                 ret = func(x->name, x->value, x->value_len, data);
867                 if (ret & XATTR_CHANGED)
868                         h->dirty = 1;
869                 if (ret & XATTR_ABORT)
870                         return 0;
871         }
872
873         return 0;
874 }
875
876 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
877                            void **value, size_t *value_len)
878 {
879         struct ext2_xattr *x;
880         void *val;
881         errcode_t err;
882
883         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
884         for (x = h->attrs; x < h->attrs + h->length; x++) {
885                 if (!x->name)
886                         continue;
887
888                 if (strcmp(x->name, key) == 0) {
889                         err = ext2fs_get_mem(x->value_len, &val);
890                         if (err)
891                                 return err;
892                         memcpy(val, x->value, x->value_len);
893                         *value = val;
894                         *value_len = x->value_len;
895                         return 0;
896                 }
897         }
898
899         return EXT2_ET_EA_KEY_NOT_FOUND;
900 }
901
902 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
903                                       size_t *size)
904 {
905         struct ext2_ext_attr_entry *entry;
906         struct ext2_inode_large *inode;
907         __u32 ea_inode_magic;
908         unsigned int minoff;
909         void *start;
910         int i;
911         errcode_t err;
912
913         i = EXT2_INODE_SIZE(fs->super);
914         if (i < sizeof(*inode))
915                 i = sizeof(*inode);
916         err = ext2fs_get_memzero(i, &inode);
917         if (err)
918                 return err;
919
920         err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
921                                      EXT2_INODE_SIZE(fs->super));
922         if (err)
923                 goto out;
924
925         /* Does the inode have size for EA? */
926         if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
927                                                   inode->i_extra_isize +
928                                                   sizeof(__u32)) {
929                 err = EXT2_ET_INLINE_DATA_NO_SPACE;
930                 goto out;
931         }
932
933         minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
934         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
935                inode->i_extra_isize, sizeof(__u32));
936         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
937                 /* has xattrs.  calculate the size */
938                 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
939                         inode->i_extra_isize + sizeof(__u32);
940                 entry = start;
941                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
942                         if (!entry->e_value_block && entry->e_value_size) {
943                                 unsigned int offs = entry->e_value_offs;
944                                 if (offs < minoff)
945                                         minoff = offs;
946                         }
947                         entry = EXT2_EXT_ATTR_NEXT(entry);
948                 }
949                 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
950         } else {
951                 /* no xattr.  return a maximum size */
952                 *size = EXT2_EXT_ATTR_SIZE(minoff -
953                                            EXT2_EXT_ATTR_LEN(strlen("data")) -
954                                            EXT2_EXT_ATTR_ROUND - sizeof(__u32));
955         }
956
957 out:
958         ext2fs_free_mem(&inode);
959         return err;
960 }
961
962 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
963                            const char *key,
964                            const void *value,
965                            size_t value_len)
966 {
967         struct ext2_xattr *x, *last_empty;
968         char *new_value;
969         errcode_t err;
970
971         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
972         last_empty = NULL;
973         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
974                 if (!x->name) {
975                         last_empty = x;
976                         continue;
977                 }
978
979                 /* Replace xattr */
980                 if (strcmp(x->name, key) == 0) {
981                         err = ext2fs_get_mem(value_len, &new_value);
982                         if (err)
983                                 return err;
984                         memcpy(new_value, value, value_len);
985                         ext2fs_free_mem(&x->value);
986                         x->value = new_value;
987                         x->value_len = value_len;
988                         handle->dirty = 1;
989                         return 0;
990                 }
991         }
992
993         /* Add attr to empty slot */
994         if (last_empty) {
995                 err = ext2fs_get_mem(strlen(key) + 1, &last_empty->name);
996                 if (err)
997                         return err;
998                 strcpy(last_empty->name, key);
999
1000                 err = ext2fs_get_mem(value_len, &last_empty->value);
1001                 if (err)
1002                         return err;
1003                 memcpy(last_empty->value, value, value_len);
1004                 last_empty->value_len = value_len;
1005                 handle->dirty = 1;
1006                 handle->count++;
1007                 return 0;
1008         }
1009
1010         /* Expand array, append slot */
1011         err = ext2fs_xattrs_expand(handle, 4);
1012         if (err)
1013                 return err;
1014
1015         x = handle->attrs + handle->length - 4;
1016         err = ext2fs_get_mem(strlen(key) + 1, &x->name);
1017         if (err)
1018                 return err;
1019         strcpy(x->name, key);
1020
1021         err = ext2fs_get_mem(value_len, &x->value);
1022         if (err)
1023                 return err;
1024         memcpy(x->value, value, value_len);
1025         x->value_len = value_len;
1026         handle->dirty = 1;
1027         handle->count++;
1028         return 0;
1029 }
1030
1031 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1032                               const char *key)
1033 {
1034         struct ext2_xattr *x;
1035
1036         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1037         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
1038                 if (!x->name)
1039                         continue;
1040
1041                 if (strcmp(x->name, key) == 0) {
1042                         ext2fs_free_mem(&x->name);
1043                         ext2fs_free_mem(&x->value);
1044                         x->value_len = 0;
1045                         handle->dirty = 1;
1046                         handle->count--;
1047                         return 0;
1048                 }
1049         }
1050
1051         /* no key found, success! */
1052         return 0;
1053 }
1054
1055 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1056                              struct ext2_xattr_handle **handle)
1057 {
1058         struct ext2_xattr_handle *h;
1059         errcode_t err;
1060
1061         if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
1062                                      EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1063             !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1064                                      EXT4_FEATURE_INCOMPAT_INLINE_DATA))
1065                 return EXT2_ET_MISSING_EA_FEATURE;
1066
1067         err = ext2fs_get_memzero(sizeof(*h), &h);
1068         if (err)
1069                 return err;
1070
1071         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1072         h->length = 4;
1073         err = ext2fs_get_arrayzero(h->length, sizeof(struct ext2_xattr),
1074                                    &h->attrs);
1075         if (err) {
1076                 ext2fs_free_mem(&h);
1077                 return err;
1078         }
1079         h->count = 0;
1080         h->ino = ino;
1081         h->fs = fs;
1082         *handle = h;
1083         return 0;
1084 }
1085
1086 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1087 {
1088         struct ext2_xattr_handle *h = *handle;
1089         errcode_t err;
1090
1091         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1092         if (h->dirty) {
1093                 err = ext2fs_xattrs_write(h);
1094                 if (err)
1095                         return err;
1096         }
1097
1098         xattrs_free_keys(h);
1099         ext2fs_free_mem(&h->attrs);
1100         ext2fs_free_mem(handle);
1101         return 0;
1102 }
1103
1104 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1105 {
1106         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1107         *count = handle->count;
1108         return 0;
1109 }