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