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