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