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