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