Whamcloud - gitweb
libext2fs: avoid pointless EA block allocation
[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 {
439         struct ext2_xattr *x = *pos;
440         struct ext2_ext_attr_entry *e = entries_start;
441         void *end = entries_start + storage_size;
442         char *shortname;
443         unsigned int entry_size, value_size;
444         int idx, ret;
445
446         memset(entries_start, 0, storage_size);
447         /* For all remaining x...  */
448         for (; x < handle->attrs + handle->length; x++) {
449                 if (!x->name)
450                         continue;
451
452                 /* Calculate index and shortname position */
453                 shortname = x->name;
454                 ret = find_ea_index(x->name, &shortname, &idx);
455
456                 /* Calculate entry and value size */
457                 entry_size = (sizeof(*e) + strlen(shortname) +
458                               EXT2_EXT_ATTR_PAD - 1) &
459                              ~(EXT2_EXT_ATTR_PAD - 1);
460                 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
461                               EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
462
463                 /*
464                  * Would entry collide with value?
465                  * Note that we must leave sufficient room for a (u32)0 to
466                  * mark the end of the entries.
467                  */
468                 if ((void *)e + entry_size + sizeof(__u32) > end - value_size)
469                         break;
470
471                 /* Fill out e appropriately */
472                 e->e_name_len = strlen(shortname);
473                 e->e_name_index = (ret ? idx : 0);
474                 e->e_value_offs = end - value_size - (void *)entries_start +
475                                 value_offset_correction;
476                 e->e_value_block = 0;
477                 e->e_value_size = x->value_len;
478
479                 /* Store name and value */
480                 end -= value_size;
481                 memcpy((void *)e + sizeof(*e), shortname, e->e_name_len);
482                 memcpy(end, x->value, e->e_value_size);
483
484                 e->e_hash = ext2fs_ext_attr_hash_entry(e, end);
485
486                 e = EXT2_EXT_ATTR_NEXT(e);
487                 *(__u32 *)e = 0;
488         }
489         *pos = x;
490
491         return 0;
492 }
493
494 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
495 {
496         struct ext2_xattr *x;
497         struct ext2_inode_large *inode;
498         void *start, *block_buf = NULL;
499         struct ext2_ext_attr_header *header;
500         __u32 ea_inode_magic;
501         blk64_t blk;
502         unsigned int storage_size;
503         unsigned int i;
504         errcode_t err;
505
506         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
507         i = EXT2_INODE_SIZE(handle->fs->super);
508         if (i < sizeof(*inode))
509                 i = sizeof(*inode);
510         err = ext2fs_get_memzero(i, &inode);
511         if (err)
512                 return err;
513
514         err = ext2fs_read_inode_full(handle->fs, handle->ino,
515                                      (struct ext2_inode *)inode,
516                                      EXT2_INODE_SIZE(handle->fs->super));
517         if (err)
518                 goto out;
519
520         /* If extra_isize isn't set, we need to set it now */
521         if (inode->i_extra_isize == 0 &&
522             EXT2_INODE_SIZE(handle->fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
523                 char *p = (char *)inode;
524                 size_t extra = handle->fs->super->s_want_extra_isize;
525
526                 if (extra == 0)
527                         extra = sizeof(__u32);
528                 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
529                 inode->i_extra_isize = extra;
530         }
531
532         /*
533          * Force the inlinedata attr to the front and the empty entries
534          * to the end.
535          */
536         x = handle->attrs;
537         qsort(x, handle->length, sizeof(struct ext2_xattr), attr_compare);
538
539         /* Does the inode have size for EA? */
540         if (EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
541                                                   inode->i_extra_isize +
542                                                   sizeof(__u32))
543                 goto write_ea_block;
544
545         /* Write the inode EA */
546         ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
547         memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
548                inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
549         storage_size = EXT2_INODE_SIZE(handle->fs->super) -
550                 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
551                 sizeof(__u32);
552         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
553                 inode->i_extra_isize + sizeof(__u32);
554
555         err = write_xattrs_to_buffer(handle, &x, start, storage_size, 0);
556         if (err)
557                 goto out;
558
559 write_ea_block:
560         /* Are we done? */
561         if (x >= handle->attrs + handle->count)
562                 goto skip_ea_block;
563
564         /* Write the EA block */
565         err = ext2fs_get_memzero(handle->fs->blocksize, &block_buf);
566         if (err)
567                 goto out;
568
569         storage_size = handle->fs->blocksize -
570                 sizeof(struct ext2_ext_attr_header);
571         start = block_buf + sizeof(struct ext2_ext_attr_header);
572
573         err = write_xattrs_to_buffer(handle, &x, start, storage_size,
574                                      (void *)start - block_buf);
575         if (err)
576                 goto out2;
577
578         if (x < handle->attrs + handle->length) {
579                 err = EXT2_ET_EA_NO_SPACE;
580                 goto out2;
581         }
582
583         /* Write a header on the EA block */
584         header = block_buf;
585         header->h_magic = EXT2_EXT_ATTR_MAGIC;
586         header->h_refcount = 1;
587         header->h_blocks = 1;
588
589         /* Get a new block for writing */
590         err = prep_ea_block_for_write(handle->fs, handle->ino, inode);
591         if (err)
592                 goto out2;
593
594         /* Finally, write the new EA block */
595         blk = ext2fs_file_acl_block(handle->fs,
596                                     (struct ext2_inode *)inode);
597         err = ext2fs_write_ext_attr3(handle->fs, blk, block_buf,
598                                      handle->ino);
599         if (err)
600                 goto out2;
601
602 skip_ea_block:
603         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
604         if (!block_buf && blk) {
605                 /* xattrs shrunk, free the block */
606                 err = ext2fs_free_ext_attr(handle->fs, handle->ino, inode);
607                 if (err)
608                         goto out;
609         }
610
611         /* Write the inode */
612         err = ext2fs_write_inode_full(handle->fs, handle->ino,
613                                       (struct ext2_inode *)inode,
614                                       EXT2_INODE_SIZE(handle->fs->super));
615         if (err)
616                 goto out2;
617
618 out2:
619         ext2fs_free_mem(&block_buf);
620 out:
621         ext2fs_free_mem(&inode);
622         handle->dirty = 0;
623         return err;
624 }
625
626 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
627                                          struct ext2_ext_attr_entry *entries,
628                                          unsigned int storage_size,
629                                          void *value_start,
630                                          size_t *nr_read)
631 {
632         struct ext2_xattr *x;
633         struct ext2_ext_attr_entry *entry, *end;
634         const char *prefix;
635         unsigned int remain, prefix_len;
636         errcode_t err;
637         unsigned int values_size = storage_size +
638                         ((char *)entries - (char *)value_start);
639
640         x = handle->attrs;
641         while (x->name)
642                 x++;
643
644         /* find the end */
645         end = entries;
646         remain = storage_size;
647         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
648                !EXT2_EXT_IS_LAST_ENTRY(end)) {
649
650                 /* header eats this space */
651                 remain -= sizeof(struct ext2_ext_attr_entry);
652
653                 /* is attribute name valid? */
654                 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
655                         return EXT2_ET_EA_BAD_NAME_LEN;
656
657                 /* attribute len eats this space */
658                 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
659                 end = EXT2_EXT_ATTR_NEXT(end);
660         }
661
662         entry = entries;
663         remain = storage_size;
664         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
665                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
666                 __u32 hash;
667
668                 /* header eats this space */
669                 remain -= sizeof(struct ext2_ext_attr_entry);
670
671                 /* attribute len eats this space */
672                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
673
674                 /* check value size */
675                 if (entry->e_value_size > remain)
676                         return EXT2_ET_EA_BAD_VALUE_SIZE;
677
678                 if (entry->e_value_offs + entry->e_value_size > values_size)
679                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
680
681                 if (entry->e_value_size > 0 &&
682                     value_start + entry->e_value_offs <
683                     (void *)end + sizeof(__u32))
684                         return EXT2_ET_EA_BAD_VALUE_OFFSET;
685
686                 /* e_value_block must be 0 in inode's ea */
687                 if (entry->e_value_block != 0)
688                         return EXT2_ET_BAD_EA_BLOCK_NUM;
689
690                 hash = ext2fs_ext_attr_hash_entry(entry, value_start +
691                                                          entry->e_value_offs);
692
693                 /* e_hash may be 0 in older inode's ea */
694                 if (entry->e_hash != 0 && entry->e_hash != hash)
695                         return EXT2_ET_BAD_EA_HASH;
696
697                 remain -= entry->e_value_size;
698
699                 /* Allocate space for more attrs? */
700                 if (x == handle->attrs + handle->length) {
701                         err = ext2fs_xattrs_expand(handle, 4);
702                         if (err)
703                                 return err;
704                         x = handle->attrs + handle->length - 4;
705                 }
706
707                 /* Extract name/value */
708                 prefix = find_ea_prefix(entry->e_name_index);
709                 prefix_len = (prefix ? strlen(prefix) : 0);
710                 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
711                                          &x->name);
712                 if (err)
713                         return err;
714                 if (prefix)
715                         memcpy(x->name, prefix, prefix_len);
716                 if (entry->e_name_len)
717                         memcpy(x->name + prefix_len,
718                                (void *)entry + sizeof(*entry),
719                                entry->e_name_len);
720
721                 err = ext2fs_get_mem(entry->e_value_size, &x->value);
722                 if (err)
723                         return err;
724                 x->value_len = entry->e_value_size;
725                 memcpy(x->value, value_start + entry->e_value_offs,
726                        entry->e_value_size);
727                 x++;
728                 (*nr_read)++;
729                 entry = EXT2_EXT_ATTR_NEXT(entry);
730         }
731
732         return 0;
733 }
734
735 static void xattrs_free_keys(struct ext2_xattr_handle *h)
736 {
737         struct ext2_xattr *a = h->attrs;
738         size_t i;
739
740         for (i = 0; i < h->length; i++) {
741                 if (a[i].name)
742                         ext2fs_free_mem(&a[i].name);
743                 if (a[i].value)
744                         ext2fs_free_mem(&a[i].value);
745         }
746         h->count = 0;
747 }
748
749 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
750 {
751         struct ext2_inode_large *inode;
752         struct ext2_ext_attr_header *header;
753         __u32 ea_inode_magic;
754         unsigned int storage_size;
755         void *start, *block_buf = NULL;
756         blk64_t blk;
757         int i;
758         errcode_t err;
759
760         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
761         i = EXT2_INODE_SIZE(handle->fs->super);
762         if (i < sizeof(*inode))
763                 i = sizeof(*inode);
764         err = ext2fs_get_memzero(i, &inode);
765         if (err)
766                 return err;
767
768         err = ext2fs_read_inode_full(handle->fs, handle->ino,
769                                      (struct ext2_inode *)inode,
770                                      EXT2_INODE_SIZE(handle->fs->super));
771         if (err)
772                 goto out;
773
774         xattrs_free_keys(handle);
775
776         /* Does the inode have size for EA? */
777         if (EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
778                                                   inode->i_extra_isize +
779                                                   sizeof(__u32))
780                 goto read_ea_block;
781
782         /* Look for EA in the inode */
783         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
784                inode->i_extra_isize, sizeof(__u32));
785         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
786                 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
787                         EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
788                         sizeof(__u32);
789                 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
790                         inode->i_extra_isize + sizeof(__u32);
791
792                 err = read_xattrs_from_buffer(handle, start, storage_size,
793                                               start, &handle->count);
794                 if (err)
795                         goto out;
796         }
797
798 read_ea_block:
799         /* Look for EA in a separate EA block */
800         blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
801         if (blk != 0) {
802                 if ((blk < handle->fs->super->s_first_data_block) ||
803                     (blk >= ext2fs_blocks_count(handle->fs->super))) {
804                         err = EXT2_ET_BAD_EA_BLOCK_NUM;
805                         goto out;
806                 }
807
808                 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
809                 if (err)
810                         goto out;
811
812                 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
813                                             handle->ino);
814                 if (err)
815                         goto out3;
816
817                 /* We only know how to deal with v2 EA blocks */
818                 header = (struct ext2_ext_attr_header *) block_buf;
819                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
820                         err = EXT2_ET_BAD_EA_HEADER;
821                         goto out3;
822                 }
823
824                 /* Read EAs */
825                 storage_size = handle->fs->blocksize -
826                         sizeof(struct ext2_ext_attr_header);
827                 start = block_buf + sizeof(struct ext2_ext_attr_header);
828                 err = read_xattrs_from_buffer(handle, start, storage_size,
829                                               block_buf, &handle->count);
830                 if (err)
831                         goto out3;
832
833                 ext2fs_free_mem(&block_buf);
834         }
835
836         ext2fs_free_mem(&block_buf);
837         ext2fs_free_mem(&inode);
838         return 0;
839
840 out3:
841         ext2fs_free_mem(&block_buf);
842 out:
843         ext2fs_free_mem(&inode);
844         return err;
845 }
846
847 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
848                                 int (*func)(char *name, char *value,
849                                             size_t value_len, void *data),
850                                 void *data)
851 {
852         struct ext2_xattr *x;
853         int ret;
854
855         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
856         for (x = h->attrs; x < h->attrs + h->length; x++) {
857                 if (!x->name)
858                         continue;
859
860                 ret = func(x->name, x->value, x->value_len, data);
861                 if (ret & XATTR_CHANGED)
862                         h->dirty = 1;
863                 if (ret & XATTR_ABORT)
864                         return 0;
865         }
866
867         return 0;
868 }
869
870 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
871                            void **value, size_t *value_len)
872 {
873         struct ext2_xattr *x;
874         void *val;
875         errcode_t err;
876
877         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
878         for (x = h->attrs; x < h->attrs + h->length; x++) {
879                 if (!x->name)
880                         continue;
881
882                 if (strcmp(x->name, key) == 0) {
883                         err = ext2fs_get_mem(x->value_len, &val);
884                         if (err)
885                                 return err;
886                         memcpy(val, x->value, x->value_len);
887                         *value = val;
888                         *value_len = x->value_len;
889                         return 0;
890                 }
891         }
892
893         return EXT2_ET_EA_KEY_NOT_FOUND;
894 }
895
896 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
897                                       size_t *size)
898 {
899         struct ext2_ext_attr_entry *entry;
900         struct ext2_inode_large *inode;
901         __u32 ea_inode_magic;
902         unsigned int minoff;
903         void *start;
904         int i;
905         errcode_t err;
906
907         i = EXT2_INODE_SIZE(fs->super);
908         if (i < sizeof(*inode))
909                 i = sizeof(*inode);
910         err = ext2fs_get_memzero(i, &inode);
911         if (err)
912                 return err;
913
914         err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
915                                      EXT2_INODE_SIZE(fs->super));
916         if (err)
917                 goto out;
918
919         /* Does the inode have size for EA? */
920         if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
921                                                   inode->i_extra_isize +
922                                                   sizeof(__u32)) {
923                 err = EXT2_ET_INLINE_DATA_NO_SPACE;
924                 goto out;
925         }
926
927         minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
928         memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
929                inode->i_extra_isize, sizeof(__u32));
930         if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
931                 /* has xattrs.  calculate the size */
932                 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
933                         inode->i_extra_isize + sizeof(__u32);
934                 entry = start;
935                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
936                         if (!entry->e_value_block && entry->e_value_size) {
937                                 unsigned int offs = entry->e_value_offs;
938                                 if (offs < minoff)
939                                         minoff = offs;
940                         }
941                         entry = EXT2_EXT_ATTR_NEXT(entry);
942                 }
943                 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
944         } else {
945                 /* no xattr.  return a maximum size */
946                 *size = EXT2_EXT_ATTR_SIZE(minoff -
947                                            EXT2_EXT_ATTR_LEN(strlen("data")) -
948                                            EXT2_EXT_ATTR_ROUND - sizeof(__u32));
949         }
950
951 out:
952         ext2fs_free_mem(&inode);
953         return err;
954 }
955
956 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
957                            const char *key,
958                            const void *value,
959                            size_t value_len)
960 {
961         struct ext2_xattr *x, *last_empty;
962         char *new_value;
963         errcode_t err;
964
965         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
966         last_empty = NULL;
967         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
968                 if (!x->name) {
969                         last_empty = x;
970                         continue;
971                 }
972
973                 /* Replace xattr */
974                 if (strcmp(x->name, key) == 0) {
975                         err = ext2fs_get_mem(value_len, &new_value);
976                         if (err)
977                                 return err;
978                         memcpy(new_value, value, value_len);
979                         ext2fs_free_mem(&x->value);
980                         x->value = new_value;
981                         x->value_len = value_len;
982                         handle->dirty = 1;
983                         return 0;
984                 }
985         }
986
987         /* Add attr to empty slot */
988         if (last_empty) {
989                 err = ext2fs_get_mem(strlen(key) + 1, &last_empty->name);
990                 if (err)
991                         return err;
992                 strcpy(last_empty->name, key);
993
994                 err = ext2fs_get_mem(value_len, &last_empty->value);
995                 if (err)
996                         return err;
997                 memcpy(last_empty->value, value, value_len);
998                 last_empty->value_len = value_len;
999                 handle->dirty = 1;
1000                 handle->count++;
1001                 return 0;
1002         }
1003
1004         /* Expand array, append slot */
1005         err = ext2fs_xattrs_expand(handle, 4);
1006         if (err)
1007                 return err;
1008
1009         x = handle->attrs + handle->length - 4;
1010         err = ext2fs_get_mem(strlen(key) + 1, &x->name);
1011         if (err)
1012                 return err;
1013         strcpy(x->name, key);
1014
1015         err = ext2fs_get_mem(value_len, &x->value);
1016         if (err)
1017                 return err;
1018         memcpy(x->value, value, value_len);
1019         x->value_len = value_len;
1020         handle->dirty = 1;
1021         handle->count++;
1022         return 0;
1023 }
1024
1025 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1026                               const char *key)
1027 {
1028         struct ext2_xattr *x;
1029
1030         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1031         for (x = handle->attrs; x < handle->attrs + handle->length; x++) {
1032                 if (!x->name)
1033                         continue;
1034
1035                 if (strcmp(x->name, key) == 0) {
1036                         ext2fs_free_mem(&x->name);
1037                         ext2fs_free_mem(&x->value);
1038                         x->value_len = 0;
1039                         handle->dirty = 1;
1040                         handle->count--;
1041                         return 0;
1042                 }
1043         }
1044
1045         /* no key found, success! */
1046         return 0;
1047 }
1048
1049 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1050                              struct ext2_xattr_handle **handle)
1051 {
1052         struct ext2_xattr_handle *h;
1053         errcode_t err;
1054
1055         if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
1056                                      EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1057             !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1058                                      EXT4_FEATURE_INCOMPAT_INLINE_DATA))
1059                 return EXT2_ET_MISSING_EA_FEATURE;
1060
1061         err = ext2fs_get_memzero(sizeof(*h), &h);
1062         if (err)
1063                 return err;
1064
1065         h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1066         h->length = 4;
1067         err = ext2fs_get_arrayzero(h->length, sizeof(struct ext2_xattr),
1068                                    &h->attrs);
1069         if (err) {
1070                 ext2fs_free_mem(&h);
1071                 return err;
1072         }
1073         h->count = 0;
1074         h->ino = ino;
1075         h->fs = fs;
1076         *handle = h;
1077         return 0;
1078 }
1079
1080 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1081 {
1082         struct ext2_xattr_handle *h = *handle;
1083         errcode_t err;
1084
1085         EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1086         if (h->dirty) {
1087                 err = ext2fs_xattrs_write(h);
1088                 if (err)
1089                         return err;
1090         }
1091
1092         xattrs_free_keys(h);
1093         ext2fs_free_mem(&h->attrs);
1094         ext2fs_free_mem(handle);
1095         return 0;
1096 }
1097
1098 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1099 {
1100         EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1101         *count = handle->count;
1102         return 0;
1103 }