0001
0002
0003
0004
0005
0006
0007 #include <linux/iomap.h>
0008 #include <linux/fiemap.h>
0009 #include <linux/namei.h>
0010 #include <linux/iversion.h>
0011 #include <linux/sched/mm.h>
0012
0013 #include "ext4_jbd2.h"
0014 #include "ext4.h"
0015 #include "xattr.h"
0016 #include "truncate.h"
0017
0018 #define EXT4_XATTR_SYSTEM_DATA "data"
0019 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
0020 #define EXT4_INLINE_DOTDOT_OFFSET 2
0021 #define EXT4_INLINE_DOTDOT_SIZE 4
0022
0023 static int ext4_get_inline_size(struct inode *inode)
0024 {
0025 if (EXT4_I(inode)->i_inline_off)
0026 return EXT4_I(inode)->i_inline_size;
0027
0028 return 0;
0029 }
0030
0031 static int get_max_inline_xattr_value_size(struct inode *inode,
0032 struct ext4_iloc *iloc)
0033 {
0034 struct ext4_xattr_ibody_header *header;
0035 struct ext4_xattr_entry *entry;
0036 struct ext4_inode *raw_inode;
0037 int free, min_offs;
0038
0039 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
0040 return 0;
0041
0042 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
0043 EXT4_GOOD_OLD_INODE_SIZE -
0044 EXT4_I(inode)->i_extra_isize -
0045 sizeof(struct ext4_xattr_ibody_header);
0046
0047
0048
0049
0050
0051
0052 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
0053 return EXT4_XATTR_SIZE(min_offs -
0054 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
0055 EXT4_XATTR_ROUND - sizeof(__u32));
0056
0057 raw_inode = ext4_raw_inode(iloc);
0058 header = IHDR(inode, raw_inode);
0059 entry = IFIRST(header);
0060
0061
0062 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
0063 if (!entry->e_value_inum && entry->e_value_size) {
0064 size_t offs = le16_to_cpu(entry->e_value_offs);
0065 if (offs < min_offs)
0066 min_offs = offs;
0067 }
0068 }
0069 free = min_offs -
0070 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
0071
0072 if (EXT4_I(inode)->i_inline_off) {
0073 entry = (struct ext4_xattr_entry *)
0074 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
0075
0076 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
0077 goto out;
0078 }
0079
0080 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
0081
0082 if (free > EXT4_XATTR_ROUND)
0083 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
0084 else
0085 free = 0;
0086
0087 out:
0088 return free;
0089 }
0090
0091
0092
0093
0094
0095
0096 int ext4_get_max_inline_size(struct inode *inode)
0097 {
0098 int error, max_inline_size;
0099 struct ext4_iloc iloc;
0100
0101 if (EXT4_I(inode)->i_extra_isize == 0)
0102 return 0;
0103
0104 error = ext4_get_inode_loc(inode, &iloc);
0105 if (error) {
0106 ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
0107 "can't get inode location %lu",
0108 inode->i_ino);
0109 return 0;
0110 }
0111
0112 down_read(&EXT4_I(inode)->xattr_sem);
0113 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
0114 up_read(&EXT4_I(inode)->xattr_sem);
0115
0116 brelse(iloc.bh);
0117
0118 if (!max_inline_size)
0119 return 0;
0120
0121 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
0122 }
0123
0124
0125
0126
0127
0128
0129 int ext4_find_inline_data_nolock(struct inode *inode)
0130 {
0131 struct ext4_xattr_ibody_find is = {
0132 .s = { .not_found = -ENODATA, },
0133 };
0134 struct ext4_xattr_info i = {
0135 .name_index = EXT4_XATTR_INDEX_SYSTEM,
0136 .name = EXT4_XATTR_SYSTEM_DATA,
0137 };
0138 int error;
0139
0140 if (EXT4_I(inode)->i_extra_isize == 0)
0141 return 0;
0142
0143 error = ext4_get_inode_loc(inode, &is.iloc);
0144 if (error)
0145 return error;
0146
0147 error = ext4_xattr_ibody_find(inode, &i, &is);
0148 if (error)
0149 goto out;
0150
0151 if (!is.s.not_found) {
0152 if (is.s.here->e_value_inum) {
0153 EXT4_ERROR_INODE(inode, "inline data xattr refers "
0154 "to an external xattr inode");
0155 error = -EFSCORRUPTED;
0156 goto out;
0157 }
0158 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
0159 (void *)ext4_raw_inode(&is.iloc));
0160 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
0161 le32_to_cpu(is.s.here->e_value_size);
0162 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0163 }
0164 out:
0165 brelse(is.iloc.bh);
0166 return error;
0167 }
0168
0169 static int ext4_read_inline_data(struct inode *inode, void *buffer,
0170 unsigned int len,
0171 struct ext4_iloc *iloc)
0172 {
0173 struct ext4_xattr_entry *entry;
0174 struct ext4_xattr_ibody_header *header;
0175 int cp_len = 0;
0176 struct ext4_inode *raw_inode;
0177
0178 if (!len)
0179 return 0;
0180
0181 BUG_ON(len > EXT4_I(inode)->i_inline_size);
0182
0183 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
0184 len : EXT4_MIN_INLINE_DATA_SIZE;
0185
0186 raw_inode = ext4_raw_inode(iloc);
0187 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
0188
0189 len -= cp_len;
0190 buffer += cp_len;
0191
0192 if (!len)
0193 goto out;
0194
0195 header = IHDR(inode, raw_inode);
0196 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
0197 EXT4_I(inode)->i_inline_off);
0198 len = min_t(unsigned int, len,
0199 (unsigned int)le32_to_cpu(entry->e_value_size));
0200
0201 memcpy(buffer,
0202 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
0203 cp_len += len;
0204
0205 out:
0206 return cp_len;
0207 }
0208
0209
0210
0211
0212
0213
0214
0215 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
0216 void *buffer, loff_t pos, unsigned int len)
0217 {
0218 struct ext4_xattr_entry *entry;
0219 struct ext4_xattr_ibody_header *header;
0220 struct ext4_inode *raw_inode;
0221 int cp_len = 0;
0222
0223 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
0224 return;
0225
0226 BUG_ON(!EXT4_I(inode)->i_inline_off);
0227 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
0228
0229 raw_inode = ext4_raw_inode(iloc);
0230 buffer += pos;
0231
0232 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
0233 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
0234 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
0235 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
0236
0237 len -= cp_len;
0238 buffer += cp_len;
0239 pos += cp_len;
0240 }
0241
0242 if (!len)
0243 return;
0244
0245 pos -= EXT4_MIN_INLINE_DATA_SIZE;
0246 header = IHDR(inode, raw_inode);
0247 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
0248 EXT4_I(inode)->i_inline_off);
0249
0250 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
0251 buffer, len);
0252 }
0253
0254 static int ext4_create_inline_data(handle_t *handle,
0255 struct inode *inode, unsigned len)
0256 {
0257 int error;
0258 void *value = NULL;
0259 struct ext4_xattr_ibody_find is = {
0260 .s = { .not_found = -ENODATA, },
0261 };
0262 struct ext4_xattr_info i = {
0263 .name_index = EXT4_XATTR_INDEX_SYSTEM,
0264 .name = EXT4_XATTR_SYSTEM_DATA,
0265 };
0266
0267 error = ext4_get_inode_loc(inode, &is.iloc);
0268 if (error)
0269 return error;
0270
0271 BUFFER_TRACE(is.iloc.bh, "get_write_access");
0272 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
0273 EXT4_JTR_NONE);
0274 if (error)
0275 goto out;
0276
0277 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
0278 value = EXT4_ZERO_XATTR_VALUE;
0279 len -= EXT4_MIN_INLINE_DATA_SIZE;
0280 } else {
0281 value = "";
0282 len = 0;
0283 }
0284
0285
0286 i.value = value;
0287 i.value_len = len;
0288
0289 error = ext4_xattr_ibody_find(inode, &i, &is);
0290 if (error)
0291 goto out;
0292
0293 BUG_ON(!is.s.not_found);
0294
0295 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
0296 if (error) {
0297 if (error == -ENOSPC)
0298 ext4_clear_inode_state(inode,
0299 EXT4_STATE_MAY_INLINE_DATA);
0300 goto out;
0301 }
0302
0303 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
0304 0, EXT4_MIN_INLINE_DATA_SIZE);
0305
0306 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
0307 (void *)ext4_raw_inode(&is.iloc));
0308 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
0309 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
0310 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
0311 get_bh(is.iloc.bh);
0312 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
0313
0314 out:
0315 brelse(is.iloc.bh);
0316 return error;
0317 }
0318
0319 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
0320 unsigned int len)
0321 {
0322 int error;
0323 void *value = NULL;
0324 struct ext4_xattr_ibody_find is = {
0325 .s = { .not_found = -ENODATA, },
0326 };
0327 struct ext4_xattr_info i = {
0328 .name_index = EXT4_XATTR_INDEX_SYSTEM,
0329 .name = EXT4_XATTR_SYSTEM_DATA,
0330 };
0331
0332
0333 if (len <= EXT4_I(inode)->i_inline_size)
0334 return 0;
0335
0336 error = ext4_get_inode_loc(inode, &is.iloc);
0337 if (error)
0338 return error;
0339
0340 error = ext4_xattr_ibody_find(inode, &i, &is);
0341 if (error)
0342 goto out;
0343
0344 BUG_ON(is.s.not_found);
0345
0346 len -= EXT4_MIN_INLINE_DATA_SIZE;
0347 value = kzalloc(len, GFP_NOFS);
0348 if (!value) {
0349 error = -ENOMEM;
0350 goto out;
0351 }
0352
0353 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
0354 value, len);
0355 if (error == -ENODATA)
0356 goto out;
0357
0358 BUFFER_TRACE(is.iloc.bh, "get_write_access");
0359 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
0360 EXT4_JTR_NONE);
0361 if (error)
0362 goto out;
0363
0364
0365 i.value = value;
0366 i.value_len = len;
0367
0368 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
0369 if (error)
0370 goto out;
0371
0372 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
0373 (void *)ext4_raw_inode(&is.iloc));
0374 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
0375 le32_to_cpu(is.s.here->e_value_size);
0376 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0377 get_bh(is.iloc.bh);
0378 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
0379
0380 out:
0381 kfree(value);
0382 brelse(is.iloc.bh);
0383 return error;
0384 }
0385
0386 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
0387 unsigned int len)
0388 {
0389 int ret, size, no_expand;
0390 struct ext4_inode_info *ei = EXT4_I(inode);
0391
0392 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
0393 return -ENOSPC;
0394
0395 size = ext4_get_max_inline_size(inode);
0396 if (size < len)
0397 return -ENOSPC;
0398
0399 ext4_write_lock_xattr(inode, &no_expand);
0400
0401 if (ei->i_inline_off)
0402 ret = ext4_update_inline_data(handle, inode, len);
0403 else
0404 ret = ext4_create_inline_data(handle, inode, len);
0405
0406 ext4_write_unlock_xattr(inode, &no_expand);
0407 return ret;
0408 }
0409
0410 static int ext4_destroy_inline_data_nolock(handle_t *handle,
0411 struct inode *inode)
0412 {
0413 struct ext4_inode_info *ei = EXT4_I(inode);
0414 struct ext4_xattr_ibody_find is = {
0415 .s = { .not_found = 0, },
0416 };
0417 struct ext4_xattr_info i = {
0418 .name_index = EXT4_XATTR_INDEX_SYSTEM,
0419 .name = EXT4_XATTR_SYSTEM_DATA,
0420 .value = NULL,
0421 .value_len = 0,
0422 };
0423 int error;
0424
0425 if (!ei->i_inline_off)
0426 return 0;
0427
0428 error = ext4_get_inode_loc(inode, &is.iloc);
0429 if (error)
0430 return error;
0431
0432 error = ext4_xattr_ibody_find(inode, &i, &is);
0433 if (error)
0434 goto out;
0435
0436 BUFFER_TRACE(is.iloc.bh, "get_write_access");
0437 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
0438 EXT4_JTR_NONE);
0439 if (error)
0440 goto out;
0441
0442 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
0443 if (error)
0444 goto out;
0445
0446 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
0447 0, EXT4_MIN_INLINE_DATA_SIZE);
0448 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
0449
0450 if (ext4_has_feature_extents(inode->i_sb)) {
0451 if (S_ISDIR(inode->i_mode) ||
0452 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
0453 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
0454 ext4_ext_tree_init(handle, inode);
0455 }
0456 }
0457 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
0458
0459 get_bh(is.iloc.bh);
0460 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
0461
0462 EXT4_I(inode)->i_inline_off = 0;
0463 EXT4_I(inode)->i_inline_size = 0;
0464 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0465 out:
0466 brelse(is.iloc.bh);
0467 if (error == -ENODATA)
0468 error = 0;
0469 return error;
0470 }
0471
0472 static int ext4_read_inline_page(struct inode *inode, struct page *page)
0473 {
0474 void *kaddr;
0475 int ret = 0;
0476 size_t len;
0477 struct ext4_iloc iloc;
0478
0479 BUG_ON(!PageLocked(page));
0480 BUG_ON(!ext4_has_inline_data(inode));
0481 BUG_ON(page->index);
0482
0483 if (!EXT4_I(inode)->i_inline_off) {
0484 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
0485 inode->i_ino);
0486 goto out;
0487 }
0488
0489 ret = ext4_get_inode_loc(inode, &iloc);
0490 if (ret)
0491 goto out;
0492
0493 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
0494 kaddr = kmap_atomic(page);
0495 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
0496 flush_dcache_page(page);
0497 kunmap_atomic(kaddr);
0498 zero_user_segment(page, len, PAGE_SIZE);
0499 SetPageUptodate(page);
0500 brelse(iloc.bh);
0501
0502 out:
0503 return ret;
0504 }
0505
0506 int ext4_readpage_inline(struct inode *inode, struct page *page)
0507 {
0508 int ret = 0;
0509
0510 down_read(&EXT4_I(inode)->xattr_sem);
0511 if (!ext4_has_inline_data(inode)) {
0512 up_read(&EXT4_I(inode)->xattr_sem);
0513 return -EAGAIN;
0514 }
0515
0516
0517
0518
0519
0520 if (!page->index)
0521 ret = ext4_read_inline_page(inode, page);
0522 else if (!PageUptodate(page)) {
0523 zero_user_segment(page, 0, PAGE_SIZE);
0524 SetPageUptodate(page);
0525 }
0526
0527 up_read(&EXT4_I(inode)->xattr_sem);
0528
0529 unlock_page(page);
0530 return ret >= 0 ? 0 : ret;
0531 }
0532
0533 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
0534 struct inode *inode)
0535 {
0536 int ret, needed_blocks, no_expand;
0537 handle_t *handle = NULL;
0538 int retries = 0, sem_held = 0;
0539 struct page *page = NULL;
0540 unsigned int flags;
0541 unsigned from, to;
0542 struct ext4_iloc iloc;
0543
0544 if (!ext4_has_inline_data(inode)) {
0545
0546
0547
0548
0549 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0550 return 0;
0551 }
0552
0553 needed_blocks = ext4_writepage_trans_blocks(inode);
0554
0555 ret = ext4_get_inode_loc(inode, &iloc);
0556 if (ret)
0557 return ret;
0558
0559 retry:
0560 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
0561 if (IS_ERR(handle)) {
0562 ret = PTR_ERR(handle);
0563 handle = NULL;
0564 goto out;
0565 }
0566
0567
0568
0569 flags = memalloc_nofs_save();
0570 page = grab_cache_page_write_begin(mapping, 0);
0571 memalloc_nofs_restore(flags);
0572 if (!page) {
0573 ret = -ENOMEM;
0574 goto out;
0575 }
0576
0577 ext4_write_lock_xattr(inode, &no_expand);
0578 sem_held = 1;
0579
0580 if (!ext4_has_inline_data(inode)) {
0581 ret = 0;
0582 goto out;
0583 }
0584
0585 from = 0;
0586 to = ext4_get_inline_size(inode);
0587 if (!PageUptodate(page)) {
0588 ret = ext4_read_inline_page(inode, page);
0589 if (ret < 0)
0590 goto out;
0591 }
0592
0593 ret = ext4_destroy_inline_data_nolock(handle, inode);
0594 if (ret)
0595 goto out;
0596
0597 if (ext4_should_dioread_nolock(inode)) {
0598 ret = __block_write_begin(page, from, to,
0599 ext4_get_block_unwritten);
0600 } else
0601 ret = __block_write_begin(page, from, to, ext4_get_block);
0602
0603 if (!ret && ext4_should_journal_data(inode)) {
0604 ret = ext4_walk_page_buffers(handle, inode, page_buffers(page),
0605 from, to, NULL,
0606 do_journal_get_write_access);
0607 }
0608
0609 if (ret) {
0610 unlock_page(page);
0611 put_page(page);
0612 page = NULL;
0613 ext4_orphan_add(handle, inode);
0614 ext4_write_unlock_xattr(inode, &no_expand);
0615 sem_held = 0;
0616 ext4_journal_stop(handle);
0617 handle = NULL;
0618 ext4_truncate_failed_write(inode);
0619
0620
0621
0622
0623
0624
0625 if (inode->i_nlink)
0626 ext4_orphan_del(NULL, inode);
0627 }
0628
0629 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
0630 goto retry;
0631
0632 if (page)
0633 block_commit_write(page, from, to);
0634 out:
0635 if (page) {
0636 unlock_page(page);
0637 put_page(page);
0638 }
0639 if (sem_held)
0640 ext4_write_unlock_xattr(inode, &no_expand);
0641 if (handle)
0642 ext4_journal_stop(handle);
0643 brelse(iloc.bh);
0644 return ret;
0645 }
0646
0647
0648
0649
0650
0651
0652
0653 int ext4_try_to_write_inline_data(struct address_space *mapping,
0654 struct inode *inode,
0655 loff_t pos, unsigned len,
0656 struct page **pagep)
0657 {
0658 int ret;
0659 handle_t *handle;
0660 unsigned int flags;
0661 struct page *page;
0662 struct ext4_iloc iloc;
0663
0664 if (pos + len > ext4_get_max_inline_size(inode))
0665 goto convert;
0666
0667 ret = ext4_get_inode_loc(inode, &iloc);
0668 if (ret)
0669 return ret;
0670
0671
0672
0673
0674
0675 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
0676 if (IS_ERR(handle)) {
0677 ret = PTR_ERR(handle);
0678 handle = NULL;
0679 goto out;
0680 }
0681
0682 ret = ext4_prepare_inline_data(handle, inode, pos + len);
0683 if (ret && ret != -ENOSPC)
0684 goto out;
0685
0686
0687 if (ret == -ENOSPC) {
0688 ext4_journal_stop(handle);
0689 brelse(iloc.bh);
0690 goto convert;
0691 }
0692
0693 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
0694 EXT4_JTR_NONE);
0695 if (ret)
0696 goto out;
0697
0698 flags = memalloc_nofs_save();
0699 page = grab_cache_page_write_begin(mapping, 0);
0700 memalloc_nofs_restore(flags);
0701 if (!page) {
0702 ret = -ENOMEM;
0703 goto out;
0704 }
0705
0706 *pagep = page;
0707 down_read(&EXT4_I(inode)->xattr_sem);
0708 if (!ext4_has_inline_data(inode)) {
0709 ret = 0;
0710 unlock_page(page);
0711 put_page(page);
0712 goto out_up_read;
0713 }
0714
0715 if (!PageUptodate(page)) {
0716 ret = ext4_read_inline_page(inode, page);
0717 if (ret < 0) {
0718 unlock_page(page);
0719 put_page(page);
0720 goto out_up_read;
0721 }
0722 }
0723
0724 ret = 1;
0725 handle = NULL;
0726 out_up_read:
0727 up_read(&EXT4_I(inode)->xattr_sem);
0728 out:
0729 if (handle && (ret != 1))
0730 ext4_journal_stop(handle);
0731 brelse(iloc.bh);
0732 return ret;
0733 convert:
0734 return ext4_convert_inline_data_to_extent(mapping, inode);
0735 }
0736
0737 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
0738 unsigned copied, struct page *page)
0739 {
0740 handle_t *handle = ext4_journal_current_handle();
0741 int no_expand;
0742 void *kaddr;
0743 struct ext4_iloc iloc;
0744 int ret = 0, ret2;
0745
0746 if (unlikely(copied < len) && !PageUptodate(page))
0747 copied = 0;
0748
0749 if (likely(copied)) {
0750 ret = ext4_get_inode_loc(inode, &iloc);
0751 if (ret) {
0752 unlock_page(page);
0753 put_page(page);
0754 ext4_std_error(inode->i_sb, ret);
0755 goto out;
0756 }
0757 ext4_write_lock_xattr(inode, &no_expand);
0758 BUG_ON(!ext4_has_inline_data(inode));
0759
0760
0761
0762
0763
0764
0765 (void) ext4_find_inline_data_nolock(inode);
0766
0767 kaddr = kmap_atomic(page);
0768 ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
0769 kunmap_atomic(kaddr);
0770 SetPageUptodate(page);
0771
0772 ClearPageDirty(page);
0773
0774 ext4_write_unlock_xattr(inode, &no_expand);
0775 brelse(iloc.bh);
0776
0777
0778
0779
0780
0781
0782 ext4_update_inode_size(inode, pos + copied);
0783 }
0784 unlock_page(page);
0785 put_page(page);
0786
0787
0788
0789
0790
0791
0792
0793 if (likely(copied))
0794 mark_inode_dirty(inode);
0795 out:
0796
0797
0798
0799
0800 if (pos + len > inode->i_size && ext4_can_truncate(inode))
0801 ext4_orphan_add(handle, inode);
0802
0803 ret2 = ext4_journal_stop(handle);
0804 if (!ret)
0805 ret = ret2;
0806 if (pos + len > inode->i_size) {
0807 ext4_truncate_failed_write(inode);
0808
0809
0810
0811
0812
0813 if (inode->i_nlink)
0814 ext4_orphan_del(NULL, inode);
0815 }
0816 return ret ? ret : copied;
0817 }
0818
0819 struct buffer_head *
0820 ext4_journalled_write_inline_data(struct inode *inode,
0821 unsigned len,
0822 struct page *page)
0823 {
0824 int ret, no_expand;
0825 void *kaddr;
0826 struct ext4_iloc iloc;
0827
0828 ret = ext4_get_inode_loc(inode, &iloc);
0829 if (ret) {
0830 ext4_std_error(inode->i_sb, ret);
0831 return NULL;
0832 }
0833
0834 ext4_write_lock_xattr(inode, &no_expand);
0835 kaddr = kmap_atomic(page);
0836 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
0837 kunmap_atomic(kaddr);
0838 ext4_write_unlock_xattr(inode, &no_expand);
0839
0840 return iloc.bh;
0841 }
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
0853 struct inode *inode,
0854 void **fsdata)
0855 {
0856 int ret = 0, inline_size;
0857 struct page *page;
0858
0859 page = grab_cache_page_write_begin(mapping, 0);
0860 if (!page)
0861 return -ENOMEM;
0862
0863 down_read(&EXT4_I(inode)->xattr_sem);
0864 if (!ext4_has_inline_data(inode)) {
0865 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0866 goto out;
0867 }
0868
0869 inline_size = ext4_get_inline_size(inode);
0870
0871 if (!PageUptodate(page)) {
0872 ret = ext4_read_inline_page(inode, page);
0873 if (ret < 0)
0874 goto out;
0875 }
0876
0877 ret = __block_write_begin(page, 0, inline_size,
0878 ext4_da_get_block_prep);
0879 if (ret) {
0880 up_read(&EXT4_I(inode)->xattr_sem);
0881 unlock_page(page);
0882 put_page(page);
0883 ext4_truncate_failed_write(inode);
0884 return ret;
0885 }
0886
0887 SetPageDirty(page);
0888 SetPageUptodate(page);
0889 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
0890 *fsdata = (void *)CONVERT_INLINE_DATA;
0891
0892 out:
0893 up_read(&EXT4_I(inode)->xattr_sem);
0894 if (page) {
0895 unlock_page(page);
0896 put_page(page);
0897 }
0898 return ret;
0899 }
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909 int ext4_da_write_inline_data_begin(struct address_space *mapping,
0910 struct inode *inode,
0911 loff_t pos, unsigned len,
0912 struct page **pagep,
0913 void **fsdata)
0914 {
0915 int ret;
0916 handle_t *handle;
0917 struct page *page;
0918 struct ext4_iloc iloc;
0919 int retries = 0;
0920 unsigned int flags;
0921
0922 ret = ext4_get_inode_loc(inode, &iloc);
0923 if (ret)
0924 return ret;
0925
0926 retry_journal:
0927 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
0928 if (IS_ERR(handle)) {
0929 ret = PTR_ERR(handle);
0930 goto out;
0931 }
0932
0933 ret = ext4_prepare_inline_data(handle, inode, pos + len);
0934 if (ret && ret != -ENOSPC)
0935 goto out_journal;
0936
0937 if (ret == -ENOSPC) {
0938 ext4_journal_stop(handle);
0939 ret = ext4_da_convert_inline_data_to_extent(mapping,
0940 inode,
0941 fsdata);
0942 if (ret == -ENOSPC &&
0943 ext4_should_retry_alloc(inode->i_sb, &retries))
0944 goto retry_journal;
0945 goto out;
0946 }
0947
0948
0949
0950
0951
0952 flags = memalloc_nofs_save();
0953 page = grab_cache_page_write_begin(mapping, 0);
0954 memalloc_nofs_restore(flags);
0955 if (!page) {
0956 ret = -ENOMEM;
0957 goto out_journal;
0958 }
0959
0960 down_read(&EXT4_I(inode)->xattr_sem);
0961 if (!ext4_has_inline_data(inode)) {
0962 ret = 0;
0963 goto out_release_page;
0964 }
0965
0966 if (!PageUptodate(page)) {
0967 ret = ext4_read_inline_page(inode, page);
0968 if (ret < 0)
0969 goto out_release_page;
0970 }
0971 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
0972 EXT4_JTR_NONE);
0973 if (ret)
0974 goto out_release_page;
0975
0976 up_read(&EXT4_I(inode)->xattr_sem);
0977 *pagep = page;
0978 brelse(iloc.bh);
0979 return 1;
0980 out_release_page:
0981 up_read(&EXT4_I(inode)->xattr_sem);
0982 unlock_page(page);
0983 put_page(page);
0984 out_journal:
0985 ext4_journal_stop(handle);
0986 out:
0987 brelse(iloc.bh);
0988 return ret;
0989 }
0990
0991 #ifdef INLINE_DIR_DEBUG
0992 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
0993 void *inline_start, int inline_size)
0994 {
0995 int offset;
0996 unsigned short de_len;
0997 struct ext4_dir_entry_2 *de = inline_start;
0998 void *dlimit = inline_start + inline_size;
0999
1000 trace_printk("inode %lu\n", dir->i_ino);
1001 offset = 0;
1002 while ((void *)de < dlimit) {
1003 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1004 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1005 offset, de_len, de->name_len, de->name,
1006 de->name_len, le32_to_cpu(de->inode));
1007 if (ext4_check_dir_entry(dir, NULL, de, bh,
1008 inline_start, inline_size, offset))
1009 BUG();
1010
1011 offset += de_len;
1012 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1013 }
1014 }
1015 #else
1016 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1017 #endif
1018
1019
1020
1021
1022
1023
1024 static int ext4_add_dirent_to_inline(handle_t *handle,
1025 struct ext4_filename *fname,
1026 struct inode *dir,
1027 struct inode *inode,
1028 struct ext4_iloc *iloc,
1029 void *inline_start, int inline_size)
1030 {
1031 int err;
1032 struct ext4_dir_entry_2 *de;
1033
1034 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1035 inline_size, fname, &de);
1036 if (err)
1037 return err;
1038
1039 BUFFER_TRACE(iloc->bh, "get_write_access");
1040 err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1041 EXT4_JTR_NONE);
1042 if (err)
1043 return err;
1044 ext4_insert_dentry(dir, inode, de, inline_size, fname);
1045
1046 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 dir->i_mtime = dir->i_ctime = current_time(dir);
1060 ext4_update_dx_flag(dir);
1061 inode_inc_iversion(dir);
1062 return 1;
1063 }
1064
1065 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1066 struct ext4_iloc *iloc)
1067 {
1068 struct ext4_xattr_entry *entry;
1069 struct ext4_xattr_ibody_header *header;
1070
1071 BUG_ON(!EXT4_I(inode)->i_inline_off);
1072
1073 header = IHDR(inode, ext4_raw_inode(iloc));
1074 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1075 EXT4_I(inode)->i_inline_off);
1076
1077 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1078 }
1079
1080
1081 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1082 {
1083 struct ext4_dir_entry_2 *de, *prev_de;
1084 void *limit;
1085 int de_len;
1086
1087 de = de_buf;
1088 if (old_size) {
1089 limit = de_buf + old_size;
1090 do {
1091 prev_de = de;
1092 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1093 de_buf += de_len;
1094 de = de_buf;
1095 } while (de_buf < limit);
1096
1097 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1098 old_size, new_size);
1099 } else {
1100
1101 de->inode = 0;
1102 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1103 }
1104 }
1105
1106 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1107 struct ext4_iloc *iloc)
1108 {
1109 int ret;
1110 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1111 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1112
1113 if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
1114 return -ENOSPC;
1115
1116 ret = ext4_update_inline_data(handle, dir,
1117 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1118 if (ret)
1119 return ret;
1120
1121 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1122 EXT4_I(dir)->i_inline_size -
1123 EXT4_MIN_INLINE_DATA_SIZE);
1124 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1125 return 0;
1126 }
1127
1128 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1129 struct ext4_iloc *iloc,
1130 void *buf, int inline_size)
1131 {
1132 int ret;
1133
1134 ret = ext4_create_inline_data(handle, inode, inline_size);
1135 if (ret) {
1136 ext4_msg(inode->i_sb, KERN_EMERG,
1137 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1138 inode->i_ino, ret);
1139 return;
1140 }
1141 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1142 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1143 }
1144
1145 static int ext4_finish_convert_inline_dir(handle_t *handle,
1146 struct inode *inode,
1147 struct buffer_head *dir_block,
1148 void *buf,
1149 int inline_size)
1150 {
1151 int err, csum_size = 0, header_size = 0;
1152 struct ext4_dir_entry_2 *de;
1153 void *target = dir_block->b_data;
1154
1155
1156
1157
1158
1159 de = target;
1160 de = ext4_init_dot_dotdot(inode, de,
1161 inode->i_sb->s_blocksize, csum_size,
1162 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1163 header_size = (void *)de - target;
1164
1165 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1166 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1167
1168 if (ext4_has_metadata_csum(inode->i_sb))
1169 csum_size = sizeof(struct ext4_dir_entry_tail);
1170
1171 inode->i_size = inode->i_sb->s_blocksize;
1172 i_size_write(inode, inode->i_sb->s_blocksize);
1173 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1174 ext4_update_final_de(dir_block->b_data,
1175 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1176 inode->i_sb->s_blocksize - csum_size);
1177
1178 if (csum_size)
1179 ext4_initialize_dirent_tail(dir_block,
1180 inode->i_sb->s_blocksize);
1181 set_buffer_uptodate(dir_block);
1182 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1183 if (err)
1184 return err;
1185 set_buffer_verified(dir_block);
1186 return ext4_mark_inode_dirty(handle, inode);
1187 }
1188
1189 static int ext4_convert_inline_data_nolock(handle_t *handle,
1190 struct inode *inode,
1191 struct ext4_iloc *iloc)
1192 {
1193 int error;
1194 void *buf = NULL;
1195 struct buffer_head *data_bh = NULL;
1196 struct ext4_map_blocks map;
1197 int inline_size;
1198
1199 inline_size = ext4_get_inline_size(inode);
1200 buf = kmalloc(inline_size, GFP_NOFS);
1201 if (!buf) {
1202 error = -ENOMEM;
1203 goto out;
1204 }
1205
1206 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1207 if (error < 0)
1208 goto out;
1209
1210
1211
1212
1213
1214 if (S_ISDIR(inode->i_mode)) {
1215 error = ext4_check_all_de(inode, iloc->bh,
1216 buf + EXT4_INLINE_DOTDOT_SIZE,
1217 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1218 if (error)
1219 goto out;
1220 }
1221
1222 error = ext4_destroy_inline_data_nolock(handle, inode);
1223 if (error)
1224 goto out;
1225
1226 map.m_lblk = 0;
1227 map.m_len = 1;
1228 map.m_flags = 0;
1229 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1230 if (error < 0)
1231 goto out_restore;
1232 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1233 error = -EIO;
1234 goto out_restore;
1235 }
1236
1237 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1238 if (!data_bh) {
1239 error = -ENOMEM;
1240 goto out_restore;
1241 }
1242
1243 lock_buffer(data_bh);
1244 error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1245 EXT4_JTR_NONE);
1246 if (error) {
1247 unlock_buffer(data_bh);
1248 error = -EIO;
1249 goto out_restore;
1250 }
1251 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1252
1253 if (!S_ISDIR(inode->i_mode)) {
1254 memcpy(data_bh->b_data, buf, inline_size);
1255 set_buffer_uptodate(data_bh);
1256 error = ext4_handle_dirty_metadata(handle,
1257 inode, data_bh);
1258 } else {
1259 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1260 buf, inline_size);
1261 }
1262
1263 unlock_buffer(data_bh);
1264 out_restore:
1265 if (error)
1266 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1267
1268 out:
1269 brelse(data_bh);
1270 kfree(buf);
1271 return error;
1272 }
1273
1274
1275
1276
1277
1278
1279 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1280 struct inode *dir, struct inode *inode)
1281 {
1282 int ret, ret2, inline_size, no_expand;
1283 void *inline_start;
1284 struct ext4_iloc iloc;
1285
1286 ret = ext4_get_inode_loc(dir, &iloc);
1287 if (ret)
1288 return ret;
1289
1290 ext4_write_lock_xattr(dir, &no_expand);
1291 if (!ext4_has_inline_data(dir))
1292 goto out;
1293
1294 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1295 EXT4_INLINE_DOTDOT_SIZE;
1296 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1297
1298 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1299 inline_start, inline_size);
1300 if (ret != -ENOSPC)
1301 goto out;
1302
1303
1304 inline_size = EXT4_I(dir)->i_inline_size -
1305 EXT4_MIN_INLINE_DATA_SIZE;
1306 if (!inline_size) {
1307
1308 ret = ext4_update_inline_dir(handle, dir, &iloc);
1309 if (ret && ret != -ENOSPC)
1310 goto out;
1311
1312 inline_size = EXT4_I(dir)->i_inline_size -
1313 EXT4_MIN_INLINE_DATA_SIZE;
1314 }
1315
1316 if (inline_size) {
1317 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1318
1319 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1320 inode, &iloc, inline_start,
1321 inline_size);
1322
1323 if (ret != -ENOSPC)
1324 goto out;
1325 }
1326
1327
1328
1329
1330
1331
1332 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1333
1334 out:
1335 ext4_write_unlock_xattr(dir, &no_expand);
1336 ret2 = ext4_mark_inode_dirty(handle, dir);
1337 if (unlikely(ret2 && !ret))
1338 ret = ret2;
1339 brelse(iloc.bh);
1340 return ret;
1341 }
1342
1343
1344
1345
1346
1347
1348 int ext4_inlinedir_to_tree(struct file *dir_file,
1349 struct inode *dir, ext4_lblk_t block,
1350 struct dx_hash_info *hinfo,
1351 __u32 start_hash, __u32 start_minor_hash,
1352 int *has_inline_data)
1353 {
1354 int err = 0, count = 0;
1355 unsigned int parent_ino;
1356 int pos;
1357 struct ext4_dir_entry_2 *de;
1358 struct inode *inode = file_inode(dir_file);
1359 int ret, inline_size = 0;
1360 struct ext4_iloc iloc;
1361 void *dir_buf = NULL;
1362 struct ext4_dir_entry_2 fake;
1363 struct fscrypt_str tmp_str;
1364
1365 ret = ext4_get_inode_loc(inode, &iloc);
1366 if (ret)
1367 return ret;
1368
1369 down_read(&EXT4_I(inode)->xattr_sem);
1370 if (!ext4_has_inline_data(inode)) {
1371 up_read(&EXT4_I(inode)->xattr_sem);
1372 *has_inline_data = 0;
1373 goto out;
1374 }
1375
1376 inline_size = ext4_get_inline_size(inode);
1377 dir_buf = kmalloc(inline_size, GFP_NOFS);
1378 if (!dir_buf) {
1379 ret = -ENOMEM;
1380 up_read(&EXT4_I(inode)->xattr_sem);
1381 goto out;
1382 }
1383
1384 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1385 up_read(&EXT4_I(inode)->xattr_sem);
1386 if (ret < 0)
1387 goto out;
1388
1389 pos = 0;
1390 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1391 while (pos < inline_size) {
1392
1393
1394
1395
1396
1397 if (pos == 0) {
1398 fake.inode = cpu_to_le32(inode->i_ino);
1399 fake.name_len = 1;
1400 strcpy(fake.name, ".");
1401 fake.rec_len = ext4_rec_len_to_disk(
1402 ext4_dir_rec_len(fake.name_len, NULL),
1403 inline_size);
1404 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1405 de = &fake;
1406 pos = EXT4_INLINE_DOTDOT_OFFSET;
1407 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1408 fake.inode = cpu_to_le32(parent_ino);
1409 fake.name_len = 2;
1410 strcpy(fake.name, "..");
1411 fake.rec_len = ext4_rec_len_to_disk(
1412 ext4_dir_rec_len(fake.name_len, NULL),
1413 inline_size);
1414 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1415 de = &fake;
1416 pos = EXT4_INLINE_DOTDOT_SIZE;
1417 } else {
1418 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1419 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1420 if (ext4_check_dir_entry(inode, dir_file, de,
1421 iloc.bh, dir_buf,
1422 inline_size, pos)) {
1423 ret = count;
1424 goto out;
1425 }
1426 }
1427
1428 if (ext4_hash_in_dirent(dir)) {
1429 hinfo->hash = EXT4_DIRENT_HASH(de);
1430 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1431 } else {
1432 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1433 }
1434 if ((hinfo->hash < start_hash) ||
1435 ((hinfo->hash == start_hash) &&
1436 (hinfo->minor_hash < start_minor_hash)))
1437 continue;
1438 if (de->inode == 0)
1439 continue;
1440 tmp_str.name = de->name;
1441 tmp_str.len = de->name_len;
1442 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1443 hinfo->minor_hash, de, &tmp_str);
1444 if (err) {
1445 ret = err;
1446 goto out;
1447 }
1448 count++;
1449 }
1450 ret = count;
1451 out:
1452 kfree(dir_buf);
1453 brelse(iloc.bh);
1454 return ret;
1455 }
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465 int ext4_read_inline_dir(struct file *file,
1466 struct dir_context *ctx,
1467 int *has_inline_data)
1468 {
1469 unsigned int offset, parent_ino;
1470 int i;
1471 struct ext4_dir_entry_2 *de;
1472 struct super_block *sb;
1473 struct inode *inode = file_inode(file);
1474 int ret, inline_size = 0;
1475 struct ext4_iloc iloc;
1476 void *dir_buf = NULL;
1477 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1478
1479 ret = ext4_get_inode_loc(inode, &iloc);
1480 if (ret)
1481 return ret;
1482
1483 down_read(&EXT4_I(inode)->xattr_sem);
1484 if (!ext4_has_inline_data(inode)) {
1485 up_read(&EXT4_I(inode)->xattr_sem);
1486 *has_inline_data = 0;
1487 goto out;
1488 }
1489
1490 inline_size = ext4_get_inline_size(inode);
1491 dir_buf = kmalloc(inline_size, GFP_NOFS);
1492 if (!dir_buf) {
1493 ret = -ENOMEM;
1494 up_read(&EXT4_I(inode)->xattr_sem);
1495 goto out;
1496 }
1497
1498 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1499 up_read(&EXT4_I(inode)->xattr_sem);
1500 if (ret < 0)
1501 goto out;
1502
1503 ret = 0;
1504 sb = inode->i_sb;
1505 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1506 offset = ctx->pos;
1507
1508
1509
1510
1511
1512
1513
1514
1515 dotdot_offset = ext4_dir_rec_len(1, NULL);
1516 dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1517 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1518 extra_size = extra_offset + inline_size;
1519
1520
1521
1522
1523
1524
1525
1526 if (!inode_eq_iversion(inode, file->f_version)) {
1527 for (i = 0; i < extra_size && i < offset;) {
1528
1529
1530
1531
1532 if (!i) {
1533 i = dotdot_offset;
1534 continue;
1535 } else if (i == dotdot_offset) {
1536 i = dotdot_size;
1537 continue;
1538 }
1539
1540
1541
1542 de = (struct ext4_dir_entry_2 *)
1543 (dir_buf + i - extra_offset);
1544
1545
1546
1547
1548
1549
1550 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1551 < ext4_dir_rec_len(1, NULL))
1552 break;
1553 i += ext4_rec_len_from_disk(de->rec_len,
1554 extra_size);
1555 }
1556 offset = i;
1557 ctx->pos = offset;
1558 file->f_version = inode_query_iversion(inode);
1559 }
1560
1561 while (ctx->pos < extra_size) {
1562 if (ctx->pos == 0) {
1563 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1564 goto out;
1565 ctx->pos = dotdot_offset;
1566 continue;
1567 }
1568
1569 if (ctx->pos == dotdot_offset) {
1570 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1571 goto out;
1572 ctx->pos = dotdot_size;
1573 continue;
1574 }
1575
1576 de = (struct ext4_dir_entry_2 *)
1577 (dir_buf + ctx->pos - extra_offset);
1578 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1579 extra_size, ctx->pos))
1580 goto out;
1581 if (le32_to_cpu(de->inode)) {
1582 if (!dir_emit(ctx, de->name, de->name_len,
1583 le32_to_cpu(de->inode),
1584 get_dtype(sb, de->file_type)))
1585 goto out;
1586 }
1587 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1588 }
1589 out:
1590 kfree(dir_buf);
1591 brelse(iloc.bh);
1592 return ret;
1593 }
1594
1595 void *ext4_read_inline_link(struct inode *inode)
1596 {
1597 struct ext4_iloc iloc;
1598 int ret, inline_size;
1599 void *link;
1600
1601 ret = ext4_get_inode_loc(inode, &iloc);
1602 if (ret)
1603 return ERR_PTR(ret);
1604
1605 ret = -ENOMEM;
1606 inline_size = ext4_get_inline_size(inode);
1607 link = kmalloc(inline_size + 1, GFP_NOFS);
1608 if (!link)
1609 goto out;
1610
1611 ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
1612 if (ret < 0) {
1613 kfree(link);
1614 goto out;
1615 }
1616 nd_terminate_link(link, inode->i_size, ret);
1617 out:
1618 if (ret < 0)
1619 link = ERR_PTR(ret);
1620 brelse(iloc.bh);
1621 return link;
1622 }
1623
1624 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1625 struct ext4_dir_entry_2 **parent_de,
1626 int *retval)
1627 {
1628 struct ext4_iloc iloc;
1629
1630 *retval = ext4_get_inode_loc(inode, &iloc);
1631 if (*retval)
1632 return NULL;
1633
1634 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1635
1636 return iloc.bh;
1637 }
1638
1639
1640
1641
1642
1643
1644 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1645 struct inode *inode)
1646 {
1647 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1648 struct ext4_iloc iloc;
1649 struct ext4_dir_entry_2 *de;
1650
1651 ret = ext4_get_inode_loc(inode, &iloc);
1652 if (ret)
1653 return ret;
1654
1655 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1656 if (ret)
1657 goto out;
1658
1659
1660
1661
1662
1663 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1664 de->inode = cpu_to_le32(parent->i_ino);
1665 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1666 de->inode = 0;
1667 de->rec_len = ext4_rec_len_to_disk(
1668 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1669 inline_size);
1670 set_nlink(inode, 2);
1671 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1672 out:
1673 brelse(iloc.bh);
1674 return ret;
1675 }
1676
1677 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1678 struct ext4_filename *fname,
1679 struct ext4_dir_entry_2 **res_dir,
1680 int *has_inline_data)
1681 {
1682 int ret;
1683 struct ext4_iloc iloc;
1684 void *inline_start;
1685 int inline_size;
1686
1687 if (ext4_get_inode_loc(dir, &iloc))
1688 return NULL;
1689
1690 down_read(&EXT4_I(dir)->xattr_sem);
1691 if (!ext4_has_inline_data(dir)) {
1692 *has_inline_data = 0;
1693 goto out;
1694 }
1695
1696 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1697 EXT4_INLINE_DOTDOT_SIZE;
1698 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1699 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1700 dir, fname, 0, res_dir);
1701 if (ret == 1)
1702 goto out_find;
1703 if (ret < 0)
1704 goto out;
1705
1706 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1707 goto out;
1708
1709 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1710 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1711
1712 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1713 dir, fname, 0, res_dir);
1714 if (ret == 1)
1715 goto out_find;
1716
1717 out:
1718 brelse(iloc.bh);
1719 iloc.bh = NULL;
1720 out_find:
1721 up_read(&EXT4_I(dir)->xattr_sem);
1722 return iloc.bh;
1723 }
1724
1725 int ext4_delete_inline_entry(handle_t *handle,
1726 struct inode *dir,
1727 struct ext4_dir_entry_2 *de_del,
1728 struct buffer_head *bh,
1729 int *has_inline_data)
1730 {
1731 int err, inline_size, no_expand;
1732 struct ext4_iloc iloc;
1733 void *inline_start;
1734
1735 err = ext4_get_inode_loc(dir, &iloc);
1736 if (err)
1737 return err;
1738
1739 ext4_write_lock_xattr(dir, &no_expand);
1740 if (!ext4_has_inline_data(dir)) {
1741 *has_inline_data = 0;
1742 goto out;
1743 }
1744
1745 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1746 EXT4_MIN_INLINE_DATA_SIZE) {
1747 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1748 EXT4_INLINE_DOTDOT_SIZE;
1749 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1750 EXT4_INLINE_DOTDOT_SIZE;
1751 } else {
1752 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1753 inline_size = ext4_get_inline_size(dir) -
1754 EXT4_MIN_INLINE_DATA_SIZE;
1755 }
1756
1757 BUFFER_TRACE(bh, "get_write_access");
1758 err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1759 EXT4_JTR_NONE);
1760 if (err)
1761 goto out;
1762
1763 err = ext4_generic_delete_entry(dir, de_del, bh,
1764 inline_start, inline_size, 0);
1765 if (err)
1766 goto out;
1767
1768 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1769 out:
1770 ext4_write_unlock_xattr(dir, &no_expand);
1771 if (likely(err == 0))
1772 err = ext4_mark_inode_dirty(handle, dir);
1773 brelse(iloc.bh);
1774 if (err != -ENOENT)
1775 ext4_std_error(dir->i_sb, err);
1776 return err;
1777 }
1778
1779
1780
1781
1782 static inline struct ext4_dir_entry_2 *
1783 ext4_get_inline_entry(struct inode *inode,
1784 struct ext4_iloc *iloc,
1785 unsigned int offset,
1786 void **inline_start,
1787 int *inline_size)
1788 {
1789 void *inline_pos;
1790
1791 BUG_ON(offset > ext4_get_inline_size(inode));
1792
1793 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1794 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1795 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1796 } else {
1797 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1798 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1799 *inline_size = ext4_get_inline_size(inode) -
1800 EXT4_MIN_INLINE_DATA_SIZE;
1801 }
1802
1803 if (inline_start)
1804 *inline_start = inline_pos;
1805 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1806 }
1807
1808 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1809 {
1810 int err, inline_size;
1811 struct ext4_iloc iloc;
1812 size_t inline_len;
1813 void *inline_pos;
1814 unsigned int offset;
1815 struct ext4_dir_entry_2 *de;
1816 bool ret = false;
1817
1818 err = ext4_get_inode_loc(dir, &iloc);
1819 if (err) {
1820 EXT4_ERROR_INODE_ERR(dir, -err,
1821 "error %d getting inode %lu block",
1822 err, dir->i_ino);
1823 return false;
1824 }
1825
1826 down_read(&EXT4_I(dir)->xattr_sem);
1827 if (!ext4_has_inline_data(dir)) {
1828 *has_inline_data = 0;
1829 ret = true;
1830 goto out;
1831 }
1832
1833 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1834 if (!le32_to_cpu(de->inode)) {
1835 ext4_warning(dir->i_sb,
1836 "bad inline directory (dir #%lu) - no `..'",
1837 dir->i_ino);
1838 goto out;
1839 }
1840
1841 inline_len = ext4_get_inline_size(dir);
1842 offset = EXT4_INLINE_DOTDOT_SIZE;
1843 while (offset < inline_len) {
1844 de = ext4_get_inline_entry(dir, &iloc, offset,
1845 &inline_pos, &inline_size);
1846 if (ext4_check_dir_entry(dir, NULL, de,
1847 iloc.bh, inline_pos,
1848 inline_size, offset)) {
1849 ext4_warning(dir->i_sb,
1850 "bad inline directory (dir #%lu) - "
1851 "inode %u, rec_len %u, name_len %d"
1852 "inline size %d",
1853 dir->i_ino, le32_to_cpu(de->inode),
1854 le16_to_cpu(de->rec_len), de->name_len,
1855 inline_size);
1856 goto out;
1857 }
1858 if (le32_to_cpu(de->inode)) {
1859 goto out;
1860 }
1861 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1862 }
1863
1864 ret = true;
1865 out:
1866 up_read(&EXT4_I(dir)->xattr_sem);
1867 brelse(iloc.bh);
1868 return ret;
1869 }
1870
1871 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1872 {
1873 int ret, no_expand;
1874
1875 ext4_write_lock_xattr(inode, &no_expand);
1876 ret = ext4_destroy_inline_data_nolock(handle, inode);
1877 ext4_write_unlock_xattr(inode, &no_expand);
1878
1879 return ret;
1880 }
1881
1882 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1883 {
1884 __u64 addr;
1885 int error = -EAGAIN;
1886 struct ext4_iloc iloc;
1887
1888 down_read(&EXT4_I(inode)->xattr_sem);
1889 if (!ext4_has_inline_data(inode))
1890 goto out;
1891
1892 error = ext4_get_inode_loc(inode, &iloc);
1893 if (error)
1894 goto out;
1895
1896 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1897 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1898 addr += offsetof(struct ext4_inode, i_block);
1899
1900 brelse(iloc.bh);
1901
1902 iomap->addr = addr;
1903 iomap->offset = 0;
1904 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1905 i_size_read(inode));
1906 iomap->type = IOMAP_INLINE;
1907 iomap->flags = 0;
1908
1909 out:
1910 up_read(&EXT4_I(inode)->xattr_sem);
1911 return error;
1912 }
1913
1914 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1915 {
1916 handle_t *handle;
1917 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1918 size_t i_size;
1919 void *value = NULL;
1920 struct ext4_xattr_ibody_find is = {
1921 .s = { .not_found = -ENODATA, },
1922 };
1923 struct ext4_xattr_info i = {
1924 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1925 .name = EXT4_XATTR_SYSTEM_DATA,
1926 };
1927
1928
1929 needed_blocks = ext4_writepage_trans_blocks(inode);
1930 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1931 if (IS_ERR(handle))
1932 return PTR_ERR(handle);
1933
1934 ext4_write_lock_xattr(inode, &no_expand);
1935 if (!ext4_has_inline_data(inode)) {
1936 ext4_write_unlock_xattr(inode, &no_expand);
1937 *has_inline = 0;
1938 ext4_journal_stop(handle);
1939 return 0;
1940 }
1941
1942 if ((err = ext4_orphan_add(handle, inode)) != 0)
1943 goto out;
1944
1945 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1946 goto out;
1947
1948 down_write(&EXT4_I(inode)->i_data_sem);
1949 i_size = inode->i_size;
1950 inline_size = ext4_get_inline_size(inode);
1951 EXT4_I(inode)->i_disksize = i_size;
1952
1953 if (i_size < inline_size) {
1954
1955
1956
1957
1958
1959
1960 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1961 retry:
1962 err = ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1963 if (err == -ENOMEM) {
1964 memalloc_retry_wait(GFP_ATOMIC);
1965 goto retry;
1966 }
1967 if (err)
1968 goto out_error;
1969 }
1970
1971
1972 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1973 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1974 goto out_error;
1975
1976 BUG_ON(is.s.not_found);
1977
1978 value_len = le32_to_cpu(is.s.here->e_value_size);
1979 value = kmalloc(value_len, GFP_NOFS);
1980 if (!value) {
1981 err = -ENOMEM;
1982 goto out_error;
1983 }
1984
1985 err = ext4_xattr_ibody_get(inode, i.name_index,
1986 i.name, value, value_len);
1987 if (err <= 0)
1988 goto out_error;
1989
1990 i.value = value;
1991 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1992 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1993 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1994 if (err)
1995 goto out_error;
1996 }
1997
1998
1999 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
2000 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
2001 memset(p + i_size, 0,
2002 EXT4_MIN_INLINE_DATA_SIZE - i_size);
2003 }
2004
2005 EXT4_I(inode)->i_inline_size = i_size <
2006 EXT4_MIN_INLINE_DATA_SIZE ?
2007 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2008 }
2009
2010 out_error:
2011 up_write(&EXT4_I(inode)->i_data_sem);
2012 out:
2013 brelse(is.iloc.bh);
2014 ext4_write_unlock_xattr(inode, &no_expand);
2015 kfree(value);
2016 if (inode->i_nlink)
2017 ext4_orphan_del(handle, inode);
2018
2019 if (err == 0) {
2020 inode->i_mtime = inode->i_ctime = current_time(inode);
2021 err = ext4_mark_inode_dirty(handle, inode);
2022 if (IS_SYNC(inode))
2023 ext4_handle_sync(handle);
2024 }
2025 ext4_journal_stop(handle);
2026 return err;
2027 }
2028
2029 int ext4_convert_inline_data(struct inode *inode)
2030 {
2031 int error, needed_blocks, no_expand;
2032 handle_t *handle;
2033 struct ext4_iloc iloc;
2034
2035 if (!ext4_has_inline_data(inode)) {
2036 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2037 return 0;
2038 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2039
2040
2041
2042
2043
2044
2045 error = filemap_flush(inode->i_mapping);
2046 if (error)
2047 return error;
2048 if (!ext4_has_inline_data(inode))
2049 return 0;
2050 }
2051
2052 needed_blocks = ext4_writepage_trans_blocks(inode);
2053
2054 iloc.bh = NULL;
2055 error = ext4_get_inode_loc(inode, &iloc);
2056 if (error)
2057 return error;
2058
2059 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2060 if (IS_ERR(handle)) {
2061 error = PTR_ERR(handle);
2062 goto out_free;
2063 }
2064
2065 ext4_write_lock_xattr(inode, &no_expand);
2066 if (ext4_has_inline_data(inode))
2067 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2068 ext4_write_unlock_xattr(inode, &no_expand);
2069 ext4_journal_stop(handle);
2070 out_free:
2071 brelse(iloc.bh);
2072 return error;
2073 }