forked from gkostka/lwext4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ext4_xattr.c
1564 lines (1348 loc) · 40.1 KB
/
ext4_xattr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Grzegorz Kostka ([email protected])
* Copyright (c) 2017 Kaho Ng ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
/** @addtogroup lwext4
* @{
*/
/**
* @file ext4_xattr.c
* @brief Extended Attribute manipulation.
*/
#include <ext4_config.h>
#include <ext4_debug.h>
#include <ext4_errno.h>
#include <ext4_misc.h>
#include <ext4_types.h>
#include <ext4_balloc.h>
#include <ext4_block_group.h>
#include <ext4_blockdev.h>
#include <ext4_crc32.h>
#include <ext4_fs.h>
#include <ext4_inode.h>
#include <ext4_super.h>
#include <ext4_trans.h>
#include <ext4_xattr.h>
#include <stdlib.h>
#include <string.h>
#if CONFIG_XATTR_ENABLE
/**
* @file ext4_xattr.c
* @brief Extended Attribute Manipulation
*/
/* Extended Attribute(EA) */
/* Magic value in attribute blocks */
#define EXT4_XATTR_MAGIC 0xEA020000
/* Maximum number of references to one attribute block */
#define EXT4_XATTR_REFCOUNT_MAX 1024
/* Name indexes */
#define EXT4_XATTR_INDEX_USER 1
#define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS 2
#define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT 3
#define EXT4_XATTR_INDEX_TRUSTED 4
#define EXT4_XATTR_INDEX_LUSTRE 5
#define EXT4_XATTR_INDEX_SECURITY 6
#define EXT4_XATTR_INDEX_SYSTEM 7
#define EXT4_XATTR_INDEX_RICHACL 8
#define EXT4_XATTR_INDEX_ENCRYPTION 9
#define EXT4_XATTR_PAD_BITS 2
#define EXT4_XATTR_PAD (1 << EXT4_XATTR_PAD_BITS)
#define EXT4_XATTR_ROUND (EXT4_XATTR_PAD - 1)
#define EXT4_XATTR_LEN(name_len) \
(((name_len) + EXT4_XATTR_ROUND + sizeof(struct ext4_xattr_entry)) & \
~EXT4_XATTR_ROUND)
#define EXT4_XATTR_NEXT(entry) \
((struct ext4_xattr_entry *)((char *)(entry) + \
EXT4_XATTR_LEN((entry)->e_name_len)))
#define EXT4_XATTR_SIZE(size) (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND)
#define EXT4_XATTR_NAME(entry) ((char *)((entry) + 1))
#define EXT4_XATTR_IHDR(sb, raw_inode) \
((struct ext4_xattr_ibody_header *)((char *)raw_inode + \
EXT4_GOOD_OLD_INODE_SIZE + \
ext4_inode_get_extra_isize( \
sb, raw_inode)))
#define EXT4_XATTR_IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr) + 1))
#define EXT4_XATTR_BHDR(block) ((struct ext4_xattr_header *)((block)->data))
#define EXT4_XATTR_ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
#define EXT4_XATTR_BFIRST(block) EXT4_XATTR_ENTRY(EXT4_XATTR_BHDR(block) + 1)
#define EXT4_XATTR_IS_LAST_ENTRY(entry) (*(uint32_t *)(entry) == 0)
#define EXT4_ZERO_XATTR_VALUE ((void *)-1)
#pragma pack(push, 1)
struct ext4_xattr_header {
uint32_t h_magic; /* magic number for identification */
uint32_t h_refcount; /* reference count */
uint32_t h_blocks; /* number of disk blocks used */
uint32_t h_hash; /* hash value of all attributes */
uint32_t h_checksum; /* crc32c(uuid+id+xattrblock) */
/* id = inum if refcount=1, blknum otherwise */
uint32_t h_reserved[3]; /* zero right now */
};
struct ext4_xattr_ibody_header {
uint32_t h_magic; /* magic number for identification */
};
struct ext4_xattr_entry {
uint8_t e_name_len; /* length of name */
uint8_t e_name_index; /* attribute name index */
uint16_t e_value_offs; /* offset in disk block of value */
uint32_t e_value_block; /* disk block attribute is stored on (n/i) */
uint32_t e_value_size; /* size of attribute value */
uint32_t e_hash; /* hash value of name and value */
};
#pragma pack(pop)
#define NAME_HASH_SHIFT 5
#define VALUE_HASH_SHIFT 16
static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header,
struct ext4_xattr_entry *entry)
{
uint32_t hash = 0;
char *name = EXT4_XATTR_NAME(entry);
int n;
for (n = 0; n < entry->e_name_len; n++) {
hash = (hash << NAME_HASH_SHIFT) ^
(hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++;
}
if (entry->e_value_block == 0 && entry->e_value_size != 0) {
uint32_t *value =
(uint32_t *)((char *)header + to_le16(entry->e_value_offs));
for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >>
EXT4_XATTR_PAD_BITS;
n; n--) {
hash = (hash << VALUE_HASH_SHIFT) ^
(hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^
to_le32(*value++);
}
}
entry->e_hash = to_le32(hash);
}
#define BLOCK_HASH_SHIFT 16
/*
* ext4_xattr_rehash()
*
* Re-compute the extended attribute hash value after an entry has changed.
*/
static void ext4_xattr_rehash(struct ext4_xattr_header *header,
struct ext4_xattr_entry *entry)
{
struct ext4_xattr_entry *here;
uint32_t hash = 0;
ext4_xattr_compute_hash(header, entry);
here = EXT4_XATTR_ENTRY(header + 1);
while (!EXT4_XATTR_IS_LAST_ENTRY(here)) {
if (!here->e_hash) {
/* Block is not shared if an entry's hash value == 0 */
hash = 0;
break;
}
hash = (hash << BLOCK_HASH_SHIFT) ^
(hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^
to_le32(here->e_hash);
here = EXT4_XATTR_NEXT(here);
}
header->h_hash = to_le32(hash);
}
#if CONFIG_META_CSUM_ENABLE
static uint32_t ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t blocknr,
struct ext4_xattr_header *header)
{
uint32_t checksum = 0;
uint64_t le64_blocknr = blocknr;
struct ext4_sblock *sb = &inode_ref->fs->sb;
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
uint32_t orig_checksum;
/* Preparation: temporarily set bg checksum to 0 */
orig_checksum = header->h_checksum;
header->h_checksum = 0;
/* First calculate crc32 checksum against fs uuid */
checksum =
ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
/* Then calculate crc32 checksum block number */
checksum =
ext4_crc32c(checksum, &le64_blocknr, sizeof(le64_blocknr));
/* Finally calculate crc32 checksum against
* the entire xattr block */
checksum =
ext4_crc32c(checksum, header, ext4_sb_get_block_size(sb));
header->h_checksum = orig_checksum;
}
return checksum;
}
#else
#define ext4_xattr_block_checksum(...) 0
#endif
static void ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t blocknr __unused,
struct ext4_xattr_header *header)
{
struct ext4_sblock *sb = &inode_ref->fs->sb;
if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
return;
header->h_checksum =
ext4_xattr_block_checksum(inode_ref, blocknr, header);
}
struct xattr_prefix {
const char *prefix;
uint8_t name_index;
};
static const struct xattr_prefix prefix_tbl[] = {
{"user.", EXT4_XATTR_INDEX_USER},
{"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS},
{"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT},
{"trusted.", EXT4_XATTR_INDEX_TRUSTED},
{"security.", EXT4_XATTR_INDEX_SECURITY},
{"system.", EXT4_XATTR_INDEX_SYSTEM},
{"system.richacl", EXT4_XATTR_INDEX_RICHACL},
{NULL, 0},
};
const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
uint8_t *name_index, size_t *name_len,
bool *found)
{
int i;
ext4_assert(name_index);
ext4_assert(found);
*found = false;
if (!full_name_len) {
if (name_len)
*name_len = 0;
return NULL;
}
for (i = 0; prefix_tbl[i].prefix; i++) {
size_t prefix_len = strlen(prefix_tbl[i].prefix);
if (full_name_len >= prefix_len &&
!memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) {
bool require_name =
prefix_tbl[i].prefix[prefix_len - 1] == '.';
*name_index = prefix_tbl[i].name_index;
if (name_len)
*name_len = full_name_len - prefix_len;
if (!(full_name_len - prefix_len) && require_name)
return NULL;
*found = true;
if (require_name)
return full_name + prefix_len;
return NULL;
}
}
if (name_len)
*name_len = 0;
return NULL;
}
const char *ext4_get_xattr_name_prefix(uint8_t name_index,
size_t *ret_prefix_len)
{
int i;
for (i = 0; prefix_tbl[i].prefix; i++) {
size_t prefix_len = strlen(prefix_tbl[i].prefix);
if (prefix_tbl[i].name_index == name_index) {
if (ret_prefix_len)
*ret_prefix_len = prefix_len;
return prefix_tbl[i].prefix;
}
}
if (ret_prefix_len)
*ret_prefix_len = 0;
return NULL;
}
static const char ext4_xattr_empty_value;
/**
* @brief Insert/Remove/Modify the given entry
*
* @param i The information of the given EA entry
* @param s Search context block
* @param dry_run Do not modify the content of the buffer
*
* @return Return EOK when finished, ENOSPC when there is no enough space
*/
static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
struct ext4_xattr_search *s, bool dry_run)
{
struct ext4_xattr_entry *last;
size_t free, min_offs = (char *)s->end - (char *)s->base,
name_len = i->name_len;
/*
* If the entry is going to be removed but not found, return 0 to
* indicate success.
*/
if (!i->value && s->not_found)
return EOK;
/* Compute min_offs and last. */
last = s->first;
for (; !EXT4_XATTR_IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
if (last->e_value_size) {
size_t offs = to_le16(last->e_value_offs);
if (offs < min_offs)
min_offs = offs;
}
}
/* Calculate free space in the block. */
free = min_offs - ((char *)last - (char *)s->base) - sizeof(uint32_t);
if (!s->not_found)
free += EXT4_XATTR_SIZE(s->here->e_value_size) +
EXT4_XATTR_LEN(s->here->e_name_len);
if (i->value) {
/* See whether there is enough space to hold new entry */
if (free <
EXT4_XATTR_SIZE(i->value_len) + EXT4_XATTR_LEN(name_len))
return ENOSPC;
}
/* Return EOK now if we do not intend to modify the content. */
if (dry_run)
return EOK;
/* First remove the old entry's data part */
if (!s->not_found) {
size_t value_offs = to_le16(s->here->e_value_offs);
void *value = (char *)s->base + value_offs;
void *first_value = (char *)s->base + min_offs;
size_t value_size =
EXT4_XATTR_SIZE(to_le32(s->here->e_value_size));
if (value_offs) {
/* Remove the data part. */
memmove((char *)first_value + value_size, first_value,
(char *)value - (char *)first_value);
/* Zero the gap created */
memset(first_value, 0, value_size);
/*
* Calculate the new min_offs after removal of the old
* entry's data part
*/
min_offs += value_size;
}
/*
* Adjust the value offset of entries which has value offset
* prior to the s->here. The offset of these entries won't be
* shifted if the size of the entry we removed is zero.
*/
for (last = s->first; !EXT4_XATTR_IS_LAST_ENTRY(last);
last = EXT4_XATTR_NEXT(last)) {
size_t offs = to_le16(last->e_value_offs);
/* For zero-value-length entry, offs will be zero. */
if (offs < value_offs)
last->e_value_offs = to_le16(offs + value_size);
}
}
/* If caller wants us to insert... */
if (i->value) {
size_t value_offs;
if (i->value_len)
value_offs = min_offs - EXT4_XATTR_SIZE(i->value_len);
else
value_offs = 0;
if (!s->not_found) {
struct ext4_xattr_entry *here = s->here;
/* Reuse the current entry we have got */
here->e_value_offs = to_le16(value_offs);
here->e_value_size = to_le32(i->value_len);
} else {
/* Insert a new entry */
last->e_name_len = (uint8_t)name_len;
last->e_name_index = i->name_index;
last->e_value_offs = to_le16(value_offs);
last->e_value_block = 0;
last->e_value_size = to_le32(i->value_len);
memcpy(EXT4_XATTR_NAME(last), i->name, name_len);
/* Set valid last entry indicator */
*(uint32_t *)EXT4_XATTR_NEXT(last) = 0;
s->here = last;
}
/* Insert the value's part */
if (value_offs) {
memcpy((char *)s->base + value_offs, i->value,
i->value_len);
/* Clear the padding bytes if there is */
if (EXT4_XATTR_SIZE(i->value_len) != i->value_len)
memset((char *)s->base + value_offs +
i->value_len,
0, EXT4_XATTR_SIZE(i->value_len) -
i->value_len);
}
} else {
size_t shift_offs;
/* Remove the whole entry */
shift_offs = (char *)EXT4_XATTR_NEXT(s->here) - (char *)s->here;
memmove(s->here, EXT4_XATTR_NEXT(s->here),
(char *)last + sizeof(uint32_t) -
(char *)EXT4_XATTR_NEXT(s->here));
/* Zero the gap created */
memset((char *)last - shift_offs + sizeof(uint32_t), 0,
shift_offs);
s->here = NULL;
}
return EOK;
}
static inline bool ext4_xattr_is_empty(struct ext4_xattr_search *s)
{
if (!EXT4_XATTR_IS_LAST_ENTRY(s->first))
return false;
return true;
}
/**
* @brief Find the entry according to given information
*
* @param i The information of the EA entry to be found,
* including name_index, name and the length of name
* @param s Search context block
*/
static void ext4_xattr_find_entry(struct ext4_xattr_info *i,
struct ext4_xattr_search *s)
{
struct ext4_xattr_entry *entry = NULL;
s->not_found = true;
s->here = NULL;
/*
* Find the wanted EA entry by simply comparing the namespace,
* name and the length of name.
*/
for (entry = s->first; !EXT4_XATTR_IS_LAST_ENTRY(entry);
entry = EXT4_XATTR_NEXT(entry)) {
size_t name_len = entry->e_name_len;
const char *name = EXT4_XATTR_NAME(entry);
if (name_len == i->name_len &&
entry->e_name_index == i->name_index &&
!memcmp(name, i->name, name_len)) {
s->here = entry;
s->not_found = false;
i->value_len = to_le32(entry->e_value_size);
if (i->value_len)
i->value = (char *)s->base +
to_le16(entry->e_value_offs);
else
i->value = NULL;
return;
}
}
}
/**
* @brief Check whether the xattr block's content is valid
*
* @param inode_ref Inode reference
* @param block The block buffer to be validated
*
* @return true if @block is valid, false otherwise.
*/
static bool ext4_xattr_is_block_valid(struct ext4_inode_ref *inode_ref,
struct ext4_block *block)
{
void *base = block->data,
*end = block->data + ext4_sb_get_block_size(&inode_ref->fs->sb);
size_t min_offs = (char *)end - (char *)base;
struct ext4_xattr_header *header = EXT4_XATTR_BHDR(block);
struct ext4_xattr_entry *entry = EXT4_XATTR_BFIRST(block);
/*
* Check whether the magic number in the header is correct.
*/
if (header->h_magic != to_le32(EXT4_XATTR_MAGIC))
return false;
/*
* The in-kernel filesystem driver only supports 1 block currently.
*/
if (header->h_blocks != to_le32(1))
return false;
/*
* Check if those entries are maliciously corrupted to inflict harm
* upon us.
*/
for (; !EXT4_XATTR_IS_LAST_ENTRY(entry);
entry = EXT4_XATTR_NEXT(entry)) {
if (!to_le32(entry->e_value_size) &&
to_le16(entry->e_value_offs))
return false;
if ((char *)base + to_le16(entry->e_value_offs) +
to_le32(entry->e_value_size) >
(char *)end)
return false;
/*
* The name length field should also be correct,
* also there should be an 4-byte zero entry at the
* end.
*/
if ((char *)EXT4_XATTR_NEXT(entry) + sizeof(uint32_t) >
(char *)end)
return false;
if (to_le32(entry->e_value_size)) {
size_t offs = to_le16(entry->e_value_offs);
if (offs < min_offs)
min_offs = offs;
}
}
/*
* Entry field and data field do not override each other.
*/
if ((char *)base + min_offs < (char *)entry + sizeof(uint32_t))
return false;
return true;
}
/**
* @brief Check whether the inode buffer's content is valid
*
* @param inode_ref Inode reference
*
* @return true if the inode buffer is valid, false otherwise.
*/
static bool ext4_xattr_is_ibody_valid(struct ext4_inode_ref *inode_ref)
{
size_t min_offs;
void *base, *end;
struct ext4_fs *fs = inode_ref->fs;
struct ext4_xattr_ibody_header *iheader;
struct ext4_xattr_entry *entry;
size_t inode_size = ext4_get16(&fs->sb, inode_size);
iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode);
entry = EXT4_XATTR_IFIRST(iheader);
base = iheader;
end = (char *)inode_ref->inode + inode_size;
min_offs = (char *)end - (char *)base;
/*
* Check whether the magic number in the header is correct.
*/
if (iheader->h_magic != to_le32(EXT4_XATTR_MAGIC))
return false;
/*
* Check if those entries are maliciously corrupted to inflict harm
* upon us.
*/
for (; !EXT4_XATTR_IS_LAST_ENTRY(entry);
entry = EXT4_XATTR_NEXT(entry)) {
if (!to_le32(entry->e_value_size) &&
to_le16(entry->e_value_offs))
return false;
if ((char *)base + to_le16(entry->e_value_offs) +
to_le32(entry->e_value_size) >
(char *)end)
return false;
/*
* The name length field should also be correct,
* also there should be an 4-byte zero entry at the
* end.
*/
if ((char *)EXT4_XATTR_NEXT(entry) + sizeof(uint32_t) >
(char *)end)
return false;
if (to_le32(entry->e_value_size)) {
size_t offs = to_le16(entry->e_value_offs);
if (offs < min_offs)
min_offs = offs;
}
}
/*
* Entry field and data field do not override each other.
*/
if ((char *)base + min_offs < (char *)entry + sizeof(uint32_t))
return false;
return true;
}
/**
* @brief An EA entry finder for inode buffer
*/
struct ext4_xattr_finder {
/**
* @brief The information of the EA entry to be find
*/
struct ext4_xattr_info i;
/**
* @brief Search context block of the current search
*/
struct ext4_xattr_search s;
/**
* @brief Inode reference to the corresponding inode
*/
struct ext4_inode_ref *inode_ref;
};
static void ext4_xattr_ibody_initialize(struct ext4_inode_ref *inode_ref)
{
struct ext4_xattr_ibody_header *header;
struct ext4_fs *fs = inode_ref->fs;
size_t extra_isize =
ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode);
size_t inode_size = ext4_get16(&fs->sb, inode_size);
if (!extra_isize)
return;
header = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode);
memset(header, 0, inode_size - EXT4_GOOD_OLD_INODE_SIZE - extra_isize);
header->h_magic = to_le32(EXT4_XATTR_MAGIC);
inode_ref->dirty = true;
}
/**
* @brief Initialize a given xattr block
*
* @param inode_ref Inode reference
* @param block xattr block buffer
*/
static void ext4_xattr_block_initialize(struct ext4_inode_ref *inode_ref,
struct ext4_block *block)
{
struct ext4_xattr_header *header;
struct ext4_fs *fs = inode_ref->fs;
memset(block->data, 0, ext4_sb_get_block_size(&fs->sb));
header = EXT4_XATTR_BHDR(block);
header->h_magic = to_le32(EXT4_XATTR_MAGIC);
header->h_refcount = to_le32(1);
header->h_blocks = to_le32(1);
ext4_trans_set_block_dirty(block->buf);
}
static void ext4_xattr_block_init_search(struct ext4_inode_ref *inode_ref,
struct ext4_xattr_search *s,
struct ext4_block *block)
{
s->base = block->data;
s->end = block->data + ext4_sb_get_block_size(&inode_ref->fs->sb);
s->first = EXT4_XATTR_BFIRST(block);
s->here = NULL;
s->not_found = true;
}
/**
* @brief Find an EA entry inside a xattr block
*
* @param inode_ref Inode reference
* @param finder The caller-provided finder block with
* information filled
* @param block The block buffer to be looked into
*
* @return Return EOK no matter the entry is found or not.
* If the IO operation or the buffer validation failed,
* return other value.
*/
static int ext4_xattr_block_find_entry(struct ext4_inode_ref *inode_ref,
struct ext4_xattr_finder *finder,
struct ext4_block *block)
{
int ret = EOK;
/* Initialize the caller-given finder */
finder->inode_ref = inode_ref;
memset(&finder->s, 0, sizeof(finder->s));
if (ret != EOK)
return ret;
/* Check the validity of the buffer */
if (!ext4_xattr_is_block_valid(inode_ref, block))
return EIO;
ext4_xattr_block_init_search(inode_ref, &finder->s, block);
ext4_xattr_find_entry(&finder->i, &finder->s);
return EOK;
}
/**
* @brief Find an EA entry inside an inode's extra space
*
* @param inode_ref Inode reference
* @param finder The caller-provided finder block with
* information filled
*
* @return Return EOK no matter the entry is found or not.
* If the IO operation or the buffer validation failed,
* return other value.
*/
static int ext4_xattr_ibody_find_entry(struct ext4_inode_ref *inode_ref,
struct ext4_xattr_finder *finder)
{
struct ext4_fs *fs = inode_ref->fs;
struct ext4_xattr_ibody_header *iheader;
size_t extra_isize =
ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode);
size_t inode_size = ext4_get16(&fs->sb, inode_size);
/* Initialize the caller-given finder */
finder->inode_ref = inode_ref;
memset(&finder->s, 0, sizeof(finder->s));
/*
* If there is no extra inode space
* set ext4_xattr_ibody_finder::s::not_found to true and return EOK
*/
if (!extra_isize) {
finder->s.not_found = true;
return EOK;
}
/* Check the validity of the buffer */
if (!ext4_xattr_is_ibody_valid(inode_ref))
return EIO;
iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode);
finder->s.base = EXT4_XATTR_IFIRST(iheader);
finder->s.end = (char *)inode_ref->inode + inode_size;
finder->s.first = EXT4_XATTR_IFIRST(iheader);
ext4_xattr_find_entry(&finder->i, &finder->s);
return EOK;
}
/**
* @brief Try to allocate a block holding EA entries.
*
* @param inode_ref Inode reference
*
* @return Error code
*/
static int ext4_xattr_try_alloc_block(struct ext4_inode_ref *inode_ref)
{
int ret = EOK;
ext4_fsblk_t xattr_block = 0;
xattr_block =
ext4_inode_get_file_acl(inode_ref->inode, &inode_ref->fs->sb);
/*
* Only allocate a xattr block when there is no xattr block
* used by the inode.
*/
if (!xattr_block) {
ext4_fsblk_t goal = ext4_fs_inode_to_goal_block(inode_ref);
ret = ext4_balloc_alloc_block(inode_ref, goal, &xattr_block);
if (ret != EOK)
goto Finish;
ext4_inode_set_file_acl(inode_ref->inode, &inode_ref->fs->sb,
xattr_block);
}
Finish:
return ret;
}
/**
* @brief Try to free a block holding EA entries.
*
* @param inode_ref Inode reference
*
* @return Error code
*/
static void ext4_xattr_try_free_block(struct ext4_inode_ref *inode_ref)
{
ext4_fsblk_t xattr_block;
xattr_block =
ext4_inode_get_file_acl(inode_ref->inode, &inode_ref->fs->sb);
/*
* Free the xattr block used by the inode when there is one.
*/
if (xattr_block) {
ext4_inode_set_file_acl(inode_ref->inode, &inode_ref->fs->sb,
0);
ext4_balloc_free_block(inode_ref, xattr_block);
inode_ref->dirty = true;
}
}
/**
* @brief Put a list of EA entries into a caller-provided buffer
* In order to make sure that @list buffer can fit in the data,
* the routine should be called twice.
*
* @param inode_ref Inode reference
* @param list A caller-provided buffer to hold a list of EA entries.
* If list == NULL, list_len will contain the size of
* the buffer required to hold these entries
* @param list_len The length of the data written to @list
* @return Error code
*/
int ext4_xattr_list(struct ext4_inode_ref *inode_ref,
struct ext4_xattr_list_entry *list, size_t *list_len)
{
int ret = EOK;
size_t buf_len = 0;
struct ext4_fs *fs = inode_ref->fs;
struct ext4_xattr_ibody_header *iheader;
size_t extra_isize =
ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode);
struct ext4_block block;
bool block_loaded = false;
ext4_fsblk_t xattr_block = 0;
struct ext4_xattr_entry *entry;
struct ext4_xattr_list_entry *list_prev = NULL;
xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
/*
* If there is extra inode space and the xattr buffer in the
* inode is valid.
*/
if (extra_isize && ext4_xattr_is_ibody_valid(inode_ref)) {
iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode);
entry = EXT4_XATTR_IFIRST(iheader);
/*
* The format of the list should be like this:
*
* name_len indicates the length in bytes of the name
* of the EA entry. The string is null-terminated.
*
* list->name => (char *)(list + 1);
* list->next => (void *)((char *)(list + 1) + name_len + 1);
*/
for (; !EXT4_XATTR_IS_LAST_ENTRY(entry);
entry = EXT4_XATTR_NEXT(entry)) {
size_t name_len = entry->e_name_len;
if (list) {
list->name_index = entry->e_name_index;
list->name_len = name_len;
list->name = (char *)(list + 1);
memcpy(list->name, EXT4_XATTR_NAME(entry),
list->name_len);
if (list_prev)
list_prev->next = list;
list_prev = list;
list = (struct ext4_xattr_list_entry
*)(list->name + name_len + 1);
}
/*
* Size calculation by pointer arithmetics.
*/
buf_len +=
(char *)((struct ext4_xattr_list_entry *)0 + 1) +
name_len + 1 -
(char *)(struct ext4_xattr_list_entry *)0;
}
}
/*
* If there is a xattr block used by the inode
*/
if (xattr_block) {
ret = ext4_trans_block_get(fs->bdev, &block, xattr_block);
if (ret != EOK)
goto out;
block_loaded = true;
/*
* As we don't allow the content in the block being invalid,
* bail out.
*/
if (!ext4_xattr_is_block_valid(inode_ref, &block)) {
ret = EIO;
goto out;
}
entry = EXT4_XATTR_BFIRST(&block);
/*
* The format of the list should be like this:
*
* name_len indicates the length in bytes of the name
* of the EA entry. The string is null-terminated.
*
* list->name => (char *)(list + 1);
* list->next => (void *)((char *)(list + 1) + name_len + 1);
*
* Same as above actually.
*/
for (; !EXT4_XATTR_IS_LAST_ENTRY(entry);
entry = EXT4_XATTR_NEXT(entry)) {
size_t name_len = entry->e_name_len;
if (list) {
list->name_index = entry->e_name_index;
list->name_len = name_len;
list->name = (char *)(list + 1);
memcpy(list->name, EXT4_XATTR_NAME(entry),
list->name_len);
if (list_prev)
list_prev->next = list;
list_prev = list;
list = (struct ext4_xattr_list_entry
*)(list->name + name_len + 1);
}
/*
* Size calculation by pointer arithmetics.
*/
buf_len +=
(char *)((struct ext4_xattr_list_entry *)0 + 1) +
name_len + 1 -
(char *)(struct ext4_xattr_list_entry *)0;
}
}
if (list_prev)
list_prev->next = NULL;
out:
if (ret == EOK && list_len)
*list_len = buf_len;
if (block_loaded)
ext4_block_set(fs->bdev, &block);
return ret;
}
/**
* @brief Query EA entry's value with given name-index and name
*
* @param inode_ref Inode reference
* @param name_index Name-index
* @param name Name of the EA entry to be queried
* @param name_len Length of name in bytes
* @param buf Output buffer to hold content
* @param buf_len Output buffer's length
* @param data_len The length of data of the EA entry found
*
* @return Error code
*/
int ext4_xattr_get(struct ext4_inode_ref *inode_ref, uint8_t name_index,
const char *name, size_t name_len, void *buf, size_t buf_len,
size_t *data_len)
{
int ret = EOK;