data.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * fs/f2fs/data.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/mpage.h>
  15. #include <linux/aio.h>
  16. #include <linux/writeback.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/bio.h>
  20. #include <linux/prefetch.h>
  21. #include "f2fs.h"
  22. #include "node.h"
  23. #include "segment.h"
  24. #include "trace.h"
  25. #include <trace/events/f2fs.h>
  26. static void f2fs_read_end_io(struct bio *bio, int err)
  27. {
  28. struct bio_vec *bvec;
  29. int i;
  30. bio_for_each_segment_all(bvec, bio, i) {
  31. struct page *page = bvec->bv_page;
  32. if (!err) {
  33. SetPageUptodate(page);
  34. } else {
  35. ClearPageUptodate(page);
  36. SetPageError(page);
  37. }
  38. unlock_page(page);
  39. }
  40. bio_put(bio);
  41. }
  42. static void f2fs_write_end_io(struct bio *bio, int err)
  43. {
  44. struct f2fs_sb_info *sbi = bio->bi_private;
  45. struct bio_vec *bvec;
  46. int i;
  47. bio_for_each_segment_all(bvec, bio, i) {
  48. struct page *page = bvec->bv_page;
  49. if (unlikely(err)) {
  50. set_page_dirty(page);
  51. set_bit(AS_EIO, &page->mapping->flags);
  52. f2fs_stop_checkpoint(sbi);
  53. }
  54. end_page_writeback(page);
  55. dec_page_count(sbi, F2FS_WRITEBACK);
  56. }
  57. if (!get_pages(sbi, F2FS_WRITEBACK) &&
  58. !list_empty(&sbi->cp_wait.task_list))
  59. wake_up(&sbi->cp_wait);
  60. bio_put(bio);
  61. }
  62. /*
  63. * Low-level block read/write IO operations.
  64. */
  65. static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
  66. int npages, bool is_read)
  67. {
  68. struct bio *bio;
  69. /* No failure on bio allocation */
  70. bio = bio_alloc(GFP_NOIO, npages);
  71. bio->bi_bdev = sbi->sb->s_bdev;
  72. bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
  73. bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
  74. bio->bi_private = sbi;
  75. return bio;
  76. }
  77. static void __submit_merged_bio(struct f2fs_bio_info *io)
  78. {
  79. struct f2fs_io_info *fio = &io->fio;
  80. if (!io->bio)
  81. return;
  82. if (is_read_io(fio->rw))
  83. trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
  84. else
  85. trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
  86. submit_bio(fio->rw, io->bio);
  87. io->bio = NULL;
  88. }
  89. void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
  90. enum page_type type, int rw)
  91. {
  92. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  93. struct f2fs_bio_info *io;
  94. io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
  95. down_write(&io->io_rwsem);
  96. /* change META to META_FLUSH in the checkpoint procedure */
  97. if (type >= META_FLUSH) {
  98. io->fio.type = META_FLUSH;
  99. if (test_opt(sbi, NOBARRIER))
  100. io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
  101. else
  102. io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
  103. }
  104. __submit_merged_bio(io);
  105. up_write(&io->io_rwsem);
  106. }
  107. /*
  108. * Fill the locked page with data located in the block address.
  109. * Return unlocked page.
  110. */
  111. int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
  112. struct f2fs_io_info *fio)
  113. {
  114. struct bio *bio;
  115. trace_f2fs_submit_page_bio(page, fio);
  116. f2fs_trace_ios(page, fio, 0);
  117. /* Allocate a new bio */
  118. bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
  119. if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
  120. bio_put(bio);
  121. f2fs_put_page(page, 1);
  122. return -EFAULT;
  123. }
  124. submit_bio(fio->rw, bio);
  125. return 0;
  126. }
  127. void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
  128. struct f2fs_io_info *fio)
  129. {
  130. enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
  131. struct f2fs_bio_info *io;
  132. bool is_read = is_read_io(fio->rw);
  133. io = is_read ? &sbi->read_io : &sbi->write_io[btype];
  134. verify_block_addr(sbi, fio->blk_addr);
  135. down_write(&io->io_rwsem);
  136. if (!is_read)
  137. inc_page_count(sbi, F2FS_WRITEBACK);
  138. if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
  139. io->fio.rw != fio->rw))
  140. __submit_merged_bio(io);
  141. alloc_new:
  142. if (io->bio == NULL) {
  143. int bio_blocks = MAX_BIO_BLOCKS(sbi);
  144. io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
  145. io->fio = *fio;
  146. }
  147. if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
  148. PAGE_CACHE_SIZE) {
  149. __submit_merged_bio(io);
  150. goto alloc_new;
  151. }
  152. io->last_block_in_bio = fio->blk_addr;
  153. f2fs_trace_ios(page, fio, 0);
  154. up_write(&io->io_rwsem);
  155. trace_f2fs_submit_page_mbio(page, fio);
  156. }
  157. /*
  158. * Lock ordering for the change of data block address:
  159. * ->data_page
  160. * ->node_page
  161. * update block addresses in the node page
  162. */
  163. static void __set_data_blkaddr(struct dnode_of_data *dn)
  164. {
  165. struct f2fs_node *rn;
  166. __le32 *addr_array;
  167. struct page *node_page = dn->node_page;
  168. unsigned int ofs_in_node = dn->ofs_in_node;
  169. f2fs_wait_on_page_writeback(node_page, NODE);
  170. rn = F2FS_NODE(node_page);
  171. /* Get physical address of data block */
  172. addr_array = blkaddr_in_node(rn);
  173. addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
  174. set_page_dirty(node_page);
  175. }
  176. int reserve_new_block(struct dnode_of_data *dn)
  177. {
  178. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  179. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  180. return -EPERM;
  181. if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
  182. return -ENOSPC;
  183. trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
  184. dn->data_blkaddr = NEW_ADDR;
  185. __set_data_blkaddr(dn);
  186. mark_inode_dirty(dn->inode);
  187. sync_inode_page(dn);
  188. return 0;
  189. }
  190. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
  191. {
  192. bool need_put = dn->inode_page ? false : true;
  193. int err;
  194. err = get_dnode_of_data(dn, index, ALLOC_NODE);
  195. if (err)
  196. return err;
  197. if (dn->data_blkaddr == NULL_ADDR)
  198. err = reserve_new_block(dn);
  199. if (err || need_put)
  200. f2fs_put_dnode(dn);
  201. return err;
  202. }
  203. static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
  204. struct buffer_head *bh_result)
  205. {
  206. struct f2fs_inode_info *fi = F2FS_I(inode);
  207. pgoff_t start_fofs, end_fofs;
  208. block_t start_blkaddr;
  209. if (is_inode_flag_set(fi, FI_NO_EXTENT))
  210. return 0;
  211. read_lock(&fi->ext.ext_lock);
  212. if (fi->ext.len == 0) {
  213. read_unlock(&fi->ext.ext_lock);
  214. return 0;
  215. }
  216. stat_inc_total_hit(inode->i_sb);
  217. start_fofs = fi->ext.fofs;
  218. end_fofs = fi->ext.fofs + fi->ext.len - 1;
  219. start_blkaddr = fi->ext.blk_addr;
  220. if (pgofs >= start_fofs && pgofs <= end_fofs) {
  221. unsigned int blkbits = inode->i_sb->s_blocksize_bits;
  222. size_t count;
  223. set_buffer_new(bh_result);
  224. map_bh(bh_result, inode->i_sb,
  225. start_blkaddr + pgofs - start_fofs);
  226. count = end_fofs - pgofs + 1;
  227. if (count < (UINT_MAX >> blkbits))
  228. bh_result->b_size = (count << blkbits);
  229. else
  230. bh_result->b_size = UINT_MAX;
  231. stat_inc_read_hit(inode->i_sb);
  232. read_unlock(&fi->ext.ext_lock);
  233. return 1;
  234. }
  235. read_unlock(&fi->ext.ext_lock);
  236. return 0;
  237. }
  238. void update_extent_cache(struct dnode_of_data *dn)
  239. {
  240. struct f2fs_inode_info *fi = F2FS_I(dn->inode);
  241. pgoff_t fofs, start_fofs, end_fofs;
  242. block_t start_blkaddr, end_blkaddr;
  243. int need_update = true;
  244. f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
  245. /* Update the page address in the parent node */
  246. __set_data_blkaddr(dn);
  247. if (is_inode_flag_set(fi, FI_NO_EXTENT))
  248. return;
  249. fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
  250. dn->ofs_in_node;
  251. write_lock(&fi->ext.ext_lock);
  252. start_fofs = fi->ext.fofs;
  253. end_fofs = fi->ext.fofs + fi->ext.len - 1;
  254. start_blkaddr = fi->ext.blk_addr;
  255. end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
  256. /* Drop and initialize the matched extent */
  257. if (fi->ext.len == 1 && fofs == start_fofs)
  258. fi->ext.len = 0;
  259. /* Initial extent */
  260. if (fi->ext.len == 0) {
  261. if (dn->data_blkaddr != NULL_ADDR) {
  262. fi->ext.fofs = fofs;
  263. fi->ext.blk_addr = dn->data_blkaddr;
  264. fi->ext.len = 1;
  265. }
  266. goto end_update;
  267. }
  268. /* Front merge */
  269. if (fofs == start_fofs - 1 && dn->data_blkaddr == start_blkaddr - 1) {
  270. fi->ext.fofs--;
  271. fi->ext.blk_addr--;
  272. fi->ext.len++;
  273. goto end_update;
  274. }
  275. /* Back merge */
  276. if (fofs == end_fofs + 1 && dn->data_blkaddr == end_blkaddr + 1) {
  277. fi->ext.len++;
  278. goto end_update;
  279. }
  280. /* Split the existing extent */
  281. if (fi->ext.len > 1 &&
  282. fofs >= start_fofs && fofs <= end_fofs) {
  283. if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
  284. fi->ext.len = fofs - start_fofs;
  285. } else {
  286. fi->ext.fofs = fofs + 1;
  287. fi->ext.blk_addr = start_blkaddr +
  288. fofs - start_fofs + 1;
  289. fi->ext.len -= fofs - start_fofs + 1;
  290. }
  291. } else {
  292. need_update = false;
  293. }
  294. /* Finally, if the extent is very fragmented, let's drop the cache. */
  295. if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
  296. fi->ext.len = 0;
  297. set_inode_flag(fi, FI_NO_EXTENT);
  298. need_update = true;
  299. }
  300. end_update:
  301. write_unlock(&fi->ext.ext_lock);
  302. if (need_update)
  303. sync_inode_page(dn);
  304. return;
  305. }
  306. struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
  307. {
  308. struct address_space *mapping = inode->i_mapping;
  309. struct dnode_of_data dn;
  310. struct page *page;
  311. int err;
  312. struct f2fs_io_info fio = {
  313. .type = DATA,
  314. .rw = sync ? READ_SYNC : READA,
  315. };
  316. page = find_get_page(mapping, index);
  317. if (page && PageUptodate(page))
  318. return page;
  319. f2fs_put_page(page, 0);
  320. set_new_dnode(&dn, inode, NULL, NULL, 0);
  321. err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
  322. if (err)
  323. return ERR_PTR(err);
  324. f2fs_put_dnode(&dn);
  325. if (dn.data_blkaddr == NULL_ADDR)
  326. return ERR_PTR(-ENOENT);
  327. /* By fallocate(), there is no cached page, but with NEW_ADDR */
  328. if (unlikely(dn.data_blkaddr == NEW_ADDR))
  329. return ERR_PTR(-EINVAL);
  330. page = grab_cache_page(mapping, index);
  331. if (!page)
  332. return ERR_PTR(-ENOMEM);
  333. if (PageUptodate(page)) {
  334. unlock_page(page);
  335. return page;
  336. }
  337. fio.blk_addr = dn.data_blkaddr;
  338. err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
  339. if (err)
  340. return ERR_PTR(err);
  341. if (sync) {
  342. wait_on_page_locked(page);
  343. if (unlikely(!PageUptodate(page))) {
  344. f2fs_put_page(page, 0);
  345. return ERR_PTR(-EIO);
  346. }
  347. }
  348. return page;
  349. }
  350. /*
  351. * If it tries to access a hole, return an error.
  352. * Because, the callers, functions in dir.c and GC, should be able to know
  353. * whether this page exists or not.
  354. */
  355. struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
  356. {
  357. struct address_space *mapping = inode->i_mapping;
  358. struct dnode_of_data dn;
  359. struct page *page;
  360. int err;
  361. struct f2fs_io_info fio = {
  362. .type = DATA,
  363. .rw = READ_SYNC,
  364. };
  365. repeat:
  366. page = grab_cache_page(mapping, index);
  367. if (!page)
  368. return ERR_PTR(-ENOMEM);
  369. set_new_dnode(&dn, inode, NULL, NULL, 0);
  370. err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
  371. if (err) {
  372. f2fs_put_page(page, 1);
  373. return ERR_PTR(err);
  374. }
  375. f2fs_put_dnode(&dn);
  376. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  377. f2fs_put_page(page, 1);
  378. return ERR_PTR(-ENOENT);
  379. }
  380. if (PageUptodate(page))
  381. return page;
  382. /*
  383. * A new dentry page is allocated but not able to be written, since its
  384. * new inode page couldn't be allocated due to -ENOSPC.
  385. * In such the case, its blkaddr can be remained as NEW_ADDR.
  386. * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
  387. */
  388. if (dn.data_blkaddr == NEW_ADDR) {
  389. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  390. SetPageUptodate(page);
  391. return page;
  392. }
  393. fio.blk_addr = dn.data_blkaddr;
  394. err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
  395. if (err)
  396. return ERR_PTR(err);
  397. lock_page(page);
  398. if (unlikely(!PageUptodate(page))) {
  399. f2fs_put_page(page, 1);
  400. return ERR_PTR(-EIO);
  401. }
  402. if (unlikely(page->mapping != mapping)) {
  403. f2fs_put_page(page, 1);
  404. goto repeat;
  405. }
  406. return page;
  407. }
  408. /*
  409. * Caller ensures that this data page is never allocated.
  410. * A new zero-filled data page is allocated in the page cache.
  411. *
  412. * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
  413. * f2fs_unlock_op().
  414. * Note that, ipage is set only by make_empty_dir.
  415. */
  416. struct page *get_new_data_page(struct inode *inode,
  417. struct page *ipage, pgoff_t index, bool new_i_size)
  418. {
  419. struct address_space *mapping = inode->i_mapping;
  420. struct page *page;
  421. struct dnode_of_data dn;
  422. int err;
  423. set_new_dnode(&dn, inode, ipage, NULL, 0);
  424. err = f2fs_reserve_block(&dn, index);
  425. if (err)
  426. return ERR_PTR(err);
  427. repeat:
  428. page = grab_cache_page(mapping, index);
  429. if (!page) {
  430. err = -ENOMEM;
  431. goto put_err;
  432. }
  433. if (PageUptodate(page))
  434. return page;
  435. if (dn.data_blkaddr == NEW_ADDR) {
  436. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  437. SetPageUptodate(page);
  438. } else {
  439. struct f2fs_io_info fio = {
  440. .type = DATA,
  441. .rw = READ_SYNC,
  442. .blk_addr = dn.data_blkaddr,
  443. };
  444. err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
  445. if (err)
  446. goto put_err;
  447. lock_page(page);
  448. if (unlikely(!PageUptodate(page))) {
  449. f2fs_put_page(page, 1);
  450. err = -EIO;
  451. goto put_err;
  452. }
  453. if (unlikely(page->mapping != mapping)) {
  454. f2fs_put_page(page, 1);
  455. goto repeat;
  456. }
  457. }
  458. if (new_i_size &&
  459. i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
  460. i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
  461. /* Only the directory inode sets new_i_size */
  462. set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
  463. }
  464. return page;
  465. put_err:
  466. f2fs_put_dnode(&dn);
  467. return ERR_PTR(err);
  468. }
  469. static int __allocate_data_block(struct dnode_of_data *dn)
  470. {
  471. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  472. struct f2fs_inode_info *fi = F2FS_I(dn->inode);
  473. struct f2fs_summary sum;
  474. struct node_info ni;
  475. int seg = CURSEG_WARM_DATA;
  476. pgoff_t fofs;
  477. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  478. return -EPERM;
  479. if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
  480. return -ENOSPC;
  481. get_node_info(sbi, dn->nid, &ni);
  482. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  483. if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
  484. seg = CURSEG_DIRECT_IO;
  485. allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
  486. /* direct IO doesn't use extent cache to maximize the performance */
  487. __set_data_blkaddr(dn);
  488. /* update i_size */
  489. fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
  490. dn->ofs_in_node;
  491. if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
  492. i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
  493. return 0;
  494. }
  495. static void __allocate_data_blocks(struct inode *inode, loff_t offset,
  496. size_t count)
  497. {
  498. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  499. struct dnode_of_data dn;
  500. u64 start = F2FS_BYTES_TO_BLK(offset);
  501. u64 len = F2FS_BYTES_TO_BLK(count);
  502. bool allocated;
  503. u64 end_offset;
  504. while (len) {
  505. f2fs_balance_fs(sbi);
  506. f2fs_lock_op(sbi);
  507. /* When reading holes, we need its node page */
  508. set_new_dnode(&dn, inode, NULL, NULL, 0);
  509. if (get_dnode_of_data(&dn, start, ALLOC_NODE))
  510. goto out;
  511. allocated = false;
  512. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  513. while (dn.ofs_in_node < end_offset && len) {
  514. if (dn.data_blkaddr == NULL_ADDR) {
  515. if (__allocate_data_block(&dn))
  516. goto sync_out;
  517. allocated = true;
  518. }
  519. len--;
  520. start++;
  521. dn.ofs_in_node++;
  522. }
  523. if (allocated)
  524. sync_inode_page(&dn);
  525. f2fs_put_dnode(&dn);
  526. f2fs_unlock_op(sbi);
  527. }
  528. return;
  529. sync_out:
  530. if (allocated)
  531. sync_inode_page(&dn);
  532. f2fs_put_dnode(&dn);
  533. out:
  534. f2fs_unlock_op(sbi);
  535. return;
  536. }
  537. /*
  538. * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
  539. * If original data blocks are allocated, then give them to blockdev.
  540. * Otherwise,
  541. * a. preallocate requested block addresses
  542. * b. do not use extent cache for better performance
  543. * c. give the block addresses to blockdev
  544. */
  545. static int __get_data_block(struct inode *inode, sector_t iblock,
  546. struct buffer_head *bh_result, int create, bool fiemap)
  547. {
  548. unsigned int blkbits = inode->i_sb->s_blocksize_bits;
  549. unsigned maxblocks = bh_result->b_size >> blkbits;
  550. struct dnode_of_data dn;
  551. int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
  552. pgoff_t pgofs, end_offset;
  553. int err = 0, ofs = 1;
  554. bool allocated = false;
  555. /* Get the page offset from the block offset(iblock) */
  556. pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
  557. if (check_extent_cache(inode, pgofs, bh_result))
  558. goto out;
  559. if (create)
  560. f2fs_lock_op(F2FS_I_SB(inode));
  561. /* When reading holes, we need its node page */
  562. set_new_dnode(&dn, inode, NULL, NULL, 0);
  563. err = get_dnode_of_data(&dn, pgofs, mode);
  564. if (err) {
  565. if (err == -ENOENT)
  566. err = 0;
  567. goto unlock_out;
  568. }
  569. if (dn.data_blkaddr == NEW_ADDR && !fiemap)
  570. goto put_out;
  571. if (dn.data_blkaddr != NULL_ADDR) {
  572. set_buffer_new(bh_result);
  573. map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
  574. } else if (create) {
  575. err = __allocate_data_block(&dn);
  576. if (err)
  577. goto put_out;
  578. allocated = true;
  579. set_buffer_new(bh_result);
  580. map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
  581. } else {
  582. goto put_out;
  583. }
  584. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  585. bh_result->b_size = (((size_t)1) << blkbits);
  586. dn.ofs_in_node++;
  587. pgofs++;
  588. get_next:
  589. if (dn.ofs_in_node >= end_offset) {
  590. if (allocated)
  591. sync_inode_page(&dn);
  592. allocated = false;
  593. f2fs_put_dnode(&dn);
  594. set_new_dnode(&dn, inode, NULL, NULL, 0);
  595. err = get_dnode_of_data(&dn, pgofs, mode);
  596. if (err) {
  597. if (err == -ENOENT)
  598. err = 0;
  599. goto unlock_out;
  600. }
  601. if (dn.data_blkaddr == NEW_ADDR && !fiemap)
  602. goto put_out;
  603. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  604. }
  605. if (maxblocks > (bh_result->b_size >> blkbits)) {
  606. block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  607. if (blkaddr == NULL_ADDR && create) {
  608. err = __allocate_data_block(&dn);
  609. if (err)
  610. goto sync_out;
  611. allocated = true;
  612. blkaddr = dn.data_blkaddr;
  613. }
  614. /* Give more consecutive addresses for the readahead */
  615. if (blkaddr == (bh_result->b_blocknr + ofs)) {
  616. ofs++;
  617. dn.ofs_in_node++;
  618. pgofs++;
  619. bh_result->b_size += (((size_t)1) << blkbits);
  620. goto get_next;
  621. }
  622. }
  623. sync_out:
  624. if (allocated)
  625. sync_inode_page(&dn);
  626. put_out:
  627. f2fs_put_dnode(&dn);
  628. unlock_out:
  629. if (create)
  630. f2fs_unlock_op(F2FS_I_SB(inode));
  631. out:
  632. trace_f2fs_get_data_block(inode, iblock, bh_result, err);
  633. return err;
  634. }
  635. static int get_data_block(struct inode *inode, sector_t iblock,
  636. struct buffer_head *bh_result, int create)
  637. {
  638. return __get_data_block(inode, iblock, bh_result, create, false);
  639. }
  640. static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
  641. struct buffer_head *bh_result, int create)
  642. {
  643. return __get_data_block(inode, iblock, bh_result, create, true);
  644. }
  645. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  646. u64 start, u64 len)
  647. {
  648. return generic_block_fiemap(inode, fieinfo,
  649. start, len, get_data_block_fiemap);
  650. }
  651. static int f2fs_read_data_page(struct file *file, struct page *page)
  652. {
  653. struct inode *inode = page->mapping->host;
  654. int ret = -EAGAIN;
  655. trace_f2fs_readpage(page, DATA);
  656. /* If the file has inline data, try to read it directly */
  657. if (f2fs_has_inline_data(inode))
  658. ret = f2fs_read_inline_data(inode, page);
  659. if (ret == -EAGAIN)
  660. ret = mpage_readpage(page, get_data_block);
  661. return ret;
  662. }
  663. static int f2fs_read_data_pages(struct file *file,
  664. struct address_space *mapping,
  665. struct list_head *pages, unsigned nr_pages)
  666. {
  667. struct inode *inode = file->f_mapping->host;
  668. /* If the file has inline data, skip readpages */
  669. if (f2fs_has_inline_data(inode))
  670. return 0;
  671. return mpage_readpages(mapping, pages, nr_pages, get_data_block);
  672. }
  673. int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
  674. {
  675. struct inode *inode = page->mapping->host;
  676. struct dnode_of_data dn;
  677. int err = 0;
  678. set_new_dnode(&dn, inode, NULL, NULL, 0);
  679. err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
  680. if (err)
  681. return err;
  682. fio->blk_addr = dn.data_blkaddr;
  683. /* This page is already truncated */
  684. if (fio->blk_addr == NULL_ADDR)
  685. goto out_writepage;
  686. set_page_writeback(page);
  687. /*
  688. * If current allocation needs SSR,
  689. * it had better in-place writes for updated data.
  690. */
  691. if (unlikely(fio->blk_addr != NEW_ADDR &&
  692. !is_cold_data(page) &&
  693. need_inplace_update(inode))) {
  694. rewrite_data_page(page, fio);
  695. set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
  696. } else {
  697. write_data_page(page, &dn, fio);
  698. update_extent_cache(&dn);
  699. set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
  700. }
  701. out_writepage:
  702. f2fs_put_dnode(&dn);
  703. return err;
  704. }
  705. static int f2fs_write_data_page(struct page *page,
  706. struct writeback_control *wbc)
  707. {
  708. struct inode *inode = page->mapping->host;
  709. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  710. loff_t i_size = i_size_read(inode);
  711. const pgoff_t end_index = ((unsigned long long) i_size)
  712. >> PAGE_CACHE_SHIFT;
  713. unsigned offset = 0;
  714. bool need_balance_fs = false;
  715. int err = 0;
  716. struct f2fs_io_info fio = {
  717. .type = DATA,
  718. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  719. };
  720. trace_f2fs_writepage(page, DATA);
  721. if (page->index < end_index)
  722. goto write;
  723. /*
  724. * If the offset is out-of-range of file size,
  725. * this page does not have to be written to disk.
  726. */
  727. offset = i_size & (PAGE_CACHE_SIZE - 1);
  728. if ((page->index >= end_index + 1) || !offset)
  729. goto out;
  730. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  731. write:
  732. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  733. goto redirty_out;
  734. if (f2fs_is_drop_cache(inode))
  735. goto out;
  736. if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
  737. available_free_memory(sbi, BASE_CHECK))
  738. goto redirty_out;
  739. /* Dentry blocks are controlled by checkpoint */
  740. if (S_ISDIR(inode->i_mode)) {
  741. if (unlikely(f2fs_cp_error(sbi)))
  742. goto redirty_out;
  743. err = do_write_data_page(page, &fio);
  744. goto done;
  745. }
  746. /* we should bypass data pages to proceed the kworkder jobs */
  747. if (unlikely(f2fs_cp_error(sbi))) {
  748. SetPageError(page);
  749. goto out;
  750. }
  751. if (!wbc->for_reclaim)
  752. need_balance_fs = true;
  753. else if (has_not_enough_free_secs(sbi, 0))
  754. goto redirty_out;
  755. err = -EAGAIN;
  756. f2fs_lock_op(sbi);
  757. if (f2fs_has_inline_data(inode))
  758. err = f2fs_write_inline_data(inode, page);
  759. if (err == -EAGAIN)
  760. err = do_write_data_page(page, &fio);
  761. f2fs_unlock_op(sbi);
  762. done:
  763. if (err && err != -ENOENT)
  764. goto redirty_out;
  765. clear_cold_data(page);
  766. out:
  767. inode_dec_dirty_pages(inode);
  768. unlock_page(page);
  769. if (need_balance_fs)
  770. f2fs_balance_fs(sbi);
  771. if (wbc->for_reclaim)
  772. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  773. return 0;
  774. redirty_out:
  775. redirty_page_for_writepage(wbc, page);
  776. return AOP_WRITEPAGE_ACTIVATE;
  777. }
  778. static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
  779. void *data)
  780. {
  781. struct address_space *mapping = data;
  782. int ret = mapping->a_ops->writepage(page, wbc);
  783. mapping_set_error(mapping, ret);
  784. return ret;
  785. }
  786. static int f2fs_write_data_pages(struct address_space *mapping,
  787. struct writeback_control *wbc)
  788. {
  789. struct inode *inode = mapping->host;
  790. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  791. bool locked = false;
  792. int ret;
  793. long diff;
  794. trace_f2fs_writepages(mapping->host, wbc, DATA);
  795. /* deal with chardevs and other special file */
  796. if (!mapping->a_ops->writepage)
  797. return 0;
  798. if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
  799. get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
  800. available_free_memory(sbi, DIRTY_DENTS))
  801. goto skip_write;
  802. diff = nr_pages_to_write(sbi, DATA, wbc);
  803. if (!S_ISDIR(inode->i_mode)) {
  804. mutex_lock(&sbi->writepages);
  805. locked = true;
  806. }
  807. ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
  808. if (locked)
  809. mutex_unlock(&sbi->writepages);
  810. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  811. remove_dirty_dir_inode(inode);
  812. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  813. return ret;
  814. skip_write:
  815. wbc->pages_skipped += get_dirty_pages(inode);
  816. return 0;
  817. }
  818. static void f2fs_write_failed(struct address_space *mapping, loff_t to)
  819. {
  820. struct inode *inode = mapping->host;
  821. if (to > inode->i_size) {
  822. truncate_pagecache(inode, inode->i_size);
  823. truncate_blocks(inode, inode->i_size, true);
  824. }
  825. }
  826. static int f2fs_write_begin(struct file *file, struct address_space *mapping,
  827. loff_t pos, unsigned len, unsigned flags,
  828. struct page **pagep, void **fsdata)
  829. {
  830. struct inode *inode = mapping->host;
  831. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  832. struct page *page, *ipage;
  833. pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
  834. struct dnode_of_data dn;
  835. int err = 0;
  836. trace_f2fs_write_begin(inode, pos, len, flags);
  837. f2fs_balance_fs(sbi);
  838. /*
  839. * We should check this at this moment to avoid deadlock on inode page
  840. * and #0 page. The locking rule for inline_data conversion should be:
  841. * lock_page(page #0) -> lock_page(inode_page)
  842. */
  843. if (index != 0) {
  844. err = f2fs_convert_inline_inode(inode);
  845. if (err)
  846. goto fail;
  847. }
  848. repeat:
  849. page = grab_cache_page_write_begin(mapping, index, flags);
  850. if (!page) {
  851. err = -ENOMEM;
  852. goto fail;
  853. }
  854. *pagep = page;
  855. f2fs_lock_op(sbi);
  856. /* check inline_data */
  857. ipage = get_node_page(sbi, inode->i_ino);
  858. if (IS_ERR(ipage)) {
  859. err = PTR_ERR(ipage);
  860. goto unlock_fail;
  861. }
  862. set_new_dnode(&dn, inode, ipage, ipage, 0);
  863. if (f2fs_has_inline_data(inode)) {
  864. if (pos + len <= MAX_INLINE_DATA) {
  865. read_inline_data(page, ipage);
  866. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  867. sync_inode_page(&dn);
  868. goto put_next;
  869. }
  870. err = f2fs_convert_inline_page(&dn, page);
  871. if (err)
  872. goto put_fail;
  873. }
  874. err = f2fs_reserve_block(&dn, index);
  875. if (err)
  876. goto put_fail;
  877. put_next:
  878. f2fs_put_dnode(&dn);
  879. f2fs_unlock_op(sbi);
  880. if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
  881. return 0;
  882. f2fs_wait_on_page_writeback(page, DATA);
  883. if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
  884. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  885. unsigned end = start + len;
  886. /* Reading beyond i_size is simple: memset to zero */
  887. zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
  888. goto out;
  889. }
  890. if (dn.data_blkaddr == NEW_ADDR) {
  891. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  892. } else {
  893. struct f2fs_io_info fio = {
  894. .type = DATA,
  895. .rw = READ_SYNC,
  896. .blk_addr = dn.data_blkaddr,
  897. };
  898. err = f2fs_submit_page_bio(sbi, page, &fio);
  899. if (err)
  900. goto fail;
  901. lock_page(page);
  902. if (unlikely(!PageUptodate(page))) {
  903. f2fs_put_page(page, 1);
  904. err = -EIO;
  905. goto fail;
  906. }
  907. if (unlikely(page->mapping != mapping)) {
  908. f2fs_put_page(page, 1);
  909. goto repeat;
  910. }
  911. }
  912. out:
  913. SetPageUptodate(page);
  914. clear_cold_data(page);
  915. return 0;
  916. put_fail:
  917. f2fs_put_dnode(&dn);
  918. unlock_fail:
  919. f2fs_unlock_op(sbi);
  920. f2fs_put_page(page, 1);
  921. fail:
  922. f2fs_write_failed(mapping, pos + len);
  923. return err;
  924. }
  925. static int f2fs_write_end(struct file *file,
  926. struct address_space *mapping,
  927. loff_t pos, unsigned len, unsigned copied,
  928. struct page *page, void *fsdata)
  929. {
  930. struct inode *inode = page->mapping->host;
  931. trace_f2fs_write_end(inode, pos, len, copied);
  932. set_page_dirty(page);
  933. if (pos + copied > i_size_read(inode)) {
  934. i_size_write(inode, pos + copied);
  935. mark_inode_dirty(inode);
  936. update_inode_page(inode);
  937. }
  938. f2fs_put_page(page, 1);
  939. return copied;
  940. }
  941. static int check_direct_IO(struct inode *inode, int rw,
  942. struct iov_iter *iter, loff_t offset)
  943. {
  944. unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
  945. if (rw == READ)
  946. return 0;
  947. if (offset & blocksize_mask)
  948. return -EINVAL;
  949. if (iov_iter_alignment(iter) & blocksize_mask)
  950. return -EINVAL;
  951. return 0;
  952. }
  953. static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
  954. struct iov_iter *iter, loff_t offset)
  955. {
  956. struct file *file = iocb->ki_filp;
  957. struct address_space *mapping = file->f_mapping;
  958. struct inode *inode = mapping->host;
  959. size_t count = iov_iter_count(iter);
  960. int err;
  961. /* we don't need to use inline_data strictly */
  962. if (f2fs_has_inline_data(inode)) {
  963. err = f2fs_convert_inline_inode(inode);
  964. if (err)
  965. return err;
  966. }
  967. if (check_direct_IO(inode, rw, iter, offset))
  968. return 0;
  969. trace_f2fs_direct_IO_enter(inode, offset, count, rw);
  970. if (rw & WRITE)
  971. __allocate_data_blocks(inode, offset, count);
  972. err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
  973. if (err < 0 && (rw & WRITE))
  974. f2fs_write_failed(mapping, offset + count);
  975. trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
  976. return err;
  977. }
  978. void f2fs_invalidate_page(struct page *page, unsigned int offset,
  979. unsigned int length)
  980. {
  981. struct inode *inode = page->mapping->host;
  982. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  983. if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
  984. (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
  985. return;
  986. if (PageDirty(page)) {
  987. if (inode->i_ino == F2FS_META_INO(sbi))
  988. dec_page_count(sbi, F2FS_DIRTY_META);
  989. else if (inode->i_ino == F2FS_NODE_INO(sbi))
  990. dec_page_count(sbi, F2FS_DIRTY_NODES);
  991. else
  992. inode_dec_dirty_pages(inode);
  993. }
  994. ClearPagePrivate(page);
  995. }
  996. int f2fs_release_page(struct page *page, gfp_t wait)
  997. {
  998. /* If this is dirty page, keep PagePrivate */
  999. if (PageDirty(page))
  1000. return 0;
  1001. ClearPagePrivate(page);
  1002. return 1;
  1003. }
  1004. static int f2fs_set_data_page_dirty(struct page *page)
  1005. {
  1006. struct address_space *mapping = page->mapping;
  1007. struct inode *inode = mapping->host;
  1008. trace_f2fs_set_page_dirty(page, DATA);
  1009. SetPageUptodate(page);
  1010. if (f2fs_is_atomic_file(inode)) {
  1011. register_inmem_page(inode, page);
  1012. return 1;
  1013. }
  1014. mark_inode_dirty(inode);
  1015. if (!PageDirty(page)) {
  1016. __set_page_dirty_nobuffers(page);
  1017. update_dirty_page(inode, page);
  1018. return 1;
  1019. }
  1020. return 0;
  1021. }
  1022. static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
  1023. {
  1024. struct inode *inode = mapping->host;
  1025. /* we don't need to use inline_data strictly */
  1026. if (f2fs_has_inline_data(inode)) {
  1027. int err = f2fs_convert_inline_inode(inode);
  1028. if (err)
  1029. return err;
  1030. }
  1031. return generic_block_bmap(mapping, block, get_data_block);
  1032. }
  1033. const struct address_space_operations f2fs_dblock_aops = {
  1034. .readpage = f2fs_read_data_page,
  1035. .readpages = f2fs_read_data_pages,
  1036. .writepage = f2fs_write_data_page,
  1037. .writepages = f2fs_write_data_pages,
  1038. .write_begin = f2fs_write_begin,
  1039. .write_end = f2fs_write_end,
  1040. .set_page_dirty = f2fs_set_data_page_dirty,
  1041. .invalidatepage = f2fs_invalidate_page,
  1042. .releasepage = f2fs_release_page,
  1043. .direct_IO = f2fs_direct_IO,
  1044. .bmap = f2fs_bmap,
  1045. };