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