1 /*
2 * ext_attr.c --- extended attribute blocks
3 *
4 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5 *
6 * Copyright (C) 2002 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
11 * %End-Header%
12 */
13
14 #include "config.h"
15 #include <stdio.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <string.h>
20 #include <time.h>
21
22 #include "ext2_fs.h"
23 #include "ext2_ext_attr.h"
24 #include "ext4_acl.h"
25
26 #include "ext2fs.h"
27
read_ea_inode_hash(ext2_filsys fs,ext2_ino_t ino,__u32 * hash)28 static errcode_t read_ea_inode_hash(ext2_filsys fs, ext2_ino_t ino, __u32 *hash)
29 {
30 struct ext2_inode inode;
31 errcode_t retval;
32
33 retval = ext2fs_read_inode(fs, ino, &inode);
34 if (retval)
35 return retval;
36 *hash = ext2fs_get_ea_inode_hash(&inode);
37 return 0;
38 }
39
40 #define NAME_HASH_SHIFT 5
41 #define VALUE_HASH_SHIFT 16
42
43 /*
44 * ext2_xattr_hash_entry()
45 *
46 * Compute the hash of an extended attribute.
47 */
ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry * entry,void * data)48 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
49 {
50 __u32 hash = 0;
51 char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
52 int n;
53
54 for (n = 0; n < entry->e_name_len; n++) {
55 hash = (hash << NAME_HASH_SHIFT) ^
56 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
57 *name++;
58 }
59
60 /* The hash needs to be calculated on the data in little-endian. */
61 if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
62 __u32 *value = (__u32 *)data;
63 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
64 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
65 hash = (hash << VALUE_HASH_SHIFT) ^
66 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
67 ext2fs_le32_to_cpu(*value++);
68 }
69 }
70
71 return hash;
72 }
73
74 /*
75 * ext2fs_ext_attr_hash_entry2()
76 *
77 * Compute the hash of an extended attribute.
78 * This version of the function supports hashing entries that reference
79 * external inodes (ea_inode feature).
80 */
ext2fs_ext_attr_hash_entry2(ext2_filsys fs,struct ext2_ext_attr_entry * entry,void * data,__u32 * hash)81 errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
82 struct ext2_ext_attr_entry *entry,
83 void *data, __u32 *hash)
84 {
85 *hash = ext2fs_ext_attr_hash_entry(entry, data);
86
87 if (entry->e_value_inum) {
88 __u32 ea_inode_hash;
89 errcode_t retval;
90
91 retval = read_ea_inode_hash(fs, entry->e_value_inum,
92 &ea_inode_hash);
93 if (retval)
94 return retval;
95
96 *hash = (*hash << VALUE_HASH_SHIFT) ^
97 (*hash >> (8*sizeof(*hash) - VALUE_HASH_SHIFT)) ^
98 ea_inode_hash;
99 }
100 return 0;
101 }
102
103 #undef NAME_HASH_SHIFT
104 #undef VALUE_HASH_SHIFT
105
106 #define BLOCK_HASH_SHIFT 16
107
108 /* Mirrors ext4_xattr_rehash() implementation in kernel. */
ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header * header,struct ext2_ext_attr_entry * end)109 void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
110 struct ext2_ext_attr_entry *end)
111 {
112 struct ext2_ext_attr_entry *here;
113 __u32 hash = 0;
114
115 here = (struct ext2_ext_attr_entry *)(header+1);
116 while (here < end && !EXT2_EXT_IS_LAST_ENTRY(here)) {
117 if (!here->e_hash) {
118 /* Block is not shared if an entry's hash value == 0 */
119 hash = 0;
120 break;
121 }
122 hash = (hash << BLOCK_HASH_SHIFT) ^
123 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
124 here->e_hash;
125 here = EXT2_EXT_ATTR_NEXT(here);
126 }
127 header->h_hash = hash;
128 }
129
130 #undef BLOCK_HASH_SHIFT
131
ext2fs_get_ea_inode_hash(struct ext2_inode * inode)132 __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode)
133 {
134 return inode->i_atime;
135 }
136
ext2fs_set_ea_inode_hash(struct ext2_inode * inode,__u32 hash)137 void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash)
138 {
139 inode->i_atime = hash;
140 }
141
ext2fs_get_ea_inode_ref(struct ext2_inode * inode)142 __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode)
143 {
144 return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
145 }
146
ext2fs_set_ea_inode_ref(struct ext2_inode * inode,__u64 ref_count)147 void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count)
148 {
149 inode->i_ctime = (__u32)(ref_count >> 32);
150 inode->osd1.linux1.l_i_version = (__u32)ref_count;
151 }
152
check_ext_attr_header(struct ext2_ext_attr_header * header)153 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
154 {
155 if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
156 header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
157 header->h_blocks != 1)
158 return EXT2_ET_BAD_EA_HEADER;
159
160 return 0;
161 }
162
ext2fs_read_ext_attr3(ext2_filsys fs,blk64_t block,void * buf,ext2_ino_t inum)163 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
164 ext2_ino_t inum)
165 {
166 int csum_failed = 0;
167 errcode_t retval;
168
169 retval = io_channel_read_blk64(fs->io, block, 1, buf);
170 if (retval)
171 return retval;
172
173 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
174 !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
175 csum_failed = 1;
176
177 #ifdef WORDS_BIGENDIAN
178 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
179 #endif
180
181 retval = check_ext_attr_header(buf);
182 if (retval == 0 && csum_failed)
183 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
184
185 return retval;
186 }
187
ext2fs_read_ext_attr2(ext2_filsys fs,blk64_t block,void * buf)188 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
189 {
190 return ext2fs_read_ext_attr3(fs, block, buf, 0);
191 }
192
ext2fs_read_ext_attr(ext2_filsys fs,blk_t block,void * buf)193 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
194 {
195 return ext2fs_read_ext_attr2(fs, block, buf);
196 }
197
ext2fs_write_ext_attr3(ext2_filsys fs,blk64_t block,void * inbuf,ext2_ino_t inum)198 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
199 ext2_ino_t inum)
200 {
201 errcode_t retval;
202 char *write_buf;
203
204 #ifdef WORDS_BIGENDIAN
205 retval = ext2fs_get_mem(fs->blocksize, &write_buf);
206 if (retval)
207 return retval;
208 ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
209 #else
210 write_buf = (char *) inbuf;
211 #endif
212
213 retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
214 (struct ext2_ext_attr_header *)write_buf);
215 if (retval)
216 return retval;
217
218 retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
219 #ifdef WORDS_BIGENDIAN
220 ext2fs_free_mem(&write_buf);
221 #endif
222 if (!retval)
223 ext2fs_mark_changed(fs);
224 return retval;
225 }
226
ext2fs_write_ext_attr2(ext2_filsys fs,blk64_t block,void * inbuf)227 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
228 {
229 return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
230 }
231
ext2fs_write_ext_attr(ext2_filsys fs,blk_t block,void * inbuf)232 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
233 {
234 return ext2fs_write_ext_attr2(fs, block, inbuf);
235 }
236
237 /*
238 * This function adjusts the reference count of the EA block.
239 */
ext2fs_adjust_ea_refcount3(ext2_filsys fs,blk64_t blk,char * block_buf,int adjust,__u32 * newcount,ext2_ino_t inum)240 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
241 char *block_buf, int adjust,
242 __u32 *newcount, ext2_ino_t inum)
243 {
244 errcode_t retval;
245 struct ext2_ext_attr_header *header;
246 char *buf = 0;
247
248 if ((blk >= ext2fs_blocks_count(fs->super)) ||
249 (blk < fs->super->s_first_data_block))
250 return EXT2_ET_BAD_EA_BLOCK_NUM;
251
252 if (!block_buf) {
253 retval = ext2fs_get_mem(fs->blocksize, &buf);
254 if (retval)
255 return retval;
256 block_buf = buf;
257 }
258
259 retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
260 if (retval)
261 goto errout;
262
263 header = (struct ext2_ext_attr_header *) block_buf;
264 header->h_refcount += adjust;
265 if (newcount)
266 *newcount = header->h_refcount;
267
268 retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
269 if (retval)
270 goto errout;
271
272 errout:
273 if (buf)
274 ext2fs_free_mem(&buf);
275 return retval;
276 }
277
ext2fs_adjust_ea_refcount2(ext2_filsys fs,blk64_t blk,char * block_buf,int adjust,__u32 * newcount)278 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
279 char *block_buf, int adjust,
280 __u32 *newcount)
281 {
282 return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
283 newcount, 0);
284 }
285
ext2fs_adjust_ea_refcount(ext2_filsys fs,blk_t blk,char * block_buf,int adjust,__u32 * newcount)286 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
287 char *block_buf, int adjust,
288 __u32 *newcount)
289 {
290 return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
291 newcount);
292 }
293
294 /* Manipulate the contents of extended attribute regions */
295 struct ext2_xattr {
296 int name_index;
297 char *name;
298 char *short_name;
299 void *value;
300 unsigned int value_len;
301 ext2_ino_t ea_ino;
302 };
303
304 struct ext2_xattr_handle {
305 errcode_t magic;
306 ext2_filsys fs;
307 struct ext2_xattr *attrs;
308 int capacity;
309 int count;
310 int ibody_count;
311 ext2_ino_t ino;
312 unsigned int flags;
313 };
314
ext2fs_xattrs_expand(struct ext2_xattr_handle * h,unsigned int expandby)315 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
316 unsigned int expandby)
317 {
318 struct ext2_xattr *new_attrs;
319 errcode_t err;
320
321 err = ext2fs_get_arrayzero(h->capacity + expandby,
322 sizeof(struct ext2_xattr), &new_attrs);
323 if (err)
324 return err;
325
326 memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
327 ext2fs_free_mem(&h->attrs);
328 h->capacity += expandby;
329 h->attrs = new_attrs;
330
331 return 0;
332 }
333
334 struct ea_name_index {
335 int index;
336 const char *name;
337 };
338
339 /* Keep these names sorted in order of decreasing specificity. */
340 static struct ea_name_index ea_names[] = {
341 {10, "gnu."},
342 {3, "system.posix_acl_default"},
343 {2, "system.posix_acl_access"},
344 {8, "system.richacl"},
345 {6, "security."},
346 {4, "trusted."},
347 {7, "system."},
348 {1, "user."},
349 {0, NULL},
350 };
351
find_ea_prefix(int index)352 static const char *find_ea_prefix(int index)
353 {
354 struct ea_name_index *e;
355
356 for (e = ea_names; e->name; e++)
357 if (e->index == index)
358 return e->name;
359
360 return NULL;
361 }
362
find_ea_index(const char * fullname,const char ** name,int * index)363 static int find_ea_index(const char *fullname, const char **name, int *index)
364 {
365 struct ea_name_index *e;
366
367 for (e = ea_names; e->name; e++) {
368 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
369 *name = fullname + strlen(e->name);
370 *index = e->index;
371 return 1;
372 }
373 }
374 return 0;
375 }
376
ext2fs_free_ext_attr(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode_large * inode)377 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
378 struct ext2_inode_large *inode)
379 {
380 struct ext2_ext_attr_header *header;
381 void *block_buf = NULL;
382 blk64_t blk;
383 errcode_t err;
384 struct ext2_inode_large i;
385
386 /* Read inode? */
387 if (inode == NULL) {
388 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
389 sizeof(struct ext2_inode_large));
390 if (err)
391 return err;
392 inode = &i;
393 }
394
395 /* Do we already have an EA block? */
396 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
397 if (blk == 0)
398 return 0;
399
400 /* Find block, zero it, write back */
401 if ((blk < fs->super->s_first_data_block) ||
402 (blk >= ext2fs_blocks_count(fs->super))) {
403 err = EXT2_ET_BAD_EA_BLOCK_NUM;
404 goto out;
405 }
406
407 err = ext2fs_get_mem(fs->blocksize, &block_buf);
408 if (err)
409 goto out;
410
411 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
412 if (err)
413 goto out2;
414
415 /* We only know how to deal with v2 EA blocks */
416 header = (struct ext2_ext_attr_header *) block_buf;
417 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
418 err = EXT2_ET_BAD_EA_HEADER;
419 goto out2;
420 }
421
422 header->h_refcount--;
423 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
424 if (err)
425 goto out2;
426
427 /* Erase link to block */
428 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
429 if (header->h_refcount == 0)
430 ext2fs_block_alloc_stats2(fs, blk, -1);
431 err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
432 if (err)
433 goto out2;
434
435 /* Write inode? */
436 if (inode == &i) {
437 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
438 sizeof(struct ext2_inode_large));
439 if (err)
440 goto out2;
441 }
442
443 out2:
444 ext2fs_free_mem(&block_buf);
445 out:
446 return err;
447 }
448
prep_ea_block_for_write(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode_large * inode)449 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
450 struct ext2_inode_large *inode)
451 {
452 struct ext2_ext_attr_header *header;
453 void *block_buf = NULL;
454 blk64_t blk, goal;
455 errcode_t err;
456
457 /* Do we already have an EA block? */
458 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
459 if (blk != 0) {
460 if ((blk < fs->super->s_first_data_block) ||
461 (blk >= ext2fs_blocks_count(fs->super))) {
462 err = EXT2_ET_BAD_EA_BLOCK_NUM;
463 goto out;
464 }
465
466 err = ext2fs_get_mem(fs->blocksize, &block_buf);
467 if (err)
468 goto out;
469
470 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
471 if (err)
472 goto out2;
473
474 /* We only know how to deal with v2 EA blocks */
475 header = (struct ext2_ext_attr_header *) block_buf;
476 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
477 err = EXT2_ET_BAD_EA_HEADER;
478 goto out2;
479 }
480
481 /* Single-user block. We're done here. */
482 if (header->h_refcount == 1)
483 goto out2;
484
485 /* We need to CoW the block. */
486 header->h_refcount--;
487 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
488 if (err)
489 goto out2;
490 } else {
491 /* No block, we must increment i_blocks */
492 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
493 1);
494 if (err)
495 goto out;
496 }
497
498 /* Allocate a block */
499 goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
500 err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
501 if (err)
502 goto out2;
503 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
504 out2:
505 if (block_buf)
506 ext2fs_free_mem(&block_buf);
507 out:
508 return err;
509 }
510
511
512 static inline int
posix_acl_xattr_count(size_t size)513 posix_acl_xattr_count(size_t size)
514 {
515 if (size < sizeof(posix_acl_xattr_header))
516 return -1;
517 size -= sizeof(posix_acl_xattr_header);
518 if (size % sizeof(posix_acl_xattr_entry))
519 return -1;
520 return size / sizeof(posix_acl_xattr_entry);
521 }
522
523 /*
524 * The lgetxattr function returns data formatted in the POSIX extended
525 * attribute format. The on-disk format uses a more compact encoding.
526 * See the ext4_acl_to_disk in fs/ext4/acl.c.
527 */
convert_posix_acl_to_disk_buffer(const void * value,size_t size,void * out_buf,size_t * size_out)528 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
529 void *out_buf, size_t *size_out)
530 {
531 const posix_acl_xattr_header *header =
532 (const posix_acl_xattr_header*) value;
533 const posix_acl_xattr_entry *end, *entry =
534 (const posix_acl_xattr_entry *)(header+1);
535 ext4_acl_header *ext_acl;
536 size_t s;
537 char *e;
538
539 int count;
540
541 if (!value)
542 return EINVAL;
543 if (size < sizeof(posix_acl_xattr_header))
544 return ENOMEM;
545 if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
546 return EINVAL;
547
548 count = posix_acl_xattr_count(size);
549 ext_acl = out_buf;
550 ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
551
552 if (count <= 0)
553 return EINVAL;
554
555 e = (char *) out_buf + sizeof(ext4_acl_header);
556 s = sizeof(ext4_acl_header);
557 for (end = entry + count; entry != end;entry++) {
558 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
559 disk_entry->e_tag = entry->e_tag;
560 disk_entry->e_perm = entry->e_perm;
561
562 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
563 case ACL_USER_OBJ:
564 case ACL_GROUP_OBJ:
565 case ACL_MASK:
566 case ACL_OTHER:
567 e += sizeof(ext4_acl_entry_short);
568 s += sizeof(ext4_acl_entry_short);
569 break;
570 case ACL_USER:
571 case ACL_GROUP:
572 disk_entry->e_id = entry->e_id;
573 e += sizeof(ext4_acl_entry);
574 s += sizeof(ext4_acl_entry);
575 break;
576 default:
577 return EINVAL;
578 }
579 }
580 *size_out = s;
581 return 0;
582 }
583
convert_disk_buffer_to_posix_acl(const void * value,size_t size,void ** out_buf,size_t * size_out)584 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
585 void **out_buf, size_t *size_out)
586 {
587 posix_acl_xattr_header *header;
588 posix_acl_xattr_entry *entry;
589 const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
590 errcode_t err;
591 const char *cp;
592 char *out;
593
594 if ((!value) ||
595 (size < sizeof(ext4_acl_header)) ||
596 (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
597 return EINVAL;
598
599 err = ext2fs_get_mem(size * 2, &out);
600 if (err)
601 return err;
602
603 header = (posix_acl_xattr_header *) out;
604 header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
605 entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
606
607 cp = (const char *) value + sizeof(ext4_acl_header);
608 size -= sizeof(ext4_acl_header);
609
610 while (size > 0) {
611 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
612
613 entry->e_tag = disk_entry->e_tag;
614 entry->e_perm = disk_entry->e_perm;
615
616 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
617 case ACL_USER_OBJ:
618 case ACL_GROUP_OBJ:
619 case ACL_MASK:
620 case ACL_OTHER:
621 entry->e_id = 0;
622 cp += sizeof(ext4_acl_entry_short);
623 size -= sizeof(ext4_acl_entry_short);
624 break;
625 case ACL_USER:
626 case ACL_GROUP:
627 entry->e_id = disk_entry->e_id;
628 cp += sizeof(ext4_acl_entry);
629 size -= sizeof(ext4_acl_entry);
630 break;
631 default:
632 ext2fs_free_mem(&out);
633 return EINVAL;
634 }
635 entry++;
636 }
637 *out_buf = out;
638 *size_out = ((char *) entry - out);
639 return 0;
640 }
641
642 static errcode_t
write_xattrs_to_buffer(ext2_filsys fs,struct ext2_xattr * attrs,int count,void * entries_start,unsigned int storage_size,unsigned int value_offset_correction,int write_hash)643 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
644 void *entries_start, unsigned int storage_size,
645 unsigned int value_offset_correction, int write_hash)
646 {
647 struct ext2_xattr *x;
648 struct ext2_ext_attr_entry *e = entries_start;
649 char *end = (char *) entries_start + storage_size;
650 unsigned int value_size;
651 errcode_t err;
652
653 memset(entries_start, 0, storage_size);
654 for (x = attrs; x < attrs + count; x++) {
655 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
656 EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
657
658 /* Fill out e appropriately */
659 e->e_name_len = strlen(x->short_name);
660 e->e_name_index = x->name_index;
661
662 e->e_value_size = x->value_len;
663 e->e_value_inum = x->ea_ino;
664
665 /* Store name */
666 memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
667 if (x->ea_ino) {
668 e->e_value_offs = 0;
669 } else {
670 end -= value_size;
671 e->e_value_offs = end - (char *) entries_start +
672 value_offset_correction;
673 memcpy(end, x->value, e->e_value_size);
674 }
675
676 if (write_hash || x->ea_ino) {
677 err = ext2fs_ext_attr_hash_entry2(fs, e,
678 x->ea_ino ? 0 : end,
679 &e->e_hash);
680 if (err)
681 return err;
682 } else
683 e->e_hash = 0;
684
685 e = EXT2_EXT_ATTR_NEXT(e);
686 *(__u32 *)e = 0;
687 }
688 return 0;
689 }
690
ext2fs_xattrs_write(struct ext2_xattr_handle * handle)691 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
692 {
693 ext2_filsys fs = handle->fs;
694 const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
695 struct ext2_inode_large *inode;
696 char *start, *block_buf = NULL;
697 struct ext2_ext_attr_header *header;
698 __u32 ea_inode_magic;
699 blk64_t blk;
700 unsigned int storage_size;
701 unsigned int i;
702 errcode_t err;
703
704 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
705 i = inode_size;
706 if (i < sizeof(*inode))
707 i = sizeof(*inode);
708 err = ext2fs_get_memzero(i, &inode);
709 if (err)
710 return err;
711
712 err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
713 inode_size);
714 if (err)
715 goto out;
716
717 /* If extra_isize isn't set, we need to set it now */
718 if (inode->i_extra_isize == 0 &&
719 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
720 char *p = (char *)inode;
721 size_t extra = fs->super->s_want_extra_isize;
722
723 if (extra == 0)
724 extra = sizeof(__u32);
725 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
726 inode->i_extra_isize = extra;
727 }
728 if (inode->i_extra_isize & 3) {
729 err = EXT2_ET_INODE_CORRUPTED;
730 goto out;
731 }
732
733 /* Does the inode have space for EA? */
734 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
735 inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
736 sizeof(__u32))
737 goto write_ea_block;
738
739 /* Write the inode EA */
740 ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
741 memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
742 inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
743 storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
744 inode->i_extra_isize - sizeof(__u32);
745 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
746 inode->i_extra_isize + sizeof(__u32);
747
748 err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
749 start, storage_size, 0, 0);
750 if (err)
751 goto out;
752 write_ea_block:
753 /* Are we done? */
754 if (handle->ibody_count == handle->count &&
755 !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
756 goto skip_ea_block;
757
758 /* Write the EA block */
759 err = ext2fs_get_memzero(fs->blocksize, &block_buf);
760 if (err)
761 goto out;
762
763 storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
764 start = block_buf + sizeof(struct ext2_ext_attr_header);
765
766 err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
767 handle->count - handle->ibody_count, start,
768 storage_size, start - block_buf, 1);
769 if (err)
770 goto out2;
771
772 /* Write a header on the EA block */
773 header = (struct ext2_ext_attr_header *) block_buf;
774 header->h_magic = EXT2_EXT_ATTR_MAGIC;
775 header->h_refcount = 1;
776 header->h_blocks = 1;
777
778 /* Get a new block for writing */
779 err = prep_ea_block_for_write(fs, handle->ino, inode);
780 if (err)
781 goto out2;
782
783 /* Finally, write the new EA block */
784 blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
785 err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
786 if (err)
787 goto out2;
788
789 skip_ea_block:
790 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
791 if (!block_buf && blk) {
792 /* xattrs shrunk, free the block */
793 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
794 if (err)
795 goto out;
796 }
797
798 /* Write the inode */
799 err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
800 inode_size);
801 if (err)
802 goto out2;
803
804 out2:
805 ext2fs_free_mem(&block_buf);
806 out:
807 ext2fs_free_mem(&inode);
808 return err;
809 }
810
read_xattrs_from_buffer(struct ext2_xattr_handle * handle,struct ext2_inode_large * inode,struct ext2_ext_attr_entry * entries,unsigned int storage_size,char * value_start)811 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
812 struct ext2_inode_large *inode,
813 struct ext2_ext_attr_entry *entries,
814 unsigned int storage_size,
815 char *value_start)
816 {
817 struct ext2_xattr *x;
818 struct ext2_ext_attr_entry *entry, *end;
819 const char *prefix;
820 unsigned int remain, prefix_len;
821 errcode_t err;
822 unsigned int values_size = storage_size +
823 ((char *)entries - value_start);
824
825 /* find the end */
826 end = entries;
827 remain = storage_size;
828 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
829 !EXT2_EXT_IS_LAST_ENTRY(end)) {
830
831 /* header eats this space */
832 remain -= sizeof(struct ext2_ext_attr_entry);
833
834 /* is attribute name valid? */
835 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
836 return EXT2_ET_EA_BAD_NAME_LEN;
837
838 /* attribute len eats this space */
839 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
840 end = EXT2_EXT_ATTR_NEXT(end);
841 }
842
843 entry = entries;
844 remain = storage_size;
845 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
846 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
847
848 /* Allocate space for more attrs? */
849 if (handle->count == handle->capacity) {
850 err = ext2fs_xattrs_expand(handle, 4);
851 if (err)
852 return err;
853 }
854
855 x = handle->attrs + handle->count;
856
857 /* header eats this space */
858 remain -= sizeof(struct ext2_ext_attr_entry);
859
860 /* attribute len eats this space */
861 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
862
863 /* Extract name */
864 prefix = find_ea_prefix(entry->e_name_index);
865 prefix_len = (prefix ? strlen(prefix) : 0);
866 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
867 &x->name);
868 if (err)
869 return err;
870 if (prefix)
871 memcpy(x->name, prefix, prefix_len);
872 if (entry->e_name_len)
873 memcpy(x->name + prefix_len,
874 (char *)entry + sizeof(*entry),
875 entry->e_name_len);
876 x->short_name = x->name + prefix_len;
877 x->name_index = entry->e_name_index;
878
879 /* Check & copy value */
880 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
881 entry->e_value_inum != 0)
882 return EXT2_ET_BAD_EA_BLOCK_NUM;
883
884 if (entry->e_value_inum == 0) {
885 if (entry->e_value_size > remain)
886 return EXT2_ET_EA_BAD_VALUE_SIZE;
887
888 if (entry->e_value_offs + entry->e_value_size > values_size)
889 return EXT2_ET_EA_BAD_VALUE_OFFSET;
890
891 if (entry->e_value_size > 0 &&
892 value_start + entry->e_value_offs <
893 (char *)end + sizeof(__u32))
894 return EXT2_ET_EA_BAD_VALUE_OFFSET;
895
896 remain -= entry->e_value_size;
897
898 err = ext2fs_get_mem(entry->e_value_size, &x->value);
899 if (err)
900 return err;
901 memcpy(x->value, value_start + entry->e_value_offs,
902 entry->e_value_size);
903 } else {
904 struct ext2_inode *ea_inode;
905 ext2_file_t ea_file;
906
907 if (entry->e_value_offs != 0)
908 return EXT2_ET_EA_BAD_VALUE_OFFSET;
909
910 if (entry->e_value_size > (64 * 1024))
911 return EXT2_ET_EA_BAD_VALUE_SIZE;
912
913 err = ext2fs_get_mem(entry->e_value_size, &x->value);
914 if (err)
915 return err;
916
917 err = ext2fs_file_open(handle->fs, entry->e_value_inum,
918 0, &ea_file);
919 if (err)
920 return err;
921
922 ea_inode = ext2fs_file_get_inode(ea_file);
923 if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
924 !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
925 ea_inode->i_links_count == 0)
926 err = EXT2_ET_EA_INODE_CORRUPTED;
927 else if ((__u64) ext2fs_file_get_size(ea_file) !=
928 entry->e_value_size)
929 err = EXT2_ET_EA_BAD_VALUE_SIZE;
930 else
931 err = ext2fs_file_read(ea_file, x->value,
932 entry->e_value_size, 0);
933 ext2fs_file_close(ea_file);
934 if (err)
935 return err;
936 }
937
938 x->ea_ino = entry->e_value_inum;
939 x->value_len = entry->e_value_size;
940
941 /* e_hash may be 0 in older inode's ea */
942 if (entry->e_hash != 0) {
943 __u32 hash;
944 void *data = (entry->e_value_inum != 0) ?
945 0 : value_start + entry->e_value_offs;
946
947 err = ext2fs_ext_attr_hash_entry2(handle->fs, entry,
948 data, &hash);
949 if (err)
950 return err;
951 if (entry->e_hash != hash) {
952 struct ext2_inode child;
953
954 /* Check whether this is an old Lustre-style
955 * ea_inode reference.
956 */
957 err = ext2fs_read_inode(handle->fs,
958 entry->e_value_inum,
959 &child);
960 if (err)
961 return err;
962 if (child.i_mtime != handle->ino ||
963 child.i_generation != inode->i_generation)
964 return EXT2_ET_BAD_EA_HASH;
965 }
966 }
967
968 handle->count++;
969 entry = EXT2_EXT_ATTR_NEXT(entry);
970 }
971
972 return 0;
973 }
974
xattrs_free_keys(struct ext2_xattr_handle * h)975 static void xattrs_free_keys(struct ext2_xattr_handle *h)
976 {
977 struct ext2_xattr *a = h->attrs;
978 int i;
979
980 for (i = 0; i < h->capacity; i++) {
981 if (a[i].name)
982 ext2fs_free_mem(&a[i].name);
983 if (a[i].value)
984 ext2fs_free_mem(&a[i].value);
985 }
986 h->count = 0;
987 h->ibody_count = 0;
988 }
989
ext2fs_xattrs_read(struct ext2_xattr_handle * handle)990 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
991 {
992 struct ext2_inode_large *inode;
993 struct ext2_ext_attr_header *header;
994 __u32 ea_inode_magic;
995 unsigned int storage_size;
996 char *start, *block_buf = NULL;
997 blk64_t blk;
998 size_t i;
999 errcode_t err;
1000
1001 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1002 i = EXT2_INODE_SIZE(handle->fs->super);
1003 if (i < sizeof(*inode))
1004 i = sizeof(*inode);
1005 err = ext2fs_get_memzero(i, &inode);
1006 if (err)
1007 return err;
1008
1009 err = ext2fs_read_inode_full(handle->fs, handle->ino,
1010 (struct ext2_inode *)inode,
1011 EXT2_INODE_SIZE(handle->fs->super));
1012 if (err)
1013 goto out;
1014
1015 xattrs_free_keys(handle);
1016
1017 /* Does the inode have space for EA? */
1018 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1019 EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1020 inode->i_extra_isize +
1021 sizeof(__u32))
1022 goto read_ea_block;
1023 if (inode->i_extra_isize & 3) {
1024 err = EXT2_ET_INODE_CORRUPTED;
1025 goto out;
1026 }
1027
1028 /* Look for EA in the inode */
1029 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1030 inode->i_extra_isize, sizeof(__u32));
1031 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1032 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1033 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1034 sizeof(__u32);
1035 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1036 inode->i_extra_isize + sizeof(__u32);
1037
1038 err = read_xattrs_from_buffer(handle, inode,
1039 (struct ext2_ext_attr_entry *) start,
1040 storage_size, start);
1041 if (err)
1042 goto out;
1043
1044 handle->ibody_count = handle->count;
1045 }
1046
1047 read_ea_block:
1048 /* Look for EA in a separate EA block */
1049 blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
1050 if (blk != 0) {
1051 if ((blk < handle->fs->super->s_first_data_block) ||
1052 (blk >= ext2fs_blocks_count(handle->fs->super))) {
1053 err = EXT2_ET_BAD_EA_BLOCK_NUM;
1054 goto out;
1055 }
1056
1057 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1058 if (err)
1059 goto out;
1060
1061 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1062 handle->ino);
1063 if (err)
1064 goto out3;
1065
1066 /* We only know how to deal with v2 EA blocks */
1067 header = (struct ext2_ext_attr_header *) block_buf;
1068 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1069 err = EXT2_ET_BAD_EA_HEADER;
1070 goto out3;
1071 }
1072
1073 /* Read EAs */
1074 storage_size = handle->fs->blocksize -
1075 sizeof(struct ext2_ext_attr_header);
1076 start = block_buf + sizeof(struct ext2_ext_attr_header);
1077 err = read_xattrs_from_buffer(handle, inode,
1078 (struct ext2_ext_attr_entry *) start,
1079 storage_size, block_buf);
1080 if (err)
1081 goto out3;
1082
1083 ext2fs_free_mem(&block_buf);
1084 }
1085
1086 ext2fs_free_mem(&block_buf);
1087 ext2fs_free_mem(&inode);
1088 return 0;
1089
1090 out3:
1091 ext2fs_free_mem(&block_buf);
1092 out:
1093 ext2fs_free_mem(&inode);
1094 return err;
1095 }
1096
ext2fs_xattrs_iterate(struct ext2_xattr_handle * h,int (* func)(char * name,char * value,size_t value_len,void * data),void * data)1097 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1098 int (*func)(char *name, char *value,
1099 size_t value_len, void *data),
1100 void *data)
1101 {
1102 struct ext2_xattr *x;
1103 int dirty = 0;
1104 int ret;
1105
1106 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1107 for (x = h->attrs; x < h->attrs + h->count; x++) {
1108 ret = func(x->name, x->value, x->value_len, data);
1109 if (ret & XATTR_CHANGED)
1110 dirty = 1;
1111 if (ret & XATTR_ABORT)
1112 break;
1113 }
1114
1115 if (dirty)
1116 return ext2fs_xattrs_write(h);
1117 return 0;
1118 }
1119
ext2fs_xattr_get(struct ext2_xattr_handle * h,const char * key,void ** value,size_t * value_len)1120 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1121 void **value, size_t *value_len)
1122 {
1123 struct ext2_xattr *x;
1124 char *val;
1125 errcode_t err;
1126
1127 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1128 for (x = h->attrs; x < h->attrs + h->count; x++) {
1129 if (strcmp(x->name, key))
1130 continue;
1131
1132 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1133 ((strcmp(key, "system.posix_acl_default") == 0) ||
1134 (strcmp(key, "system.posix_acl_access") == 0))) {
1135 err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1136 value, value_len);
1137 return err;
1138 } else {
1139 err = ext2fs_get_mem(x->value_len, &val);
1140 if (err)
1141 return err;
1142 memcpy(val, x->value, x->value_len);
1143 *value = val;
1144 *value_len = x->value_len;
1145 return 0;
1146 }
1147 }
1148
1149 return EXT2_ET_EA_KEY_NOT_FOUND;
1150 }
1151
ext2fs_xattr_inode_max_size(ext2_filsys fs,ext2_ino_t ino,size_t * size)1152 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1153 size_t *size)
1154 {
1155 struct ext2_ext_attr_entry *entry;
1156 struct ext2_inode_large *inode;
1157 __u32 ea_inode_magic;
1158 unsigned int minoff;
1159 char *start;
1160 size_t i;
1161 errcode_t err;
1162
1163 i = EXT2_INODE_SIZE(fs->super);
1164 if (i < sizeof(*inode))
1165 i = sizeof(*inode);
1166 err = ext2fs_get_memzero(i, &inode);
1167 if (err)
1168 return err;
1169
1170 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1171 EXT2_INODE_SIZE(fs->super));
1172 if (err)
1173 goto out;
1174
1175 /* Does the inode have size for EA? */
1176 if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1177 inode->i_extra_isize +
1178 sizeof(__u32)) {
1179 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1180 goto out;
1181 }
1182
1183 minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1184 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1185 inode->i_extra_isize, sizeof(__u32));
1186 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1187 /* has xattrs. calculate the size */
1188 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1189 inode->i_extra_isize + sizeof(__u32);
1190 entry = (struct ext2_ext_attr_entry *) start;
1191 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1192 if (!entry->e_value_inum && entry->e_value_size) {
1193 unsigned int offs = entry->e_value_offs;
1194 if (offs < minoff)
1195 minoff = offs;
1196 }
1197 entry = EXT2_EXT_ATTR_NEXT(entry);
1198 }
1199 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1200 } else {
1201 /* no xattr. return a maximum size */
1202 *size = EXT2_EXT_ATTR_SIZE(minoff -
1203 EXT2_EXT_ATTR_LEN(strlen("data")) -
1204 EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1205 }
1206
1207 out:
1208 ext2fs_free_mem(&inode);
1209 return err;
1210 }
1211
xattr_create_ea_inode(ext2_filsys fs,const void * value,size_t value_len,ext2_ino_t * ea_ino)1212 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1213 size_t value_len, ext2_ino_t *ea_ino)
1214 {
1215 struct ext2_inode inode;
1216 ext2_ino_t ino;
1217 ext2_file_t file;
1218 __u32 hash;
1219 errcode_t ret;
1220
1221 ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1222 if (ret)
1223 return ret;
1224
1225 memset(&inode, 0, sizeof(inode));
1226 inode.i_flags |= EXT4_EA_INODE_FL;
1227 if (ext2fs_has_feature_extents(fs->super))
1228 inode.i_flags |= EXT4_EXTENTS_FL;
1229 inode.i_size = 0;
1230 inode.i_mode = LINUX_S_IFREG | 0600;
1231 inode.i_links_count = 1;
1232 ret = ext2fs_write_new_inode(fs, ino, &inode);
1233 if (ret)
1234 return ret;
1235 /*
1236 * ref_count and hash utilize inode's i_*time fields.
1237 * ext2fs_write_new_inode() call above initializes these fields with
1238 * current time. That's why ref count and hash updates are done
1239 * separately below.
1240 */
1241 ext2fs_set_ea_inode_ref(&inode, 1);
1242 hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1243 ext2fs_set_ea_inode_hash(&inode, hash);
1244
1245 ret = ext2fs_write_inode(fs, ino, &inode);
1246 if (ret)
1247 return ret;
1248
1249 ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1250 if (ret)
1251 return ret;
1252 ret = ext2fs_file_write(file, value, value_len, NULL);
1253 ext2fs_file_close(file);
1254 if (ret)
1255 return ret;
1256
1257 ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1258
1259 *ea_ino = ino;
1260 return 0;
1261 }
1262
xattr_inode_dec_ref(ext2_filsys fs,ext2_ino_t ino)1263 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1264 {
1265 struct ext2_inode_large inode;
1266 __u64 ref_count;
1267 errcode_t ret;
1268
1269 ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1270 sizeof(inode));
1271 if (ret)
1272 goto out;
1273
1274 ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1275 ref_count--;
1276 ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1277
1278 if (ref_count)
1279 goto write_out;
1280
1281 inode.i_links_count = 0;
1282 inode.i_dtime = fs->now ? fs->now : time(0);
1283
1284 ret = ext2fs_free_ext_attr(fs, ino, &inode);
1285 if (ret)
1286 goto write_out;
1287
1288 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1289 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1290 0, ~0ULL);
1291 if (ret)
1292 goto out;
1293 }
1294
1295 ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1296
1297 write_out:
1298 ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1299 sizeof(inode));
1300 out:
1301 return ret;
1302 }
1303
xattr_update_entry(ext2_filsys fs,struct ext2_xattr * x,const char * name,const char * short_name,int index,const void * value,size_t value_len,int in_inode)1304 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1305 const char *name, const char *short_name,
1306 int index, const void *value,
1307 size_t value_len, int in_inode)
1308 {
1309 ext2_ino_t ea_ino = 0;
1310 void *new_value = NULL;
1311 char *new_name = NULL;
1312 int name_len;
1313 errcode_t ret;
1314
1315 if (!x->name) {
1316 name_len = strlen(name);
1317 ret = ext2fs_get_mem(name_len + 1, &new_name);
1318 if (ret)
1319 goto fail;
1320 memcpy(new_name, name, name_len + 1);
1321 }
1322
1323 ret = ext2fs_get_mem(value_len, &new_value);
1324 if (ret)
1325 goto fail;
1326 memcpy(new_value, value, value_len);
1327
1328 if (in_inode) {
1329 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1330 if (ret)
1331 goto fail;
1332 }
1333
1334 if (x->ea_ino) {
1335 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1336 if (ret)
1337 goto fail;
1338 }
1339
1340 if (!x->name) {
1341 x->name = new_name;
1342 x->short_name = new_name + (short_name - name);
1343 }
1344 x->name_index = index;
1345
1346 if (x->value)
1347 ext2fs_free_mem(&x->value);
1348 x->value = new_value;
1349 x->value_len = value_len;
1350 x->ea_ino = ea_ino;
1351 return 0;
1352 fail:
1353 if (new_name)
1354 ext2fs_free_mem(&new_name);
1355 if (new_value)
1356 ext2fs_free_mem(&new_value);
1357 if (ea_ino)
1358 xattr_inode_dec_ref(fs, ea_ino);
1359 return ret;
1360 }
1361
xattr_find_position(struct ext2_xattr * attrs,int count,const char * shortname,int name_idx)1362 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1363 const char *shortname, int name_idx)
1364 {
1365 struct ext2_xattr *x;
1366 int i;
1367 int shortname_len, x_shortname_len;
1368
1369 shortname_len = strlen(shortname);
1370
1371 for (i = 0, x = attrs; i < count; i++, x++) {
1372 if (name_idx < x->name_index)
1373 break;
1374 if (name_idx > x->name_index)
1375 continue;
1376
1377 x_shortname_len = strlen(x->short_name);
1378 if (shortname_len < x_shortname_len)
1379 break;
1380 if (shortname_len > x_shortname_len)
1381 continue;
1382
1383 if (memcmp(shortname, x->short_name, shortname_len) <= 0)
1384 break;
1385 }
1386 return i;
1387 }
1388
xattr_array_update(struct ext2_xattr_handle * h,const char * name,const void * value,size_t value_len,int ibody_free,int block_free,int old_idx,int in_inode)1389 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1390 const char *name,
1391 const void *value, size_t value_len,
1392 int ibody_free, int block_free,
1393 int old_idx, int in_inode)
1394 {
1395 struct ext2_xattr tmp;
1396 int add_to_ibody;
1397 int needed;
1398 int name_len, name_idx = 0;
1399 const char *shortname = name;
1400 int new_idx;
1401 int ret;
1402
1403 find_ea_index(name, &shortname, &name_idx);
1404 name_len = strlen(shortname);
1405
1406 needed = EXT2_EXT_ATTR_LEN(name_len);
1407 if (!in_inode)
1408 needed += EXT2_EXT_ATTR_SIZE(value_len);
1409
1410 if (old_idx >= 0 && old_idx < h->ibody_count) {
1411 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1412 if (!h->attrs[old_idx].ea_ino)
1413 ibody_free += EXT2_EXT_ATTR_SIZE(
1414 h->attrs[old_idx].value_len);
1415 }
1416
1417 if (needed <= ibody_free) {
1418 if (old_idx < 0) {
1419 new_idx = h->ibody_count;
1420 add_to_ibody = 1;
1421 goto add_new;
1422 }
1423
1424 /* Update the existing entry. */
1425 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1426 shortname, name_idx, value,
1427 value_len, in_inode);
1428 if (ret)
1429 return ret;
1430 if (h->ibody_count <= old_idx) {
1431 /* Move entry from block to the end of ibody. */
1432 tmp = h->attrs[old_idx];
1433 memmove(h->attrs + h->ibody_count + 1,
1434 h->attrs + h->ibody_count,
1435 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1436 h->attrs[h->ibody_count] = tmp;
1437 h->ibody_count++;
1438 }
1439 return 0;
1440 }
1441
1442 if (h->ibody_count <= old_idx) {
1443 block_free += EXT2_EXT_ATTR_LEN(name_len);
1444 if (!h->attrs[old_idx].ea_ino)
1445 block_free +=
1446 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1447 }
1448
1449 if (needed > block_free)
1450 return EXT2_ET_EA_NO_SPACE;
1451
1452 if (old_idx >= 0) {
1453 /* Update the existing entry. */
1454 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1455 shortname, name_idx, value,
1456 value_len, in_inode);
1457 if (ret)
1458 return ret;
1459 if (old_idx < h->ibody_count) {
1460 /*
1461 * Move entry from ibody to the block. Note that
1462 * entries in the block are sorted.
1463 */
1464 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1465 h->count - h->ibody_count,
1466 shortname, name_idx);
1467 new_idx += h->ibody_count - 1;
1468 tmp = h->attrs[old_idx];
1469 memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1470 (new_idx - old_idx) * sizeof(*h->attrs));
1471 h->attrs[new_idx] = tmp;
1472 h->ibody_count--;
1473 }
1474 return 0;
1475 }
1476
1477 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1478 h->count - h->ibody_count,
1479 shortname, name_idx);
1480 new_idx += h->ibody_count;
1481 add_to_ibody = 0;
1482
1483 add_new:
1484 if (h->count == h->capacity) {
1485 ret = ext2fs_xattrs_expand(h, 4);
1486 if (ret)
1487 return ret;
1488 }
1489
1490 ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
1491 name_idx, value, value_len, in_inode);
1492 if (ret)
1493 return ret;
1494
1495 tmp = h->attrs[h->count];
1496 memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1497 (h->count - new_idx)*sizeof(*h->attrs));
1498 h->attrs[new_idx] = tmp;
1499 if (add_to_ibody)
1500 h->ibody_count++;
1501 h->count++;
1502 return 0;
1503 }
1504
space_used(struct ext2_xattr * attrs,int count)1505 static int space_used(struct ext2_xattr *attrs, int count)
1506 {
1507 int total = 0;
1508 struct ext2_xattr *x;
1509 int i, len;
1510
1511 for (i = 0, x = attrs; i < count; i++, x++) {
1512 len = strlen(x->short_name);
1513 total += EXT2_EXT_ATTR_LEN(len);
1514 if (!x->ea_ino)
1515 total += EXT2_EXT_ATTR_SIZE(x->value_len);
1516 }
1517 return total;
1518 }
1519
1520 /*
1521 * The minimum size of EA value when you start storing it in an external inode
1522 * size of block - size of header - size of 1 entry - 4 null bytes
1523 */
1524 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1525 ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1526
ext2fs_xattr_set(struct ext2_xattr_handle * h,const char * name,const void * value,size_t value_len)1527 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1528 const char *name,
1529 const void *value,
1530 size_t value_len)
1531 {
1532 ext2_filsys fs = h->fs;
1533 const int inode_size = EXT2_INODE_SIZE(fs->super);
1534 struct ext2_inode_large *inode = NULL;
1535 struct ext2_xattr *x;
1536 char *new_value;
1537 int ibody_free, block_free;
1538 int in_inode = 0;
1539 int old_idx = -1;
1540 int extra_isize;
1541 errcode_t ret;
1542
1543 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1544
1545 ret = ext2fs_get_mem(value_len, &new_value);
1546 if (ret)
1547 return ret;
1548 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1549 ((strcmp(name, "system.posix_acl_default") == 0) ||
1550 (strcmp(name, "system.posix_acl_access") == 0))) {
1551 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1552 new_value, &value_len);
1553 if (ret)
1554 goto out;
1555 } else if (value_len)
1556 memcpy(new_value, value, value_len);
1557
1558 /* Imitate kernel behavior by skipping update if value is the same. */
1559 for (x = h->attrs; x < h->attrs + h->count; x++) {
1560 if (!strcmp(x->name, name)) {
1561 if (!x->ea_ino && x->value_len == value_len &&
1562 (!value_len ||
1563 !memcmp(x->value, new_value, value_len))) {
1564 ret = 0;
1565 goto out;
1566 }
1567 old_idx = x - h->attrs;
1568 break;
1569 }
1570 }
1571
1572 ret = ext2fs_get_memzero(inode_size, &inode);
1573 if (ret)
1574 goto out;
1575 ret = ext2fs_read_inode_full(fs, h->ino,
1576 (struct ext2_inode *)inode,
1577 inode_size);
1578 if (ret)
1579 goto out;
1580 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1581 extra_isize = inode->i_extra_isize;
1582 if (extra_isize == 0) {
1583 extra_isize = fs->super->s_want_extra_isize;
1584 if (extra_isize == 0)
1585 extra_isize = sizeof(__u32);
1586 }
1587 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1588 ibody_free -= extra_isize;
1589 /* Extended attribute magic and final null entry. */
1590 ibody_free -= sizeof(__u32) * 2;
1591 ibody_free -= space_used(h->attrs, h->ibody_count);
1592 } else
1593 ibody_free = 0;
1594
1595 /* Inline data can only go to ibody. */
1596 if (strcmp(name, "system.data") == 0) {
1597 if (h->ibody_count <= old_idx) {
1598 ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1599 goto out;
1600 }
1601 ret = xattr_array_update(h, name, new_value, value_len,
1602 ibody_free,
1603 0 /* block_free */, old_idx,
1604 0 /* in_inode */);
1605 if (ret)
1606 goto out;
1607 goto write_out;
1608 }
1609
1610 block_free = fs->blocksize;
1611 block_free -= sizeof(struct ext2_ext_attr_header);
1612 /* Final null entry. */
1613 block_free -= sizeof(__u32);
1614 block_free -= space_used(h->attrs + h->ibody_count,
1615 h->count - h->ibody_count);
1616
1617 if (ext2fs_has_feature_ea_inode(fs->super) &&
1618 value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1619 in_inode = 1;
1620
1621 ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1622 block_free, old_idx, in_inode);
1623 if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1624 ext2fs_has_feature_ea_inode(fs->super))
1625 ret = xattr_array_update(h, name, new_value, value_len,
1626 ibody_free, block_free, old_idx, 1 /* in_inode */);
1627 if (ret)
1628 goto out;
1629
1630 write_out:
1631 ret = ext2fs_xattrs_write(h);
1632 out:
1633 if (inode)
1634 ext2fs_free_mem(&inode);
1635 ext2fs_free_mem(&new_value);
1636 return ret;
1637 }
1638
ext2fs_xattr_remove(struct ext2_xattr_handle * handle,const char * key)1639 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1640 const char *key)
1641 {
1642 struct ext2_xattr *x;
1643 struct ext2_xattr *end = handle->attrs + handle->count;
1644
1645 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1646 for (x = handle->attrs; x < end; x++) {
1647 if (strcmp(x->name, key) == 0) {
1648 ext2fs_free_mem(&x->name);
1649 ext2fs_free_mem(&x->value);
1650 if (x->ea_ino)
1651 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1652 memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1653 memset(end - 1, 0, sizeof(*end));
1654 if (x < handle->attrs + handle->ibody_count)
1655 handle->ibody_count--;
1656 handle->count--;
1657 return ext2fs_xattrs_write(handle);
1658 }
1659 }
1660
1661 /* no key found, success! */
1662 return 0;
1663 }
1664
ext2fs_xattrs_open(ext2_filsys fs,ext2_ino_t ino,struct ext2_xattr_handle ** handle)1665 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1666 struct ext2_xattr_handle **handle)
1667 {
1668 struct ext2_xattr_handle *h;
1669 errcode_t err;
1670
1671 if (!ext2fs_has_feature_xattr(fs->super) &&
1672 !ext2fs_has_feature_inline_data(fs->super))
1673 return EXT2_ET_MISSING_EA_FEATURE;
1674
1675 err = ext2fs_get_memzero(sizeof(*h), &h);
1676 if (err)
1677 return err;
1678
1679 h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1680 h->capacity = 4;
1681 err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1682 &h->attrs);
1683 if (err) {
1684 ext2fs_free_mem(&h);
1685 return err;
1686 }
1687 h->count = 0;
1688 h->ino = ino;
1689 h->fs = fs;
1690 *handle = h;
1691 return 0;
1692 }
1693
ext2fs_xattrs_close(struct ext2_xattr_handle ** handle)1694 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1695 {
1696 struct ext2_xattr_handle *h = *handle;
1697
1698 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1699 xattrs_free_keys(h);
1700 ext2fs_free_mem(&h->attrs);
1701 ext2fs_free_mem(handle);
1702 return 0;
1703 }
1704
ext2fs_xattrs_count(struct ext2_xattr_handle * handle,size_t * count)1705 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1706 {
1707 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1708 *count = handle->count;
1709 return 0;
1710 }
1711
ext2fs_xattrs_flags(struct ext2_xattr_handle * handle,unsigned int * new_flags,unsigned int * old_flags)1712 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1713 unsigned int *new_flags, unsigned int *old_flags)
1714 {
1715 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1716 if (old_flags)
1717 *old_flags = handle->flags;
1718 if (new_flags)
1719 handle->flags = *new_flags;
1720 return 0;
1721 }
1722