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