1 /*
2 * Copyright (c) 2012 Taobao.
3 * Written by Tao Ma <boyu.mt@taobao.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <linux/fiemap.h>
16
17 #include "ext4_jbd2.h"
18 #include "ext4.h"
19 #include "xattr.h"
20 #include "truncate.h"
21 #include <trace/events/android_fs.h>
22
23 #define EXT4_XATTR_SYSTEM_DATA "data"
24 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
25 #define EXT4_INLINE_DOTDOT_OFFSET 2
26 #define EXT4_INLINE_DOTDOT_SIZE 4
27
ext4_get_inline_size(struct inode * inode)28 static int ext4_get_inline_size(struct inode *inode)
29 {
30 if (EXT4_I(inode)->i_inline_off)
31 return EXT4_I(inode)->i_inline_size;
32
33 return 0;
34 }
35
get_max_inline_xattr_value_size(struct inode * inode,struct ext4_iloc * iloc)36 static int get_max_inline_xattr_value_size(struct inode *inode,
37 struct ext4_iloc *iloc)
38 {
39 struct ext4_xattr_ibody_header *header;
40 struct ext4_xattr_entry *entry;
41 struct ext4_inode *raw_inode;
42 int free, min_offs;
43
44 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
45 EXT4_GOOD_OLD_INODE_SIZE -
46 EXT4_I(inode)->i_extra_isize -
47 sizeof(struct ext4_xattr_ibody_header);
48
49 /*
50 * We need to subtract another sizeof(__u32) since an in-inode xattr
51 * needs an empty 4 bytes to indicate the gap between the xattr entry
52 * and the name/value pair.
53 */
54 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
55 return EXT4_XATTR_SIZE(min_offs -
56 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
57 EXT4_XATTR_ROUND - sizeof(__u32));
58
59 raw_inode = ext4_raw_inode(iloc);
60 header = IHDR(inode, raw_inode);
61 entry = IFIRST(header);
62
63 /* Compute min_offs. */
64 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
65 if (!entry->e_value_block && entry->e_value_size) {
66 size_t offs = le16_to_cpu(entry->e_value_offs);
67 if (offs < min_offs)
68 min_offs = offs;
69 }
70 }
71 free = min_offs -
72 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
73
74 if (EXT4_I(inode)->i_inline_off) {
75 entry = (struct ext4_xattr_entry *)
76 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
77
78 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
79 goto out;
80 }
81
82 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
83
84 if (free > EXT4_XATTR_ROUND)
85 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
86 else
87 free = 0;
88
89 out:
90 return free;
91 }
92
93 /*
94 * Get the maximum size we now can store in an inode.
95 * If we can't find the space for a xattr entry, don't use the space
96 * of the extents since we have no space to indicate the inline data.
97 */
ext4_get_max_inline_size(struct inode * inode)98 int ext4_get_max_inline_size(struct inode *inode)
99 {
100 int error, max_inline_size;
101 struct ext4_iloc iloc;
102
103 if (EXT4_I(inode)->i_extra_isize == 0)
104 return 0;
105
106 error = ext4_get_inode_loc(inode, &iloc);
107 if (error) {
108 ext4_error_inode(inode, __func__, __LINE__, 0,
109 "can't get inode location %lu",
110 inode->i_ino);
111 return 0;
112 }
113
114 down_read(&EXT4_I(inode)->xattr_sem);
115 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
116 up_read(&EXT4_I(inode)->xattr_sem);
117
118 brelse(iloc.bh);
119
120 if (!max_inline_size)
121 return 0;
122
123 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
124 }
125
126 /*
127 * this function does not take xattr_sem, which is OK because it is
128 * currently only used in a code path coming form ext4_iget, before
129 * the new inode has been unlocked
130 */
ext4_find_inline_data_nolock(struct inode * inode)131 int ext4_find_inline_data_nolock(struct inode *inode)
132 {
133 struct ext4_xattr_ibody_find is = {
134 .s = { .not_found = -ENODATA, },
135 };
136 struct ext4_xattr_info i = {
137 .name_index = EXT4_XATTR_INDEX_SYSTEM,
138 .name = EXT4_XATTR_SYSTEM_DATA,
139 };
140 int error;
141
142 if (EXT4_I(inode)->i_extra_isize == 0)
143 return 0;
144
145 error = ext4_get_inode_loc(inode, &is.iloc);
146 if (error)
147 return error;
148
149 error = ext4_xattr_ibody_find(inode, &i, &is);
150 if (error)
151 goto out;
152
153 if (!is.s.not_found) {
154 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
155 (void *)ext4_raw_inode(&is.iloc));
156 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
157 le32_to_cpu(is.s.here->e_value_size);
158 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
159 }
160 out:
161 brelse(is.iloc.bh);
162 return error;
163 }
164
ext4_read_inline_data(struct inode * inode,void * buffer,unsigned int len,struct ext4_iloc * iloc)165 static int ext4_read_inline_data(struct inode *inode, void *buffer,
166 unsigned int len,
167 struct ext4_iloc *iloc)
168 {
169 struct ext4_xattr_entry *entry;
170 struct ext4_xattr_ibody_header *header;
171 int cp_len = 0;
172 struct ext4_inode *raw_inode;
173
174 if (!len)
175 return 0;
176
177 BUG_ON(len > EXT4_I(inode)->i_inline_size);
178
179 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
180 len : EXT4_MIN_INLINE_DATA_SIZE;
181
182 raw_inode = ext4_raw_inode(iloc);
183 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
184
185 len -= cp_len;
186 buffer += cp_len;
187
188 if (!len)
189 goto out;
190
191 header = IHDR(inode, raw_inode);
192 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
193 EXT4_I(inode)->i_inline_off);
194 len = min_t(unsigned int, len,
195 (unsigned int)le32_to_cpu(entry->e_value_size));
196
197 memcpy(buffer,
198 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
199 cp_len += len;
200
201 out:
202 return cp_len;
203 }
204
205 /*
206 * write the buffer to the inline inode.
207 * If 'create' is set, we don't need to do the extra copy in the xattr
208 * value since it is already handled by ext4_xattr_ibody_inline_set.
209 * That saves us one memcpy.
210 */
ext4_write_inline_data(struct inode * inode,struct ext4_iloc * iloc,void * buffer,loff_t pos,unsigned int len)211 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
212 void *buffer, loff_t pos, unsigned int len)
213 {
214 struct ext4_xattr_entry *entry;
215 struct ext4_xattr_ibody_header *header;
216 struct ext4_inode *raw_inode;
217 int cp_len = 0;
218
219 BUG_ON(!EXT4_I(inode)->i_inline_off);
220 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
221
222 raw_inode = ext4_raw_inode(iloc);
223 buffer += pos;
224
225 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
226 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
227 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
228 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
229
230 len -= cp_len;
231 buffer += cp_len;
232 pos += cp_len;
233 }
234
235 if (!len)
236 return;
237
238 pos -= EXT4_MIN_INLINE_DATA_SIZE;
239 header = IHDR(inode, raw_inode);
240 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
241 EXT4_I(inode)->i_inline_off);
242
243 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
244 buffer, len);
245 }
246
ext4_create_inline_data(handle_t * handle,struct inode * inode,unsigned len)247 static int ext4_create_inline_data(handle_t *handle,
248 struct inode *inode, unsigned len)
249 {
250 int error;
251 void *value = NULL;
252 struct ext4_xattr_ibody_find is = {
253 .s = { .not_found = -ENODATA, },
254 };
255 struct ext4_xattr_info i = {
256 .name_index = EXT4_XATTR_INDEX_SYSTEM,
257 .name = EXT4_XATTR_SYSTEM_DATA,
258 };
259
260 error = ext4_get_inode_loc(inode, &is.iloc);
261 if (error)
262 return error;
263
264 BUFFER_TRACE(is.iloc.bh, "get_write_access");
265 error = ext4_journal_get_write_access(handle, is.iloc.bh);
266 if (error)
267 goto out;
268
269 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
270 value = EXT4_ZERO_XATTR_VALUE;
271 len -= EXT4_MIN_INLINE_DATA_SIZE;
272 } else {
273 value = "";
274 len = 0;
275 }
276
277 /* Insert the the xttr entry. */
278 i.value = value;
279 i.value_len = len;
280
281 error = ext4_xattr_ibody_find(inode, &i, &is);
282 if (error)
283 goto out;
284
285 BUG_ON(!is.s.not_found);
286
287 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
288 if (error) {
289 if (error == -ENOSPC)
290 ext4_clear_inode_state(inode,
291 EXT4_STATE_MAY_INLINE_DATA);
292 goto out;
293 }
294
295 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
296 0, EXT4_MIN_INLINE_DATA_SIZE);
297
298 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
299 (void *)ext4_raw_inode(&is.iloc));
300 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
301 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
302 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
303 get_bh(is.iloc.bh);
304 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
305
306 out:
307 brelse(is.iloc.bh);
308 return error;
309 }
310
ext4_update_inline_data(handle_t * handle,struct inode * inode,unsigned int len)311 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
312 unsigned int len)
313 {
314 int error;
315 void *value = NULL;
316 struct ext4_xattr_ibody_find is = {
317 .s = { .not_found = -ENODATA, },
318 };
319 struct ext4_xattr_info i = {
320 .name_index = EXT4_XATTR_INDEX_SYSTEM,
321 .name = EXT4_XATTR_SYSTEM_DATA,
322 };
323
324 /* If the old space is ok, write the data directly. */
325 if (len <= EXT4_I(inode)->i_inline_size)
326 return 0;
327
328 error = ext4_get_inode_loc(inode, &is.iloc);
329 if (error)
330 return error;
331
332 error = ext4_xattr_ibody_find(inode, &i, &is);
333 if (error)
334 goto out;
335
336 BUG_ON(is.s.not_found);
337
338 len -= EXT4_MIN_INLINE_DATA_SIZE;
339 value = kzalloc(len, GFP_NOFS);
340 if (!value) {
341 error = -ENOMEM;
342 goto out;
343 }
344
345 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
346 value, len);
347 if (error == -ENODATA)
348 goto out;
349
350 BUFFER_TRACE(is.iloc.bh, "get_write_access");
351 error = ext4_journal_get_write_access(handle, is.iloc.bh);
352 if (error)
353 goto out;
354
355 /* Update the xttr entry. */
356 i.value = value;
357 i.value_len = len;
358
359 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
360 if (error)
361 goto out;
362
363 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
364 (void *)ext4_raw_inode(&is.iloc));
365 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
366 le32_to_cpu(is.s.here->e_value_size);
367 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
368 get_bh(is.iloc.bh);
369 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
370
371 out:
372 kfree(value);
373 brelse(is.iloc.bh);
374 return error;
375 }
376
ext4_prepare_inline_data(handle_t * handle,struct inode * inode,unsigned int len)377 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
378 unsigned int len)
379 {
380 int ret, size, no_expand;
381 struct ext4_inode_info *ei = EXT4_I(inode);
382
383 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
384 return -ENOSPC;
385
386 size = ext4_get_max_inline_size(inode);
387 if (size < len)
388 return -ENOSPC;
389
390 ext4_write_lock_xattr(inode, &no_expand);
391
392 if (ei->i_inline_off)
393 ret = ext4_update_inline_data(handle, inode, len);
394 else
395 ret = ext4_create_inline_data(handle, inode, len);
396
397 ext4_write_unlock_xattr(inode, &no_expand);
398 return ret;
399 }
400
ext4_destroy_inline_data_nolock(handle_t * handle,struct inode * inode)401 static int ext4_destroy_inline_data_nolock(handle_t *handle,
402 struct inode *inode)
403 {
404 struct ext4_inode_info *ei = EXT4_I(inode);
405 struct ext4_xattr_ibody_find is = {
406 .s = { .not_found = 0, },
407 };
408 struct ext4_xattr_info i = {
409 .name_index = EXT4_XATTR_INDEX_SYSTEM,
410 .name = EXT4_XATTR_SYSTEM_DATA,
411 .value = NULL,
412 .value_len = 0,
413 };
414 int error;
415
416 if (!ei->i_inline_off)
417 return 0;
418
419 error = ext4_get_inode_loc(inode, &is.iloc);
420 if (error)
421 return error;
422
423 error = ext4_xattr_ibody_find(inode, &i, &is);
424 if (error)
425 goto out;
426
427 BUFFER_TRACE(is.iloc.bh, "get_write_access");
428 error = ext4_journal_get_write_access(handle, is.iloc.bh);
429 if (error)
430 goto out;
431
432 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
433 if (error)
434 goto out;
435
436 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
437 0, EXT4_MIN_INLINE_DATA_SIZE);
438
439 if (ext4_has_feature_extents(inode->i_sb)) {
440 if (S_ISDIR(inode->i_mode) ||
441 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
442 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
443 ext4_ext_tree_init(handle, inode);
444 }
445 }
446 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
447
448 get_bh(is.iloc.bh);
449 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
450
451 EXT4_I(inode)->i_inline_off = 0;
452 EXT4_I(inode)->i_inline_size = 0;
453 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
454 out:
455 brelse(is.iloc.bh);
456 if (error == -ENODATA)
457 error = 0;
458 return error;
459 }
460
ext4_read_inline_page(struct inode * inode,struct page * page)461 static int ext4_read_inline_page(struct inode *inode, struct page *page)
462 {
463 void *kaddr;
464 int ret = 0;
465 size_t len;
466 struct ext4_iloc iloc;
467
468 BUG_ON(!PageLocked(page));
469 BUG_ON(!ext4_has_inline_data(inode));
470 BUG_ON(page->index);
471
472 if (!EXT4_I(inode)->i_inline_off) {
473 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
474 inode->i_ino);
475 goto out;
476 }
477
478 ret = ext4_get_inode_loc(inode, &iloc);
479 if (ret)
480 goto out;
481
482 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
483 kaddr = kmap_atomic(page);
484 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
485 flush_dcache_page(page);
486 kunmap_atomic(kaddr);
487 zero_user_segment(page, len, PAGE_SIZE);
488 SetPageUptodate(page);
489 brelse(iloc.bh);
490
491 out:
492 return ret;
493 }
494
ext4_readpage_inline(struct inode * inode,struct page * page)495 int ext4_readpage_inline(struct inode *inode, struct page *page)
496 {
497 int ret = 0;
498
499 down_read(&EXT4_I(inode)->xattr_sem);
500 if (!ext4_has_inline_data(inode)) {
501 up_read(&EXT4_I(inode)->xattr_sem);
502 return -EAGAIN;
503 }
504
505 if (trace_android_fs_dataread_start_enabled()) {
506 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
507
508 path = android_fstrace_get_pathname(pathbuf,
509 MAX_TRACE_PATHBUF_LEN,
510 inode);
511 trace_android_fs_dataread_start(inode, page_offset(page),
512 PAGE_SIZE, current->pid,
513 path, current->comm);
514 }
515
516 /*
517 * Current inline data can only exist in the 1st page,
518 * So for all the other pages, just set them uptodate.
519 */
520 if (!page->index)
521 ret = ext4_read_inline_page(inode, page);
522 else if (!PageUptodate(page)) {
523 zero_user_segment(page, 0, PAGE_SIZE);
524 SetPageUptodate(page);
525 }
526
527 trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE);
528
529 up_read(&EXT4_I(inode)->xattr_sem);
530
531 unlock_page(page);
532 return ret >= 0 ? 0 : ret;
533 }
534
ext4_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode,unsigned flags)535 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
536 struct inode *inode,
537 unsigned flags)
538 {
539 int ret, needed_blocks, no_expand;
540 handle_t *handle = NULL;
541 int retries = 0, sem_held = 0;
542 struct page *page = NULL;
543 unsigned from, to;
544 struct ext4_iloc iloc;
545
546 if (!ext4_has_inline_data(inode)) {
547 /*
548 * clear the flag so that no new write
549 * will trap here again.
550 */
551 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
552 return 0;
553 }
554
555 needed_blocks = ext4_writepage_trans_blocks(inode);
556
557 ret = ext4_get_inode_loc(inode, &iloc);
558 if (ret)
559 return ret;
560
561 retry:
562 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
563 if (IS_ERR(handle)) {
564 ret = PTR_ERR(handle);
565 handle = NULL;
566 goto out;
567 }
568
569 /* We cannot recurse into the filesystem as the transaction is already
570 * started */
571 flags |= AOP_FLAG_NOFS;
572
573 page = grab_cache_page_write_begin(mapping, 0, flags);
574 if (!page) {
575 ret = -ENOMEM;
576 goto out;
577 }
578
579 ext4_write_lock_xattr(inode, &no_expand);
580 sem_held = 1;
581 /* If some one has already done this for us, just exit. */
582 if (!ext4_has_inline_data(inode)) {
583 ret = 0;
584 goto out;
585 }
586
587 from = 0;
588 to = ext4_get_inline_size(inode);
589 if (!PageUptodate(page)) {
590 ret = ext4_read_inline_page(inode, page);
591 if (ret < 0)
592 goto out;
593 }
594
595 ret = ext4_destroy_inline_data_nolock(handle, inode);
596 if (ret)
597 goto out;
598
599 if (ext4_should_dioread_nolock(inode)) {
600 ret = __block_write_begin(page, from, to,
601 ext4_get_block_unwritten);
602 } else
603 ret = __block_write_begin(page, from, to, ext4_get_block);
604
605 if (!ret && ext4_should_journal_data(inode)) {
606 ret = ext4_walk_page_buffers(handle, page_buffers(page),
607 from, to, NULL,
608 do_journal_get_write_access);
609 }
610
611 if (ret) {
612 unlock_page(page);
613 put_page(page);
614 page = NULL;
615 ext4_orphan_add(handle, inode);
616 ext4_write_unlock_xattr(inode, &no_expand);
617 sem_held = 0;
618 ext4_journal_stop(handle);
619 handle = NULL;
620 ext4_truncate_failed_write(inode);
621 /*
622 * If truncate failed early the inode might
623 * still be on the orphan list; we need to
624 * make sure the inode is removed from the
625 * orphan list in that case.
626 */
627 if (inode->i_nlink)
628 ext4_orphan_del(NULL, inode);
629 }
630
631 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
632 goto retry;
633
634 if (page)
635 block_commit_write(page, from, to);
636 out:
637 if (page) {
638 unlock_page(page);
639 put_page(page);
640 }
641 if (sem_held)
642 ext4_write_unlock_xattr(inode, &no_expand);
643 if (handle)
644 ext4_journal_stop(handle);
645 brelse(iloc.bh);
646 return ret;
647 }
648
649 /*
650 * Try to write data in the inode.
651 * If the inode has inline data, check whether the new write can be
652 * in the inode also. If not, create the page the handle, move the data
653 * to the page make it update and let the later codes create extent for it.
654 */
ext4_try_to_write_inline_data(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,unsigned flags,struct page ** pagep)655 int ext4_try_to_write_inline_data(struct address_space *mapping,
656 struct inode *inode,
657 loff_t pos, unsigned len,
658 unsigned flags,
659 struct page **pagep)
660 {
661 int ret;
662 handle_t *handle;
663 struct page *page;
664 struct ext4_iloc iloc;
665
666 if (pos + len > ext4_get_max_inline_size(inode))
667 goto convert;
668
669 ret = ext4_get_inode_loc(inode, &iloc);
670 if (ret)
671 return ret;
672
673 /*
674 * The possible write could happen in the inode,
675 * so try to reserve the space in inode first.
676 */
677 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
678 if (IS_ERR(handle)) {
679 ret = PTR_ERR(handle);
680 handle = NULL;
681 goto out;
682 }
683
684 ret = ext4_prepare_inline_data(handle, inode, pos + len);
685 if (ret && ret != -ENOSPC)
686 goto out;
687
688 /* We don't have space in inline inode, so convert it to extent. */
689 if (ret == -ENOSPC) {
690 ext4_journal_stop(handle);
691 brelse(iloc.bh);
692 goto convert;
693 }
694
695 flags |= AOP_FLAG_NOFS;
696
697 page = grab_cache_page_write_begin(mapping, 0, flags);
698 if (!page) {
699 ret = -ENOMEM;
700 goto out;
701 }
702
703 *pagep = page;
704 down_read(&EXT4_I(inode)->xattr_sem);
705 if (!ext4_has_inline_data(inode)) {
706 ret = 0;
707 unlock_page(page);
708 put_page(page);
709 goto out_up_read;
710 }
711
712 if (!PageUptodate(page)) {
713 ret = ext4_read_inline_page(inode, page);
714 if (ret < 0)
715 goto out_up_read;
716 }
717
718 ret = 1;
719 handle = NULL;
720 out_up_read:
721 up_read(&EXT4_I(inode)->xattr_sem);
722 out:
723 if (handle)
724 ext4_journal_stop(handle);
725 brelse(iloc.bh);
726 return ret;
727 convert:
728 return ext4_convert_inline_data_to_extent(mapping,
729 inode, flags);
730 }
731
ext4_write_inline_data_end(struct inode * inode,loff_t pos,unsigned len,unsigned copied,struct page * page)732 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
733 unsigned copied, struct page *page)
734 {
735 int ret, no_expand;
736 void *kaddr;
737 struct ext4_iloc iloc;
738
739 if (unlikely(copied < len)) {
740 if (!PageUptodate(page)) {
741 copied = 0;
742 goto out;
743 }
744 }
745
746 ret = ext4_get_inode_loc(inode, &iloc);
747 if (ret) {
748 ext4_std_error(inode->i_sb, ret);
749 copied = 0;
750 goto out;
751 }
752
753 ext4_write_lock_xattr(inode, &no_expand);
754 BUG_ON(!ext4_has_inline_data(inode));
755
756 kaddr = kmap_atomic(page);
757 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
758 kunmap_atomic(kaddr);
759 SetPageUptodate(page);
760 /* clear page dirty so that writepages wouldn't work for us. */
761 ClearPageDirty(page);
762
763 ext4_write_unlock_xattr(inode, &no_expand);
764 brelse(iloc.bh);
765 out:
766 return copied;
767 }
768
769 struct buffer_head *
ext4_journalled_write_inline_data(struct inode * inode,unsigned len,struct page * page)770 ext4_journalled_write_inline_data(struct inode *inode,
771 unsigned len,
772 struct page *page)
773 {
774 int ret, no_expand;
775 void *kaddr;
776 struct ext4_iloc iloc;
777
778 ret = ext4_get_inode_loc(inode, &iloc);
779 if (ret) {
780 ext4_std_error(inode->i_sb, ret);
781 return NULL;
782 }
783
784 ext4_write_lock_xattr(inode, &no_expand);
785 kaddr = kmap_atomic(page);
786 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
787 kunmap_atomic(kaddr);
788 ext4_write_unlock_xattr(inode, &no_expand);
789
790 return iloc.bh;
791 }
792
793 /*
794 * Try to make the page cache and handle ready for the inline data case.
795 * We can call this function in 2 cases:
796 * 1. The inode is created and the first write exceeds inline size. We can
797 * clear the inode state safely.
798 * 2. The inode has inline data, then we need to read the data, make it
799 * update and dirty so that ext4_da_writepages can handle it. We don't
800 * need to start the journal since the file's metatdata isn't changed now.
801 */
ext4_da_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode,unsigned flags,void ** fsdata)802 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
803 struct inode *inode,
804 unsigned flags,
805 void **fsdata)
806 {
807 int ret = 0, inline_size;
808 struct page *page;
809
810 page = grab_cache_page_write_begin(mapping, 0, flags);
811 if (!page)
812 return -ENOMEM;
813
814 down_read(&EXT4_I(inode)->xattr_sem);
815 if (!ext4_has_inline_data(inode)) {
816 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
817 goto out;
818 }
819
820 inline_size = ext4_get_inline_size(inode);
821
822 if (!PageUptodate(page)) {
823 ret = ext4_read_inline_page(inode, page);
824 if (ret < 0)
825 goto out;
826 }
827
828 ret = __block_write_begin(page, 0, inline_size,
829 ext4_da_get_block_prep);
830 if (ret) {
831 up_read(&EXT4_I(inode)->xattr_sem);
832 unlock_page(page);
833 put_page(page);
834 ext4_truncate_failed_write(inode);
835 return ret;
836 }
837
838 SetPageDirty(page);
839 SetPageUptodate(page);
840 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
841 *fsdata = (void *)CONVERT_INLINE_DATA;
842
843 out:
844 up_read(&EXT4_I(inode)->xattr_sem);
845 if (page) {
846 unlock_page(page);
847 put_page(page);
848 }
849 return ret;
850 }
851
852 /*
853 * Prepare the write for the inline data.
854 * If the the data can be written into the inode, we just read
855 * the page and make it uptodate, and start the journal.
856 * Otherwise read the page, makes it dirty so that it can be
857 * handle in writepages(the i_disksize update is left to the
858 * normal ext4_da_write_end).
859 */
ext4_da_write_inline_data_begin(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)860 int ext4_da_write_inline_data_begin(struct address_space *mapping,
861 struct inode *inode,
862 loff_t pos, unsigned len,
863 unsigned flags,
864 struct page **pagep,
865 void **fsdata)
866 {
867 int ret, inline_size;
868 handle_t *handle;
869 struct page *page;
870 struct ext4_iloc iloc;
871 int retries;
872
873 ret = ext4_get_inode_loc(inode, &iloc);
874 if (ret)
875 return ret;
876
877 retry_journal:
878 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
879 if (IS_ERR(handle)) {
880 ret = PTR_ERR(handle);
881 goto out;
882 }
883
884 inline_size = ext4_get_max_inline_size(inode);
885
886 ret = -ENOSPC;
887 if (inline_size >= pos + len) {
888 ret = ext4_prepare_inline_data(handle, inode, pos + len);
889 if (ret && ret != -ENOSPC)
890 goto out_journal;
891 }
892
893 /*
894 * We cannot recurse into the filesystem as the transaction
895 * is already started.
896 */
897 flags |= AOP_FLAG_NOFS;
898
899 if (ret == -ENOSPC) {
900 ret = ext4_da_convert_inline_data_to_extent(mapping,
901 inode,
902 flags,
903 fsdata);
904 ext4_journal_stop(handle);
905 if (ret == -ENOSPC &&
906 ext4_should_retry_alloc(inode->i_sb, &retries))
907 goto retry_journal;
908 goto out;
909 }
910
911
912 page = grab_cache_page_write_begin(mapping, 0, flags);
913 if (!page) {
914 ret = -ENOMEM;
915 goto out_journal;
916 }
917
918 down_read(&EXT4_I(inode)->xattr_sem);
919 if (!ext4_has_inline_data(inode)) {
920 ret = 0;
921 goto out_release_page;
922 }
923
924 if (!PageUptodate(page)) {
925 ret = ext4_read_inline_page(inode, page);
926 if (ret < 0)
927 goto out_release_page;
928 }
929
930 up_read(&EXT4_I(inode)->xattr_sem);
931 *pagep = page;
932 brelse(iloc.bh);
933 return 1;
934 out_release_page:
935 up_read(&EXT4_I(inode)->xattr_sem);
936 unlock_page(page);
937 put_page(page);
938 out_journal:
939 ext4_journal_stop(handle);
940 out:
941 brelse(iloc.bh);
942 return ret;
943 }
944
ext4_da_write_inline_data_end(struct inode * inode,loff_t pos,unsigned len,unsigned copied,struct page * page)945 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
946 unsigned len, unsigned copied,
947 struct page *page)
948 {
949 int i_size_changed = 0;
950 int ret;
951
952 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
953 if (ret < 0) {
954 unlock_page(page);
955 put_page(page);
956 return ret;
957 }
958 copied = ret;
959
960 /*
961 * No need to use i_size_read() here, the i_size
962 * cannot change under us because we hold i_mutex.
963 *
964 * But it's important to update i_size while still holding page lock:
965 * page writeout could otherwise come in and zero beyond i_size.
966 */
967 if (pos+copied > inode->i_size) {
968 i_size_write(inode, pos+copied);
969 i_size_changed = 1;
970 }
971 unlock_page(page);
972 put_page(page);
973
974 /*
975 * Don't mark the inode dirty under page lock. First, it unnecessarily
976 * makes the holding time of page lock longer. Second, it forces lock
977 * ordering of page lock and transaction start for journaling
978 * filesystems.
979 */
980 if (i_size_changed)
981 mark_inode_dirty(inode);
982
983 return copied;
984 }
985
986 #ifdef INLINE_DIR_DEBUG
ext4_show_inline_dir(struct inode * dir,struct buffer_head * bh,void * inline_start,int inline_size)987 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
988 void *inline_start, int inline_size)
989 {
990 int offset;
991 unsigned short de_len;
992 struct ext4_dir_entry_2 *de = inline_start;
993 void *dlimit = inline_start + inline_size;
994
995 trace_printk("inode %lu\n", dir->i_ino);
996 offset = 0;
997 while ((void *)de < dlimit) {
998 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
999 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1000 offset, de_len, de->name_len, de->name,
1001 de->name_len, le32_to_cpu(de->inode));
1002 if (ext4_check_dir_entry(dir, NULL, de, bh,
1003 inline_start, inline_size, offset))
1004 BUG();
1005
1006 offset += de_len;
1007 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1008 }
1009 }
1010 #else
1011 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1012 #endif
1013
1014 /*
1015 * Add a new entry into a inline dir.
1016 * It will return -ENOSPC if no space is available, and -EIO
1017 * and -EEXIST if directory entry already exists.
1018 */
ext4_add_dirent_to_inline(handle_t * handle,struct ext4_filename * fname,struct inode * dir,struct inode * inode,struct ext4_iloc * iloc,void * inline_start,int inline_size)1019 static int ext4_add_dirent_to_inline(handle_t *handle,
1020 struct ext4_filename *fname,
1021 struct inode *dir,
1022 struct inode *inode,
1023 struct ext4_iloc *iloc,
1024 void *inline_start, int inline_size)
1025 {
1026 int err;
1027 struct ext4_dir_entry_2 *de;
1028
1029 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1030 inline_size, fname, &de);
1031 if (err)
1032 return err;
1033
1034 BUFFER_TRACE(iloc->bh, "get_write_access");
1035 err = ext4_journal_get_write_access(handle, iloc->bh);
1036 if (err)
1037 return err;
1038 ext4_insert_dentry(inode, de, inline_size, fname);
1039
1040 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1041
1042 /*
1043 * XXX shouldn't update any times until successful
1044 * completion of syscall, but too many callers depend
1045 * on this.
1046 *
1047 * XXX similarly, too many callers depend on
1048 * ext4_new_inode() setting the times, but error
1049 * recovery deletes the inode, so the worst that can
1050 * happen is that the times are slightly out of date
1051 * and/or different from the directory change time.
1052 */
1053 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1054 ext4_update_dx_flag(dir);
1055 dir->i_version++;
1056 ext4_mark_inode_dirty(handle, dir);
1057 return 1;
1058 }
1059
ext4_get_inline_xattr_pos(struct inode * inode,struct ext4_iloc * iloc)1060 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1061 struct ext4_iloc *iloc)
1062 {
1063 struct ext4_xattr_entry *entry;
1064 struct ext4_xattr_ibody_header *header;
1065
1066 BUG_ON(!EXT4_I(inode)->i_inline_off);
1067
1068 header = IHDR(inode, ext4_raw_inode(iloc));
1069 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1070 EXT4_I(inode)->i_inline_off);
1071
1072 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1073 }
1074
1075 /* Set the final de to cover the whole block. */
ext4_update_final_de(void * de_buf,int old_size,int new_size)1076 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1077 {
1078 struct ext4_dir_entry_2 *de, *prev_de;
1079 void *limit;
1080 int de_len;
1081
1082 de = (struct ext4_dir_entry_2 *)de_buf;
1083 if (old_size) {
1084 limit = de_buf + old_size;
1085 do {
1086 prev_de = de;
1087 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1088 de_buf += de_len;
1089 de = (struct ext4_dir_entry_2 *)de_buf;
1090 } while (de_buf < limit);
1091
1092 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1093 old_size, new_size);
1094 } else {
1095 /* this is just created, so create an empty entry. */
1096 de->inode = 0;
1097 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1098 }
1099 }
1100
ext4_update_inline_dir(handle_t * handle,struct inode * dir,struct ext4_iloc * iloc)1101 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1102 struct ext4_iloc *iloc)
1103 {
1104 int ret;
1105 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1106 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1107
1108 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1109 return -ENOSPC;
1110
1111 ret = ext4_update_inline_data(handle, dir,
1112 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1113 if (ret)
1114 return ret;
1115
1116 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1117 EXT4_I(dir)->i_inline_size -
1118 EXT4_MIN_INLINE_DATA_SIZE);
1119 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1120 return 0;
1121 }
1122
ext4_restore_inline_data(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc,void * buf,int inline_size)1123 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1124 struct ext4_iloc *iloc,
1125 void *buf, int inline_size)
1126 {
1127 ext4_create_inline_data(handle, inode, inline_size);
1128 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1129 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1130 }
1131
ext4_finish_convert_inline_dir(handle_t * handle,struct inode * inode,struct buffer_head * dir_block,void * buf,int inline_size)1132 static int ext4_finish_convert_inline_dir(handle_t *handle,
1133 struct inode *inode,
1134 struct buffer_head *dir_block,
1135 void *buf,
1136 int inline_size)
1137 {
1138 int err, csum_size = 0, header_size = 0;
1139 struct ext4_dir_entry_2 *de;
1140 struct ext4_dir_entry_tail *t;
1141 void *target = dir_block->b_data;
1142
1143 /*
1144 * First create "." and ".." and then copy the dir information
1145 * back to the block.
1146 */
1147 de = (struct ext4_dir_entry_2 *)target;
1148 de = ext4_init_dot_dotdot(inode, de,
1149 inode->i_sb->s_blocksize, csum_size,
1150 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1151 header_size = (void *)de - target;
1152
1153 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1154 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1155
1156 if (ext4_has_metadata_csum(inode->i_sb))
1157 csum_size = sizeof(struct ext4_dir_entry_tail);
1158
1159 inode->i_size = inode->i_sb->s_blocksize;
1160 i_size_write(inode, inode->i_sb->s_blocksize);
1161 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1162 ext4_update_final_de(dir_block->b_data,
1163 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1164 inode->i_sb->s_blocksize - csum_size);
1165
1166 if (csum_size) {
1167 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1168 inode->i_sb->s_blocksize);
1169 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1170 }
1171 set_buffer_uptodate(dir_block);
1172 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1173 if (err)
1174 return err;
1175 set_buffer_verified(dir_block);
1176 return ext4_mark_inode_dirty(handle, inode);
1177 }
1178
ext4_convert_inline_data_nolock(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc)1179 static int ext4_convert_inline_data_nolock(handle_t *handle,
1180 struct inode *inode,
1181 struct ext4_iloc *iloc)
1182 {
1183 int error;
1184 void *buf = NULL;
1185 struct buffer_head *data_bh = NULL;
1186 struct ext4_map_blocks map;
1187 int inline_size;
1188
1189 inline_size = ext4_get_inline_size(inode);
1190 buf = kmalloc(inline_size, GFP_NOFS);
1191 if (!buf) {
1192 error = -ENOMEM;
1193 goto out;
1194 }
1195
1196 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1197 if (error < 0)
1198 goto out;
1199
1200 /*
1201 * Make sure the inline directory entries pass checks before we try to
1202 * convert them, so that we avoid touching stuff that needs fsck.
1203 */
1204 if (S_ISDIR(inode->i_mode)) {
1205 error = ext4_check_all_de(inode, iloc->bh,
1206 buf + EXT4_INLINE_DOTDOT_SIZE,
1207 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1208 if (error)
1209 goto out;
1210 }
1211
1212 error = ext4_destroy_inline_data_nolock(handle, inode);
1213 if (error)
1214 goto out;
1215
1216 map.m_lblk = 0;
1217 map.m_len = 1;
1218 map.m_flags = 0;
1219 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1220 if (error < 0)
1221 goto out_restore;
1222 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1223 error = -EIO;
1224 goto out_restore;
1225 }
1226
1227 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1228 if (!data_bh) {
1229 error = -ENOMEM;
1230 goto out_restore;
1231 }
1232
1233 lock_buffer(data_bh);
1234 error = ext4_journal_get_create_access(handle, data_bh);
1235 if (error) {
1236 unlock_buffer(data_bh);
1237 error = -EIO;
1238 goto out_restore;
1239 }
1240 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1241
1242 if (!S_ISDIR(inode->i_mode)) {
1243 memcpy(data_bh->b_data, buf, inline_size);
1244 set_buffer_uptodate(data_bh);
1245 error = ext4_handle_dirty_metadata(handle,
1246 inode, data_bh);
1247 } else {
1248 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1249 buf, inline_size);
1250 }
1251
1252 unlock_buffer(data_bh);
1253 out_restore:
1254 if (error)
1255 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1256
1257 out:
1258 brelse(data_bh);
1259 kfree(buf);
1260 return error;
1261 }
1262
1263 /*
1264 * Try to add the new entry to the inline data.
1265 * If succeeds, return 0. If not, extended the inline dir and copied data to
1266 * the new created block.
1267 */
ext4_try_add_inline_entry(handle_t * handle,struct ext4_filename * fname,struct inode * dir,struct inode * inode)1268 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1269 struct inode *dir, struct inode *inode)
1270 {
1271 int ret, inline_size, no_expand;
1272 void *inline_start;
1273 struct ext4_iloc iloc;
1274
1275 ret = ext4_get_inode_loc(dir, &iloc);
1276 if (ret)
1277 return ret;
1278
1279 ext4_write_lock_xattr(dir, &no_expand);
1280 if (!ext4_has_inline_data(dir))
1281 goto out;
1282
1283 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1284 EXT4_INLINE_DOTDOT_SIZE;
1285 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1286
1287 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1288 inline_start, inline_size);
1289 if (ret != -ENOSPC)
1290 goto out;
1291
1292 /* check whether it can be inserted to inline xattr space. */
1293 inline_size = EXT4_I(dir)->i_inline_size -
1294 EXT4_MIN_INLINE_DATA_SIZE;
1295 if (!inline_size) {
1296 /* Try to use the xattr space.*/
1297 ret = ext4_update_inline_dir(handle, dir, &iloc);
1298 if (ret && ret != -ENOSPC)
1299 goto out;
1300
1301 inline_size = EXT4_I(dir)->i_inline_size -
1302 EXT4_MIN_INLINE_DATA_SIZE;
1303 }
1304
1305 if (inline_size) {
1306 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1307
1308 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1309 inode, &iloc, inline_start,
1310 inline_size);
1311
1312 if (ret != -ENOSPC)
1313 goto out;
1314 }
1315
1316 /*
1317 * The inline space is filled up, so create a new block for it.
1318 * As the extent tree will be created, we have to save the inline
1319 * dir first.
1320 */
1321 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1322
1323 out:
1324 ext4_mark_inode_dirty(handle, dir);
1325 ext4_write_unlock_xattr(dir, &no_expand);
1326 brelse(iloc.bh);
1327 return ret;
1328 }
1329
1330 /*
1331 * This function fills a red-black tree with information from an
1332 * inlined dir. It returns the number directory entries loaded
1333 * into the tree. If there is an error it is returned in err.
1334 */
htree_inlinedir_to_tree(struct file * dir_file,struct inode * dir,ext4_lblk_t block,struct dx_hash_info * hinfo,__u32 start_hash,__u32 start_minor_hash,int * has_inline_data)1335 int htree_inlinedir_to_tree(struct file *dir_file,
1336 struct inode *dir, ext4_lblk_t block,
1337 struct dx_hash_info *hinfo,
1338 __u32 start_hash, __u32 start_minor_hash,
1339 int *has_inline_data)
1340 {
1341 int err = 0, count = 0;
1342 unsigned int parent_ino;
1343 int pos;
1344 struct ext4_dir_entry_2 *de;
1345 struct inode *inode = file_inode(dir_file);
1346 int ret, inline_size = 0;
1347 struct ext4_iloc iloc;
1348 void *dir_buf = NULL;
1349 struct ext4_dir_entry_2 fake;
1350 struct fscrypt_str tmp_str;
1351
1352 ret = ext4_get_inode_loc(inode, &iloc);
1353 if (ret)
1354 return ret;
1355
1356 down_read(&EXT4_I(inode)->xattr_sem);
1357 if (!ext4_has_inline_data(inode)) {
1358 up_read(&EXT4_I(inode)->xattr_sem);
1359 *has_inline_data = 0;
1360 goto out;
1361 }
1362
1363 inline_size = ext4_get_inline_size(inode);
1364 dir_buf = kmalloc(inline_size, GFP_NOFS);
1365 if (!dir_buf) {
1366 ret = -ENOMEM;
1367 up_read(&EXT4_I(inode)->xattr_sem);
1368 goto out;
1369 }
1370
1371 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1372 up_read(&EXT4_I(inode)->xattr_sem);
1373 if (ret < 0)
1374 goto out;
1375
1376 pos = 0;
1377 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1378 while (pos < inline_size) {
1379 /*
1380 * As inlined dir doesn't store any information about '.' and
1381 * only the inode number of '..' is stored, we have to handle
1382 * them differently.
1383 */
1384 if (pos == 0) {
1385 fake.inode = cpu_to_le32(inode->i_ino);
1386 fake.name_len = 1;
1387 strcpy(fake.name, ".");
1388 fake.rec_len = ext4_rec_len_to_disk(
1389 EXT4_DIR_REC_LEN(fake.name_len),
1390 inline_size);
1391 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1392 de = &fake;
1393 pos = EXT4_INLINE_DOTDOT_OFFSET;
1394 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1395 fake.inode = cpu_to_le32(parent_ino);
1396 fake.name_len = 2;
1397 strcpy(fake.name, "..");
1398 fake.rec_len = ext4_rec_len_to_disk(
1399 EXT4_DIR_REC_LEN(fake.name_len),
1400 inline_size);
1401 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1402 de = &fake;
1403 pos = EXT4_INLINE_DOTDOT_SIZE;
1404 } else {
1405 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1406 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1407 if (ext4_check_dir_entry(inode, dir_file, de,
1408 iloc.bh, dir_buf,
1409 inline_size, pos)) {
1410 ret = count;
1411 goto out;
1412 }
1413 }
1414
1415 ext4fs_dirhash(de->name, de->name_len, hinfo);
1416 if ((hinfo->hash < start_hash) ||
1417 ((hinfo->hash == start_hash) &&
1418 (hinfo->minor_hash < start_minor_hash)))
1419 continue;
1420 if (de->inode == 0)
1421 continue;
1422 tmp_str.name = de->name;
1423 tmp_str.len = de->name_len;
1424 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1425 hinfo->minor_hash, de, &tmp_str);
1426 if (err) {
1427 count = err;
1428 goto out;
1429 }
1430 count++;
1431 }
1432 ret = count;
1433 out:
1434 kfree(dir_buf);
1435 brelse(iloc.bh);
1436 return ret;
1437 }
1438
1439 /*
1440 * So this function is called when the volume is mkfsed with
1441 * dir_index disabled. In order to keep f_pos persistent
1442 * after we convert from an inlined dir to a blocked based,
1443 * we just pretend that we are a normal dir and return the
1444 * offset as if '.' and '..' really take place.
1445 *
1446 */
ext4_read_inline_dir(struct file * file,struct dir_context * ctx,int * has_inline_data)1447 int ext4_read_inline_dir(struct file *file,
1448 struct dir_context *ctx,
1449 int *has_inline_data)
1450 {
1451 unsigned int offset, parent_ino;
1452 int i;
1453 struct ext4_dir_entry_2 *de;
1454 struct super_block *sb;
1455 struct inode *inode = file_inode(file);
1456 int ret, inline_size = 0;
1457 struct ext4_iloc iloc;
1458 void *dir_buf = NULL;
1459 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1460
1461 ret = ext4_get_inode_loc(inode, &iloc);
1462 if (ret)
1463 return ret;
1464
1465 down_read(&EXT4_I(inode)->xattr_sem);
1466 if (!ext4_has_inline_data(inode)) {
1467 up_read(&EXT4_I(inode)->xattr_sem);
1468 *has_inline_data = 0;
1469 goto out;
1470 }
1471
1472 inline_size = ext4_get_inline_size(inode);
1473 dir_buf = kmalloc(inline_size, GFP_NOFS);
1474 if (!dir_buf) {
1475 ret = -ENOMEM;
1476 up_read(&EXT4_I(inode)->xattr_sem);
1477 goto out;
1478 }
1479
1480 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1481 up_read(&EXT4_I(inode)->xattr_sem);
1482 if (ret < 0)
1483 goto out;
1484
1485 ret = 0;
1486 sb = inode->i_sb;
1487 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1488 offset = ctx->pos;
1489
1490 /*
1491 * dotdot_offset and dotdot_size is the real offset and
1492 * size for ".." and "." if the dir is block based while
1493 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1494 * So we will use extra_offset and extra_size to indicate them
1495 * during the inline dir iteration.
1496 */
1497 dotdot_offset = EXT4_DIR_REC_LEN(1);
1498 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1499 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1500 extra_size = extra_offset + inline_size;
1501
1502 /*
1503 * If the version has changed since the last call to
1504 * readdir(2), then we might be pointing to an invalid
1505 * dirent right now. Scan from the start of the inline
1506 * dir to make sure.
1507 */
1508 if (file->f_version != inode->i_version) {
1509 for (i = 0; i < extra_size && i < offset;) {
1510 /*
1511 * "." is with offset 0 and
1512 * ".." is dotdot_offset.
1513 */
1514 if (!i) {
1515 i = dotdot_offset;
1516 continue;
1517 } else if (i == dotdot_offset) {
1518 i = dotdot_size;
1519 continue;
1520 }
1521 /* for other entry, the real offset in
1522 * the buf has to be tuned accordingly.
1523 */
1524 de = (struct ext4_dir_entry_2 *)
1525 (dir_buf + i - extra_offset);
1526 /* It's too expensive to do a full
1527 * dirent test each time round this
1528 * loop, but we do have to test at
1529 * least that it is non-zero. A
1530 * failure will be detected in the
1531 * dirent test below. */
1532 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1533 < EXT4_DIR_REC_LEN(1))
1534 break;
1535 i += ext4_rec_len_from_disk(de->rec_len,
1536 extra_size);
1537 }
1538 offset = i;
1539 ctx->pos = offset;
1540 file->f_version = inode->i_version;
1541 }
1542
1543 while (ctx->pos < extra_size) {
1544 if (ctx->pos == 0) {
1545 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1546 goto out;
1547 ctx->pos = dotdot_offset;
1548 continue;
1549 }
1550
1551 if (ctx->pos == dotdot_offset) {
1552 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1553 goto out;
1554 ctx->pos = dotdot_size;
1555 continue;
1556 }
1557
1558 de = (struct ext4_dir_entry_2 *)
1559 (dir_buf + ctx->pos - extra_offset);
1560 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1561 extra_size, ctx->pos))
1562 goto out;
1563 if (le32_to_cpu(de->inode)) {
1564 if (!dir_emit(ctx, de->name, de->name_len,
1565 le32_to_cpu(de->inode),
1566 get_dtype(sb, de->file_type)))
1567 goto out;
1568 }
1569 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1570 }
1571 out:
1572 kfree(dir_buf);
1573 brelse(iloc.bh);
1574 return ret;
1575 }
1576
ext4_get_first_inline_block(struct inode * inode,struct ext4_dir_entry_2 ** parent_de,int * retval)1577 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1578 struct ext4_dir_entry_2 **parent_de,
1579 int *retval)
1580 {
1581 struct ext4_iloc iloc;
1582
1583 *retval = ext4_get_inode_loc(inode, &iloc);
1584 if (*retval)
1585 return NULL;
1586
1587 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1588
1589 return iloc.bh;
1590 }
1591
1592 /*
1593 * Try to create the inline data for the new dir.
1594 * If it succeeds, return 0, otherwise return the error.
1595 * In case of ENOSPC, the caller should create the normal disk layout dir.
1596 */
ext4_try_create_inline_dir(handle_t * handle,struct inode * parent,struct inode * inode)1597 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1598 struct inode *inode)
1599 {
1600 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1601 struct ext4_iloc iloc;
1602 struct ext4_dir_entry_2 *de;
1603
1604 ret = ext4_get_inode_loc(inode, &iloc);
1605 if (ret)
1606 return ret;
1607
1608 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1609 if (ret)
1610 goto out;
1611
1612 /*
1613 * For inline dir, we only save the inode information for the ".."
1614 * and create a fake dentry to cover the left space.
1615 */
1616 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1617 de->inode = cpu_to_le32(parent->i_ino);
1618 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1619 de->inode = 0;
1620 de->rec_len = ext4_rec_len_to_disk(
1621 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1622 inline_size);
1623 set_nlink(inode, 2);
1624 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1625 out:
1626 brelse(iloc.bh);
1627 return ret;
1628 }
1629
ext4_find_inline_entry(struct inode * dir,struct ext4_filename * fname,const struct qstr * d_name,struct ext4_dir_entry_2 ** res_dir,int * has_inline_data)1630 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1631 struct ext4_filename *fname,
1632 const struct qstr *d_name,
1633 struct ext4_dir_entry_2 **res_dir,
1634 int *has_inline_data)
1635 {
1636 int ret;
1637 struct ext4_iloc iloc;
1638 void *inline_start;
1639 int inline_size;
1640
1641 if (ext4_get_inode_loc(dir, &iloc))
1642 return NULL;
1643
1644 down_read(&EXT4_I(dir)->xattr_sem);
1645 if (!ext4_has_inline_data(dir)) {
1646 *has_inline_data = 0;
1647 goto out;
1648 }
1649
1650 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1651 EXT4_INLINE_DOTDOT_SIZE;
1652 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1653 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1654 dir, fname, d_name, 0, res_dir);
1655 if (ret == 1)
1656 goto out_find;
1657 if (ret < 0)
1658 goto out;
1659
1660 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1661 goto out;
1662
1663 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1664 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1665
1666 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1667 dir, fname, d_name, 0, res_dir);
1668 if (ret == 1)
1669 goto out_find;
1670
1671 out:
1672 brelse(iloc.bh);
1673 iloc.bh = NULL;
1674 out_find:
1675 up_read(&EXT4_I(dir)->xattr_sem);
1676 return iloc.bh;
1677 }
1678
ext4_delete_inline_entry(handle_t * handle,struct inode * dir,struct ext4_dir_entry_2 * de_del,struct buffer_head * bh,int * has_inline_data)1679 int ext4_delete_inline_entry(handle_t *handle,
1680 struct inode *dir,
1681 struct ext4_dir_entry_2 *de_del,
1682 struct buffer_head *bh,
1683 int *has_inline_data)
1684 {
1685 int err, inline_size, no_expand;
1686 struct ext4_iloc iloc;
1687 void *inline_start;
1688
1689 err = ext4_get_inode_loc(dir, &iloc);
1690 if (err)
1691 return err;
1692
1693 ext4_write_lock_xattr(dir, &no_expand);
1694 if (!ext4_has_inline_data(dir)) {
1695 *has_inline_data = 0;
1696 goto out;
1697 }
1698
1699 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1700 EXT4_MIN_INLINE_DATA_SIZE) {
1701 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1702 EXT4_INLINE_DOTDOT_SIZE;
1703 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1704 EXT4_INLINE_DOTDOT_SIZE;
1705 } else {
1706 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1707 inline_size = ext4_get_inline_size(dir) -
1708 EXT4_MIN_INLINE_DATA_SIZE;
1709 }
1710
1711 BUFFER_TRACE(bh, "get_write_access");
1712 err = ext4_journal_get_write_access(handle, bh);
1713 if (err)
1714 goto out;
1715
1716 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1717 inline_start, inline_size, 0);
1718 if (err)
1719 goto out;
1720
1721 err = ext4_mark_inode_dirty(handle, dir);
1722 if (unlikely(err))
1723 goto out;
1724
1725 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1726 out:
1727 ext4_write_unlock_xattr(dir, &no_expand);
1728 brelse(iloc.bh);
1729 if (err != -ENOENT)
1730 ext4_std_error(dir->i_sb, err);
1731 return err;
1732 }
1733
1734 /*
1735 * Get the inline dentry at offset.
1736 */
1737 static inline struct ext4_dir_entry_2 *
ext4_get_inline_entry(struct inode * inode,struct ext4_iloc * iloc,unsigned int offset,void ** inline_start,int * inline_size)1738 ext4_get_inline_entry(struct inode *inode,
1739 struct ext4_iloc *iloc,
1740 unsigned int offset,
1741 void **inline_start,
1742 int *inline_size)
1743 {
1744 void *inline_pos;
1745
1746 BUG_ON(offset > ext4_get_inline_size(inode));
1747
1748 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1749 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1750 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1751 } else {
1752 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1753 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1754 *inline_size = ext4_get_inline_size(inode) -
1755 EXT4_MIN_INLINE_DATA_SIZE;
1756 }
1757
1758 if (inline_start)
1759 *inline_start = inline_pos;
1760 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1761 }
1762
empty_inline_dir(struct inode * dir,int * has_inline_data)1763 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1764 {
1765 int err, inline_size;
1766 struct ext4_iloc iloc;
1767 void *inline_pos;
1768 unsigned int offset;
1769 struct ext4_dir_entry_2 *de;
1770 bool ret = true;
1771
1772 err = ext4_get_inode_loc(dir, &iloc);
1773 if (err) {
1774 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1775 err, dir->i_ino);
1776 return true;
1777 }
1778
1779 down_read(&EXT4_I(dir)->xattr_sem);
1780 if (!ext4_has_inline_data(dir)) {
1781 *has_inline_data = 0;
1782 goto out;
1783 }
1784
1785 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1786 if (!le32_to_cpu(de->inode)) {
1787 ext4_warning(dir->i_sb,
1788 "bad inline directory (dir #%lu) - no `..'",
1789 dir->i_ino);
1790 ret = true;
1791 goto out;
1792 }
1793
1794 offset = EXT4_INLINE_DOTDOT_SIZE;
1795 while (offset < dir->i_size) {
1796 de = ext4_get_inline_entry(dir, &iloc, offset,
1797 &inline_pos, &inline_size);
1798 if (ext4_check_dir_entry(dir, NULL, de,
1799 iloc.bh, inline_pos,
1800 inline_size, offset)) {
1801 ext4_warning(dir->i_sb,
1802 "bad inline directory (dir #%lu) - "
1803 "inode %u, rec_len %u, name_len %d"
1804 "inline size %d",
1805 dir->i_ino, le32_to_cpu(de->inode),
1806 le16_to_cpu(de->rec_len), de->name_len,
1807 inline_size);
1808 ret = true;
1809 goto out;
1810 }
1811 if (le32_to_cpu(de->inode)) {
1812 ret = false;
1813 goto out;
1814 }
1815 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1816 }
1817
1818 out:
1819 up_read(&EXT4_I(dir)->xattr_sem);
1820 brelse(iloc.bh);
1821 return ret;
1822 }
1823
ext4_destroy_inline_data(handle_t * handle,struct inode * inode)1824 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1825 {
1826 int ret, no_expand;
1827
1828 ext4_write_lock_xattr(inode, &no_expand);
1829 ret = ext4_destroy_inline_data_nolock(handle, inode);
1830 ext4_write_unlock_xattr(inode, &no_expand);
1831
1832 return ret;
1833 }
1834
ext4_inline_data_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,int * has_inline,__u64 start,__u64 len)1835 int ext4_inline_data_fiemap(struct inode *inode,
1836 struct fiemap_extent_info *fieinfo,
1837 int *has_inline, __u64 start, __u64 len)
1838 {
1839 __u64 physical = 0;
1840 __u64 inline_len;
1841 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1842 FIEMAP_EXTENT_LAST;
1843 int error = 0;
1844 struct ext4_iloc iloc;
1845
1846 down_read(&EXT4_I(inode)->xattr_sem);
1847 if (!ext4_has_inline_data(inode)) {
1848 *has_inline = 0;
1849 goto out;
1850 }
1851 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1852 i_size_read(inode));
1853 if (start >= inline_len)
1854 goto out;
1855 if (start + len < inline_len)
1856 inline_len = start + len;
1857 inline_len -= start;
1858
1859 error = ext4_get_inode_loc(inode, &iloc);
1860 if (error)
1861 goto out;
1862
1863 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1864 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1865 physical += offsetof(struct ext4_inode, i_block);
1866
1867 if (physical)
1868 error = fiemap_fill_next_extent(fieinfo, start, physical,
1869 inline_len, flags);
1870 brelse(iloc.bh);
1871 out:
1872 up_read(&EXT4_I(inode)->xattr_sem);
1873 return (error < 0 ? error : 0);
1874 }
1875
1876 /*
1877 * Called during xattr set, and if we can sparse space 'needed',
1878 * just create the extent tree evict the data to the outer block.
1879 *
1880 * We use jbd2 instead of page cache to move data to the 1st block
1881 * so that the whole transaction can be committed as a whole and
1882 * the data isn't lost because of the delayed page cache write.
1883 */
ext4_try_to_evict_inline_data(handle_t * handle,struct inode * inode,int needed)1884 int ext4_try_to_evict_inline_data(handle_t *handle,
1885 struct inode *inode,
1886 int needed)
1887 {
1888 int error;
1889 struct ext4_xattr_entry *entry;
1890 struct ext4_inode *raw_inode;
1891 struct ext4_iloc iloc;
1892
1893 error = ext4_get_inode_loc(inode, &iloc);
1894 if (error)
1895 return error;
1896
1897 raw_inode = ext4_raw_inode(&iloc);
1898 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
1899 EXT4_I(inode)->i_inline_off);
1900 if (EXT4_XATTR_LEN(entry->e_name_len) +
1901 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
1902 error = -ENOSPC;
1903 goto out;
1904 }
1905
1906 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1907 out:
1908 brelse(iloc.bh);
1909 return error;
1910 }
1911
ext4_inline_data_truncate(struct inode * inode,int * has_inline)1912 void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1913 {
1914 handle_t *handle;
1915 int inline_size, value_len, needed_blocks, no_expand;
1916 size_t i_size;
1917 void *value = NULL;
1918 struct ext4_xattr_ibody_find is = {
1919 .s = { .not_found = -ENODATA, },
1920 };
1921 struct ext4_xattr_info i = {
1922 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1923 .name = EXT4_XATTR_SYSTEM_DATA,
1924 };
1925
1926
1927 needed_blocks = ext4_writepage_trans_blocks(inode);
1928 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1929 if (IS_ERR(handle))
1930 return;
1931
1932 ext4_write_lock_xattr(inode, &no_expand);
1933 if (!ext4_has_inline_data(inode)) {
1934 *has_inline = 0;
1935 ext4_journal_stop(handle);
1936 return;
1937 }
1938
1939 if (ext4_orphan_add(handle, inode))
1940 goto out;
1941
1942 if (ext4_get_inode_loc(inode, &is.iloc))
1943 goto out;
1944
1945 down_write(&EXT4_I(inode)->i_data_sem);
1946 i_size = inode->i_size;
1947 inline_size = ext4_get_inline_size(inode);
1948 EXT4_I(inode)->i_disksize = i_size;
1949
1950 if (i_size < inline_size) {
1951 /* Clear the content in the xattr space. */
1952 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1953 if (ext4_xattr_ibody_find(inode, &i, &is))
1954 goto out_error;
1955
1956 BUG_ON(is.s.not_found);
1957
1958 value_len = le32_to_cpu(is.s.here->e_value_size);
1959 value = kmalloc(value_len, GFP_NOFS);
1960 if (!value)
1961 goto out_error;
1962
1963 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1964 value, value_len))
1965 goto out_error;
1966
1967 i.value = value;
1968 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1969 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1970 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1971 goto out_error;
1972 }
1973
1974 /* Clear the content within i_blocks. */
1975 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1976 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1977 memset(p + i_size, 0,
1978 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1979 }
1980
1981 EXT4_I(inode)->i_inline_size = i_size <
1982 EXT4_MIN_INLINE_DATA_SIZE ?
1983 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1984 }
1985
1986 out_error:
1987 up_write(&EXT4_I(inode)->i_data_sem);
1988 out:
1989 brelse(is.iloc.bh);
1990 ext4_write_unlock_xattr(inode, &no_expand);
1991 kfree(value);
1992 if (inode->i_nlink)
1993 ext4_orphan_del(handle, inode);
1994
1995 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1996 ext4_mark_inode_dirty(handle, inode);
1997 if (IS_SYNC(inode))
1998 ext4_handle_sync(handle);
1999
2000 ext4_journal_stop(handle);
2001 return;
2002 }
2003
ext4_convert_inline_data(struct inode * inode)2004 int ext4_convert_inline_data(struct inode *inode)
2005 {
2006 int error, needed_blocks, no_expand;
2007 handle_t *handle;
2008 struct ext4_iloc iloc;
2009
2010 if (!ext4_has_inline_data(inode)) {
2011 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2012 return 0;
2013 }
2014
2015 needed_blocks = ext4_writepage_trans_blocks(inode);
2016
2017 iloc.bh = NULL;
2018 error = ext4_get_inode_loc(inode, &iloc);
2019 if (error)
2020 return error;
2021
2022 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2023 if (IS_ERR(handle)) {
2024 error = PTR_ERR(handle);
2025 goto out_free;
2026 }
2027
2028 ext4_write_lock_xattr(inode, &no_expand);
2029 if (ext4_has_inline_data(inode))
2030 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2031 ext4_write_unlock_xattr(inode, &no_expand);
2032 ext4_journal_stop(handle);
2033 out_free:
2034 brelse(iloc.bh);
2035 return error;
2036 }
2037