f498f8f688b89b4e78d4a25b80e60b9381cdc142
[lwext4.git] / lwext4 / ext4_xattr.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4_xattr.c
34  * @brief Extended Attribute manipulation.
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_types.h"
39 #include "ext4_fs.h"
40 #include "ext4_errno.h"
41 #include "ext4_blockdev.h"
42 #include "ext4_super.h"
43 #include "ext4_debug.h"
44 #include "ext4_block_group.h"
45 #include "ext4_balloc.h"
46 #include "ext4_inode.h"
47 #include "ext4_extent.h"
48
49 #include <string.h>
50 #include <stdlib.h>
51
52 /**
53  * @file  ext4_xattr.c
54  * @brief Extended Attribute Manipulation
55  */
56
57 #define NAME_HASH_SHIFT 5
58 #define VALUE_HASH_SHIFT 16
59
60 static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header,
61                                            struct ext4_xattr_entry *entry)
62 {
63         uint32_t hash = 0;
64         char *name = EXT4_XATTR_NAME(entry);
65         int n;
66
67         for (n = 0; n < entry->e_name_len; n++) {
68                 hash = (hash << NAME_HASH_SHIFT) ^
69                        (hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++;
70         }
71
72         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
73                 uint32_t *value =
74                     (uint32_t *)((char *)header + to_le16(entry->e_value_offs));
75                 for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >>
76                          EXT4_XATTR_PAD_BITS;
77                      n; n--) {
78                         hash = (hash << VALUE_HASH_SHIFT) ^
79                                (hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^
80                                to_le32(*value++);
81                 }
82         }
83         entry->e_hash = to_le32(hash);
84 }
85
86 #define BLOCK_HASH_SHIFT 16
87
88 /*
89  * ext4_xattr_rehash()
90  *
91  * Re-compute the extended attribute hash value after an entry has changed.
92  */
93 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
94                               struct ext4_xattr_entry *entry)
95 {
96         struct ext4_xattr_entry *here;
97         uint32_t hash = 0;
98
99         ext4_xattr_compute_hash(header, entry);
100         here = EXT4_XATTR_ENTRY(header + 1);
101         while (!EXT4_XATTR_IS_LAST_ENTRY(here)) {
102                 if (!here->e_hash) {
103                         /* Block is not shared if an entry's hash value == 0 */
104                         hash = 0;
105                         break;
106                 }
107                 hash = (hash << BLOCK_HASH_SHIFT) ^
108                        (hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^
109                        to_le32(here->e_hash);
110                 here = EXT4_XATTR_NEXT(here);
111         }
112         header->h_hash = to_le32(hash);
113 }
114
115 static int ext4_xattr_item_cmp(struct ext4_xattr_item *a,
116                                struct ext4_xattr_item *b)
117 {
118         int result;
119         result = a->name_index - b->name_index;
120         if (result)
121                 return result;
122
123         result = a->name_len - b->name_len;
124         if (result)
125                 return result;
126
127         return memcmp(a->name, b->name, a->name_len);
128 }
129
130 RB_GENERATE_INTERNAL(ext4_xattr_tree, ext4_xattr_item, node,
131                      ext4_xattr_item_cmp, static inline)
132
133 static struct ext4_xattr_item *
134 ext4_xattr_item_alloc(uint8_t name_index, char *name, size_t name_len)
135 {
136         struct ext4_xattr_item *item;
137         item = malloc(sizeof(struct ext4_xattr_item) + name_len);
138         if (!item)
139                 return NULL;
140
141         item->name_index = name_index;
142         item->name = (char *)(item + 1);
143         item->name_len = name_len;
144         item->data = NULL;
145         item->data_size = 0;
146
147         memset(&item->node, 0, sizeof(item->node));
148         memcpy(item->name, name, name_len);
149
150         return item;
151 }
152
153 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
154                                       void *orig_data, size_t data_size)
155 {
156         void *data = NULL;
157         ext4_assert(!item->data);
158         data = malloc(data_size);
159         if (!data)
160                 return ENOMEM;
161
162         if (orig_data)
163                 memcpy(data, orig_data, data_size);
164
165         item->data = data;
166         item->data_size = data_size;
167         return EOK;
168 }
169
170 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
171 {
172         ext4_assert(item->data);
173         free(item->data);
174         item->data = NULL;
175         item->data_size = 0;
176 }
177
178 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
179                                        size_t new_data_size)
180 {
181         if (new_data_size != item->data_size) {
182                 void *new_data;
183                 new_data = realloc(item->data, new_data_size);
184                 if (!new_data)
185                         return ENOMEM;
186
187                 item->data = new_data;
188                 item->data_size = new_data_size;
189         }
190         return EOK;
191 }
192
193 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
194 {
195         if (item->data)
196                 ext4_xattr_item_free_data(item);
197
198         free(item);
199 }
200
201 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
202                                    struct ext4_xattr_entry *entry,
203                                    bool in_inode)
204 {
205         void *ret;
206         if (in_inode) {
207                 struct ext4_xattr_ibody_header *header;
208                 struct ext4_xattr_entry *first_entry;
209                 int16_t inode_size =
210                     ext4_get16(&xattr_ref->fs->sb, inode_size);
211                 header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
212                 first_entry = EXT4_XATTR_IFIRST(header);
213
214                 ret = (void *)((char *)first_entry +
215                                to_le16(entry->e_value_offs));
216                 if ((char *)ret +
217                         EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
218                         (char *)xattr_ref->inode_ref->inode >
219                     inode_size)
220                         ret = NULL;
221
222         } else {
223                 int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
224                 ret = (void *)((char *)xattr_ref->block.data +
225                                to_le16(entry->e_value_offs));
226                 if ((char *)ret +
227                         EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
228                         (char *)xattr_ref->block.data >
229                     block_size)
230                         ret = NULL;
231         }
232         return ret;
233 }
234
235 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
236 {
237         int ret = EOK;
238         size_t size_rem;
239         void *data;
240         struct ext4_xattr_entry *entry = NULL;
241
242         ext4_assert(xattr_ref->block.data);
243         entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
244
245         size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
246         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
247              entry = EXT4_XATTR_NEXT(entry),
248              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
249                 struct ext4_xattr_item *item;
250                 char *e_name = EXT4_XATTR_NAME(entry);
251
252                 data = ext4_xattr_entry_data(xattr_ref, entry, false);
253                 if (!data) {
254                         ret = EIO;
255                         goto Finish;
256                 }
257
258                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
259                                              (size_t)entry->e_name_len);
260                 if (!item) {
261                         ret = ENOMEM;
262                         goto Finish;
263                 }
264                 if (ext4_xattr_item_alloc_data(
265                         item, data, to_le32(entry->e_value_size)) != EOK) {
266                         ext4_xattr_item_free(item);
267                         ret = ENOMEM;
268                         goto Finish;
269                 }
270                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
271                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
272                                       EXT4_XATTR_LEN(item->name_len);
273         }
274
275 Finish:
276         return ret;
277 }
278
279 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
280 {
281         void *data;
282         size_t size_rem;
283         int ret = EOK;
284         struct ext4_xattr_ibody_header *header = NULL;
285         struct ext4_xattr_entry *entry = NULL;
286         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
287
288         header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
289         entry = EXT4_XATTR_IFIRST(header);
290
291         size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
292                    xattr_ref->inode_ref->inode->extra_isize;
293         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
294              entry = EXT4_XATTR_NEXT(entry),
295              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
296                 struct ext4_xattr_item *item;
297                 char *e_name = EXT4_XATTR_NAME(entry);
298
299                 data = ext4_xattr_entry_data(xattr_ref, entry, true);
300                 if (!data) {
301                         ret = EIO;
302                         goto Finish;
303                 }
304
305                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
306                                              (size_t)entry->e_name_len);
307                 if (!item) {
308                         ret = ENOMEM;
309                         goto Finish;
310                 }
311                 if (ext4_xattr_item_alloc_data(
312                         item, data, to_le32(entry->e_value_size)) != EOK) {
313                         ext4_xattr_item_free(item);
314                         ret = ENOMEM;
315                         goto Finish;
316                 }
317                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
318                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
319                                       EXT4_XATTR_LEN(item->name_len);
320         }
321
322 Finish:
323         return ret;
324 }
325
326 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
327 {
328         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
329         uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
330                             xattr_ref->inode_ref->inode->extra_isize;
331         return size_rem;
332 }
333
334 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
335 {
336         return ext4_sb_get_block_size(&xattr_ref->fs->sb);
337 }
338
339 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
340 {
341         int ret = EOK;
342         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
343         if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
344                 ret = ext4_xattr_inode_fetch(xattr_ref);
345                 if (ret != EOK)
346                         return ret;
347         }
348
349         if (xattr_ref->block_loaded)
350                 ret = ext4_xattr_block_fetch(xattr_ref);
351
352         xattr_ref->dirty = false;
353         return ret;
354 }
355
356 static struct ext4_xattr_item *
357 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
358                        char *name, size_t name_len)
359 {
360         struct ext4_xattr_item tmp, *ret;
361         tmp.name_index = name_index;
362         tmp.name = name;
363         tmp.name_len = name_len;
364         ret = RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
365         return ret;
366 }
367
368 static struct ext4_xattr_item *
369 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
370                        char *name, size_t name_len, void *data,
371                        size_t data_size)
372 {
373         struct ext4_xattr_item *item;
374         item = ext4_xattr_item_alloc(name_index, name, name_len);
375         if (!item)
376                 return NULL;
377
378         if (xattr_ref->ea_size + EXT4_XATTR_SIZE(item->data_size) +
379                 EXT4_XATTR_LEN(item->name_len) >
380             ext4_xattr_inode_space(xattr_ref) +
381                 ext4_xattr_block_space(xattr_ref)) {
382                 ext4_xattr_item_free(item);
383                 return NULL;
384         }
385         if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
386                 ext4_xattr_item_free(item);
387                 return NULL;
388         }
389         RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
390         xattr_ref->ea_size +=
391             EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
392         xattr_ref->dirty = true;
393         return item;
394 }
395
396 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
397                                   uint8_t name_index, char *name,
398                                   size_t name_len)
399 {
400         int ret = ENOENT;
401         struct ext4_xattr_item *item =
402             ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
403         if (item) {
404                 if (item == xattr_ref->iter_from)
405                         xattr_ref->iter_from =
406                             RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
407
408                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
409                 ext4_xattr_item_free(item);
410                 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
411                                       EXT4_XATTR_LEN(item->name_len);
412                 xattr_ref->dirty = true;
413                 ret = EOK;
414         }
415         return ret;
416 }
417
418 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
419                                   struct ext4_xattr_item *item,
420                                   size_t new_data_size)
421 {
422         int ret = EOK;
423         if (xattr_ref->ea_size - EXT4_XATTR_SIZE(item->data_size) +
424                 EXT4_XATTR_SIZE(new_data_size) >
425             ext4_xattr_inode_space(xattr_ref) +
426                 ext4_xattr_block_space(xattr_ref)) {
427
428                 return ENOSPC;
429         }
430         ret = ext4_xattr_item_resize_data(item, new_data_size);
431         if (ret != EOK) {
432                 return ret;
433         }
434         xattr_ref->ea_size -=
435             EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_SIZE(new_data_size);
436         xattr_ref->dirty = true;
437         return ret;
438 }
439
440 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
441 {
442         struct ext4_xattr_item *item, *save_item;
443         uint64_t xattr_block = ext4_inode_get_file_acl(
444             xattr_ref->inode_ref->inode, &xattr_ref->fs->sb);
445         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
446         {
447                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
448                 ext4_xattr_item_free(item);
449         }
450         xattr_ref->ea_size = 0;
451         if (xattr_block)
452                 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
453
454         if (ext4_xattr_inode_space(xattr_ref) >
455             sizeof(struct ext4_xattr_ibody_header))
456                 xattr_ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
457 }
458
459 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
460 {
461         int ret = EOK;
462
463         uint64_t xattr_block = 0;
464         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
465                                               &xattr_ref->fs->sb);
466         if (!xattr_block) {
467                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
468                                               (uint32_t *)&xattr_block);
469                 if (ret != EOK)
470                         goto Finish;
471
472                 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
473                                      xattr_block);
474                 if (ret != EOK) {
475                         ext4_balloc_free_block(xattr_ref->inode_ref,
476                                                xattr_block);
477                         goto Finish;
478                 }
479
480                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
481                                         &xattr_ref->fs->sb, xattr_block);
482                 xattr_ref->inode_ref->dirty = true;
483                 xattr_ref->block_loaded = true;
484                 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
485         }
486
487 Finish:
488         return ret;
489 }
490
491 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
492 {
493         uint64_t xattr_block;
494         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
495                                               &xattr_ref->fs->sb);
496         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
497                                 0);
498         ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
499         ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
500         xattr_ref->inode_ref->dirty = true;
501         xattr_ref->block_loaded = false;
502         xattr_ref->ea_size -= sizeof(struct ext4_xattr_header);
503 }
504
505 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
506 {
507         struct ext4_xattr_header *block_header = NULL;
508         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
509
510         memset(block_header, 0, sizeof(struct ext4_xattr_header));
511         block_header->h_magic = EXT4_XATTR_MAGIC;
512         block_header->h_refcount = to_le32(1);
513         block_header->h_blocks = to_le32(1);
514 }
515
516 static void
517 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
518                            struct ext4_xattr_ibody_header *ibody_header,
519                            struct ext4_xattr_entry *entry, void *ibody_data_ptr)
520 {
521         entry->e_name_len = to_le32(item->name_len);
522         entry->e_name_index = item->name_index;
523         entry->e_value_offs =
524             (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
525         entry->e_value_block = 0;
526         entry->e_value_size = item->data_size;
527 }
528
529 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
530                                        struct ext4_xattr_header *block_header,
531                                        struct ext4_xattr_entry *block_entry,
532                                        void *block_data_ptr)
533 {
534         block_entry->e_name_len = to_le32(item->name_len);
535         block_entry->e_name_index = item->name_index;
536         block_entry->e_value_offs =
537             (char *)block_data_ptr - (char *)block_header;
538         block_entry->e_value_block = 0;
539         block_entry->e_value_size = item->data_size;
540 }
541
542 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
543 {
544         int ret = EOK;
545         bool block_modified = false;
546         void *ibody_data = 0;
547         void *block_data = 0;
548         struct ext4_xattr_item *item, *save_item;
549         size_t inode_size_rem, block_size_rem;
550         struct ext4_xattr_ibody_header *ibody_header = NULL;
551         struct ext4_xattr_header *block_header = NULL;
552         struct ext4_xattr_entry *entry = NULL;
553         struct ext4_xattr_entry *block_entry = NULL;
554
555         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
556         block_size_rem = ext4_xattr_block_space(xattr_ref);
557         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
558                 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
559                 entry = EXT4_XATTR_IFIRST(ibody_header);
560         }
561
562         if (!xattr_ref->dirty)
563                 goto Finish;
564         /* If there are enough spaces in the ibody EA table.*/
565         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
566                 memset(ibody_header, 0, inode_size_rem);
567                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
568                 ibody_data = (char *)ibody_header + inode_size_rem;
569                 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
570
571                 xattr_ref->inode_ref->dirty = true;
572         }
573         /* If we need an extra block to hold the EA entries*/
574         if (xattr_ref->ea_size > inode_size_rem) {
575                 if (!xattr_ref->block_loaded) {
576                         ret = ext4_xattr_try_alloc_block(xattr_ref);
577                         if (ret != EOK)
578                                 goto Finish;
579                 }
580                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
581                 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
582                 ext4_xattr_set_block_header(xattr_ref);
583                 block_data = (char *)block_header + block_size_rem;
584                 block_size_rem -= sizeof(struct ext4_xattr_header);
585
586                 xattr_ref->block.dirty = true;
587         } else {
588                 /* We don't need an extra block.*/
589                 if (xattr_ref->block_loaded) {
590                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
591                         block_header->h_refcount =
592                             to_le32(to_le32(block_header->h_refcount) - 1);
593                         if (!block_header->h_refcount) {
594                                 ext4_xattr_try_free_block(xattr_ref);
595                                 block_header = NULL;
596                         } else {
597                                 block_entry =
598                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
599                                 block_data =
600                                     (char *)block_header + block_size_rem;
601                                 block_size_rem -=
602                                     sizeof(struct ext4_xattr_header);
603                                 ext4_inode_set_file_acl(
604                                     xattr_ref->inode_ref->inode,
605                                     &xattr_ref->fs->sb, 0);
606
607                                 xattr_ref->inode_ref->dirty = true;
608                                 xattr_ref->block.dirty = true;
609                         }
610                 }
611         }
612         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
613         {
614                 if (EXT4_XATTR_SIZE(item->data_size) +
615                         EXT4_XATTR_LEN(item->name_len) <=
616                     inode_size_rem) {
617                         ibody_data = (char *)ibody_data -
618                                      EXT4_XATTR_SIZE(item->data_size);
619                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
620                                                    ibody_data);
621                         memcpy(EXT4_XATTR_NAME(entry), item->name,
622                                item->name_len);
623                         memcpy(ibody_data, item->data, item->data_size);
624                         entry = EXT4_XATTR_NEXT(entry);
625                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
626                                           EXT4_XATTR_LEN(item->name_len);
627
628                         xattr_ref->inode_ref->dirty = true;
629                         continue;
630                 }
631                 if (EXT4_XATTR_SIZE(item->data_size) +
632                         EXT4_XATTR_LEN(item->name_len) >
633                     block_size_rem) {
634                         ret = ENOSPC;
635                         goto Finish;
636                 }
637                 block_data =
638                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
639                 ext4_xattr_set_block_entry(item, block_header, block_entry,
640                                            block_data);
641                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
642                        item->name_len);
643                 memcpy(block_data, item->data, item->data_size);
644                 block_entry = EXT4_XATTR_NEXT(block_entry);
645                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
646                                   EXT4_XATTR_LEN(item->name_len);
647
648                 block_modified = true;
649         }
650         xattr_ref->dirty = false;
651         if (block_modified) {
652                 ext4_xattr_rehash(block_header,
653                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
654                 xattr_ref->block.dirty = true;
655         }
656
657 Finish:
658         return ret;
659 }
660
661 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
662                            int(iter)(struct ext4_xattr_ref *ref,
663                                      struct ext4_xattr_item *item))
664 {
665         struct ext4_xattr_item *item;
666         if (!ref->iter_from)
667                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
668
669         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
670         {
671                 int ret = EXT4_XATTR_ITERATE_CONT;
672                 if (iter)
673                         iter(ref, item);
674
675                 if (ret != EXT4_XATTR_ITERATE_CONT) {
676                         if (ret == EXT4_XATTR_ITERATE_STOP)
677                                 ref->iter_from = NULL;
678
679                         break;
680                 }
681         }
682 }
683
684 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
685 {
686         ref->iter_from = NULL;
687 }
688
689 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
690                       char *name, size_t name_len, void *data, size_t data_size,
691                       bool replace)
692 {
693         int ret = EOK;
694         struct ext4_xattr_item *item =
695             ext4_xattr_lookup_item(ref, name_index, name, name_len);
696         if (replace) {
697                 if (!item) {
698                         ret = ENOATTR;
699                         goto Finish;
700                 }
701                 if (item->data_size != data_size)
702                         ret = ext4_xattr_resize_item(ref, item, data_size);
703
704                 if (ret != EOK) {
705                         goto Finish;
706                 }
707                 memcpy(item->data, data, data_size);
708         } else {
709                 if (item) {
710                         ret = EEXIST;
711                         goto Finish;
712                 }
713                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
714                                               data, data_size);
715                 if (!item)
716                         ret = ENOMEM;
717         }
718 Finish:
719         return ret;
720 }
721
722 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
723                          char *name, size_t name_len)
724 {
725         return ext4_xattr_remove_item(ref, name_index, name, name_len);
726 }
727
728 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
729                       char *name, size_t name_len, void *buf, size_t buf_size,
730                       size_t *size_got)
731 {
732         int ret = EOK;
733         size_t item_size = 0;
734         struct ext4_xattr_item *item =
735             ext4_xattr_lookup_item(ref, name_index, name, name_len);
736
737         if (!item) {
738                 ret = ENOATTR;
739                 goto Finish;
740         }
741         item_size = item->data_size;
742         if (buf_size > item_size)
743                 buf_size = item_size;
744
745         if (buf)
746                 memcpy(buf, item->data, buf_size);
747
748 Finish:
749         if (size_got)
750                 *size_got = buf_size;
751
752         return ret;
753 }
754
755 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
756                           struct ext4_xattr_ref *ref)
757 {
758         int rc;
759         uint64_t xattr_block;
760         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
761         RB_INIT(&ref->root);
762         ref->ea_size = 0;
763         ref->iter_from = NULL;
764         if (xattr_block) {
765                 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
766                 if (rc != EOK)
767                         return EIO;
768
769                 ref->ea_size += sizeof(struct ext4_xattr_header);
770                 ref->block_loaded = true;
771         } else
772                 ref->block_loaded = false;
773
774         ref->inode_ref = inode_ref;
775         ref->fs = fs;
776
777         if (ext4_xattr_inode_space(ref) >
778             sizeof(struct ext4_xattr_ibody_header))
779                 ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
780
781         rc = ext4_xattr_fetch(ref);
782         if (rc != EOK) {
783                 ext4_xattr_purge_items(ref);
784                 if (xattr_block)
785                         ext4_block_set(fs->bdev, &inode_ref->block);
786
787                 ref->block_loaded = false;
788                 return rc;
789         }
790         return EOK;
791 }
792
793 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
794 {
795         ext4_xattr_write_to_disk(ref);
796         if (ref->block_loaded) {
797                 ext4_block_set(ref->fs->bdev, &ref->block);
798                 ref->block_loaded = false;
799         }
800         ext4_xattr_purge_items(ref);
801         ref->inode_ref = NULL;
802         ref->fs = NULL;
803 }
804
805 struct xattr_prefix {
806         char *prefix;
807         uint8_t name_index;
808 };
809
810 static const struct xattr_prefix prefix_tbl[] = {
811     {"user.", EXT4_XATTR_INDEX_USER},
812     {"system.", EXT4_XATTR_INDEX_SYSTEM},
813     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
814     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
815     {NULL, 0},
816 };
817
818 char *ext4_extract_xattr_name(char *full_name, size_t full_name_len,
819                               uint8_t *name_index, size_t *name_len)
820 {
821         int i;
822         ext4_assert(name_index);
823         if (!full_name_len)
824                 return NULL;
825
826         for (i = 0; prefix_tbl[i].prefix; i++) {
827                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
828                 if (full_name_len >= prefix_len &&
829                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
830                         *name_index = prefix_tbl[i].name_index;
831                         if (name_len)
832                                 *name_len = full_name_len - prefix_len;
833
834                         return full_name + prefix_len;
835                 }
836         }
837         if (name_len)
838                 *name_len = 0;
839
840         return NULL;
841 }
842
843 /**
844  * @}
845  */