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