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