2 * inline_data.c --- data in inode
4 * Copyright (C) 2012 Zheng Liu <wenqing.lz@taobao.com>
7 * This file may be redistributed under the terms of the GNU library
8 * General Public License, version 2.
15 #include <limits.h> /* for PATH_MAX */
18 #include "ext2_ext_attr.h"
23 struct ext2_inline_data {
26 size_t ea_size; /* the size of inline data in ea area */
30 static errcode_t ext2fs_inline_data_ea_set(struct ext2_inline_data *data)
32 struct ext2_xattr_handle *handle;
35 retval = ext2fs_xattrs_open(data->fs, data->ino, &handle);
39 retval = ext2fs_xattrs_read(handle);
43 retval = ext2fs_xattr_set(handle, "system.data",
44 data->ea_data, data->ea_size);
48 retval = ext2fs_xattrs_write(handle);
51 (void) ext2fs_xattrs_close(&handle);
55 static errcode_t ext2fs_inline_data_ea_get(struct ext2_inline_data *data)
57 struct ext2_xattr_handle *handle;
63 retval = ext2fs_xattrs_open(data->fs, data->ino, &handle);
67 retval = ext2fs_xattrs_read(handle);
71 retval = ext2fs_xattr_get(handle, "system.data",
72 (void **)&data->ea_data, &data->ea_size);
73 if (retval == EXT2_ET_EA_KEY_NOT_FOUND) {
81 (void) ext2fs_xattrs_close(&handle);
85 errcode_t ext2fs_inline_data_init(ext2_filsys fs, ext2_ino_t ino)
87 struct ext2_inline_data data;
93 return ext2fs_inline_data_ea_set(&data);
96 errcode_t ext2fs_inline_data_size(ext2_filsys fs, ext2_ino_t ino, size_t *size)
98 struct ext2_inode inode;
99 struct ext2_inline_data data;
102 retval = ext2fs_read_inode(fs, ino, &inode);
106 if (!(inode.i_flags & EXT4_INLINE_DATA_FL))
107 return EXT2_ET_NO_INLINE_DATA;
111 retval = ext2fs_inline_data_ea_get(&data);
115 *size = EXT4_MIN_INLINE_DATA_SIZE + data.ea_size;
116 return ext2fs_free_mem(&data.ea_data);
119 int ext2fs_inline_data_dir_iterate(ext2_filsys fs, ext2_ino_t ino,
122 struct dir_context *ctx;
123 struct ext2_inode inode;
124 struct ext2_dir_entry dirent;
125 struct ext2_inline_data data;
126 int ret = BLOCK_ABORT;
127 e2_blkcnt_t blockcnt = 0;
129 unsigned int old_buflen;
132 ctx = (struct dir_context *)priv_data;
134 old_buflen = ctx->buflen;
135 old_flags = ctx->flags;
136 ctx->flags |= DIRENT_FLAG_INCLUDE_INLINE_DATA;
138 ctx->errcode = ext2fs_read_inode(fs, ino, &inode);
142 if (!(inode.i_flags & EXT4_INLINE_DATA_FL)) {
143 ctx->errcode = EXT2_ET_NO_INLINE_DATA;
147 if (!LINUX_S_ISDIR(inode.i_mode)) {
148 ctx->errcode = EXT2_ET_NO_DIRECTORY;
153 /* we first check '.' and '..' dir */
156 ext2fs_set_rec_len(fs, EXT2_DIR_REC_LEN(2), &dirent);
157 dirent.name[0] = '.';
158 dirent.name[1] = '\0';
159 ctx->buf = (char *)&dirent;
160 ext2fs_get_rec_len(fs, &dirent, &ctx->buflen);
161 ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
162 if (ret & BLOCK_ABORT)
165 dirent.inode = ext2fs_le32_to_cpu(inode.i_block[0]);
167 ext2fs_set_rec_len(fs, EXT2_DIR_REC_LEN(3), &dirent);
168 dirent.name[0] = '.';
169 dirent.name[1] = '.';
170 dirent.name[2] = '\0';
171 ctx->buf = (char *)&dirent;
172 ext2fs_get_rec_len(fs, &dirent, &ctx->buflen);
173 ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
174 if (ret & BLOCK_INLINE_DATA_CHANGED) {
177 inode.i_block[0] = ext2fs_cpu_to_le32(dirent.inode);
178 err = ext2fs_write_inode(fs, ino, &inode);
181 ret &= ~BLOCK_INLINE_DATA_CHANGED;
183 if (ret & BLOCK_ABORT)
186 ctx->buf = (char *)inode.i_block + EXT4_INLINE_DATA_DOTDOT_SIZE;
187 ctx->buflen = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE;
188 #ifdef WORDS_BIGENDIAN
189 ctx->errcode = ext2fs_dirent_swab_in2(fs, ctx->buf, ctx->buflen, 0);
195 ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
196 if (ret & BLOCK_INLINE_DATA_CHANGED) {
197 #ifdef WORDS_BIGENDIAN
198 ctx->errcode = ext2fs_dirent_swab_out2(fs, ctx->buf,
205 ctx->errcode = ext2fs_write_inode(fs, ino, &inode);
208 ret &= ~BLOCK_INLINE_DATA_CHANGED;
210 if (ret & BLOCK_ABORT)
215 ctx->errcode = ext2fs_inline_data_ea_get(&data);
220 if (data.ea_size <= 0)
223 ctx->buf = data.ea_data;
224 ctx->buflen = data.ea_size;
225 #ifdef WORDS_BIGENDIAN
226 ctx->errcode = ext2fs_dirent_swab_in2(fs, ctx->buf, ctx->buflen, 0);
233 ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
234 if (ret & BLOCK_INLINE_DATA_CHANGED) {
235 #ifdef WORDS_BIGENDIAN
236 ctx->errcode = ext2fs_dirent_swab_out2(fs, ctx->buf,
243 ctx->errcode = ext2fs_inline_data_ea_set(&data);
249 ext2fs_free_mem(&data.ea_data);
252 ctx->buflen = old_buflen;
253 ctx->flags = old_flags;
254 ret &= ~(BLOCK_ABORT | BLOCK_INLINE_DATA_CHANGED);
258 errcode_t ext2fs_inline_data_ea_remove(ext2_filsys fs, ext2_ino_t ino)
260 struct ext2_xattr_handle *handle;
263 retval = ext2fs_xattrs_open(fs, ino, &handle);
267 retval = ext2fs_xattrs_read(handle);
271 retval = ext2fs_xattr_remove(handle, "system.data");
275 retval = ext2fs_xattrs_write(handle);
278 (void) ext2fs_xattrs_close(&handle);
282 static errcode_t ext2fs_inline_data_convert_dir(ext2_filsys fs, ext2_ino_t ino,
283 char *bbuf, char *ibuf, int size)
285 struct ext2_dir_entry *dir, *dir2;
286 struct ext2_dir_entry_tail *t;
288 unsigned int offset, rec_len;
292 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
293 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
294 csum_size = sizeof(struct ext2_dir_entry_tail);
296 /* Create '.' and '..' */
297 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
298 EXT2_FEATURE_INCOMPAT_FILETYPE))
299 filetype = EXT2_FT_DIR;
302 * Set up entry for '.'
304 dir = (struct ext2_dir_entry *) bbuf;
306 ext2fs_dirent_set_name_len(dir, 1);
307 ext2fs_dirent_set_file_type(dir, filetype);
309 rec_len = (fs->blocksize - csum_size) - EXT2_DIR_REC_LEN(1);
310 dir->rec_len = EXT2_DIR_REC_LEN(1);
313 * Set up entry for '..'
315 dir = (struct ext2_dir_entry *) (bbuf + dir->rec_len);
316 dir->rec_len = EXT2_DIR_REC_LEN(2);
317 dir->inode = ext2fs_le32_to_cpu(((__u32 *)ibuf)[0]);
318 ext2fs_dirent_set_name_len(dir, 2);
319 ext2fs_dirent_set_file_type(dir, filetype);
324 * Ajust the last rec_len
326 offset = EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2);
327 dir = (struct ext2_dir_entry *) (bbuf + offset);
328 memcpy(bbuf + offset, ibuf + EXT4_INLINE_DATA_DOTDOT_SIZE,
329 size - EXT4_INLINE_DATA_DOTDOT_SIZE);
330 size += EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) -
331 EXT4_INLINE_DATA_DOTDOT_SIZE;
335 retval = ext2fs_get_rec_len(fs, dir, &rec_len);
339 dir = (struct ext2_dir_entry *) (bbuf + offset);
340 } while (offset < size);
341 rec_len += fs->blocksize - csum_size - offset;
342 retval = ext2fs_set_rec_len(fs, rec_len, dir2);
347 t = EXT2_DIRENT_TAIL(bbuf, fs->blocksize);
348 ext2fs_initialize_dirent_tail(fs, t);
356 ext2fs_inline_data_dir_expand(ext2_filsys fs, ext2_ino_t ino,
357 struct ext2_inode *inode, char *buf, size_t size)
363 retval = ext2fs_get_memzero(fs->blocksize, &blk_buf);
367 #ifdef WORDS_BIGENDIAN
368 retval = ext2fs_dirent_swab_in2(fs, buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
374 /* Adjust the rec_len */
375 retval = ext2fs_inline_data_convert_dir(fs, ino, blk_buf, buf, size);
378 /* Allocate a new block */
379 retval = ext2fs_new_block2(fs, 0, 0, &blk);
382 retval = ext2fs_write_dir_block4(fs, blk, blk_buf, 0, ino);
387 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, EXT3_FEATURE_INCOMPAT_EXTENTS))
388 inode->i_flags |= EXT4_EXTENTS_FL;
389 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
390 retval = ext2fs_iblk_add_blocks(fs, inode, 1);
393 inode->i_size = fs->blocksize;
394 retval = ext2fs_bmap2(fs, ino, inode, 0, BMAP_SET, 0, 0, &blk);
397 retval = ext2fs_write_inode(fs, ino, inode);
400 ext2fs_block_alloc_stats(fs, blk, +1);
403 ext2fs_free_mem(&blk_buf);
408 ext2fs_inline_data_file_expand(ext2_filsys fs, ext2_ino_t ino,
409 struct ext2_inode *inode, char *buf, size_t size)
415 memset(inode->i_block, 0, sizeof(inode->i_block));
416 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
417 EXT3_FEATURE_INCOMPAT_EXTENTS)) {
418 ext2_extent_handle_t handle;
420 inode->i_flags &= ~EXT4_EXTENTS_FL;
421 retval = ext2fs_extent_open2(fs, ino, inode, &handle);
424 ext2fs_extent_free(handle);
426 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
428 retval = ext2fs_write_inode(fs, ino, inode);
432 /* Write out the block buffer */
433 retval = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
436 retval = ext2fs_file_write(e2_file, buf, size, 0);
437 ext2fs_file_close(e2_file);
441 errcode_t ext2fs_inline_data_expand(ext2_filsys fs, ext2_ino_t ino)
443 struct ext2_inode inode;
444 struct ext2_inline_data data;
447 char *inline_buf = 0;
449 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
451 retval = ext2fs_read_inode(fs, ino, &inode);
455 if (!(inode.i_flags & EXT4_INLINE_DATA_FL))
456 return EXT2_ET_NO_INLINE_DATA;
460 retval = ext2fs_inline_data_ea_get(&data);
463 inline_size = data.ea_size + EXT4_MIN_INLINE_DATA_SIZE;
464 retval = ext2fs_get_mem(inline_size, &inline_buf);
468 memcpy(inline_buf, (void *)inode.i_block, EXT4_MIN_INLINE_DATA_SIZE);
469 if (data.ea_size > 0) {
470 memcpy(inline_buf + EXT4_MIN_INLINE_DATA_SIZE,
471 data.ea_data, data.ea_size);
474 memset((void *)inode.i_block, 0, EXT4_MIN_INLINE_DATA_SIZE);
476 * NOTE: We must do this write -> ea_remove -> read cycle here because
477 * removing the inline data EA can free the EA block, which is a change
478 * that our stack copy of the inode will never see. If that happens,
479 * we can end up with the EA block and lblk 0 pointing to the same
480 * pblk, which is bad news.
482 retval = ext2fs_write_inode(fs, ino, &inode);
485 retval = ext2fs_inline_data_ea_remove(fs, ino);
488 retval = ext2fs_read_inode(fs, ino, &inode);
492 if (LINUX_S_ISDIR(inode.i_mode)) {
493 retval = ext2fs_inline_data_dir_expand(fs, ino, &inode,
494 inline_buf, inline_size);
496 retval = ext2fs_inline_data_file_expand(fs, ino, &inode,
497 inline_buf, inline_size);
502 ext2fs_free_mem(&inline_buf);
503 ext2fs_free_mem(&data.ea_data);
508 * When caller uses this function to retrieve the inline data, it must
509 * allocate a buffer which has the size of inline data. The size of
510 * inline data can be know by ext2fs_inline_data_get_size().
512 errcode_t ext2fs_inline_data_get(ext2_filsys fs, ext2_ino_t ino,
513 struct ext2_inode *inode,
514 void *buf, size_t *size)
516 struct ext2_inode inode_buf;
517 struct ext2_inline_data data;
521 retval = ext2fs_read_inode(fs, ino, &inode_buf);
529 retval = ext2fs_inline_data_ea_get(&data);
533 memcpy(buf, (void *)inode->i_block, EXT4_MIN_INLINE_DATA_SIZE);
534 if (data.ea_size > 0)
535 memcpy(buf + EXT4_MIN_INLINE_DATA_SIZE,
536 data.ea_data, data.ea_size);
539 *size = EXT4_MIN_INLINE_DATA_SIZE + data.ea_size;
540 ext2fs_free_mem(&data.ea_data);
544 errcode_t ext2fs_inline_data_set(ext2_filsys fs, ext2_ino_t ino,
545 struct ext2_inode *inode,
546 void *buf, size_t size)
548 struct ext2_inode inode_buf;
549 struct ext2_inline_data data;
551 size_t free_ea_size, existing_size, free_inode_size;
554 retval = ext2fs_read_inode(fs, ino, &inode_buf);
560 if (size <= EXT4_MIN_INLINE_DATA_SIZE) {
561 retval = ext2fs_inline_data_ea_remove(fs, ino);
564 memcpy((void *)inode->i_block, buf, size);
565 return ext2fs_write_inode(fs, ino, inode);
568 retval = ext2fs_xattr_inode_max_size(fs, ino, &free_ea_size);
572 retval = ext2fs_inline_data_size(fs, ino, &existing_size);
576 if (existing_size < EXT4_MIN_INLINE_DATA_SIZE)
577 free_inode_size = EXT4_MIN_INLINE_DATA_SIZE - existing_size;
581 if (size != existing_size &&
582 size > existing_size + free_ea_size + free_inode_size)
583 return EXT2_ET_INLINE_DATA_NO_SPACE;
585 memcpy((void *)inode->i_block, buf, EXT4_MIN_INLINE_DATA_SIZE);
586 retval = ext2fs_write_inode(fs, ino, inode);
591 data.ea_size = size - EXT4_MIN_INLINE_DATA_SIZE;
592 data.ea_data = buf + EXT4_MIN_INLINE_DATA_SIZE;
593 return ext2fs_inline_data_ea_set(&data);
600 * The length of buffer is set to 64 because in inode's i_block member it only
601 * can save 60 bytes. Thus this value can let the data being saved in extra
604 #define BUFF_SIZE (64)
606 static errcode_t file_test(ext2_filsys fs)
608 struct ext2_inode inode;
612 char *buf = 0, *cmpbuf = 0;
614 /* create a new file */
615 retval = ext2fs_new_inode(fs, 2, 010755, 0, &newfile);
617 com_err("file_test", retval, "while allocaing a new inode");
621 memset(&inode, 0, sizeof(inode));
622 inode.i_flags |= EXT4_INLINE_DATA_FL;
623 inode.i_size = EXT4_MIN_INLINE_DATA_SIZE;
624 inode.i_mode = LINUX_S_IFREG;
625 retval = ext2fs_write_new_inode(fs, newfile, &inode);
627 com_err("file_test", retval, "while writting a new inode");
631 retval = ext2fs_inline_data_init(fs, newfile);
633 com_err("file_test", retval, "while init 'system.data'");
637 retval = ext2fs_inline_data_size(fs, newfile, &size);
639 com_err("file_test", retval, "while getting size");
643 if (size != EXT4_MIN_INLINE_DATA_SIZE) {
645 "tst_inline_data: size of inline data is wrong\n");
649 ext2fs_get_mem(BUFF_SIZE, &buf);
650 memset(buf, 'a', BUFF_SIZE);
651 retval = ext2fs_inline_data_set(fs, newfile, 0, buf, BUFF_SIZE);
653 com_err("file_test", retval,
654 "while setting inline data %s", buf);
658 ext2fs_get_mem(BUFF_SIZE, &cmpbuf);
659 retval = ext2fs_inline_data_get(fs, newfile, 0, cmpbuf, &size);
661 com_err("file_test", retval, "while getting inline data");
665 if (size != BUFF_SIZE) {
667 "tst_inline_data: size %lu != buflen %u\n",
673 if (memcmp(buf, cmpbuf, BUFF_SIZE)) {
674 fprintf(stderr, "tst_inline_data: buf != cmpbuf\n");
679 retval = ext2fs_punch(fs, newfile, 0, 0, 0, ~0ULL);
681 com_err("file_test", retval, "while truncating inode");
685 /* reload inode and check isize */
686 ext2fs_read_inode(fs, newfile, &inode);
687 if (inode.i_size != 0) {
688 fprintf(stderr, "tst_inline_data: i_size should be 0\n");
694 ext2fs_free_mem(&cmpbuf);
696 ext2fs_free_mem(&buf);
700 static errcode_t dir_test(ext2_filsys fs)
702 const char *dot_name = ".";
703 const char *stub_name = "stub";
704 const char *parent_name = "test";
705 ext2_ino_t parent, dir, tmp;
707 char dirname[PATH_MAX];
710 retval = ext2fs_mkdir(fs, 11, 11, stub_name);
712 com_err("dir_test", retval, "while creating %s dir", stub_name);
716 retval = ext2fs_mkdir(fs, 11, 0, parent_name);
718 com_err("dir_test", retval,
719 "while creating %s dir", parent_name);
723 retval = ext2fs_lookup(fs, 11, parent_name, strlen(parent_name),
726 com_err("dir_test", retval,
727 "while looking up %s dir", parent_name);
731 retval = ext2fs_lookup(fs, parent, dot_name, strlen(dot_name),
734 com_err("dir_test", retval,
735 "while looking up %s dir", parent_name);
740 fprintf(stderr, "tst_inline_data: parent (%u) != tmp (%u)\n",
745 for (i = 0, dir = 13; i < 4; i++, dir++) {
747 snprintf(dirname, sizeof(dirname), "%d", i);
748 retval = ext2fs_mkdir(fs, parent, 0, dirname);
750 com_err("dir_test", retval,
751 "while creating %s dir", dirname);
755 retval = ext2fs_lookup(fs, parent, dirname, strlen(dirname),
758 com_err("dir_test", retval,
759 "while looking up %s dir", parent_name);
765 "tst_inline_data: dir (%u) != tmp (%u)\n",
771 snprintf(dirname, sizeof(dirname), "%d", i);
772 retval = ext2fs_mkdir(fs, parent, 0, dirname);
773 if (retval && retval != EXT2_ET_DIR_NO_SPACE) {
774 com_err("dir_test", retval, "while creating %s dir", dirname);
778 retval = ext2fs_expand_dir(fs, parent);
780 com_err("dir_test", retval, "while expanding %s dir", parent_name);
787 int main(int argc, char *argv[])
790 struct ext2_super_block param;
794 initialize_ext2_error_table();
796 memset(¶m, 0, sizeof(param));
797 ext2fs_blocks_count_set(¶m, 32768);
798 param.s_inodes_count = 100;
800 param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_INLINE_DATA;
801 param.s_rev_level = EXT2_DYNAMIC_REV;
802 param.s_inode_size = 256;
804 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
805 test_io_manager, &fs);
807 com_err("setup", retval,
808 "while initializing filesystem");
812 retval = ext2fs_allocate_tables(fs);
814 com_err("setup", retval,
815 "while allocating tables for test filesysmte");
819 /* initialize inode cache */
821 ext2_ino_t first_ino = EXT2_FIRST_INO(fs->super);
824 /* we just want to init inode cache. So ignore error */
825 ext2fs_create_inode_cache(fs, 16);
828 "tst_inline_data: init inode cache failed\n");
832 /* setup inode cache */
833 for (i = 0; i < fs->icache->cache_size; i++)
834 fs->icache->cache[i].ino = first_ino++;
839 fprintf(stderr, "tst_inline_data(FILE): FAILED\n");
842 printf("tst_inline_data(FILE): OK\n");
845 fprintf(stderr, "tst_inline_data(DIR): FAILED\n");
848 printf("tst_inline_data(DIR): OK\n");