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