Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ext4 / inode.c
1 /*
2 * linux/fs/ext4/inode.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/inode.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * 64-bit file support on 64-bit platforms by Jakub Jelinek
16 * (jj@sunsite.ms.mff.cuni.cz)
17 *
18 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
19 */
20
21 #include <linux/fs.h>
22 #include <linux/time.h>
23 #include <linux/jbd2.h>
24 #include <linux/highuid.h>
25 #include <linux/pagemap.h>
26 #include <linux/quotaops.h>
27 #include <linux/string.h>
28 #include <linux/buffer_head.h>
29 #include <linux/writeback.h>
30 #include <linux/pagevec.h>
31 #include <linux/mpage.h>
32 #include <linux/namei.h>
33 #include <linux/uio.h>
34 #include <linux/bio.h>
35 #include <linux/workqueue.h>
36 #include <linux/kernel.h>
37 #include <linux/printk.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/aio.h>
41 #include <linux/bitops.h>
42
43 #include "ext4_jbd2.h"
44 #include "xattr.h"
45 #include "acl.h"
46 #include "truncate.h"
47
48 #include <trace/events/ext4.h>
49 #include <linux/blkdev.h>
50
51 #define MPAGE_DA_EXTENT_TAIL 0x01
52
53 static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
54 struct ext4_inode_info *ei)
55 {
56 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
57 __u32 csum;
58 __u16 dummy_csum = 0;
59 int offset = offsetof(struct ext4_inode, i_checksum_lo);
60 unsigned int csum_size = sizeof(dummy_csum);
61
62 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
63 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
64 offset += csum_size;
65 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
66 EXT4_GOOD_OLD_INODE_SIZE - offset);
67
68 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
69 offset = offsetof(struct ext4_inode, i_checksum_hi);
70 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
71 EXT4_GOOD_OLD_INODE_SIZE,
72 offset - EXT4_GOOD_OLD_INODE_SIZE);
73 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
74 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
75 csum_size);
76 offset += csum_size;
77 }
78 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
79 EXT4_INODE_SIZE(inode->i_sb) - offset);
80 }
81
82 return csum;
83 }
84
85 static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
86 struct ext4_inode_info *ei)
87 {
88 __u32 provided, calculated;
89
90 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
91 cpu_to_le32(EXT4_OS_LINUX) ||
92 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
93 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
94 return 1;
95
96 provided = le16_to_cpu(raw->i_checksum_lo);
97 calculated = ext4_inode_csum(inode, raw, ei);
98 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
99 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
100 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
101 else
102 calculated &= 0xFFFF;
103
104 return provided == calculated;
105 }
106
107 static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
108 struct ext4_inode_info *ei)
109 {
110 __u32 csum;
111
112 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
113 cpu_to_le32(EXT4_OS_LINUX) ||
114 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
115 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
116 return;
117
118 csum = ext4_inode_csum(inode, raw, ei);
119 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
120 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
121 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
122 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
123 }
124
125 static inline int ext4_begin_ordered_truncate(struct inode *inode,
126 loff_t new_size)
127 {
128 trace_ext4_begin_ordered_truncate(inode, new_size);
129 /*
130 * If jinode is zero, then we never opened the file for
131 * writing, so there's no need to call
132 * jbd2_journal_begin_ordered_truncate() since there's no
133 * outstanding writes we need to flush.
134 */
135 if (!EXT4_I(inode)->jinode)
136 return 0;
137 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
138 EXT4_I(inode)->jinode,
139 new_size);
140 }
141
142 static void ext4_invalidatepage(struct page *page, unsigned long offset);
143 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
144 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
145 static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
146 struct inode *inode, struct page *page, loff_t from,
147 loff_t length, int flags);
148
149 /*
150 * Test whether an inode is a fast symlink.
151 */
152 static int ext4_inode_is_fast_symlink(struct inode *inode)
153 {
154 int ea_blocks = EXT4_I(inode)->i_file_acl ?
155 (inode->i_sb->s_blocksize >> 9) : 0;
156
157 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
158 }
159
160 /*
161 * Restart the transaction associated with *handle. This does a commit,
162 * so before we call here everything must be consistently dirtied against
163 * this transaction.
164 */
165 int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
166 int nblocks)
167 {
168 int ret;
169
170 /*
171 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
172 * moment, get_block can be called only for blocks inside i_size since
173 * page cache has been already dropped and writes are blocked by
174 * i_mutex. So we can safely drop the i_data_sem here.
175 */
176 BUG_ON(EXT4_JOURNAL(inode) == NULL);
177 jbd_debug(2, "restarting handle %p\n", handle);
178 up_write(&EXT4_I(inode)->i_data_sem);
179 ret = ext4_journal_restart(handle, nblocks);
180 down_write(&EXT4_I(inode)->i_data_sem);
181 ext4_discard_preallocations(inode);
182
183 return ret;
184 }
185
186 /*
187 * Called at the last iput() if i_nlink is zero.
188 */
189 void ext4_evict_inode(struct inode *inode)
190 {
191 handle_t *handle;
192 int err;
193
194 trace_ext4_evict_inode(inode);
195
196 if (inode->i_nlink) {
197 /*
198 * When journalling data dirty buffers are tracked only in the
199 * journal. So although mm thinks everything is clean and
200 * ready for reaping the inode might still have some pages to
201 * write in the running transaction or waiting to be
202 * checkpointed. Thus calling jbd2_journal_invalidatepage()
203 * (via truncate_inode_pages()) to discard these buffers can
204 * cause data loss. Also even if we did not discard these
205 * buffers, we would have no way to find them after the inode
206 * is reaped and thus user could see stale data if he tries to
207 * read them before the transaction is checkpointed. So be
208 * careful and force everything to disk here... We use
209 * ei->i_datasync_tid to store the newest transaction
210 * containing inode's data.
211 *
212 * Note that directories do not have this problem because they
213 * don't use page cache.
214 */
215 if (inode->i_ino != EXT4_JOURNAL_INO &&
216 ext4_should_journal_data(inode) &&
217 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
218 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
219 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
220
221 jbd2_complete_transaction(journal, commit_tid);
222 filemap_write_and_wait(&inode->i_data);
223 }
224 truncate_inode_pages(&inode->i_data, 0);
225 ext4_ioend_shutdown(inode);
226 goto no_delete;
227 }
228
229 if (!is_bad_inode(inode))
230 dquot_initialize(inode);
231
232 if (ext4_should_order_data(inode))
233 ext4_begin_ordered_truncate(inode, 0);
234 truncate_inode_pages(&inode->i_data, 0);
235 ext4_ioend_shutdown(inode);
236
237 if (is_bad_inode(inode))
238 goto no_delete;
239
240 /*
241 * Protect us against freezing - iput() caller didn't have to have any
242 * protection against it
243 */
244 sb_start_intwrite(inode->i_sb);
245 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
246 ext4_blocks_for_truncate(inode)+3);
247 if (IS_ERR(handle)) {
248 ext4_std_error(inode->i_sb, PTR_ERR(handle));
249 /*
250 * If we're going to skip the normal cleanup, we still need to
251 * make sure that the in-core orphan linked list is properly
252 * cleaned up.
253 */
254 ext4_orphan_del(NULL, inode);
255 sb_end_intwrite(inode->i_sb);
256 goto no_delete;
257 }
258
259 if (IS_SYNC(inode))
260 ext4_handle_sync(handle);
261 inode->i_size = 0;
262 err = ext4_mark_inode_dirty(handle, inode);
263 if (err) {
264 ext4_warning(inode->i_sb,
265 "couldn't mark inode dirty (err %d)", err);
266 goto stop_handle;
267 }
268 if (inode->i_blocks)
269 ext4_truncate(inode);
270
271 /*
272 * ext4_ext_truncate() doesn't reserve any slop when it
273 * restarts journal transactions; therefore there may not be
274 * enough credits left in the handle to remove the inode from
275 * the orphan list and set the dtime field.
276 */
277 if (!ext4_handle_has_enough_credits(handle, 3)) {
278 err = ext4_journal_extend(handle, 3);
279 if (err > 0)
280 err = ext4_journal_restart(handle, 3);
281 if (err != 0) {
282 ext4_warning(inode->i_sb,
283 "couldn't extend journal (err %d)", err);
284 stop_handle:
285 ext4_journal_stop(handle);
286 ext4_orphan_del(NULL, inode);
287 sb_end_intwrite(inode->i_sb);
288 goto no_delete;
289 }
290 }
291
292 /*
293 * Kill off the orphan record which ext4_truncate created.
294 * AKPM: I think this can be inside the above `if'.
295 * Note that ext4_orphan_del() has to be able to cope with the
296 * deletion of a non-existent orphan - this is because we don't
297 * know if ext4_truncate() actually created an orphan record.
298 * (Well, we could do this if we need to, but heck - it works)
299 */
300 ext4_orphan_del(handle, inode);
301 EXT4_I(inode)->i_dtime = get_seconds();
302
303 /*
304 * One subtle ordering requirement: if anything has gone wrong
305 * (transaction abort, IO errors, whatever), then we can still
306 * do these next steps (the fs will already have been marked as
307 * having errors), but we can't free the inode if the mark_dirty
308 * fails.
309 */
310 if (ext4_mark_inode_dirty(handle, inode))
311 /* If that failed, just do the required in-core inode clear. */
312 ext4_clear_inode(inode);
313 else
314 ext4_free_inode(handle, inode);
315 ext4_journal_stop(handle);
316 sb_end_intwrite(inode->i_sb);
317 return;
318 no_delete:
319 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
320 }
321
322 #ifdef CONFIG_QUOTA
323 qsize_t *ext4_get_reserved_space(struct inode *inode)
324 {
325 return &EXT4_I(inode)->i_reserved_quota;
326 }
327 #endif
328
329 /*
330 * Calculate the number of metadata blocks need to reserve
331 * to allocate a block located at @lblock
332 */
333 static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
334 {
335 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
336 return ext4_ext_calc_metadata_amount(inode, lblock);
337
338 return ext4_ind_calc_metadata_amount(inode, lblock);
339 }
340
341 /*
342 * Called with i_data_sem down, which is important since we can call
343 * ext4_discard_preallocations() from here.
344 */
345 void ext4_da_update_reserve_space(struct inode *inode,
346 int used, int quota_claim)
347 {
348 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
349 struct ext4_inode_info *ei = EXT4_I(inode);
350
351 spin_lock(&ei->i_block_reservation_lock);
352 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
353 if (unlikely(used > ei->i_reserved_data_blocks)) {
354 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
355 "with only %d reserved data blocks",
356 __func__, inode->i_ino, used,
357 ei->i_reserved_data_blocks);
358 WARN_ON(1);
359 used = ei->i_reserved_data_blocks;
360 }
361
362 if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
363 ext4_warning(inode->i_sb, "ino %lu, allocated %d "
364 "with only %d reserved metadata blocks "
365 "(releasing %d blocks with reserved %d data blocks)",
366 inode->i_ino, ei->i_allocated_meta_blocks,
367 ei->i_reserved_meta_blocks, used,
368 ei->i_reserved_data_blocks);
369 WARN_ON(1);
370 ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
371 }
372
373 /* Update per-inode reservations */
374 ei->i_reserved_data_blocks -= used;
375 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
376 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
377 used + ei->i_allocated_meta_blocks);
378 ei->i_allocated_meta_blocks = 0;
379
380 if (ei->i_reserved_data_blocks == 0) {
381 /*
382 * We can release all of the reserved metadata blocks
383 * only when we have written all of the delayed
384 * allocation blocks.
385 */
386 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
387 ei->i_reserved_meta_blocks);
388 ei->i_reserved_meta_blocks = 0;
389 ei->i_da_metadata_calc_len = 0;
390 }
391 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
392
393 /* Update quota subsystem for data blocks */
394 if (quota_claim)
395 dquot_claim_block(inode, EXT4_C2B(sbi, used));
396 else {
397 /*
398 * We did fallocate with an offset that is already delayed
399 * allocated. So on delayed allocated writeback we should
400 * not re-claim the quota for fallocated blocks.
401 */
402 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
403 }
404
405 /*
406 * If we have done all the pending block allocations and if
407 * there aren't any writers on the inode, we can discard the
408 * inode's preallocations.
409 */
410 if ((ei->i_reserved_data_blocks == 0) &&
411 (atomic_read(&inode->i_writecount) == 0))
412 ext4_discard_preallocations(inode);
413 }
414
415 static int __check_block_validity(struct inode *inode, const char *func,
416 unsigned int line,
417 struct ext4_map_blocks *map)
418 {
419 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
420 map->m_len)) {
421 ext4_error_inode(inode, func, line, map->m_pblk,
422 "lblock %lu mapped to illegal pblock "
423 "(length %d)", (unsigned long) map->m_lblk,
424 map->m_len);
425 return -EIO;
426 }
427 return 0;
428 }
429
430 #define check_block_validity(inode, map) \
431 __check_block_validity((inode), __func__, __LINE__, (map))
432
433 /*
434 * Return the number of contiguous dirty pages in a given inode
435 * starting at page frame idx.
436 */
437 static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
438 unsigned int max_pages)
439 {
440 struct address_space *mapping = inode->i_mapping;
441 pgoff_t index;
442 struct pagevec pvec;
443 pgoff_t num = 0;
444 int i, nr_pages, done = 0;
445
446 if (max_pages == 0)
447 return 0;
448 pagevec_init(&pvec, 0);
449 while (!done) {
450 index = idx;
451 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
452 PAGECACHE_TAG_DIRTY,
453 (pgoff_t)PAGEVEC_SIZE);
454 if (nr_pages == 0)
455 break;
456 for (i = 0; i < nr_pages; i++) {
457 struct page *page = pvec.pages[i];
458 struct buffer_head *bh, *head;
459
460 lock_page(page);
461 if (unlikely(page->mapping != mapping) ||
462 !PageDirty(page) ||
463 PageWriteback(page) ||
464 page->index != idx) {
465 done = 1;
466 unlock_page(page);
467 break;
468 }
469 if (page_has_buffers(page)) {
470 bh = head = page_buffers(page);
471 do {
472 if (!buffer_delay(bh) &&
473 !buffer_unwritten(bh))
474 done = 1;
475 bh = bh->b_this_page;
476 } while (!done && (bh != head));
477 }
478 unlock_page(page);
479 if (done)
480 break;
481 idx++;
482 num++;
483 if (num >= max_pages) {
484 done = 1;
485 break;
486 }
487 }
488 pagevec_release(&pvec);
489 }
490 return num;
491 }
492
493 #ifdef ES_AGGRESSIVE_TEST
494 static void ext4_map_blocks_es_recheck(handle_t *handle,
495 struct inode *inode,
496 struct ext4_map_blocks *es_map,
497 struct ext4_map_blocks *map,
498 int flags)
499 {
500 int retval;
501
502 map->m_flags = 0;
503 /*
504 * There is a race window that the result is not the same.
505 * e.g. xfstests #223 when dioread_nolock enables. The reason
506 * is that we lookup a block mapping in extent status tree with
507 * out taking i_data_sem. So at the time the unwritten extent
508 * could be converted.
509 */
510 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
511 down_read((&EXT4_I(inode)->i_data_sem));
512 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
513 retval = ext4_ext_map_blocks(handle, inode, map, flags &
514 EXT4_GET_BLOCKS_KEEP_SIZE);
515 } else {
516 retval = ext4_ind_map_blocks(handle, inode, map, flags &
517 EXT4_GET_BLOCKS_KEEP_SIZE);
518 }
519 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
520 up_read((&EXT4_I(inode)->i_data_sem));
521 /*
522 * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
523 * because it shouldn't be marked in es_map->m_flags.
524 */
525 map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY);
526
527 /*
528 * We don't check m_len because extent will be collpased in status
529 * tree. So the m_len might not equal.
530 */
531 if (es_map->m_lblk != map->m_lblk ||
532 es_map->m_flags != map->m_flags ||
533 es_map->m_pblk != map->m_pblk) {
534 printk("ES cache assertation failed for inode: %lu "
535 "es_cached ex [%d/%d/%llu/%x] != "
536 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
537 inode->i_ino, es_map->m_lblk, es_map->m_len,
538 es_map->m_pblk, es_map->m_flags, map->m_lblk,
539 map->m_len, map->m_pblk, map->m_flags,
540 retval, flags);
541 }
542 }
543 #endif /* ES_AGGRESSIVE_TEST */
544
545 /*
546 * The ext4_map_blocks() function tries to look up the requested blocks,
547 * and returns if the blocks are already mapped.
548 *
549 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
550 * and store the allocated blocks in the result buffer head and mark it
551 * mapped.
552 *
553 * If file type is extents based, it will call ext4_ext_map_blocks(),
554 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
555 * based files
556 *
557 * On success, it returns the number of blocks being mapped or allocate.
558 * if create==0 and the blocks are pre-allocated and uninitialized block,
559 * the result buffer head is unmapped. If the create ==1, it will make sure
560 * the buffer head is mapped.
561 *
562 * It returns 0 if plain look up failed (blocks have not been allocated), in
563 * that case, buffer head is unmapped
564 *
565 * It returns the error in case of allocation failure.
566 */
567 int ext4_map_blocks(handle_t *handle, struct inode *inode,
568 struct ext4_map_blocks *map, int flags)
569 {
570 struct extent_status es;
571 int retval;
572 #ifdef ES_AGGRESSIVE_TEST
573 struct ext4_map_blocks orig_map;
574
575 memcpy(&orig_map, map, sizeof(*map));
576 #endif
577
578 map->m_flags = 0;
579 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
580 "logical block %lu\n", inode->i_ino, flags, map->m_len,
581 (unsigned long) map->m_lblk);
582
583 /* Lookup extent status tree firstly */
584 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
585 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
586 map->m_pblk = ext4_es_pblock(&es) +
587 map->m_lblk - es.es_lblk;
588 map->m_flags |= ext4_es_is_written(&es) ?
589 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
590 retval = es.es_len - (map->m_lblk - es.es_lblk);
591 if (retval > map->m_len)
592 retval = map->m_len;
593 map->m_len = retval;
594 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
595 retval = 0;
596 } else {
597 BUG_ON(1);
598 }
599 #ifdef ES_AGGRESSIVE_TEST
600 ext4_map_blocks_es_recheck(handle, inode, map,
601 &orig_map, flags);
602 #endif
603 goto found;
604 }
605
606 /*
607 * Try to see if we can get the block without requesting a new
608 * file system block.
609 */
610 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
611 down_read((&EXT4_I(inode)->i_data_sem));
612 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
613 retval = ext4_ext_map_blocks(handle, inode, map, flags &
614 EXT4_GET_BLOCKS_KEEP_SIZE);
615 } else {
616 retval = ext4_ind_map_blocks(handle, inode, map, flags &
617 EXT4_GET_BLOCKS_KEEP_SIZE);
618 }
619 if (retval > 0) {
620 int ret;
621 unsigned long long status;
622
623 #ifdef ES_AGGRESSIVE_TEST
624 if (retval != map->m_len) {
625 printk("ES len assertation failed for inode: %lu "
626 "retval %d != map->m_len %d "
627 "in %s (lookup)\n", inode->i_ino, retval,
628 map->m_len, __func__);
629 }
630 #endif
631
632 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
633 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
634 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
635 !(status & EXTENT_STATUS_WRITTEN) &&
636 ext4_find_delalloc_range(inode, map->m_lblk,
637 map->m_lblk + map->m_len - 1))
638 status |= EXTENT_STATUS_DELAYED;
639 ret = ext4_es_insert_extent(inode, map->m_lblk,
640 map->m_len, map->m_pblk, status);
641 if (ret < 0)
642 retval = ret;
643 }
644 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
645 up_read((&EXT4_I(inode)->i_data_sem));
646
647 found:
648 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
649 int ret = check_block_validity(inode, map);
650 if (ret != 0)
651 return ret;
652 }
653
654 /* If it is only a block(s) look up */
655 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
656 return retval;
657
658 /*
659 * Returns if the blocks have already allocated
660 *
661 * Note that if blocks have been preallocated
662 * ext4_ext_get_block() returns the create = 0
663 * with buffer head unmapped.
664 */
665 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
666 return retval;
667
668 /*
669 * Here we clear m_flags because after allocating an new extent,
670 * it will be set again.
671 */
672 map->m_flags &= ~EXT4_MAP_FLAGS;
673
674 /*
675 * New blocks allocate and/or writing to uninitialized extent
676 * will possibly result in updating i_data, so we take
677 * the write lock of i_data_sem, and call get_blocks()
678 * with create == 1 flag.
679 */
680 down_write((&EXT4_I(inode)->i_data_sem));
681
682 /*
683 * if the caller is from delayed allocation writeout path
684 * we have already reserved fs blocks for allocation
685 * let the underlying get_block() function know to
686 * avoid double accounting
687 */
688 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
689 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
690 /*
691 * We need to check for EXT4 here because migrate
692 * could have changed the inode type in between
693 */
694 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
695 retval = ext4_ext_map_blocks(handle, inode, map, flags);
696 } else {
697 retval = ext4_ind_map_blocks(handle, inode, map, flags);
698
699 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
700 /*
701 * We allocated new blocks which will result in
702 * i_data's format changing. Force the migrate
703 * to fail by clearing migrate flags
704 */
705 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
706 }
707
708 /*
709 * Update reserved blocks/metadata blocks after successful
710 * block allocation which had been deferred till now. We don't
711 * support fallocate for non extent files. So we can update
712 * reserve space here.
713 */
714 if ((retval > 0) &&
715 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
716 ext4_da_update_reserve_space(inode, retval, 1);
717 }
718 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
719 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
720
721 if (retval > 0) {
722 int ret;
723 unsigned long long status;
724
725 #ifdef ES_AGGRESSIVE_TEST
726 if (retval != map->m_len) {
727 printk("ES len assertation failed for inode: %lu "
728 "retval %d != map->m_len %d "
729 "in %s (allocation)\n", inode->i_ino, retval,
730 map->m_len, __func__);
731 }
732 #endif
733
734 /*
735 * If the extent has been zeroed out, we don't need to update
736 * extent status tree.
737 */
738 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
739 ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
740 if (ext4_es_is_written(&es))
741 goto has_zeroout;
742 }
743 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
744 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
745 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
746 !(status & EXTENT_STATUS_WRITTEN) &&
747 ext4_find_delalloc_range(inode, map->m_lblk,
748 map->m_lblk + map->m_len - 1))
749 status |= EXTENT_STATUS_DELAYED;
750 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
751 map->m_pblk, status);
752 if (ret < 0)
753 retval = ret;
754 }
755
756 has_zeroout:
757 up_write((&EXT4_I(inode)->i_data_sem));
758 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
759 int ret = check_block_validity(inode, map);
760 if (ret != 0)
761 return ret;
762
763 /*
764 * Inodes with freshly allocated blocks where contents will be
765 * visible after transaction commit must be on transaction's
766 * ordered data list.
767 */
768 if (map->m_flags & EXT4_MAP_NEW &&
769 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
770 !IS_NOQUOTA(inode) &&
771 ext4_should_order_data(inode)) {
772 ret = ext4_jbd2_file_inode(handle, inode);
773 if (ret)
774 return ret;
775 }
776 }
777 return retval;
778 }
779
780 /* Maximum number of blocks we map for direct IO at once. */
781 #define DIO_MAX_BLOCKS 4096
782
783 static int _ext4_get_block(struct inode *inode, sector_t iblock,
784 struct buffer_head *bh, int flags)
785 {
786 handle_t *handle = ext4_journal_current_handle();
787 struct ext4_map_blocks map;
788 int ret = 0, started = 0;
789 int dio_credits;
790
791 if (ext4_has_inline_data(inode))
792 return -ERANGE;
793
794 map.m_lblk = iblock;
795 map.m_len = bh->b_size >> inode->i_blkbits;
796
797 if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
798 /* Direct IO write... */
799 if (map.m_len > DIO_MAX_BLOCKS)
800 map.m_len = DIO_MAX_BLOCKS;
801 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
802 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
803 dio_credits);
804 if (IS_ERR(handle)) {
805 ret = PTR_ERR(handle);
806 return ret;
807 }
808 started = 1;
809 }
810
811 ret = ext4_map_blocks(handle, inode, &map, flags);
812 if (ret > 0) {
813 map_bh(bh, inode->i_sb, map.m_pblk);
814 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
815 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
816 ret = 0;
817 }
818 if (started)
819 ext4_journal_stop(handle);
820 return ret;
821 }
822
823 int ext4_get_block(struct inode *inode, sector_t iblock,
824 struct buffer_head *bh, int create)
825 {
826 return _ext4_get_block(inode, iblock, bh,
827 create ? EXT4_GET_BLOCKS_CREATE : 0);
828 }
829
830 /*
831 * `handle' can be NULL if create is zero
832 */
833 struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
834 ext4_lblk_t block, int create, int *errp)
835 {
836 struct ext4_map_blocks map;
837 struct buffer_head *bh;
838 int fatal = 0, err;
839
840 J_ASSERT(handle != NULL || create == 0);
841
842 map.m_lblk = block;
843 map.m_len = 1;
844 err = ext4_map_blocks(handle, inode, &map,
845 create ? EXT4_GET_BLOCKS_CREATE : 0);
846
847 /* ensure we send some value back into *errp */
848 *errp = 0;
849
850 if (create && err == 0)
851 err = -ENOSPC; /* should never happen */
852 if (err < 0)
853 *errp = err;
854 if (err <= 0)
855 return NULL;
856
857 bh = sb_getblk(inode->i_sb, map.m_pblk);
858 if (unlikely(!bh)) {
859 *errp = -ENOMEM;
860 return NULL;
861 }
862 if (map.m_flags & EXT4_MAP_NEW) {
863 J_ASSERT(create != 0);
864 J_ASSERT(handle != NULL);
865
866 /*
867 * Now that we do not always journal data, we should
868 * keep in mind whether this should always journal the
869 * new buffer as metadata. For now, regular file
870 * writes use ext4_get_block instead, so it's not a
871 * problem.
872 */
873 lock_buffer(bh);
874 BUFFER_TRACE(bh, "call get_create_access");
875 fatal = ext4_journal_get_create_access(handle, bh);
876 if (!fatal && !buffer_uptodate(bh)) {
877 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
878 set_buffer_uptodate(bh);
879 }
880 unlock_buffer(bh);
881 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
882 err = ext4_handle_dirty_metadata(handle, inode, bh);
883 if (!fatal)
884 fatal = err;
885 } else {
886 BUFFER_TRACE(bh, "not a new buffer");
887 }
888 if (fatal) {
889 *errp = fatal;
890 brelse(bh);
891 bh = NULL;
892 }
893 return bh;
894 }
895
896 struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
897 ext4_lblk_t block, int create, int *err)
898 {
899 struct buffer_head *bh;
900
901 bh = ext4_getblk(handle, inode, block, create, err);
902 if (!bh)
903 return bh;
904 if (buffer_uptodate(bh))
905 return bh;
906 ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
907 wait_on_buffer(bh);
908 if (buffer_uptodate(bh))
909 return bh;
910 put_bh(bh);
911 *err = -EIO;
912 return NULL;
913 }
914
915 int ext4_walk_page_buffers(handle_t *handle,
916 struct buffer_head *head,
917 unsigned from,
918 unsigned to,
919 int *partial,
920 int (*fn)(handle_t *handle,
921 struct buffer_head *bh))
922 {
923 struct buffer_head *bh;
924 unsigned block_start, block_end;
925 unsigned blocksize = head->b_size;
926 int err, ret = 0;
927 struct buffer_head *next;
928
929 for (bh = head, block_start = 0;
930 ret == 0 && (bh != head || !block_start);
931 block_start = block_end, bh = next) {
932 next = bh->b_this_page;
933 block_end = block_start + blocksize;
934 if (block_end <= from || block_start >= to) {
935 if (partial && !buffer_uptodate(bh))
936 *partial = 1;
937 continue;
938 }
939 err = (*fn)(handle, bh);
940 if (!ret)
941 ret = err;
942 }
943 return ret;
944 }
945
946 /*
947 * To preserve ordering, it is essential that the hole instantiation and
948 * the data write be encapsulated in a single transaction. We cannot
949 * close off a transaction and start a new one between the ext4_get_block()
950 * and the commit_write(). So doing the jbd2_journal_start at the start of
951 * prepare_write() is the right place.
952 *
953 * Also, this function can nest inside ext4_writepage(). In that case, we
954 * *know* that ext4_writepage() has generated enough buffer credits to do the
955 * whole page. So we won't block on the journal in that case, which is good,
956 * because the caller may be PF_MEMALLOC.
957 *
958 * By accident, ext4 can be reentered when a transaction is open via
959 * quota file writes. If we were to commit the transaction while thus
960 * reentered, there can be a deadlock - we would be holding a quota
961 * lock, and the commit would never complete if another thread had a
962 * transaction open and was blocking on the quota lock - a ranking
963 * violation.
964 *
965 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
966 * will _not_ run commit under these circumstances because handle->h_ref
967 * is elevated. We'll still have enough credits for the tiny quotafile
968 * write.
969 */
970 int do_journal_get_write_access(handle_t *handle,
971 struct buffer_head *bh)
972 {
973 int dirty = buffer_dirty(bh);
974 int ret;
975
976 if (!buffer_mapped(bh) || buffer_freed(bh))
977 return 0;
978 /*
979 * __block_write_begin() could have dirtied some buffers. Clean
980 * the dirty bit as jbd2_journal_get_write_access() could complain
981 * otherwise about fs integrity issues. Setting of the dirty bit
982 * by __block_write_begin() isn't a real problem here as we clear
983 * the bit before releasing a page lock and thus writeback cannot
984 * ever write the buffer.
985 */
986 if (dirty)
987 clear_buffer_dirty(bh);
988 ret = ext4_journal_get_write_access(handle, bh);
989 if (!ret && dirty)
990 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
991 return ret;
992 }
993
994 static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
995 struct buffer_head *bh_result, int create);
996 static int ext4_write_begin(struct file *file, struct address_space *mapping,
997 loff_t pos, unsigned len, unsigned flags,
998 struct page **pagep, void **fsdata)
999 {
1000 struct inode *inode = mapping->host;
1001 int ret, needed_blocks;
1002 handle_t *handle;
1003 int retries = 0;
1004 struct page *page;
1005 pgoff_t index;
1006 unsigned from, to;
1007 #if defined(FEATURE_STORAGE_PID_LOGGER)
1008 extern unsigned char *page_logger;
1009 struct page_pid_logger *tmp_logger;
1010 unsigned long page_index;
1011 extern spinlock_t g_locker;
1012 unsigned long g_flags;
1013 #endif
1014
1015 trace_ext4_write_begin(inode, pos, len, flags);
1016 /*
1017 * Reserve one block more for addition to orphan list in case
1018 * we allocate blocks but write fails for some reason
1019 */
1020 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
1021 index = pos >> PAGE_CACHE_SHIFT;
1022 from = pos & (PAGE_CACHE_SIZE - 1);
1023 to = from + len;
1024
1025 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1026 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1027 flags, pagep);
1028 if (ret < 0)
1029 return ret;
1030 if (ret == 1)
1031 return 0;
1032 }
1033
1034 /*
1035 * grab_cache_page_write_begin() can take a long time if the
1036 * system is thrashing due to memory pressure, or if the page
1037 * is being written back. So grab it first before we start
1038 * the transaction handle. This also allows us to allocate
1039 * the page (if needed) without using GFP_NOFS.
1040 */
1041 retry_grab:
1042 page = grab_cache_page_write_begin(mapping, index, flags);
1043 if (!page)
1044 return -ENOMEM;
1045 unlock_page(page);
1046
1047 retry_journal:
1048 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
1049 if (IS_ERR(handle)) {
1050 page_cache_release(page);
1051 return PTR_ERR(handle);
1052 }
1053
1054 lock_page(page);
1055 if (page->mapping != mapping) {
1056 /* The page got truncated from under us */
1057 unlock_page(page);
1058 page_cache_release(page);
1059 ext4_journal_stop(handle);
1060 goto retry_grab;
1061 }
1062 /* In case writeback began while the page was unlocked */
1063 wait_for_stable_page(page);
1064
1065 if (ext4_should_dioread_nolock(inode))
1066 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
1067 else
1068 ret = __block_write_begin(page, pos, len, ext4_get_block);
1069
1070 if (!ret && ext4_should_journal_data(inode)) {
1071 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1072 from, to, NULL,
1073 do_journal_get_write_access);
1074 }
1075
1076 if (ret) {
1077 unlock_page(page);
1078 /*
1079 * __block_write_begin may have instantiated a few blocks
1080 * outside i_size. Trim these off again. Don't need
1081 * i_size_read because we hold i_mutex.
1082 *
1083 * Add inode to orphan list in case we crash before
1084 * truncate finishes
1085 */
1086 if (pos + len > inode->i_size && ext4_can_truncate(inode))
1087 ext4_orphan_add(handle, inode);
1088
1089 ext4_journal_stop(handle);
1090 if (pos + len > inode->i_size) {
1091 ext4_truncate_failed_write(inode);
1092 /*
1093 * If truncate failed early the inode might
1094 * still be on the orphan list; we need to
1095 * make sure the inode is removed from the
1096 * orphan list in that case.
1097 */
1098 if (inode->i_nlink)
1099 ext4_orphan_del(NULL, inode);
1100 }
1101
1102 if (ret == -ENOSPC &&
1103 ext4_should_retry_alloc(inode->i_sb, &retries))
1104 goto retry_journal;
1105 page_cache_release(page);
1106 return ret;
1107 }
1108 *pagep = page;
1109 #if defined(FEATURE_STORAGE_PID_LOGGER)
1110 if( page_logger && (*pagep)) {
1111 //#if defined(CONFIG_FLATMEM)
1112 //page_index = (unsigned long)((*pagep) - mem_map) ;
1113 //#else
1114 page_index = (unsigned long)(__page_to_pfn(*pagep))- PHYS_PFN_OFFSET;
1115 //#endif
1116 tmp_logger =((struct page_pid_logger *)page_logger) + page_index;
1117 spin_lock_irqsave(&g_locker, g_flags);
1118 if( page_index < num_physpages) {
1119 if( tmp_logger->pid1 == 0XFFFF)
1120 tmp_logger->pid1 = current->pid;
1121 else if( tmp_logger->pid1 != current->pid)
1122 tmp_logger->pid2 = current->pid;
1123 }
1124 spin_unlock_irqrestore(&g_locker, g_flags);
1125 }
1126 #endif
1127 return ret;
1128 }
1129
1130 /* For write_end() in data=journal mode */
1131 static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1132 {
1133 int ret;
1134 if (!buffer_mapped(bh) || buffer_freed(bh))
1135 return 0;
1136 set_buffer_uptodate(bh);
1137 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1138 clear_buffer_meta(bh);
1139 clear_buffer_prio(bh);
1140 return ret;
1141 }
1142
1143 /*
1144 * We need to pick up the new inode size which generic_commit_write gave us
1145 * `file' can be NULL - eg, when called from page_symlink().
1146 *
1147 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1148 * buffers are managed internally.
1149 */
1150 static int ext4_write_end(struct file *file,
1151 struct address_space *mapping,
1152 loff_t pos, unsigned len, unsigned copied,
1153 struct page *page, void *fsdata)
1154 {
1155 handle_t *handle = ext4_journal_current_handle();
1156 struct inode *inode = mapping->host;
1157 int ret = 0, ret2;
1158 int i_size_changed = 0;
1159
1160 trace_ext4_write_end(inode, pos, len, copied);
1161 if (ext4_has_inline_data(inode)) {
1162 ret = ext4_write_inline_data_end(inode, pos, len,
1163 copied, page);
1164 if (ret < 0)
1165 goto errout;
1166 copied = ret;
1167 } else
1168 copied = block_write_end(file, mapping, pos,
1169 len, copied, page, fsdata);
1170
1171 /*
1172 * No need to use i_size_read() here, the i_size
1173 * cannot change under us because we hole i_mutex.
1174 *
1175 * But it's important to update i_size while still holding page lock:
1176 * page writeout could otherwise come in and zero beyond i_size.
1177 */
1178 if (pos + copied > inode->i_size) {
1179 i_size_write(inode, pos + copied);
1180 i_size_changed = 1;
1181 }
1182
1183 if (pos + copied > EXT4_I(inode)->i_disksize) {
1184 /* We need to mark inode dirty even if
1185 * new_i_size is less that inode->i_size
1186 * but greater than i_disksize. (hint delalloc)
1187 */
1188 ext4_update_i_disksize(inode, (pos + copied));
1189 i_size_changed = 1;
1190 }
1191 unlock_page(page);
1192 page_cache_release(page);
1193
1194 /*
1195 * Don't mark the inode dirty under page lock. First, it unnecessarily
1196 * makes the holding time of page lock longer. Second, it forces lock
1197 * ordering of page lock and transaction start for journaling
1198 * filesystems.
1199 */
1200 if (i_size_changed)
1201 ext4_mark_inode_dirty(handle, inode);
1202
1203 if (copied < 0)
1204 ret = copied;
1205 if (pos + len > inode->i_size && ext4_can_truncate(inode))
1206 /* if we have allocated more blocks and copied
1207 * less. We will have blocks allocated outside
1208 * inode->i_size. So truncate them
1209 */
1210 ext4_orphan_add(handle, inode);
1211 errout:
1212 ret2 = ext4_journal_stop(handle);
1213 if (!ret)
1214 ret = ret2;
1215
1216 if (pos + len > inode->i_size) {
1217 ext4_truncate_failed_write(inode);
1218 /*
1219 * If truncate failed early the inode might still be
1220 * on the orphan list; we need to make sure the inode
1221 * is removed from the orphan list in that case.
1222 */
1223 if (inode->i_nlink)
1224 ext4_orphan_del(NULL, inode);
1225 }
1226
1227 return ret ? ret : copied;
1228 }
1229
1230 static int ext4_journalled_write_end(struct file *file,
1231 struct address_space *mapping,
1232 loff_t pos, unsigned len, unsigned copied,
1233 struct page *page, void *fsdata)
1234 {
1235 handle_t *handle = ext4_journal_current_handle();
1236 struct inode *inode = mapping->host;
1237 int ret = 0, ret2;
1238 int partial = 0;
1239 unsigned from, to;
1240 loff_t new_i_size;
1241
1242 trace_ext4_journalled_write_end(inode, pos, len, copied);
1243 from = pos & (PAGE_CACHE_SIZE - 1);
1244 to = from + len;
1245
1246 BUG_ON(!ext4_handle_valid(handle));
1247
1248 if (ext4_has_inline_data(inode))
1249 copied = ext4_write_inline_data_end(inode, pos, len,
1250 copied, page);
1251 else {
1252 if (copied < len) {
1253 if (!PageUptodate(page))
1254 copied = 0;
1255 page_zero_new_buffers(page, from+copied, to);
1256 }
1257
1258 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1259 to, &partial, write_end_fn);
1260 if (!partial)
1261 SetPageUptodate(page);
1262 }
1263 new_i_size = pos + copied;
1264 if (new_i_size > inode->i_size)
1265 i_size_write(inode, pos+copied);
1266 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
1267 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1268 if (new_i_size > EXT4_I(inode)->i_disksize) {
1269 ext4_update_i_disksize(inode, new_i_size);
1270 ret2 = ext4_mark_inode_dirty(handle, inode);
1271 if (!ret)
1272 ret = ret2;
1273 }
1274
1275 unlock_page(page);
1276 page_cache_release(page);
1277 if (pos + len > inode->i_size && ext4_can_truncate(inode))
1278 /* if we have allocated more blocks and copied
1279 * less. We will have blocks allocated outside
1280 * inode->i_size. So truncate them
1281 */
1282 ext4_orphan_add(handle, inode);
1283
1284 ret2 = ext4_journal_stop(handle);
1285 if (!ret)
1286 ret = ret2;
1287 if (pos + len > inode->i_size) {
1288 ext4_truncate_failed_write(inode);
1289 /*
1290 * If truncate failed early the inode might still be
1291 * on the orphan list; we need to make sure the inode
1292 * is removed from the orphan list in that case.
1293 */
1294 if (inode->i_nlink)
1295 ext4_orphan_del(NULL, inode);
1296 }
1297
1298 return ret ? ret : copied;
1299 }
1300
1301 /*
1302 * Reserve a metadata for a single block located at lblock
1303 */
1304 static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
1305 {
1306 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1307 struct ext4_inode_info *ei = EXT4_I(inode);
1308 unsigned int md_needed;
1309 ext4_lblk_t save_last_lblock;
1310 int save_len;
1311
1312 /*
1313 * recalculate the amount of metadata blocks to reserve
1314 * in order to allocate nrblocks
1315 * worse case is one extent per block
1316 */
1317 spin_lock(&ei->i_block_reservation_lock);
1318 /*
1319 * ext4_calc_metadata_amount() has side effects, which we have
1320 * to be prepared undo if we fail to claim space.
1321 */
1322 save_len = ei->i_da_metadata_calc_len;
1323 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1324 md_needed = EXT4_NUM_B2C(sbi,
1325 ext4_calc_metadata_amount(inode, lblock));
1326 trace_ext4_da_reserve_space(inode, md_needed);
1327
1328 /*
1329 * We do still charge estimated metadata to the sb though;
1330 * we cannot afford to run out of free blocks.
1331 */
1332 if (ext4_claim_free_clusters(sbi, md_needed, 0)) {
1333 ei->i_da_metadata_calc_len = save_len;
1334 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1335 spin_unlock(&ei->i_block_reservation_lock);
1336 return -ENOSPC;
1337 }
1338 ei->i_reserved_meta_blocks += md_needed;
1339 spin_unlock(&ei->i_block_reservation_lock);
1340
1341 return 0; /* success */
1342 }
1343
1344 /*
1345 * Reserve a single cluster located at lblock
1346 */
1347 static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
1348 {
1349 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1350 struct ext4_inode_info *ei = EXT4_I(inode);
1351 unsigned int md_needed;
1352 int ret;
1353 ext4_lblk_t save_last_lblock;
1354 int save_len;
1355
1356 /*
1357 * We will charge metadata quota at writeout time; this saves
1358 * us from metadata over-estimation, though we may go over by
1359 * a small amount in the end. Here we just reserve for data.
1360 */
1361 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
1362 if (ret)
1363 return ret;
1364
1365 /*
1366 * recalculate the amount of metadata blocks to reserve
1367 * in order to allocate nrblocks
1368 * worse case is one extent per block
1369 */
1370 spin_lock(&ei->i_block_reservation_lock);
1371 /*
1372 * ext4_calc_metadata_amount() has side effects, which we have
1373 * to be prepared undo if we fail to claim space.
1374 */
1375 save_len = ei->i_da_metadata_calc_len;
1376 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1377 md_needed = EXT4_NUM_B2C(sbi,
1378 ext4_calc_metadata_amount(inode, lblock));
1379 trace_ext4_da_reserve_space(inode, md_needed);
1380
1381 /*
1382 * We do still charge estimated metadata to the sb though;
1383 * we cannot afford to run out of free blocks.
1384 */
1385 if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
1386 ei->i_da_metadata_calc_len = save_len;
1387 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1388 spin_unlock(&ei->i_block_reservation_lock);
1389 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
1390 return -ENOSPC;
1391 }
1392 ei->i_reserved_data_blocks++;
1393 ei->i_reserved_meta_blocks += md_needed;
1394 spin_unlock(&ei->i_block_reservation_lock);
1395
1396 return 0; /* success */
1397 }
1398
1399 static void ext4_da_release_space(struct inode *inode, int to_free)
1400 {
1401 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1402 struct ext4_inode_info *ei = EXT4_I(inode);
1403
1404 if (!to_free)
1405 return; /* Nothing to release, exit */
1406
1407 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1408
1409 trace_ext4_da_release_space(inode, to_free);
1410 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
1411 /*
1412 * if there aren't enough reserved blocks, then the
1413 * counter is messed up somewhere. Since this
1414 * function is called from invalidate page, it's
1415 * harmless to return without any action.
1416 */
1417 ext4_warning(inode->i_sb, "ext4_da_release_space: "
1418 "ino %lu, to_free %d with only %d reserved "
1419 "data blocks", inode->i_ino, to_free,
1420 ei->i_reserved_data_blocks);
1421 WARN_ON(1);
1422 to_free = ei->i_reserved_data_blocks;
1423 }
1424 ei->i_reserved_data_blocks -= to_free;
1425
1426 if (ei->i_reserved_data_blocks == 0) {
1427 /*
1428 * We can release all of the reserved metadata blocks
1429 * only when we have written all of the delayed
1430 * allocation blocks.
1431 * Note that in case of bigalloc, i_reserved_meta_blocks,
1432 * i_reserved_data_blocks, etc. refer to number of clusters.
1433 */
1434 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
1435 ei->i_reserved_meta_blocks);
1436 ei->i_reserved_meta_blocks = 0;
1437 ei->i_da_metadata_calc_len = 0;
1438 }
1439
1440 /* update fs dirty data blocks counter */
1441 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
1442
1443 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1444
1445 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
1446 }
1447
1448 static void ext4_da_page_release_reservation(struct page *page,
1449 unsigned long offset)
1450 {
1451 int to_release = 0, contiguous_blks = 0;
1452 struct buffer_head *head, *bh;
1453 unsigned int curr_off = 0;
1454 struct inode *inode = page->mapping->host;
1455 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1456 int num_clusters;
1457 ext4_fsblk_t lblk;
1458
1459 head = page_buffers(page);
1460 bh = head;
1461 do {
1462 unsigned int next_off = curr_off + bh->b_size;
1463
1464 if ((offset <= curr_off) && (buffer_delay(bh))) {
1465 to_release++;
1466 contiguous_blks++;
1467 clear_buffer_delay(bh);
1468 } else if (contiguous_blks) {
1469 lblk = page->index <<
1470 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1471 lblk += (curr_off >> inode->i_blkbits) -
1472 contiguous_blks;
1473 ext4_es_remove_extent(inode, lblk, contiguous_blks);
1474 contiguous_blks = 0;
1475 }
1476 curr_off = next_off;
1477 } while ((bh = bh->b_this_page) != head);
1478
1479 if (contiguous_blks) {
1480 lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1481 lblk += (curr_off >> inode->i_blkbits) - contiguous_blks;
1482 ext4_es_remove_extent(inode, lblk, contiguous_blks);
1483 }
1484
1485 /* If we have released all the blocks belonging to a cluster, then we
1486 * need to release the reserved space for that cluster. */
1487 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1488 while (num_clusters > 0) {
1489 lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
1490 ((num_clusters - 1) << sbi->s_cluster_bits);
1491 if (sbi->s_cluster_ratio == 1 ||
1492 !ext4_find_delalloc_cluster(inode, lblk))
1493 ext4_da_release_space(inode, 1);
1494
1495 num_clusters--;
1496 }
1497 }
1498
1499 /*
1500 * Delayed allocation stuff
1501 */
1502
1503 /*
1504 * mpage_da_submit_io - walks through extent of pages and try to write
1505 * them with writepage() call back
1506 *
1507 * @mpd->inode: inode
1508 * @mpd->first_page: first page of the extent
1509 * @mpd->next_page: page after the last page of the extent
1510 *
1511 * By the time mpage_da_submit_io() is called we expect all blocks
1512 * to be allocated. this may be wrong if allocation failed.
1513 *
1514 * As pages are already locked by write_cache_pages(), we can't use it
1515 */
1516 static int mpage_da_submit_io(struct mpage_da_data *mpd,
1517 struct ext4_map_blocks *map)
1518 {
1519 struct pagevec pvec;
1520 unsigned long index, end;
1521 int ret = 0, err, nr_pages, i;
1522 struct inode *inode = mpd->inode;
1523 struct address_space *mapping = inode->i_mapping;
1524 loff_t size = i_size_read(inode);
1525 unsigned int len, block_start;
1526 struct buffer_head *bh, *page_bufs = NULL;
1527 sector_t pblock = 0, cur_logical = 0;
1528 struct ext4_io_submit io_submit;
1529
1530 BUG_ON(mpd->next_page <= mpd->first_page);
1531 memset(&io_submit, 0, sizeof(io_submit));
1532 /*
1533 * We need to start from the first_page to the next_page - 1
1534 * to make sure we also write the mapped dirty buffer_heads.
1535 * If we look at mpd->b_blocknr we would only be looking
1536 * at the currently mapped buffer_heads.
1537 */
1538 index = mpd->first_page;
1539 end = mpd->next_page - 1;
1540
1541 pagevec_init(&pvec, 0);
1542 while (index <= end) {
1543 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1544 if (nr_pages == 0)
1545 break;
1546 for (i = 0; i < nr_pages; i++) {
1547 int skip_page = 0;
1548 struct page *page = pvec.pages[i];
1549
1550 index = page->index;
1551 if (index > end)
1552 break;
1553
1554 if (index == size >> PAGE_CACHE_SHIFT)
1555 len = size & ~PAGE_CACHE_MASK;
1556 else
1557 len = PAGE_CACHE_SIZE;
1558 if (map) {
1559 cur_logical = index << (PAGE_CACHE_SHIFT -
1560 inode->i_blkbits);
1561 pblock = map->m_pblk + (cur_logical -
1562 map->m_lblk);
1563 }
1564 index++;
1565
1566 BUG_ON(!PageLocked(page));
1567 BUG_ON(PageWriteback(page));
1568
1569 bh = page_bufs = page_buffers(page);
1570 block_start = 0;
1571 do {
1572 if (map && (cur_logical >= map->m_lblk) &&
1573 (cur_logical <= (map->m_lblk +
1574 (map->m_len - 1)))) {
1575 if (buffer_delay(bh)) {
1576 clear_buffer_delay(bh);
1577 bh->b_blocknr = pblock;
1578 }
1579 if (buffer_unwritten(bh) ||
1580 buffer_mapped(bh))
1581 BUG_ON(bh->b_blocknr != pblock);
1582 if (map->m_flags & EXT4_MAP_UNINIT)
1583 set_buffer_uninit(bh);
1584 clear_buffer_unwritten(bh);
1585 }
1586
1587 /*
1588 * skip page if block allocation undone and
1589 * block is dirty
1590 */
1591 if (ext4_bh_delay_or_unwritten(NULL, bh))
1592 skip_page = 1;
1593 bh = bh->b_this_page;
1594 block_start += bh->b_size;
1595 cur_logical++;
1596 pblock++;
1597 } while (bh != page_bufs);
1598
1599 if (skip_page) {
1600 unlock_page(page);
1601 continue;
1602 }
1603
1604 clear_page_dirty_for_io(page);
1605 err = ext4_bio_write_page(&io_submit, page, len,
1606 mpd->wbc);
1607 if (!err)
1608 mpd->pages_written++;
1609 /*
1610 * In error case, we have to continue because
1611 * remaining pages are still locked
1612 */
1613 if (ret == 0)
1614 ret = err;
1615 }
1616 pagevec_release(&pvec);
1617 }
1618 ext4_io_submit(&io_submit);
1619 return ret;
1620 }
1621
1622 static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
1623 {
1624 int nr_pages, i;
1625 pgoff_t index, end;
1626 struct pagevec pvec;
1627 struct inode *inode = mpd->inode;
1628 struct address_space *mapping = inode->i_mapping;
1629 ext4_lblk_t start, last;
1630
1631 index = mpd->first_page;
1632 end = mpd->next_page - 1;
1633
1634 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1635 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1636 ext4_es_remove_extent(inode, start, last - start + 1);
1637
1638 pagevec_init(&pvec, 0);
1639 while (index <= end) {
1640 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1641 if (nr_pages == 0)
1642 break;
1643 for (i = 0; i < nr_pages; i++) {
1644 struct page *page = pvec.pages[i];
1645 if (page->index > end)
1646 break;
1647 BUG_ON(!PageLocked(page));
1648 BUG_ON(PageWriteback(page));
1649 block_invalidatepage(page, 0);
1650 ClearPageUptodate(page);
1651 unlock_page(page);
1652 }
1653 index = pvec.pages[nr_pages - 1]->index + 1;
1654 pagevec_release(&pvec);
1655 }
1656 return;
1657 }
1658
1659 static void ext4_print_free_blocks(struct inode *inode)
1660 {
1661 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1662 struct super_block *sb = inode->i_sb;
1663 struct ext4_inode_info *ei = EXT4_I(inode);
1664
1665 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
1666 EXT4_C2B(EXT4_SB(inode->i_sb),
1667 ext4_count_free_clusters(sb)));
1668 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1669 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
1670 (long long) EXT4_C2B(EXT4_SB(sb),
1671 percpu_counter_sum(&sbi->s_freeclusters_counter)));
1672 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
1673 (long long) EXT4_C2B(EXT4_SB(sb),
1674 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
1675 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1676 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
1677 ei->i_reserved_data_blocks);
1678 ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
1679 ei->i_reserved_meta_blocks);
1680 ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u",
1681 ei->i_allocated_meta_blocks);
1682 return;
1683 }
1684
1685 /*
1686 * mpage_da_map_and_submit - go through given space, map them
1687 * if necessary, and then submit them for I/O
1688 *
1689 * @mpd - bh describing space
1690 *
1691 * The function skips space we know is already mapped to disk blocks.
1692 *
1693 */
1694 static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
1695 {
1696 int err, blks, get_blocks_flags;
1697 struct ext4_map_blocks map, *mapp = NULL;
1698 sector_t next = mpd->b_blocknr;
1699 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
1700 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
1701 handle_t *handle = NULL;
1702
1703 /*
1704 * If the blocks are mapped already, or we couldn't accumulate
1705 * any blocks, then proceed immediately to the submission stage.
1706 */
1707 if ((mpd->b_size == 0) ||
1708 ((mpd->b_state & (1 << BH_Mapped)) &&
1709 !(mpd->b_state & (1 << BH_Delay)) &&
1710 !(mpd->b_state & (1 << BH_Unwritten))))
1711 goto submit_io;
1712
1713 handle = ext4_journal_current_handle();
1714 BUG_ON(!handle);
1715
1716 /*
1717 * Call ext4_map_blocks() to allocate any delayed allocation
1718 * blocks, or to convert an uninitialized extent to be
1719 * initialized (in the case where we have written into
1720 * one or more preallocated blocks).
1721 *
1722 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
1723 * indicate that we are on the delayed allocation path. This
1724 * affects functions in many different parts of the allocation
1725 * call path. This flag exists primarily because we don't
1726 * want to change *many* call functions, so ext4_map_blocks()
1727 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
1728 * inode's allocation semaphore is taken.
1729 *
1730 * If the blocks in questions were delalloc blocks, set
1731 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
1732 * variables are updated after the blocks have been allocated.
1733 */
1734 map.m_lblk = next;
1735 map.m_len = max_blocks;
1736 /*
1737 * We're in delalloc path and it is possible that we're going to
1738 * need more metadata blocks than previously reserved. However
1739 * we must not fail because we're in writeback and there is
1740 * nothing we can do about it so it might result in data loss.
1741 * So use reserved blocks to allocate metadata if possible.
1742 */
1743 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
1744 EXT4_GET_BLOCKS_METADATA_NOFAIL;
1745 if (ext4_should_dioread_nolock(mpd->inode))
1746 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
1747 if (mpd->b_state & (1 << BH_Delay))
1748 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
1749
1750
1751 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
1752 if (blks < 0) {
1753 struct super_block *sb = mpd->inode->i_sb;
1754
1755 err = blks;
1756 /*
1757 * If get block returns EAGAIN or ENOSPC and there
1758 * appears to be free blocks we will just let
1759 * mpage_da_submit_io() unlock all of the pages.
1760 */
1761 if (err == -EAGAIN)
1762 goto submit_io;
1763
1764 if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
1765 mpd->retval = err;
1766 goto submit_io;
1767 }
1768
1769 /*
1770 * get block failure will cause us to loop in
1771 * writepages, because a_ops->writepage won't be able
1772 * to make progress. The page will be redirtied by
1773 * writepage and writepages will again try to write
1774 * the same.
1775 */
1776 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1777 ext4_msg(sb, KERN_CRIT,
1778 "delayed block allocation failed for inode %lu "
1779 "at logical offset %llu with max blocks %zd "
1780 "with error %d", mpd->inode->i_ino,
1781 (unsigned long long) next,
1782 mpd->b_size >> mpd->inode->i_blkbits, err);
1783 ext4_msg(sb, KERN_CRIT,
1784 "This should not happen!! Data will be lost");
1785 if (err == -ENOSPC)
1786 ext4_print_free_blocks(mpd->inode);
1787 }
1788 /* invalidate all the pages */
1789 ext4_da_block_invalidatepages(mpd);
1790
1791 /* Mark this page range as having been completed */
1792 mpd->io_done = 1;
1793 return;
1794 }
1795 BUG_ON(blks == 0);
1796
1797 mapp = &map;
1798 if (map.m_flags & EXT4_MAP_NEW) {
1799 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
1800 int i;
1801
1802 for (i = 0; i < map.m_len; i++)
1803 unmap_underlying_metadata(bdev, map.m_pblk + i);
1804 }
1805
1806 /*
1807 * Update on-disk size along with block allocation.
1808 */
1809 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
1810 if (disksize > i_size_read(mpd->inode))
1811 disksize = i_size_read(mpd->inode);
1812 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
1813 ext4_update_i_disksize(mpd->inode, disksize);
1814 err = ext4_mark_inode_dirty(handle, mpd->inode);
1815 if (err)
1816 ext4_error(mpd->inode->i_sb,
1817 "Failed to mark inode %lu dirty",
1818 mpd->inode->i_ino);
1819 }
1820
1821 submit_io:
1822 mpage_da_submit_io(mpd, mapp);
1823 mpd->io_done = 1;
1824 }
1825
1826 #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1827 (1 << BH_Delay) | (1 << BH_Unwritten))
1828
1829 /*
1830 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1831 *
1832 * @mpd->lbh - extent of blocks
1833 * @logical - logical number of the block in the file
1834 * @b_state - b_state of the buffer head added
1835 *
1836 * the function is used to collect contig. blocks in same state
1837 */
1838 static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
1839 unsigned long b_state)
1840 {
1841 sector_t next;
1842 int blkbits = mpd->inode->i_blkbits;
1843 int nrblocks = mpd->b_size >> blkbits;
1844
1845 /*
1846 * XXX Don't go larger than mballoc is willing to allocate
1847 * This is a stopgap solution. We eventually need to fold
1848 * mpage_da_submit_io() into this function and then call
1849 * ext4_map_blocks() multiple times in a loop
1850 */
1851 if (nrblocks >= (8*1024*1024 >> blkbits))
1852 goto flush_it;
1853
1854 /* check if the reserved journal credits might overflow */
1855 if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
1856 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1857 /*
1858 * With non-extent format we are limited by the journal
1859 * credit available. Total credit needed to insert
1860 * nrblocks contiguous blocks is dependent on the
1861 * nrblocks. So limit nrblocks.
1862 */
1863 goto flush_it;
1864 }
1865 }
1866 /*
1867 * First block in the extent
1868 */
1869 if (mpd->b_size == 0) {
1870 mpd->b_blocknr = logical;
1871 mpd->b_size = 1 << blkbits;
1872 mpd->b_state = b_state & BH_FLAGS;
1873 return;
1874 }
1875
1876 next = mpd->b_blocknr + nrblocks;
1877 /*
1878 * Can we merge the block to our big extent?
1879 */
1880 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
1881 mpd->b_size += 1 << blkbits;
1882 return;
1883 }
1884
1885 flush_it:
1886 /*
1887 * We couldn't merge the block to our extent, so we
1888 * need to flush current extent and start new one
1889 */
1890 mpage_da_map_and_submit(mpd);
1891 return;
1892 }
1893
1894 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
1895 {
1896 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
1897 }
1898
1899 /*
1900 * This function is grabs code from the very beginning of
1901 * ext4_map_blocks, but assumes that the caller is from delayed write
1902 * time. This function looks up the requested blocks and sets the
1903 * buffer delay bit under the protection of i_data_sem.
1904 */
1905 static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1906 struct ext4_map_blocks *map,
1907 struct buffer_head *bh)
1908 {
1909 struct extent_status es;
1910 int retval;
1911 sector_t invalid_block = ~((sector_t) 0xffff);
1912 #ifdef ES_AGGRESSIVE_TEST
1913 struct ext4_map_blocks orig_map;
1914
1915 memcpy(&orig_map, map, sizeof(*map));
1916 #endif
1917
1918 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1919 invalid_block = ~0;
1920
1921 map->m_flags = 0;
1922 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1923 "logical block %lu\n", inode->i_ino, map->m_len,
1924 (unsigned long) map->m_lblk);
1925
1926 /* Lookup extent status tree firstly */
1927 if (ext4_es_lookup_extent(inode, iblock, &es)) {
1928
1929 if (ext4_es_is_hole(&es)) {
1930 retval = 0;
1931 down_read((&EXT4_I(inode)->i_data_sem));
1932 goto add_delayed;
1933 }
1934
1935 /*
1936 * Delayed extent could be allocated by fallocate.
1937 * So we need to check it.
1938 */
1939 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1940 map_bh(bh, inode->i_sb, invalid_block);
1941 set_buffer_new(bh);
1942 set_buffer_delay(bh);
1943 return 0;
1944 }
1945
1946 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1947 retval = es.es_len - (iblock - es.es_lblk);
1948 if (retval > map->m_len)
1949 retval = map->m_len;
1950 map->m_len = retval;
1951 if (ext4_es_is_written(&es))
1952 map->m_flags |= EXT4_MAP_MAPPED;
1953 else if (ext4_es_is_unwritten(&es))
1954 map->m_flags |= EXT4_MAP_UNWRITTEN;
1955 else
1956 BUG_ON(1);
1957
1958 #ifdef ES_AGGRESSIVE_TEST
1959 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1960 #endif
1961 return retval;
1962 }
1963
1964 /*
1965 * Try to see if we can get the block without requesting a new
1966 * file system block.
1967 */
1968 down_read((&EXT4_I(inode)->i_data_sem));
1969 if (ext4_has_inline_data(inode)) {
1970 /*
1971 * We will soon create blocks for this page, and let
1972 * us pretend as if the blocks aren't allocated yet.
1973 * In case of clusters, we have to handle the work
1974 * of mapping from cluster so that the reserved space
1975 * is calculated properly.
1976 */
1977 if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
1978 ext4_find_delalloc_cluster(inode, map->m_lblk))
1979 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
1980 retval = 0;
1981 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
1982 retval = ext4_ext_map_blocks(NULL, inode, map,
1983 EXT4_GET_BLOCKS_NO_PUT_HOLE);
1984 else
1985 retval = ext4_ind_map_blocks(NULL, inode, map,
1986 EXT4_GET_BLOCKS_NO_PUT_HOLE);
1987
1988 add_delayed:
1989 if (retval == 0) {
1990 int ret;
1991 /*
1992 * XXX: __block_prepare_write() unmaps passed block,
1993 * is it OK?
1994 */
1995 /*
1996 * If the block was allocated from previously allocated cluster,
1997 * then we don't need to reserve it again. However we still need
1998 * to reserve metadata for every block we're going to write.
1999 */
2000 if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
2001 ret = ext4_da_reserve_space(inode, iblock);
2002 if (ret) {
2003 /* not enough space to reserve */
2004 retval = ret;
2005 goto out_unlock;
2006 }
2007 } else {
2008 ret = ext4_da_reserve_metadata(inode, iblock);
2009 if (ret) {
2010 /* not enough space to reserve */
2011 retval = ret;
2012 goto out_unlock;
2013 }
2014 }
2015
2016 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
2017 ~0, EXTENT_STATUS_DELAYED);
2018 if (ret) {
2019 retval = ret;
2020 goto out_unlock;
2021 }
2022
2023 /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
2024 * and it should not appear on the bh->b_state.
2025 */
2026 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
2027
2028 map_bh(bh, inode->i_sb, invalid_block);
2029 set_buffer_new(bh);
2030 set_buffer_delay(bh);
2031 } else if (retval > 0) {
2032 int ret;
2033 unsigned long long status;
2034
2035 #ifdef ES_AGGRESSIVE_TEST
2036 if (retval != map->m_len) {
2037 printk("ES len assertation failed for inode: %lu "
2038 "retval %d != map->m_len %d "
2039 "in %s (lookup)\n", inode->i_ino, retval,
2040 map->m_len, __func__);
2041 }
2042 #endif
2043
2044 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
2045 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
2046 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
2047 map->m_pblk, status);
2048 if (ret != 0)
2049 retval = ret;
2050 }
2051
2052 out_unlock:
2053 up_read((&EXT4_I(inode)->i_data_sem));
2054
2055 return retval;
2056 }
2057
2058 /*
2059 * This is a special get_blocks_t callback which is used by
2060 * ext4_da_write_begin(). It will either return mapped block or
2061 * reserve space for a single block.
2062 *
2063 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2064 * We also have b_blocknr = -1 and b_bdev initialized properly
2065 *
2066 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2067 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2068 * initialized properly.
2069 */
2070 int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2071 struct buffer_head *bh, int create)
2072 {
2073 struct ext4_map_blocks map;
2074 int ret = 0;
2075
2076 BUG_ON(create == 0);
2077 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
2078
2079 map.m_lblk = iblock;
2080 map.m_len = 1;
2081
2082 /*
2083 * first, we need to know whether the block is allocated already
2084 * preallocated blocks are unmapped but should treated
2085 * the same as allocated blocks.
2086 */
2087 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
2088 if (ret <= 0)
2089 return ret;
2090
2091 map_bh(bh, inode->i_sb, map.m_pblk);
2092 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
2093
2094 if (buffer_unwritten(bh)) {
2095 /* A delayed write to unwritten bh should be marked
2096 * new and mapped. Mapped ensures that we don't do
2097 * get_block multiple times when we write to the same
2098 * offset and new ensures that we do proper zero out
2099 * for partial write.
2100 */
2101 set_buffer_new(bh);
2102 set_buffer_mapped(bh);
2103 }
2104 return 0;
2105 }
2106
2107 static int bget_one(handle_t *handle, struct buffer_head *bh)
2108 {
2109 get_bh(bh);
2110 return 0;
2111 }
2112
2113 static int bput_one(handle_t *handle, struct buffer_head *bh)
2114 {
2115 put_bh(bh);
2116 return 0;
2117 }
2118
2119 static int __ext4_journalled_writepage(struct page *page,
2120 unsigned int len)
2121 {
2122 struct address_space *mapping = page->mapping;
2123 struct inode *inode = mapping->host;
2124 struct buffer_head *page_bufs = NULL;
2125 handle_t *handle = NULL;
2126 int ret = 0, err = 0;
2127 int inline_data = ext4_has_inline_data(inode);
2128 struct buffer_head *inode_bh = NULL;
2129
2130 ClearPageChecked(page);
2131
2132 if (inline_data) {
2133 BUG_ON(page->index != 0);
2134 BUG_ON(len > ext4_get_max_inline_size(inode));
2135 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
2136 if (inode_bh == NULL)
2137 goto out;
2138 } else {
2139 page_bufs = page_buffers(page);
2140 if (!page_bufs) {
2141 BUG();
2142 goto out;
2143 }
2144 ext4_walk_page_buffers(handle, page_bufs, 0, len,
2145 NULL, bget_one);
2146 }
2147 /*
2148 * We need to release the page lock before we start the
2149 * journal, so grab a reference so the page won't disappear
2150 * out from under us.
2151 */
2152 get_page(page);
2153 unlock_page(page);
2154
2155 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2156 ext4_writepage_trans_blocks(inode));
2157 if (IS_ERR(handle)) {
2158 ret = PTR_ERR(handle);
2159 put_page(page);
2160 goto out_no_pagelock;
2161 }
2162 BUG_ON(!ext4_handle_valid(handle));
2163
2164 lock_page(page);
2165 put_page(page);
2166 if (page->mapping != mapping) {
2167 /* The page got truncated from under us */
2168 ext4_journal_stop(handle);
2169 ret = 0;
2170 goto out;
2171 }
2172
2173 if (inline_data) {
2174 ret = ext4_journal_get_write_access(handle, inode_bh);
2175
2176 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
2177
2178 } else {
2179 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
2180 do_journal_get_write_access);
2181
2182 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
2183 write_end_fn);
2184 }
2185 if (ret == 0)
2186 ret = err;
2187 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
2188 err = ext4_journal_stop(handle);
2189 if (!ret)
2190 ret = err;
2191
2192 if (!ext4_has_inline_data(inode))
2193 ext4_walk_page_buffers(handle, page_bufs, 0, len,
2194 NULL, bput_one);
2195 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
2196 out:
2197 unlock_page(page);
2198 out_no_pagelock:
2199 brelse(inode_bh);
2200 return ret;
2201 }
2202
2203 /*
2204 * Note that we don't need to start a transaction unless we're journaling data
2205 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2206 * need to file the inode to the transaction's list in ordered mode because if
2207 * we are writing back data added by write(), the inode is already there and if
2208 * we are writing back data modified via mmap(), no one guarantees in which
2209 * transaction the data will hit the disk. In case we are journaling data, we
2210 * cannot start transaction directly because transaction start ranks above page
2211 * lock so we have to do some magic.
2212 *
2213 * This function can get called via...
2214 * - ext4_da_writepages after taking page lock (have journal handle)
2215 * - journal_submit_inode_data_buffers (no journal handle)
2216 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
2217 * - grab_page_cache when doing write_begin (have journal handle)
2218 *
2219 * We don't do any block allocation in this function. If we have page with
2220 * multiple blocks we need to write those buffer_heads that are mapped. This
2221 * is important for mmaped based write. So if we do with blocksize 1K
2222 * truncate(f, 1024);
2223 * a = mmap(f, 0, 4096);
2224 * a[0] = 'a';
2225 * truncate(f, 4096);
2226 * we have in the page first buffer_head mapped via page_mkwrite call back
2227 * but other buffer_heads would be unmapped but dirty (dirty done via the
2228 * do_wp_page). So writepage should write the first block. If we modify
2229 * the mmap area beyond 1024 we will again get a page_fault and the
2230 * page_mkwrite callback will do the block allocation and mark the
2231 * buffer_heads mapped.
2232 *
2233 * We redirty the page if we have any buffer_heads that is either delay or
2234 * unwritten in the page.
2235 *
2236 * We can get recursively called as show below.
2237 *
2238 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2239 * ext4_writepage()
2240 *
2241 * But since we don't do any block allocation we should not deadlock.
2242 * Page also have the dirty flag cleared so we don't get recurive page_lock.
2243 */
2244 static int ext4_writepage(struct page *page,
2245 struct writeback_control *wbc)
2246 {
2247 int ret = 0;
2248 loff_t size;
2249 unsigned int len;
2250 struct buffer_head *page_bufs = NULL;
2251 struct inode *inode = page->mapping->host;
2252 struct ext4_io_submit io_submit;
2253
2254 trace_ext4_writepage(page);
2255 size = i_size_read(inode);
2256 if (page->index == size >> PAGE_CACHE_SHIFT)
2257 len = size & ~PAGE_CACHE_MASK;
2258 else
2259 len = PAGE_CACHE_SIZE;
2260
2261 page_bufs = page_buffers(page);
2262 /*
2263 * We cannot do block allocation or other extent handling in this
2264 * function. If there are buffers needing that, we have to redirty
2265 * the page. But we may reach here when we do a journal commit via
2266 * journal_submit_inode_data_buffers() and in that case we must write
2267 * allocated buffers to achieve data=ordered mode guarantees.
2268 */
2269 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2270 ext4_bh_delay_or_unwritten)) {
2271 redirty_page_for_writepage(wbc, page);
2272 if (current->flags & PF_MEMALLOC) {
2273 /*
2274 * For memory cleaning there's no point in writing only
2275 * some buffers. So just bail out. Warn if we came here
2276 * from direct reclaim.
2277 */
2278 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2279 == PF_MEMALLOC);
2280 unlock_page(page);
2281 return 0;
2282 }
2283 }
2284
2285 if (PageChecked(page) && ext4_should_journal_data(inode))
2286 /*
2287 * It's mmapped pagecache. Add buffers and journal it. There
2288 * doesn't seem much point in redirtying the page here.
2289 */
2290 return __ext4_journalled_writepage(page, len);
2291
2292 memset(&io_submit, 0, sizeof(io_submit));
2293 ret = ext4_bio_write_page(&io_submit, page, len, wbc);
2294 ext4_io_submit(&io_submit);
2295 return ret;
2296 }
2297
2298 /*
2299 * This is called via ext4_da_writepages() to
2300 * calculate the total number of credits to reserve to fit
2301 * a single extent allocation into a single transaction,
2302 * ext4_da_writpeages() will loop calling this before
2303 * the block allocation.
2304 */
2305
2306 static int ext4_da_writepages_trans_blocks(struct inode *inode)
2307 {
2308 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2309
2310 /*
2311 * With non-extent format the journal credit needed to
2312 * insert nrblocks contiguous block is dependent on
2313 * number of contiguous block. So we will limit
2314 * number of contiguous block to a sane value
2315 */
2316 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
2317 (max_blocks > EXT4_MAX_TRANS_DATA))
2318 max_blocks = EXT4_MAX_TRANS_DATA;
2319
2320 return ext4_chunk_trans_blocks(inode, max_blocks);
2321 }
2322
2323 /*
2324 * write_cache_pages_da - walk the list of dirty pages of the given
2325 * address space and accumulate pages that need writing, and call
2326 * mpage_da_map_and_submit to map a single contiguous memory region
2327 * and then write them.
2328 */
2329 static int write_cache_pages_da(handle_t *handle,
2330 struct address_space *mapping,
2331 struct writeback_control *wbc,
2332 struct mpage_da_data *mpd,
2333 pgoff_t *done_index)
2334 {
2335 struct buffer_head *bh, *head;
2336 struct inode *inode = mapping->host;
2337 struct pagevec pvec;
2338 unsigned int nr_pages;
2339 sector_t logical;
2340 pgoff_t index, end;
2341 long nr_to_write = wbc->nr_to_write;
2342 int i, tag, ret = 0;
2343
2344 memset(mpd, 0, sizeof(struct mpage_da_data));
2345 mpd->wbc = wbc;
2346 mpd->inode = inode;
2347 pagevec_init(&pvec, 0);
2348 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2349 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2350
2351 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2352 tag = PAGECACHE_TAG_TOWRITE;
2353 else
2354 tag = PAGECACHE_TAG_DIRTY;
2355
2356 *done_index = index;
2357 while (index <= end) {
2358 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
2359 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2360 if (nr_pages == 0)
2361 return 0;
2362
2363 for (i = 0; i < nr_pages; i++) {
2364 struct page *page = pvec.pages[i];
2365
2366 /*
2367 * At this point, the page may be truncated or
2368 * invalidated (changing page->mapping to NULL), or
2369 * even swizzled back from swapper_space to tmpfs file
2370 * mapping. However, page->index will not change
2371 * because we have a reference on the page.
2372 */
2373 if (page->index > end)
2374 goto out;
2375
2376 *done_index = page->index + 1;
2377
2378 /*
2379 * If we can't merge this page, and we have
2380 * accumulated an contiguous region, write it
2381 */
2382 if ((mpd->next_page != page->index) &&
2383 (mpd->next_page != mpd->first_page)) {
2384 mpage_da_map_and_submit(mpd);
2385 goto ret_extent_tail;
2386 }
2387
2388 lock_page(page);
2389
2390 /*
2391 * If the page is no longer dirty, or its
2392 * mapping no longer corresponds to inode we
2393 * are writing (which means it has been
2394 * truncated or invalidated), or the page is
2395 * already under writeback and we are not
2396 * doing a data integrity writeback, skip the page
2397 */
2398 if (!PageDirty(page) ||
2399 (PageWriteback(page) &&
2400 (wbc->sync_mode == WB_SYNC_NONE)) ||
2401 unlikely(page->mapping != mapping)) {
2402 unlock_page(page);
2403 continue;
2404 }
2405
2406 wait_on_page_writeback(page);
2407 BUG_ON(PageWriteback(page));
2408
2409 /*
2410 * If we have inline data and arrive here, it means that
2411 * we will soon create the block for the 1st page, so
2412 * we'd better clear the inline data here.
2413 */
2414 if (ext4_has_inline_data(inode)) {
2415 BUG_ON(ext4_test_inode_state(inode,
2416 EXT4_STATE_MAY_INLINE_DATA));
2417 ext4_destroy_inline_data(handle, inode);
2418 }
2419
2420 if (mpd->next_page != page->index)
2421 mpd->first_page = page->index;
2422 mpd->next_page = page->index + 1;
2423 logical = (sector_t) page->index <<
2424 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2425
2426 /* Add all dirty buffers to mpd */
2427 head = page_buffers(page);
2428 bh = head;
2429 do {
2430 BUG_ON(buffer_locked(bh));
2431 /*
2432 * We need to try to allocate unmapped blocks
2433 * in the same page. Otherwise we won't make
2434 * progress with the page in ext4_writepage
2435 */
2436 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
2437 mpage_add_bh_to_extent(mpd, logical,
2438 bh->b_state);
2439 if (mpd->io_done)
2440 goto ret_extent_tail;
2441 } else if (buffer_dirty(bh) &&
2442 buffer_mapped(bh)) {
2443 /*
2444 * mapped dirty buffer. We need to
2445 * update the b_state because we look
2446 * at b_state in mpage_da_map_blocks.
2447 * We don't update b_size because if we
2448 * find an unmapped buffer_head later
2449 * we need to use the b_state flag of
2450 * that buffer_head.
2451 */
2452 if (mpd->b_size == 0)
2453 mpd->b_state =
2454 bh->b_state & BH_FLAGS;
2455 }
2456 logical++;
2457 } while ((bh = bh->b_this_page) != head);
2458
2459 if (nr_to_write > 0) {
2460 nr_to_write--;
2461 if (nr_to_write == 0 &&
2462 wbc->sync_mode == WB_SYNC_NONE)
2463 /*
2464 * We stop writing back only if we are
2465 * not doing integrity sync. In case of
2466 * integrity sync we have to keep going
2467 * because someone may be concurrently
2468 * dirtying pages, and we might have
2469 * synced a lot of newly appeared dirty
2470 * pages, but have not synced all of the
2471 * old dirty pages.
2472 */
2473 goto out;
2474 }
2475 }
2476 pagevec_release(&pvec);
2477 cond_resched();
2478 }
2479 return 0;
2480 ret_extent_tail:
2481 ret = MPAGE_DA_EXTENT_TAIL;
2482 out:
2483 pagevec_release(&pvec);
2484 cond_resched();
2485 return ret;
2486 }
2487
2488
2489 static int ext4_da_writepages(struct address_space *mapping,
2490 struct writeback_control *wbc)
2491 {
2492 pgoff_t index;
2493 int range_whole = 0;
2494 handle_t *handle = NULL;
2495 struct mpage_da_data mpd;
2496 struct inode *inode = mapping->host;
2497 int pages_written = 0;
2498 unsigned int max_pages;
2499 int range_cyclic, cycled = 1, io_done = 0;
2500 int needed_blocks, ret = 0;
2501 long desired_nr_to_write, nr_to_writebump = 0;
2502 loff_t range_start = wbc->range_start;
2503 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2504 pgoff_t done_index = 0;
2505 pgoff_t end;
2506 struct blk_plug plug;
2507
2508 trace_ext4_da_writepages(inode, wbc);
2509
2510 /*
2511 * No pages to write? This is mainly a kludge to avoid starting
2512 * a transaction for special inodes like journal inode on last iput()
2513 * because that could violate lock ordering on umount
2514 */
2515 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2516 return 0;
2517
2518 /*
2519 * If the filesystem has aborted, it is read-only, so return
2520 * right away instead of dumping stack traces later on that
2521 * will obscure the real source of the problem. We test
2522 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2523 * the latter could be true if the filesystem is mounted
2524 * read-only, and in that case, ext4_da_writepages should
2525 * *never* be called, so if that ever happens, we would want
2526 * the stack trace.
2527 */
2528 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
2529 return -EROFS;
2530
2531 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2532 range_whole = 1;
2533
2534 range_cyclic = wbc->range_cyclic;
2535 if (wbc->range_cyclic) {
2536 index = mapping->writeback_index;
2537 if (index)
2538 cycled = 0;
2539 wbc->range_start = index << PAGE_CACHE_SHIFT;
2540 wbc->range_end = LLONG_MAX;
2541 wbc->range_cyclic = 0;
2542 end = -1;
2543 } else {
2544 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2545 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2546 }
2547
2548 /*
2549 * This works around two forms of stupidity. The first is in
2550 * the writeback code, which caps the maximum number of pages
2551 * written to be 1024 pages. This is wrong on multiple
2552 * levels; different architectues have a different page size,
2553 * which changes the maximum amount of data which gets
2554 * written. Secondly, 4 megabytes is way too small. XFS
2555 * forces this value to be 16 megabytes by multiplying
2556 * nr_to_write parameter by four, and then relies on its
2557 * allocator to allocate larger extents to make them
2558 * contiguous. Unfortunately this brings us to the second
2559 * stupidity, which is that ext4's mballoc code only allocates
2560 * at most 2048 blocks. So we force contiguous writes up to
2561 * the number of dirty blocks in the inode, or
2562 * sbi->max_writeback_mb_bump whichever is smaller.
2563 */
2564 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2565 if (!range_cyclic && range_whole) {
2566 if (wbc->nr_to_write == LONG_MAX)
2567 desired_nr_to_write = wbc->nr_to_write;
2568 else
2569 desired_nr_to_write = wbc->nr_to_write * 8;
2570 } else
2571 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2572 max_pages);
2573 if (desired_nr_to_write > max_pages)
2574 desired_nr_to_write = max_pages;
2575
2576 if (wbc->nr_to_write < desired_nr_to_write) {
2577 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2578 wbc->nr_to_write = desired_nr_to_write;
2579 }
2580
2581 retry:
2582 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2583 tag_pages_for_writeback(mapping, index, end);
2584
2585 blk_start_plug(&plug);
2586 while (!ret && wbc->nr_to_write > 0) {
2587
2588 /*
2589 * we insert one extent at a time. So we need
2590 * credit needed for single extent allocation.
2591 * journalled mode is currently not supported
2592 * by delalloc
2593 */
2594 BUG_ON(ext4_should_journal_data(inode));
2595 needed_blocks = ext4_da_writepages_trans_blocks(inode);
2596
2597 /* start a new transaction*/
2598 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2599 needed_blocks);
2600 if (IS_ERR(handle)) {
2601 ret = PTR_ERR(handle);
2602 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
2603 "%ld pages, ino %lu; err %d", __func__,
2604 wbc->nr_to_write, inode->i_ino, ret);
2605 blk_finish_plug(&plug);
2606 goto out_writepages;
2607 }
2608
2609 /*
2610 * Now call write_cache_pages_da() to find the next
2611 * contiguous region of logical blocks that need
2612 * blocks to be allocated by ext4 and submit them.
2613 */
2614 ret = write_cache_pages_da(handle, mapping,
2615 wbc, &mpd, &done_index);
2616 /*
2617 * If we have a contiguous extent of pages and we
2618 * haven't done the I/O yet, map the blocks and submit
2619 * them for I/O.
2620 */
2621 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2622 mpage_da_map_and_submit(&mpd);
2623 ret = MPAGE_DA_EXTENT_TAIL;
2624 }
2625 trace_ext4_da_write_pages(inode, &mpd);
2626 wbc->nr_to_write -= mpd.pages_written;
2627
2628 ext4_journal_stop(handle);
2629
2630 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
2631 /* commit the transaction which would
2632 * free blocks released in the transaction
2633 * and try again
2634 */
2635 jbd2_journal_force_commit_nested(sbi->s_journal);
2636 ret = 0;
2637 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
2638 /*
2639 * Got one extent now try with rest of the pages.
2640 * If mpd.retval is set -EIO, journal is aborted.
2641 * So we don't need to write any more.
2642 */
2643 pages_written += mpd.pages_written;
2644 ret = mpd.retval;
2645 io_done = 1;
2646 } else if (wbc->nr_to_write)
2647 /*
2648 * There is no more writeout needed
2649 * or we requested for a noblocking writeout
2650 * and we found the device congested
2651 */
2652 break;
2653 }
2654 blk_finish_plug(&plug);
2655 if (!io_done && !cycled) {
2656 cycled = 1;
2657 index = 0;
2658 wbc->range_start = index << PAGE_CACHE_SHIFT;
2659 wbc->range_end = mapping->writeback_index - 1;
2660 goto retry;
2661 }
2662
2663 /* Update index */
2664 wbc->range_cyclic = range_cyclic;
2665 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2666 /*
2667 * set the writeback_index so that range_cyclic
2668 * mode will write it back later
2669 */
2670 mapping->writeback_index = done_index;
2671
2672 out_writepages:
2673 wbc->nr_to_write -= nr_to_writebump;
2674 wbc->range_start = range_start;
2675 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
2676 return ret;
2677 }
2678
2679 static int ext4_nonda_switch(struct super_block *sb)
2680 {
2681 s64 free_clusters, dirty_clusters;
2682 struct ext4_sb_info *sbi = EXT4_SB(sb);
2683
2684 /*
2685 * switch to non delalloc mode if we are running low
2686 * on free block. The free block accounting via percpu
2687 * counters can get slightly wrong with percpu_counter_batch getting
2688 * accumulated on each CPU without updating global counters
2689 * Delalloc need an accurate free block accounting. So switch
2690 * to non delalloc when we are near to error range.
2691 */
2692 free_clusters =
2693 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2694 dirty_clusters =
2695 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
2696 /*
2697 * Start pushing delalloc when 1/2 of free blocks are dirty.
2698 */
2699 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
2700 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
2701
2702 if (2 * free_clusters < 3 * dirty_clusters ||
2703 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
2704 /*
2705 * free block count is less than 150% of dirty blocks
2706 * or free blocks is less than watermark
2707 */
2708 return 1;
2709 }
2710 return 0;
2711 }
2712
2713 /* We always reserve for an inode update; the superblock could be there too */
2714 static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2715 {
2716 if (likely(EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
2717 EXT4_FEATURE_RO_COMPAT_LARGE_FILE)))
2718 return 1;
2719
2720 if (pos + len <= 0x7fffffffULL)
2721 return 1;
2722
2723 /* We might need to update the superblock to set LARGE_FILE */
2724 return 2;
2725 }
2726
2727 static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
2728 loff_t pos, unsigned len, unsigned flags,
2729 struct page **pagep, void **fsdata)
2730 {
2731 int ret, retries = 0;
2732 struct page *page;
2733 pgoff_t index;
2734 struct inode *inode = mapping->host;
2735 handle_t *handle;
2736
2737 index = pos >> PAGE_CACHE_SHIFT;
2738
2739 if (ext4_nonda_switch(inode->i_sb)) {
2740 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2741 return ext4_write_begin(file, mapping, pos,
2742 len, flags, pagep, fsdata);
2743 }
2744 *fsdata = (void *)0;
2745 trace_ext4_da_write_begin(inode, pos, len, flags);
2746
2747 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2748 ret = ext4_da_write_inline_data_begin(mapping, inode,
2749 pos, len, flags,
2750 pagep, fsdata);
2751 if (ret < 0)
2752 return ret;
2753 if (ret == 1)
2754 return 0;
2755 }
2756
2757 /*
2758 * grab_cache_page_write_begin() can take a long time if the
2759 * system is thrashing due to memory pressure, or if the page
2760 * is being written back. So grab it first before we start
2761 * the transaction handle. This also allows us to allocate
2762 * the page (if needed) without using GFP_NOFS.
2763 */
2764 retry_grab:
2765 page = grab_cache_page_write_begin(mapping, index, flags);
2766 if (!page)
2767 return -ENOMEM;
2768 unlock_page(page);
2769
2770 /*
2771 * With delayed allocation, we don't log the i_disksize update
2772 * if there is delayed block allocation. But we still need
2773 * to journalling the i_disksize update if writes to the end
2774 * of file which has an already mapped buffer.
2775 */
2776 retry_journal:
2777 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2778 ext4_da_write_credits(inode, pos, len));
2779 if (IS_ERR(handle)) {
2780 page_cache_release(page);
2781 return PTR_ERR(handle);
2782 }
2783
2784 lock_page(page);
2785 if (page->mapping != mapping) {
2786 /* The page got truncated from under us */
2787 unlock_page(page);
2788 page_cache_release(page);
2789 ext4_journal_stop(handle);
2790 goto retry_grab;
2791 }
2792 /* In case writeback began while the page was unlocked */
2793 wait_for_stable_page(page);
2794
2795 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
2796 if (ret < 0) {
2797 unlock_page(page);
2798 ext4_journal_stop(handle);
2799 /*
2800 * block_write_begin may have instantiated a few blocks
2801 * outside i_size. Trim these off again. Don't need
2802 * i_size_read because we hold i_mutex.
2803 */
2804 if (pos + len > inode->i_size)
2805 ext4_truncate_failed_write(inode);
2806
2807 if (ret == -ENOSPC &&
2808 ext4_should_retry_alloc(inode->i_sb, &retries))
2809 goto retry_journal;
2810
2811 page_cache_release(page);
2812 return ret;
2813 }
2814
2815 *pagep = page;
2816 return ret;
2817 }
2818
2819 /*
2820 * Check if we should update i_disksize
2821 * when write to the end of file but not require block allocation
2822 */
2823 static int ext4_da_should_update_i_disksize(struct page *page,
2824 unsigned long offset)
2825 {
2826 struct buffer_head *bh;
2827 struct inode *inode = page->mapping->host;
2828 unsigned int idx;
2829 int i;
2830
2831 bh = page_buffers(page);
2832 idx = offset >> inode->i_blkbits;
2833
2834 for (i = 0; i < idx; i++)
2835 bh = bh->b_this_page;
2836
2837 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
2838 return 0;
2839 return 1;
2840 }
2841
2842 static int ext4_da_write_end(struct file *file,
2843 struct address_space *mapping,
2844 loff_t pos, unsigned len, unsigned copied,
2845 struct page *page, void *fsdata)
2846 {
2847 struct inode *inode = mapping->host;
2848 int ret = 0, ret2;
2849 handle_t *handle = ext4_journal_current_handle();
2850 loff_t new_i_size;
2851 unsigned long start, end;
2852 int write_mode = (int)(unsigned long)fsdata;
2853
2854 if (write_mode == FALL_BACK_TO_NONDELALLOC)
2855 return ext4_write_end(file, mapping, pos,
2856 len, copied, page, fsdata);
2857
2858 trace_ext4_da_write_end(inode, pos, len, copied);
2859 start = pos & (PAGE_CACHE_SIZE - 1);
2860 end = start + copied - 1;
2861
2862 /*
2863 * generic_write_end() will run mark_inode_dirty() if i_size
2864 * changes. So let's piggyback the i_disksize mark_inode_dirty
2865 * into that.
2866 */
2867 new_i_size = pos + copied;
2868 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
2869 if (ext4_has_inline_data(inode) ||
2870 ext4_da_should_update_i_disksize(page, end)) {
2871 down_write(&EXT4_I(inode)->i_data_sem);
2872 if (new_i_size > EXT4_I(inode)->i_disksize)
2873 EXT4_I(inode)->i_disksize = new_i_size;
2874 up_write(&EXT4_I(inode)->i_data_sem);
2875 /* We need to mark inode dirty even if
2876 * new_i_size is less that inode->i_size
2877 * bu greater than i_disksize.(hint delalloc)
2878 */
2879 ext4_mark_inode_dirty(handle, inode);
2880 }
2881 }
2882
2883 if (write_mode != CONVERT_INLINE_DATA &&
2884 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
2885 ext4_has_inline_data(inode))
2886 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
2887 page);
2888 else
2889 ret2 = generic_write_end(file, mapping, pos, len, copied,
2890 page, fsdata);
2891
2892 copied = ret2;
2893 if (ret2 < 0)
2894 ret = ret2;
2895 ret2 = ext4_journal_stop(handle);
2896 if (!ret)
2897 ret = ret2;
2898
2899 return ret ? ret : copied;
2900 }
2901
2902 static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2903 {
2904 /*
2905 * Drop reserved blocks
2906 */
2907 BUG_ON(!PageLocked(page));
2908 if (!page_has_buffers(page))
2909 goto out;
2910
2911 ext4_da_page_release_reservation(page, offset);
2912
2913 out:
2914 ext4_invalidatepage(page, offset);
2915
2916 return;
2917 }
2918
2919 /*
2920 * Force all delayed allocation blocks to be allocated for a given inode.
2921 */
2922 int ext4_alloc_da_blocks(struct inode *inode)
2923 {
2924 trace_ext4_alloc_da_blocks(inode);
2925
2926 if (!EXT4_I(inode)->i_reserved_data_blocks &&
2927 !EXT4_I(inode)->i_reserved_meta_blocks)
2928 return 0;
2929
2930 /*
2931 * We do something simple for now. The filemap_flush() will
2932 * also start triggering a write of the data blocks, which is
2933 * not strictly speaking necessary (and for users of
2934 * laptop_mode, not even desirable). However, to do otherwise
2935 * would require replicating code paths in:
2936 *
2937 * ext4_da_writepages() ->
2938 * write_cache_pages() ---> (via passed in callback function)
2939 * __mpage_da_writepage() -->
2940 * mpage_add_bh_to_extent()
2941 * mpage_da_map_blocks()
2942 *
2943 * The problem is that write_cache_pages(), located in
2944 * mm/page-writeback.c, marks pages clean in preparation for
2945 * doing I/O, which is not desirable if we're not planning on
2946 * doing I/O at all.
2947 *
2948 * We could call write_cache_pages(), and then redirty all of
2949 * the pages by calling redirty_page_for_writepage() but that
2950 * would be ugly in the extreme. So instead we would need to
2951 * replicate parts of the code in the above functions,
2952 * simplifying them because we wouldn't actually intend to
2953 * write out the pages, but rather only collect contiguous
2954 * logical block extents, call the multi-block allocator, and
2955 * then update the buffer heads with the block allocations.
2956 *
2957 * For now, though, we'll cheat by calling filemap_flush(),
2958 * which will map the blocks, and start the I/O, but not
2959 * actually wait for the I/O to complete.
2960 */
2961 return filemap_flush(inode->i_mapping);
2962 }
2963
2964 /*
2965 * bmap() is special. It gets used by applications such as lilo and by
2966 * the swapper to find the on-disk block of a specific piece of data.
2967 *
2968 * Naturally, this is dangerous if the block concerned is still in the
2969 * journal. If somebody makes a swapfile on an ext4 data-journaling
2970 * filesystem and enables swap, then they may get a nasty shock when the
2971 * data getting swapped to that swapfile suddenly gets overwritten by
2972 * the original zero's written out previously to the journal and
2973 * awaiting writeback in the kernel's buffer cache.
2974 *
2975 * So, if we see any bmap calls here on a modified, data-journaled file,
2976 * take extra steps to flush any blocks which might be in the cache.
2977 */
2978 static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
2979 {
2980 struct inode *inode = mapping->host;
2981 journal_t *journal;
2982 int err;
2983
2984 /*
2985 * We can get here for an inline file via the FIBMAP ioctl
2986 */
2987 if (ext4_has_inline_data(inode))
2988 return 0;
2989
2990 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2991 test_opt(inode->i_sb, DELALLOC)) {
2992 /*
2993 * With delalloc we want to sync the file
2994 * so that we can make sure we allocate
2995 * blocks for file
2996 */
2997 filemap_write_and_wait(mapping);
2998 }
2999
3000 if (EXT4_JOURNAL(inode) &&
3001 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
3002 /*
3003 * This is a REALLY heavyweight approach, but the use of
3004 * bmap on dirty files is expected to be extremely rare:
3005 * only if we run lilo or swapon on a freshly made file
3006 * do we expect this to happen.
3007 *
3008 * (bmap requires CAP_SYS_RAWIO so this does not
3009 * represent an unprivileged user DOS attack --- we'd be
3010 * in trouble if mortal users could trigger this path at
3011 * will.)
3012 *
3013 * NB. EXT4_STATE_JDATA is not set on files other than
3014 * regular files. If somebody wants to bmap a directory
3015 * or symlink and gets confused because the buffer
3016 * hasn't yet been flushed to disk, they deserve
3017 * everything they get.
3018 */
3019
3020 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
3021 journal = EXT4_JOURNAL(inode);
3022 jbd2_journal_lock_updates(journal);
3023 err = jbd2_journal_flush(journal);
3024 jbd2_journal_unlock_updates(journal);
3025
3026 if (err)
3027 return 0;
3028 }
3029
3030 return generic_block_bmap(mapping, block, ext4_get_block);
3031 }
3032
3033 static int ext4_readpage(struct file *file, struct page *page)
3034 {
3035 int ret = -EAGAIN;
3036 struct inode *inode = page->mapping->host;
3037
3038 trace_ext4_readpage(page);
3039
3040 if (ext4_has_inline_data(inode))
3041 ret = ext4_readpage_inline(inode, page);
3042
3043 if (ret == -EAGAIN)
3044 return mpage_readpage(page, ext4_get_block);
3045
3046 return ret;
3047 }
3048
3049 static int
3050 ext4_readpages(struct file *file, struct address_space *mapping,
3051 struct list_head *pages, unsigned nr_pages)
3052 {
3053 struct inode *inode = mapping->host;
3054
3055 /* If the file has inline data, no need to do readpages. */
3056 if (ext4_has_inline_data(inode))
3057 return 0;
3058
3059 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
3060 }
3061
3062 static void ext4_invalidatepage(struct page *page, unsigned long offset)
3063 {
3064 trace_ext4_invalidatepage(page, offset);
3065
3066 /* No journalling happens on data buffers when this function is used */
3067 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3068
3069 block_invalidatepage(page, offset);
3070 }
3071
3072 static int __ext4_journalled_invalidatepage(struct page *page,
3073 unsigned long offset)
3074 {
3075 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3076
3077 trace_ext4_journalled_invalidatepage(page, offset);
3078
3079 /*
3080 * If it's a full truncate we just forget about the pending dirtying
3081 */
3082 if (offset == 0)
3083 ClearPageChecked(page);
3084
3085 return jbd2_journal_invalidatepage(journal, page, offset);
3086 }
3087
3088 /* Wrapper for aops... */
3089 static void ext4_journalled_invalidatepage(struct page *page,
3090 unsigned long offset)
3091 {
3092 WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
3093 }
3094
3095 static int ext4_releasepage(struct page *page, gfp_t wait)
3096 {
3097 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3098
3099 trace_ext4_releasepage(page);
3100
3101 /* Page has dirty journalled data -> cannot release */
3102 if (PageChecked(page))
3103 return 0;
3104 if (journal)
3105 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3106 else
3107 return try_to_free_buffers(page);
3108 }
3109
3110 /*
3111 * ext4_get_block used when preparing for a DIO write or buffer write.
3112 * We allocate an uinitialized extent if blocks haven't been allocated.
3113 * The extent will be converted to initialized after the IO is complete.
3114 */
3115 int ext4_get_block_write(struct inode *inode, sector_t iblock,
3116 struct buffer_head *bh_result, int create)
3117 {
3118 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
3119 inode->i_ino, create);
3120 return _ext4_get_block(inode, iblock, bh_result,
3121 EXT4_GET_BLOCKS_IO_CREATE_EXT);
3122 }
3123
3124 static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
3125 struct buffer_head *bh_result, int create)
3126 {
3127 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3128 inode->i_ino, create);
3129 return _ext4_get_block(inode, iblock, bh_result,
3130 EXT4_GET_BLOCKS_NO_LOCK);
3131 }
3132
3133 static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3134 ssize_t size, void *private, int ret,
3135 bool is_async)
3136 {
3137 struct inode *inode = file_inode(iocb->ki_filp);
3138 ext4_io_end_t *io_end = iocb->private;
3139
3140 /* if not async direct IO or dio with 0 bytes write, just return */
3141 if (!io_end || !size)
3142 goto out;
3143
3144 ext_debug("ext4_end_io_dio(): io_end 0x%p "
3145 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
3146 iocb->private, io_end->inode->i_ino, iocb, offset,
3147 size);
3148
3149 iocb->private = NULL;
3150
3151 /* if not aio dio with unwritten extents, just free io and return */
3152 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
3153 ext4_free_io_end(io_end);
3154 out:
3155 inode_dio_done(inode);
3156 if (is_async)
3157 aio_complete(iocb, ret, 0);
3158 return;
3159 }
3160
3161 io_end->offset = offset;
3162 io_end->size = size;
3163 if (is_async) {
3164 io_end->iocb = iocb;
3165 io_end->result = ret;
3166 }
3167
3168 ext4_add_complete_io(io_end);
3169 }
3170
3171 /*
3172 * For ext4 extent files, ext4 will do direct-io write to holes,
3173 * preallocated extents, and those write extend the file, no need to
3174 * fall back to buffered IO.
3175 *
3176 * For holes, we fallocate those blocks, mark them as uninitialized
3177 * If those blocks were preallocated, we mark sure they are split, but
3178 * still keep the range to write as uninitialized.
3179 *
3180 * The unwritten extents will be converted to written when DIO is completed.
3181 * For async direct IO, since the IO may still pending when return, we
3182 * set up an end_io call back function, which will do the conversion
3183 * when async direct IO completed.
3184 *
3185 * If the O_DIRECT write will extend the file then add this inode to the
3186 * orphan list. So recovery will truncate it back to the original size
3187 * if the machine crashes during the write.
3188 *
3189 */
3190 static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3191 const struct iovec *iov, loff_t offset,
3192 unsigned long nr_segs)
3193 {
3194 struct file *file = iocb->ki_filp;
3195 struct inode *inode = file->f_mapping->host;
3196 ssize_t ret;
3197 size_t count = iov_length(iov, nr_segs);
3198 int overwrite = 0;
3199 get_block_t *get_block_func = NULL;
3200 int dio_flags = 0;
3201 loff_t final_size = offset + count;
3202
3203 /* Use the old path for reads and writes beyond i_size. */
3204 if (rw != WRITE || final_size > inode->i_size)
3205 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3206
3207 BUG_ON(iocb->private == NULL);
3208
3209 /* If we do a overwrite dio, i_mutex locking can be released */
3210 overwrite = *((int *)iocb->private);
3211
3212 if (overwrite) {
3213 atomic_inc(&inode->i_dio_count);
3214 down_read(&EXT4_I(inode)->i_data_sem);
3215 mutex_unlock(&inode->i_mutex);
3216 }
3217
3218 /*
3219 * We could direct write to holes and fallocate.
3220 *
3221 * Allocated blocks to fill the hole are marked as
3222 * uninitialized to prevent parallel buffered read to expose
3223 * the stale data before DIO complete the data IO.
3224 *
3225 * As to previously fallocated extents, ext4 get_block will
3226 * just simply mark the buffer mapped but still keep the
3227 * extents uninitialized.
3228 *
3229 * For non AIO case, we will convert those unwritten extents
3230 * to written after return back from blockdev_direct_IO.
3231 *
3232 * For async DIO, the conversion needs to be deferred when the
3233 * IO is completed. The ext4 end_io callback function will be
3234 * called to take care of the conversion work. Here for async
3235 * case, we allocate an io_end structure to hook to the iocb.
3236 */
3237 iocb->private = NULL;
3238 ext4_inode_aio_set(inode, NULL);
3239 if (!is_sync_kiocb(iocb)) {
3240 ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
3241 if (!io_end) {
3242 ret = -ENOMEM;
3243 goto retake_lock;
3244 }
3245 io_end->flag |= EXT4_IO_END_DIRECT;
3246 iocb->private = io_end;
3247 /*
3248 * we save the io structure for current async direct
3249 * IO, so that later ext4_map_blocks() could flag the
3250 * io structure whether there is a unwritten extents
3251 * needs to be converted when IO is completed.
3252 */
3253 ext4_inode_aio_set(inode, io_end);
3254 }
3255
3256 if (overwrite) {
3257 get_block_func = ext4_get_block_write_nolock;
3258 } else {
3259 get_block_func = ext4_get_block_write;
3260 dio_flags = DIO_LOCKING;
3261 }
3262 ret = __blockdev_direct_IO(rw, iocb, inode,
3263 inode->i_sb->s_bdev, iov,
3264 offset, nr_segs,
3265 get_block_func,
3266 ext4_end_io_dio,
3267 NULL,
3268 dio_flags);
3269
3270 if (iocb->private)
3271 ext4_inode_aio_set(inode, NULL);
3272 /*
3273 * The io_end structure takes a reference to the inode, that
3274 * structure needs to be destroyed and the reference to the
3275 * inode need to be dropped, when IO is complete, even with 0
3276 * byte write, or failed.
3277 *
3278 * In the successful AIO DIO case, the io_end structure will
3279 * be destroyed and the reference to the inode will be dropped
3280 * after the end_io call back function is called.
3281 *
3282 * In the case there is 0 byte write, or error case, since VFS
3283 * direct IO won't invoke the end_io call back function, we
3284 * need to free the end_io structure here.
3285 */
3286 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3287 ext4_free_io_end(iocb->private);
3288 iocb->private = NULL;
3289 } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
3290 EXT4_STATE_DIO_UNWRITTEN)) {
3291 int err;
3292 /*
3293 * for non AIO case, since the IO is already
3294 * completed, we could do the conversion right here
3295 */
3296 err = ext4_convert_unwritten_extents(inode,
3297 offset, ret);
3298 if (err < 0)
3299 ret = err;
3300 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3301 }
3302
3303 retake_lock:
3304 /* take i_mutex locking again if we do a ovewrite dio */
3305 if (overwrite) {
3306 inode_dio_done(inode);
3307 up_read(&EXT4_I(inode)->i_data_sem);
3308 mutex_lock(&inode->i_mutex);
3309 }
3310
3311 return ret;
3312 }
3313
3314 static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3315 const struct iovec *iov, loff_t offset,
3316 unsigned long nr_segs)
3317 {
3318 struct file *file = iocb->ki_filp;
3319 struct inode *inode = file->f_mapping->host;
3320 ssize_t ret;
3321
3322 /*
3323 * If we are doing data journalling we don't support O_DIRECT
3324 */
3325 if (ext4_should_journal_data(inode))
3326 return 0;
3327
3328 /* Let buffer I/O handle the inline data case. */
3329 if (ext4_has_inline_data(inode))
3330 return 0;
3331
3332 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
3333 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3334 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3335 else
3336 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3337 trace_ext4_direct_IO_exit(inode, offset,
3338 iov_length(iov, nr_segs), rw, ret);
3339 return ret;
3340 }
3341
3342 /*
3343 * Pages can be marked dirty completely asynchronously from ext4's journalling
3344 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3345 * much here because ->set_page_dirty is called under VFS locks. The page is
3346 * not necessarily locked.
3347 *
3348 * We cannot just dirty the page and leave attached buffers clean, because the
3349 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3350 * or jbddirty because all the journalling code will explode.
3351 *
3352 * So what we do is to mark the page "pending dirty" and next time writepage
3353 * is called, propagate that into the buffers appropriately.
3354 */
3355 static int ext4_journalled_set_page_dirty(struct page *page)
3356 {
3357 SetPageChecked(page);
3358 return __set_page_dirty_nobuffers(page);
3359 }
3360
3361 static const struct address_space_operations ext4_aops = {
3362 .readpage = ext4_readpage,
3363 .readpages = ext4_readpages,
3364 .writepage = ext4_writepage,
3365 .write_begin = ext4_write_begin,
3366 .write_end = ext4_write_end,
3367 .bmap = ext4_bmap,
3368 .invalidatepage = ext4_invalidatepage,
3369 .releasepage = ext4_releasepage,
3370 .direct_IO = ext4_direct_IO,
3371 .migratepage = buffer_migrate_page,
3372 .is_partially_uptodate = block_is_partially_uptodate,
3373 .error_remove_page = generic_error_remove_page,
3374 };
3375
3376 static const struct address_space_operations ext4_journalled_aops = {
3377 .readpage = ext4_readpage,
3378 .readpages = ext4_readpages,
3379 .writepage = ext4_writepage,
3380 .write_begin = ext4_write_begin,
3381 .write_end = ext4_journalled_write_end,
3382 .set_page_dirty = ext4_journalled_set_page_dirty,
3383 .bmap = ext4_bmap,
3384 .invalidatepage = ext4_journalled_invalidatepage,
3385 .releasepage = ext4_releasepage,
3386 .direct_IO = ext4_direct_IO,
3387 .is_partially_uptodate = block_is_partially_uptodate,
3388 .error_remove_page = generic_error_remove_page,
3389 };
3390
3391 static const struct address_space_operations ext4_da_aops = {
3392 .readpage = ext4_readpage,
3393 .readpages = ext4_readpages,
3394 .writepage = ext4_writepage,
3395 .writepages = ext4_da_writepages,
3396 .write_begin = ext4_da_write_begin,
3397 .write_end = ext4_da_write_end,
3398 .bmap = ext4_bmap,
3399 .invalidatepage = ext4_da_invalidatepage,
3400 .releasepage = ext4_releasepage,
3401 .direct_IO = ext4_direct_IO,
3402 .migratepage = buffer_migrate_page,
3403 .is_partially_uptodate = block_is_partially_uptodate,
3404 .error_remove_page = generic_error_remove_page,
3405 };
3406
3407 void ext4_set_aops(struct inode *inode)
3408 {
3409 switch (ext4_inode_journal_mode(inode)) {
3410 case EXT4_INODE_ORDERED_DATA_MODE:
3411 ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
3412 break;
3413 case EXT4_INODE_WRITEBACK_DATA_MODE:
3414 ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
3415 break;
3416 case EXT4_INODE_JOURNAL_DATA_MODE:
3417 inode->i_mapping->a_ops = &ext4_journalled_aops;
3418 return;
3419 default:
3420 BUG();
3421 }
3422 if (test_opt(inode->i_sb, DELALLOC))
3423 inode->i_mapping->a_ops = &ext4_da_aops;
3424 else
3425 inode->i_mapping->a_ops = &ext4_aops;
3426 }
3427
3428
3429 /*
3430 * ext4_discard_partial_page_buffers()
3431 * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
3432 * This function finds and locks the page containing the offset
3433 * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
3434 * Calling functions that already have the page locked should call
3435 * ext4_discard_partial_page_buffers_no_lock directly.
3436 */
3437 int ext4_discard_partial_page_buffers(handle_t *handle,
3438 struct address_space *mapping, loff_t from,
3439 loff_t length, int flags)
3440 {
3441 struct inode *inode = mapping->host;
3442 struct page *page;
3443 int err = 0;
3444
3445 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3446 mapping_gfp_mask(mapping) & ~__GFP_FS);
3447 if (!page)
3448 return -ENOMEM;
3449
3450 err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
3451 from, length, flags);
3452
3453 unlock_page(page);
3454 page_cache_release(page);
3455 return err;
3456 }
3457
3458 /*
3459 * ext4_discard_partial_page_buffers_no_lock()
3460 * Zeros a page range of length 'length' starting from offset 'from'.
3461 * Buffer heads that correspond to the block aligned regions of the
3462 * zeroed range will be unmapped. Unblock aligned regions
3463 * will have the corresponding buffer head mapped if needed so that
3464 * that region of the page can be updated with the partial zero out.
3465 *
3466 * This function assumes that the page has already been locked. The
3467 * The range to be discarded must be contained with in the given page.
3468 * If the specified range exceeds the end of the page it will be shortened
3469 * to the end of the page that corresponds to 'from'. This function is
3470 * appropriate for updating a page and it buffer heads to be unmapped and
3471 * zeroed for blocks that have been either released, or are going to be
3472 * released.
3473 *
3474 * handle: The journal handle
3475 * inode: The files inode
3476 * page: A locked page that contains the offset "from"
3477 * from: The starting byte offset (from the beginning of the file)
3478 * to begin discarding
3479 * len: The length of bytes to discard
3480 * flags: Optional flags that may be used:
3481 *
3482 * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
3483 * Only zero the regions of the page whose buffer heads
3484 * have already been unmapped. This flag is appropriate
3485 * for updating the contents of a page whose blocks may
3486 * have already been released, and we only want to zero
3487 * out the regions that correspond to those released blocks.
3488 *
3489 * Returns zero on success or negative on failure.
3490 */
3491 static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
3492 struct inode *inode, struct page *page, loff_t from,
3493 loff_t length, int flags)
3494 {
3495 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3496 unsigned int offset = from & (PAGE_CACHE_SIZE-1);
3497 unsigned int blocksize, max, pos;
3498 ext4_lblk_t iblock;
3499 struct buffer_head *bh;
3500 int err = 0;
3501
3502 blocksize = inode->i_sb->s_blocksize;
3503 max = PAGE_CACHE_SIZE - offset;
3504
3505 if (index != page->index)
3506 return -EINVAL;
3507
3508 /*
3509 * correct length if it does not fall between
3510 * 'from' and the end of the page
3511 */
3512 if (length > max || length < 0)
3513 length = max;
3514
3515 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3516
3517 if (!page_has_buffers(page))
3518 create_empty_buffers(page, blocksize, 0);
3519
3520 /* Find the buffer that contains "offset" */
3521 bh = page_buffers(page);
3522 pos = blocksize;
3523 while (offset >= pos) {
3524 bh = bh->b_this_page;
3525 iblock++;
3526 pos += blocksize;
3527 }
3528
3529 pos = offset;
3530 while (pos < offset + length) {
3531 unsigned int end_of_block, range_to_discard;
3532
3533 err = 0;
3534
3535 /* The length of space left to zero and unmap */
3536 range_to_discard = offset + length - pos;
3537
3538 /* The length of space until the end of the block */
3539 end_of_block = blocksize - (pos & (blocksize-1));
3540
3541 /*
3542 * Do not unmap or zero past end of block
3543 * for this buffer head
3544 */
3545 if (range_to_discard > end_of_block)
3546 range_to_discard = end_of_block;
3547
3548
3549 /*
3550 * Skip this buffer head if we are only zeroing unampped
3551 * regions of the page
3552 */
3553 if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
3554 buffer_mapped(bh))
3555 goto next;
3556
3557 /* If the range is block aligned, unmap */
3558 if (range_to_discard == blocksize) {
3559 clear_buffer_dirty(bh);
3560 bh->b_bdev = NULL;
3561 clear_buffer_mapped(bh);
3562 clear_buffer_req(bh);
3563 clear_buffer_new(bh);
3564 clear_buffer_delay(bh);
3565 clear_buffer_unwritten(bh);
3566 clear_buffer_uptodate(bh);
3567 zero_user(page, pos, range_to_discard);
3568 BUFFER_TRACE(bh, "Buffer discarded");
3569 goto next;
3570 }
3571
3572 /*
3573 * If this block is not completely contained in the range
3574 * to be discarded, then it is not going to be released. Because
3575 * we need to keep this block, we need to make sure this part
3576 * of the page is uptodate before we modify it by writeing
3577 * partial zeros on it.
3578 */
3579 if (!buffer_mapped(bh)) {
3580 /*
3581 * Buffer head must be mapped before we can read
3582 * from the block
3583 */
3584 BUFFER_TRACE(bh, "unmapped");
3585 ext4_get_block(inode, iblock, bh, 0);
3586 /* unmapped? It's a hole - nothing to do */
3587 if (!buffer_mapped(bh)) {
3588 BUFFER_TRACE(bh, "still unmapped");
3589 goto next;
3590 }
3591 }
3592
3593 /* Ok, it's mapped. Make sure it's up-to-date */
3594 if (PageUptodate(page))
3595 set_buffer_uptodate(bh);
3596
3597 if (!buffer_uptodate(bh)) {
3598 err = -EIO;
3599 ll_rw_block(READ, 1, &bh);
3600 wait_on_buffer(bh);
3601 /* Uhhuh. Read error. Complain and punt.*/
3602 if (!buffer_uptodate(bh))
3603 goto next;
3604 }
3605
3606 if (ext4_should_journal_data(inode)) {
3607 BUFFER_TRACE(bh, "get write access");
3608 err = ext4_journal_get_write_access(handle, bh);
3609 if (err)
3610 goto next;
3611 }
3612
3613 zero_user(page, pos, range_to_discard);
3614
3615 err = 0;
3616 if (ext4_should_journal_data(inode)) {
3617 err = ext4_handle_dirty_metadata(handle, inode, bh);
3618 } else
3619 mark_buffer_dirty(bh);
3620
3621 BUFFER_TRACE(bh, "Partial buffer zeroed");
3622 next:
3623 bh = bh->b_this_page;
3624 iblock++;
3625 pos += range_to_discard;
3626 }
3627
3628 return err;
3629 }
3630
3631 int ext4_can_truncate(struct inode *inode)
3632 {
3633 if (S_ISREG(inode->i_mode))
3634 return 1;
3635 if (S_ISDIR(inode->i_mode))
3636 return 1;
3637 if (S_ISLNK(inode->i_mode))
3638 return !ext4_inode_is_fast_symlink(inode);
3639 return 0;
3640 }
3641
3642 /*
3643 * ext4_punch_hole: punches a hole in a file by releasing the blocks
3644 * associated with the given offset and length
3645 *
3646 * @inode: File inode
3647 * @offset: The offset where the hole will begin
3648 * @len: The length of the hole
3649 *
3650 * Returns: 0 on success or negative on failure
3651 */
3652
3653 int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3654 {
3655 #if 0
3656 struct inode *inode = file_inode(file);
3657 struct super_block *sb = inode->i_sb;
3658 ext4_lblk_t first_block, stop_block;
3659 struct address_space *mapping = inode->i_mapping;
3660 loff_t first_page, last_page, page_len;
3661 loff_t first_page_offset, last_page_offset;
3662 handle_t *handle;
3663 unsigned int credits;
3664 int ret = 0;
3665
3666 if (!S_ISREG(inode->i_mode))
3667 return -EOPNOTSUPP;
3668
3669 if (EXT4_SB(sb)->s_cluster_ratio > 1) {
3670 /* TODO: Add support for bigalloc file systems */
3671 return -EOPNOTSUPP;
3672 }
3673
3674 trace_ext4_punch_hole(inode, offset, length);
3675
3676 /*
3677 * Write out all dirty pages to avoid race conditions
3678 * Then release them.
3679 */
3680 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3681 ret = filemap_write_and_wait_range(mapping, offset,
3682 offset + length - 1);
3683 if (ret)
3684 return ret;
3685 }
3686
3687 mutex_lock(&inode->i_mutex);
3688 /* It's not possible punch hole on append only file */
3689 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
3690 ret = -EPERM;
3691 goto out_mutex;
3692 }
3693 if (IS_SWAPFILE(inode)) {
3694 ret = -ETXTBSY;
3695 goto out_mutex;
3696 }
3697
3698 /* No need to punch hole beyond i_size */
3699 if (offset >= inode->i_size)
3700 goto out_mutex;
3701
3702 /*
3703 * If the hole extends beyond i_size, set the hole
3704 * to end after the page that contains i_size
3705 */
3706 if (offset + length > inode->i_size) {
3707 length = inode->i_size +
3708 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
3709 offset;
3710 }
3711
3712 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
3713 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
3714
3715 first_page_offset = first_page << PAGE_CACHE_SHIFT;
3716 last_page_offset = last_page << PAGE_CACHE_SHIFT;
3717
3718 /* Now release the pages */
3719 if (last_page_offset > first_page_offset) {
3720 truncate_pagecache_range(inode, first_page_offset,
3721 last_page_offset - 1);
3722 }
3723
3724 /* Wait all existing dio workers, newcomers will block on i_mutex */
3725 ext4_inode_block_unlocked_dio(inode);
3726 ret = ext4_flush_unwritten_io(inode);
3727 if (ret)
3728 goto out_dio;
3729 inode_dio_wait(inode);
3730
3731 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3732 credits = ext4_writepage_trans_blocks(inode);
3733 else
3734 credits = ext4_blocks_for_truncate(inode);
3735 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3736 if (IS_ERR(handle)) {
3737 ret = PTR_ERR(handle);
3738 ext4_std_error(sb, ret);
3739 goto out_dio;
3740 }
3741
3742 /*
3743 * Now we need to zero out the non-page-aligned data in the
3744 * pages at the start and tail of the hole, and unmap the
3745 * buffer heads for the block aligned regions of the page that
3746 * were completely zeroed.
3747 */
3748 if (first_page > last_page) {
3749 /*
3750 * If the file space being truncated is contained
3751 * within a page just zero out and unmap the middle of
3752 * that page
3753 */
3754 ret = ext4_discard_partial_page_buffers(handle,
3755 mapping, offset, length, 0);
3756
3757 if (ret)
3758 goto out_stop;
3759 } else {
3760 /*
3761 * zero out and unmap the partial page that contains
3762 * the start of the hole
3763 */
3764 page_len = first_page_offset - offset;
3765 if (page_len > 0) {
3766 ret = ext4_discard_partial_page_buffers(handle, mapping,
3767 offset, page_len, 0);
3768 if (ret)
3769 goto out_stop;
3770 }
3771
3772 /*
3773 * zero out and unmap the partial page that contains
3774 * the end of the hole
3775 */
3776 page_len = offset + length - last_page_offset;
3777 if (page_len > 0) {
3778 ret = ext4_discard_partial_page_buffers(handle, mapping,
3779 last_page_offset, page_len, 0);
3780 if (ret)
3781 goto out_stop;
3782 }
3783 }
3784
3785 /*
3786 * If i_size is contained in the last page, we need to
3787 * unmap and zero the partial page after i_size
3788 */
3789 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
3790 inode->i_size % PAGE_CACHE_SIZE != 0) {
3791 page_len = PAGE_CACHE_SIZE -
3792 (inode->i_size & (PAGE_CACHE_SIZE - 1));
3793
3794 if (page_len > 0) {
3795 ret = ext4_discard_partial_page_buffers(handle,
3796 mapping, inode->i_size, page_len, 0);
3797
3798 if (ret)
3799 goto out_stop;
3800 }
3801 }
3802
3803 first_block = (offset + sb->s_blocksize - 1) >>
3804 EXT4_BLOCK_SIZE_BITS(sb);
3805 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
3806
3807 /* If there are no blocks to remove, return now */
3808 if (first_block >= stop_block)
3809 goto out_stop;
3810
3811 down_write(&EXT4_I(inode)->i_data_sem);
3812 ext4_discard_preallocations(inode);
3813
3814 ret = ext4_es_remove_extent(inode, first_block,
3815 stop_block - first_block);
3816 if (ret) {
3817 up_write(&EXT4_I(inode)->i_data_sem);
3818 goto out_stop;
3819 }
3820
3821 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3822 ret = ext4_ext_remove_space(inode, first_block,
3823 stop_block - 1);
3824 else
3825 ret = ext4_free_hole_blocks(handle, inode, first_block,
3826 stop_block);
3827
3828 ext4_discard_preallocations(inode);
3829 up_write(&EXT4_I(inode)->i_data_sem);
3830 if (IS_SYNC(inode))
3831 ext4_handle_sync(handle);
3832 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3833 ext4_mark_inode_dirty(handle, inode);
3834 out_stop:
3835 ext4_journal_stop(handle);
3836 out_dio:
3837 ext4_inode_resume_unlocked_dio(inode);
3838 out_mutex:
3839 mutex_unlock(&inode->i_mutex);
3840 return ret;
3841 #else
3842 /*
3843 * Disabled as per b/28760453
3844 */
3845 return -EOPNOTSUPP;
3846 #endif
3847 }
3848
3849 /*
3850 * ext4_truncate()
3851 *
3852 * We block out ext4_get_block() block instantiations across the entire
3853 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
3854 * simultaneously on behalf of the same inode.
3855 *
3856 * As we work through the truncate and commit bits of it to the journal there
3857 * is one core, guiding principle: the file's tree must always be consistent on
3858 * disk. We must be able to restart the truncate after a crash.
3859 *
3860 * The file's tree may be transiently inconsistent in memory (although it
3861 * probably isn't), but whenever we close off and commit a journal transaction,
3862 * the contents of (the filesystem + the journal) must be consistent and
3863 * restartable. It's pretty simple, really: bottom up, right to left (although
3864 * left-to-right works OK too).
3865 *
3866 * Note that at recovery time, journal replay occurs *before* the restart of
3867 * truncate against the orphan inode list.
3868 *
3869 * The committed inode has the new, desired i_size (which is the same as
3870 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
3871 * that this inode's truncate did not complete and it will again call
3872 * ext4_truncate() to have another go. So there will be instantiated blocks
3873 * to the right of the truncation point in a crashed ext4 filesystem. But
3874 * that's fine - as long as they are linked from the inode, the post-crash
3875 * ext4_truncate() run will find them and release them.
3876 */
3877 void ext4_truncate(struct inode *inode)
3878 {
3879 struct ext4_inode_info *ei = EXT4_I(inode);
3880 unsigned int credits;
3881 handle_t *handle;
3882 struct address_space *mapping = inode->i_mapping;
3883 loff_t page_len;
3884
3885 /*
3886 * There is a possibility that we're either freeing the inode
3887 * or it completely new indode. In those cases we might not
3888 * have i_mutex locked because it's not necessary.
3889 */
3890 if (!(inode->i_state & (I_NEW|I_FREEING)))
3891 WARN_ON(!mutex_is_locked(&inode->i_mutex));
3892 trace_ext4_truncate_enter(inode);
3893
3894 if (!ext4_can_truncate(inode))
3895 return;
3896
3897 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3898
3899 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
3900 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
3901
3902 if (ext4_has_inline_data(inode)) {
3903 int has_inline = 1;
3904
3905 ext4_inline_data_truncate(inode, &has_inline);
3906 if (has_inline)
3907 return;
3908 }
3909
3910 /*
3911 * finish any pending end_io work so we won't run the risk of
3912 * converting any truncated blocks to initialized later
3913 */
3914 ext4_flush_unwritten_io(inode);
3915
3916 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3917 credits = ext4_writepage_trans_blocks(inode);
3918 else
3919 credits = ext4_blocks_for_truncate(inode);
3920
3921 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3922 if (IS_ERR(handle)) {
3923 ext4_std_error(inode->i_sb, PTR_ERR(handle));
3924 return;
3925 }
3926
3927 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
3928 page_len = PAGE_CACHE_SIZE -
3929 (inode->i_size & (PAGE_CACHE_SIZE - 1));
3930
3931 if (ext4_discard_partial_page_buffers(handle,
3932 mapping, inode->i_size, page_len, 0))
3933 goto out_stop;
3934 }
3935
3936 /*
3937 * We add the inode to the orphan list, so that if this
3938 * truncate spans multiple transactions, and we crash, we will
3939 * resume the truncate when the filesystem recovers. It also
3940 * marks the inode dirty, to catch the new size.
3941 *
3942 * Implication: the file must always be in a sane, consistent
3943 * truncatable state while each transaction commits.
3944 */
3945 if (ext4_orphan_add(handle, inode))
3946 goto out_stop;
3947
3948 down_write(&EXT4_I(inode)->i_data_sem);
3949
3950 ext4_discard_preallocations(inode);
3951
3952 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3953 ext4_ext_truncate(handle, inode);
3954 else
3955 ext4_ind_truncate(handle, inode);
3956
3957 up_write(&ei->i_data_sem);
3958
3959 if (IS_SYNC(inode))
3960 ext4_handle_sync(handle);
3961
3962 out_stop:
3963 /*
3964 * If this was a simple ftruncate() and the file will remain alive,
3965 * then we need to clear up the orphan record which we created above.
3966 * However, if this was a real unlink then we were called by
3967 * ext4_delete_inode(), and we allow that function to clean up the
3968 * orphan info for us.
3969 */
3970 if (inode->i_nlink)
3971 ext4_orphan_del(handle, inode);
3972
3973 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3974 ext4_mark_inode_dirty(handle, inode);
3975 ext4_journal_stop(handle);
3976
3977 trace_ext4_truncate_exit(inode);
3978 }
3979
3980 /*
3981 * ext4_get_inode_loc returns with an extra refcount against the inode's
3982 * underlying buffer_head on success. If 'in_mem' is true, we have all
3983 * data in memory that is needed to recreate the on-disk version of this
3984 * inode.
3985 */
3986 static int __ext4_get_inode_loc(struct inode *inode,
3987 struct ext4_iloc *iloc, int in_mem)
3988 {
3989 struct ext4_group_desc *gdp;
3990 struct buffer_head *bh;
3991 struct super_block *sb = inode->i_sb;
3992 ext4_fsblk_t block;
3993 int inodes_per_block, inode_offset;
3994
3995 iloc->bh = NULL;
3996 if (!ext4_valid_inum(sb, inode->i_ino))
3997 return -EIO;
3998
3999 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4000 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4001 if (!gdp)
4002 return -EIO;
4003
4004 /*
4005 * Figure out the offset within the block group inode table
4006 */
4007 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4008 inode_offset = ((inode->i_ino - 1) %
4009 EXT4_INODES_PER_GROUP(sb));
4010 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4011 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4012
4013 bh = sb_getblk(sb, block);
4014 if (unlikely(!bh))
4015 return -ENOMEM;
4016 if (!buffer_uptodate(bh)) {
4017 lock_buffer(bh);
4018
4019 /*
4020 * If the buffer has the write error flag, we have failed
4021 * to write out another inode in the same block. In this
4022 * case, we don't have to read the block because we may
4023 * read the old inode data successfully.
4024 */
4025 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4026 set_buffer_uptodate(bh);
4027
4028 if (buffer_uptodate(bh)) {
4029 /* someone brought it uptodate while we waited */
4030 unlock_buffer(bh);
4031 goto has_buffer;
4032 }
4033
4034 /*
4035 * If we have all information of the inode in memory and this
4036 * is the only valid inode in the block, we need not read the
4037 * block.
4038 */
4039 if (in_mem) {
4040 struct buffer_head *bitmap_bh;
4041 int i, start;
4042
4043 start = inode_offset & ~(inodes_per_block - 1);
4044
4045 /* Is the inode bitmap in cache? */
4046 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
4047 if (unlikely(!bitmap_bh))
4048 goto make_io;
4049
4050 /*
4051 * If the inode bitmap isn't in cache then the
4052 * optimisation may end up performing two reads instead
4053 * of one, so skip it.
4054 */
4055 if (!buffer_uptodate(bitmap_bh)) {
4056 brelse(bitmap_bh);
4057 goto make_io;
4058 }
4059 for (i = start; i < start + inodes_per_block; i++) {
4060 if (i == inode_offset)
4061 continue;
4062 if (ext4_test_bit(i, bitmap_bh->b_data))
4063 break;
4064 }
4065 brelse(bitmap_bh);
4066 if (i == start + inodes_per_block) {
4067 /* all other inodes are free, so skip I/O */
4068 memset(bh->b_data, 0, bh->b_size);
4069 set_buffer_uptodate(bh);
4070 unlock_buffer(bh);
4071 goto has_buffer;
4072 }
4073 }
4074
4075 make_io:
4076 /*
4077 * If we need to do any I/O, try to pre-readahead extra
4078 * blocks from the inode table.
4079 */
4080 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4081 ext4_fsblk_t b, end, table;
4082 unsigned num;
4083 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
4084
4085 table = ext4_inode_table(sb, gdp);
4086 /* s_inode_readahead_blks is always a power of 2 */
4087 b = block & ~((ext4_fsblk_t) ra_blks - 1);
4088 if (table > b)
4089 b = table;
4090 end = b + ra_blks;
4091 num = EXT4_INODES_PER_GROUP(sb);
4092 if (ext4_has_group_desc_csum(sb))
4093 num -= ext4_itable_unused_count(sb, gdp);
4094 table += num / inodes_per_block;
4095 if (end > table)
4096 end = table;
4097 while (b <= end)
4098 sb_breadahead(sb, b++);
4099 }
4100
4101 /*
4102 * There are other valid inodes in the buffer, this inode
4103 * has in-inode xattrs, or we don't have this inode in memory.
4104 * Read the block from disk.
4105 */
4106 trace_ext4_load_inode(inode);
4107 get_bh(bh);
4108 bh->b_end_io = end_buffer_read_sync;
4109 #ifdef FEATURE_STORAGE_META_LOG
4110 if( bh && bh->b_bdev && bh->b_bdev->bd_disk)
4111 set_metadata_rw_status(bh->b_bdev->bd_disk->first_minor, WAIT_READ_CNT);
4112 #endif
4113 submit_bh(READ | REQ_META | REQ_PRIO, bh);
4114 wait_on_buffer(bh);
4115 if (!buffer_uptodate(bh)) {
4116 EXT4_ERROR_INODE_BLOCK(inode, block,
4117 "unable to read itable block");
4118 brelse(bh);
4119 return -EIO;
4120 }
4121 }
4122 has_buffer:
4123 iloc->bh = bh;
4124 return 0;
4125 }
4126
4127 int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
4128 {
4129 /* We have all inode data except xattrs in memory here. */
4130 return __ext4_get_inode_loc(inode, iloc,
4131 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
4132 }
4133
4134 void ext4_set_inode_flags(struct inode *inode)
4135 {
4136 unsigned int flags = EXT4_I(inode)->i_flags;
4137 unsigned int new_fl = 0;
4138
4139 if (flags & EXT4_SYNC_FL)
4140 new_fl |= S_SYNC;
4141 if (flags & EXT4_APPEND_FL)
4142 new_fl |= S_APPEND;
4143 if (flags & EXT4_IMMUTABLE_FL)
4144 new_fl |= S_IMMUTABLE;
4145 if (flags & EXT4_NOATIME_FL)
4146 new_fl |= S_NOATIME;
4147 if (flags & EXT4_DIRSYNC_FL)
4148 new_fl |= S_DIRSYNC;
4149 set_mask_bits(&inode->i_flags,
4150 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC, new_fl);
4151 }
4152
4153 /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4154 void ext4_get_inode_flags(struct ext4_inode_info *ei)
4155 {
4156 unsigned int vfs_fl;
4157 unsigned long old_fl, new_fl;
4158
4159 do {
4160 vfs_fl = ei->vfs_inode.i_flags;
4161 old_fl = ei->i_flags;
4162 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4163 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4164 EXT4_DIRSYNC_FL);
4165 if (vfs_fl & S_SYNC)
4166 new_fl |= EXT4_SYNC_FL;
4167 if (vfs_fl & S_APPEND)
4168 new_fl |= EXT4_APPEND_FL;
4169 if (vfs_fl & S_IMMUTABLE)
4170 new_fl |= EXT4_IMMUTABLE_FL;
4171 if (vfs_fl & S_NOATIME)
4172 new_fl |= EXT4_NOATIME_FL;
4173 if (vfs_fl & S_DIRSYNC)
4174 new_fl |= EXT4_DIRSYNC_FL;
4175 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
4176 }
4177
4178 static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
4179 struct ext4_inode_info *ei)
4180 {
4181 blkcnt_t i_blocks ;
4182 struct inode *inode = &(ei->vfs_inode);
4183 struct super_block *sb = inode->i_sb;
4184
4185 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4186 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4187 /* we are using combined 48 bit field */
4188 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4189 le32_to_cpu(raw_inode->i_blocks_lo);
4190 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
4191 /* i_blocks represent file system block size */
4192 return i_blocks << (inode->i_blkbits - 9);
4193 } else {
4194 return i_blocks;
4195 }
4196 } else {
4197 return le32_to_cpu(raw_inode->i_blocks_lo);
4198 }
4199 }
4200
4201 static inline void ext4_iget_extra_inode(struct inode *inode,
4202 struct ext4_inode *raw_inode,
4203 struct ext4_inode_info *ei)
4204 {
4205 __le32 *magic = (void *)raw_inode +
4206 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
4207 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
4208 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
4209 ext4_find_inline_data_nolock(inode);
4210 } else
4211 EXT4_I(inode)->i_inline_off = 0;
4212 }
4213
4214 struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
4215 {
4216 struct ext4_iloc iloc;
4217 struct ext4_inode *raw_inode;
4218 struct ext4_inode_info *ei;
4219 struct inode *inode;
4220 journal_t *journal = EXT4_SB(sb)->s_journal;
4221 long ret;
4222 loff_t size;
4223 int block;
4224 uid_t i_uid;
4225 gid_t i_gid;
4226
4227 inode = iget_locked(sb, ino);
4228 if (!inode)
4229 return ERR_PTR(-ENOMEM);
4230 if (!(inode->i_state & I_NEW))
4231 return inode;
4232
4233 ei = EXT4_I(inode);
4234 iloc.bh = NULL;
4235
4236 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4237 if (ret < 0)
4238 goto bad_inode;
4239 raw_inode = ext4_raw_inode(&iloc);
4240
4241 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4242 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4243 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4244 EXT4_INODE_SIZE(inode->i_sb)) {
4245 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4246 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4247 EXT4_INODE_SIZE(inode->i_sb));
4248 ret = -EIO;
4249 goto bad_inode;
4250 }
4251 } else
4252 ei->i_extra_isize = 0;
4253
4254 /* Precompute checksum seed for inode metadata */
4255 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4256 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
4257 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4258 __u32 csum;
4259 __le32 inum = cpu_to_le32(inode->i_ino);
4260 __le32 gen = raw_inode->i_generation;
4261 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4262 sizeof(inum));
4263 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4264 sizeof(gen));
4265 }
4266
4267 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4268 EXT4_ERROR_INODE(inode, "checksum invalid");
4269 ret = -EIO;
4270 goto bad_inode;
4271 }
4272
4273 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4274 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4275 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
4276 if (!(test_opt(inode->i_sb, NO_UID32))) {
4277 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4278 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4279 }
4280 i_uid_write(inode, i_uid);
4281 i_gid_write(inode, i_gid);
4282 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
4283
4284 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
4285 ei->i_inline_off = 0;
4286 ei->i_dir_start_lookup = 0;
4287 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4288 /* We now have enough fields to check if the inode was active or not.
4289 * This is needed because nfsd might try to access dead inodes
4290 * the test is that same one that e2fsck uses
4291 * NeilBrown 1999oct15
4292 */
4293 if (inode->i_nlink == 0) {
4294 if ((inode->i_mode == 0 ||
4295 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4296 ino != EXT4_BOOT_LOADER_INO) {
4297 /* this inode is deleted */
4298 ret = -ESTALE;
4299 goto bad_inode;
4300 }
4301 /* The only unlinked inodes we let through here have
4302 * valid i_mode and are being read by the orphan
4303 * recovery code: that's fine, we're about to complete
4304 * the process of deleting those.
4305 * OR it is the EXT4_BOOT_LOADER_INO which is
4306 * not initialized on a new filesystem. */
4307 }
4308 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
4309 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
4310 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
4311 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
4312 ei->i_file_acl |=
4313 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4314 inode->i_size = ext4_isize(raw_inode);
4315 if ((size = i_size_read(inode)) < 0) {
4316 EXT4_ERROR_INODE(inode, "bad i_size value: %lld", size);
4317 ret = -EIO;
4318 goto bad_inode;
4319 }
4320 ei->i_disksize = inode->i_size;
4321 #ifdef CONFIG_QUOTA
4322 ei->i_reserved_quota = 0;
4323 #endif
4324 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4325 ei->i_block_group = iloc.block_group;
4326 ei->i_last_alloc_group = ~0;
4327 /*
4328 * NOTE! The in-memory inode i_data array is in little-endian order
4329 * even on big-endian machines: we do NOT byteswap the block numbers!
4330 */
4331 for (block = 0; block < EXT4_N_BLOCKS; block++)
4332 ei->i_data[block] = raw_inode->i_block[block];
4333 INIT_LIST_HEAD(&ei->i_orphan);
4334
4335 /*
4336 * Set transaction id's of transactions that have to be committed
4337 * to finish f[data]sync. We set them to currently running transaction
4338 * as we cannot be sure that the inode or some of its metadata isn't
4339 * part of the transaction - the inode could have been reclaimed and
4340 * now it is reread from disk.
4341 */
4342 if (journal) {
4343 transaction_t *transaction;
4344 tid_t tid;
4345
4346 read_lock(&journal->j_state_lock);
4347 if (journal->j_running_transaction)
4348 transaction = journal->j_running_transaction;
4349 else
4350 transaction = journal->j_committing_transaction;
4351 if (transaction)
4352 tid = transaction->t_tid;
4353 else
4354 tid = journal->j_commit_sequence;
4355 read_unlock(&journal->j_state_lock);
4356 ei->i_sync_tid = tid;
4357 ei->i_datasync_tid = tid;
4358 }
4359
4360 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4361 if (ei->i_extra_isize == 0) {
4362 /* The extra space is currently unused. Use it. */
4363 ei->i_extra_isize = sizeof(struct ext4_inode) -
4364 EXT4_GOOD_OLD_INODE_SIZE;
4365 } else {
4366 ext4_iget_extra_inode(inode, raw_inode, ei);
4367 }
4368 }
4369
4370 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4371 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4372 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4373 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4374
4375 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4376 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4377 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4378 inode->i_version |=
4379 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4380 }
4381
4382 ret = 0;
4383 if (ei->i_file_acl &&
4384 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
4385 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4386 ei->i_file_acl);
4387 ret = -EIO;
4388 goto bad_inode;
4389 } else if (!ext4_has_inline_data(inode)) {
4390 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4391 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4392 (S_ISLNK(inode->i_mode) &&
4393 !ext4_inode_is_fast_symlink(inode))))
4394 /* Validate extent which is part of inode */
4395 ret = ext4_ext_check_inode(inode);
4396 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4397 (S_ISLNK(inode->i_mode) &&
4398 !ext4_inode_is_fast_symlink(inode))) {
4399 /* Validate block references which are part of inode */
4400 ret = ext4_ind_check_inode(inode);
4401 }
4402 }
4403 if (ret)
4404 goto bad_inode;
4405
4406 if (S_ISREG(inode->i_mode)) {
4407 inode->i_op = &ext4_file_inode_operations;
4408 inode->i_fop = &ext4_file_operations;
4409 ext4_set_aops(inode);
4410 } else if (S_ISDIR(inode->i_mode)) {
4411 inode->i_op = &ext4_dir_inode_operations;
4412 inode->i_fop = &ext4_dir_operations;
4413 } else if (S_ISLNK(inode->i_mode)) {
4414 if (ext4_inode_is_fast_symlink(inode)) {
4415 inode->i_op = &ext4_fast_symlink_inode_operations;
4416 nd_terminate_link(ei->i_data, inode->i_size,
4417 sizeof(ei->i_data) - 1);
4418 } else {
4419 inode->i_op = &ext4_symlink_inode_operations;
4420 ext4_set_aops(inode);
4421 }
4422 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4423 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
4424 inode->i_op = &ext4_special_inode_operations;
4425 if (raw_inode->i_block[0])
4426 init_special_inode(inode, inode->i_mode,
4427 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4428 else
4429 init_special_inode(inode, inode->i_mode,
4430 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4431 } else if (ino == EXT4_BOOT_LOADER_INO) {
4432 make_bad_inode(inode);
4433 } else {
4434 ret = -EIO;
4435 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
4436 goto bad_inode;
4437 }
4438 brelse(iloc.bh);
4439 ext4_set_inode_flags(inode);
4440 unlock_new_inode(inode);
4441 return inode;
4442
4443 bad_inode:
4444 brelse(iloc.bh);
4445 iget_failed(inode);
4446 return ERR_PTR(ret);
4447 }
4448
4449 struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
4450 {
4451 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
4452 return ERR_PTR(-EIO);
4453 return ext4_iget(sb, ino);
4454 }
4455
4456 static int ext4_inode_blocks_set(handle_t *handle,
4457 struct ext4_inode *raw_inode,
4458 struct ext4_inode_info *ei)
4459 {
4460 struct inode *inode = &(ei->vfs_inode);
4461 u64 i_blocks = inode->i_blocks;
4462 struct super_block *sb = inode->i_sb;
4463
4464 if (i_blocks <= ~0U) {
4465 /*
4466 * i_blocks can be represented in a 32 bit variable
4467 * as multiple of 512 bytes
4468 */
4469 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4470 raw_inode->i_blocks_high = 0;
4471 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4472 return 0;
4473 }
4474 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4475 return -EFBIG;
4476
4477 if (i_blocks <= 0xffffffffffffULL) {
4478 /*
4479 * i_blocks can be represented in a 48 bit variable
4480 * as multiple of 512 bytes
4481 */
4482 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4483 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
4484 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4485 } else {
4486 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4487 /* i_block is stored in file system block size */
4488 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4489 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4490 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
4491 }
4492 return 0;
4493 }
4494
4495 /*
4496 * Post the struct inode info into an on-disk inode location in the
4497 * buffer-cache. This gobbles the caller's reference to the
4498 * buffer_head in the inode location struct.
4499 *
4500 * The caller must have write access to iloc->bh.
4501 */
4502 static int ext4_do_update_inode(handle_t *handle,
4503 struct inode *inode,
4504 struct ext4_iloc *iloc)
4505 {
4506 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4507 struct ext4_inode_info *ei = EXT4_I(inode);
4508 struct buffer_head *bh = iloc->bh;
4509 int err = 0, rc, block;
4510 int need_datasync = 0;
4511 uid_t i_uid;
4512 gid_t i_gid;
4513
4514 /* For fields not not tracking in the in-memory inode,
4515 * initialise them to zero for new inodes. */
4516 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
4517 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
4518
4519 ext4_get_inode_flags(ei);
4520 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
4521 i_uid = i_uid_read(inode);
4522 i_gid = i_gid_read(inode);
4523 if (!(test_opt(inode->i_sb, NO_UID32))) {
4524 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4525 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
4526 /*
4527 * Fix up interoperability with old kernels. Otherwise, old inodes get
4528 * re-used with the upper 16 bits of the uid/gid intact
4529 */
4530 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4531 raw_inode->i_uid_high = 0;
4532 raw_inode->i_gid_high = 0;
4533 } else {
4534 raw_inode->i_uid_high =
4535 cpu_to_le16(high_16_bits(i_uid));
4536 raw_inode->i_gid_high =
4537 cpu_to_le16(high_16_bits(i_gid));
4538 }
4539 } else {
4540 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4541 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
4542 raw_inode->i_uid_high = 0;
4543 raw_inode->i_gid_high = 0;
4544 }
4545 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
4546
4547 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4548 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4549 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4550 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4551
4552 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4553 goto out_brelse;
4554 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4555 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
4556 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4557 cpu_to_le32(EXT4_OS_HURD))
4558 raw_inode->i_file_acl_high =
4559 cpu_to_le16(ei->i_file_acl >> 32);
4560 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
4561 if (ei->i_disksize != ext4_isize(raw_inode)) {
4562 ext4_isize_set(raw_inode, ei->i_disksize);
4563 need_datasync = 1;
4564 }
4565 if (ei->i_disksize > 0x7fffffffULL) {
4566 struct super_block *sb = inode->i_sb;
4567 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4568 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4569 EXT4_SB(sb)->s_es->s_rev_level ==
4570 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4571 /* If this is the first large file
4572 * created, add a flag to the superblock.
4573 */
4574 err = ext4_journal_get_write_access(handle,
4575 EXT4_SB(sb)->s_sbh);
4576 if (err)
4577 goto out_brelse;
4578 ext4_update_dynamic_rev(sb);
4579 EXT4_SET_RO_COMPAT_FEATURE(sb,
4580 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4581 ext4_handle_sync(handle);
4582 err = ext4_handle_dirty_super(handle, sb);
4583 }
4584 }
4585 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4586 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4587 if (old_valid_dev(inode->i_rdev)) {
4588 raw_inode->i_block[0] =
4589 cpu_to_le32(old_encode_dev(inode->i_rdev));
4590 raw_inode->i_block[1] = 0;
4591 } else {
4592 raw_inode->i_block[0] = 0;
4593 raw_inode->i_block[1] =
4594 cpu_to_le32(new_encode_dev(inode->i_rdev));
4595 raw_inode->i_block[2] = 0;
4596 }
4597 } else if (!ext4_has_inline_data(inode)) {
4598 for (block = 0; block < EXT4_N_BLOCKS; block++)
4599 raw_inode->i_block[block] = ei->i_data[block];
4600 }
4601
4602 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4603 if (ei->i_extra_isize) {
4604 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4605 raw_inode->i_version_hi =
4606 cpu_to_le32(inode->i_version >> 32);
4607 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
4608 }
4609
4610 ext4_inode_csum_set(inode, raw_inode, ei);
4611
4612 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4613 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
4614 if (!err)
4615 err = rc;
4616 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
4617
4618 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
4619 out_brelse:
4620 brelse(bh);
4621 ext4_std_error(inode->i_sb, err);
4622 return err;
4623 }
4624
4625 /*
4626 * ext4_write_inode()
4627 *
4628 * We are called from a few places:
4629 *
4630 * - Within generic_file_write() for O_SYNC files.
4631 * Here, there will be no transaction running. We wait for any running
4632 * transaction to commit.
4633 *
4634 * - Within sys_sync(), kupdate and such.
4635 * We wait on commit, if tol to.
4636 *
4637 * - Within prune_icache() (PF_MEMALLOC == true)
4638 * Here we simply return. We can't afford to block kswapd on the
4639 * journal commit.
4640 *
4641 * In all cases it is actually safe for us to return without doing anything,
4642 * because the inode has been copied into a raw inode buffer in
4643 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
4644 * knfsd.
4645 *
4646 * Note that we are absolutely dependent upon all inode dirtiers doing the
4647 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4648 * which we are interested.
4649 *
4650 * It would be a bug for them to not do this. The code:
4651 *
4652 * mark_inode_dirty(inode)
4653 * stuff();
4654 * inode->i_size = expr;
4655 *
4656 * is in error because a kswapd-driven write_inode() could occur while
4657 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4658 * will no longer be on the superblock's dirty inode list.
4659 */
4660 int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
4661 {
4662 int err;
4663
4664 if (current->flags & PF_MEMALLOC)
4665 return 0;
4666
4667 if (EXT4_SB(inode->i_sb)->s_journal) {
4668 if (ext4_journal_current_handle()) {
4669 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4670 dump_stack();
4671 return -EIO;
4672 }
4673
4674 if (wbc->sync_mode != WB_SYNC_ALL)
4675 return 0;
4676
4677 err = ext4_force_commit(inode->i_sb);
4678 } else {
4679 struct ext4_iloc iloc;
4680
4681 err = __ext4_get_inode_loc(inode, &iloc, 0);
4682 if (err)
4683 return err;
4684 if (wbc->sync_mode == WB_SYNC_ALL)
4685 sync_dirty_buffer(iloc.bh);
4686 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
4687 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4688 "IO error syncing inode");
4689 err = -EIO;
4690 }
4691 brelse(iloc.bh);
4692 }
4693 return err;
4694 }
4695
4696 /*
4697 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4698 * buffers that are attached to a page stradding i_size and are undergoing
4699 * commit. In that case we have to wait for commit to finish and try again.
4700 */
4701 static void ext4_wait_for_tail_page_commit(struct inode *inode)
4702 {
4703 struct page *page;
4704 unsigned offset;
4705 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
4706 tid_t commit_tid = 0;
4707 int ret;
4708
4709 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
4710 /*
4711 * All buffers in the last page remain valid? Then there's nothing to
4712 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4713 * blocksize case
4714 */
4715 if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
4716 return;
4717 while (1) {
4718 page = find_lock_page(inode->i_mapping,
4719 inode->i_size >> PAGE_CACHE_SHIFT);
4720 if (!page)
4721 return;
4722 ret = __ext4_journalled_invalidatepage(page, offset);
4723 unlock_page(page);
4724 page_cache_release(page);
4725 if (ret != -EBUSY)
4726 return;
4727 commit_tid = 0;
4728 read_lock(&journal->j_state_lock);
4729 if (journal->j_committing_transaction)
4730 commit_tid = journal->j_committing_transaction->t_tid;
4731 read_unlock(&journal->j_state_lock);
4732 if (commit_tid)
4733 jbd2_log_wait_commit(journal, commit_tid);
4734 }
4735 }
4736
4737 /*
4738 * ext4_setattr()
4739 *
4740 * Called from notify_change.
4741 *
4742 * We want to trap VFS attempts to truncate the file as soon as
4743 * possible. In particular, we want to make sure that when the VFS
4744 * shrinks i_size, we put the inode on the orphan list and modify
4745 * i_disksize immediately, so that during the subsequent flushing of
4746 * dirty pages and freeing of disk blocks, we can guarantee that any
4747 * commit will leave the blocks being flushed in an unused state on
4748 * disk. (On recovery, the inode will get truncated and the blocks will
4749 * be freed, so we have a strong guarantee that no future commit will
4750 * leave these blocks visible to the user.)
4751 *
4752 * Another thing we have to assure is that if we are in ordered mode
4753 * and inode is still attached to the committing transaction, we must
4754 * we start writeout of all the dirty pages which are being truncated.
4755 * This way we are sure that all the data written in the previous
4756 * transaction are already on disk (truncate waits for pages under
4757 * writeback).
4758 *
4759 * Called with inode->i_mutex down.
4760 */
4761 int ext4_setattr(struct dentry *dentry, struct iattr *attr)
4762 {
4763 struct inode *inode = dentry->d_inode;
4764 int error, rc = 0;
4765 int orphan = 0;
4766 const unsigned int ia_valid = attr->ia_valid;
4767
4768 error = inode_change_ok(inode, attr);
4769 if (error)
4770 return error;
4771
4772 if (is_quota_modification(inode, attr))
4773 dquot_initialize(inode);
4774 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
4775 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
4776 handle_t *handle;
4777
4778 /* (user+group)*(old+new) structure, inode write (sb,
4779 * inode block, ? - but truncate inode update has it) */
4780 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4781 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4782 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
4783 if (IS_ERR(handle)) {
4784 error = PTR_ERR(handle);
4785 goto err_out;
4786 }
4787 error = dquot_transfer(inode, attr);
4788 if (error) {
4789 ext4_journal_stop(handle);
4790 return error;
4791 }
4792 /* Update corresponding info in inode so that everything is in
4793 * one transaction */
4794 if (attr->ia_valid & ATTR_UID)
4795 inode->i_uid = attr->ia_uid;
4796 if (attr->ia_valid & ATTR_GID)
4797 inode->i_gid = attr->ia_gid;
4798 error = ext4_mark_inode_dirty(handle, inode);
4799 ext4_journal_stop(handle);
4800 }
4801
4802 if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
4803 handle_t *handle;
4804 loff_t oldsize = inode->i_size;
4805
4806 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4807 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4808
4809 if (attr->ia_size > sbi->s_bitmap_maxbytes)
4810 return -EFBIG;
4811 }
4812
4813 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
4814 inode_inc_iversion(inode);
4815
4816 if (S_ISREG(inode->i_mode) &&
4817 (attr->ia_size < inode->i_size)) {
4818 if (ext4_should_order_data(inode)) {
4819 error = ext4_begin_ordered_truncate(inode,
4820 attr->ia_size);
4821 if (error)
4822 goto err_out;
4823 }
4824 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4825 if (IS_ERR(handle)) {
4826 error = PTR_ERR(handle);
4827 goto err_out;
4828 }
4829 if (ext4_handle_valid(handle)) {
4830 error = ext4_orphan_add(handle, inode);
4831 orphan = 1;
4832 }
4833 EXT4_I(inode)->i_disksize = attr->ia_size;
4834 rc = ext4_mark_inode_dirty(handle, inode);
4835 if (!error)
4836 error = rc;
4837 ext4_journal_stop(handle);
4838 if (error) {
4839 ext4_orphan_del(NULL, inode);
4840 goto err_out;
4841 }
4842 }
4843
4844 i_size_write(inode, attr->ia_size);
4845 /*
4846 * Blocks are going to be removed from the inode. Wait
4847 * for dio in flight. Temporarily disable
4848 * dioread_nolock to prevent livelock.
4849 */
4850 if (orphan) {
4851 if (!ext4_should_journal_data(inode)) {
4852 ext4_inode_block_unlocked_dio(inode);
4853 inode_dio_wait(inode);
4854 ext4_inode_resume_unlocked_dio(inode);
4855 } else
4856 ext4_wait_for_tail_page_commit(inode);
4857 }
4858 /*
4859 * Truncate pagecache after we've waited for commit
4860 * in data=journal mode to make pages freeable.
4861 */
4862 truncate_pagecache(inode, oldsize, inode->i_size);
4863 }
4864 /*
4865 * We want to call ext4_truncate() even if attr->ia_size ==
4866 * inode->i_size for cases like truncation of fallocated space
4867 */
4868 if (attr->ia_valid & ATTR_SIZE)
4869 ext4_truncate(inode);
4870
4871 if (!rc) {
4872 setattr_copy(inode, attr);
4873 mark_inode_dirty(inode);
4874 }
4875
4876 /*
4877 * If the call to ext4_truncate failed to get a transaction handle at
4878 * all, we need to clean up the in-core orphan list manually.
4879 */
4880 if (orphan && inode->i_nlink)
4881 ext4_orphan_del(NULL, inode);
4882
4883 if (!rc && (ia_valid & ATTR_MODE))
4884 rc = ext4_acl_chmod(inode);
4885
4886 err_out:
4887 ext4_std_error(inode->i_sb, error);
4888 if (!error)
4889 error = rc;
4890 return error;
4891 }
4892
4893 int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4894 struct kstat *stat)
4895 {
4896 struct inode *inode;
4897 unsigned long long delalloc_blocks;
4898
4899 inode = dentry->d_inode;
4900 generic_fillattr(inode, stat);
4901
4902 /*
4903 * We can't update i_blocks if the block allocation is delayed
4904 * otherwise in the case of system crash before the real block
4905 * allocation is done, we will have i_blocks inconsistent with
4906 * on-disk file blocks.
4907 * We always keep i_blocks updated together with real
4908 * allocation. But to not confuse with user, stat
4909 * will return the blocks that include the delayed allocation
4910 * blocks for this file.
4911 */
4912 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
4913 EXT4_I(inode)->i_reserved_data_blocks);
4914
4915 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits-9);
4916 return 0;
4917 }
4918
4919 static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4920 {
4921 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
4922 return ext4_ind_trans_blocks(inode, nrblocks, chunk);
4923 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
4924 }
4925
4926 /*
4927 * Account for index blocks, block groups bitmaps and block group
4928 * descriptor blocks if modify datablocks and index blocks
4929 * worse case, the indexs blocks spread over different block groups
4930 *
4931 * If datablocks are discontiguous, they are possible to spread over
4932 * different block groups too. If they are contiguous, with flexbg,
4933 * they could still across block group boundary.
4934 *
4935 * Also account for superblock, inode, quota and xattr blocks
4936 */
4937 static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4938 {
4939 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4940 int gdpblocks;
4941 int idxblocks;
4942 int ret = 0;
4943
4944 /*
4945 * How many index blocks need to touch to modify nrblocks?
4946 * The "Chunk" flag indicating whether the nrblocks is
4947 * physically contiguous on disk
4948 *
4949 * For Direct IO and fallocate, they calls get_block to allocate
4950 * one single extent at a time, so they could set the "Chunk" flag
4951 */
4952 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4953
4954 ret = idxblocks;
4955
4956 /*
4957 * Now let's see how many group bitmaps and group descriptors need
4958 * to account
4959 */
4960 groups = idxblocks;
4961 if (chunk)
4962 groups += 1;
4963 else
4964 groups += nrblocks;
4965
4966 gdpblocks = groups;
4967 if (groups > ngroups)
4968 groups = ngroups;
4969 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4970 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4971
4972 /* bitmaps and block group descriptor blocks */
4973 ret += groups + gdpblocks;
4974
4975 /* Blocks for super block, inode, quota and xattr blocks */
4976 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
4977
4978 return ret;
4979 }
4980
4981 /*
4982 * Calculate the total number of credits to reserve to fit
4983 * the modification of a single pages into a single transaction,
4984 * which may include multiple chunks of block allocations.
4985 *
4986 * This could be called via ext4_write_begin()
4987 *
4988 * We need to consider the worse case, when
4989 * one new block per extent.
4990 */
4991 int ext4_writepage_trans_blocks(struct inode *inode)
4992 {
4993 int bpp = ext4_journal_blocks_per_page(inode);
4994 int ret;
4995
4996 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4997
4998 /* Account for data blocks for journalled mode */
4999 if (ext4_should_journal_data(inode))
5000 ret += bpp;
5001 return ret;
5002 }
5003
5004 /*
5005 * Calculate the journal credits for a chunk of data modification.
5006 *
5007 * This is called from DIO, fallocate or whoever calling
5008 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
5009 *
5010 * journal buffers for data blocks are not included here, as DIO
5011 * and fallocate do no need to journal data buffers.
5012 */
5013 int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5014 {
5015 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5016 }
5017
5018 /*
5019 * The caller must have previously called ext4_reserve_inode_write().
5020 * Give this, we know that the caller already has write access to iloc->bh.
5021 */
5022 int ext4_mark_iloc_dirty(handle_t *handle,
5023 struct inode *inode, struct ext4_iloc *iloc)
5024 {
5025 int err = 0;
5026
5027 if (IS_I_VERSION(inode))
5028 inode_inc_iversion(inode);
5029
5030 /* the do_update_inode consumes one bh->b_count */
5031 get_bh(iloc->bh);
5032
5033 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
5034 err = ext4_do_update_inode(handle, inode, iloc);
5035 put_bh(iloc->bh);
5036 return err;
5037 }
5038
5039 /*
5040 * On success, We end up with an outstanding reference count against
5041 * iloc->bh. This _must_ be cleaned up later.
5042 */
5043
5044 int
5045 ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5046 struct ext4_iloc *iloc)
5047 {
5048 int err;
5049
5050 err = ext4_get_inode_loc(inode, iloc);
5051 if (!err) {
5052 BUFFER_TRACE(iloc->bh, "get_write_access");
5053 err = ext4_journal_get_write_access(handle, iloc->bh);
5054 if (err) {
5055 brelse(iloc->bh);
5056 iloc->bh = NULL;
5057 }
5058 }
5059 ext4_std_error(inode->i_sb, err);
5060 return err;
5061 }
5062
5063 /*
5064 * Expand an inode by new_extra_isize bytes.
5065 * Returns 0 on success or negative error number on failure.
5066 */
5067 static int ext4_expand_extra_isize(struct inode *inode,
5068 unsigned int new_extra_isize,
5069 struct ext4_iloc iloc,
5070 handle_t *handle)
5071 {
5072 struct ext4_inode *raw_inode;
5073 struct ext4_xattr_ibody_header *header;
5074
5075 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5076 return 0;
5077
5078 raw_inode = ext4_raw_inode(&iloc);
5079
5080 header = IHDR(inode, raw_inode);
5081
5082 /* No extended attributes present */
5083 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5084 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5085 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5086 new_extra_isize);
5087 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5088 return 0;
5089 }
5090
5091 /* try to expand with EAs present */
5092 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5093 raw_inode, handle);
5094 }
5095
5096 /*
5097 * What we do here is to mark the in-core inode as clean with respect to inode
5098 * dirtiness (it may still be data-dirty).
5099 * This means that the in-core inode may be reaped by prune_icache
5100 * without having to perform any I/O. This is a very good thing,
5101 * because *any* task may call prune_icache - even ones which
5102 * have a transaction open against a different journal.
5103 *
5104 * Is this cheating? Not really. Sure, we haven't written the
5105 * inode out, but prune_icache isn't a user-visible syncing function.
5106 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5107 * we start and wait on commits.
5108 */
5109 int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
5110 {
5111 struct ext4_iloc iloc;
5112 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5113 static unsigned int mnt_count;
5114 int err, ret;
5115
5116 might_sleep();
5117 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
5118 err = ext4_reserve_inode_write(handle, inode, &iloc);
5119 if (err)
5120 return err;
5121 if (ext4_handle_valid(handle) &&
5122 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
5123 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5124 /*
5125 * We need extra buffer credits since we may write into EA block
5126 * with this same handle. If journal_extend fails, then it will
5127 * only result in a minor loss of functionality for that inode.
5128 * If this is felt to be critical, then e2fsck should be run to
5129 * force a large enough s_min_extra_isize.
5130 */
5131 if ((jbd2_journal_extend(handle,
5132 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5133 ret = ext4_expand_extra_isize(inode,
5134 sbi->s_want_extra_isize,
5135 iloc, handle);
5136 if (ret) {
5137 ext4_set_inode_state(inode,
5138 EXT4_STATE_NO_EXPAND);
5139 if (mnt_count !=
5140 le16_to_cpu(sbi->s_es->s_mnt_count)) {
5141 ext4_warning(inode->i_sb,
5142 "Unable to expand inode %lu. Delete"
5143 " some EAs or run e2fsck.",
5144 inode->i_ino);
5145 mnt_count =
5146 le16_to_cpu(sbi->s_es->s_mnt_count);
5147 }
5148 }
5149 }
5150 }
5151 return ext4_mark_iloc_dirty(handle, inode, &iloc);
5152 }
5153
5154 /*
5155 * ext4_dirty_inode() is called from __mark_inode_dirty()
5156 *
5157 * We're really interested in the case where a file is being extended.
5158 * i_size has been changed by generic_commit_write() and we thus need
5159 * to include the updated inode in the current transaction.
5160 *
5161 * Also, dquot_alloc_block() will always dirty the inode when blocks
5162 * are allocated to the file.
5163 *
5164 * If the inode is marked synchronous, we don't honour that here - doing
5165 * so would cause a commit on atime updates, which we don't bother doing.
5166 * We handle synchronous inodes at the highest possible level.
5167 */
5168 void ext4_dirty_inode(struct inode *inode, int flags)
5169 {
5170 handle_t *handle;
5171
5172 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
5173 if (IS_ERR(handle))
5174 goto out;
5175
5176 ext4_mark_inode_dirty(handle, inode);
5177
5178 ext4_journal_stop(handle);
5179 out:
5180 return;
5181 }
5182
5183 #if 0
5184 /*
5185 * Bind an inode's backing buffer_head into this transaction, to prevent
5186 * it from being flushed to disk early. Unlike
5187 * ext4_reserve_inode_write, this leaves behind no bh reference and
5188 * returns no iloc structure, so the caller needs to repeat the iloc
5189 * lookup to mark the inode dirty later.
5190 */
5191 static int ext4_pin_inode(handle_t *handle, struct inode *inode)
5192 {
5193 struct ext4_iloc iloc;
5194
5195 int err = 0;
5196 if (handle) {
5197 err = ext4_get_inode_loc(inode, &iloc);
5198 if (!err) {
5199 BUFFER_TRACE(iloc.bh, "get_write_access");
5200 err = jbd2_journal_get_write_access(handle, iloc.bh);
5201 if (!err)
5202 err = ext4_handle_dirty_metadata(handle,
5203 NULL,
5204 iloc.bh);
5205 brelse(iloc.bh);
5206 }
5207 }
5208 ext4_std_error(inode->i_sb, err);
5209 return err;
5210 }
5211 #endif
5212
5213 int ext4_change_inode_journal_flag(struct inode *inode, int val)
5214 {
5215 journal_t *journal;
5216 handle_t *handle;
5217 int err;
5218
5219 /*
5220 * We have to be very careful here: changing a data block's
5221 * journaling status dynamically is dangerous. If we write a
5222 * data block to the journal, change the status and then delete
5223 * that block, we risk forgetting to revoke the old log record
5224 * from the journal and so a subsequent replay can corrupt data.
5225 * So, first we make sure that the journal is empty and that
5226 * nobody is changing anything.
5227 */
5228
5229 journal = EXT4_JOURNAL(inode);
5230 if (!journal)
5231 return 0;
5232 if (is_journal_aborted(journal))
5233 return -EROFS;
5234 /* We have to allocate physical blocks for delalloc blocks
5235 * before flushing journal. otherwise delalloc blocks can not
5236 * be allocated any more. even more truncate on delalloc blocks
5237 * could trigger BUG by flushing delalloc blocks in journal.
5238 * There is no delalloc block in non-journal data mode.
5239 */
5240 if (val && test_opt(inode->i_sb, DELALLOC)) {
5241 err = ext4_alloc_da_blocks(inode);
5242 if (err < 0)
5243 return err;
5244 }
5245
5246 /* Wait for all existing dio workers */
5247 ext4_inode_block_unlocked_dio(inode);
5248 inode_dio_wait(inode);
5249
5250 jbd2_journal_lock_updates(journal);
5251
5252 /*
5253 * OK, there are no updates running now, and all cached data is
5254 * synced to disk. We are now in a completely consistent state
5255 * which doesn't have anything in the journal, and we know that
5256 * no filesystem updates are running, so it is safe to modify
5257 * the inode's in-core data-journaling state flag now.
5258 */
5259
5260 if (val)
5261 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
5262 else {
5263 jbd2_journal_flush(journal);
5264 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
5265 }
5266 ext4_set_aops(inode);
5267
5268 jbd2_journal_unlock_updates(journal);
5269 ext4_inode_resume_unlocked_dio(inode);
5270
5271 /* Finally we can mark the inode as dirty. */
5272
5273 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
5274 if (IS_ERR(handle))
5275 return PTR_ERR(handle);
5276
5277 err = ext4_mark_inode_dirty(handle, inode);
5278 ext4_handle_sync(handle);
5279 ext4_journal_stop(handle);
5280 ext4_std_error(inode->i_sb, err);
5281
5282 return err;
5283 }
5284
5285 static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5286 {
5287 return !buffer_mapped(bh);
5288 }
5289
5290 int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
5291 {
5292 struct page *page = vmf->page;
5293 loff_t size;
5294 unsigned long len;
5295 int ret;
5296 struct file *file = vma->vm_file;
5297 struct inode *inode = file_inode(file);
5298 struct address_space *mapping = inode->i_mapping;
5299 handle_t *handle;
5300 get_block_t *get_block;
5301 int retries = 0;
5302
5303 sb_start_pagefault(inode->i_sb);
5304 file_update_time(vma->vm_file);
5305 /* Delalloc case is easy... */
5306 if (test_opt(inode->i_sb, DELALLOC) &&
5307 !ext4_should_journal_data(inode) &&
5308 !ext4_nonda_switch(inode->i_sb)) {
5309 do {
5310 ret = __block_page_mkwrite(vma, vmf,
5311 ext4_da_get_block_prep);
5312 } while (ret == -ENOSPC &&
5313 ext4_should_retry_alloc(inode->i_sb, &retries));
5314 goto out_ret;
5315 }
5316
5317 lock_page(page);
5318 size = i_size_read(inode);
5319 /* Page got truncated from under us? */
5320 if (page->mapping != mapping || page_offset(page) > size) {
5321 unlock_page(page);
5322 ret = VM_FAULT_NOPAGE;
5323 goto out;
5324 }
5325
5326 if (page->index == size >> PAGE_CACHE_SHIFT)
5327 len = size & ~PAGE_CACHE_MASK;
5328 else
5329 len = PAGE_CACHE_SIZE;
5330 /*
5331 * Return if we have all the buffers mapped. This avoids the need to do
5332 * journal_start/journal_stop which can block and take a long time
5333 */
5334 if (page_has_buffers(page)) {
5335 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5336 0, len, NULL,
5337 ext4_bh_unmapped)) {
5338 /* Wait so that we don't change page under IO */
5339 wait_for_stable_page(page);
5340 ret = VM_FAULT_LOCKED;
5341 goto out;
5342 }
5343 }
5344 unlock_page(page);
5345 /* OK, we need to fill the hole... */
5346 if (ext4_should_dioread_nolock(inode))
5347 get_block = ext4_get_block_write;
5348 else
5349 get_block = ext4_get_block;
5350 retry_alloc:
5351 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5352 ext4_writepage_trans_blocks(inode));
5353 if (IS_ERR(handle)) {
5354 ret = VM_FAULT_SIGBUS;
5355 goto out;
5356 }
5357 ret = __block_page_mkwrite(vma, vmf, get_block);
5358 if (!ret && ext4_should_journal_data(inode)) {
5359 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
5360 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
5361 unlock_page(page);
5362 ret = VM_FAULT_SIGBUS;
5363 ext4_journal_stop(handle);
5364 goto out;
5365 }
5366 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5367 }
5368 ext4_journal_stop(handle);
5369 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
5370 goto retry_alloc;
5371 out_ret:
5372 ret = block_page_mkwrite_return(ret);
5373 out:
5374 sb_end_pagefault(inode->i_sb);
5375 return ret;
5376 }