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