ext4_xattr: ext4_xattr_insert_item now returns error code
[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             (int32_t)EXT4_XATTR_SIZE(data_size) -
481             (int32_t)EXT4_XATTR_LEN(item->name_len) < 0) {
482                 if (xattr_ref->block_size_rem -
483                     (int32_t)EXT4_XATTR_SIZE(data_size) -
484                     (int32_t)EXT4_XATTR_LEN(item->name_len) < 0) {
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                                 (int32_t)EXT4_XATTR_SIZE(old_data_size) -
590                                 (int32_t)EXT4_XATTR_SIZE(new_data_size) < 0) {
591                         if (xattr_ref->block_size_rem -
592                                         (int32_t)EXT4_XATTR_SIZE(new_data_size) -
593                                         (int32_t)EXT4_XATTR_LEN(item->name_len) < 0)
594                                 return ENOSPC;
595
596                         to_block = true;
597                 }
598         } else {
599                 if (xattr_ref->block_size_rem +
600                                 (int32_t)EXT4_XATTR_SIZE(old_data_size) -
601                                 (int32_t)EXT4_XATTR_SIZE(new_data_size) < 0) {
602                         if (xattr_ref->inode_size_rem -
603                                         (int32_t)EXT4_XATTR_SIZE(new_data_size) -
604                                         (int32_t)EXT4_XATTR_LEN(item->name_len) < 0)
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         xattr_ref->inode_size_rem = ext4_xattr_inode_space(xattr_ref) -
667                 sizeof(struct ext4_xattr_ibody_header);
668         if (xattr_ref->inode_size_rem < 0)
669                 xattr_ref->inode_size_rem = 0;
670
671         xattr_ref->block_size_rem = ext4_xattr_block_space(xattr_ref) -
672                 sizeof(struct ext4_xattr_header);
673 }
674
675 static int ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
676 {
677         int ret = EOK;
678
679         ext4_fsblk_t xattr_block = 0;
680         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
681                                               &xattr_ref->fs->sb);
682         if (!xattr_block) {
683                 ext4_fsblk_t goal =
684                         ext4_fs_inode_to_goal_block(xattr_ref->inode_ref);
685
686                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
687                                               goal,
688                                               &xattr_block);
689                 if (ret != EOK)
690                         goto Finish;
691
692                 ret = ext4_trans_block_get(xattr_ref->fs->bdev, &xattr_ref->block,
693                                      xattr_block);
694                 if (ret != EOK) {
695                         ext4_balloc_free_block(xattr_ref->inode_ref,
696                                                xattr_block);
697                         goto Finish;
698                 }
699
700                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
701                                         &xattr_ref->fs->sb, xattr_block);
702                 xattr_ref->inode_ref->dirty = true;
703                 xattr_ref->block_loaded = true;
704         }
705
706 Finish:
707         return ret;
708 }
709
710 static void ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
711 {
712         ext4_fsblk_t xattr_block;
713         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
714                                               &xattr_ref->fs->sb);
715         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode, &xattr_ref->fs->sb,
716                                 0);
717         ext4_block_set(xattr_ref->fs->bdev, &xattr_ref->block);
718         ext4_balloc_free_block(xattr_ref->inode_ref, xattr_block);
719         xattr_ref->inode_ref->dirty = true;
720         xattr_ref->block_loaded = false;
721 }
722
723 static void ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
724 {
725         struct ext4_xattr_header *block_header = NULL;
726         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
727
728         memset(block_header, 0, sizeof(struct ext4_xattr_header));
729         block_header->h_magic = EXT4_XATTR_MAGIC;
730         block_header->h_refcount = to_le32(1);
731         block_header->h_blocks = to_le32(1);
732 }
733
734 static void
735 ext4_xattr_set_inode_entry(struct ext4_xattr_item *item,
736                            struct ext4_xattr_ibody_header *ibody_header,
737                            struct ext4_xattr_entry *entry, void *ibody_data_ptr)
738 {
739         entry->e_name_len = (uint8_t)item->name_len;
740         entry->e_name_index = item->name_index;
741         entry->e_value_offs =
742             to_le16((char *)ibody_data_ptr - (char *)EXT4_XATTR_IFIRST(ibody_header));
743         entry->e_value_block = 0;
744         entry->e_value_size = to_le32(item->data_size);
745 }
746
747 static void ext4_xattr_set_block_entry(struct ext4_xattr_item *item,
748                                        struct ext4_xattr_header *block_header,
749                                        struct ext4_xattr_entry *block_entry,
750                                        void *block_data_ptr)
751 {
752         block_entry->e_name_len = (uint8_t)item->name_len;
753         block_entry->e_name_index = item->name_index;
754         block_entry->e_value_offs =
755             to_le16((char *)block_data_ptr - (char *)block_header);
756         block_entry->e_value_block = 0;
757         block_entry->e_value_size = to_le32(item->data_size);
758 }
759
760 static int ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
761 {
762         int ret = EOK;
763         bool block_modified = false;
764         void *ibody_data = NULL;
765         void *block_data = NULL;
766         struct ext4_xattr_item *item, *save_item;
767         size_t inode_size_rem, block_size_rem;
768         struct ext4_xattr_ibody_header *ibody_header = NULL;
769         struct ext4_xattr_header *block_header = NULL;
770         struct ext4_xattr_entry *entry = NULL;
771         struct ext4_xattr_entry *block_entry = NULL;
772
773         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
774         block_size_rem = ext4_xattr_block_space(xattr_ref);
775         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
776                 ibody_header = EXT4_XATTR_IHDR(&xattr_ref->fs->sb,
777                                                xattr_ref->inode_ref->inode);
778                 entry = EXT4_XATTR_IFIRST(ibody_header);
779         }
780
781         if (!xattr_ref->dirty)
782                 goto Finish;
783         /* If there are enough spaces in the ibody EA table.*/
784         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
785                 memset(ibody_header, 0, inode_size_rem);
786                 ibody_header->h_magic = EXT4_XATTR_MAGIC;
787                 ibody_data = (char *)ibody_header + inode_size_rem;
788                 inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
789
790                 xattr_ref->inode_ref->dirty = true;
791         }
792         /* If we need an extra block to hold the EA entries*/
793         if (xattr_ref->ea_size > inode_size_rem) {
794                 if (!xattr_ref->block_loaded) {
795                         ret = ext4_xattr_try_alloc_block(xattr_ref);
796                         if (ret != EOK)
797                                 goto Finish;
798                 }
799                 memset(xattr_ref->block.data, 0,
800                        ext4_sb_get_block_size(&xattr_ref->fs->sb));
801                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
802                 block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
803                 ext4_xattr_set_block_header(xattr_ref);
804                 block_data = (char *)block_header + block_size_rem;
805                 block_size_rem -= sizeof(struct ext4_xattr_header);
806
807                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
808         } else {
809                 /* We don't need an extra block.*/
810                 if (xattr_ref->block_loaded) {
811                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
812                         block_header->h_refcount =
813                             to_le32(to_le32(block_header->h_refcount) - 1);
814                         if (!block_header->h_refcount) {
815                                 ext4_xattr_try_free_block(xattr_ref);
816                                 block_header = NULL;
817                         } else {
818                                 block_entry =
819                                     EXT4_XATTR_BFIRST(&xattr_ref->block);
820                                 block_data =
821                                     (char *)block_header + block_size_rem;
822                                 block_size_rem -=
823                                     sizeof(struct ext4_xattr_header);
824                                 ext4_inode_set_file_acl(
825                                     xattr_ref->inode_ref->inode,
826                                     &xattr_ref->fs->sb, 0);
827
828                                 xattr_ref->inode_ref->dirty = true;
829                                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
830                         }
831                 }
832         }
833         RB_FOREACH_SAFE(item, ext4_xattr_tree, &xattr_ref->root, save_item)
834         {
835                 if (EXT4_XATTR_SIZE(item->data_size) +
836                         EXT4_XATTR_LEN(item->name_len) <=
837                     inode_size_rem) {
838                         ibody_data = (char *)ibody_data -
839                                      EXT4_XATTR_SIZE(item->data_size);
840                         ext4_xattr_set_inode_entry(item, ibody_header, entry,
841                                                    ibody_data);
842                         memcpy(EXT4_XATTR_NAME(entry), item->name,
843                                item->name_len);
844                         memcpy(ibody_data, item->data, item->data_size);
845                         entry = EXT4_XATTR_NEXT(entry);
846                         inode_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
847                                           EXT4_XATTR_LEN(item->name_len);
848
849                         xattr_ref->inode_ref->dirty = true;
850                         continue;
851                 }
852                 if (EXT4_XATTR_SIZE(item->data_size) +
853                         EXT4_XATTR_LEN(item->name_len) >
854                     block_size_rem) {
855                         ret = ENOSPC;
856                         ext4_dbg(DEBUG_XATTR, "IMPOSSIBLE ENOSPC AS WE DID INSPECTION!\n");
857                         ext4_assert(0);
858                 }
859                 block_data =
860                     (char *)block_data - EXT4_XATTR_SIZE(item->data_size);
861                 ext4_xattr_set_block_entry(item, block_header, block_entry,
862                                            block_data);
863                 memcpy(EXT4_XATTR_NAME(block_entry), item->name,
864                        item->name_len);
865                 memcpy(block_data, item->data, item->data_size);
866                 block_entry = EXT4_XATTR_NEXT(block_entry);
867                 block_size_rem -= EXT4_XATTR_SIZE(item->data_size) +
868                                   EXT4_XATTR_LEN(item->name_len);
869
870                 block_modified = true;
871         }
872         xattr_ref->dirty = false;
873         if (block_modified) {
874                 ext4_xattr_rehash(block_header,
875                                   EXT4_XATTR_BFIRST(&xattr_ref->block));
876                 ext4_xattr_set_block_checksum(xattr_ref->inode_ref,
877                                               xattr_ref->block.lb_id,
878                                               block_header);
879                 ext4_trans_set_block_dirty(xattr_ref->block.buf);
880         }
881
882 Finish:
883         return ret;
884 }
885
886 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
887                            int (*iter)(struct ext4_xattr_ref *ref,
888                                      struct ext4_xattr_item *item))
889 {
890         struct ext4_xattr_item *item;
891         if (!ref->iter_from)
892                 ref->iter_from = RB_MIN(ext4_xattr_tree, &ref->root);
893
894         RB_FOREACH_FROM(item, ext4_xattr_tree, ref->iter_from)
895         {
896                 int ret = EXT4_XATTR_ITERATE_CONT;
897                 if (iter)
898                         iter(ref, item);
899
900                 if (ret != EXT4_XATTR_ITERATE_CONT) {
901                         if (ret == EXT4_XATTR_ITERATE_STOP)
902                                 ref->iter_from = NULL;
903
904                         break;
905                 }
906         }
907 }
908
909 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref)
910 {
911         ref->iter_from = NULL;
912 }
913
914 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
915                       const char *name, size_t name_len, const void *data,
916                       size_t data_size, bool replace)
917 {
918         int ret = EOK;
919         struct ext4_xattr_item *item =
920             ext4_xattr_lookup_item(ref, name_index, name, name_len);
921         if (replace) {
922                 if (!item) {
923                         ret = ENODATA;
924                         goto Finish;
925                 }
926                 if (item->data_size != data_size)
927                         ret = ext4_xattr_resize_item(ref, item, data_size);
928
929                 if (ret != EOK) {
930                         goto Finish;
931                 }
932                 memcpy(item->data, data, data_size);
933         } else {
934                 if (item) {
935                         ret = EEXIST;
936                         goto Finish;
937                 }
938                 item = ext4_xattr_insert_item(ref, name_index, name, name_len,
939                                               data, data_size, &ret);
940         }
941 Finish:
942         return ret;
943 }
944
945 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
946                          const char *name, size_t name_len)
947 {
948         return ext4_xattr_remove_item(ref, name_index, name, name_len);
949 }
950
951 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, uint8_t name_index,
952                       const char *name, size_t name_len, void *buf,
953                       size_t buf_size, size_t *data_size)
954 {
955         int ret = EOK;
956         size_t item_size = 0;
957         struct ext4_xattr_item *item =
958             ext4_xattr_lookup_item(ref, name_index, name, name_len);
959
960         if (!item) {
961                 ret = ENODATA;
962                 goto Finish;
963         }
964         item_size = item->data_size;
965         if (buf_size > item_size)
966                 buf_size = item_size;
967
968         if (buf)
969                 memcpy(buf, item->data, buf_size);
970
971 Finish:
972         if (data_size)
973                 *data_size = item_size;
974
975         return ret;
976 }
977
978 int ext4_fs_get_xattr_ref(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
979                           struct ext4_xattr_ref *ref)
980 {
981         int rc;
982         ext4_fsblk_t xattr_block;
983         xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
984         RB_INIT(&ref->root);
985         ref->ea_size = 0;
986         ref->iter_from = NULL;
987         if (xattr_block) {
988                 rc = ext4_trans_block_get(fs->bdev, &ref->block, xattr_block);
989                 if (rc != EOK)
990                         return EIO;
991
992                 ref->block_loaded = true;
993         } else
994                 ref->block_loaded = false;
995
996         ref->inode_ref = inode_ref;
997         ref->fs = fs;
998
999         ref->inode_size_rem = ext4_xattr_inode_space(ref) -
1000                 sizeof(struct ext4_xattr_ibody_header);
1001         if (ref->inode_size_rem < 0)
1002                 ref->inode_size_rem = 0;
1003
1004         ref->block_size_rem = ext4_xattr_block_space(ref) -
1005                 sizeof(struct ext4_xattr_header);
1006         rc = ext4_xattr_fetch(ref);
1007         if (rc != EOK) {
1008                 ext4_xattr_purge_items(ref);
1009                 if (xattr_block)
1010                         ext4_block_set(fs->bdev, &ref->block);
1011
1012                 ref->block_loaded = false;
1013                 return rc;
1014         }
1015         return EOK;
1016 }
1017
1018 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
1019 {
1020         int rc = ext4_xattr_write_to_disk(ref);
1021         if (ref->block_loaded) {
1022                 if (rc != EOK)
1023                         ext4_bcache_clear_dirty(ref->block.buf);
1024
1025                 ext4_block_set(ref->fs->bdev, &ref->block);
1026                 ref->block_loaded = false;
1027         }
1028         ext4_xattr_purge_items(ref);
1029         ref->inode_ref = NULL;
1030         ref->fs = NULL;
1031 }
1032
1033 struct xattr_prefix {
1034         const char *prefix;
1035         uint8_t name_index;
1036 };
1037
1038 static const struct xattr_prefix prefix_tbl[] = {
1039     {"user.", EXT4_XATTR_INDEX_USER},
1040     {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
1041     {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
1042     {"trusted.", EXT4_XATTR_INDEX_TRUSTED},
1043     {"security.", EXT4_XATTR_INDEX_SECURITY},
1044     {"system.", EXT4_XATTR_INDEX_SYSTEM},
1045     {"system.richacl", EXT4_XATTR_INDEX_RICHACL},
1046     {NULL, 0},
1047 };
1048
1049 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
1050                               uint8_t *name_index, size_t *name_len,
1051                               bool *found)
1052 {
1053         int i;
1054         ext4_assert(name_index);
1055         ext4_assert(found);
1056
1057         *found = false;
1058
1059         if (!full_name_len) {
1060                 if (name_len)
1061                         *name_len = 0;
1062
1063                 return NULL;
1064         }
1065
1066         for (i = 0; prefix_tbl[i].prefix; i++) {
1067                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1068                 if (full_name_len >= prefix_len &&
1069                     !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
1070                         bool require_name =
1071                                 prefix_tbl[i].prefix[prefix_len - 1] == '.';
1072                         *name_index = prefix_tbl[i].name_index;
1073                         if (name_len)
1074                                 *name_len = full_name_len - prefix_len;
1075
1076                         if (!(full_name_len - prefix_len) && require_name)
1077                                 return NULL;
1078
1079                         *found = true;
1080                         if (require_name)
1081                                 return full_name + prefix_len;
1082
1083                         return NULL;
1084                 }
1085         }
1086         if (name_len)
1087                 *name_len = 0;
1088
1089         return NULL;
1090 }
1091
1092 const char *ext4_get_xattr_name_prefix(uint8_t name_index,
1093                                        size_t *ret_prefix_len)
1094 {
1095         int i;
1096
1097         for (i = 0; prefix_tbl[i].prefix; i++) {
1098                 size_t prefix_len = strlen(prefix_tbl[i].prefix);
1099                 if (prefix_tbl[i].name_index == name_index) {
1100                         if (ret_prefix_len)
1101                                 *ret_prefix_len = prefix_len;
1102
1103                         return prefix_tbl[i].prefix;
1104                 }
1105         }
1106         if (ret_prefix_len)
1107                 *ret_prefix_len = 0;
1108
1109         return NULL;
1110 }
1111
1112 /**
1113  * @}
1114  */