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