08cf45931997384a4265b4b2837a3099dd0ab659
[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                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
454                 ext4_xattr_item_free(item);
455                 xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
456                                         EXT4_XATTR_LEN(item->name_len);
457                 xattr_ref->dirty = true;
458                 ret = EOK;
459         }
460         return ret;
461 }
462
463 static int
464 ext4_xattr_resize_item(struct ext4_xattr_ref *xattr_ref,
465                        struct ext4_xattr_item *item,
466                        size_t new_data_size)
467 {
468         int ret = EOK;
469         if (xattr_ref->ea_size - EXT4_XATTR_SIZE(item->data_size) +
470                                 EXT4_XATTR_SIZE(new_data_size) >
471                 ext4_xattr_inode_space(xattr_ref) +
472                 ext4_xattr_block_space(xattr_ref)) {
473
474                 return ENOSPC;
475         }
476         ret = ext4_xattr_item_resize_data(item,
477                                           new_data_size);
478         if (ret != EOK) {
479                 return ret;
480         }
481         xattr_ref->ea_size -= EXT4_XATTR_SIZE(item->data_size) +
482                                 EXT4_XATTR_SIZE(new_data_size);
483         xattr_ref->dirty = true;
484         return ret;
485 }
486
487 static void
488 ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref)
489 {
490         struct ext4_xattr_item *item, *save_item;
491         uint64_t xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
492                                               &xattr_ref->fs->sb);
493         RB_FOREACH_SAFE(item,
494                         ext4_xattr_tree,
495                         &xattr_ref->root,
496                         save_item) {
497                 RB_REMOVE(ext4_xattr_tree, &xattr_ref->root, item);
498                 ext4_xattr_item_free(item);
499         }
500         xattr_ref->ea_size = 0;
501         if (xattr_block)
502                 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
503
504         if (ext4_xattr_inode_space(xattr_ref) >
505             sizeof(struct ext4_xattr_ibody_header))
506                 xattr_ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
507 }
508
509
510 static int
511 ext4_xattr_try_alloc_block(struct ext4_xattr_ref *xattr_ref)
512 {
513         int ret = EOK;
514
515         uint64_t xattr_block = 0;
516         xattr_block = ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
517                                               &xattr_ref->fs->sb);
518         if (!xattr_block) {
519                 ret = ext4_balloc_alloc_block(xattr_ref->inode_ref,
520                                 (uint32_t *)&xattr_block);
521                 if (ret != EOK)
522                         goto Finish;
523
524                 ret = ext4_block_get(xattr_ref->fs->bdev,
525                                 &xattr_ref->block,
526                                 xattr_block);
527                 if (ret != EOK) {
528                         ext4_balloc_free_block(xattr_ref->inode_ref,
529                                         xattr_block);
530                         goto Finish;
531                 }
532
533                 ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
534                                         &xattr_ref->fs->sb,
535                                         xattr_block);
536                 xattr_ref->inode_ref->dirty = true;
537                 xattr_ref->block_loaded = true;
538                 xattr_ref->ea_size += sizeof(struct ext4_xattr_header);
539         }
540
541 Finish:
542         return ret;
543 }
544
545 static void
546 ext4_xattr_try_free_block(struct ext4_xattr_ref *xattr_ref)
547 {
548         uint64_t xattr_block;
549         xattr_block =
550                 ext4_inode_get_file_acl(xattr_ref->inode_ref->inode,
551                                 &xattr_ref->fs->sb);
552         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
553                         &xattr_ref->fs->sb,
554                         0);
555         ext4_block_set(xattr_ref->fs->bdev,
556                         &xattr_ref->block);
557         ext4_balloc_free_block(xattr_ref->inode_ref,
558                         xattr_block);
559         xattr_ref->inode_ref->dirty = true;
560         xattr_ref->block_loaded = false;
561         xattr_ref->ea_size -= sizeof(struct ext4_xattr_header);
562
563 }
564
565 static void
566 ext4_xattr_set_block_header(struct ext4_xattr_ref *xattr_ref)
567 {
568         struct ext4_xattr_header *block_header = NULL;
569         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
570
571         memset(block_header, 0, sizeof(struct ext4_xattr_header));
572         block_header->h_magic = EXT4_XATTR_MAGIC;
573         block_header->h_refcount = to_le32(1);
574         block_header->h_blocks = to_le32(1);
575 }
576
577 static int
578 ext4_xattr_write_to_disk(struct ext4_xattr_ref *xattr_ref)
579 {
580         int ret = EOK;
581         bool block_modified = false;
582         void *ibody_data, *block_data;
583         struct ext4_xattr_item *item, *save_item;
584         size_t inode_size_rem, block_size_rem;
585         struct ext4_xattr_ibody_header *ibody_header = NULL;
586         struct ext4_xattr_header *block_header = NULL;
587         struct ext4_xattr_entry *entry = NULL;
588         struct ext4_xattr_entry *block_entry = NULL;
589
590         inode_size_rem = ext4_xattr_inode_space(xattr_ref);
591         block_size_rem = ext4_xattr_block_space(xattr_ref);
592         if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
593                 ibody_header = EXT4_XATTR_IHDR(xattr_ref->inode_ref->inode);
594                 entry = EXT4_XATTR_IFIRST(ibody_header);
595         }
596
597         if (xattr_ref->dirty) {
598                 /* If there are enough spaces in the ibody EA table.*/
599                 if (inode_size_rem > sizeof(struct ext4_xattr_ibody_header)) {
600                         memset(ibody_header, 0, inode_size_rem);
601                         ibody_header->h_magic = EXT4_XATTR_MAGIC;
602                         ibody_data = (char *)ibody_header + inode_size_rem;
603                         inode_size_rem -= sizeof(struct ext4_xattr_ibody_header);
604
605                         xattr_ref->inode_ref->dirty = true;
606                 }
607                 /* If we need an extra block to hold the EA entries*/
608                 if (xattr_ref->ea_size > inode_size_rem) {
609                         if (!xattr_ref->block_loaded) {
610                                 ret = ext4_xattr_try_alloc_block(xattr_ref);
611                                 if (ret != EOK)
612                                         goto Finish;
613
614                         }
615                         block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
616                         block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
617                         ext4_xattr_set_block_header(xattr_ref);
618                         block_data = (char *)block_header + block_size_rem;
619                         block_size_rem -= sizeof(struct ext4_xattr_header);
620
621                         xattr_ref->block.dirty = true;
622                 } else {
623                         /* We don't need an extra block.*/
624                         if (xattr_ref->block_loaded) {
625                                 block_header = EXT4_XATTR_BHDR(&xattr_ref->block);
626                                 block_header->h_refcount =
627                                         to_le32(to_le32(block_header->h_refcount) - 1);
628                                 if (!block_header->h_refcount) {
629                                         ext4_xattr_try_free_block(xattr_ref);
630                                         block_header = NULL;
631                                 } else {
632                                         block_entry = EXT4_XATTR_BFIRST(&xattr_ref->block);
633                                         block_data = (char *)block_header + block_size_rem;
634                                         block_size_rem -= sizeof(struct ext4_xattr_header);
635                                         ext4_inode_set_file_acl(xattr_ref->inode_ref->inode,
636                                                         &xattr_ref->fs->sb,
637                                                         0);
638
639                                         xattr_ref->inode_ref->dirty = true;
640                                         xattr_ref->block.dirty = true;
641                                 }
642                         }
643                 }
644                 RB_FOREACH_SAFE(item,
645                                 ext4_xattr_tree,
646                                 &xattr_ref->root,
647                                 save_item) {
648                         if (EXT4_XATTR_SIZE(item->data_size) +
649                                 EXT4_XATTR_LEN(item->name_len) <=
650                             inode_size_rem) {
651                                 ibody_data = (char *)ibody_data -
652                                         EXT4_XATTR_SIZE(item->data_size);
653                                 entry->e_name_len   = to_le32(item->name_len);
654                                 entry->e_name_index = item->name_index;
655                                 entry->e_value_offs =
656                                         (char *)ibody_data -
657                                         (char *)EXT4_XATTR_IFIRST(ibody_header);
658                                 entry->e_value_block = 0;
659                                 entry->e_value_size = item->data_size;
660                                 memcpy(EXT4_XATTR_NAME(entry),
661                                         item->name, item->name_len);
662                                 memcpy(ibody_data, item->data, item->data_size);
663                                 entry = EXT4_XATTR_NEXT(entry);
664                                 inode_size_rem -=
665                                         EXT4_XATTR_SIZE(item->data_size) +
666                                         EXT4_XATTR_LEN(item->name_len);
667
668                                 xattr_ref->inode_ref->dirty = true;
669                                 continue;
670                         }
671                         if (EXT4_XATTR_SIZE(item->data_size) +
672                                 EXT4_XATTR_LEN(item->name_len) > block_size_rem) {
673                                 ret = ENOSPC;
674                                 goto Finish;
675                         }
676                         block_data = (char *)block_data -
677                                 EXT4_XATTR_SIZE(item->data_size);
678                         block_entry->e_name_len   = to_le32(item->name_len);
679                         block_entry->e_name_index = item->name_index;
680                         block_entry->e_value_offs =
681                                 (char *)block_data - (char *)block_header;
682                         block_entry->e_value_block = 0;
683                         block_entry->e_value_size = item->data_size;
684                         memcpy(EXT4_XATTR_NAME(block_entry),
685                                 item->name, item->name_len);
686                         memcpy(block_data,
687                                 item->data, item->data_size);
688                         block_entry = EXT4_XATTR_NEXT(block_entry);
689                         block_size_rem -=
690                                 EXT4_XATTR_SIZE(item->data_size) +
691                                 EXT4_XATTR_LEN(item->name_len);
692
693                         block_modified = true;
694                 }
695                 xattr_ref->dirty = false;
696                 if (block_modified) {
697                         ext4_xattr_rehash(block_header,
698                                           EXT4_XATTR_BFIRST(&xattr_ref->block));
699                         xattr_ref->block.dirty = true;
700                 }
701         }
702
703 Finish:
704         return ret;
705 }
706
707
708 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref,
709                       uint8_t name_index,
710                       char   *name,
711                       size_t  name_len,
712                       void   *data,
713                       size_t  data_size,
714                       bool    replace)
715 {
716         int ret = EOK;
717         struct ext4_xattr_item *item = 
718                 ext4_xattr_lookup_item(ref,
719                                         name_index,
720                                         name,
721                                         name_len);
722         if (replace) {
723                 if (!item) {
724                         ret = ENOATTR;
725                         goto Finish;
726                 }
727                 if (item->data_size != data_size)
728                         ret = ext4_xattr_resize_item(ref,
729                                                      item,
730                                                      data_size);
731
732                 if (ret != EOK) {
733                         goto Finish;
734                 }
735                 memcpy(item->data, data, data_size);
736         } else {
737                 if (item) {
738                         ret = EEXIST;
739                         goto Finish;
740                 }
741                 item = ext4_xattr_insert_item(ref,
742                                               name_index,
743                                               name,
744                                               name_len,
745                                               data,
746                                               data_size);
747                 if (!item)
748                         ret = ENOMEM;
749
750         }
751 Finish:
752         return ret;
753 }
754
755 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref,
756                          uint8_t name_index,
757                          char   *name,
758                          size_t  name_len)
759 {
760         return ext4_xattr_remove_item(ref,
761                                       name_index,
762                                       name,
763                                       name_len);
764 }
765
766 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref,
767                           uint8_t name_index,
768                           char    *name,
769                           size_t   name_len,
770                           void    *buf,
771                           size_t   buf_size,
772                           size_t  *size_got)
773 {
774         int ret = EOK;
775         size_t item_size = 0;
776         struct ext4_xattr_item *item = 
777                 ext4_xattr_lookup_item(ref,
778                                         name_index,
779                                         name,
780                                         name_len);
781
782         if (!item) {
783                 ret = ENOATTR;
784                 goto Finish;
785         }
786         item_size = item->data_size;
787         if (buf_size > item_size)
788                 buf_size = item_size;
789
790         if (buf)
791                 memcpy(buf, item->data, buf_size);
792
793 Finish:
794         if (size_got)
795                 *size_got = buf_size;
796
797         return ret;
798 }
799
800 int ext4_fs_get_xattr_ref(struct ext4_fs *fs,
801                           struct ext4_inode_ref *inode_ref,
802                           struct ext4_xattr_ref *ref)
803 {
804         int rc;
805         uint64_t xattr_block;
806         xattr_block = ext4_inode_get_file_acl(inode_ref->inode,
807                                               &fs->sb);
808         RB_INIT(&ref->root);
809         ref->ea_size = 0;
810         if (xattr_block) {
811                 rc = ext4_block_get(fs->bdev,
812                                     &ref->block, xattr_block);
813                 if (rc != EOK)
814                         return EIO;
815         
816                 ref->ea_size += sizeof(struct ext4_xattr_header);
817                 ref->block_loaded = true;
818         } else
819                 ref->block_loaded = false;
820
821         ref->inode_ref = inode_ref;
822         ref->fs = fs;
823
824         if (ext4_xattr_inode_space(ref) >
825             sizeof(struct ext4_xattr_ibody_header))
826                 ref->ea_size += sizeof(struct ext4_xattr_ibody_header);
827
828         rc = ext4_xattr_fetch(ref);
829         if (rc != EOK) {
830                 ext4_xattr_purge_items(ref);
831                 if (xattr_block)
832                         ext4_block_set(fs->bdev, &inode_ref->block);
833
834                 ref->block_loaded = false;
835                 return rc;
836         }
837         return EOK;
838 }
839
840 void ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref)
841 {
842         ext4_xattr_write_to_disk(ref);
843         if (ref->block_loaded) {
844                 ext4_block_set(ref->fs->bdev, &ref->block);
845                 ref->block_loaded = false;
846         }
847         ext4_xattr_purge_items(ref);
848         ref->inode_ref = NULL;
849         ref->fs = NULL;
850 }
851
852 /**
853  * @}
854  */