Add mkfs to debug module
[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, const 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                                       const 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         char *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 = ((char *)first_entry + to_le16(entry->e_value_offs));
215                 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
216                         (char *)xattr_ref->inode_ref->inode > inode_size)
217                         ret = NULL;
218
219                 return ret;
220
221         }
222         int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
223         ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
224         if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
225                         (char *)xattr_ref->block.data > block_size)
226                 ret = NULL;
227         return ret;
228 }
229
230 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
231 {
232         int ret = EOK;
233         size_t size_rem;
234         void *data;
235         struct ext4_xattr_entry *entry = NULL;
236
237         ext4_assert(xattr_ref->block.data);
238         entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
239
240         size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
241         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
242              entry = EXT4_XATTR_NEXT(entry),
243              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
244                 struct ext4_xattr_item *item;
245                 char *e_name = EXT4_XATTR_NAME(entry);
246
247                 data = ext4_xattr_entry_data(xattr_ref, entry, false);
248                 if (!data) {
249                         ret = EIO;
250                         goto Finish;
251                 }
252
253                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
254                                              (size_t)entry->e_name_len);
255                 if (!item) {
256                         ret = ENOMEM;
257                         goto Finish;
258                 }
259                 if (ext4_xattr_item_alloc_data(
260                         item, data, to_le32(entry->e_value_size)) != EOK) {
261                         ext4_xattr_item_free(item);
262                         ret = ENOMEM;
263                         goto Finish;
264                 }
265                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
266                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
267                                       EXT4_XATTR_LEN(item->name_len);
268         }
269
270 Finish:
271         return ret;
272 }
273
274 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
275 {
276         void *data;
277         size_t size_rem;
278         int ret = EOK;
279         struct ext4_xattr_ibody_header *header = NULL;
280         struct ext4_xattr_entry *entry = NULL;
281         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
282
283         header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
284         entry = EXT4_XATTR_IFIRST(header);
285
286         size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
287                    xattr_ref->inode_ref->inode->extra_isize;
288         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
289              entry = EXT4_XATTR_NEXT(entry),
290              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
291                 struct ext4_xattr_item *item;
292                 char *e_name = EXT4_XATTR_NAME(entry);
293
294                 data = ext4_xattr_entry_data(xattr_ref, entry, true);
295                 if (!data) {
296                         ret = EIO;
297                         goto Finish;
298                 }
299
300                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
301                                              (size_t)entry->e_name_len);
302                 if (!item) {
303                         ret = ENOMEM;
304                         goto Finish;
305                 }
306                 if (ext4_xattr_item_alloc_data(
307                         item, data, to_le32(entry->e_value_size)) != EOK) {
308                         ext4_xattr_item_free(item);
309                         ret = ENOMEM;
310                         goto Finish;
311                 }
312                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
313                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
314                                       EXT4_XATTR_LEN(item->name_len);
315         }
316
317 Finish:
318         return ret;
319 }
320
321 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
322 {
323         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
324         uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
325                             xattr_ref->inode_ref->inode->extra_isize;
326         return size_rem;
327 }
328
329 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
330 {
331         return ext4_sb_get_block_size(&xattr_ref->fs->sb);
332 }
333
334 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
335 {
336         int ret = EOK;
337         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
338         if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
339                 ret = ext4_xattr_inode_fetch(xattr_ref);
340                 if (ret != EOK)
341                         return ret;
342         }
343
344         if (xattr_ref->block_loaded)
345                 ret = ext4_xattr_block_fetch(xattr_ref);
346
347         xattr_ref->dirty = false;
348         return ret;
349 }
350
351 static struct ext4_xattr_item *
352 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
353                        const char *name, size_t name_len)
354 {
355         struct ext4_xattr_item tmp = {
356                 .name_index = name_index,
357                 .name = (char *)name, /*RB_FIND - won't touch this string*/
358                 .name_len = name_len,
359         };
360
361         return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
362 }
363
364 static struct ext4_xattr_item *
365 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
366                        const char *name, size_t name_len, const void *data,
367                        size_t data_size)
368 {
369         struct ext4_xattr_item *item;
370         item = ext4_xattr_item_alloc(name_index, name, name_len);
371         if (!item)
372                 return NULL;
373
374         if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
375                 EXT4_XATTR_LEN(item->name_len)
376                         >
377             ext4_xattr_inode_space(xattr_ref) -
378                 sizeof(struct ext4_xattr_ibody_header))
379                 &&
380             (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
381                 EXT4_XATTR_LEN(item->name_len) >
382             ext4_xattr_block_space(xattr_ref) -
383                 sizeof(struct ext4_xattr_header))) {
384                 ext4_xattr_item_free(item);
385
386                 return NULL;
387         }
388         if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
389                 ext4_xattr_item_free(item);
390                 return NULL;
391         }
392         RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
393         xattr_ref->ea_size +=
394             EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
395         xattr_ref->dirty = true;
396         return item;
397 }
398
399 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
400                                   uint8_t name_index, const char *name,
401                                   size_t name_len)
402 {
403         int ret = ENOENT;
404         struct ext4_xattr_item *item =
405             ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
406         if (item) {
407                 if (item == xattr_ref->iter_from)
408                         xattr_ref->iter_from =
409                             RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
410
411                 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
412                                       EXT4_XATTR_LEN(item->name_len);
413
414                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
415                 ext4_xattr_item_free(item);
416                 xattr_ref->dirty = true;
417                 ret = EOK;
418         }
419         return ret;
420 }
421
422 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
423                                   struct ext4_xattr_item *item,
424                                   size_t new_data_size)
425 {
426         int ret = EOK;
427         size_t old_data_size = item->data_size;
428         if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
429                 EXT4_XATTR_SIZE(new_data_size)
430                         >
431             ext4_xattr_inode_space(xattr_ref) -
432                 sizeof(struct ext4_xattr_ibody_header))
433                 &&
434             (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
435                 EXT4_XATTR_SIZE(new_data_size)
436                         >
437             ext4_xattr_block_space(xattr_ref) -
438                 sizeof(struct ext4_xattr_header))) {
439
440                 return ENOSPC;
441         }
442         ret = ext4_xattr_item_resize_data(item, new_data_size);
443         if (ret != EOK) {
444                 return ret;
445         }
446         xattr_ref->ea_size =
447             xattr_ref->ea_size -
448             EXT4_XATTR_SIZE(old_data_size) +
449             EXT4_XATTR_SIZE(new_data_size);
450         xattr_ref->dirty = true;
451         return ret;
452 }
453
454 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
455 {
456         struct ext4_xattr_item *item, *save_item;
457         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
458         {
459                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
460                 ext4_xattr_item_free(item);
461         }
462         xattr_ref->ea_size = 0;
463 }
464
465 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
466 {
467         int ret = EOK;
468
469         uint64_t xattr_block = 0;
470         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
471                                               &xattr_ref->fs->sb);
472         if (!xattr_block) {
473                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
474                                               (uint32_t *)&xattr_block);
475                 if (ret != EOK)
476                         goto Finish;
477
478                 ret = ext4_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
479                                      xattr_block);
480                 if (ret != EOK) {
481                         ext4_balloc_free_block(xattr_ref->inode_ref,
482                                                xattr_block);
483                         goto Finish;
484                 }
485
486                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
487                                         &xattr_ref->fs->sb, xattr_block);
488                 xattr_ref->inode_ref->dirty = true;
489                 xattr_ref->block_loaded = true;
490         }
491
492 Finish:
493         return ret;
494 }
495
496 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
497 {
498         uint64_t xattr_block;
499         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
500                                               &xattr_ref->fs->sb);
501         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
502                                 0);
503         ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
504         ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
505         xattr_ref->inode_ref->dirty = true;
506         xattr_ref->block_loaded = false;
507 }
508
509 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
510 {
511         struct ext4_xattr_header *block_header = NULL;
512         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
513
514         memset(block_header, 0, sizeof(struct ext4_xattr_header));
515         block_header->h_magic = EXT4_XATTR_MAGIC;
516         block_header->h_refcount = to_le32(1);
517         block_header->h_blocks = to_le32(1);
518 }
519
520 static void
521 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
522                            struct ext4_xattr_ibody_header *ibody_header,
523                            struct ext4_xattr_entry *entry, void *ibody_data_ptr)
524 {
525         entry->e_name_len = to_le32(item->name_len);
526         entry->e_name_index = item->name_index;
527         entry->e_value_offs =
528             (char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header);
529         entry->e_value_block = 0;
530         entry->e_value_size = item->data_size;
531 }
532
533 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
534                                        struct ext4_xattr_header *block_header,
535                                        struct ext4_xattr_entry *block_entry,
536                                        void *block_data_ptr)
537 {
538         block_entry->e_name_len = to_le32(item->name_len);
539         block_entry->e_name_index = item->name_index;
540         block_entry->e_value_offs =
541             (char *)block_data_ptr - (char *)block_header;
542         block_entry->e_value_block = 0;
543         block_entry->e_value_size = item->data_size;
544 }
545
546 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
547 {
548         int ret = EOK;
549         bool block_modified = false;
550         void *ibody_data = NULL;
551         void *block_data = NULL;
552         struct ext4_xattr_item *item, *save_item;
553         size_t inode_size_rem, block_size_rem;
554         struct ext4_xattr_ibody_header *ibody_header = NULL;
555         struct ext4_xattr_header *block_header = NULL;
556         struct ext4_xattr_entry *entry = NULL;
557         struct ext4_xattr_entry *block_entry = NULL;
558
559         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
560         block_size_rem = ext4_xattr_block_space(xattr_ref);
561         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
562                 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
563                 entry = EXT4_XATTR_IFIRST(ibody_header);
564         }
565
566         if (!xattr_ref->dirty)
567                 goto Finish;
568         /* If there are enough spaces in the ibody EA table.*/
569         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
570                 memset(ibody_header, 0, inode_size_rem);
571                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
572                 ibody_data = (char *)ibody_header + inode_size_rem;
573                 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
574
575                 xattr_ref->inode_ref->dirty = true;
576         }
577         /* If we need an extra block to hold the EA entries*/
578         if (xattr_ref->ea_size > inode_size_rem) {
579                 if (!xattr_ref->block_loaded) {
580                         ret = ext4_xattr_try_alloc_block(xattr_ref);
581                         if (ret != EOK)
582                                 goto Finish;
583                 }
584                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
585                 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
586                 ext4_xattr_set_block_header(xattr_ref);
587                 block_data = (char *)block_header + block_size_rem;
588                 block_size_rem -= sizeof(struct ext4_xattr_header);
589
590                 xattr_ref->block.dirty = true;
591         } else {
592                 /* We don't need an extra block.*/
593                 if (xattr_ref->block_loaded) {
594                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
595                         block_header->h_refcount =
596                             to_le32(to_le32(block_header->h_refcount) - 1);
597                         if (!block_header->h_refcount) {
598                                 ext4_xattr_try_free_block(xattr_ref);
599                                 block_header = NULL;
600                         } else {
601                                 block_entry =
602                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
603                                 block_data =
604                                     (char *)block_header + block_size_rem;
605                                 block_size_rem -=
606                                     sizeof(struct ext4_xattr_header);
607                                 ext4_inode_set_file_acl(
608                                     xattr_ref->inode_ref->inode,
609                                     &xattr_ref->fs->sb, 0);
610
611                                 xattr_ref->inode_ref->dirty = true;
612                                 xattr_ref->block.dirty = true;
613                         }
614                 }
615         }
616         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
617         {
618                 if (EXT4_XATTR_SIZE(item->data_size) +
619                         EXT4_XATTR_LEN(item->name_len) <=
620                     inode_size_rem) {
621                         ibody_data = (char *)ibody_data -
622                                      EXT4_XATTR_SIZE(item->data_size);
623                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
624                                                    ibody_data);
625                         memcpy(EXT4_XATTR_NAME(entry), item->name,
626                                item->name_len);
627                         memcpy(ibody_data, item->data, item->data_size);
628                         entry = EXT4_XATTR_NEXT(entry);
629                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
630                                           EXT4_XATTR_LEN(item->name_len);
631
632                         xattr_ref->inode_ref->dirty = true;
633                         continue;
634                 }
635                 if (EXT4_XATTR_SIZE(item->data_size) +
636                         EXT4_XATTR_LEN(item->name_len) >
637                     block_size_rem) {
638                         ret = ENOSPC;
639                         goto Finish;
640                 }
641                 block_data =
642                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
643                 ext4_xattr_set_block_entry(item, block_header, block_entry,
644                                            block_data);
645                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
646                        item->name_len);
647                 memcpy(block_data, item->data, item->data_size);
648                 block_entry = EXT4_XATTR_NEXT(block_entry);
649                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
650                                   EXT4_XATTR_LEN(item->name_len);
651
652                 block_modified = true;
653         }
654         xattr_ref->dirty = false;
655         if (block_modified) {
656                 ext4_xattr_rehash(block_header,
657                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
658                 xattr_ref->block.dirty = true;
659         }
660
661 Finish:
662         return ret;
663 }
664
665 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
666                            int (*iter)(struct ext4_xattr_ref *ref,
667                                      struct ext4_xattr_item *item))
668 {
669         struct ext4_xattr_item *item;
670         if (!ref->iter_from)
671                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
672
673         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
674         {
675                 int ret = EXT4_XATTR_ITERATE_CONT;
676                 if (iter)
677                         iter(ref, item);
678
679                 if (ret != EXT4_XATTR_ITERATE_CONT) {
680                         if (ret == EXT4_XATTR_ITERATE_STOP)
681                                 ref->iter_from = NULL;
682
683                         break;
684                 }
685         }
686 }
687
688 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
689 {
690         ref->iter_from = NULL;
691 }
692
693 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
694                       const char *name, size_t name_len, const void *data,
695                       size_t data_size, bool replace)
696 {
697         int ret = EOK;
698         struct ext4_xattr_item *item =
699             ext4_xattr_lookup_item(ref, name_index, name, name_len);
700         if (replace) {
701                 if (!item) {
702                         ret = ENODATA;
703                         goto Finish;
704                 }
705                 if (item->data_size != data_size)
706                         ret = ext4_xattr_resize_item(ref, item, data_size);
707
708                 if (ret != EOK) {
709                         goto Finish;
710                 }
711                 memcpy(item->data, data, data_size);
712         } else {
713                 if (item) {
714                         ret = EEXIST;
715                         goto Finish;
716                 }
717                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
718                                               data, data_size);
719                 if (!item)
720                         ret = ENOMEM;
721         }
722 Finish:
723         return ret;
724 }
725
726 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
727                          const char *name, size_t name_len)
728 {
729         return ext4_xattr_remove_item(ref, name_index, name, name_len);
730 }
731
732 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
733                       const char *name, size_t name_len, void *buf, size_t buf_size,
734                       size_t *data_size)
735 {
736         int ret = EOK;
737         size_t item_size = 0;
738         struct ext4_xattr_item *item =
739             ext4_xattr_lookup_item(ref, name_index, name, name_len);
740
741         if (!item) {
742                 ret = ENODATA;
743                 goto Finish;
744         }
745         item_size = item->data_size;
746         if (buf_size > item_size)
747                 buf_size = item_size;
748
749         if (buf)
750                 memcpy(buf, item->data, buf_size);
751
752 Finish:
753         if (data_size)
754                 *data_size = item_size;
755
756         return ret;
757 }
758
759 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
760                           struct ext4_xattr_ref *ref)
761 {
762         int rc;
763         uint64_t xattr_block;
764         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
765         RB_INIT(&ref->root);
766         ref->ea_size = 0;
767         ref->iter_from = NULL;
768         if (xattr_block) {
769                 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
770                 if (rc != EOK)
771                         return EIO;
772
773                 ref->block_loaded = true;
774         } else
775                 ref->block_loaded = false;
776
777         ref->inode_ref = inode_ref;
778         ref->fs = fs;
779
780         rc = ext4_xattr_fetch(ref);
781         if (rc != EOK) {
782                 ext4_xattr_purge_items(ref);
783                 if (xattr_block)
784                         ext4_block_set(fs->bdev, &inode_ref->block);
785
786                 ref->block_loaded = false;
787                 return rc;
788         }
789         return EOK;
790 }
791
792 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
793 {
794         ext4_xattr_write_to_disk(ref);
795         if (ref->block_loaded) {
796                 ext4_block_set(ref->fs->bdev, &ref->block);
797                 ref->block_loaded = false;
798         }
799         ext4_xattr_purge_items(ref);
800         ref->inode_ref = NULL;
801         ref->fs = NULL;
802 }
803
804 struct xattr_prefix {
805         const char *prefix;
806         uint8_t name_index;
807 };
808
809 static const struct xattr_prefix prefix_tbl[] = {
810     {"user.", EXT4_XATTR_INDEX_USER},
811     {"system.", EXT4_XATTR_INDEX_SYSTEM},
812     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
813     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
814     {NULL, 0},
815 };
816
817 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
818                               uint8_t *name_index, size_t *name_len)
819 {
820         int i;
821         ext4_assert(name_index);
822         if (!full_name_len) {
823                 if (name_len)
824                         *name_len = 0;
825
826                 return NULL;
827         }
828
829         for (i = 0; prefix_tbl[i].prefix; i++) {
830                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
831                 if (full_name_len >= prefix_len &&
832                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
833                         *name_index = prefix_tbl[i].name_index;
834                         if (name_len)
835                                 *name_len = full_name_len - prefix_len;
836
837                         return full_name + prefix_len;
838                 }
839         }
840         if (name_len)
841                 *name_len = 0;
842
843         return NULL;
844 }
845
846 const char *ext4_get_xattr_name_prefix(uint8_t name_index, size_t *ret_prefix_len)
847 {
848         int i;
849
850         for (i = 0; prefix_tbl[i].prefix; i++) {
851                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
852                 if (prefix_tbl[i].name_index == name_index) {
853                         if (ret_prefix_len)
854                                 *ret_prefix_len = prefix_len;
855
856                         return prefix_tbl[i].prefix;
857                 }
858         }
859         if (ret_prefix_len)
860                 *ret_prefix_len = 0;
861
862         return NULL;
863 }
864
865 /**
866  * @}
867  */