ext4_xattr: better handling on some corner error case
[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 {
451         struct ext4_xattr_item *item;
452         item = ext4_xattr_item_alloc(name_index, name, name_len);
453         if (!item)
454                 return NULL;
455
456         if ((xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
457                 EXT4_XATTR_LEN(item->name_len)
458                         >
459             ext4_xattr_inode_space(xattr_ref) -
460                 sizeof(struct ext4_xattr_ibody_header))
461                 &&
462             (xattr_ref->ea_size + EXT4_XATTR_SIZE(data_size) +
463                 EXT4_XATTR_LEN(item->name_len) >
464             ext4_xattr_block_space(xattr_ref) -
465                 sizeof(struct ext4_xattr_header))) {
466                 ext4_xattr_item_free(item);
467
468                 return NULL;
469         }
470         item->in_inode = true;
471         if (xattr_ref->inode_size_rem -
472             (int32_t)EXT4_XATTR_SIZE(data_size) -
473             (int32_t)EXT4_XATTR_LEN(item->name_len) < 0) {
474                 if (xattr_ref->block_size_rem -
475                     (int32_t)EXT4_XATTR_SIZE(data_size) -
476                     (int32_t)EXT4_XATTR_LEN(item->name_len) < 0)
477                         return NULL;
478
479                 item->in_inode = false;
480         }
481         if (ext4_xattr_item_alloc_data(item, data, data_size) != EOK) {
482                 ext4_xattr_item_free(item);
483                 return NULL;
484         }
485         RB_INSERT(ext4_xattr_tree, &xattr_ref->root, item);
486         xattr_ref->ea_size +=
487             EXT4_XATTR_SIZE(item->data_size) + EXT4_XATTR_LEN(item->name_len);
488         if (item->in_inode) {
489                 xattr_ref->inode_size_rem -=
490                         EXT4_XATTR_SIZE(item->data_size) +
491                         EXT4_XATTR_LEN(item->name_len);
492         } else {
493                 xattr_ref->block_size_rem -=
494                         EXT4_XATTR_SIZE(item->data_size) +
495                         EXT4_XATTR_LEN(item->name_len);
496         }
497         xattr_ref->dirty = true;
498         return item;
499 }
500
501 static int ext4_xattr_remove_item(struct ext4_xattr_ref *xattr_ref,
502                                   uint8_t name_index, const char *name,
503                                   size_t name_len)
504 {
505         int ret = ENOENT;
506         struct ext4_xattr_item *item =
507             ext4_xattr_lookup_item(xattr_ref, name_index, name, name_len);
508         if (item) {
509                 if (item == xattr_ref->iter_from)
510                         xattr_ref->iter_from =
511                             RB_NEXT(ext4_xattr_tree, &xattr_ref->root, item);
512
513                 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
514                                       EXT4_XATTR_LEN(item->name_len);
515
516                 if (item->in_inode) {
517                         xattr_ref->inode_size_rem +=
518                                 EXT4_XATTR_SIZE(item->data_size) +
519                                 EXT4_XATTR_LEN(item->name_len);
520                 } else {
521                         xattr_ref->block_size_rem +=
522                                 EXT4_XATTR_SIZE(item->data_size) +
523                                 EXT4_XATTR_LEN(item->name_len);
524                 }
525
526                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
527                 ext4_xattr_item_free(item);
528                 xattr_ref->dirty = true;
529                 ret = EOK;
530         }
531         return ret;
532 }
533
534 static int ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
535                                   struct ext4_xattr_item *item,
536                                   size_t new_data_size)
537 {
538         int ret = EOK;
539         bool to_inode = false, to_block = false;
540         size_t old_data_size = item->data_size;
541         int32_t orig_room_size = item->in_inode ?
542                 xattr_ref->inode_size_rem :
543                 xattr_ref->block_size_rem;
544
545         /*
546          * Check if we can hold this entry in both in-inode and
547          * on-block form
548          */
549         if ((xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
550                 EXT4_XATTR_SIZE(new_data_size)
551                         >
552             ext4_xattr_inode_space(xattr_ref) -
553                 sizeof(struct ext4_xattr_ibody_header))
554                 &&
555             (xattr_ref->ea_size - EXT4_XATTR_SIZE(old_data_size) +
556                 EXT4_XATTR_SIZE(new_data_size)
557                         >
558             ext4_xattr_block_space(xattr_ref) -
559                 sizeof(struct ext4_xattr_header))) {
560
561                 return ENOSPC;
562         }
563
564         /*
565          * More complicated case: we do not allow entries stucking in
566          * the middle between in-inode space and on-block space, so
567          * the entry has to stay in either inode space or block space.
568          */
569         if (item->in_inode) {
570                 if (xattr_ref->inode_size_rem +
571                                 (int32_t)EXT4_XATTR_SIZE(old_data_size) -
572                                 (int32_t)EXT4_XATTR_SIZE(new_data_size) < 0) {
573                         if (xattr_ref->block_size_rem -
574                                         (int32_t)EXT4_XATTR_SIZE(new_data_size) -
575                                         (int32_t)EXT4_XATTR_LEN(item->name_len) < 0)
576                                 return ENOSPC;
577
578                         to_block = true;
579                 }
580         } else {
581                 if (xattr_ref->block_size_rem +
582                                 (int32_t)EXT4_XATTR_SIZE(old_data_size) -
583                                 (int32_t)EXT4_XATTR_SIZE(new_data_size) < 0) {
584                         if (xattr_ref->inode_size_rem -
585                                         (int32_t)EXT4_XATTR_SIZE(new_data_size) -
586                                         (int32_t)EXT4_XATTR_LEN(item->name_len) < 0)
587                                 return ENOSPC;
588
589                         to_inode = true;
590                 }
591         }
592         ret = ext4_xattr_item_resize_data(item, new_data_size);
593         if (ret != EOK)
594                 return ret;
595
596         xattr_ref->ea_size =
597             xattr_ref->ea_size -
598             EXT4_XATTR_SIZE(old_data_size) +
599             EXT4_XATTR_SIZE(new_data_size);
600
601         /*
602          * This entry may originally lie in inode space or block space,
603          * and it is going to be transferred to another place.
604          */
605         if (to_block) {
606                 xattr_ref->inode_size_rem +=
607                         EXT4_XATTR_SIZE(old_data_size) +
608                         EXT4_XATTR_LEN(item->name_len);
609                 xattr_ref->block_size_rem -=
610                         EXT4_XATTR_SIZE(new_data_size) +
611                         EXT4_XATTR_LEN(item->name_len);
612                 item->in_inode = false;
613         } else if (to_inode) {
614                 xattr_ref->block_size_rem +=
615                         EXT4_XATTR_SIZE(old_data_size) +
616                         EXT4_XATTR_LEN(item->name_len);
617                 xattr_ref->inode_size_rem -=
618                         EXT4_XATTR_SIZE(new_data_size) +
619                         EXT4_XATTR_LEN(item->name_len);
620                 item->in_inode = true;
621         } else {
622                 /*
623                  * No need to transfer as there is enough space for the entry
624                  * to stay in inode space or block space it used to be.
625                  */
626                 orig_room_size +=
627                         EXT4_XATTR_SIZE(old_data_size);
628                 orig_room_size -=
629                         EXT4_XATTR_SIZE(new_data_size);
630                 if (item->in_inode)
631                         xattr_ref->inode_size_rem = orig_room_size;
632                 else
633                         xattr_ref->block_size_rem = orig_room_size;
634
635         }
636         xattr_ref->dirty = true;
637         return ret;
638 }
639
640 static void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
641 {
642         struct ext4_xattr_item *item, *save_item;
643         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item) {
644                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
645                 ext4_xattr_item_free(item);
646         }
647         xattr_ref->ea_size = 0;
648         xattr_ref->inode_size_rem = ext4_xattr_inode_space(xattr_ref) -
649                 sizeof(struct ext4_xattr_ibody_header);
650         if (xattr_ref->inode_size_rem < 0)
651                 xattr_ref->inode_size_rem = 0;
652
653         xattr_ref->block_size_rem = ext4_xattr_block_space(xattr_ref) -
654                 sizeof(struct ext4_xattr_header);
655 }
656
657 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
658 {
659         int ret = EOK;
660
661         ext4_fsblk_t xattr_block = 0;
662         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
663                                               &xattr_ref->fs->sb);
664         if (!xattr_block) {
665                 ext4_fsblk_t goal =
666                         ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
667
668                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
669                                               goal,
670                                               &xattr_block);
671                 if (ret != EOK)
672                         goto Finish;
673
674                 ret = ext4_trans_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
675                                      xattr_block);
676                 if (ret != EOK) {
677                         ext4_balloc_free_block(xattr_ref->inode_ref,
678                                                xattr_block);
679                         goto Finish;
680                 }
681
682                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
683                                         &xattr_ref->fs->sb, xattr_block);
684                 xattr_ref->inode_ref->dirty = true;
685                 xattr_ref->block_loaded = true;
686         }
687
688 Finish:
689         return ret;
690 }
691
692 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
693 {
694         ext4_fsblk_t xattr_block;
695         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
696                                               &xattr_ref->fs->sb);
697         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
698                                 0);
699         ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
700         ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
701         xattr_ref->inode_ref->dirty = true;
702         xattr_ref->block_loaded = false;
703 }
704
705 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
706 {
707         struct ext4_xattr_header *block_header = NULL;
708         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
709
710         memset(block_header, 0, sizeof(struct ext4_xattr_header));
711         block_header->h_magic = EXT4_XATTR_MAGIC;
712         block_header->h_refcount = to_le32(1);
713         block_header->h_blocks = to_le32(1);
714 }
715
716 static void
717 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
718                            struct ext4_xattr_ibody_header *ibody_header,
719                            struct ext4_xattr_entry *entry, void *ibody_data_ptr)
720 {
721         entry->e_name_len = (uint8_t)item->name_len;
722         entry->e_name_index = item->name_index;
723         entry->e_value_offs =
724             to_le16((char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header));
725         entry->e_value_block = 0;
726         entry->e_value_size = to_le32(item->data_size);
727 }
728
729 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
730                                        struct ext4_xattr_header *block_header,
731                                        struct ext4_xattr_entry *block_entry,
732                                        void *block_data_ptr)
733 {
734         block_entry->e_name_len = (uint8_t)item->name_len;
735         block_entry->e_name_index = item->name_index;
736         block_entry->e_value_offs =
737             to_le16((char *)block_data_ptr - (char *)block_header);
738         block_entry->e_value_block = 0;
739         block_entry->e_value_size = to_le32(item->data_size);
740 }
741
742 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
743 {
744         int ret = EOK;
745         bool block_modified = false;
746         void *ibody_data = NULL;
747         void *block_data = NULL;
748         struct ext4_xattr_item *item, *save_item;
749         size_t inode_size_rem, block_size_rem;
750         struct ext4_xattr_ibody_header *ibody_header = NULL;
751         struct ext4_xattr_header *block_header = NULL;
752         struct ext4_xattr_entry *entry = NULL;
753         struct ext4_xattr_entry *block_entry = NULL;
754
755         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
756         block_size_rem = ext4_xattr_block_space(xattr_ref);
757         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
758                 ibody_header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
759                                                xattr_ref->inode_ref->inode);
760                 entry = EXT4_XATTR_IFIRST(ibody_header);
761         }
762
763         if (!xattr_ref->dirty)
764                 goto Finish;
765         /* If there are enough spaces in the ibody EA table.*/
766         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
767                 memset(ibody_header, 0, inode_size_rem);
768                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
769                 ibody_data = (char *)ibody_header + inode_size_rem;
770                 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
771
772                 xattr_ref->inode_ref->dirty = true;
773         }
774         /* If we need an extra block to hold the EA entries*/
775         if (xattr_ref->ea_size > inode_size_rem) {
776                 if (!xattr_ref->block_loaded) {
777                         ret = ext4_xattr_try_alloc_block(xattr_ref);
778                         if (ret != EOK)
779                                 goto Finish;
780                 }
781                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
782                 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
783                 ext4_xattr_set_block_header(xattr_ref);
784                 block_data = (char *)block_header + block_size_rem;
785                 block_size_rem -= sizeof(struct ext4_xattr_header);
786
787                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
788         } else {
789                 /* We don't need an extra block.*/
790                 if (xattr_ref->block_loaded) {
791                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
792                         block_header->h_refcount =
793                             to_le32(to_le32(block_header->h_refcount) - 1);
794                         if (!block_header->h_refcount) {
795                                 ext4_xattr_try_free_block(xattr_ref);
796                                 block_header = NULL;
797                         } else {
798                                 block_entry =
799                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
800                                 block_data =
801                                     (char *)block_header + block_size_rem;
802                                 block_size_rem -=
803                                     sizeof(struct ext4_xattr_header);
804                                 ext4_inode_set_file_acl(
805                                     xattr_ref->inode_ref->inode,
806                                     &xattr_ref->fs->sb, 0);
807
808                                 xattr_ref->inode_ref->dirty = true;
809                                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
810                         }
811                 }
812         }
813         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
814         {
815                 if (EXT4_XATTR_SIZE(item->data_size) +
816                         EXT4_XATTR_LEN(item->name_len) <=
817                     inode_size_rem) {
818                         ibody_data = (char *)ibody_data -
819                                      EXT4_XATTR_SIZE(item->data_size);
820                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
821                                                    ibody_data);
822                         memcpy(EXT4_XATTR_NAME(entry), item->name,
823                                item->name_len);
824                         memcpy(ibody_data, item->data, item->data_size);
825                         entry = EXT4_XATTR_NEXT(entry);
826                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
827                                           EXT4_XATTR_LEN(item->name_len);
828
829                         xattr_ref->inode_ref->dirty = true;
830                         continue;
831                 }
832                 if (EXT4_XATTR_SIZE(item->data_size) +
833                         EXT4_XATTR_LEN(item->name_len) >
834                     block_size_rem) {
835                         ret = ENOSPC;
836                         ext4_dbg(DEBUG_XATTR, "IMPOSSIBLE ENOSPC AS WE DID INSPECTION!\n");
837                         ext4_assert(0);
838                 }
839                 block_data =
840                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
841                 ext4_xattr_set_block_entry(item, block_header, block_entry,
842                                            block_data);
843                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
844                        item->name_len);
845                 memcpy(block_data, item->data, item->data_size);
846                 block_entry = EXT4_XATTR_NEXT(block_entry);
847                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
848                                   EXT4_XATTR_LEN(item->name_len);
849
850                 block_modified = true;
851         }
852         xattr_ref->dirty = false;
853         if (block_modified) {
854                 ext4_xattr_rehash(block_header,
855                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
856                 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
857                                               xattr_ref->block.lb_id,
858                                               block_header);
859                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
860         }
861
862 Finish:
863         return ret;
864 }
865
866 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
867                            int (*iter)(struct ext4_xattr_ref *ref,
868                                      struct ext4_xattr_item *item))
869 {
870         struct ext4_xattr_item *item;
871         if (!ref->iter_from)
872                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
873
874         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
875         {
876                 int ret = EXT4_XATTR_ITERATE_CONT;
877                 if (iter)
878                         iter(ref, item);
879
880                 if (ret != EXT4_XATTR_ITERATE_CONT) {
881                         if (ret == EXT4_XATTR_ITERATE_STOP)
882                                 ref->iter_from = NULL;
883
884                         break;
885                 }
886         }
887 }
888
889 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
890 {
891         ref->iter_from = NULL;
892 }
893
894 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
895                       const char *name, size_t name_len, const void *data,
896                       size_t data_size, bool replace)
897 {
898         int ret = EOK;
899         struct ext4_xattr_item *item =
900             ext4_xattr_lookup_item(ref, name_index, name, name_len);
901         if (replace) {
902                 if (!item) {
903                         ret = ENODATA;
904                         goto Finish;
905                 }
906                 if (item->data_size != data_size)
907                         ret = ext4_xattr_resize_item(ref, item, data_size);
908
909                 if (ret != EOK) {
910                         goto Finish;
911                 }
912                 memcpy(item->data, data, data_size);
913         } else {
914                 if (item) {
915                         ret = EEXIST;
916                         goto Finish;
917                 }
918                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
919                                               data, data_size);
920                 if (!item)
921                         ret = ENOMEM;
922         }
923 Finish:
924         return ret;
925 }
926
927 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
928                          const char *name, size_t name_len)
929 {
930         return ext4_xattr_remove_item(ref, name_index, name, name_len);
931 }
932
933 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
934                       const char *name, size_t name_len, void *buf,
935                       size_t buf_size, size_t *data_size)
936 {
937         int ret = EOK;
938         size_t item_size = 0;
939         struct ext4_xattr_item *item =
940             ext4_xattr_lookup_item(ref, name_index, name, name_len);
941
942         if (!item) {
943                 ret = ENODATA;
944                 goto Finish;
945         }
946         item_size = item->data_size;
947         if (buf_size > item_size)
948                 buf_size = item_size;
949
950         if (buf)
951                 memcpy(buf, item->data, buf_size);
952
953 Finish:
954         if (data_size)
955                 *data_size = item_size;
956
957         return ret;
958 }
959
960 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
961                           struct ext4_xattr_ref *ref)
962 {
963         int rc;
964         ext4_fsblk_t xattr_block;
965         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
966         RB_INIT(&ref->root);
967         ref->ea_size = 0;
968         ref->iter_from = NULL;
969         if (xattr_block) {
970                 rc = ext4_trans_block_get(fs->bdev, &ref->block, xattr_block);
971                 if (rc != EOK)
972                         return EIO;
973
974                 ref->block_loaded = true;
975         } else
976                 ref->block_loaded = false;
977
978         ref->inode_ref = inode_ref;
979         ref->fs = fs;
980
981         ref->inode_size_rem = ext4_xattr_inode_space(ref) -
982                 sizeof(struct ext4_xattr_ibody_header);
983         if (ref->inode_size_rem < 0)
984                 ref->inode_size_rem = 0;
985
986         ref->block_size_rem = ext4_xattr_block_space(ref) -
987                 sizeof(struct ext4_xattr_header);
988         rc = ext4_xattr_fetch(ref);
989         if (rc != EOK) {
990                 ext4_xattr_purge_items(ref);
991                 if (xattr_block)
992                         ext4_block_set(fs->bdev, &ref->block);
993
994                 ref->block_loaded = false;
995                 return rc;
996         }
997         return EOK;
998 }
999
1000 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
1001 {
1002         ext4_xattr_write_to_disk(ref);
1003         if (ref->block_loaded) {
1004                 ext4_block_set(ref->fs->bdev, &ref->block);
1005                 ref->block_loaded = false;
1006         }
1007         ext4_xattr_purge_items(ref);
1008         ref->inode_ref = NULL;
1009         ref->fs = NULL;
1010 }
1011
1012 struct xattr_prefix {
1013         const char *prefix;
1014         uint8_t name_index;
1015 };
1016
1017 static const struct xattr_prefix prefix_tbl[] = {
1018     {"user.", EXT4_XATTR_INDEX_USER},
1019     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
1020     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
1021     {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
1022     {"security.", EXT4_XATTR_INDEX_SECURITY},
1023     {"system.", EXT4_XATTR_INDEX_SYSTEM},
1024     {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
1025     {NULL, 0},
1026 };
1027
1028 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
1029                               uint8_t *name_index, size_t *name_len,
1030                               bool *found)
1031 {
1032         int i;
1033         ext4_assert(name_index);
1034         ext4_assert(found);
1035
1036         *found = false;
1037
1038         if (!full_name_len) {
1039                 if (name_len)
1040                         *name_len = 0;
1041
1042                 return NULL;
1043         }
1044
1045         for (i = 0; prefix_tbl[i].prefix; i++) {
1046                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1047                 if (full_name_len >= prefix_len &&
1048                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
1049                         bool require_name =
1050                                 prefix_tbl[i].prefix[prefix_len - 1] == '.';
1051                         *name_index = prefix_tbl[i].name_index;
1052                         if (name_len)
1053                                 *name_len = full_name_len - prefix_len;
1054
1055                         if (!(full_name_len - prefix_len) && require_name)
1056                                 return NULL;
1057
1058                         *found = true;
1059                         if (require_name)
1060                                 return full_name + prefix_len;
1061
1062                         return NULL;
1063                 }
1064         }
1065         if (name_len)
1066                 *name_len = 0;
1067
1068         return NULL;
1069 }
1070
1071 const char *ext4_get_xattr_name_prefix(uint8_t name_index,
1072                                        size_t *ret_prefix_len)
1073 {
1074         int i;
1075
1076         for (i = 0; prefix_tbl[i].prefix; i++) {
1077                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1078                 if (prefix_tbl[i].name_index == name_index) {
1079                         if (ret_prefix_len)
1080                                 *ret_prefix_len = prefix_len;
1081
1082                         return prefix_tbl[i].prefix;
1083                 }
1084         }
1085         if (ret_prefix_len)
1086                 *ret_prefix_len = 0;
1087
1088         return NULL;
1089 }
1090
1091 /**
1092  * @}
1093  */