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