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