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