ext4_xattr: fix not computing hash of on-block entries
[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->is_data && !b->is_data)
171                 return -1;
172         
173         if (!a->is_data && b->is_data)
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         item->in_inode = false;
204
205         memset(&item->node, 0, sizeof(item->node));
206         memcpy(item->name, name, name_len);
207
208         if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
209             name_len == 4 &&
210             !memcmp(name, "data", 4))
211                 item->is_data = true;
212         else
213                 item->is_data = false;
214
215         return item;
216 }
217
218 static int ext4_xattr_item_alloc_data(struct ext4_xattr_item *item,
219                                       const void *orig_data, size_t data_size)
220 {
221         void *data = NULL;
222         ext4_assert(!item->data);
223         data = malloc(data_size);
224         if (!data)
225                 return ENOMEM;
226
227         if (orig_data)
228                 memcpy(data, orig_data, data_size);
229
230         item->data = data;
231         item->data_size = data_size;
232         return EOK;
233 }
234
235 static void ext4_xattr_item_free_data(struct ext4_xattr_item *item)
236 {
237         ext4_assert(item->data);
238         free(item->data);
239         item->data = NULL;
240         item->data_size = 0;
241 }
242
243 static int ext4_xattr_item_resize_data(struct ext4_xattr_item *item,
244                                        size_t new_data_size)
245 {
246         if (new_data_size != item->data_size) {
247                 void *new_data;
248                 new_data = realloc(item->data, new_data_size);
249                 if (!new_data)
250                         return ENOMEM;
251
252                 item->data = new_data;
253                 item->data_size = new_data_size;
254         }
255         return EOK;
256 }
257
258 static void ext4_xattr_item_free(struct ext4_xattr_item *item)
259 {
260         if (item->data)
261                 ext4_xattr_item_free_data(item);
262
263         free(item);
264 }
265
266 static void *ext4_xattr_entry_data(struct ext4_xattr_ref *xattr_ref,
267                                    struct ext4_xattr_entry *entry,
268                                    bool in_inode)
269 {
270         char *ret;
271         if (in_inode) {
272                 struct ext4_xattr_ibody_header *header;
273                 struct ext4_xattr_entry *first_entry;
274                 int16_t inode_size =
275                     ext4_get16(&xattr_ref->fs->sb, inode_size);
276                 header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
277                                 xattr_ref->inode_ref->inode);
278                 first_entry = EXT4_XATTR_IFIRST(header);
279
280                 ret = ((char *)first_entry + to_le16(entry->e_value_offs));
281                 if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
282                         (char *)xattr_ref->inode_ref->inode > inode_size)
283                         ret = NULL;
284
285                 return ret;
286
287         }
288         int32_t block_size = ext4_sb_get_block_size(&xattr_ref->fs->sb);
289         ret = ((char *)xattr_ref->block.data + to_le16(entry->e_value_offs));
290         if (ret + EXT4_XATTR_SIZE(to_le32(entry->e_value_size)) -
291                         (char *)xattr_ref->block.data > block_size)
292                 ret = NULL;
293         return ret;
294 }
295
296 static int ext4_xattr_block_fetch(struct ext4_xattr_ref *xattr_ref)
297 {
298         int ret = EOK;
299         size_t size_rem;
300         void *data;
301         struct ext4_xattr_entry *entry = NULL;
302
303         ext4_assert(xattr_ref->block.data);
304         entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
305
306         size_rem = ext4_sb_get_block_size(&xattr_ref->fs->sb);
307         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
308              entry = EXT4_XATTR_NEXT(entry),
309              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
310                 struct ext4_xattr_item *item;
311                 char *e_name = EXT4_XATTR_NAME(entry);
312
313                 data = ext4_xattr_entry_data(xattr_ref, entry, false);
314                 if (!data) {
315                         ret = EIO;
316                         goto Finish;
317                 }
318
319                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
320                                              (size_t)entry->e_name_len);
321                 if (!item) {
322                         ret = ENOMEM;
323                         goto Finish;
324                 }
325                 if (ext4_xattr_item_alloc_data(
326                         item, data, to_le32(entry->e_value_size)) != EOK) {
327                         ext4_xattr_item_free(item);
328                         ret = ENOMEM;
329                         goto Finish;
330                 }
331                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
332                 xattr_ref->block_size_rem -=
333                         EXT4_XATTR_SIZE(item->data_size) +
334                         EXT4_XATTR_LEN(item->name_len);
335                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
336                                       EXT4_XATTR_LEN(item->name_len);
337         }
338
339 Finish:
340         return ret;
341 }
342
343 static int ext4_xattr_inode_fetch(struct ext4_xattr_ref *xattr_ref)
344 {
345         void *data;
346         size_t size_rem;
347         int ret = EOK;
348         struct ext4_xattr_ibody_header *header = NULL;
349         struct ext4_xattr_entry *entry = NULL;
350         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
351         uint16_t extra_isize = ext4_inode_get_extra_isize(&xattr_ref->fs->sb,
352                                         xattr_ref->inode_ref->inode);
353
354         header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
355                                  xattr_ref->inode_ref->inode);
356         entry = EXT4_XATTR_IFIRST(header);
357
358         size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
359                    extra_isize;
360         for (; size_rem > 0 && !EXT4_XATTR_IS_LAST_ENTRY(entry);
361              entry = EXT4_XATTR_NEXT(entry),
362              size_rem -= EXT4_XATTR_LEN(entry->e_name_len)) {
363                 struct ext4_xattr_item *item;
364                 char *e_name = EXT4_XATTR_NAME(entry);
365
366                 data = ext4_xattr_entry_data(xattr_ref, entry, true);
367                 if (!data) {
368                         ret = EIO;
369                         goto Finish;
370                 }
371
372                 item = ext4_xattr_item_alloc(entry->e_name_index, e_name,
373                                              (size_t)entry->e_name_len);
374                 if (!item) {
375                         ret = ENOMEM;
376                         goto Finish;
377                 }
378                 if (ext4_xattr_item_alloc_data(
379                         item, data, to_le32(entry->e_value_size)) != EOK) {
380                         ext4_xattr_item_free(item);
381                         ret = ENOMEM;
382                         goto Finish;
383                 }
384                 item->in_inode = true;
385                 RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
386                 xattr_ref->inode_size_rem -=
387                         EXT4_XATTR_SIZE(item->data_size) +
388                         EXT4_XATTR_LEN(item->name_len);
389                 xattr_ref->ea_size += EXT4_XATTR_SIZE(item->data_size) +
390                                       EXT4_XATTR_LEN(item->name_len);
391         }
392
393 Finish:
394         return ret;
395 }
396
397 static size_t ext4_xattr_inode_space(struct ext4_xattr_ref *xattr_ref)
398 {
399         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
400         uint16_t extra_isize = ext4_inode_get_extra_isize(&xattr_ref->fs->sb,
401                                         xattr_ref->inode_ref->inode);
402         uint16_t size_rem = inode_size - EXT4_GOOD_OLD_INODE_SIZE -
403                             extra_isize;
404         return size_rem;
405 }
406
407 static size_t ext4_xattr_block_space(struct ext4_xattr_ref *xattr_ref)
408 {
409         return ext4_sb_get_block_size(&xattr_ref->fs->sb);
410 }
411
412 static int ext4_xattr_fetch(struct ext4_xattr_ref *xattr_ref)
413 {
414         int ret = EOK;
415         uint16_t inode_size = ext4_get16(&xattr_ref->fs->sb, inode_size);
416         if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
417                 ret = ext4_xattr_inode_fetch(xattr_ref);
418                 if (ret != EOK)
419                         return ret;
420         }
421
422         if (xattr_ref->block_loaded)
423                 ret = ext4_xattr_block_fetch(xattr_ref);
424
425         xattr_ref->dirty = false;
426         return ret;
427 }
428
429 static struct ext4_xattr_item *
430 ext4_xattr_lookup_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
431                        const char *name, size_t name_len)
432 {
433         struct ext4_xattr_item tmp = {
434                 .name_index = name_index,
435                 .name = (char *)name, /*RB_FIND - won't touch this string*/
436                 .name_len = name_len,
437         };
438         if (name_index == EXT4_XATTR_INDEX_SYSTEM &&
439             name_len == 4 &&
440             !memcmp(name, "data", 4))
441                 tmp.is_data = true;
442
443         return RB_FIND(ext4_xattr_tree, &xattr_ref->root, &tmp);
444 }
445
446 static struct ext4_xattr_item *
447 ext4_xattr_insert_item(struct ext4_xattr_ref *xattr_ref, uint8_t name_index,
448                        const char *name, size_t name_len, const void *data,
449                        size_t data_size,
450                        int *err)
451 {
452         struct ext4_xattr_item *item;
453         item = ext4_xattr_item_alloc(name_index, name, name_len);
454         if (!item) {
455                 if (err)
456                         *err = ENOMEM;
457
458                 return NULL;
459         }
460
461         if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
462                 EXT4_XATTR_LEN(item->name_len)
463                         >
464             ext4_xattr_inode_space(xattr_ref) -
465                 sizeof(struct ext4_xattr_ibody_header))
466                 &&
467             (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
468                 EXT4_XATTR_LEN(item->name_len) >
469             ext4_xattr_block_space(xattr_ref) -
470                 sizeof(struct ext4_xattr_header))) {
471                 ext4_xattr_item_free(item);
472
473                 if (err)
474                         *err = ENOSPC;
475
476                 return NULL;
477         }
478         item->in_inode = true;
479         if (xattr_ref->inode_size_rem <
480             EXT4_XATTR_SIZE(data_size) +
481             EXT4_XATTR_LEN(item->name_len)) {
482                 if (xattr_ref->block_size_rem <
483                     EXT4_XATTR_SIZE(data_size) +
484                     EXT4_XATTR_LEN(item->name_len)) {
485                         if (err)
486                                 *err = ENOSPC;
487
488                         return NULL;
489                 }
490
491                 item->in_inode = false;
492         }
493         if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
494                 ext4_xattr_item_free(item);
495                 if (err)
496                         *err = ENOMEM;
497
498                 return NULL;
499         }
500         RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
501         xattr_ref->ea_size +=
502             EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
503         if (item->in_inode) {
504                 xattr_ref->inode_size_rem -=
505                         EXT4_XATTR_SIZE(item->data_size) +
506                         EXT4_XATTR_LEN(item->name_len);
507         } else {
508                 xattr_ref->block_size_rem -=
509                         EXT4_XATTR_SIZE(item->data_size) +
510                         EXT4_XATTR_LEN(item->name_len);
511         }
512         xattr_ref->dirty = true;
513         if (err)
514                 *err = EOK;
515
516         return item;
517 }
518
519 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
520                                   uint8_t name_index, const char *name,
521                                   size_t name_len)
522 {
523         int ret = ENOENT;
524         struct ext4_xattr_item *item =
525             ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
526         if (item) {
527                 if (item == xattr_ref->iter_from)
528                         xattr_ref->iter_from =
529                             RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
530
531                 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
532                                       EXT4_XATTR_LEN(item->name_len);
533
534                 if (item->in_inode) {
535                         xattr_ref->inode_size_rem +=
536                                 EXT4_XATTR_SIZE(item->data_size) +
537                                 EXT4_XATTR_LEN(item->name_len);
538                 } else {
539                         xattr_ref->block_size_rem +=
540                                 EXT4_XATTR_SIZE(item->data_size) +
541                                 EXT4_XATTR_LEN(item->name_len);
542                 }
543
544                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
545                 ext4_xattr_item_free(item);
546                 xattr_ref->dirty = true;
547                 ret = EOK;
548         }
549         return ret;
550 }
551
552 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
553                                   struct ext4_xattr_item *item,
554                                   size_t new_data_size)
555 {
556         int ret = EOK;
557         bool to_inode = false, to_block = false;
558         size_t old_data_size = item->data_size;
559         int32_t orig_room_size = item->in_inode ?
560                 xattr_ref->inode_size_rem :
561                 xattr_ref->block_size_rem;
562
563         /*
564          * Check if we can hold this entry in both in-inode and
565          * on-block form
566          */
567         if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
568                 EXT4_XATTR_SIZE(new_data_size)
569                         >
570             ext4_xattr_inode_space(xattr_ref) -
571                 sizeof(struct ext4_xattr_ibody_header))
572                 &&
573             (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
574                 EXT4_XATTR_SIZE(new_data_size)
575                         >
576             ext4_xattr_block_space(xattr_ref) -
577                 sizeof(struct ext4_xattr_header))) {
578
579                 return ENOSPC;
580         }
581
582         /*
583          * More complicated case: we do not allow entries stucking in
584          * the middle between in-inode space and on-block space, so
585          * the entry has to stay in either inode space or block space.
586          */
587         if (item->in_inode) {
588                 if (xattr_ref->inode_size_rem +
589                                 EXT4_XATTR_SIZE(old_data_size) <
590                                 EXT4_XATTR_SIZE(new_data_size)) {
591                         if (xattr_ref->block_size_rem <
592                                         EXT4_XATTR_SIZE(new_data_size) +
593                                         EXT4_XATTR_LEN(item->name_len))
594                                 return ENOSPC;
595
596                         to_block = true;
597                 }
598         } else {
599                 if (xattr_ref->block_size_rem +
600                                 EXT4_XATTR_SIZE(old_data_size) <
601                                 EXT4_XATTR_SIZE(new_data_size)) {
602                         if (xattr_ref->inode_size_rem <
603                                         EXT4_XATTR_SIZE(new_data_size) +
604                                         EXT4_XATTR_LEN(item->name_len))
605                                 return ENOSPC;
606
607                         to_inode = true;
608                 }
609         }
610         ret = ext4_xattr_item_resize_data(item, new_data_size);
611         if (ret != EOK)
612                 return ret;
613
614         xattr_ref->ea_size =
615             xattr_ref->ea_size -
616             EXT4_XATTR_SIZE(old_data_size) +
617             EXT4_XATTR_SIZE(new_data_size);
618
619         /*
620          * This entry may originally lie in inode space or block space,
621          * and it is going to be transferred to another place.
622          */
623         if (to_block) {
624                 xattr_ref->inode_size_rem +=
625                         EXT4_XATTR_SIZE(old_data_size) +
626                         EXT4_XATTR_LEN(item->name_len);
627                 xattr_ref->block_size_rem -=
628                         EXT4_XATTR_SIZE(new_data_size) +
629                         EXT4_XATTR_LEN(item->name_len);
630                 item->in_inode = false;
631         } else if (to_inode) {
632                 xattr_ref->block_size_rem +=
633                         EXT4_XATTR_SIZE(old_data_size) +
634                         EXT4_XATTR_LEN(item->name_len);
635                 xattr_ref->inode_size_rem -=
636                         EXT4_XATTR_SIZE(new_data_size) +
637                         EXT4_XATTR_LEN(item->name_len);
638                 item->in_inode = true;
639         } else {
640                 /*
641                  * No need to transfer as there is enough space for the entry
642                  * to stay in inode space or block space it used to be.
643                  */
644                 orig_room_size +=
645                         EXT4_XATTR_SIZE(old_data_size);
646                 orig_room_size -=
647                         EXT4_XATTR_SIZE(new_data_size);
648                 if (item->in_inode)
649                         xattr_ref->inode_size_rem = orig_room_size;
650                 else
651                         xattr_ref->block_size_rem = orig_room_size;
652
653         }
654         xattr_ref->dirty = true;
655         return ret;
656 }
657
658 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
659 {
660         struct ext4_xattr_item *item, *save_item;
661         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item) {
662                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
663                 ext4_xattr_item_free(item);
664         }
665         xattr_ref->ea_size = 0;
666         if (ext4_xattr_inode_space(xattr_ref) <
667             sizeof(struct ext4_xattr_ibody_header))
668                 xattr_ref->inode_size_rem = 0;
669         else
670                 xattr_ref->inode_size_rem =
671                         ext4_xattr_inode_space(xattr_ref) -
672                         sizeof(struct ext4_xattr_ibody_header);
673
674         xattr_ref->block_size_rem =
675                 ext4_xattr_block_space(xattr_ref) -
676                 sizeof(struct ext4_xattr_header);
677 }
678
679 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
680 {
681         int ret = EOK;
682
683         ext4_fsblk_t xattr_block = 0;
684         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
685                                               &xattr_ref->fs->sb);
686         if (!xattr_block) {
687                 ext4_fsblk_t goal =
688                         ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
689
690                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
691                                               goal,
692                                               &xattr_block);
693                 if (ret != EOK)
694                         goto Finish;
695
696                 ret = ext4_trans_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
697                                      xattr_block);
698                 if (ret != EOK) {
699                         ext4_balloc_free_block(xattr_ref->inode_ref,
700                                                xattr_block);
701                         goto Finish;
702                 }
703
704                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
705                                         &xattr_ref->fs->sb, xattr_block);
706                 xattr_ref->inode_ref->dirty = true;
707                 xattr_ref->block_loaded = true;
708         }
709
710 Finish:
711         return ret;
712 }
713
714 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
715 {
716         ext4_fsblk_t xattr_block;
717         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
718                                               &xattr_ref->fs->sb);
719         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
720                                 0);
721         ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
722         ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
723         xattr_ref->inode_ref->dirty = true;
724         xattr_ref->block_loaded = false;
725 }
726
727 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
728 {
729         struct ext4_xattr_header *block_header = NULL;
730         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
731
732         memset(block_header, 0, sizeof(struct ext4_xattr_header));
733         block_header->h_magic = EXT4_XATTR_MAGIC;
734         block_header->h_refcount = to_le32(1);
735         block_header->h_blocks = to_le32(1);
736 }
737
738 static void
739 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
740                            struct ext4_xattr_ibody_header *ibody_header,
741                            struct ext4_xattr_entry *entry, void *ibody_data_ptr)
742 {
743         entry->e_name_len = (uint8_t)item->name_len;
744         entry->e_name_index = item->name_index;
745         entry->e_value_offs =
746             to_le16((char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header));
747         entry->e_value_block = 0;
748         entry->e_value_size = to_le32(item->data_size);
749 }
750
751 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
752                                        struct ext4_xattr_header *block_header,
753                                        struct ext4_xattr_entry *block_entry,
754                                        void *block_data_ptr)
755 {
756         block_entry->e_name_len = (uint8_t)item->name_len;
757         block_entry->e_name_index = item->name_index;
758         block_entry->e_value_offs =
759             to_le16((char *)block_data_ptr - (char *)block_header);
760         block_entry->e_value_block = 0;
761         block_entry->e_value_size = to_le32(item->data_size);
762 }
763
764 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
765 {
766         int ret = EOK;
767         bool block_modified = false;
768         void *ibody_data = NULL;
769         void *block_data = NULL;
770         struct ext4_xattr_item *item, *save_item;
771         size_t inode_size_rem, block_size_rem;
772         struct ext4_xattr_ibody_header *ibody_header = NULL;
773         struct ext4_xattr_header *block_header = NULL;
774         struct ext4_xattr_entry *entry = NULL;
775         struct ext4_xattr_entry *block_entry = NULL;
776
777         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
778         block_size_rem = ext4_xattr_block_space(xattr_ref);
779         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
780                 ibody_header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
781                                                xattr_ref->inode_ref->inode);
782                 entry = EXT4_XATTR_IFIRST(ibody_header);
783         }
784
785         if (!xattr_ref->dirty)
786                 goto Finish;
787         /* If there are enough spaces in the ibody EA table.*/
788         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
789                 memset(ibody_header, 0, inode_size_rem);
790                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
791                 ibody_data = (char *)ibody_header + inode_size_rem;
792                 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
793
794                 xattr_ref->inode_ref->dirty = true;
795         }
796         /* If we need an extra block to hold the EA entries*/
797         if (xattr_ref->ea_size > inode_size_rem) {
798                 if (!xattr_ref->block_loaded) {
799                         ret = ext4_xattr_try_alloc_block(xattr_ref);
800                         if (ret != EOK)
801                                 goto Finish;
802                 }
803                 memset(xattr_ref->block.data, 0,
804                        ext4_sb_get_block_size(&xattr_ref->fs->sb));
805                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
806                 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
807                 ext4_xattr_set_block_header(xattr_ref);
808                 block_data = (char *)block_header + block_size_rem;
809                 block_size_rem -= sizeof(struct ext4_xattr_header);
810
811                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
812         } else {
813                 /* We don't need an extra block.*/
814                 if (xattr_ref->block_loaded) {
815                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
816                         block_header->h_refcount =
817                             to_le32(to_le32(block_header->h_refcount) - 1);
818                         if (!block_header->h_refcount) {
819                                 ext4_xattr_try_free_block(xattr_ref);
820                                 block_header = NULL;
821                         } else {
822                                 block_entry =
823                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
824                                 block_data =
825                                     (char *)block_header + block_size_rem;
826                                 block_size_rem -=
827                                     sizeof(struct ext4_xattr_header);
828                                 ext4_inode_set_file_acl(
829                                     xattr_ref->inode_ref->inode,
830                                     &xattr_ref->fs->sb, 0);
831
832                                 xattr_ref->inode_ref->dirty = true;
833                                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
834                         }
835                 }
836         }
837         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
838         {
839                 if (EXT4_XATTR_SIZE(item->data_size) +
840                         EXT4_XATTR_LEN(item->name_len) <=
841                     inode_size_rem) {
842                         ibody_data = (char *)ibody_data -
843                                      EXT4_XATTR_SIZE(item->data_size);
844                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
845                                                    ibody_data);
846                         memcpy(EXT4_XATTR_NAME(entry), item->name,
847                                item->name_len);
848                         memcpy(ibody_data, item->data, item->data_size);
849                         entry = EXT4_XATTR_NEXT(entry);
850                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
851                                           EXT4_XATTR_LEN(item->name_len);
852
853                         xattr_ref->inode_ref->dirty = true;
854                         continue;
855                 }
856                 if (EXT4_XATTR_SIZE(item->data_size) +
857                         EXT4_XATTR_LEN(item->name_len) >
858                     block_size_rem) {
859                         ret = ENOSPC;
860                         ext4_dbg(DEBUG_XATTR, "IMPOSSIBLE ENOSPC AS WE DID INSPECTION!\n");
861                         ext4_assert(0);
862                 }
863                 block_data =
864                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
865                 ext4_xattr_set_block_entry(item, block_header, block_entry,
866                                            block_data);
867                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
868                        item->name_len);
869                 memcpy(block_data, item->data, item->data_size);
870                 ext4_xattr_compute_hash(block_header, block_entry);
871                 block_entry = EXT4_XATTR_NEXT(block_entry);
872                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
873                                   EXT4_XATTR_LEN(item->name_len);
874
875                 block_modified = true;
876         }
877         xattr_ref->dirty = false;
878         if (block_modified) {
879                 ext4_xattr_rehash(block_header,
880                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
881                 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
882                                               xattr_ref->block.lb_id,
883                                               block_header);
884                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
885         }
886
887 Finish:
888         return ret;
889 }
890
891 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
892                            int (*iter)(struct ext4_xattr_ref *ref,
893                                      struct ext4_xattr_item *item))
894 {
895         struct ext4_xattr_item *item;
896         if (!ref->iter_from)
897                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
898
899         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
900         {
901                 int ret = EXT4_XATTR_ITERATE_CONT;
902                 if (iter)
903                         iter(ref, item);
904
905                 if (ret != EXT4_XATTR_ITERATE_CONT) {
906                         if (ret == EXT4_XATTR_ITERATE_STOP)
907                                 ref->iter_from = NULL;
908
909                         break;
910                 }
911         }
912 }
913
914 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
915 {
916         ref->iter_from = NULL;
917 }
918
919 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
920                       const char *name, size_t name_len, const void *data,
921                       size_t data_size, bool replace)
922 {
923         int ret = EOK;
924         struct ext4_xattr_item *item =
925             ext4_xattr_lookup_item(ref, name_index, name, name_len);
926         if (replace) {
927                 if (!item) {
928                         ret = ENODATA;
929                         goto Finish;
930                 }
931                 if (item->data_size != data_size)
932                         ret = ext4_xattr_resize_item(ref, item, data_size);
933
934                 if (ret != EOK) {
935                         goto Finish;
936                 }
937                 memcpy(item->data, data, data_size);
938         } else {
939                 if (item) {
940                         ret = EEXIST;
941                         goto Finish;
942                 }
943                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
944                                               data, data_size, &ret);
945         }
946 Finish:
947         return ret;
948 }
949
950 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
951                          const char *name, size_t name_len)
952 {
953         return ext4_xattr_remove_item(ref, name_index, name, name_len);
954 }
955
956 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
957                       const char *name, size_t name_len, void *buf,
958                       size_t buf_size, size_t *data_size)
959 {
960         int ret = EOK;
961         size_t item_size = 0;
962         struct ext4_xattr_item *item =
963             ext4_xattr_lookup_item(ref, name_index, name, name_len);
964
965         if (!item) {
966                 ret = ENODATA;
967                 goto Finish;
968         }
969         item_size = item->data_size;
970         if (buf_size > item_size)
971                 buf_size = item_size;
972
973         if (buf)
974                 memcpy(buf, item->data, buf_size);
975
976 Finish:
977         if (data_size)
978                 *data_size = item_size;
979
980         return ret;
981 }
982
983 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
984                           struct ext4_xattr_ref *ref)
985 {
986         int rc;
987         ext4_fsblk_t xattr_block;
988         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
989         RB_INIT(&ref->root);
990         ref->ea_size = 0;
991         ref->iter_from = NULL;
992         if (xattr_block) {
993                 rc = ext4_trans_block_get(fs->bdev, &ref->block, xattr_block);
994                 if (rc != EOK)
995                         return EIO;
996
997                 ref->block_loaded = true;
998         } else
999                 ref->block_loaded = false;
1000
1001         ref->inode_ref = inode_ref;
1002         ref->fs = fs;
1003
1004         if (ext4_xattr_inode_space(ref) <
1005             sizeof(struct ext4_xattr_ibody_header))
1006                 ref->inode_size_rem = 0;
1007         else
1008                 ref->inode_size_rem =
1009                         ext4_xattr_inode_space(ref) -
1010                         sizeof(struct ext4_xattr_ibody_header);
1011
1012         ref->block_size_rem =
1013                 ext4_xattr_block_space(ref) -
1014                 sizeof(struct ext4_xattr_header);
1015
1016         rc = ext4_xattr_fetch(ref);
1017         if (rc != EOK) {
1018                 ext4_xattr_purge_items(ref);
1019                 if (xattr_block)
1020                         ext4_block_set(fs->bdev, &ref->block);
1021
1022                 ref->block_loaded = false;
1023                 return rc;
1024         }
1025         return EOK;
1026 }
1027
1028 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
1029 {
1030         int rc = ext4_xattr_write_to_disk(ref);
1031         if (ref->block_loaded) {
1032                 if (rc != EOK)
1033                         ext4_bcache_clear_dirty(ref->block.buf);
1034
1035                 ext4_block_set(ref->fs->bdev, &ref->block);
1036                 ref->block_loaded = false;
1037         }
1038         ext4_xattr_purge_items(ref);
1039         ref->inode_ref = NULL;
1040         ref->fs = NULL;
1041 }
1042
1043 struct xattr_prefix {
1044         const char *prefix;
1045         uint8_t name_index;
1046 };
1047
1048 static const struct xattr_prefix prefix_tbl[] = {
1049     {"user.", EXT4_XATTR_INDEX_USER},
1050     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
1051     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
1052     {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
1053     {"security.", EXT4_XATTR_INDEX_SECURITY},
1054     {"system.", EXT4_XATTR_INDEX_SYSTEM},
1055     {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
1056     {NULL, 0},
1057 };
1058
1059 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
1060                               uint8_t *name_index, size_t *name_len,
1061                               bool *found)
1062 {
1063         int i;
1064         ext4_assert(name_index);
1065         ext4_assert(found);
1066
1067         *found = false;
1068
1069         if (!full_name_len) {
1070                 if (name_len)
1071                         *name_len = 0;
1072
1073                 return NULL;
1074         }
1075
1076         for (i = 0; prefix_tbl[i].prefix; i++) {
1077                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1078                 if (full_name_len >= prefix_len &&
1079                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
1080                         bool require_name =
1081                                 prefix_tbl[i].prefix[prefix_len - 1] == '.';
1082                         *name_index = prefix_tbl[i].name_index;
1083                         if (name_len)
1084                                 *name_len = full_name_len - prefix_len;
1085
1086                         if (!(full_name_len - prefix_len) && require_name)
1087                                 return NULL;
1088
1089                         *found = true;
1090                         if (require_name)
1091                                 return full_name + prefix_len;
1092
1093                         return NULL;
1094                 }
1095         }
1096         if (name_len)
1097                 *name_len = 0;
1098
1099         return NULL;
1100 }
1101
1102 const char *ext4_get_xattr_name_prefix(uint8_t name_index,
1103                                        size_t *ret_prefix_len)
1104 {
1105         int i;
1106
1107         for (i = 0; prefix_tbl[i].prefix; i++) {
1108                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1109                 if (prefix_tbl[i].name_index == name_index) {
1110                         if (ret_prefix_len)
1111                                 *ret_prefix_len = prefix_len;
1112
1113                         return prefix_tbl[i].prefix;
1114                 }
1115         }
1116         if (ret_prefix_len)
1117                 *ret_prefix_len = 0;
1118
1119         return NULL;
1120 }
1121
1122 /**
1123  * @}
1124  */