Fix some indentation issues
[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                 goto Finish;
569         /* If there are enough spaces in the ibody EA table.*/
570         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
571                 memset(ibody_header, 0, inode_size_rem);
572                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
573                 ibody_data = (char *)ibody_header + inode_size_rem;
574                 inode_size_rem -= 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 = EXT4_XATTR_BHDR(&xattr_ref->block);
596                         block_header->h_refcount =
597                             to_le32(to_le32(block_header->h_refcount) - 1);
598                         if (!block_header->h_refcount) {
599                                 ext4_xattr_try_free_block(xattr_ref);
600                                 block_header = NULL;
601                         } else {
602                                 block_entry =
603                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
604                                 block_data =
605                                     (char *)block_header + block_size_rem;
606                                 block_size_rem -=
607                                     sizeof(struct ext4_xattr_header);
608                                 ext4_inode_set_file_acl(
609                                     xattr_ref->inode_ref->inode,
610                                     &xattr_ref->fs->sb, 0);
611
612                                 xattr_ref->inode_ref->dirty = true;
613                                 xattr_ref->block.dirty = true;
614                         }
615                 }
616         }
617         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
618         {
619                 if (EXT4_XATTR_SIZE(item->data_size) +
620                         EXT4_XATTR_LEN(item->name_len) <=
621                     inode_size_rem) {
622                         ibody_data = (char *)ibody_data -
623                                      EXT4_XATTR_SIZE(item->data_size);
624                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
625                                                    ibody_data);
626                         memcpy(EXT4_XATTR_NAME(entry), item->name,
627                                item->name_len);
628                         memcpy(ibody_data, item->data, item->data_size);
629                         entry = EXT4_XATTR_NEXT(entry);
630                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
631                                           EXT4_XATTR_LEN(item->name_len);
632
633                         xattr_ref->inode_ref->dirty = true;
634                         continue;
635                 }
636                 if (EXT4_XATTR_SIZE(item->data_size) +
637                         EXT4_XATTR_LEN(item->name_len) >
638                     block_size_rem) {
639                         ret = ENOSPC;
640                         goto Finish;
641                 }
642                 block_data =
643                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
644                 ext4_xattr_set_block_entry(item, block_header, block_entry,
645                                            block_data);
646                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
647                        item->name_len);
648                 memcpy(block_data, item->data, item->data_size);
649                 block_entry = EXT4_XATTR_NEXT(block_entry);
650                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
651                                   EXT4_XATTR_LEN(item->name_len);
652
653                 block_modified = true;
654         }
655         xattr_ref->dirty = false;
656         if (block_modified) {
657                 ext4_xattr_rehash(block_header,
658                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
659                 xattr_ref->block.dirty = true;
660         }
661
662 Finish:
663         return ret;
664 }
665
666 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
667                            int(iter)(struct ext4_xattr_ref *ref,
668                                      struct ext4_xattr_item *item))
669 {
670         struct ext4_xattr_item *item;
671         if (!ref->iter_from)
672                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
673
674         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
675         {
676                 int ret = EXT4_XATTR_ITERATE_CONT;
677                 if (iter)
678                         iter(ref, item);
679
680                 if (ret != EXT4_XATTR_ITERATE_CONT) {
681                         if (ret == EXT4_XATTR_ITERATE_STOP)
682                                 ref->iter_from = NULL;
683
684                         break;
685                 }
686         }
687 }
688
689 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
690 {
691         ref->iter_from = NULL;
692 }
693
694 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
695                       char *name, size_t name_len, void *data, size_t data_size,
696                       bool replace)
697 {
698         int ret = EOK;
699         struct ext4_xattr_item *item =
700             ext4_xattr_lookup_item(ref, name_index, name, name_len);
701         if (replace) {
702                 if (!item) {
703                         ret = ENOATTR;
704                         goto Finish;
705                 }
706                 if (item->data_size != data_size)
707                         ret = ext4_xattr_resize_item(ref, item, data_size);
708
709                 if (ret != EOK) {
710                         goto Finish;
711                 }
712                 memcpy(item->data, data, data_size);
713         } else {
714                 if (item) {
715                         ret = EEXIST;
716                         goto Finish;
717                 }
718                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
719                                               data, data_size);
720                 if (!item)
721                         ret = ENOMEM;
722         }
723 Finish:
724         return ret;
725 }
726
727 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
728                          char *name, size_t name_len)
729 {
730         return ext4_xattr_remove_item(ref, name_index, name, name_len);
731 }
732
733 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
734                       char *name, size_t name_len, void *buf, size_t buf_size,
735                       size_t *size_got)
736 {
737         int ret = EOK;
738         size_t item_size = 0;
739         struct ext4_xattr_item *item =
740             ext4_xattr_lookup_item(ref, name_index, name, name_len);
741
742         if (!item) {
743                 ret = ENOATTR;
744                 goto Finish;
745         }
746         item_size = item->data_size;
747         if (buf_size > item_size)
748                 buf_size = item_size;
749
750         if (buf)
751                 memcpy(buf, item->data, buf_size);
752
753 Finish:
754         if (size_got)
755                 *size_got = buf_size;
756
757         return ret;
758 }
759
760 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
761                           struct ext4_xattr_ref *ref)
762 {
763         int rc;
764         uint64_t xattr_block;
765         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
766         RB_INIT(&ref->root);
767         ref->ea_size = 0;
768         ref->iter_from = NULL;
769         if (xattr_block) {
770                 rc = ext4_block_get(fs->bdev, &ref->block, xattr_block);
771                 if (rc != EOK)
772                         return EIO;
773
774                 ref->ea_size += sizeof(struct ext4_xattr_header);
775                 ref->block_loaded = true;
776         } else
777                 ref->block_loaded = false;
778
779         ref->inode_ref = inode_ref;
780         ref->fs = fs;
781
782         if (ext4_xattr_inode_space(ref) >
783             sizeof(struct ext4_xattr_ibody_header))
784                 ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
785
786         rc = ext4_xattr_fetch(ref);
787         if (rc != EOK) {
788                 ext4_xattr_purge_items(ref);
789                 if (xattr_block)
790                         ext4_block_set(fs->bdev, &inode_ref->block);
791
792                 ref->block_loaded = false;
793                 return rc;
794         }
795         return EOK;
796 }
797
798 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
799 {
800         ext4_xattr_write_to_disk(ref);
801         if (ref->block_loaded) {
802                 ext4_block_set(ref->fs->bdev, &ref->block);
803                 ref->block_loaded = false;
804         }
805         ext4_xattr_purge_items(ref);
806         ref->inode_ref = NULL;
807         ref->fs = NULL;
808 }
809
810 struct xattr_prefix {
811         char *prefix;
812         uint8_t name_index;
813 };
814
815 static const struct xattr_prefix prefix_tbl[] = {
816     {"user.", EXT4_XATTR_INDEX_USER},
817     {"system.", EXT4_XATTR_INDEX_SYSTEM},
818     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
819     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
820     {NULL, 0},
821 };
822
823 char *ext4_extract_xattr_name(char *full_name, size_t full_name_len,
824                               uint8_t *name_index, size_t *name_len)
825 {
826         int i;
827         ext4_assert(name_index);
828         if (!full_name_len)
829                 return NULL;
830
831         for (i = 0; prefix_tbl[i].prefix; i++) {
832                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
833                 if (full_name_len >= prefix_len &&
834                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
835                         *name_index = prefix_tbl[i].name_index;
836                         if (name_len)
837                                 *name_len = full_name_len - prefix_len;
838
839                         return full_name + prefix_len;
840                 }
841         }
842         if (name_len)
843                 *name_len = 0;
844
845         return NULL;
846 }
847
848 /**
849  * @}
850  */