OSDN Git Service

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