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