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